blob: a3c499cd6b620666a4b28804b4df78532a464b03 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000031#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000032#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000036#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000039#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner6831a812006-02-13 09:18:02 +000067 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000068 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000071
Mon P Wange5ab34e2009-02-04 19:38:14 +000072 /// IsLegalizingCallArguments - This member is used only for the purpose
73 /// of providing assert to check for LegalizeTypes because legalizing an
74 /// operation might introduce call nodes that might need type legalization.
75 bool IsLegalizingCallArgs;
76
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000078 Legal, // The target natively supports this operation.
79 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000080 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000081 };
Scott Michelfdc40a02009-02-17 22:15:04 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// ValueTypeActions - This is a bitvector that contains two bits for each
84 /// value type, where the two bits correspond to the LegalizeAction enum.
85 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000086 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Chris Lattner3e928bb2005-01-07 07:47:09 +000088 /// LegalizedNodes - For nodes that are of legal width, and that have more
89 /// than one use, this map indicates what regularized operand to use. This
90 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000091 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000092
Chris Lattner03c85462005-01-15 05:21:40 +000093 /// PromotedNodes - For nodes that are below legal width, and that have more
94 /// than one use, this map indicates what promoted value to use. This allows
95 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000096 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000097
Chris Lattnerc7029802006-03-18 01:44:44 +000098 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000099 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000102
Chris Lattnerc7029802006-03-18 01:44:44 +0000103 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +0000104 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +0000105 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000106 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000107
Dan Gohman7f321562007-06-25 16:23:39 +0000108 /// ScalarizedNodes - For nodes that need to be converted from vector types to
109 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000110 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000111 std::map<SDValue, SDValue> ScalarizedNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Mon P Wangf007a8b2008-11-06 05:31:54 +0000113 /// WidenNodes - For nodes that need to be widened from one vector type to
114 /// another, this contains the mapping of those that we have already widen.
115 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000116 std::map<SDValue, SDValue> WidenNodes;
117
Dan Gohman475871a2008-07-27 21:46:04 +0000118 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000119 LegalizedNodes.insert(std::make_pair(From, To));
120 // If someone requests legalization of the new node, return itself.
121 if (From != To)
122 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000123 }
Dan Gohman475871a2008-07-27 21:46:04 +0000124 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000125 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000126 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000127 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000128 // If someone requests legalization of the new node, return itself.
129 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000130 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000131 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000132 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
133 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000134 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000135 // If someone requests legalization of the new node, return itself.
136 LegalizedNodes.insert(std::make_pair(To, To));
137 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000138
Chris Lattner3e928bb2005-01-07 07:47:09 +0000139public:
Eli Friedman1fde9c52009-05-24 02:46:31 +0000140 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141
Chris Lattner3e928bb2005-01-07 07:47:09 +0000142 /// getTypeAction - Return how we should legalize values of this type, either
143 /// it is already legal or we need to expand it into multiple registers of
144 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000145 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000146 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147 }
148
149 /// isTypeLegal - Return true if this type is legal on this target.
150 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000151 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152 return getTypeAction(VT) == Legal;
153 }
154
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155 void LegalizeDAG();
156
Chris Lattner456a93a2006-01-28 07:39:30 +0000157private:
Dan Gohman7f321562007-06-25 16:23:39 +0000158 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000159 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000160 void HandleOp(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000161
Chris Lattnerc7029802006-03-18 01:44:44 +0000162 /// LegalizeOp - We know that the specified value has a legal type.
163 /// Recursively ensure that the operands have legal types, then return the
164 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000166
Dan Gohman82669522007-10-11 23:57:53 +0000167 /// UnrollVectorOp - We know that the given vector has a legal type, however
168 /// the operation it performs is not legal and is an operation that we have
169 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
170 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000171 SDValue UnrollVectorOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Nate Begeman68679912008-04-25 18:07:40 +0000173 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
174 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
175 /// is necessary to spill the vector being inserted into to memory, perform
176 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Dale Johannesenbb5da912009-02-02 20:41:04 +0000178 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000179
Chris Lattnerc7029802006-03-18 01:44:44 +0000180 /// PromoteOp - Given an operation that produces a value in an invalid type,
181 /// promote it to compute the value into a larger type. The produced value
182 /// will have the correct bits for the low portion of the register, but no
183 /// guarantee is made about the top bits: it may be zero, sign-extended, or
184 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000185 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000186
Dan Gohman475871a2008-07-27 21:46:04 +0000187 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000188 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000189 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000190 /// the ExpandedNodes map is filled in for any results that are expanded, and
191 /// the Lo/Hi values are returned. This applies to integer types and Vector
192 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000193 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000194
Scott Michelfdc40a02009-02-17 22:15:04 +0000195 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
Mon P Wangf007a8b2008-11-06 05:31:54 +0000196 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
197 /// for the existing elements but no guarantee is made about the new elements
198 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
199 /// when we have an instruction operating on an illegal vector type and we
200 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000201 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
202
Dan Gohman7f321562007-06-25 16:23:39 +0000203 /// SplitVectorOp - Given an operand of vector type, break it down into
204 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000205 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Scott Michelfdc40a02009-02-17 22:15:04 +0000206
Dan Gohman89b20c02007-06-27 14:06:22 +0000207 /// ScalarizeVectorOp - Given an operand of single-element vector type
208 /// (e.g. v1f32), convert it into the equivalent operation that returns a
209 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue ScalarizeVectorOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000211
Mon P Wangf007a8b2008-11-06 05:31:54 +0000212 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000213 typedef SmallVector<SDValue, 16> SDValueVector;
214
Mon P Wang0c397192008-10-30 08:01:45 +0000215 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
216 /// the LdChain contains a single load and false if it contains a token
217 /// factor for multiple loads. It takes
218 /// Result: location to return the result
219 /// LdChain: location to return the load chain
220 /// Op: load operation to widen
221 /// NVT: widen vector result type we want for the load
Scott Michelfdc40a02009-02-17 22:15:04 +0000222 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
Mon P Wang0c397192008-10-30 08:01:45 +0000223 SDValue Op, MVT NVT);
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Mon P Wang0c397192008-10-30 08:01:45 +0000225 /// Helper genWidenVectorLoads - Helper function to generate a set of
226 /// loads to load a vector with a resulting wider type. It takes
227 /// LdChain: list of chains for the load we have generated
228 /// Chain: incoming chain for the ld vector
229 /// BasePtr: base pointer to load from
230 /// SV: memory disambiguation source value
231 /// SVOffset: memory disambiugation offset
232 /// Alignment: alignment of the memory
233 /// isVolatile: volatile load
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 /// LdWidth: width of memory that we want to load
Mon P Wang0c397192008-10-30 08:01:45 +0000235 /// ResType: the wider result result type for the resulting loaded vector
236 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
237 SDValue BasePtr, const Value *SV,
238 int SVOffset, unsigned Alignment,
239 bool isVolatile, unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +0000240 MVT ResType, DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000241
Mon P Wang0c397192008-10-30 08:01:45 +0000242 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
243 /// location. It takes
244 /// ST: store node that we want to replace
245 /// Chain: incoming store chain
246 /// BasePtr: base address of where we want to store into
Scott Michelfdc40a02009-02-17 22:15:04 +0000247 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
Mon P Wang0c397192008-10-30 08:01:45 +0000248 SDValue BasePtr);
Scott Michelfdc40a02009-02-17 22:15:04 +0000249
Mon P Wang0c397192008-10-30 08:01:45 +0000250 /// Helper genWidenVectorStores - Helper function to generate a set of
251 /// stores to store a widen vector into non widen memory
252 // It takes
253 // StChain: list of chains for the stores we have generated
254 // Chain: incoming chain for the ld vector
255 // BasePtr: base pointer to load from
256 // SV: memory disambiguation source value
257 // SVOffset: memory disambiugation offset
258 // Alignment: alignment of the memory
259 // isVolatile: volatile lod
Scott Michelfdc40a02009-02-17 22:15:04 +0000260 // ValOp: value to store
261 // StWidth: width of memory that we want to store
Mon P Wang0c397192008-10-30 08:01:45 +0000262 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
263 SDValue BasePtr, const Value *SV,
264 int SVOffset, unsigned Alignment,
265 bool isVolatile, SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +0000266 unsigned StWidth, DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000267
Nate Begeman5a5ca152009-04-29 05:20:52 +0000268 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
269 /// performs the same shuffe in terms of order or result bytes, but on a type
270 /// whose vector element type is narrower than the original shuffle type.
271 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
272 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
273 SDValue N1, SDValue N2,
274 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000276 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000277 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000278
Dale Johannesenbb5da912009-02-02 20:41:04 +0000279 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
280 DebugLoc dl);
281 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
282 DebugLoc dl);
283 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
284 DebugLoc dl) {
285 LegalizeSetCCOperands(LHS, RHS, CC, dl);
286 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000287 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000288
Dan Gohman475871a2008-07-27 21:46:04 +0000289 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
290 SDValue &Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +0000291 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl);
Chris Lattnercad063f2005-07-16 00:19:57 +0000292
Dale Johannesen8a782a22009-02-02 22:12:50 +0000293 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000294 SDValue ExpandBUILD_VECTOR(SDNode *Node);
295 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Scott Michelfdc40a02009-02-17 22:15:04 +0000296 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +0000297 SDValue Op, DebugLoc dl);
298 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
299 DebugLoc dl);
300 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
301 DebugLoc dl);
302 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
303 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000304
Dale Johannesen8a782a22009-02-02 22:12:50 +0000305 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
306 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000307 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +0000308 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000309 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +0000310 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000311
Dan Gohman475871a2008-07-27 21:46:04 +0000312 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
313 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000314 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000315};
316}
317
Nate Begeman5a5ca152009-04-29 05:20:52 +0000318/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
319/// performs the same shuffe in terms of order or result bytes, but on a type
320/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000321/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000322SDValue
323SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
324 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000325 SmallVectorImpl<int> &Mask) const {
326 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000327 unsigned NumMaskElts = VT.getVectorNumElements();
328 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000329 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000330
Nate Begeman9008ca62009-04-27 18:41:29 +0000331 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
332
333 if (NumEltsGrowth == 1)
334 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
335
336 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000337 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000338 int Idx = Mask[i];
339 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
340 if (Idx < 0)
341 NewMask.push_back(-1);
342 else
343 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000344 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000345 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000346 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000347 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
348 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000349}
350
Bill Wendling5aa49772009-02-24 02:35:30 +0000351SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000352 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000353 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000354 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000355 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000356 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000357}
358
Chris Lattner3e928bb2005-01-07 07:47:09 +0000359void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000360 LastCALLSEQ_END = DAG.getEntryNode();
361 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000362 IsLegalizingCallArgs = false;
363
Chris Lattnerab510a72005-10-02 17:49:46 +0000364 // The legalize process is inherently a bottom-up recursive process (users
365 // legalize their uses before themselves). Given infinite stack space, we
366 // could just start legalizing on the root and traverse the whole graph. In
367 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000368 // blocks. To avoid this problem, compute an ordering of the nodes where each
369 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000370 DAG.AssignTopologicalOrder();
371 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
372 E = prior(DAG.allnodes_end()); I != next(E); ++I)
373 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000374
375 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000376 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000377 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
378 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000379
380 ExpandedNodes.clear();
381 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000382 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000383 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000384 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000385 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000386
387 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000388 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000389}
390
Chris Lattner6831a812006-02-13 09:18:02 +0000391
392/// FindCallEndFromCallStart - Given a chained node that is part of a call
393/// sequence, find the CALLSEQ_END node that terminates the call sequence.
394static SDNode *FindCallEndFromCallStart(SDNode *Node) {
395 if (Node->getOpcode() == ISD::CALLSEQ_END)
396 return Node;
397 if (Node->use_empty())
398 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000399
Chris Lattner6831a812006-02-13 09:18:02 +0000400 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000401 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000402 if (TheChain.getValueType() != MVT::Other) {
403 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000404 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000405 if (TheChain.getValueType() != MVT::Other) {
406 // Otherwise, hunt for it.
407 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
408 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000409 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000410 break;
411 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
413 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000414 if (TheChain.getValueType() != MVT::Other)
415 return 0;
416 }
417 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000418
Chris Lattner6831a812006-02-13 09:18:02 +0000419 for (SDNode::use_iterator UI = Node->use_begin(),
420 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000421
Chris Lattner6831a812006-02-13 09:18:02 +0000422 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000423 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000424 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
425 if (User->getOperand(i) == TheChain)
426 if (SDNode *Result = FindCallEndFromCallStart(User))
427 return Result;
428 }
429 return 0;
430}
431
Scott Michelfdc40a02009-02-17 22:15:04 +0000432/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000433/// sequence, find the CALLSEQ_START node that initiates the call sequence.
434static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
435 assert(Node && "Didn't find callseq_start for a call??");
436 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000437
Chris Lattner6831a812006-02-13 09:18:02 +0000438 assert(Node->getOperand(0).getValueType() == MVT::Other &&
439 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000440 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000441}
442
443/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000444/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000445/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000446///
447/// Keep track of the nodes we fine that actually do lead to Dest in
448/// NodesLeadingTo. This avoids retraversing them exponential number of times.
449///
450bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000451 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000452 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000453
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000454 // If we've already processed this node and it does lead to Dest, there is no
455 // need to reprocess it.
456 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000457
Chris Lattner6831a812006-02-13 09:18:02 +0000458 // If the first result of this node has been already legalized, then it cannot
459 // reach N.
460 switch (getTypeAction(N->getValueType(0))) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000461 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000462 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000463 break;
464 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000465 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000466 break;
467 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000468 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000469 break;
470 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Chris Lattner6831a812006-02-13 09:18:02 +0000472 // Okay, this node has not already been legalized. Check and legalize all
473 // operands. If none lead to Dest, then we can legalize this node.
474 bool OperandsLeadToDest = false;
475 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
476 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000477 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000478
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000479 if (OperandsLeadToDest) {
480 NodesLeadingTo.insert(N);
481 return true;
482 }
Chris Lattner6831a812006-02-13 09:18:02 +0000483
484 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000485 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000486 return false;
487}
488
Mon P Wang0c397192008-10-30 08:01:45 +0000489/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000490/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000491void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000492 // Don't touch TargetConstants
493 if (Op.getOpcode() == ISD::TargetConstant)
494 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000495 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000496 // We should never see any illegal result types here.
497 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Dan Gohman7f321562007-06-25 16:23:39 +0000498 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000499 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000500 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000501 case Promote:
502 if (!VT.isVector()) {
503 (void)PromoteOp(Op);
504 break;
505 }
506 else {
507 // See if we can widen otherwise use Expand to either scalarize or split
508 MVT WidenVT = TLI.getWidenVectorType(VT);
509 if (WidenVT != MVT::Other) {
510 (void) WidenVectorOp(Op, WidenVT);
511 break;
512 }
513 // else fall thru to expand since we can't widen the vector
514 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000515 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000516 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000517 // If this is an illegal scalar, expand it into its two component
518 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000519 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000520 if (Op.getOpcode() == ISD::TargetConstant)
521 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000522 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000523 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000524 // If this is an illegal single element vector, convert it to a
525 // scalar operation.
526 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000527 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000528 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000529 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000530 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000531 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000532 }
533 break;
534 }
535}
Chris Lattner6831a812006-02-13 09:18:02 +0000536
Evan Cheng9f877882006-12-13 20:57:08 +0000537/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
538/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000539static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000540 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000541 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000542 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000543
544 // If a FP immediate is precise when represented as a float and if the
545 // target can do an extending load from float to double, we put it into
546 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000547 // double. This shrinks FP constants and canonicalizes them for targets where
548 // an FP extending load is the same cost as a normal load (such as on the x87
549 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000550 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000551 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000552 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000553 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000554 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000555 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000556 }
557
Duncan Sands83ec4b62008-06-06 12:08:01 +0000558 MVT OrigVT = VT;
559 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000560 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000561 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000562 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
563 // Only do this if the target has a native EXTLOAD instruction from
564 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000565 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000566 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000567 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000568 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
569 VT = SVT;
570 Extend = true;
571 }
Evan Cheng00495212006-12-12 21:32:44 +0000572 }
573
Dan Gohman475871a2008-07-27 21:46:04 +0000574 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000575 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000576 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000577 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000578 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000579 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000580 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000581 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000582 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000583}
584
Chris Lattner6831a812006-02-13 09:18:02 +0000585
Evan Cheng912095b2007-01-04 21:56:39 +0000586/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
587/// operations.
588static
Dan Gohman475871a2008-07-27 21:46:04 +0000589SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0d137d72009-01-15 16:43:02 +0000590 SelectionDAG &DAG,
591 const TargetLowering &TLI) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000592 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000593 MVT VT = Node->getValueType(0);
594 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000595 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
596 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000597 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000598
Evan Cheng912095b2007-01-04 21:56:39 +0000599 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000600 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000601 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
602 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000603 Mask1 = DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT, Mask1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000604 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT,
Dale Johannesenbb5da912009-02-02 20:41:04 +0000605 Node->getOperand(1));
606 SignBit = DAG.getNode(ISD::AND, dl, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000607 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000608 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000609 if (SizeDiff > 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000610 SignBit = DAG.getNode(ISD::SRL, dl, SrcNVT, SignBit,
Evan Cheng912095b2007-01-04 21:56:39 +0000611 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000612 SignBit = DAG.getNode(ISD::TRUNCATE, dl, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000613 } else if (SizeDiff < 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000614 SignBit = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, SignBit);
615 SignBit = DAG.getNode(ISD::SHL, dl, NVT, SignBit,
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000616 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
617 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000618
619 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000620 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000621 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
622 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000623 Mask2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask2);
624 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
625 Result = DAG.getNode(ISD::AND, dl, NVT, Result, Mask2);
Evan Cheng068c5f42007-01-05 21:31:51 +0000626
627 // Or the value with the sign bit.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000628 Result = DAG.getNode(ISD::OR, dl, NVT, Result, SignBit);
Evan Cheng912095b2007-01-04 21:56:39 +0000629 return Result;
630}
631
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
633static
Dan Gohman475871a2008-07-27 21:46:04 +0000634SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000635 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000636 SDValue Chain = ST->getChain();
637 SDValue Ptr = ST->getBasePtr();
638 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000639 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000640 int Alignment = ST->getAlignment();
641 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000642 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000643 if (ST->getMemoryVT().isFloatingPoint() ||
644 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000645 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
646 if (TLI.isTypeLegal(intVT)) {
647 // Expand to a bitconvert of the value to the integer type of the
648 // same size, then a (misaligned) int store.
649 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000650 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
651 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000652 SVOffset, ST->isVolatile(), Alignment);
653 } else {
654 // Do a (aligned) store to a stack slot, then copy from the stack slot
655 // to the final destination using (unaligned) integer loads and stores.
656 MVT StoredVT = ST->getMemoryVT();
657 MVT RegVT =
658 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
659 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
660 unsigned RegBytes = RegVT.getSizeInBits() / 8;
661 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000662
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000663 // Make sure the stack slot is also aligned for the register type.
664 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
665
666 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000667 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000668 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000669 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
670 SmallVector<SDValue, 8> Stores;
671 unsigned Offset = 0;
672
673 // Do all but one copies using the full register width.
674 for (unsigned i = 1; i < NumRegs; i++) {
675 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000676 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000677 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000678 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000679 ST->getSrcValue(), SVOffset + Offset,
680 ST->isVolatile(),
681 MinAlign(ST->getAlignment(), Offset)));
682 // Increment the pointers.
683 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000684 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000685 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000686 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000687 }
688
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000689 // The last store may be partial. Do a truncating store. On big-endian
690 // machines this requires an extending load from the stack slot to ensure
691 // that the bits are in the right place.
692 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000693
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000694 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000695 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000696 NULL, 0, MemVT);
697
Dale Johannesenbb5da912009-02-02 20:41:04 +0000698 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000699 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000700 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000701 MinAlign(ST->getAlignment(), Offset)));
702 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000703 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000704 Stores.size());
705 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000706 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000707 assert(ST->getMemoryVT().isInteger() &&
708 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000709 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000710 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000711 MVT NewStoredVT =
712 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
713 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000714 int IncrementSize = NumBits / 8;
715
716 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
718 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000719 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000720
721 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000722 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000723 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000724 ST->getSrcValue(), SVOffset, NewStoredVT,
725 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000726 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000727 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000728 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000729 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000730 ST->getSrcValue(), SVOffset + IncrementSize,
731 NewStoredVT, ST->isVolatile(), Alignment);
732
Dale Johannesenbb5da912009-02-02 20:41:04 +0000733 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000734}
735
736/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
737static
Dan Gohman475871a2008-07-27 21:46:04 +0000738SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000739 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000740 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000741 SDValue Chain = LD->getChain();
742 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000743 MVT VT = LD->getValueType(0);
744 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000745 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000746 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000747 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
748 if (TLI.isTypeLegal(intVT)) {
749 // Expand to a (misaligned) integer load of the same size,
750 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000751 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000752 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000753 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000754 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000755 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000756 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000757
Duncan Sands05e11fa2008-12-12 21:47:02 +0000758 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000759 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000760 } else {
761 // Copy the value to a (aligned) stack slot using (unaligned) integer
762 // loads and stores, then do a (aligned) load from the stack slot.
763 MVT RegVT = TLI.getRegisterType(intVT);
764 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
765 unsigned RegBytes = RegVT.getSizeInBits() / 8;
766 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
767
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000768 // Make sure the stack slot is also aligned for the register type.
769 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
770
Duncan Sands05e11fa2008-12-12 21:47:02 +0000771 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
772 SmallVector<SDValue, 8> Stores;
773 SDValue StackPtr = StackBase;
774 unsigned Offset = 0;
775
776 // Do all but one copies using the full register width.
777 for (unsigned i = 1; i < NumRegs; i++) {
778 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000779 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000780 SVOffset + Offset, LD->isVolatile(),
781 MinAlign(LD->getAlignment(), Offset));
782 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000783 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000784 NULL, 0));
785 // Increment the pointers.
786 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000787 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
788 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000789 Increment);
790 }
791
792 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000793 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000794 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000795 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000796 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000797 MinAlign(LD->getAlignment(), Offset));
798 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000799 // On big-endian machines this requires a truncating store to ensure
800 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000801 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000802 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000803
804 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000805 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000806 Stores.size());
807
808 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000809 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000810 NULL, 0, LoadedVT);
811
812 // Callers expect a MERGE_VALUES node.
813 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000814 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000815 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000816 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000818 "Unaligned load of unsupported type.");
819
Dale Johannesen8155d642008-02-27 22:36:00 +0000820 // Compute the new VT that is half the size of the old one. This is an
821 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822 unsigned NumBits = LoadedVT.getSizeInBits();
823 MVT NewLoadedVT;
824 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000825 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000826
Chris Lattnere400af82007-11-19 21:38:03 +0000827 unsigned Alignment = LD->getAlignment();
828 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000829 ISD::LoadExtType HiExtType = LD->getExtensionType();
830
831 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
832 if (HiExtType == ISD::NON_EXTLOAD)
833 HiExtType = ISD::ZEXTLOAD;
834
835 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000837 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000838 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000839 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000840 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000841 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000842 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000843 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000844 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000845 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000846 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000847 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000848 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000849 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000850 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000851 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000852 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000853 }
854
855 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000856 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000857 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
858 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000859
Dale Johannesenbb5da912009-02-02 20:41:04 +0000860 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000861 Hi.getValue(1));
862
Dan Gohman475871a2008-07-27 21:46:04 +0000863 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000864 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000865}
Evan Cheng912095b2007-01-04 21:56:39 +0000866
Dan Gohman82669522007-10-11 23:57:53 +0000867/// UnrollVectorOp - We know that the given vector has a legal type, however
868/// the operation it performs is not legal and is an operation that we have
869/// no way of lowering. "Unroll" the vector, splitting out the scalars and
870/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000871SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000873 assert(isTypeLegal(VT) &&
874 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000875 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000876 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 unsigned NE = VT.getVectorNumElements();
878 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000879 DebugLoc dl = Op.getDebugLoc();
Dan Gohman82669522007-10-11 23:57:53 +0000880
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SmallVector<SDValue, 8> Scalars;
882 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000883 for (unsigned i = 0; i != NE; ++i) {
884 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000885 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000886 MVT OperandVT = Operand.getValueType();
887 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000888 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 MVT OperandEltVT = OperandVT.getVectorElementType();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000890 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dan Gohman82669522007-10-11 23:57:53 +0000891 OperandEltVT,
892 Operand,
893 DAG.getConstant(i, MVT::i32));
894 } else {
895 // A scalar operand; just use it as is.
896 Operands[j] = Operand;
897 }
898 }
Mon P Wange9f10152008-12-09 05:46:39 +0000899
900 switch (Op.getOpcode()) {
901 default:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000902 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT,
Mon P Wange9f10152008-12-09 05:46:39 +0000903 &Operands[0], Operands.size()));
904 break;
905 case ISD::SHL:
906 case ISD::SRA:
907 case ISD::SRL:
Duncan Sands92abc622009-01-31 15:50:11 +0000908 case ISD::ROTL:
909 case ISD::ROTR:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000910 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT, Operands[0],
Duncan Sands92abc622009-01-31 15:50:11 +0000911 DAG.getShiftAmountOperand(Operands[1])));
Mon P Wange9f10152008-12-09 05:46:39 +0000912 break;
913 }
Dan Gohman82669522007-10-11 23:57:53 +0000914 }
915
Evan Chenga87008d2009-02-25 22:49:59 +0000916 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Scalars[0], Scalars.size());
Dan Gohman82669522007-10-11 23:57:53 +0000917}
918
Duncan Sands007f9842008-01-10 10:28:30 +0000919/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000920static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000921 RTLIB::Libcall Call_F32,
922 RTLIB::Libcall Call_F64,
923 RTLIB::Libcall Call_F80,
924 RTLIB::Libcall Call_PPCF128) {
925 return
926 VT == MVT::f32 ? Call_F32 :
927 VT == MVT::f64 ? Call_F64 :
928 VT == MVT::f80 ? Call_F80 :
929 VT == MVT::ppcf128 ? Call_PPCF128 :
930 RTLIB::UNKNOWN_LIBCALL;
931}
932
Nate Begeman68679912008-04-25 18:07:40 +0000933/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
934/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
935/// is necessary to spill the vector being inserted into to memory, perform
936/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000937SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000938PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
939 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SDValue Tmp1 = Vec;
941 SDValue Tmp2 = Val;
942 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000943
Nate Begeman68679912008-04-25 18:07:40 +0000944 // If the target doesn't support this, we have to spill the input vector
945 // to a temporary stack slot, update the element, then reload it. This is
946 // badness. We could also load the value into a vector register (either
947 // with a "move to register" or "extload into register" instruction, then
948 // permute it into place, if the idx is a constant and if the idx is
949 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000950 MVT VT = Tmp1.getValueType();
951 MVT EltVT = VT.getVectorElementType();
952 MVT IdxVT = Tmp3.getValueType();
953 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000955
Gabor Greifba36cb52008-08-28 21:40:38 +0000956 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000957
958 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000959 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000960 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000961
962 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000963 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000964 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000965 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000966 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000967 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
968 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000969 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000970 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000971 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000972 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000973 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000974 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000975}
976
Mon P Wange9f10152008-12-09 05:46:39 +0000977
Dan Gohmana3466152007-07-13 20:14:11 +0000978/// LegalizeOp - We know that the specified value has a legal type, and
979/// that its operands are legal. Now ensure that the operation itself
980/// is legal, recursively ensuring that the operands' operations remain
981/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000982SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000983 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
984 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
Gabor Greifba36cb52008-08-28 21:40:38 +0000986 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000987 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000988
Eli Friedman1fde9c52009-05-24 02:46:31 +0000989 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
990 assert(getTypeAction(Node->getValueType(i)) == Legal &&
991 "Unexpected illegal type!");
992
993 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
994 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
995 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
996 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000997
Chris Lattner45982da2005-05-12 16:53:42 +0000998 // Note that LegalizeOp may be reentered even from single-use nodes, which
999 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001000 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001001 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001002
Dan Gohman475871a2008-07-27 21:46:04 +00001003 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1004 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001005 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001006
Chris Lattner3e928bb2005-01-07 07:47:09 +00001007 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001008 case ISD::FrameIndex:
1009 case ISD::EntryToken:
1010 case ISD::Register:
1011 case ISD::BasicBlock:
1012 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001013 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001014 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001015 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001016 case ISD::TargetConstantPool:
1017 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001018 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001019 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001020 case ISD::VALUETYPE:
1021 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001022 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001023 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001024 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001025 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001026 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001027 "This must be legal!");
1028 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001029 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001030 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1031 // If this is a target node, legalize it by legalizing the operands then
1032 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001033 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001034 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001035 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001036
Jay Foade3e51c02009-05-21 09:52:38 +00001037 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(), Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001038
1039 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1040 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001041 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001042 }
1043 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001044#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001045 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001046#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001047 assert(0 && "Do not know how to legalize this operator!");
1048 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001049 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001051 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001052 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001053 case ISD::ConstantPool:
1054 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001055 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1056 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001057 case TargetLowering::Custom:
1058 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001059 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001060 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001061 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001062 break;
1063 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001064 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001065 case ISD::FRAMEADDR:
1066 case ISD::RETURNADDR:
1067 // The only option for these nodes is to custom lower them. If the target
1068 // does not custom lower them, then return zero.
1069 Tmp1 = TLI.LowerOperation(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00001070 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001071 Result = Tmp1;
1072 else
1073 Result = DAG.getConstant(0, TLI.getPointerTy());
1074 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001075 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001076 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001077 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1078 default: assert(0 && "This action is not supported yet!");
1079 case TargetLowering::Custom:
1080 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001082 // Fall Thru
1083 case TargetLowering::Legal:
1084 Result = DAG.getConstant(0, VT);
1085 break;
1086 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001087 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001088 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001089 case ISD::EXCEPTIONADDR: {
1090 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001091 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001092 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1093 default: assert(0 && "This action is not supported yet!");
1094 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001095 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001096 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001097 }
1098 break;
1099 case TargetLowering::Custom:
1100 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001101 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001102 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001103 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001104 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001105 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001106 break;
1107 }
1108 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001109 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001110 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001111
Gabor Greifba36cb52008-08-28 21:40:38 +00001112 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001113 "Cannot return more than two values!");
1114
1115 // Since we produced two values, make sure to remember that we
1116 // legalized both of them.
1117 Tmp1 = LegalizeOp(Result);
1118 Tmp2 = LegalizeOp(Result.getValue(1));
1119 AddLegalizedOperand(Op.getValue(0), Tmp1);
1120 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001121 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001122 case ISD::EHSELECTION: {
1123 Tmp1 = LegalizeOp(Node->getOperand(0));
1124 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001125 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001126 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1127 default: assert(0 && "This action is not supported yet!");
1128 case TargetLowering::Expand: {
1129 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001130 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001131 }
1132 break;
1133 case TargetLowering::Custom:
1134 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001135 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001136 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001137 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001138 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001139 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +00001140 break;
1141 }
1142 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001143 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001144 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001145
Gabor Greifba36cb52008-08-28 21:40:38 +00001146 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001147 "Cannot return more than two values!");
1148
1149 // Since we produced two values, make sure to remember that we
1150 // legalized both of them.
1151 Tmp1 = LegalizeOp(Result);
1152 Tmp2 = LegalizeOp(Result.getValue(1));
1153 AddLegalizedOperand(Op.getValue(0), Tmp1);
1154 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001155 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001156 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001157 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001158 // The only "good" option for this node is to custom lower it.
1159 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1160 default: assert(0 && "This action is not supported at all!");
1161 case TargetLowering::Custom:
1162 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001163 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001164 // Fall Thru
1165 case TargetLowering::Legal:
1166 // Target does not know, how to lower this, lower to noop
1167 Result = LegalizeOp(Node->getOperand(0));
1168 break;
1169 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001170 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001171 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001172 case ISD::AssertSext:
1173 case ISD::AssertZext:
1174 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001175 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001176 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001177 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001178 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001179 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001180 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001181 case ISD::CopyFromReg:
1182 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001183 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001184 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001186 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001187 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001188 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001189 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1191 } else {
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1193 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001194 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1195 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001196 // Since CopyFromReg produces two values, make sure to remember that we
1197 // legalized both of them.
1198 AddLegalizedOperand(Op.getValue(0), Result);
1199 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001200 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001201 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001202 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001203 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001204 default: assert(0 && "This action is not supported yet!");
1205 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001206 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001207 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001208 else if (VT.isFloatingPoint())
1209 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001210 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001211 else
1212 assert(0 && "Unknown value type!");
1213 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001214 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001215 break;
1216 }
1217 break;
1218 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001219
Chris Lattner48b61a72006-03-28 00:40:33 +00001220 case ISD::INTRINSIC_W_CHAIN:
1221 case ISD::INTRINSIC_WO_CHAIN:
1222 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001223 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001224 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1225 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001226 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001227
Chris Lattner10d7fa62006-03-26 09:12:51 +00001228 // Allow the target to custom lower its intrinsics if it wants to.
Scott Michelfdc40a02009-02-17 22:15:04 +00001229 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001230 TargetLowering::Custom) {
1231 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001232 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001233 }
1234
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001236
1237 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001238 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001239 "Cannot return more than two values!");
1240
Scott Michelfdc40a02009-02-17 22:15:04 +00001241 // Since loads produce two values, make sure to remember that we
Chris Lattner13fc2f12006-03-27 20:28:29 +00001242 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001243 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1244 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001245 return Result.getValue(Op.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00001246 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001247
Dan Gohman7f460202008-06-30 20:59:49 +00001248 case ISD::DBG_STOPPOINT:
1249 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001250 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00001251
Dan Gohman7f460202008-06-30 20:59:49 +00001252 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001253 case TargetLowering::Promote:
1254 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +00001255 case TargetLowering::Expand: {
1256 DwarfWriter *DW = DAG.getDwarfWriter();
1257 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
1258 MVT::Other);
1259 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
Bill Wendling92c1e122009-02-13 02:16:35 +00001261 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
1262 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1263 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1264 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Bill Wendling92c1e122009-02-13 02:16:35 +00001266 unsigned Line = DSP->getLine();
1267 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +00001268
Bill Wendling98a366d2009-04-29 23:29:43 +00001269 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +00001270 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
1271 // won't hurt anything.
1272 if (useDEBUG_LOC) {
1273 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +00001274 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001275 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +00001276 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
1277 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001278 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +00001279 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
1280 }
Bill Wendling92c1e122009-02-13 02:16:35 +00001281 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +00001282 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +00001283 }
1284 } else {
1285 Result = Tmp1; // chain
1286 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001287 break;
Bill Wendling92c1e122009-02-13 02:16:35 +00001288 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001289 case TargetLowering::Custom:
1290 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001291 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001292 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001293 case TargetLowering::Legal: {
1294 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1295 if (Action == Legal && Tmp1 == Node->getOperand(0))
1296 break;
1297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001299 Ops.push_back(Tmp1);
1300 if (Action == Legal) {
1301 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1302 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1303 } else {
1304 // Otherwise promote them.
1305 Ops.push_back(PromoteOp(Node->getOperand(1)));
1306 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001307 }
Evan Cheng71e86852008-07-08 20:06:39 +00001308 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1309 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1310 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001311 break;
1312 }
Evan Cheng71e86852008-07-08 20:06:39 +00001313 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001314 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001315
1316 case ISD::DECLARE:
1317 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1318 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1319 default: assert(0 && "This action is not supported yet!");
1320 case TargetLowering::Legal:
1321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1323 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1325 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001326 case TargetLowering::Expand:
1327 Result = LegalizeOp(Node->getOperand(0));
1328 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001330 break;
1331
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001332 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001333 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001334 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001335 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001336 case TargetLowering::Legal: {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Eli Friedman957bffa2009-05-24 08:42:01 +00001338 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001339 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00001340 Tmp2 = Node->getOperand(1);
1341 Tmp3 = Node->getOperand(2);
1342 Tmp4 = Node->getOperand(3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001344 break;
1345 }
Evan Cheng71e86852008-07-08 20:06:39 +00001346 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001347 break;
Jim Laskeyabf6d172006-01-05 01:25:28 +00001348
Dan Gohman44066042008-07-01 00:05:16 +00001349 case ISD::DBG_LABEL:
1350 case ISD::EH_LABEL:
1351 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1352 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001353 default: assert(0 && "This action is not supported yet!");
1354 case TargetLowering::Legal:
1355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001356 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001357 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001358 case TargetLowering::Expand:
1359 Result = LegalizeOp(Node->getOperand(0));
1360 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001361 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001362 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001363
Evan Cheng27b7db52008-03-08 00:58:38 +00001364 case ISD::PREFETCH:
1365 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1366 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1367 default: assert(0 && "This action is not supported yet!");
1368 case TargetLowering::Legal:
1369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1370 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1371 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1372 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1373 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1374 break;
1375 case TargetLowering::Expand:
1376 // It's a noop.
1377 Result = LegalizeOp(Node->getOperand(0));
1378 break;
1379 }
1380 break;
1381
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001382 case ISD::MEMBARRIER: {
1383 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001384 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1385 default: assert(0 && "This action is not supported yet!");
1386 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001387 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001388 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001389 for (int x = 1; x < 6; ++x) {
1390 Ops[x] = Node->getOperand(x);
Duncan Sandse90a6152008-02-27 08:53:44 +00001391 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001392 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1393 break;
1394 }
1395 case TargetLowering::Expand:
1396 //There is no libgcc call for this op
1397 Result = Node->getOperand(0); // Noop
1398 break;
1399 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001400 break;
1401 }
1402
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001403 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001404 unsigned int num_operands = 4;
1405 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001407 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001408 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001409 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Scott Michelfdc40a02009-02-17 22:15:04 +00001410
Mon P Wang63307c32008-05-05 19:05:59 +00001411 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1412 default: assert(0 && "This action is not supported yet!");
1413 case TargetLowering::Custom:
1414 Result = TLI.LowerOperation(Result, DAG);
1415 break;
1416 case TargetLowering::Legal:
1417 break;
1418 }
Dan Gohman475871a2008-07-27 21:46:04 +00001419 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1420 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001421 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001422 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001423 case ISD::ATOMIC_LOAD_ADD:
1424 case ISD::ATOMIC_LOAD_SUB:
1425 case ISD::ATOMIC_LOAD_AND:
1426 case ISD::ATOMIC_LOAD_OR:
1427 case ISD::ATOMIC_LOAD_XOR:
1428 case ISD::ATOMIC_LOAD_NAND:
1429 case ISD::ATOMIC_LOAD_MIN:
1430 case ISD::ATOMIC_LOAD_MAX:
1431 case ISD::ATOMIC_LOAD_UMIN:
1432 case ISD::ATOMIC_LOAD_UMAX:
1433 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001434 unsigned int num_operands = 3;
1435 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001437 for (unsigned int x = 0; x < num_operands; ++x)
1438 Ops[x] = LegalizeOp(Node->getOperand(x));
1439 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001440
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001441 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001442 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001443 case TargetLowering::Custom:
1444 Result = TLI.LowerOperation(Result, DAG);
1445 break;
1446 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001447 break;
1448 }
Dan Gohman475871a2008-07-27 21:46:04 +00001449 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1450 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001451 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001452 }
Scott Michelc1513d22007-08-08 23:23:31 +00001453 case ISD::Constant: {
1454 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1455 unsigned opAction =
1456 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1457
Chris Lattner3e928bb2005-01-07 07:47:09 +00001458 // We know we don't need to expand constants here, constants only have one
1459 // value and we check that it is fine above.
1460
Scott Michelc1513d22007-08-08 23:23:31 +00001461 if (opAction == TargetLowering::Custom) {
1462 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001463 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001464 Result = Tmp1;
1465 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001466 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001467 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001468 case ISD::ConstantFP: {
1469 // Spill FP immediates to the constant pool if the target cannot directly
1470 // codegen them. Targets often have some immediate values that can be
1471 // efficiently generated into an FP register without a load. We explicitly
1472 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001473 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1474
Chris Lattner3181a772006-01-29 06:26:56 +00001475 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1476 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001477 case TargetLowering::Legal:
1478 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001479 case TargetLowering::Custom:
1480 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001481 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001482 Result = Tmp3;
1483 break;
1484 }
1485 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001486 case TargetLowering::Expand: {
1487 // Check to see if this FP immediate is already legal.
1488 bool isLegal = false;
1489 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1490 E = TLI.legal_fpimm_end(); I != E; ++I) {
1491 if (CFP->isExactlyValue(*I)) {
1492 isLegal = true;
1493 break;
1494 }
1495 }
1496 // If this is a legal constant, turn it into a TargetConstantFP node.
1497 if (isLegal)
1498 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001499 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001500 }
Nate Begemane1795842008-02-14 08:57:00 +00001501 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001502 break;
1503 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001504 case ISD::TokenFactor:
1505 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001506 Tmp1 = LegalizeOp(Node->getOperand(0));
1507 Tmp2 = LegalizeOp(Node->getOperand(1));
1508 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1509 } else if (Node->getNumOperands() == 3) {
1510 Tmp1 = LegalizeOp(Node->getOperand(0));
1511 Tmp2 = LegalizeOp(Node->getOperand(1));
1512 Tmp3 = LegalizeOp(Node->getOperand(2));
1513 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001514 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001515 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001516 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001517 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1518 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001519 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001520 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001521 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001522
Chris Lattnerfdfded52006-04-12 16:20:43 +00001523 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001524 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001525 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001526 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001528 // A call within a calling sequence must be legalized to something
1529 // other than the normal CALLSEQ_END. Violating this gets Legalize
1530 // into an infinite loop.
1531 assert ((!IsLegalizingCall ||
1532 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001533 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001534 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001535
1536 // The number of incoming and outgoing values should match; unless the final
1537 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001538 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1539 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1540 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001541 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001542 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001544 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001545 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1547 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001548 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001549 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001550 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001551 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001552 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001553 }
1554 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001555 case ISD::BUILD_VECTOR:
1556 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001557 default: assert(0 && "This action is not supported yet!");
1558 case TargetLowering::Custom:
1559 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001560 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001561 Result = Tmp3;
1562 break;
1563 }
1564 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001565 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001567 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001568 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001569 break;
1570 case ISD::INSERT_VECTOR_ELT:
1571 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001572 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001573
1574 // The type of the value to insert may not be legal, even though the vector
1575 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1576 // here.
Eli Friedman957bffa2009-05-24 08:42:01 +00001577 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2332b9f2006-03-19 01:17:20 +00001578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001579
Chris Lattner2332b9f2006-03-19 01:17:20 +00001580 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1581 Node->getValueType(0))) {
1582 default: assert(0 && "This action is not supported yet!");
1583 case TargetLowering::Legal:
1584 break;
1585 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001586 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001588 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001589 break;
1590 }
1591 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001592 case TargetLowering::Promote:
1593 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001594 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001595 // If the insert index is a constant, codegen this as a scalar_to_vector,
1596 // then a shuffle that inserts it into the right position in the vector.
1597 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001598 // SCALAR_TO_VECTOR requires that the type of the value being inserted
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001599 // match the element type of the vector being created, except for
1600 // integers in which case the inserted value can be over width.
1601 MVT EltVT = Op.getValueType().getVectorElementType();
1602 if (Tmp2.getValueType() == EltVT ||
1603 (EltVT.isInteger() && Tmp2.getValueType().bitsGE(EltVT))) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001604 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001605 Tmp1.getValueType(), Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Duncan Sands83ec4b62008-06-06 12:08:01 +00001607 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
Nate Begeman0325d902008-02-13 06:43:04 +00001608 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1609 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1610 // elt 0 of the RHS.
Nate Begeman9008ca62009-04-27 18:41:29 +00001611 SmallVector<int, 8> ShufOps;
1612 for (unsigned i = 0; i != NumElts; ++i)
1613 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
1614
1615 Result = DAG.getVectorShuffle(Tmp1.getValueType(), dl, Tmp1, ScVec,
1616 &ShufOps[0]);
Nate Begeman0325d902008-02-13 06:43:04 +00001617 Result = LegalizeOp(Result);
1618 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001619 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001620 }
Dale Johannesenbb5da912009-02-02 20:41:04 +00001621 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001622 break;
1623 }
1624 }
1625 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001626 case ISD::SCALAR_TO_VECTOR:
1627 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1628 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1629 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1630 Node->getValueType(0))) {
1631 default: assert(0 && "This action is not supported yet!");
1632 case TargetLowering::Legal:
1633 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001634 case TargetLowering::Custom:
1635 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001636 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001637 Result = Tmp3;
1638 break;
1639 }
1640 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001641 case TargetLowering::Expand:
1642 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001643 break;
1644 }
Chris Lattnerce872152006-03-19 06:31:19 +00001645 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001646 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001647 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1648 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1650 MVT VT = Result.getValueType();
1651
Nate Begeman5a5ca152009-04-29 05:20:52 +00001652 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001653 SmallVector<int, 8> Mask;
1654 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001655
1656 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001657 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001658 default: assert(0 && "Unknown operation action!");
1659 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001660 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001661 "vector shuffle should not be created if not legal!");
1662 break;
1663 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001664 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001665 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001666 Result = Tmp3;
1667 break;
1668 }
1669 // FALLTHROUGH
1670 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001671 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001672 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001673 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001674 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001675 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001676 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001677 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001678 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001679 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001680 if (Idx < NumElems)
1681 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1682 DAG.getIntPtrConstant(Idx)));
1683 else
1684 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1685 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001686 }
Evan Chenga87008d2009-02-25 22:49:59 +00001687 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001688 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001689 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001690 case TargetLowering::Promote: {
1691 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001692 MVT OVT = Node->getValueType(0);
1693 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001694
1695 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001696 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1697 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001698
Chris Lattner4352cc92006-04-04 17:23:26 +00001699 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001700 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001701 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001702 break;
1703 }
Chris Lattner87100e02006-03-20 01:52:29 +00001704 }
1705 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001706 }
Chris Lattner384504c2006-03-21 20:44:12 +00001707 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001708 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001709 Tmp2 = LegalizeOp(Node->getOperand(1));
1710 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001711 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001712 break;
1713
Scott Michelfdc40a02009-02-17 22:15:04 +00001714 case ISD::EXTRACT_SUBVECTOR:
Eli Friedman1fde9c52009-05-24 02:46:31 +00001715 Tmp1 = LegalizeOp(Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00001716 Tmp2 = LegalizeOp(Node->getOperand(1));
1717 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Eli Friedman1fde9c52009-05-24 02:46:31 +00001718
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001719 switch (TLI.getOperationAction(ISD::EXTRACT_SUBVECTOR,
1720 Node->getValueType(0))) {
1721 default: assert(0 && "Unknown operation action!");
1722 case TargetLowering::Legal:
1723 break;
1724 case TargetLowering::Custom:
1725 Tmp3 = TLI.LowerOperation(Result, DAG);
1726 if (Tmp3.getNode()) {
1727 Result = Tmp3;
1728 break;
1729 }
1730 // FALLTHROUGH
1731 case TargetLowering::Expand: {
1732 Result = ExpandExtractFromVectorThroughStack(Result);
1733 break;
1734 }
1735 }
Dan Gohman65956352007-06-13 15:12:02 +00001736 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Mon P Wang0c397192008-10-30 08:01:45 +00001738 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001739 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001740 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001741 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1742 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1743 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1744
1745 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1746 Node->getValueType(0))) {
1747 default: assert(0 && "Unknown operation action!");
1748 case TargetLowering::Legal:
1749 break;
1750 case TargetLowering::Custom:
1751 Tmp3 = TLI.LowerOperation(Result, DAG);
1752 if (Tmp3.getNode()) {
1753 Result = Tmp3;
1754 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001755 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001756 // FALLTHROUGH
1757 case TargetLowering::Expand: {
1758 // Use extract/insert/build vector for now. We might try to be
1759 // more clever later.
1760 MVT PtrVT = TLI.getPointerTy();
1761 SmallVector<SDValue, 8> Ops;
1762 unsigned NumOperands = Node->getNumOperands();
1763 for (unsigned i=0; i < NumOperands; ++i) {
1764 SDValue SubOp = Node->getOperand(i);
1765 MVT VVT = SubOp.getNode()->getValueType(0);
1766 MVT EltVT = VVT.getVectorElementType();
1767 unsigned NumSubElem = VVT.getVectorNumElements();
1768 for (unsigned j=0; j < NumSubElem; ++j) {
1769 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1770 DAG.getConstant(j, PtrVT)));
1771 }
1772 }
1773 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1774 Node->getValueType(0),
1775 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001776 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001777 }
1778 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001779 }
1780
Chris Lattner6831a812006-02-13 09:18:02 +00001781 case ISD::CALLSEQ_START: {
1782 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001783
Chris Lattner6831a812006-02-13 09:18:02 +00001784 // Recursively Legalize all of the inputs of the call end that do not lead
1785 // to this call start. This ensures that any libcalls that need be inserted
1786 // are inserted *before* the CALLSEQ_START.
Mon P Wange5ab34e2009-02-04 19:38:14 +00001787 IsLegalizingCallArgs = true;
Chris Lattner00755df2007-02-04 00:27:56 +00001788 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001789 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001790 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001791 NodesLeadingTo);
1792 }
Mon P Wange5ab34e2009-02-04 19:38:14 +00001793 IsLegalizingCallArgs = false;
Chris Lattner6831a812006-02-13 09:18:02 +00001794
1795 // Now that we legalized all of the inputs (which may have inserted
1796 // libcalls) create the new CALLSEQ_START node.
1797 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1798
1799 // Merge in the last call, to ensure that this call start after the last
1800 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001801 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001803 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001804 Tmp1 = LegalizeOp(Tmp1);
1805 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001806
Chris Lattner6831a812006-02-13 09:18:02 +00001807 // Do not try to legalize the target-specific arguments (#1+).
1808 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001810 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001811 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001812 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001813
Chris Lattner6831a812006-02-13 09:18:02 +00001814 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001815 AddLegalizedOperand(Op.getValue(0), Result);
1816 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1817 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Chris Lattner6831a812006-02-13 09:18:02 +00001819 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001821 // process, no libcalls can/will be inserted, guaranteeing that no calls
1822 // can overlap.
1823 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001824 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001825 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001826 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001827
Chris Lattner6831a812006-02-13 09:18:02 +00001828 // Legalize the call, starting from the CALLSEQ_END.
1829 LegalizeOp(LastCALLSEQ_END);
1830 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1831 return Result;
1832 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001833 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001834 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1835 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001836 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001837 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1838 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001839 assert(I != LegalizedNodes.end() &&
1840 "Legalizing the call start should have legalized this node!");
1841 return I->second;
1842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001843
1844 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001845 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001847 // Do not try to legalize the target-specific arguments (#1+), except for
1848 // an optional flag input.
1849 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1850 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001852 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001853 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001854 }
1855 } else {
1856 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1857 if (Tmp1 != Node->getOperand(0) ||
1858 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001860 Ops[0] = Tmp1;
1861 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001862 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001863 }
Chris Lattner6a542892006-01-24 05:48:21 +00001864 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001865 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001866 // This finishes up call legalization.
1867 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001868
Chris Lattner4b653a02006-02-14 00:55:02 +00001869 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001870 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001871 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001872 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001873 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001874 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001875 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001876 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1877 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1878 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001880
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001881 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001882 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001883 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001884 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001885 case TargetLowering::Expand: {
1886 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1887 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1888 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001889 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001890
1891 // Chain the dynamic stack allocation so that it doesn't modify the stack
1892 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001893 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001894
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001896 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001897 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001898 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001899 unsigned StackAlign =
1900 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1901 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001902 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001903 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001904 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001905 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001906
Dale Johannesenca57b842009-02-02 23:46:53 +00001907 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001908 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001909
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Tmp1 = LegalizeOp(Tmp1);
1911 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001912 break;
1913 }
1914 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001915 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001916 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001917 Tmp1 = LegalizeOp(Tmp3);
1918 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001919 }
Chris Lattner903d2782006-01-15 08:54:32 +00001920 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001921 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001922 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001923 }
Chris Lattner903d2782006-01-15 08:54:32 +00001924 // Since this op produce two values, make sure to remember that we
1925 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001926 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1927 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001928 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001929 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001930 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001931 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001932 bool Changed = false;
1933 // Legalize all of the operands of the inline asm, in case they are nodes
1934 // that need to be expanded or something. Note we skip the asm string and
1935 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001936 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001937 Changed = Op != Ops[0];
1938 Ops[0] = Op;
1939
1940 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1941 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00001942 unsigned NumVals = InlineAsm::
1943 getNumOperandRegisters(cast<ConstantSDNode>(Ops[i])->getZExtValue());
Chris Lattner25a022c2006-07-11 01:40:09 +00001944 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001946 if (Op != Ops[i]) {
1947 Changed = true;
1948 Ops[i] = Op;
1949 }
1950 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001951 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001952
1953 if (HasInFlag) {
1954 Op = LegalizeOp(Ops.back());
1955 Changed |= Op != Ops.back();
1956 Ops.back() = Op;
1957 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001958
Chris Lattner25a022c2006-07-11 01:40:09 +00001959 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001960 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001961
Chris Lattnerce7518c2006-01-26 22:24:51 +00001962 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1964 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001965 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001966 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001967 case ISD::BR:
1968 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001969 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001970 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001971 Tmp1 = LegalizeOp(Tmp1);
1972 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001973
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001975 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001976 case ISD::BRIND:
1977 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1978 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001979 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Nate Begeman37efe672006-04-22 18:53:45 +00001980 Tmp1 = LegalizeOp(Tmp1);
1981 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001982
Eli Friedman957bffa2009-05-24 08:42:01 +00001983 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Nate Begeman37efe672006-04-22 18:53:45 +00001984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1985 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001986 case ISD::BR_JT:
1987 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1988 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001989 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001990 Tmp1 = LegalizeOp(Tmp1);
1991 LastCALLSEQ_END = DAG.getEntryNode();
1992
1993 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1994 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1995
Scott Michelfdc40a02009-02-17 22:15:04 +00001996 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001997 default: assert(0 && "This action is not supported yet!");
1998 case TargetLowering::Legal: break;
1999 case TargetLowering::Custom:
2000 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002001 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002002 break;
2003 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SDValue Chain = Result.getOperand(0);
2005 SDValue Table = Result.getOperand(1);
2006 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002007
Duncan Sands83ec4b62008-06-06 12:08:01 +00002008 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002009 MachineFunction &MF = DAG.getMachineFunction();
2010 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00002011 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00002012 Index, DAG.getConstant(EntrySize, PTy));
2013 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002014
Duncan Sands712f7b32008-12-12 08:13:38 +00002015 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00002016 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00002017 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002018 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002019 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002020 // For PIC, the sequence is:
2021 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002022 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00002023 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00002024 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002025 }
Dale Johannesenca57b842009-02-02 23:46:53 +00002026 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002027 }
2028 }
2029 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002030 case ISD::BRCOND:
2031 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002032 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002033 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002034 Tmp1 = LegalizeOp(Tmp1);
2035 LastCALLSEQ_END = DAG.getEntryNode();
2036
Eli Friedman957bffa2009-05-24 08:42:01 +00002037 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00002038
2039 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002040 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00002041
2042 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00002043 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002044 case TargetLowering::Legal: break;
2045 case TargetLowering::Custom:
2046 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002047 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002048 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002049 case TargetLowering::Expand:
2050 // Expand brcond's setcc into its constituent parts and create a BR_CC
2051 // Node.
2052 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002053 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00002054 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00002055 Tmp2.getOperand(0), Tmp2.getOperand(1),
2056 Node->getOperand(2));
2057 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002058 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00002059 DAG.getCondCode(ISD::SETNE), Tmp2,
2060 DAG.getConstant(0, Tmp2.getValueType()),
2061 Node->getOperand(2));
2062 }
2063 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002064 }
2065 break;
2066 case ISD::BR_CC:
2067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002068 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002069 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002070 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00002072 Tmp3 = Node->getOperand(3); // RHS
2073 Tmp4 = Node->getOperand(1); // CC
2074
Scott Michelfdc40a02009-02-17 22:15:04 +00002075 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00002076 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00002077 LastCALLSEQ_END = DAG.getEntryNode();
2078
Evan Cheng7f042682008-10-15 02:05:31 +00002079 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002080 // the LHS is a legal SETCC itself. In this case, we need to compare
2081 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002082 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002083 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2084 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
2087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002088 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00002089
Chris Lattner181b7a32005-12-17 23:46:46 +00002090 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2091 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
2094 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002095 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002096 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002097 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002098 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002099 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002100 LoadSDNode *LD = cast<LoadSDNode>(Node);
2101 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2102 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002103
Evan Cheng466685d2006-10-09 20:57:25 +00002104 ISD::LoadExtType ExtType = LD->getExtensionType();
2105 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002106 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2108 Tmp3 = Result.getValue(0);
2109 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Evan Cheng466685d2006-10-09 20:57:25 +00002111 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2112 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002113 case TargetLowering::Legal:
2114 // If this is an unaligned load and the target doesn't support it,
2115 // expand it.
2116 if (!TLI.allowsUnalignedMemoryAccesses()) {
2117 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002118 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002119 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002120 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002121 TLI);
2122 Tmp3 = Result.getOperand(0);
2123 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002124 Tmp3 = LegalizeOp(Tmp3);
2125 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002126 }
2127 }
2128 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002129 case TargetLowering::Custom:
2130 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002131 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002132 Tmp3 = LegalizeOp(Tmp1);
2133 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002134 }
Evan Cheng466685d2006-10-09 20:57:25 +00002135 break;
2136 case TargetLowering::Promote: {
2137 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002139 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002141
Dale Johannesenca57b842009-02-02 23:46:53 +00002142 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002143 LD->getSrcValueOffset(),
2144 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002145 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00002146 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002147 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002148 }
Evan Cheng466685d2006-10-09 20:57:25 +00002149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002150 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00002151 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002152 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2153 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002154 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002155 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 MVT SrcVT = LD->getMemoryVT();
2157 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002158 int SVOffset = LD->getSrcValueOffset();
2159 unsigned Alignment = LD->getAlignment();
2160 bool isVolatile = LD->isVolatile();
2161
Duncan Sands83ec4b62008-06-06 12:08:01 +00002162 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002163 // Some targets pretend to have an i1 loading operation, and actually
2164 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2165 // bits are guaranteed to be zero; it helps the optimizers understand
2166 // that these bits are zero. It is also useful for EXTLOAD, since it
2167 // tells the optimizers that those bits are undefined. It would be
2168 // nice to have an effective generic way of getting these benefits...
2169 // Until such a way is found, don't insist on promoting i1 here.
2170 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002171 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002172 // Promote to a byte-sized load if not loading an integral number of
2173 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002174 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2175 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002177
2178 // The extra bits are guaranteed to be zero, since we stored them that
2179 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2180
2181 ISD::LoadExtType NewExtType =
2182 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2183
Dale Johannesenca57b842009-02-02 23:46:53 +00002184 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002185 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2186 NVT, isVolatile, Alignment);
2187
2188 Ch = Result.getValue(1); // The chain.
2189
2190 if (ExtType == ISD::SEXTLOAD)
2191 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00002192 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002193 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002194 Result, DAG.getValueType(SrcVT));
2195 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2196 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00002197 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002198 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002199 DAG.getValueType(SrcVT));
2200
2201 Tmp1 = LegalizeOp(Result);
2202 Tmp2 = LegalizeOp(Ch);
2203 } else if (SrcWidth & (SrcWidth - 1)) {
2204 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002206 "Unsupported extload!");
2207 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2208 assert(RoundWidth < SrcWidth);
2209 unsigned ExtraWidth = SrcWidth - RoundWidth;
2210 assert(ExtraWidth < RoundWidth);
2211 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2212 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002213 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2214 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002215 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002216 unsigned IncrementSize;
2217
2218 if (TLI.isLittleEndian()) {
2219 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2220 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002221 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2222 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002223 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2224 Alignment);
2225
2226 // Load the remaining ExtraWidth bits.
2227 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002228 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002229 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002230 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002231 LD->getSrcValue(), SVOffset + IncrementSize,
2232 ExtraVT, isVolatile,
2233 MinAlign(Alignment, IncrementSize));
2234
2235 // Build a factor node to remember that this load is independent of the
2236 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002237 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002238 Hi.getValue(1));
2239
2240 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002241 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002242 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2243
2244 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002245 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002246 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002247 // Big endian - avoid unaligned loads.
2248 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2249 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002250 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002251 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2252 Alignment);
2253
2254 // Load the remaining ExtraWidth bits.
2255 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002256 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002257 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002259 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002260 LD->getSrcValue(), SVOffset + IncrementSize,
2261 ExtraVT, isVolatile,
2262 MinAlign(Alignment, IncrementSize));
2263
2264 // Build a factor node to remember that this load is independent of the
2265 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002266 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002267 Hi.getValue(1));
2268
2269 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002270 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002271 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2272
2273 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002274 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002275 }
2276
2277 Tmp1 = LegalizeOp(Result);
2278 Tmp2 = LegalizeOp(Ch);
2279 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002280 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002281 default: assert(0 && "This action is not supported yet!");
2282 case TargetLowering::Custom:
2283 isCustom = true;
2284 // FALLTHROUGH
2285 case TargetLowering::Legal:
2286 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2287 Tmp1 = Result.getValue(0);
2288 Tmp2 = Result.getValue(1);
2289
2290 if (isCustom) {
2291 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002292 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002293 Tmp1 = LegalizeOp(Tmp3);
2294 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2295 }
2296 } else {
2297 // If this is an unaligned load and the target doesn't support it,
2298 // expand it.
2299 if (!TLI.allowsUnalignedMemoryAccesses()) {
2300 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002301 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002302 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002303 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002304 TLI);
2305 Tmp1 = Result.getOperand(0);
2306 Tmp2 = Result.getOperand(1);
2307 Tmp1 = LegalizeOp(Tmp1);
2308 Tmp2 = LegalizeOp(Tmp2);
2309 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002310 }
2311 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002312 break;
2313 case TargetLowering::Expand:
2314 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2315 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002316 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002317 LD->getSrcValueOffset(),
2318 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00002319 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002320 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002321 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2322 Tmp2 = LegalizeOp(Load.getValue(1));
2323 break;
2324 }
2325 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2326 // Turn the unsupported load into an EXTLOAD followed by an explicit
2327 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00002328 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002329 Tmp1, Tmp2, LD->getSrcValue(),
2330 LD->getSrcValueOffset(), SrcVT,
2331 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002332 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002333 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00002334 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2335 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002336 Result, DAG.getValueType(SrcVT));
2337 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002338 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002339 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2340 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002341 break;
2342 }
Evan Cheng466685d2006-10-09 20:57:25 +00002343 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002344
Evan Cheng466685d2006-10-09 20:57:25 +00002345 // Since loads produce two values, make sure to remember that we legalized
2346 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002347 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2348 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002349 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002350 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002351 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002352 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002353 MVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman957bffa2009-05-24 08:42:01 +00002354 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2355 // 1 -> Hi
2356 Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2357 DAG.getConstant(OpTy.getSizeInBits()/2,
2358 TLI.getShiftAmountTy()));
2359 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2360 } else {
2361 // 0 -> Lo
2362 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2363 Node->getOperand(0));
Nate Begeman5dc897b2005-10-19 00:06:56 +00002364 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002365 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002366 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002367
2368 case ISD::CopyToReg:
2369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002370
Chris Lattner7310fb12005-12-18 15:27:43 +00002371 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002372 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002373 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002375 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002376 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002377 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002378 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2380 Tmp3);
2381 } else {
2382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002384
Chris Lattner7310fb12005-12-18 15:27:43 +00002385 // Since this produces two values, make sure to remember that we legalized
2386 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002387 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2388 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002389 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002390 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002391 break;
2392
2393 case ISD::RET:
2394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002395
2396 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002397 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002398 Tmp1 = LegalizeOp(Tmp1);
2399 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Chris Lattner3e928bb2005-01-07 07:47:09 +00002401 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002402 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002403 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002404 Tmp3 = Node->getOperand(2); // Signness
Eli Friedman957bffa2009-05-24 08:42:01 +00002405 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002406 break;
2407 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002408 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002409 break;
2410 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002412 NewValues.push_back(Tmp1);
Eli Friedman957bffa2009-05-24 08:42:01 +00002413 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2414 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2415 NewValues.push_back(Node->getOperand(i+1));
2416 }
2417 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002418 break;
2419 }
2420 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002421
Eli Friedman957bffa2009-05-24 08:42:01 +00002422 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2423 default: assert(0 && "This action is not supported yet!");
2424 case TargetLowering::Legal: break;
2425 case TargetLowering::Custom:
2426 Tmp1 = TLI.LowerOperation(Result, DAG);
2427 if (Tmp1.getNode()) Result = Tmp1;
2428 break;
Evan Cheng17c428e2006-01-06 00:41:43 +00002429 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002430 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002431 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002432 StoreSDNode *ST = cast<StoreSDNode>(Node);
2433 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2434 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002435 int SVOffset = ST->getSrcValueOffset();
2436 unsigned Alignment = ST->getAlignment();
2437 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002438
Evan Cheng8b2794a2006-10-13 21:14:26 +00002439 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002440 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2441 // FIXME: We shouldn't do this for TargetConstantFP's.
2442 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2443 // to phase ordering between legalized code and the dag combiner. This
2444 // probably means that we need to integrate dag combiner and legalizer
2445 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002446 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002447 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002448 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00002449 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002450 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002451 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002452 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00002453 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002454 SVOffset, isVolatile, Alignment);
2455 break;
2456 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002457 // If this target supports 64-bit registers, do a single 64-bit store.
2458 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002459 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002460 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00002461 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002462 SVOffset, isVolatile, Alignment);
2463 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002464 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002465 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2466 // stores. If the target supports neither 32- nor 64-bits, this
2467 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002468 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002469 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2470 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002471 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002472
Dale Johannesenca57b842009-02-02 23:46:53 +00002473 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002474 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002475 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002476 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00002477 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002478 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002479
Dale Johannesenca57b842009-02-02 23:46:53 +00002480 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002481 break;
2482 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002483 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002484 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002485
Eli Friedman957bffa2009-05-24 08:42:01 +00002486 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002487 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00002488 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002489 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002490
Duncan Sands83ec4b62008-06-06 12:08:01 +00002491 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002492 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2493 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002494 case TargetLowering::Legal:
2495 // If this is an unaligned store and the target doesn't support it,
2496 // expand it.
2497 if (!TLI.allowsUnalignedMemoryAccesses()) {
2498 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002499 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002500 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002501 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002502 TLI);
2503 }
2504 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002505 case TargetLowering::Custom:
2506 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002507 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002508 break;
2509 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002510 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002511 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002512 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00002513 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002514 ST->getSrcValue(), SVOffset, isVolatile,
2515 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002516 break;
2517 }
2518 break;
2519 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002520 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00002521 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002522
Duncan Sands83ec4b62008-06-06 12:08:01 +00002523 MVT StVT = ST->getMemoryVT();
2524 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002525
Duncan Sands83ec4b62008-06-06 12:08:01 +00002526 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002527 // Promote to a byte-sized store with upper bits zero if not
2528 // storing an integral number of bytes. For example, promote
2529 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002530 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00002531 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
2532 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002533 SVOffset, NVT, isVolatile, Alignment);
2534 } else if (StWidth & (StWidth - 1)) {
2535 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002536 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002537 "Unsupported truncstore!");
2538 unsigned RoundWidth = 1 << Log2_32(StWidth);
2539 assert(RoundWidth < StWidth);
2540 unsigned ExtraWidth = StWidth - RoundWidth;
2541 assert(ExtraWidth < RoundWidth);
2542 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2543 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2545 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002546 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002547 unsigned IncrementSize;
2548
2549 if (TLI.isLittleEndian()) {
2550 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2551 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002552 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002553 SVOffset, RoundVT,
2554 isVolatile, Alignment);
2555
2556 // Store the remaining ExtraWidth bits.
2557 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002558 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002559 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002560 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002561 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002562 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002563 SVOffset + IncrementSize, ExtraVT, isVolatile,
2564 MinAlign(Alignment, IncrementSize));
2565 } else {
2566 // Big endian - avoid unaligned stores.
2567 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2568 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002569 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002570 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002571 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
2572 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002573
2574 // Store the remaining ExtraWidth bits.
2575 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002576 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002577 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002578 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002579 SVOffset + IncrementSize, ExtraVT, isVolatile,
2580 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002581 }
Duncan Sands7e857202008-01-22 07:17:34 +00002582
2583 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00002584 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00002585 } else {
2586 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2587 Tmp2 != ST->getBasePtr())
2588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2589 ST->getOffset());
2590
2591 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2592 default: assert(0 && "This action is not supported yet!");
2593 case TargetLowering::Legal:
2594 // If this is an unaligned store and the target doesn't support it,
2595 // expand it.
2596 if (!TLI.allowsUnalignedMemoryAccesses()) {
2597 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002598 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002599 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002600 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002601 TLI);
2602 }
2603 break;
2604 case TargetLowering::Custom:
2605 Result = TLI.LowerOperation(Result, DAG);
2606 break;
2607 case Expand:
2608 // TRUNCSTORE:i16 i32 -> STORE i16
2609 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002610 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
2611 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
2612 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002613 break;
2614 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002615 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002616 }
2617 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002618 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002619 case ISD::PCMARKER:
2620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002622 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002623 case ISD::STACKSAVE:
2624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002625 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2626 Tmp1 = Result.getValue(0);
2627 Tmp2 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002628
Chris Lattner140d53c2006-01-13 02:50:02 +00002629 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2630 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002631 case TargetLowering::Legal: break;
2632 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002633 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002634 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002635 Tmp1 = LegalizeOp(Tmp3);
2636 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002637 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002638 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002639 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00002640 // Expand to CopyFromReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002641 // StackPointerRegisterToSaveRestore.
2642 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002643 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002644 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002645 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002646 } else {
Dale Johannesene8d72302009-02-06 23:05:02 +00002647 Tmp1 = DAG.getUNDEF(Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002648 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002649 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002650 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002651 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002652
2653 // Since stacksave produce two values, make sure to remember that we
2654 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002655 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2656 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002657 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002658
Chris Lattner140d53c2006-01-13 02:50:02 +00002659 case ISD::STACKRESTORE:
2660 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2661 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002662 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Chris Lattner140d53c2006-01-13 02:50:02 +00002664 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2665 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002666 case TargetLowering::Legal: break;
2667 case TargetLowering::Custom:
2668 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002669 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002670 break;
2671 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00002672 // Expand to CopyToReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002673 // StackPointerRegisterToSaveRestore.
2674 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002675 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002676 } else {
2677 Result = Tmp1;
2678 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002679 break;
2680 }
2681 break;
2682
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002683 case ISD::READCYCLECOUNTER:
2684 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002685 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002686 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2687 Node->getValueType(0))) {
2688 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002689 case TargetLowering::Legal:
2690 Tmp1 = Result.getValue(0);
2691 Tmp2 = Result.getValue(1);
2692 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002693 case TargetLowering::Custom:
2694 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002695 Tmp1 = LegalizeOp(Result.getValue(0));
2696 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002697 break;
2698 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002699
2700 // Since rdcc produce two values, make sure to remember that we legalized
2701 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002702 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2703 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002704 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002705
Chris Lattner2ee743f2005-01-14 22:08:15 +00002706 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00002707 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00002708 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002709 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002710
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00002712
Nate Begemanb942a3d2005-08-23 04:29:48 +00002713 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002714 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002715 case TargetLowering::Legal: break;
2716 case TargetLowering::Custom: {
2717 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002718 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002719 break;
2720 }
Nate Begeman9373a812005-08-10 20:51:12 +00002721 case TargetLowering::Expand:
2722 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002723 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00002724 Tmp2, Tmp3,
2725 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2726 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002727 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00002728 DAG.getConstant(0, Tmp1.getValueType()),
2729 Tmp2, Tmp3, ISD::SETNE);
2730 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002731 break;
2732 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002733 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002734 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2735 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002736 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002737 ExtOp = ISD::BIT_CONVERT;
2738 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002739 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002740 ExtOp = ISD::ANY_EXTEND;
2741 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002742 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002743 ExtOp = ISD::FP_EXTEND;
2744 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002745 }
2746 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002747 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
2748 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002749 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002750 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002751 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00002752 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00002753 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002754 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00002755 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002756 break;
2757 }
2758 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002759 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002760 case ISD::SELECT_CC: {
2761 Tmp1 = Node->getOperand(0); // LHS
2762 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002763 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2764 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002765 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00002766
2767 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00002768 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002769
Evan Cheng7f042682008-10-15 02:05:31 +00002770 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002771 // the LHS is a legal SETCC itself. In this case, we need to compare
2772 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002773 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002774 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2775 CC = DAG.getCondCode(ISD::SETNE);
2776 }
2777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2778
2779 // Everything is legal, see if we should expand this op or something.
2780 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2781 default: assert(0 && "This action is not supported yet!");
2782 case TargetLowering::Legal: break;
2783 case TargetLowering::Custom:
2784 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002785 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002786 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002787 }
2788 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002789 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002790 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002791 Tmp1 = Node->getOperand(0);
2792 Tmp2 = Node->getOperand(1);
2793 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00002794 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002795
2796 // If we had to Expand the SetCC operands into a SELECT node, then it may
2797 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00002798 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002799 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002800 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002801 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002802 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002803
Chris Lattner73e142f2006-01-30 22:43:50 +00002804 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002805 default: assert(0 && "Cannot handle this action for SETCC yet!");
2806 case TargetLowering::Custom:
2807 isCustom = true;
2808 // FALLTHROUGH.
2809 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002811 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002812 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002813 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002814 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002815 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002816 case TargetLowering::Promote: {
2817 // First step, figure out the appropriate operation to use.
2818 // Allow SETCC to not be supported for all legal data types
2819 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 MVT NewInTy = Node->getOperand(0).getValueType();
2821 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002822
2823 // Scan for the appropriate larger type to use.
2824 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002825 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002826
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002828 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002829 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002830 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002831
Andrew Lenharthae355752005-11-30 17:12:26 +00002832 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002833 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002834 break;
2835 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002836 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002837 assert(0 && "Cannot promote Legal Integer SETCC yet");
2838 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002839 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2840 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002841 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002842 Tmp1 = LegalizeOp(Tmp1);
2843 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002844 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002845 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002846 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002847 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002848 case TargetLowering::Expand:
2849 // Expand a setcc node into a select_cc of the same condition, lhs, and
2850 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002851 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002852 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002853 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002854 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002855 break;
2856 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002857 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00002858 case ISD::VSETCC: {
2859 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2860 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00002861 SDValue CC = Node->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002862
Nate Begemanb43e9c12008-05-12 19:40:03 +00002863 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2864
2865 // Everything is legal, see if we should expand this op or something.
2866 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2867 default: assert(0 && "This action is not supported yet!");
2868 case TargetLowering::Legal: break;
2869 case TargetLowering::Custom:
2870 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002871 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00002872 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002873 case TargetLowering::Expand: {
2874 // Unroll into a nasty set of scalar code for now.
2875 MVT VT = Node->getValueType(0);
2876 unsigned NumElems = VT.getVectorNumElements();
2877 MVT EltVT = VT.getVectorElementType();
2878 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
2879 SmallVector<SDValue, 8> Ops(NumElems);
2880 for (unsigned i = 0; i < NumElems; ++i) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002881 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002882 Tmp1, DAG.getIntPtrConstant(i));
Dale Johannesenca57b842009-02-02 23:46:53 +00002883 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
2884 In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2885 TmpEltVT, Tmp2,
2886 DAG.getIntPtrConstant(i)),
Mon P Wang84aff842008-12-17 08:49:47 +00002887 CC);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002888 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
2889 DAG.getConstant(APInt::getAllOnesValue
2890 (EltVT.getSizeInBits()), EltVT),
2891 DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002892 }
Evan Chenga87008d2009-02-25 22:49:59 +00002893 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002894 break;
2895 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00002896 }
2897 break;
2898 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002899
Chris Lattner5b359c62005-04-02 04:00:59 +00002900 case ISD::SHL_PARTS:
2901 case ISD::SRA_PARTS:
2902 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00002903 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002904 bool Changed = false;
Duncan Sands92abc622009-01-31 15:50:11 +00002905 unsigned N = Node->getNumOperands();
2906 for (unsigned i = 0; i + 1 < N; ++i) {
Chris Lattner84f67882005-01-20 18:52:28 +00002907 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2908 Changed |= Ops.back() != Node->getOperand(i);
2909 }
Duncan Sands92abc622009-01-31 15:50:11 +00002910 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
2911 Changed |= Ops.back() != Node->getOperand(N-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002912 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002913 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002914
Evan Cheng05a2d562006-01-09 18:31:59 +00002915 switch (TLI.getOperationAction(Node->getOpcode(),
2916 Node->getValueType(0))) {
2917 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002918 case TargetLowering::Legal: break;
2919 case TargetLowering::Custom:
2920 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002921 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002923 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002924 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00002925 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002926 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00002927 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002928 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002929 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002930 return RetVal;
2931 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002932 break;
2933 }
2934
Chris Lattner2c8086f2005-04-02 05:00:07 +00002935 // Since these produce multiple values, make sure to remember that we
2936 // legalized all of them.
2937 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00002938 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00002939 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00002940 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002941
2942 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002943 case ISD::ADD:
2944 case ISD::SUB:
2945 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002946 case ISD::MULHS:
2947 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002948 case ISD::UDIV:
2949 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002950 case ISD::AND:
2951 case ISD::OR:
2952 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002953 case ISD::SHL:
2954 case ISD::SRL:
2955 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002956 case ISD::FADD:
2957 case ISD::FSUB:
2958 case ISD::FMUL:
2959 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002960 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002961 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002962 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002963
2964 if ((Node->getOpcode() == ISD::SHL ||
2965 Node->getOpcode() == ISD::SRL ||
2966 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00002967 !Node->getValueType(0).isVector())
2968 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
2969
Eli Friedman957bffa2009-05-24 08:42:01 +00002970 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002971
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002973
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002974 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002975 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002976 case TargetLowering::Legal: break;
2977 case TargetLowering::Custom:
2978 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002979 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002980 Result = Tmp1;
2981 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002982 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002983 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002984 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002985 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002986
Dan Gohman525178c2007-10-08 18:33:35 +00002987 // See if multiply or divide can be lowered using two-result operations.
2988 SDVTList VTs = DAG.getVTList(VT, VT);
2989 if (Node->getOpcode() == ISD::MUL) {
2990 // We just need the low half of the multiply; try both the signed
2991 // and unsigned forms. If the target supports both SMUL_LOHI and
2992 // UMUL_LOHI, form a preference by checking which forms of plain
2993 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002994 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2995 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2996 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2997 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002998 unsigned OpToUse = 0;
2999 if (HasSMUL_LOHI && !HasMULHS) {
3000 OpToUse = ISD::SMUL_LOHI;
3001 } else if (HasUMUL_LOHI && !HasMULHU) {
3002 OpToUse = ISD::UMUL_LOHI;
3003 } else if (HasSMUL_LOHI) {
3004 OpToUse = ISD::SMUL_LOHI;
3005 } else if (HasUMUL_LOHI) {
3006 OpToUse = ISD::UMUL_LOHI;
3007 }
3008 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00003009 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00003010 break;
3011 }
3012 }
3013 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003014 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003015 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00003016 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003017 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003018 break;
3019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003020 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003021 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003022 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
3023 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003024 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003025 break;
3026 }
3027 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003028 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00003029 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00003030 break;
3031 }
3032 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003033 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00003034 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
3035 break;
3036 }
3037 if (Node->getOpcode() == ISD::SUB &&
3038 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3039 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
3040 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
3041 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
3042 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3043 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00003044 break;
3045 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003046
Dan Gohman82669522007-10-11 23:57:53 +00003047 // Check to see if we have a libcall for this operator.
3048 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3049 bool isSigned = false;
3050 switch (Node->getOpcode()) {
3051 case ISD::UDIV:
3052 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00003053 isSigned = Node->getOpcode() == ISD::SDIV;
3054 if (VT == MVT::i16)
3055 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
3056 else if (VT == MVT::i32)
3057 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
3058 else if (VT == MVT::i64)
3059 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
3060 else if (VT == MVT::i128)
3061 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
3062 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003063 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00003064 if (VT == MVT::i16)
3065 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00003066 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00003067 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00003068 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00003069 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00003070 else if (VT == MVT::i128)
3071 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00003072 break;
Dan Gohman82669522007-10-11 23:57:53 +00003073 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003074 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3075 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003076 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003077 case ISD::FDIV:
3078 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3079 RTLIB::DIV_PPCF128);
3080 break;
Dan Gohman82669522007-10-11 23:57:53 +00003081 default: break;
3082 }
3083 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003085 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003086 break;
3087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003088
Duncan Sands83ec4b62008-06-06 12:08:01 +00003089 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003090 "Cannot expand this binary operator!");
3091 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003092 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003093 break;
3094 }
Evan Chengcc987612006-04-12 21:20:24 +00003095 case TargetLowering::Promote: {
3096 switch (Node->getOpcode()) {
3097 default: assert(0 && "Do not know how to promote this BinOp!");
3098 case ISD::AND:
3099 case ISD::OR:
3100 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003101 MVT OVT = Node->getValueType(0);
3102 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3103 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003104 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003105 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
3106 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
3107 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00003108 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003109 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00003110 break;
3111 }
3112 }
3113 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003114 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003115 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003116
Dan Gohmane14ea862007-10-05 14:17:22 +00003117 case ISD::SMUL_LOHI:
3118 case ISD::UMUL_LOHI:
3119 case ISD::SDIVREM:
3120 case ISD::UDIVREM:
3121 // These nodes will only be produced by target-specific lowering, so
3122 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003123 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003124 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003125
3126 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3127 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003129 break;
3130
Chris Lattnera09f8482006-03-05 05:09:38 +00003131 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3132 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00003133 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00003134
Chris Lattnera09f8482006-03-05 05:09:38 +00003135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00003136
Chris Lattnera09f8482006-03-05 05:09:38 +00003137 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3138 default: assert(0 && "Operation not supported");
3139 case TargetLowering::Custom:
3140 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003141 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003142 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003143 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003144 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003145 // If this target supports fabs/fneg natively and select is cheap,
3146 // do this efficiently.
3147 if (!TLI.isSelectExpensive() &&
3148 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3149 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003150 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003151 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003152 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003153 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003154 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dale Johannesenca57b842009-02-02 23:46:53 +00003155 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
3156 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003157 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3158 // Get the absolute value of the result.
Dale Johannesenca57b842009-02-02 23:46:53 +00003159 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003160 // Select between the nabs and abs value based on the sign bit of
3161 // the input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003162 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
Scott Michelfdc40a02009-02-17 22:15:04 +00003163 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003164 AbsVal),
3165 AbsVal);
3166 Result = LegalizeOp(Result);
3167 break;
3168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003169
Chris Lattner8f4191d2006-03-13 06:08:38 +00003170 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003171 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003172 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3173 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
Dale Johannesenca57b842009-02-02 23:46:53 +00003174 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), Result);
Evan Cheng912095b2007-01-04 21:56:39 +00003175 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003176 break;
3177 }
Evan Cheng912095b2007-01-04 21:56:39 +00003178 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003179 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003180
Nate Begeman551bf3f2006-02-17 05:43:56 +00003181 case ISD::ADDC:
3182 case ISD::SUBC:
3183 Tmp1 = LegalizeOp(Node->getOperand(0));
3184 Tmp2 = LegalizeOp(Node->getOperand(1));
3185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003186 Tmp3 = Result.getValue(0);
3187 Tmp4 = Result.getValue(1);
3188
3189 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3190 default: assert(0 && "This action is not supported yet!");
3191 case TargetLowering::Legal:
3192 break;
3193 case TargetLowering::Custom:
3194 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3195 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003196 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003197 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3198 }
3199 break;
3200 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003201 // Since this produces two values, make sure to remember that we legalized
3202 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003203 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3204 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3205 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003206
Nate Begeman551bf3f2006-02-17 05:43:56 +00003207 case ISD::ADDE:
3208 case ISD::SUBE:
3209 Tmp1 = LegalizeOp(Node->getOperand(0));
3210 Tmp2 = LegalizeOp(Node->getOperand(1));
3211 Tmp3 = LegalizeOp(Node->getOperand(2));
3212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003213 Tmp3 = Result.getValue(0);
3214 Tmp4 = Result.getValue(1);
3215
3216 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3217 default: assert(0 && "This action is not supported yet!");
3218 case TargetLowering::Legal:
3219 break;
3220 case TargetLowering::Custom:
3221 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3222 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003223 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003224 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3225 }
3226 break;
3227 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003228 // Since this produces two values, make sure to remember that we legalized
3229 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003230 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3231 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3232 return Op.getResNo() ? Tmp4 : Tmp3;
Scott Michelfdc40a02009-02-17 22:15:04 +00003233
Nate Begeman419f8b62005-10-18 00:27:41 +00003234 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003236 // TODO: handle the case where the Lo and Hi operands are not of legal type
3237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3238 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3239 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003240 case TargetLowering::Promote:
3241 case TargetLowering::Custom:
3242 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003243 case TargetLowering::Legal:
3244 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00003245 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003246 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003247 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00003248 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
3249 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
3250 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003251 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003252 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00003253 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003254 break;
3255 }
3256 break;
3257 }
3258
Nate Begemanc105e192005-04-06 00:23:54 +00003259 case ISD::UREM:
3260 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003261 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003262 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3263 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003264
Nate Begemanc105e192005-04-06 00:23:54 +00003265 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003266 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3267 case TargetLowering::Custom:
3268 isCustom = true;
3269 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003270 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003271 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003272 if (isCustom) {
3273 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003274 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003275 }
Nate Begemanc105e192005-04-06 00:23:54 +00003276 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003277 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003278 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003279 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003280 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003281
Dan Gohman525178c2007-10-08 18:33:35 +00003282 // See if remainder can be lowered using two-result operations.
3283 SDVTList VTs = DAG.getVTList(VT, VT);
3284 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003285 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003286 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00003287 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003288 break;
3289 }
3290 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003291 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003292 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3293 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003294 break;
3295 }
3296
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00003297 if (VT.isInteger() &&
3298 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
3299 // X % Y -> X-X/Y*Y
3300 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3301 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3302 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
3303 break;
Nate Begemanc105e192005-04-06 00:23:54 +00003304 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00003305
3306 // Check to see if we have a libcall for this operator.
3307 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3308 switch (Node->getOpcode()) {
3309 default: break;
3310 case ISD::UREM:
3311 case ISD::SREM:
3312 if (VT == MVT::i16)
3313 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
3314 else if (VT == MVT::i32)
3315 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
3316 else if (VT == MVT::i64)
3317 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
3318 else if (VT == MVT::i128)
3319 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
3320 break;
3321 case ISD::FREM:
3322 // Floating point mod -> fmod libcall.
3323 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3324 RTLIB::REM_F80, RTLIB::REM_PPCF128);
3325 break;
3326 }
3327
3328 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3329 SDValue Dummy;
3330 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
3331 break;
3332 }
3333
3334 assert(VT.isVector() &&
3335 "Cannot expand this binary operator!");
3336 // Expand the operation into a bunch of nasty scalar code.
3337 Result = LegalizeOp(UnrollVectorOp(Op));
Nate Begemanc105e192005-04-06 00:23:54 +00003338 break;
3339 }
Dan Gohman525178c2007-10-08 18:33:35 +00003340 }
Nate Begemanc105e192005-04-06 00:23:54 +00003341 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003342 case ISD::VAARG: {
3343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3344 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3345
Duncan Sands83ec4b62008-06-06 12:08:01 +00003346 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003347 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3348 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003349 case TargetLowering::Custom:
3350 isCustom = true;
3351 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003352 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3354 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003355 Tmp1 = Result.getValue(1);
3356
3357 if (isCustom) {
3358 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003359 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003360 Result = LegalizeOp(Tmp2);
3361 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3362 }
3363 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003364 break;
3365 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003366 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003367 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003368 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00003369 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003370 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00003371 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003372 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003373 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00003374 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003375 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00003376 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003377 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003378 Result = LegalizeOp(Result);
3379 break;
3380 }
3381 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003382 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00003383 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003384 AddLegalizedOperand(SDValue(Node, 0), Result);
3385 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003386 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003388
3389 case ISD::VACOPY:
Nate Begemanacc398c2006-01-25 18:21:52 +00003390 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3391 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3392 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3393
3394 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3395 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003396 case TargetLowering::Custom:
3397 isCustom = true;
3398 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003399 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003400 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3401 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003402 if (isCustom) {
3403 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003404 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003405 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003406 break;
3407 case TargetLowering::Expand:
3408 // This defaults to loading a pointer from the input and storing it to the
3409 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003410 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3411 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003412 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3413 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003414 break;
3415 }
3416 break;
3417
Scott Michelfdc40a02009-02-17 22:15:04 +00003418 case ISD::VAEND:
Nate Begemanacc398c2006-01-25 18:21:52 +00003419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3420 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3421
3422 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3423 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003424 case TargetLowering::Custom:
3425 isCustom = true;
3426 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003427 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003429 if (isCustom) {
3430 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003431 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003432 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003433 break;
3434 case TargetLowering::Expand:
3435 Result = Tmp1; // Default to a no-op, return the chain
3436 break;
3437 }
3438 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003439
3440 case ISD::VASTART:
Nate Begemanacc398c2006-01-25 18:21:52 +00003441 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3442 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3443
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003445
Nate Begemanacc398c2006-01-25 18:21:52 +00003446 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3447 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003448 case TargetLowering::Legal: break;
3449 case TargetLowering::Custom:
3450 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003451 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003452 break;
3453 }
3454 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003455
Nate Begeman35ef9132006-01-11 21:21:00 +00003456 case ISD::ROTL:
3457 case ISD::ROTR:
3458 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003459 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003460 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003461 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3462 default:
3463 assert(0 && "ROTL/ROTR legalize operation not supported");
3464 break;
3465 case TargetLowering::Legal:
3466 break;
3467 case TargetLowering::Custom:
3468 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003469 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003470 break;
3471 case TargetLowering::Promote:
3472 assert(0 && "Do not know how to promote ROTL/ROTR");
3473 break;
3474 case TargetLowering::Expand:
3475 assert(0 && "Do not know how to expand ROTL/ROTR");
3476 break;
3477 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003478 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003479
Nate Begemand88fc032006-01-14 03:14:10 +00003480 case ISD::BSWAP:
3481 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3482 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003483 case TargetLowering::Custom:
3484 assert(0 && "Cannot custom legalize this yet!");
3485 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003486 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003487 break;
3488 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003489 MVT OVT = Tmp1.getValueType();
3490 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3491 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003492
Dale Johannesenca57b842009-02-02 23:46:53 +00003493 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3494 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3495 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Chris Lattner456a93a2006-01-28 07:39:30 +00003496 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3497 break;
3498 }
3499 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003500 Result = ExpandBSWAP(Tmp1, dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003502 }
3503 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003504
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003505 case ISD::CTPOP:
3506 case ISD::CTTZ:
3507 case ISD::CTLZ:
3508 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3509 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003510 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003511 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003512 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003513 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003514 TargetLowering::Custom) {
3515 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003516 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003517 Result = Tmp1;
3518 }
Scott Michel910b66d2007-07-30 21:00:31 +00003519 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003520 break;
3521 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003522 MVT OVT = Tmp1.getValueType();
3523 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003524
3525 // Zero extend the argument.
Dale Johannesenca57b842009-02-02 23:46:53 +00003526 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003527 // Perform the larger operation, then subtract if needed.
Dale Johannesenca57b842009-02-02 23:46:53 +00003528 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003529 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003530 case ISD::CTPOP:
3531 Result = Tmp1;
3532 break;
3533 case ISD::CTTZ:
3534 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenca57b842009-02-02 23:46:53 +00003535 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3536 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003537 ISD::SETEQ);
Dale Johannesenca57b842009-02-02 23:46:53 +00003538 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003539 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003540 break;
3541 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003542 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenca57b842009-02-02 23:46:53 +00003543 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 DAG.getConstant(NVT.getSizeInBits() -
3545 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003546 break;
3547 }
3548 break;
3549 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003550 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003551 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003552 break;
3553 }
3554 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003555
Chris Lattner2c8086f2005-04-02 05:00:07 +00003556 // Unary operators
3557 case ISD::FABS:
3558 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003559 case ISD::FSQRT:
3560 case ISD::FSIN:
3561 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003562 case ISD::FLOG:
3563 case ISD::FLOG2:
3564 case ISD::FLOG10:
3565 case ISD::FEXP:
3566 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003567 case ISD::FTRUNC:
3568 case ISD::FFLOOR:
3569 case ISD::FCEIL:
3570 case ISD::FRINT:
3571 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003572 Tmp1 = LegalizeOp(Node->getOperand(0));
3573 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003574 case TargetLowering::Promote:
3575 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003576 isCustom = true;
3577 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003578 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003579 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003580 if (isCustom) {
3581 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003582 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003583 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003584 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003585 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003586 switch (Node->getOpcode()) {
3587 default: assert(0 && "Unreachable!");
3588 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003589 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3590 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00003591 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003592 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003593 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003594 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003595 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003596 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003597 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003598 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003599 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3600 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003601 break;
3602 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003603 case ISD::FSQRT:
3604 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00003605 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003606 case ISD::FLOG:
3607 case ISD::FLOG2:
3608 case ISD::FLOG10:
3609 case ISD::FEXP:
3610 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003611 case ISD::FTRUNC:
3612 case ISD::FFLOOR:
3613 case ISD::FCEIL:
3614 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003615 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003616 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003617
3618 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003619 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003620 Result = LegalizeOp(UnrollVectorOp(Op));
3621 break;
3622 }
3623
Evan Cheng56966222007-01-12 02:11:51 +00003624 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003625 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003626 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003627 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3628 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003629 break;
3630 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003631 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3632 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003633 break;
3634 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003635 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3636 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003637 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003638 case ISD::FLOG:
3639 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3640 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3641 break;
3642 case ISD::FLOG2:
3643 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3644 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3645 break;
3646 case ISD::FLOG10:
3647 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3648 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3649 break;
3650 case ISD::FEXP:
3651 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3652 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3653 break;
3654 case ISD::FEXP2:
3655 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3656 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3657 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003658 case ISD::FTRUNC:
3659 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3660 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3661 break;
3662 case ISD::FFLOOR:
3663 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3664 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3665 break;
3666 case ISD::FCEIL:
3667 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3668 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3669 break;
3670 case ISD::FRINT:
3671 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3672 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3673 break;
3674 case ISD::FNEARBYINT:
3675 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3676 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3677 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003678 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003679 default: assert(0 && "Unreachable!");
3680 }
Dan Gohman475871a2008-07-27 21:46:04 +00003681 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003682 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003683 break;
3684 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003685 }
3686 break;
3687 }
3688 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003689 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003691
3692 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003693 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003694 Result = LegalizeOp(UnrollVectorOp(Op));
3695 break;
3696 }
3697
3698 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003699 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3700 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003701 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003702 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003703 break;
3704 }
Chris Lattner35481892005-12-23 00:16:34 +00003705 case ISD::BIT_CONVERT:
Eli Friedman1fde9c52009-05-24 02:46:31 +00003706 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3707 Node->getOperand(0).getValueType())) {
3708 default: assert(0 && "Unknown operation action!");
3709 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003710 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003711 Node->getValueType(0), dl);
Eli Friedman1fde9c52009-05-24 02:46:31 +00003712 break;
3713 case TargetLowering::Legal:
3714 Tmp1 = LegalizeOp(Node->getOperand(0));
3715 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3716 break;
Chris Lattner35481892005-12-23 00:16:34 +00003717 }
3718 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003719 case ISD::CONVERT_RNDSAT: {
3720 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3721 switch (CvtCode) {
3722 default: assert(0 && "Unknown cvt code!");
3723 case ISD::CVT_SF:
3724 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003725 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003726 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003727 case ISD::CVT_FS:
3728 case ISD::CVT_FU:
3729 case ISD::CVT_SS:
3730 case ISD::CVT_SU:
3731 case ISD::CVT_US:
3732 case ISD::CVT_UU: {
3733 SDValue DTyOp = Node->getOperand(1);
3734 SDValue STyOp = Node->getOperand(2);
3735 SDValue RndOp = Node->getOperand(3);
3736 SDValue SatOp = Node->getOperand(4);
Eli Friedman957bffa2009-05-24 08:42:01 +00003737 Tmp1 = LegalizeOp(Node->getOperand(0));
3738 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3739 RndOp, SatOp);
3740 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3741 TargetLowering::Custom) {
3742 Tmp1 = TLI.LowerOperation(Result, DAG);
3743 if (Tmp1.getNode()) Result = Tmp1;
Mon P Wang77cdf302008-11-10 20:54:11 +00003744 }
3745 break;
3746 }
3747 } // end switch CvtCode
3748 break;
3749 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003750 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003751 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003752 case ISD::UINT_TO_FP: {
3753 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003754 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00003755 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003756 break;
3757 }
3758 case ISD::TRUNCATE:
Eli Friedman957bffa2009-05-24 08:42:01 +00003759 Tmp1 = LegalizeOp(Node->getOperand(0));
3760 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3761 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3762 case TargetLowering::Custom:
3763 isCustom = true;
3764 // FALLTHROUGH
3765 case TargetLowering::Legal:
3766 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3767 if (isCustom) {
3768 Tmp1 = TLI.LowerOperation(Result, DAG);
3769 if (Tmp1.getNode()) Result = Tmp1;
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00003770 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003771 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003772 case TargetLowering::Expand:
3773 assert(Result.getValueType().isVector() && "must be vector type");
3774 // Unroll the truncate. We should do better.
3775 Result = LegalizeOp(UnrollVectorOp(Result));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003776 break;
3777 }
3778 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003779
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003780 case ISD::FP_TO_SINT:
3781 case ISD::FP_TO_UINT:
Eli Friedman957bffa2009-05-24 08:42:01 +00003782 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003783
Eli Friedman957bffa2009-05-24 08:42:01 +00003784 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3785 default: assert(0 && "Unknown operation action!");
3786 case TargetLowering::Custom:
3787 isCustom = true;
3788 // FALLTHROUGH
3789 case TargetLowering::Legal:
3790 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3791 if (isCustom) {
3792 Tmp1 = TLI.LowerOperation(Result, DAG);
3793 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner07dffd62005-08-26 00:14:16 +00003794 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003795 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003796 case TargetLowering::Promote:
3797 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3798 Node->getOpcode() == ISD::FP_TO_SINT,
3799 dl);
Evan Cheng6af00d52006-12-13 01:57:55 +00003800 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003801 case TargetLowering::Expand:
3802 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3803 SDValue True, False;
3804 MVT VT = Node->getOperand(0).getValueType();
3805 MVT NVT = Node->getValueType(0);
3806 const uint64_t zero[] = {0, 0};
3807 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3808 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3809 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3810 Tmp2 = DAG.getConstantFP(apf, VT);
3811 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3812 Node->getOperand(0),
3813 Tmp2, ISD::SETLT);
3814 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3815 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3816 DAG.getNode(ISD::FSUB, dl, VT,
3817 Node->getOperand(0), Tmp2));
3818 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3819 DAG.getConstant(x, NVT));
3820 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
3821 } else {
3822 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3823 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003824 break;
3825 }
3826 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003827
Chris Lattnerf2670a82008-01-16 06:57:07 +00003828 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 MVT DstVT = Op.getValueType();
3830 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003831 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3832 // The only other way we can lower this is to turn it into a STORE,
3833 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003834 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003835 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003836 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003837 Tmp1 = LegalizeOp(Node->getOperand(0));
3838 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003839 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003840 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003841 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003842 MVT DstVT = Op.getValueType();
3843 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003844 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3845 if (SrcVT == MVT::ppcf128) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00003846 // FIXME: Figure out how to extract the double without
3847 // help from type legalization
Dale Johannesen5411a392007-08-09 01:04:01 +00003848 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003849 // The only other way we can lower this is to turn it into a STORE,
3850 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003851 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003852 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003853 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003854 Tmp1 = LegalizeOp(Node->getOperand(0));
3855 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003856 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003857 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003858 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003859 case ISD::ZERO_EXTEND:
3860 case ISD::SIGN_EXTEND:
Eli Friedman957bffa2009-05-24 08:42:01 +00003861 Tmp1 = LegalizeOp(Node->getOperand(0));
3862 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3863 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3864 TargetLowering::Custom) {
3865 Tmp1 = TLI.LowerOperation(Result, DAG);
3866 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003867 }
3868 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003869 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003870 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003871 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003873
3874 // If this operation is not supported, convert it to a shl/shr or load/store
3875 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003876 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3877 default: assert(0 && "This action not supported for this op yet!");
3878 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003880 break;
3881 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003882 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003883 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003884 // NOTE: we could fall back on load/store here too for targets without
3885 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003886 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3887 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003888 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenca57b842009-02-02 23:46:53 +00003889 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003890 Node->getOperand(0), ShiftCst);
Dale Johannesenca57b842009-02-02 23:46:53 +00003891 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003892 Result, ShiftCst);
3893 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003894 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003895 // EXTLOAD pair, targetting a temporary location (a stack slot).
3896
3897 // NOTE: there is a choice here between constantly creating new stack
3898 // slots and always reusing the same one. We currently always create
3899 // new ones, as reuse may inhibit scheduling.
Scott Michelfdc40a02009-02-17 22:15:04 +00003900 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003901 Node->getValueType(0), dl);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003902 } else {
3903 assert(0 && "Unknown op");
3904 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003905 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003906 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003907 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003908 }
Duncan Sands36397f52007-07-27 12:58:54 +00003909 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00003910 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00003911 for (unsigned i = 0; i != 6; ++i)
3912 Ops[i] = LegalizeOp(Node->getOperand(i));
3913 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3914 // The only option for this node is to custom lower it.
3915 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003916 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003917
3918 // Since trampoline produces two values, make sure to remember that we
3919 // legalized both of them.
3920 Tmp1 = LegalizeOp(Result.getValue(1));
3921 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00003922 AddLegalizedOperand(SDValue(Node, 0), Result);
3923 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003924 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003925 }
Dan Gohman9c78a392008-05-14 00:43:10 +00003926 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003927 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003928 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3929 default: assert(0 && "This action not supported for this op yet!");
3930 case TargetLowering::Custom:
3931 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003932 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003933 // Fall Thru
3934 case TargetLowering::Legal:
3935 // If this operation is not supported, lower it to constant 1
3936 Result = DAG.getConstant(1, VT);
3937 break;
3938 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00003939 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003940 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003941 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003942 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003943 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3944 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003945 case TargetLowering::Legal:
3946 Tmp1 = LegalizeOp(Node->getOperand(0));
3947 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3948 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003949 case TargetLowering::Custom:
3950 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003951 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003952 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003953 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003954 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003955 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003956 TargetLowering::ArgListTy Args;
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003957 std::pair<SDValue, SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00003958 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003959 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003960 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003961 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003962 Result = CallResult.second;
3963 break;
3964 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003965 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003966 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003967
Bill Wendling74c37652008-12-09 22:08:41 +00003968 case ISD::SADDO:
3969 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003970 MVT VT = Node->getValueType(0);
3971 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3972 default: assert(0 && "This action not supported for this op yet!");
3973 case TargetLowering::Custom:
3974 Result = TLI.LowerOperation(Op, DAG);
3975 if (Result.getNode()) break;
3976 // FALLTHROUGH
3977 case TargetLowering::Legal: {
3978 SDValue LHS = LegalizeOp(Node->getOperand(0));
3979 SDValue RHS = LegalizeOp(Node->getOperand(1));
3980
Scott Michelfdc40a02009-02-17 22:15:04 +00003981 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00003982 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00003983 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003984 MVT OType = Node->getValueType(1);
3985
Bill Wendlinga6af91a2008-11-25 08:19:22 +00003986 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003987
Bill Wendling740464e2008-11-25 19:40:17 +00003988 // LHSSign -> LHS >= 0
3989 // RHSSign -> RHS >= 0
3990 // SumSign -> Sum >= 0
3991 //
Bill Wendling74c37652008-12-09 22:08:41 +00003992 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00003993 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00003994 // Sub:
3995 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00003996 //
Dale Johannesenca57b842009-02-02 23:46:53 +00003997 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3998 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00003999 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
4000 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00004001 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004002
Dale Johannesenca57b842009-02-02 23:46:53 +00004003 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
4004 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004005
Dale Johannesenca57b842009-02-02 23:46:53 +00004006 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004007
4008 MVT ValueVTs[] = { LHS.getValueType(), OType };
4009 SDValue Ops[] = { Sum, Cmp };
4010
Scott Michelfdc40a02009-02-17 22:15:04 +00004011 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00004012 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004013 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004014 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00004015 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004016 break;
4017 }
4018 }
4019
4020 break;
4021 }
Bill Wendling74c37652008-12-09 22:08:41 +00004022 case ISD::UADDO:
4023 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004024 MVT VT = Node->getValueType(0);
4025 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4026 default: assert(0 && "This action not supported for this op yet!");
4027 case TargetLowering::Custom:
4028 Result = TLI.LowerOperation(Op, DAG);
4029 if (Result.getNode()) break;
4030 // FALLTHROUGH
4031 case TargetLowering::Legal: {
4032 SDValue LHS = LegalizeOp(Node->getOperand(0));
4033 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004034
Bill Wendling74c37652008-12-09 22:08:41 +00004035 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004036 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004037 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004038 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00004039 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00004040 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00004041 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004042
Bill Wendling41ea7e72008-11-24 19:21:46 +00004043 MVT ValueVTs[] = { LHS.getValueType(), OType };
4044 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004045
Scott Michelfdc40a02009-02-17 22:15:04 +00004046 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00004047 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004048 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004049 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00004050 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004051 break;
4052 }
4053 }
4054
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004055 break;
4056 }
Bill Wendling74c37652008-12-09 22:08:41 +00004057 case ISD::SMULO:
4058 case ISD::UMULO: {
4059 MVT VT = Node->getValueType(0);
4060 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4061 default: assert(0 && "This action is not supported at all!");
4062 case TargetLowering::Custom:
4063 Result = TLI.LowerOperation(Op, DAG);
4064 if (Result.getNode()) break;
4065 // Fall Thru
4066 case TargetLowering::Legal:
4067 // FIXME: According to Hacker's Delight, this can be implemented in
4068 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004069 // requires a division + a branch.
Scott Michelfdc40a02009-02-17 22:15:04 +00004070 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
Bill Wendling74c37652008-12-09 22:08:41 +00004071 break;
4072 }
4073 break;
4074 }
4075
Chris Lattner45b8caf2005-01-15 07:15:18 +00004076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004077
Chris Lattner4ddd2832006-04-08 04:13:17 +00004078 assert(Result.getValueType() == Op.getValueType() &&
4079 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004080
Chris Lattner456a93a2006-01-28 07:39:30 +00004081 // Make sure that the generated code is itself legal.
4082 if (Result != Op)
4083 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004084
Chris Lattner45982da2005-05-12 16:53:42 +00004085 // Note that LegalizeOp may be reentered even from single-use nodes, which
4086 // means that we always must cache transformed nodes.
4087 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004088 return Result;
4089}
4090
Chris Lattner8b6fa222005-01-15 22:16:26 +00004091/// PromoteOp - Given an operation that produces a value in an invalid type,
4092/// promote it to compute the value into a larger type. The produced value will
4093/// have the correct bits for the low portion of the register, but no guarantee
4094/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004095SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00004096 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004097 MVT VT = Op.getValueType();
4098 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004099 assert(getTypeAction(VT) == Promote &&
4100 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004101 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004102 "Cannot promote to smaller type!");
4103
Dan Gohman475871a2008-07-27 21:46:04 +00004104 SDValue Tmp1, Tmp2, Tmp3;
4105 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004106 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004107 DebugLoc dl = Node->getDebugLoc();
Chris Lattner03c85462005-01-15 05:21:40 +00004108
Dan Gohman475871a2008-07-27 21:46:04 +00004109 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004110 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004111
Chris Lattner03c85462005-01-15 05:21:40 +00004112 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004113 case ISD::CopyFromReg:
4114 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004115 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004116#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004117 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004118#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004119 assert(0 && "Do not know how to promote this operator!");
4120 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004121 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00004122 Result = DAG.getUNDEF(NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004123 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004124 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004125 if (VT != MVT::i1)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004126 Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
Chris Lattnerec176e32005-08-30 16:56:19 +00004127 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004128 Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004129 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4130 break;
4131 case ISD::ConstantFP:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004132 Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004133 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4134 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004135
Duncan Sands5480c042009-01-01 15:52:00 +00004136 case ISD::SETCC: {
4137 MVT VT0 = Node->getOperand(0).getValueType();
4138 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004139 && "SetCC type is not legal??");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004140 Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004141 Node->getOperand(0), Node->getOperand(1),
4142 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004143 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004144 }
Chris Lattner03c85462005-01-15 05:21:40 +00004145 case ISD::TRUNCATE:
4146 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4147 case Legal:
4148 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004149 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004150 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004151 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dale Johannesen8a782a22009-02-02 22:12:50 +00004152 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
Chris Lattner03c85462005-01-15 05:21:40 +00004153 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004154 case Promote:
4155 // The truncation is not required, because we don't guarantee anything
4156 // about high bits anyway.
4157 Result = PromoteOp(Node->getOperand(0));
4158 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004159 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004160 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4161 // Truncate the low part of the expanded value to the result type
Dale Johannesen8a782a22009-02-02 22:12:50 +00004162 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004163 }
4164 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004165 case ISD::SIGN_EXTEND:
4166 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004167 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4169 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4170 case Legal:
4171 // Input is legal? Just do extend all the way to the larger type.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004172 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004173 break;
4174 case Promote:
4175 // Promote the reg if it's smaller.
4176 Result = PromoteOp(Node->getOperand(0));
4177 // The high bits are not guaranteed to be anything. Insert an extend.
4178 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004179 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004180 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004181 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004182 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004183 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004184 break;
4185 }
4186 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004187 case ISD::CONVERT_RNDSAT: {
4188 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4189 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4190 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4191 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4192 "can only promote integers");
Dale Johannesenc460ae92009-02-04 00:13:36 +00004193 Result = DAG.getConvertRndSat(NVT, dl, Node->getOperand(0),
Mon P Wang77cdf302008-11-10 20:54:11 +00004194 Node->getOperand(1), Node->getOperand(2),
4195 Node->getOperand(3), Node->getOperand(4),
4196 CvtCode);
4197 break;
4198
4199 }
Chris Lattner35481892005-12-23 00:16:34 +00004200 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004201 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004202 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00004203 Result = PromoteOp(Result);
4204 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004205
Chris Lattner8b6fa222005-01-15 22:16:26 +00004206 case ISD::FP_EXTEND:
4207 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4208 case ISD::FP_ROUND:
4209 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4210 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4211 case Promote: assert(0 && "Unreachable with 2 FP types!");
4212 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004213 if (Node->getConstantOperandVal(1) == 0) {
4214 // Input is legal? Do an FP_ROUND_INREG.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004215 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004216 DAG.getValueType(VT));
4217 } else {
4218 // Just remove the truncate, it isn't affecting the value.
Scott Michelfdc40a02009-02-17 22:15:04 +00004219 Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004220 Node->getOperand(1));
4221 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004222 break;
4223 }
4224 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004225 case ISD::SINT_TO_FP:
4226 case ISD::UINT_TO_FP:
4227 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4228 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004229 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004230 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004231 break;
4232
4233 case Promote:
4234 Result = PromoteOp(Node->getOperand(0));
4235 if (Node->getOpcode() == ISD::SINT_TO_FP)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004236 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004237 Result,
4238 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004239 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004240 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004241 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004242 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004243 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004244 break;
4245 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004246 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004247 Node->getOperand(0), dl);
Chris Lattner77e77a62005-01-21 06:05:23 +00004248 // Round if we cannot tolerate excess precision.
4249 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004250 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004251 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004252 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004253 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004254 break;
4255
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004256 case ISD::SIGN_EXTEND_INREG:
4257 Result = PromoteOp(Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004258 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004259 Node->getOperand(1));
4260 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004261 case ISD::FP_TO_SINT:
4262 case ISD::FP_TO_UINT:
4263 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4264 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004265 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004266 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004267 break;
4268 case Promote:
4269 // The input result is prerounded, so we don't have to do anything
4270 // special.
4271 Tmp1 = PromoteOp(Node->getOperand(0));
4272 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004273 }
Nate Begemand2558e32005-08-14 01:20:53 +00004274 // If we're promoting a UINT to a larger size, check to see if the new node
4275 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4276 // we can use that instead. This allows us to generate better code for
4277 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4278 // legal, such as PowerPC.
Scott Michelfdc40a02009-02-17 22:15:04 +00004279 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004280 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4281 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004282 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Dale Johannesen8a782a22009-02-02 22:12:50 +00004283 Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004284 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004285 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004286 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004287 break;
4288
Chris Lattner2c8086f2005-04-02 05:00:07 +00004289 case ISD::FABS:
4290 case ISD::FNEG:
4291 Tmp1 = PromoteOp(Node->getOperand(0));
4292 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004293 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00004294 // NOTE: we do not have to do any extra rounding here for
4295 // NoExcessFPPrecision, because we know the input will have the appropriate
4296 // precision, and these operations don't modify precision at all.
4297 break;
4298
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004299 case ISD::FLOG:
4300 case ISD::FLOG2:
4301 case ISD::FLOG10:
4302 case ISD::FEXP:
4303 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004304 case ISD::FSQRT:
4305 case ISD::FSIN:
4306 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004307 case ISD::FTRUNC:
4308 case ISD::FFLOOR:
4309 case ISD::FCEIL:
4310 case ISD::FRINT:
4311 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004312 Tmp1 = PromoteOp(Node->getOperand(0));
4313 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004314 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004315 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004316 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004317 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004318 break;
4319
Evan Cheng9d24ac52008-09-09 23:35:53 +00004320 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004321 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004322 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004323 // directly as well, which may be better.
4324 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004325 Tmp2 = Node->getOperand(1);
4326 if (Node->getOpcode() == ISD::FPOW)
4327 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004328 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004329 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004330 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004331 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004332 DAG.getValueType(VT));
4333 break;
4334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004335
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004336 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004337 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004338 Tmp2 = PromoteOp(Node->getOperand(2));
4339 Tmp3 = PromoteOp(Node->getOperand(3));
Scott Michelfdc40a02009-02-17 22:15:04 +00004340 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
4341 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004342 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004343 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004344 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004345 // Remember that we legalized the chain.
4346 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4347 break;
4348 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004349 case ISD::ATOMIC_LOAD_ADD:
4350 case ISD::ATOMIC_LOAD_SUB:
4351 case ISD::ATOMIC_LOAD_AND:
4352 case ISD::ATOMIC_LOAD_OR:
4353 case ISD::ATOMIC_LOAD_XOR:
4354 case ISD::ATOMIC_LOAD_NAND:
4355 case ISD::ATOMIC_LOAD_MIN:
4356 case ISD::ATOMIC_LOAD_MAX:
4357 case ISD::ATOMIC_LOAD_UMIN:
4358 case ISD::ATOMIC_LOAD_UMAX:
4359 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004360 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004361 Tmp2 = PromoteOp(Node->getOperand(2));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004362 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004363 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004364 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004365 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004366 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004367 // Remember that we legalized the chain.
4368 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4369 break;
4370 }
4371
Chris Lattner03c85462005-01-15 05:21:40 +00004372 case ISD::AND:
4373 case ISD::OR:
4374 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004375 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004376 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004377 case ISD::MUL:
4378 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004379 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004380 // that too is okay if they are integer operations.
4381 Tmp1 = PromoteOp(Node->getOperand(0));
4382 Tmp2 = PromoteOp(Node->getOperand(1));
4383 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004384 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004385 break;
4386 case ISD::FADD:
4387 case ISD::FSUB:
4388 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004389 Tmp1 = PromoteOp(Node->getOperand(0));
4390 Tmp2 = PromoteOp(Node->getOperand(1));
4391 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004392 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004393
Chris Lattner01b3d732005-09-28 22:28:18 +00004394 // Floating point operations will give excess precision that we may not be
4395 // able to tolerate. If we DO allow excess precision, just leave it,
4396 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004397 // FIXME: Why would we need to round FP ops more than integer ones?
4398 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004399 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004400 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004401 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004402 break;
4403
Chris Lattner8b6fa222005-01-15 22:16:26 +00004404 case ISD::SDIV:
4405 case ISD::SREM:
4406 // These operators require that their input be sign extended.
4407 Tmp1 = PromoteOp(Node->getOperand(0));
4408 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004409 if (NVT.isInteger()) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004410 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004411 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004412 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Chris Lattner15e4b012005-07-10 00:07:11 +00004413 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004414 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004415 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004416
4417 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004418 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004419 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004420 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004421 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004422 case ISD::FDIV:
4423 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004424 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004425 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004426 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004427 case Expand: assert(0 && "not implemented");
4428 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4429 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004430 }
4431 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004432 case Expand: assert(0 && "not implemented");
4433 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4434 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004435 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004436 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner01b3d732005-09-28 22:28:18 +00004438 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004439 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004440 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner01b3d732005-09-28 22:28:18 +00004441 DAG.getValueType(VT));
4442 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004443
4444 case ISD::UDIV:
4445 case ISD::UREM:
4446 // These operators require that their input be zero extended.
4447 Tmp1 = PromoteOp(Node->getOperand(0));
4448 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004449 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004450 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4451 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
4452 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004453 break;
4454
4455 case ISD::SHL:
4456 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004457 Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004458 break;
4459 case ISD::SRA:
4460 // The input value must be properly sign extended.
4461 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004462 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004463 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004464 Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004465 break;
4466 case ISD::SRL:
4467 // The input value must be properly zero extended.
4468 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004469 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4470 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004471 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004472
4473 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004474 Tmp1 = Node->getOperand(0); // Get the chain.
4475 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004476 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00004477 Tmp3 = DAG.getVAArg(VT, dl, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004478 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004479 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004480 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenc460ae92009-02-04 00:13:36 +00004481 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004482 // Increment the pointer, VAList, to the next vaarg
Scott Michelfdc40a02009-02-17 22:15:04 +00004483 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004484 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004485 TLI.getPointerTy()));
4486 // Store the incremented VAList to the legalized pointer
Dale Johannesen8a782a22009-02-02 22:12:50 +00004487 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004488 // Load the actual argument out of the pointer VAList
Dale Johannesen8a782a22009-02-02 22:12:50 +00004489 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004490 }
4491 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004492 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004493 break;
4494
Evan Cheng466685d2006-10-09 20:57:25 +00004495 case ISD::LOAD: {
4496 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004497 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4498 ? ISD::EXTLOAD : LD->getExtensionType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004499 Result = DAG.getExtLoad(ExtType, dl, NVT,
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004500 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004501 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004502 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004503 LD->isVolatile(),
4504 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004505 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004506 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004507 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004508 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004509 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004510 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4511 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004512
Duncan Sands83ec4b62008-06-06 12:08:01 +00004513 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004514 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004515 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4516 // Ensure that the resulting node is at least the same size as the operands'
4517 // value types, because we cannot assume that TLI.getSetCCValueType() is
4518 // constant.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004519 Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004520 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004521 }
Nate Begeman9373a812005-08-10 20:51:12 +00004522 case ISD::SELECT_CC:
4523 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4524 Tmp3 = PromoteOp(Node->getOperand(3)); // False
Dale Johannesen8a782a22009-02-02 22:12:50 +00004525 Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004526 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004527 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004528 case ISD::BSWAP:
4529 Tmp1 = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004530 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
4531 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4532 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004533 DAG.getConstant(NVT.getSizeInBits() -
4534 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004535 TLI.getShiftAmountTy()));
4536 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004537 case ISD::CTPOP:
4538 case ISD::CTTZ:
4539 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004540 // Zero extend the argument
Dale Johannesen8a782a22009-02-02 22:12:50 +00004541 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004542 // Perform the larger operation, then subtract if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004543 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004544 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004545 case ISD::CTPOP:
4546 Result = Tmp1;
4547 break;
4548 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004549 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004550 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004551 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004552 ISD::SETEQ);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004553 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004554 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004555 break;
4556 case ISD::CTLZ:
4557 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00004558 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004559 DAG.getConstant(NVT.getSizeInBits() -
4560 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004561 break;
4562 }
4563 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004564 case ISD::EXTRACT_SUBVECTOR:
4565 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004566 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004567 case ISD::EXTRACT_VECTOR_ELT:
4568 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4569 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004570 }
4571
Gabor Greifba36cb52008-08-28 21:40:38 +00004572 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004573
4574 // Make sure the result is itself legal.
4575 Result = LegalizeOp(Result);
Scott Michelfdc40a02009-02-17 22:15:04 +00004576
Chris Lattner456a93a2006-01-28 07:39:30 +00004577 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004578 AddPromotedOperand(Op, Result);
4579 return Result;
4580}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004581
Dan Gohman7f321562007-06-25 16:23:39 +00004582/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4583/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4584/// based on the vector type. The return type of this matches the element type
4585/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004586SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004587 // We know that operand #0 is the Vec vector. If the index is a constant
4588 // or if the invec is a supported hardware type, we can use it. Otherwise,
4589 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004590 SDValue Vec = Op.getOperand(0);
4591 SDValue Idx = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004592 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004593
Duncan Sands83ec4b62008-06-06 12:08:01 +00004594 MVT TVT = Vec.getValueType();
4595 unsigned NumElems = TVT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00004596
Dan Gohman7f321562007-06-25 16:23:39 +00004597 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4598 default: assert(0 && "This action is not supported yet!");
4599 case TargetLowering::Custom: {
4600 Vec = LegalizeOp(Vec);
4601 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004602 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004603 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004604 return Tmp3;
4605 break;
4606 }
4607 case TargetLowering::Legal:
4608 if (isTypeLegal(TVT)) {
4609 Vec = LegalizeOp(Vec);
4610 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004611 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004612 }
4613 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004614 case TargetLowering::Promote:
4615 assert(TVT.isVector() && "not vector type");
4616 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004617 case TargetLowering::Expand:
4618 break;
4619 }
4620
Eli Friedman1fde9c52009-05-24 02:46:31 +00004621 if (NumElems == 1)
Chris Lattner15972212006-03-31 17:55:51 +00004622 // This must be an access of the only element. Return it.
Eli Friedman1fde9c52009-05-24 02:46:31 +00004623 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Vec);
4624 else
4625 return ExpandExtractFromVectorThroughStack(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004626}
4627
Eli Friedman3d43b3f2009-05-23 22:37:25 +00004628SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
4629 SDValue Vec = Op.getOperand(0);
4630 SDValue Idx = Op.getOperand(1);
4631 DebugLoc dl = Op.getDebugLoc();
4632 // Store the value to a temporary stack slot, then LOAD the returned part.
4633 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4634 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
4635
4636 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00004637 unsigned EltSize =
4638 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00004639 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
4640 DAG.getConstant(EltSize, Idx.getValueType()));
4641
4642 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
4643 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
4644 else
4645 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
4646
4647 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
4648
4649 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
4650}
4651
Dan Gohman7f321562007-06-25 16:23:39 +00004652/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004653/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004654SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004655 // We know that operand #0 is the Vec vector. For now we assume the index
4656 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004657 SDValue Vec = Op.getOperand(0);
4658 SDValue Idx = LegalizeOp(Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004659
Duncan Sands83ec4b62008-06-06 12:08:01 +00004660 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00004661
Duncan Sands83ec4b62008-06-06 12:08:01 +00004662 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004663 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004664 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004665 }
4666
4667 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004668 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004669 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004670 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004671 Vec = Lo;
4672 } else {
4673 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004674 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4675 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004676 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004677
Dan Gohman65956352007-06-13 15:12:02 +00004678 // It's now an extract from the appropriate high or low part. Recurse.
4679 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004680 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004681}
4682
Nate Begeman750ac1b2006-02-01 07:19:44 +00004683/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4684/// with condition CC on the current target. This usually involves legalizing
4685/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4686/// there may be no choice but to create a new SetCC node to represent the
4687/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004688/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4689void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4690 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00004691 SDValue &CC,
4692 DebugLoc dl) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004693 SDValue Tmp1, Tmp2, Tmp3, Result;
4694
Nate Begeman750ac1b2006-02-01 07:19:44 +00004695 switch (getTypeAction(LHS.getValueType())) {
4696 case Legal:
4697 Tmp1 = LegalizeOp(LHS); // LHS
4698 Tmp2 = LegalizeOp(RHS); // RHS
4699 break;
4700 case Promote:
4701 Tmp1 = PromoteOp(LHS); // LHS
4702 Tmp2 = PromoteOp(RHS); // RHS
4703
4704 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004705 if (LHS.getValueType().isInteger()) {
4706 MVT VT = LHS.getValueType();
4707 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004708
4709 // Otherwise, we have to insert explicit sign or zero extends. Note
4710 // that we could insert sign extends for ALL conditions, but zero extend
4711 // is cheaper on many machines (an AND instead of two shifts), so prefer
4712 // it.
4713 switch (cast<CondCodeSDNode>(CC)->get()) {
4714 default: assert(0 && "Unknown integer comparison!");
4715 case ISD::SETEQ:
4716 case ISD::SETNE:
4717 case ISD::SETUGE:
4718 case ISD::SETUGT:
4719 case ISD::SETULE:
4720 case ISD::SETULT:
4721 // ALL of these operations will work if we either sign or zero extend
4722 // the operands (including the unsigned comparisons!). Zero extend is
4723 // usually a simpler/cheaper operation, so prefer it.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004724 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4725 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004726 break;
4727 case ISD::SETGE:
4728 case ISD::SETGT:
4729 case ISD::SETLT:
4730 case ISD::SETLE:
Dale Johannesenbb5da912009-02-02 20:41:04 +00004731 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Nate Begeman750ac1b2006-02-01 07:19:44 +00004732 DAG.getValueType(VT));
Dale Johannesenbb5da912009-02-02 20:41:04 +00004733 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Nate Begeman750ac1b2006-02-01 07:19:44 +00004734 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00004735 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4736 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00004737 break;
4738 }
4739 }
4740 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004741 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004742 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004743 if (VT == MVT::f32 || VT == MVT::f64) {
4744 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004745 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004746 switch (cast<CondCodeSDNode>(CC)->get()) {
4747 case ISD::SETEQ:
4748 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004749 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004750 break;
4751 case ISD::SETNE:
4752 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004753 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004754 break;
4755 case ISD::SETGE:
4756 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004757 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004758 break;
4759 case ISD::SETLT:
4760 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004761 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004762 break;
4763 case ISD::SETLE:
4764 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004765 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004766 break;
4767 case ISD::SETGT:
4768 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004769 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004770 break;
4771 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004772 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4773 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004774 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004775 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004776 break;
4777 default:
Evan Cheng56966222007-01-12 02:11:51 +00004778 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004779 switch (cast<CondCodeSDNode>(CC)->get()) {
4780 case ISD::SETONE:
4781 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004782 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004783 // Fallthrough
4784 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004785 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004786 break;
4787 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004788 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004789 break;
4790 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004791 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004792 break;
4793 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004794 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004795 break;
Evan Cheng56966222007-01-12 02:11:51 +00004796 case ISD::SETUEQ:
4797 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004798 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004799 default: assert(0 && "Unsupported FP setcc!");
4800 }
4801 }
Duncan Sandsf9516202008-06-30 10:19:09 +00004802
Dan Gohman475871a2008-07-27 21:46:04 +00004803 SDValue Dummy;
4804 SDValue Ops[2] = { LHS, RHS };
Dale Johannesenbb5da912009-02-02 20:41:04 +00004805 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004806 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004807 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004808 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004809 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesenbb5da912009-02-02 20:41:04 +00004810 Tmp1 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004811 TLI.getSetCCResultType(Tmp1.getValueType()),
4812 Tmp1, Tmp2, CC);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004813 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004814 false /*sign irrelevant*/, Dummy);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004815 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004816 TLI.getSetCCResultType(LHS.getValueType()), LHS,
4817 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesenbb5da912009-02-02 20:41:04 +00004818 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00004819 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00004820 }
Evan Cheng21cacc42008-07-07 07:18:09 +00004821 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00004822 RHS = Tmp2;
4823 return;
4824 }
4825
Dan Gohman475871a2008-07-27 21:46:04 +00004826 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004827 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004828 ExpandOp(RHS, RHSLo, RHSHi);
4829 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4830
4831 if (VT==MVT::ppcf128) {
4832 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00004833 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004834 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00004835 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004836 // The following can be improved, but not that much.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004837 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004838 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004839 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004840 LHSLo, RHSLo, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004841 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
4842 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004843 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004844 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004845 LHSHi, RHSHi, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004846 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
4847 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00004848 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00004849 break;
4850 }
4851
4852 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004853 case ISD::SETEQ:
4854 case ISD::SETNE:
4855 if (RHSLo == RHSHi)
4856 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4857 if (RHSCST->isAllOnesValue()) {
4858 // Comparison to -1.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004859 Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004860 Tmp2 = RHSLo;
4861 break;
4862 }
4863
Dale Johannesenbb5da912009-02-02 20:41:04 +00004864 Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
4865 Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
4866 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004867 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4868 break;
4869 default:
4870 // If this is a comparison of the sign bit, just look at the top part.
4871 // X > -1, x < 0
4872 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
Scott Michelfdc40a02009-02-17 22:15:04 +00004873 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004874 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004875 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4876 CST->isAllOnesValue())) { // X > -1
4877 Tmp1 = LHSHi;
4878 Tmp2 = RHSHi;
4879 break;
4880 }
4881
4882 // FIXME: This generated code sucks.
4883 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004884 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004885 default: assert(0 && "Unknown integer setcc!");
4886 case ISD::SETLT:
4887 case ISD::SETULT: LowCC = ISD::SETULT; break;
4888 case ISD::SETGT:
4889 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4890 case ISD::SETLE:
4891 case ISD::SETULE: LowCC = ISD::SETULE; break;
4892 case ISD::SETGE:
4893 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4894 }
4895
4896 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4897 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4898 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4899
4900 // NOTE: on targets without efficient SELECT of bools, we can always use
4901 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004902 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00004903 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004904 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004905 if (!Tmp1.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004906 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004907 LHSLo, RHSLo, LowCC);
4908 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004909 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004910 if (!Tmp2.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004911 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004912 TLI.getSetCCResultType(LHSHi.getValueType()),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004913 LHSHi, RHSHi, CC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004914
Gabor Greifba36cb52008-08-28 21:40:38 +00004915 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
4916 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00004917 if ((Tmp1C && Tmp1C->isNullValue()) ||
4918 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004919 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4920 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004921 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004922 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4923 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4924 // low part is known false, returns high part.
4925 // For LE / GE, if high part is known false, ignore the low part.
4926 // For LT / GT, if high part is known true, ignore the low part.
4927 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00004928 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004929 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00004930 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
4931 LHSHi, RHSHi, ISD::SETEQ, false,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004932 DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004933 if (!Result.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004934 Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004935 LHSHi, RHSHi, ISD::SETEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004936 Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
Evan Cheng2e677812007-02-08 22:16:19 +00004937 Result, Tmp1, Tmp2));
4938 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00004939 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004940 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004941 }
4942 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004943 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004944 LHS = Tmp1;
4945 RHS = Tmp2;
4946}
4947
Evan Cheng7f042682008-10-15 02:05:31 +00004948/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
4949/// condition code CC on the current target. This routine assumes LHS and rHS
4950/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
4951/// illegal condition code into AND / OR of multiple SETCC values.
4952void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
4953 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00004954 SDValue &CC,
4955 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00004956 MVT OpVT = LHS.getValueType();
4957 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4958 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
4959 default: assert(0 && "Unknown condition code action!");
4960 case TargetLowering::Legal:
4961 // Nothing to do.
4962 break;
4963 case TargetLowering::Expand: {
4964 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
4965 unsigned Opc = 0;
4966 switch (CCCode) {
4967 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00004968 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
4969 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
4970 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4971 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
4972 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4973 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4974 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4975 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4976 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4977 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4978 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4979 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00004980 // FIXME: Implement more expansions.
4981 }
4982
Dale Johannesenbb5da912009-02-02 20:41:04 +00004983 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
4984 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
4985 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00004986 RHS = SDValue();
4987 CC = SDValue();
4988 break;
4989 }
4990 }
4991}
4992
Chris Lattner1401d152008-01-16 07:45:30 +00004993/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4994/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4995/// a load from the stack slot to DestVT, extending it if needed.
4996/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004997SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
4998 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004999 MVT DestVT,
5000 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00005001 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005002 unsigned SrcAlign =
5003 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
5004 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005005 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00005006
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005007 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005008 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00005009 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5010
Duncan Sands83ec4b62008-06-06 12:08:01 +00005011 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5012 unsigned SlotSize = SlotVT.getSizeInBits();
5013 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005014 unsigned DestAlign =
5015 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00005016
Chris Lattner1401d152008-01-16 07:45:30 +00005017 // Emit a store to the stack slot. Use a truncstore if the input value is
5018 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00005020
Chris Lattner1401d152008-01-16 07:45:30 +00005021 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005022 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005023 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005024 else {
5025 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005026 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005027 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005029
Chris Lattner35481892005-12-23 00:16:34 +00005030 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005031 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005032 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00005033
Chris Lattner1401d152008-01-16 07:45:30 +00005034 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005035 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00005036 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005037}
5038
Dan Gohman475871a2008-07-27 21:46:04 +00005039SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005040 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00005041 // Create a vector sized/aligned stack slot, store the value to element #0,
5042 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005043 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005044
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005045 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005046 int SPFI = StackPtrFI->getIndex();
5047
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005048 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
5049 StackPtr,
5050 PseudoSourceValue::getFixedStack(SPFI), 0,
5051 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005052 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005053 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005054}
5055
5056
Chris Lattnerce872152006-03-19 06:31:19 +00005057/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005058/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005059SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005060 unsigned NumElems = Node->getNumOperands();
5061 SDValue SplatValue = Node->getOperand(0);
5062 DebugLoc dl = Node->getDebugLoc();
5063 MVT VT = Node->getValueType(0);
5064 MVT OpVT = SplatValue.getValueType();
5065 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005066
5067 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005068 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00005069 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005070
Dan Gohman475871a2008-07-27 21:46:04 +00005071 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005072 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00005073 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00005074 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005075 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005076 bool isConstant = true;
5077 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5078 SplatValue.getOpcode() != ISD::UNDEF)
5079 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005080
Evan Cheng033e6812006-03-24 01:17:21 +00005081 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005082 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005083 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005084 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005085 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005086 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005087 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005088
5089 // If this isn't a constant element or an undef, we can't use a constant
5090 // pool load.
5091 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5092 V.getOpcode() != ISD::UNDEF)
5093 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005095
Chris Lattnerce872152006-03-19 06:31:19 +00005096 if (isOnlyLowElement) {
5097 // If the low element is an undef too, then this whole things is an undef.
5098 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005099 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005100 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005101 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00005102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005103
Chris Lattner2eb86532006-03-24 07:29:17 +00005104 // If all elements are constants, create a load from the constant pool.
5105 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00005106 std::vector<Constant*> CV;
5107 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005108 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00005109 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005110 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00005111 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005112 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005113 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005114 } else {
5115 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005116 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005117 CV.push_back(UndefValue::get(OpNTy));
5118 }
5119 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005120 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005121 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005122 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005123 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005124 PseudoSourceValue::getConstantPool(), 0,
5125 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005127
Gabor Greifba36cb52008-08-28 21:40:38 +00005128 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005129 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00005130 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00005131
5132 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00005133 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00005134 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00005135 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005136 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00005137
Chris Lattner87100e02006-03-20 01:52:29 +00005138 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00005139 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
5140 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00005141 }
5142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005143
Evan Cheng033e6812006-03-24 01:17:21 +00005144 // If there are only two unique elements, we may be able to turn this into a
5145 // vector shuffle.
5146 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005147 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SDValue Val1 = Node->getOperand(1);
5149 SDValue Val2;
5150 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005151 if (MI->first != Val1)
5152 Val2 = MI->first;
5153 else
5154 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00005155
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005156 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005157 // vector shuffle has the undef vector on the RHS.
5158 if (Val1.getOpcode() == ISD::UNDEF)
5159 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005160
Evan Cheng033e6812006-03-24 01:17:21 +00005161 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00005162 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005163
5164 // Set elements of the shuffle mask for Val1.
5165 std::vector<unsigned> &Val1Elts = Values[Val1];
5166 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005167 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005168
5169 // Set elements of the shuffle mask for Val2.
5170 std::vector<unsigned> &Val2Elts = Values[Val2];
5171 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5172 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00005173 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00005174
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005175 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005176 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00005177 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005178 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
5179 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005180 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00005181 }
5182 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005183
Chris Lattnerce872152006-03-19 06:31:19 +00005184 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5185 // aligned object on the stack, store each element into it, then load
5186 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00005187 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005188 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00005189 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5190 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5191
Chris Lattnerce872152006-03-19 06:31:19 +00005192 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005193 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005194 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00005195 // Store (in the right endianness) the elements to memory.
5196 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5197 // Ignore undef elements.
5198 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005199
Chris Lattner841c8822006-03-22 01:46:54 +00005200 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00005201
Dan Gohman475871a2008-07-27 21:46:04 +00005202 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005203 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005204
Dale Johannesen8a782a22009-02-02 22:12:50 +00005205 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
5206 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00005207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005208
Dan Gohman475871a2008-07-27 21:46:04 +00005209 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005210 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00005211 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005212 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005213 else
5214 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005215
Chris Lattnerce872152006-03-19 06:31:19 +00005216 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005217 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005218}
5219
Chris Lattner5b359c62005-04-02 04:00:59 +00005220void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005221 SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005222 SDValue &Lo, SDValue &Hi,
5223 DebugLoc dl) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005224 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005226 ExpandOp(Op, LHSL, LHSH);
5227
Dan Gohman475871a2008-07-27 21:46:04 +00005228 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005229 MVT VT = LHSL.getValueType();
Dan Gohmanfc166572009-04-09 23:54:40 +00005230 Lo = DAG.getNode(NodeOp, dl, DAG.getVTList(VT, VT), Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005231 Hi = Lo.getValue(1);
5232}
5233
5234
Chris Lattnere34b3962005-01-19 04:19:40 +00005235/// ExpandShift - Try to find a clever way to expand this shift operation out to
5236/// smaller elements. If we can't find a way that is more efficient than a
5237/// libcall on this target, return false. Otherwise, return true with the
5238/// low-parts expanded into Lo and Hi.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005239bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005240 SDValue &Lo, SDValue &Hi,
5241 DebugLoc dl) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005242 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5243 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005244
Duncan Sands83ec4b62008-06-06 12:08:01 +00005245 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005246 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005247 MVT ShTy = ShAmt.getValueType();
5248 unsigned ShBits = ShTy.getSizeInBits();
5249 unsigned VTBits = Op.getValueType().getSizeInBits();
5250 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005251
Chris Lattner7a3c8552007-10-14 20:35:12 +00005252 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005253 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005254 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005255 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005256 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005257 ExpandOp(Op, InL, InH);
5258 switch(Opc) {
5259 case ISD::SHL:
5260 if (Cst > VTBits) {
5261 Lo = DAG.getConstant(0, NVT);
5262 Hi = DAG.getConstant(0, NVT);
5263 } else if (Cst > NVTBits) {
5264 Lo = DAG.getConstant(0, NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005265 Hi = DAG.getNode(ISD::SHL, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005266 NVT, InL, DAG.getConstant(Cst-NVTBits, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005267 } else if (Cst == NVTBits) {
5268 Lo = DAG.getConstant(0, NVT);
5269 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005270 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005271 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
5272 Hi = DAG.getNode(ISD::OR, dl, NVT,
5273 DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005274 DAG.getNode(ISD::SRL, dl, NVT, InL,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005275 DAG.getConstant(NVTBits-Cst, ShTy)));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005276 }
5277 return true;
5278 case ISD::SRL:
5279 if (Cst > VTBits) {
5280 Lo = DAG.getConstant(0, NVT);
5281 Hi = DAG.getConstant(0, NVT);
5282 } else if (Cst > NVTBits) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005283 Lo = DAG.getNode(ISD::SRL, dl, NVT,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005284 InH, DAG.getConstant(Cst-NVTBits, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005285 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005286 } else if (Cst == NVTBits) {
5287 Lo = InH;
5288 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005289 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005290 Lo = DAG.getNode(ISD::OR, dl, NVT,
5291 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005292 DAG.getNode(ISD::SHL, dl, NVT, InH,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005293 DAG.getConstant(NVTBits-Cst, ShTy)));
5294 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005295 }
5296 return true;
5297 case ISD::SRA:
5298 if (Cst > VTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005299 Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005300 DAG.getConstant(NVTBits-1, ShTy));
5301 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005302 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005303 DAG.getConstant(Cst-NVTBits, ShTy));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005304 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005305 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005306 } else if (Cst == NVTBits) {
5307 Lo = InH;
Dale Johannesen8a782a22009-02-02 22:12:50 +00005308 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005309 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005310 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005311 Lo = DAG.getNode(ISD::OR, dl, NVT,
5312 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005313 DAG.getNode(ISD::SHL, dl,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005314 NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5315 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005316 }
5317 return true;
5318 }
5319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005320
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005321 // Okay, the shift amount isn't constant. However, if we can tell that it is
5322 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005323 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5324 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005325 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00005326
Dan Gohman9e255b72008-02-22 01:12:31 +00005327 // If we know that if any of the high bits of the shift amount are one, then
5328 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005329 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005330 // Mask out the high bit, which we know is set.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005331 Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005332 DAG.getConstant(~Mask, Amt.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005334 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005336 ExpandOp(Op, InL, InH);
5337 switch(Opc) {
5338 case ISD::SHL:
5339 Lo = DAG.getConstant(0, NVT); // Low part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005340 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005341 return true;
5342 case ISD::SRL:
5343 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005344 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005345 return true;
5346 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005347 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005348 DAG.getConstant(NVTBits-1, Amt.getValueType()));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005349 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005350 return true;
5351 }
5352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005353
Dan Gohman9e255b72008-02-22 01:12:31 +00005354 // If we know that the high bits of the shift amount are all zero, then we can
5355 // do this as a couple of simple shifts.
5356 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005357 // Compute 32-amt.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005358 SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005359 DAG.getConstant(NVTBits, Amt.getValueType()),
5360 Amt);
Scott Michelfdc40a02009-02-17 22:15:04 +00005361
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005362 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005363 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005364 ExpandOp(Op, InL, InH);
5365 switch(Opc) {
5366 case ISD::SHL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005367 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
5368 Hi = DAG.getNode(ISD::OR, dl, NVT,
5369 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
5370 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005371 return true;
5372 case ISD::SRL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005373 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
5374 Lo = DAG.getNode(ISD::OR, dl, NVT,
5375 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5376 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005377 return true;
5378 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005379 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
5380 Lo = DAG.getNode(ISD::OR, dl, NVT,
5381 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5382 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005383 return true;
5384 }
5385 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005386
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005387 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005388}
Chris Lattner77e77a62005-01-21 06:05:23 +00005389
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005390
Chris Lattner77e77a62005-01-21 06:05:23 +00005391// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5392// does not fit into a register, return the lo part and set the hi part to the
5393// by-reg argument. If it does fit into a single register, return the result
5394// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005395SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5396 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005397 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005398 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00005399 // Legalizing the call will automatically add the previous call to the
5400 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005401 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Chris Lattner77e77a62005-01-21 06:05:23 +00005403 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005404 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005405 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005406 MVT ArgVT = Node->getOperand(i).getValueType();
5407 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00005408 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005409 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005410 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005411 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005412 }
Bill Wendling056292f2008-09-16 21:48:12 +00005413 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005414 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005415
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005416 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005417 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005418 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005419 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005420 CallingConv::C, false, Callee, Args, DAG,
5421 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005422
Chris Lattner6831a812006-02-13 09:18:02 +00005423 // Legalize the call sequence, starting with the chain. This will advance
5424 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5425 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5426 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005427 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005428 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005429 default: assert(0 && "Unknown thing");
5430 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005431 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005432 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005433 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005434 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005435 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005436 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005437 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005438}
5439
Dan Gohman7f8613e2008-08-14 20:04:46 +00005440/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5441///
5442SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005443LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5444 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00005445 bool isCustom = false;
5446 SDValue Tmp1;
5447 switch (getTypeAction(Op.getValueType())) {
5448 case Legal:
5449 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5450 Op.getValueType())) {
5451 default: assert(0 && "Unknown operation action!");
5452 case TargetLowering::Custom:
5453 isCustom = true;
5454 // FALLTHROUGH
5455 case TargetLowering::Legal:
5456 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005457 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005458 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5459 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005460 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005461 DestTy, Tmp1);
5462 if (isCustom) {
5463 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005464 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005465 }
5466 break;
5467 case TargetLowering::Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005468 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005469 break;
5470 case TargetLowering::Promote:
Dale Johannesenaf435272009-02-02 19:03:57 +00005471 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005472 break;
5473 }
5474 break;
5475 case Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005476 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005477 break;
5478 case Promote:
5479 Tmp1 = PromoteOp(Op);
5480 if (isSigned) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005481 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005482 Tmp1, DAG.getValueType(Op.getValueType()));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005483 } else {
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005484 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, Op.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00005485 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005486 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005487 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5488 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005489 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005490 DestTy, Tmp1);
5491 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5492 break;
5493 }
5494 return Result;
5495}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005496
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005497/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5498///
Dan Gohman475871a2008-07-27 21:46:04 +00005499SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005500ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005501 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005502 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005503
Dan Gohman7f8613e2008-08-14 20:04:46 +00005504 // Expand unsupported int-to-fp vector casts by unrolling them.
5505 if (DestTy.isVector()) {
5506 if (!ExpandSource)
5507 return LegalizeOp(UnrollVectorOp(Source));
5508 MVT DestEltTy = DestTy.getVectorElementType();
5509 if (DestTy.getVectorNumElements() == 1) {
5510 SDValue Scalar = ScalarizeVectorOp(Source);
5511 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00005512 DestEltTy, Scalar, dl);
Evan Chenga87008d2009-02-25 22:49:59 +00005513 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005514 }
5515 SDValue Lo, Hi;
5516 SplitVectorOp(Source, Lo, Hi);
5517 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5518 DestTy.getVectorNumElements() / 2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005519 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +00005520 Lo, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005521 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +00005522 Hi, dl);
5523 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengefa53392008-10-13 18:46:18 +00005524 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005525 }
5526
Evan Cheng9845eb52008-04-01 02:18:22 +00005527 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5528 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005529 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005530 // incoming integer is set. To handle this, we dynamically test to see if
5531 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005532 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005533 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005535 ExpandOp(Source, Lo, Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005536 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005537 } else {
5538 // The comparison for the sign bit will use the entire operand.
5539 Hi = Source;
5540 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005541
Dale Johannesen53997b02008-11-04 20:52:49 +00005542 // Check to see if the target has a custom way to lower this. If so, use
5543 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005544 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5545 default: assert(0 && "This action not implemented for this operation!");
5546 case TargetLowering::Legal:
5547 case TargetLowering::Expand:
5548 break; // This case is handled below.
5549 case TargetLowering::Custom: {
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005550 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, dl, DestTy,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005551 Source), DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005552 if (NV.getNode())
5553 return LegalizeOp(NV);
5554 break; // The target decided this was legal after all
5555 }
5556 }
5557
Chris Lattner66de05b2005-05-13 04:45:13 +00005558 // If this is unsigned, and not supported, first perform the conversion to
5559 // signed, then adjust the result if the sign bit is set.
Dale Johannesenaf435272009-02-02 19:03:57 +00005560 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Chris Lattner66de05b2005-05-13 04:45:13 +00005561
Scott Michelfdc40a02009-02-17 22:15:04 +00005562 SDValue SignSet = DAG.getSetCC(dl,
Dale Johannesenaf435272009-02-02 19:03:57 +00005563 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005564 Hi, DAG.getConstant(0, Hi.getValueType()),
5565 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005566 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00005567 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005568 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005569 uint64_t FF = 0x5f800000ULL;
5570 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00005571 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005572
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005574 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00005575 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005576 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005578 if (DestTy == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00005579 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005580 PseudoSourceValue::getConstantPool(), 0,
5581 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005582 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005583 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenaf435272009-02-02 19:03:57 +00005584 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005585 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005586 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005587 MVT::f32, false, Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00005588 else
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005589 assert(0 && "Unexpected conversion");
5590
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005592 if (SCVT != DestTy) {
5593 // Destination type needs to be expanded as well. The FADD now we are
5594 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005595 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5596 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesenaf435272009-02-02 19:03:57 +00005597 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005598 SignedConv, SignedConv.getValue(1));
5599 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005600 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005601 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005602 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005603 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005604
Chris Lattnera88a2602005-05-14 05:33:54 +00005605 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005606 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005607 default: assert(0 && "This action not implemented for this operation!");
5608 case TargetLowering::Legal:
5609 case TargetLowering::Expand:
5610 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005611 case TargetLowering::Custom: {
Dale Johannesenaf435272009-02-02 19:03:57 +00005612 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005613 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005614 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005615 return LegalizeOp(NV);
5616 break; // The target decided this was legal after all
5617 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005618 }
5619
Chris Lattner13689e22005-05-12 07:00:44 +00005620 // Expand the source, then glue it back together for the call. We must expand
5621 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005622 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005623 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005624 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005625 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005626 }
Chris Lattner13689e22005-05-12 07:00:44 +00005627
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005628 RTLIB::Libcall LC = isSigned ?
5629 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5630 RTLIB::getUINTTOFP(SourceVT, DestTy);
5631 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5632
Dale Johannesenaf435272009-02-02 19:03:57 +00005633 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005635 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5636 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesenaf435272009-02-02 19:03:57 +00005637 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmana2e94852008-03-10 23:03:31 +00005638 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005639}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005640
Chris Lattner22cde6a2006-01-28 08:25:58 +00005641/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5642/// INT_TO_FP operation of the specified operand when the target requests that
5643/// we expand it. At this point, we know that the result and operand types are
5644/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005645SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5646 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00005647 MVT DestVT,
5648 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005649 if (Op0.getValueType() == MVT::i32) {
5650 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00005651
Chris Lattner23594d42008-01-16 07:03:22 +00005652 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005653 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00005654
Chris Lattner22cde6a2006-01-28 08:25:58 +00005655 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005656 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005657 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005658 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005659 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005660 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005661 if (TLI.isLittleEndian())
5662 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00005663
Chris Lattner22cde6a2006-01-28 08:25:58 +00005664 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005666 if (isSigned) {
5667 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005668 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00005669 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005670 } else {
5671 Op0Mapped = Op0;
5672 }
5673 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00005674 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005675 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005676 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005677 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005678 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00005679 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005680 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00005681 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005682 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005684 BitsToDouble(0x4330000080000000ULL) :
5685 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005686 MVT::f64);
5687 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00005688 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005689 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005690 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005691 // handle final rounding
5692 if (DestVT == MVT::f64) {
5693 // do nothing
5694 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005695 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005696 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00005697 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005698 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005699 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005700 }
5701 return Result;
5702 }
5703 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00005704 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005705
Dale Johannesenaf435272009-02-02 19:03:57 +00005706 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005707 Op0, DAG.getConstant(0, Op0.getValueType()),
5708 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005709 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00005710 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005711 SignSet, Four, Zero);
5712
5713 // If the sign bit of the integer is set, the large number will be treated
5714 // as a negative number. To counteract this, the dynamic code adds an
5715 // offset depending on the data type.
5716 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005717 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005718 default: assert(0 && "Unsupported integer type!");
5719 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5720 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5721 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5722 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5723 }
5724 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00005725 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005726
Dan Gohman475871a2008-07-27 21:46:04 +00005727 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005728 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00005729 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005730 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005731 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005732 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00005733 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005734 PseudoSourceValue::getConstantPool(), 0,
5735 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005736 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005737 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00005738 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00005739 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005740 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005741 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005742 }
5743
Dale Johannesenaf435272009-02-02 19:03:57 +00005744 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005745}
5746
5747/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5748/// *INT_TO_FP operation of the specified operand when the target requests that
5749/// we promote it. At this point, we know that the result and operand types are
5750/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5751/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005752SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5753 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00005754 bool isSigned,
5755 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005756 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005757 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005758
5759 unsigned OpToUse = 0;
5760
5761 // Scan for the appropriate larger type to use.
5762 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005763 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5764 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005765
5766 // If the target supports SINT_TO_FP of this type, use it.
5767 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5768 default: break;
5769 case TargetLowering::Legal:
5770 if (!TLI.isTypeLegal(NewInTy))
5771 break; // Can't use this datatype.
5772 // FALL THROUGH.
5773 case TargetLowering::Custom:
5774 OpToUse = ISD::SINT_TO_FP;
5775 break;
5776 }
5777 if (OpToUse) break;
5778 if (isSigned) continue;
5779
5780 // If the target supports UINT_TO_FP of this type, use it.
5781 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5782 default: break;
5783 case TargetLowering::Legal:
5784 if (!TLI.isTypeLegal(NewInTy))
5785 break; // Can't use this datatype.
5786 // FALL THROUGH.
5787 case TargetLowering::Custom:
5788 OpToUse = ISD::UINT_TO_FP;
5789 break;
5790 }
5791 if (OpToUse) break;
5792
5793 // Otherwise, try a larger type.
5794 }
5795
5796 // Okay, we found the operation and type to use. Zero extend our input to the
5797 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00005798 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005799 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00005800 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005801}
5802
5803/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5804/// FP_TO_*INT operation of the specified operand when the target requests that
5805/// we promote it. At this point, we know that the result and operand types are
5806/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5807/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00005808SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5809 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00005810 bool isSigned,
5811 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005812 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005813 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005814
5815 unsigned OpToUse = 0;
5816
5817 // Scan for the appropriate larger type to use.
5818 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005819 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5820 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005821
5822 // If the target supports FP_TO_SINT returning this type, use it.
5823 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5824 default: break;
5825 case TargetLowering::Legal:
5826 if (!TLI.isTypeLegal(NewOutTy))
5827 break; // Can't use this datatype.
5828 // FALL THROUGH.
5829 case TargetLowering::Custom:
5830 OpToUse = ISD::FP_TO_SINT;
5831 break;
5832 }
5833 if (OpToUse) break;
5834
5835 // If the target supports FP_TO_UINT of this type, use it.
5836 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5837 default: break;
5838 case TargetLowering::Legal:
5839 if (!TLI.isTypeLegal(NewOutTy))
5840 break; // Can't use this datatype.
5841 // FALL THROUGH.
5842 case TargetLowering::Custom:
5843 OpToUse = ISD::FP_TO_UINT;
5844 break;
5845 }
5846 if (OpToUse) break;
5847
5848 // Otherwise, try a larger type.
5849 }
5850
Scott Michelfdc40a02009-02-17 22:15:04 +00005851
Chris Lattner27a6c732007-11-24 07:07:01 +00005852 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00005853 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00005854
Chris Lattner27a6c732007-11-24 07:07:01 +00005855 // If the operation produces an invalid type, it must be custom lowered. Use
5856 // the target lowering hooks to expand it. Just keep the low part of the
5857 // expanded operation, we know that we're truncating anyway.
5858 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00005859 SmallVector<SDValue, 2> Results;
5860 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
5861 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
5862 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00005863 }
Duncan Sands126d9072008-07-04 11:47:58 +00005864
Chris Lattner27a6c732007-11-24 07:07:01 +00005865 // Truncate the result of the extended FP_TO_*INT operation to the desired
5866 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00005867 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005868}
5869
5870/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5871///
Dale Johannesen8a782a22009-02-02 22:12:50 +00005872SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005873 MVT VT = Op.getValueType();
5874 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005875 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005876 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005877 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5878 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005879 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5880 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5881 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005882 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005883 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
5884 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5885 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5886 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
5887 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5888 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5889 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
5890 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
5891 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005892 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005893 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
5894 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
5895 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
5896 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5897 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5898 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
5899 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
5900 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
5901 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5902 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5903 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5904 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5905 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5906 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5907 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
5908 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
5909 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
5910 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
5911 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
5912 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
5913 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005914 }
5915}
5916
5917/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5918///
Scott Michelfdc40a02009-02-17 22:15:04 +00005919SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005920 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005921 switch (Opc) {
5922 default: assert(0 && "Cannot expand this yet!");
5923 case ISD::CTPOP: {
5924 static const uint64_t mask[6] = {
5925 0x5555555555555555ULL, 0x3333333333333333ULL,
5926 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5927 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5928 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005929 MVT VT = Op.getValueType();
5930 MVT ShVT = TLI.getShiftAmountTy();
5931 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005932 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5933 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005934 unsigned EltSize = VT.isVector() ?
5935 VT.getVectorElementType().getSizeInBits() : len;
5936 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005937 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005938 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00005939 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00005940 DAG.getNode(ISD::AND, dl, VT,
5941 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
5942 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005943 }
5944 return Op;
5945 }
5946 case ISD::CTLZ: {
5947 // for now, we do this:
5948 // x = x | (x >> 1);
5949 // x = x | (x >> 2);
5950 // ...
5951 // x = x | (x >>16);
5952 // x = x | (x >>32); // for 64-bit input
5953 // return popcount(~x);
5954 //
5955 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005956 MVT VT = Op.getValueType();
5957 MVT ShVT = TLI.getShiftAmountTy();
5958 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005959 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005961 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00005962 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005963 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00005964 Op = DAG.getNOT(dl, Op, VT);
5965 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005966 }
5967 case ISD::CTTZ: {
5968 // for now, we use: { return popcount(~x & (x - 1)); }
5969 // unless the target has ctlz but not ctpop, in which case we use:
5970 // { return 32 - nlz(~x & (x-1)); }
5971 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005972 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005973 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
5974 DAG.getNOT(dl, Op, VT),
5975 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00005976 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005977 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005978 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
5979 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00005980 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005981 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00005982 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
5983 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005984 }
5985 }
5986}
Chris Lattnere34b3962005-01-19 04:19:40 +00005987
Dan Gohman475871a2008-07-27 21:46:04 +00005988/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00005989/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00005990/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00005991/// ExpandedNodes map is filled in for any results that are expanded, and the
5992/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00005993void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Eli Friedman1fde9c52009-05-24 02:46:31 +00005994 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00005995 MVT VT = Op.getValueType();
5996 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00005997 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005998 DebugLoc dl = Node->getDebugLoc();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005999 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006000 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006001 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006002
Chris Lattner6fdcb252005-09-02 20:32:45 +00006003 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006004 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006005 = ExpandedNodes.find(Op);
6006 if (I != ExpandedNodes.end()) {
6007 Lo = I->second.first;
6008 Hi = I->second.second;
6009 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006010 }
6011
Chris Lattner3e928bb2005-01-07 07:47:09 +00006012 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006013 case ISD::CopyFromReg:
6014 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006015 case ISD::FP_ROUND_INREG:
Scott Michelfdc40a02009-02-17 22:15:04 +00006016 if (VT == MVT::ppcf128 &&
6017 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006018 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006019 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006020 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006021 Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006022 SDValue Result =
6023 TLI.LowerOperation(DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src,
6024 Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006025 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6026 Lo = Result.getNode()->getOperand(0);
6027 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006028 break;
6029 }
6030 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006031 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006032#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006033 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006034#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006035 assert(0 && "Do not know how to expand this operator!");
6036 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006037 case ISD::EXTRACT_ELEMENT:
6038 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006039 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006040 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006041 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006042 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006043 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6044 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6045 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006046 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00006047 Lo = DAG.getUNDEF(NVT);
6048 Hi = DAG.getUNDEF(NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006049 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006050 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006051 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006052 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6053 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6054 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006055 break;
6056 }
Evan Cheng00495212006-12-12 21:32:44 +00006057 case ISD::ConstantFP: {
6058 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006059 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006060 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006061 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6062 MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00006063 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
Dale Johannesena471c2e2007-10-11 18:07:22 +00006064 MVT::f64);
6065 break;
6066 }
Evan Cheng279101e2006-12-12 22:19:28 +00006067 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006068 if (getTypeAction(Lo.getValueType()) == Expand)
6069 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006070 break;
6071 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006072 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006073 // Return the operands.
6074 Lo = Node->getOperand(0);
6075 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006076 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006077
Chris Lattner27a6c732007-11-24 07:07:01 +00006078 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006079 if (Node->getNumValues() == 1) {
6080 ExpandOp(Op.getOperand(0), Lo, Hi);
6081 break;
6082 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006083 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006084 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006085 Op.getValue(1).getValueType() == MVT::Other &&
6086 "unhandled MERGE_VALUES");
6087 ExpandOp(Op.getOperand(0), Lo, Hi);
6088 // Remember that we legalized the chain.
6089 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6090 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006091
Chris Lattner58f79632005-12-12 22:27:43 +00006092 case ISD::SIGN_EXTEND_INREG:
6093 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006094 // sext_inreg the low part if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006095 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006096
Chris Lattner4bdd2752006-10-06 17:34:12 +00006097 // The high part gets the sign extension from the lo-part. This handles
6098 // things like sextinreg V:i64 from i8.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006099 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006100 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006101 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006102 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006103
Nate Begemand88fc032006-01-14 03:14:10 +00006104 case ISD::BSWAP: {
6105 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006106 SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
6107 Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
Nate Begemand88fc032006-01-14 03:14:10 +00006108 Lo = TempLo;
6109 break;
6110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006111
Chris Lattneredb1add2005-05-11 04:51:16 +00006112 case ISD::CTPOP:
6113 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006114 Lo = DAG.getNode(ISD::ADD, dl, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6115 DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
6116 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006117 Hi = DAG.getConstant(0, NVT);
6118 break;
6119
Chris Lattner39a8f332005-05-12 19:05:01 +00006120 case ISD::CTLZ: {
6121 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006122 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006123 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006124 SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
6125 SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
6126 BitsC, ISD::SETNE);
6127 SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
6128 LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006129
Dale Johannesen8a782a22009-02-02 22:12:50 +00006130 Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006131 Hi = DAG.getConstant(0, NVT);
6132 break;
6133 }
6134
6135 case ISD::CTTZ: {
6136 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006137 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006138 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006139 SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
6140 SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
6141 BitsC, ISD::SETNE);
6142 SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
6143 HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006144
Dale Johannesen8a782a22009-02-02 22:12:50 +00006145 Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006146 Hi = DAG.getConstant(0, NVT);
6147 break;
6148 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006149
Nate Begemanacc398c2006-01-25 18:21:52 +00006150 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006151 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6152 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dale Johannesenc460ae92009-02-04 00:13:36 +00006153 Lo = DAG.getVAArg(NVT, dl, Ch, Ptr, Node->getOperand(2));
6154 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00006155
6156 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006157 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006158 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006159 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006160 std::swap(Lo, Hi);
6161 break;
6162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006163
Chris Lattner3e928bb2005-01-07 07:47:09 +00006164 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006165 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006166 SDValue Ch = LD->getChain(); // Legalize the chain.
6167 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006168 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006169 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006170 int SVOffset = LD->getSrcValueOffset();
6171 unsigned Alignment = LD->getAlignment();
6172 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006173
Evan Cheng466685d2006-10-09 20:57:25 +00006174 if (ExtType == ISD::NON_EXTLOAD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006175 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006176 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006177 if (VT == MVT::f32 || VT == MVT::f64) {
6178 // f32->i32 or f64->i64 one to one expansion.
6179 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006180 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006181 // Recursively expand the new load.
6182 if (getTypeAction(NVT) == Expand)
6183 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006184 break;
6185 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006186
Evan Cheng466685d2006-10-09 20:57:25 +00006187 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006188 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006189 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006190 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006191 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006192 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006193 Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006194 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006195
Evan Cheng466685d2006-10-09 20:57:25 +00006196 // Build a factor node to remember that this load is independent of the
6197 // other one.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006198 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006199 Hi.getValue(1));
Evan Cheng466685d2006-10-09 20:57:25 +00006200
6201 // Remember that we legalized the chain.
6202 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006203 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006204 std::swap(Lo, Hi);
6205 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006206 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006207
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006208 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6209 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006210 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dale Johannesen8a782a22009-02-02 22:12:50 +00006211 SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006212 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006213 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006214 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006215 ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
Evan Cheng548f6112006-12-13 03:19:57 +00006216 break;
6217 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006218
Evan Cheng466685d2006-10-09 20:57:25 +00006219 if (EVT == NVT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006220 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006221 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006222 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00006223 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006224 SVOffset, EVT, isVolatile,
6225 Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00006226
Evan Cheng466685d2006-10-09 20:57:25 +00006227 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006228 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006229
6230 if (ExtType == ISD::SEXTLOAD) {
6231 // The high part is obtained by SRA'ing all but one of the bits of the
6232 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006233 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006234 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Evan Cheng466685d2006-10-09 20:57:25 +00006235 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6236 } else if (ExtType == ISD::ZEXTLOAD) {
6237 // The high part is just a zero.
6238 Hi = DAG.getConstant(0, NVT);
6239 } else /* if (ExtType == ISD::EXTLOAD) */ {
6240 // The high part is undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00006241 Hi = DAG.getUNDEF(NVT);
Evan Cheng466685d2006-10-09 20:57:25 +00006242 }
6243 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006244 break;
6245 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006246 case ISD::AND:
6247 case ISD::OR:
6248 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006249 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006250 ExpandOp(Node->getOperand(0), LL, LH);
6251 ExpandOp(Node->getOperand(1), RL, RH);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006252 Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
6253 Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006254 break;
6255 }
6256 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006257 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006258 ExpandOp(Node->getOperand(1), LL, LH);
6259 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006260 if (getTypeAction(NVT) == Expand)
6261 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006262 Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006263 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006264 Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006265 break;
6266 }
Nate Begeman9373a812005-08-10 20:51:12 +00006267 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006268 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006269 ExpandOp(Node->getOperand(2), TL, TH);
6270 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006271 if (getTypeAction(NVT) == Expand)
6272 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006273 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Nate Begeman9373a812005-08-10 20:51:12 +00006274 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006275 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006276 Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Evan Cheng19103b12006-12-15 22:42:55 +00006277 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006278 break;
6279 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006280 case ISD::ANY_EXTEND:
6281 // The low part is any extension of the input (which degenerates to a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006282 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
Chris Lattner8137c9e2006-01-28 05:07:51 +00006283 // The high part is undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00006284 Hi = DAG.getUNDEF(NVT);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006285 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006286 case ISD::SIGN_EXTEND: {
6287 // The low part is just a sign extension of the input (which degenerates to
6288 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006289 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006290
Chris Lattner3e928bb2005-01-07 07:47:09 +00006291 // The high part is obtained by SRA'ing all but one of the bits of the lo
6292 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006293 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006294 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Chris Lattner8137c9e2006-01-28 05:07:51 +00006295 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006296 break;
6297 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006298 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006299 // The low part is just a zero extension of the input (which degenerates to
6300 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006301 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006302
Chris Lattner3e928bb2005-01-07 07:47:09 +00006303 // The high part is just a zero.
6304 Hi = DAG.getConstant(0, NVT);
6305 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006306
Chris Lattner4c948eb2007-02-13 23:55:16 +00006307 case ISD::TRUNCATE: {
6308 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006309 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006310 ExpandOp(Node->getOperand(0), NewLo, Hi);
Scott Michelfdc40a02009-02-17 22:15:04 +00006311
Chris Lattner4c948eb2007-02-13 23:55:16 +00006312 // The low part is now either the right size, or it is closer. If not the
6313 // right size, make an illegal truncate so we recursively expand it.
6314 if (NewLo.getValueType() != Node->getValueType(0))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006315 NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
Chris Lattner4c948eb2007-02-13 23:55:16 +00006316 ExpandOp(NewLo, Lo, Hi);
6317 break;
6318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006319
Chris Lattner35481892005-12-23 00:16:34 +00006320 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006322 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6323 // If the target wants to, allow it to lower this itself.
6324 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6325 case Expand: assert(0 && "cannot expand FP!");
6326 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6327 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6328 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006329 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006330 }
6331
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006332 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006333 if (VT == MVT::f32 || VT == MVT::f64) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006334 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006335 if (getTypeAction(NVT) == Expand)
6336 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006337 break;
6338 }
6339
6340 // If source operand will be expanded to the same type as VT, i.e.
6341 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006342 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006343 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6344 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006345 break;
6346 }
6347
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006348 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006349 if (Tmp.getNode() == 0)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006350 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00006351
Chris Lattner35481892005-12-23 00:16:34 +00006352 ExpandOp(Tmp, Lo, Hi);
6353 break;
6354 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006355
Chris Lattner27a6c732007-11-24 07:07:01 +00006356 case ISD::READCYCLECOUNTER: {
Scott Michelfdc40a02009-02-17 22:15:04 +00006357 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
Chris Lattner308575b2005-11-20 22:56:56 +00006358 TargetLowering::Custom &&
6359 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006360 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006361 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006362 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006363 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006364 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006365 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006366 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006367
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006368 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006369 // This operation does not need a loop.
6370 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6371 assert(Tmp.getNode() && "Node must be custom expanded!");
6372 ExpandOp(Tmp.getValue(0), Lo, Hi);
6373 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6374 LegalizeOp(Tmp.getValue(1)));
6375 break;
6376 }
6377
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006378 case ISD::ATOMIC_LOAD_ADD:
6379 case ISD::ATOMIC_LOAD_SUB:
6380 case ISD::ATOMIC_LOAD_AND:
6381 case ISD::ATOMIC_LOAD_OR:
6382 case ISD::ATOMIC_LOAD_XOR:
6383 case ISD::ATOMIC_LOAD_NAND:
6384 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006385 // These operations require a loop to be generated. We can't do that yet,
6386 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006387 SDValue In2Lo, In2Hi, In2;
6388 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006389 In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006390 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00006391 SDValue Replace =
Dale Johannesen8a782a22009-02-02 22:12:50 +00006392 DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006393 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006394 Anode->getSrcValue(), Anode->getAlignment());
6395 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006396 ExpandOp(Result.getValue(0), Lo, Hi);
6397 // Remember that we legalized the chain.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006398 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006399 break;
6400 }
6401
Chris Lattner4e6c7462005-01-08 19:27:05 +00006402 // These operators cannot be expanded directly, emit them as calls to
6403 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006404 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006405 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006406 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006407 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6408 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006409 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6410 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006411 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006412
Dale Johannesen8a782a22009-02-02 22:12:50 +00006413 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
Chris Lattnerf20d1832005-07-30 01:40:57 +00006414
Chris Lattner80a3e942005-07-29 00:33:32 +00006415 // Now that the custom expander is done, expand the result, which is still
6416 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006417 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006418 ExpandOp(Op, Lo, Hi);
6419 break;
6420 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006421 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006422
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006423 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6424 VT);
6425 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6426 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006427 break;
Evan Cheng56966222007-01-12 02:11:51 +00006428 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006429
Evan Cheng56966222007-01-12 02:11:51 +00006430 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006431 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006432 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6434 case Expand: assert(0 && "cannot expand FP!");
6435 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6436 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6437 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006438
Dale Johannesen8a782a22009-02-02 22:12:50 +00006439 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006440
6441 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006442 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006443 ExpandOp(Op, Lo, Hi);
6444 break;
6445 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006446 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006447
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006448 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6449 VT);
6450 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6451 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006452 break;
Evan Cheng56966222007-01-12 02:11:51 +00006453 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006454
Evan Cheng05a2d562006-01-09 18:31:59 +00006455 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006456 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006457 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006458 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006459 SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006460 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006461 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006462 // Now that the custom expander is done, expand the result, which is
6463 // still VT.
6464 ExpandOp(Op, Lo, Hi);
6465 break;
6466 }
6467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006468
6469 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
Chris Lattner79980b02006-09-13 03:50:39 +00006470 // this X << 1 as X+X.
6471 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006472 if (ShAmt->getAPIntValue() == 1 &&
Scott Michelfdc40a02009-02-17 22:15:04 +00006473 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006474 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006475 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006476 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6477 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6478 LoOps[1] = LoOps[0];
Dale Johannesen8a782a22009-02-02 22:12:50 +00006479 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattner79980b02006-09-13 03:50:39 +00006480
6481 HiOps[1] = HiOps[0];
6482 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006483 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattner79980b02006-09-13 03:50:39 +00006484 break;
6485 }
6486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006487
Chris Lattnere34b3962005-01-19 04:19:40 +00006488 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006489 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006490 break;
Chris Lattner47599822005-04-02 03:38:53 +00006491
6492 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006493 TargetLowering::LegalizeAction Action =
6494 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6495 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6496 Action == TargetLowering::Custom) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006497 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006498 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006499 break;
6500 }
6501
Chris Lattnere34b3962005-01-19 04:19:40 +00006502 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006503 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006504 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006505 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006506
Evan Cheng05a2d562006-01-09 18:31:59 +00006507 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006508 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006509 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006510 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dale Johannesene72c5962009-02-06 21:55:48 +00006511 SDValue Op = DAG.getNode(ISD::SRA, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006512 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006513 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006514 // Now that the custom expander is done, expand the result, which is
6515 // still VT.
6516 ExpandOp(Op, Lo, Hi);
6517 break;
6518 }
6519 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006520
Chris Lattnere34b3962005-01-19 04:19:40 +00006521 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006522 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006523 break;
Chris Lattner47599822005-04-02 03:38:53 +00006524
6525 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006526 TargetLowering::LegalizeAction Action =
6527 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6528 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6529 Action == TargetLowering::Custom) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006530 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006531 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006532 break;
6533 }
6534
Chris Lattnere34b3962005-01-19 04:19:40 +00006535 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006536 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006537 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006538 }
6539
6540 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006541 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006542 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006543 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006544 SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006545 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006546 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006547 // Now that the custom expander is done, expand the result, which is
6548 // still VT.
6549 ExpandOp(Op, Lo, Hi);
6550 break;
6551 }
6552 }
6553
Chris Lattnere34b3962005-01-19 04:19:40 +00006554 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006555 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006556 break;
Chris Lattner47599822005-04-02 03:38:53 +00006557
6558 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006559 TargetLowering::LegalizeAction Action =
6560 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6561 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6562 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006563 ExpandShiftParts(ISD::SRL_PARTS,
6564 Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006565 break;
6566 }
6567
Chris Lattnere34b3962005-01-19 04:19:40 +00006568 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006569 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006570 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006571 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006572
Misha Brukmanedf128a2005-04-21 22:36:52 +00006573 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006574 case ISD::SUB: {
6575 // If the target wants to custom expand this, let them.
6576 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6577 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006578 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006579 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006580 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006581 break;
6582 }
6583 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006584 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006585 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006586 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6587 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006588 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006589 LoOps[0] = LHSL;
6590 LoOps[1] = RHSL;
6591 HiOps[0] = LHSH;
6592 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006593
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006594 //cascaded check to see if any smaller size has a a carry flag.
6595 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6596 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006597 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6598 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006599 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006600 hasCarry = true;
6601 break;
6602 }
6603 }
6604
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006605 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006606 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006607 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006608 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006609 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006610 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006611 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006612 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006613 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006614 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006615 }
6616 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006617 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006618 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006619 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
6620 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
6621 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006622 Lo, LoOps[0], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006623 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
Scott Michelfdc40a02009-02-17 22:15:04 +00006624 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006625 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006626 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006627 Lo, LoOps[1], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006628 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
Scott Michelfdc40a02009-02-17 22:15:04 +00006629 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006630 Carry1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006631 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006632 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006633 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
6634 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
6635 SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
6636 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
Scott Michelfdc40a02009-02-17 22:15:04 +00006637 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006638 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006639 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006640 }
6641 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006642 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006644
Chris Lattnerb429f732007-05-17 18:15:41 +00006645 case ISD::ADDC:
6646 case ISD::SUBC: {
6647 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006648 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006649 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6650 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6651 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006652 SDValue LoOps[2] = { LHSL, RHSL };
6653 SDValue HiOps[3] = { LHSH, RHSH };
Scott Michelfdc40a02009-02-17 22:15:04 +00006654
Chris Lattnerb429f732007-05-17 18:15:41 +00006655 if (Node->getOpcode() == ISD::ADDC) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006656 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00006657 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006658 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006659 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006660 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00006661 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006662 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006663 }
6664 // Remember that we legalized the flag.
6665 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6666 break;
6667 }
6668 case ISD::ADDE:
6669 case ISD::SUBE: {
6670 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006671 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006672 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6673 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6674 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6676 SDValue HiOps[3] = { LHSH, RHSH };
Scott Michelfdc40a02009-02-17 22:15:04 +00006677
Dale Johannesen8a782a22009-02-02 22:12:50 +00006678 Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006679 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006680 Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006681
Chris Lattnerb429f732007-05-17 18:15:41 +00006682 // Remember that we legalized the flag.
6683 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6684 break;
6685 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006686 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006687 // If the target wants to custom expand this, let them.
6688 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006689 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006690 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006691 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006692 break;
6693 }
6694 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006695
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006696 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
6697 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
6698 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
6699 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006700 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006702 ExpandOp(Node->getOperand(0), LL, LH);
6703 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006704 unsigned OuterBitSize = Op.getValueSizeInBits();
6705 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006706 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6707 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006708 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6709 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6710 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006711 // The inputs are both zero-extended.
6712 if (HasUMUL_LOHI) {
6713 // We can emit a umul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006714 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006715 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006716 break;
6717 }
6718 if (HasMULHU) {
6719 // We can emit a mulhu+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006720 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6721 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00006722 break;
6723 }
Dan Gohman525178c2007-10-08 18:33:35 +00006724 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006725 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006726 // The input values are both sign-extended.
6727 if (HasSMUL_LOHI) {
6728 // We can emit a smul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006729 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006730 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006731 break;
6732 }
6733 if (HasMULHS) {
6734 // We can emit a mulhs+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006735 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6736 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00006737 break;
6738 }
6739 }
6740 if (HasUMUL_LOHI) {
6741 // Lo,Hi = umul LHS, RHS.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006742 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00006743 DAG.getVTList(NVT, NVT), LL, RL);
6744 Lo = UMulLOHI;
6745 Hi = UMulLOHI.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006746 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
6747 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
6748 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
6749 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006750 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006751 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006752 if (HasMULHU) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006753 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6754 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
6755 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
6756 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
6757 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
6758 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006759 break;
6760 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006761 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006762
Dan Gohman525178c2007-10-08 18:33:35 +00006763 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006764 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006765 break;
6766 }
Evan Cheng56966222007-01-12 02:11:51 +00006767 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006768 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006769 break;
6770 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006771 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006772 break;
6773 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006774 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006775 break;
6776 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006777 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006778 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006779
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006780 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006781 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6782 RTLIB::ADD_F64,
6783 RTLIB::ADD_F80,
6784 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006785 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006786 break;
6787 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006788 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6789 RTLIB::SUB_F64,
6790 RTLIB::SUB_F80,
6791 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006792 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006793 break;
6794 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00006795 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6796 RTLIB::MUL_F64,
6797 RTLIB::MUL_F80,
6798 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006799 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006800 break;
6801 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006802 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6803 RTLIB::DIV_F64,
6804 RTLIB::DIV_F80,
6805 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006806 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006807 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006808 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006809 if (VT == MVT::ppcf128) {
6810 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6811 Node->getOperand(0).getValueType()==MVT::f64);
6812 const uint64_t zero = 0;
6813 if (Node->getOperand(0).getValueType()==MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006814 Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006815 else
6816 Hi = Node->getOperand(0);
6817 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6818 break;
6819 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006820 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6821 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6822 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006823 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006824 }
6825 case ISD::FP_ROUND: {
6826 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6827 VT);
6828 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6829 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006830 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006831 }
Evan Cheng4b887022008-09-09 23:02:14 +00006832 case ISD::FSQRT:
6833 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00006834 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006835 case ISD::FLOG:
6836 case ISD::FLOG2:
6837 case ISD::FLOG10:
6838 case ISD::FEXP:
6839 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006840 case ISD::FTRUNC:
6841 case ISD::FFLOOR:
6842 case ISD::FCEIL:
6843 case ISD::FRINT:
6844 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00006845 case ISD::FPOW:
6846 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00006847 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006848 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006849 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006850 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6851 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006852 break;
6853 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006854 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6855 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006856 break;
6857 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006858 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6859 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006860 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006861 case ISD::FLOG:
6862 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
6863 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
6864 break;
6865 case ISD::FLOG2:
6866 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
6867 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
6868 break;
6869 case ISD::FLOG10:
6870 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
6871 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
6872 break;
6873 case ISD::FEXP:
6874 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
6875 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
6876 break;
6877 case ISD::FEXP2:
6878 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
6879 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
6880 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006881 case ISD::FTRUNC:
6882 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
6883 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
6884 break;
6885 case ISD::FFLOOR:
6886 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
6887 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
6888 break;
6889 case ISD::FCEIL:
6890 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
6891 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
6892 break;
6893 case ISD::FRINT:
6894 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
6895 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
6896 break;
6897 case ISD::FNEARBYINT:
6898 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
6899 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
6900 break;
Evan Cheng4b887022008-09-09 23:02:14 +00006901 case ISD::FPOW:
6902 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
6903 RTLIB::POW_PPCF128);
6904 break;
6905 case ISD::FPOWI:
6906 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
6907 RTLIB::POWI_PPCF128);
6908 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006909 default: assert(0 && "Unreachable!");
6910 }
Duncan Sands460a14e2008-04-12 17:14:18 +00006911 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006912 break;
6913 }
Evan Cheng966bf242006-12-16 00:52:40 +00006914 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006915 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006916 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00006917 ExpandOp(Node->getOperand(0), Lo, Tmp);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006918 Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
Dale Johannesenf6467742007-10-12 19:02:17 +00006919 // lo = hi==fabs(hi) ? lo : -lo;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006920 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006921 Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
6922 DAG.getCondCode(ISD::SETEQ));
Dale Johannesenf6467742007-10-12 19:02:17 +00006923 break;
6924 }
Dan Gohman475871a2008-07-27 21:46:04 +00006925 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006926 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6927 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006928 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
6929 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
6930 Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00006931 if (getTypeAction(NVT) == Expand)
6932 ExpandOp(Lo, Lo, Hi);
6933 break;
6934 }
6935 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006936 if (VT == MVT::ppcf128) {
6937 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006938 Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
6939 Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
Dale Johannesenf6467742007-10-12 19:02:17 +00006940 break;
6941 }
Dan Gohman475871a2008-07-27 21:46:04 +00006942 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006943 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6944 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006945 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
6946 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
6947 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00006948 if (getTypeAction(NVT) == Expand)
6949 ExpandOp(Lo, Lo, Hi);
6950 break;
6951 }
Evan Cheng912095b2007-01-04 21:56:39 +00006952 case ISD::FCOPYSIGN: {
6953 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6954 if (getTypeAction(NVT) == Expand)
6955 ExpandOp(Lo, Lo, Hi);
6956 break;
6957 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006958 case ISD::SINT_TO_FP:
6959 case ISD::UINT_TO_FP: {
6960 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006961 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00006962
6963 // Promote the operand if needed. Do this before checking for
6964 // ppcf128 so conversions of i16 and i8 work.
6965 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00006966 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00006967 Tmp = isSigned
Dale Johannesen8a782a22009-02-02 22:12:50 +00006968 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
Dale Johannesencf498192008-03-18 17:28:38 +00006969 DAG.getValueType(SrcVT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006970 : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006971 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00006972 SrcVT = Node->getOperand(0).getValueType();
6973 }
6974
Dan Gohmana2e94852008-03-10 23:03:31 +00006975 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006976 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006977 if (isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006978 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006979 Node->getOperand(0)));
6980 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6981 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006982 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Scott Michelfdc40a02009-02-17 22:15:04 +00006983 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006984 Node->getOperand(0)));
6985 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006986 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006987 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen8a782a22009-02-02 22:12:50 +00006988 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
6989 MVT::ppcf128, Node->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00006990 DAG.getConstant(0, MVT::i32),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006991 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006992 DAG.getConstantFP
6993 (APFloat(APInt(128, 2, TwoE32)),
6994 MVT::ppcf128)),
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006995 Hi,
6996 DAG.getCondCode(ISD::SETLT)),
6997 Lo, Hi);
6998 }
6999 break;
7000 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007001 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7002 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007003 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007004 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
7005 Node->getOperand(0)), Lo, Hi);
7006 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007007 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
Dale Johannesen8a782a22009-02-02 22:12:50 +00007008 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
7009 Node->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00007010 DAG.getConstant(0, MVT::i64),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007011 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007012 DAG.getConstantFP
7013 (APFloat(APInt(128, 2, TwoE64)),
7014 MVT::ppcf128)),
Dale Johannesen6e63e092007-10-12 17:52:03 +00007015 Hi,
7016 DAG.getCondCode(ISD::SETLT)),
7017 Lo, Hi);
7018 break;
7019 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007020
Dan Gohmana2e94852008-03-10 23:03:31 +00007021 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00007022 Node->getOperand(0), dl);
Evan Cheng110cf482008-04-01 01:50:16 +00007023 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007024 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007025 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007026 break;
7027 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007028 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007029
Chris Lattner83397362005-12-21 18:02:52 +00007030 // Make sure the resultant values have been legalized themselves, unless this
7031 // is a type that requires multi-step expansion.
7032 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7033 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007034 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007035 // Don't legalize the high part if it is expanded to a single node.
7036 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007037 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007038
7039 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007040 bool isNew =
7041 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007042 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007043 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007044}
7045
Dan Gohman7f321562007-06-25 16:23:39 +00007046/// SplitVectorOp - Given an operand of vector type, break it down into
7047/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007048void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7049 SDValue &Hi) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007050 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00007051 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007052 SDNode *Node = Op.getNode();
Dale Johannesenbb5da912009-02-02 20:41:04 +00007053 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007054 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007055 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007056
Duncan Sands83ec4b62008-06-06 12:08:01 +00007057 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007058
7059 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7060 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7061
Duncan Sands83ec4b62008-06-06 12:08:01 +00007062 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7063 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007064
Chris Lattnerc7029802006-03-18 01:44:44 +00007065 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007066 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007067 = SplitNodes.find(Op);
7068 if (I != SplitNodes.end()) {
7069 Lo = I->second.first;
7070 Hi = I->second.second;
7071 return;
7072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007073
Chris Lattnerc7029802006-03-18 01:44:44 +00007074 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007075 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007076#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007077 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007078#endif
7079 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007080 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00007081 Lo = DAG.getUNDEF(NewVT_Lo);
7082 Hi = DAG.getUNDEF(NewVT_Hi);
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007083 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007084 case ISD::BUILD_PAIR:
7085 Lo = Node->getOperand(0);
7086 Hi = Node->getOperand(1);
7087 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007088 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007089 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7090 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007091 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007092 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007093 if (Index < NewNumElts_Lo)
Dale Johannesenc6be1102009-02-02 22:49:46 +00007094 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007095 DAG.getIntPtrConstant(Index));
7096 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007097 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007098 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7099 break;
7100 }
Dan Gohman475871a2008-07-27 21:46:04 +00007101 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007102 Node->getOperand(1),
7103 Node->getOperand(2), dl);
Nate Begeman68679912008-04-25 18:07:40 +00007104 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007105 break;
7106 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007107 case ISD::VECTOR_SHUFFLE: {
7108 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007109 SDValue Mask = Node->getOperand(2);
7110 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007111 MVT PtrVT = TLI.getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00007112
7113 // Insert all of the elements from the input that are needed. We use
Chris Lattner6c9c6802007-11-19 21:16:54 +00007114 // buildvector of extractelement here because the input vectors will have
7115 // to be legalized, so this makes the code simpler.
7116 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007118 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007119 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007120 continue;
7121 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007122 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007123 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007124 if (Idx >= NumElements) {
7125 InVec = Node->getOperand(1);
7126 Idx -= NumElements;
7127 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007128 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007129 DAG.getConstant(Idx, PtrVT)));
7130 }
Evan Chenga87008d2009-02-25 22:49:59 +00007131 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007132 Ops.clear();
Scott Michelfdc40a02009-02-17 22:15:04 +00007133
Chris Lattner6c9c6802007-11-19 21:16:54 +00007134 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007135 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007136 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007137 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007138 continue;
7139 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007140 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007141 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007142 if (Idx >= NumElements) {
7143 InVec = Node->getOperand(1);
7144 Idx -= NumElements;
7145 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007146 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007147 DAG.getConstant(Idx, PtrVT)));
7148 }
Evan Chenga87008d2009-02-25 22:49:59 +00007149 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007150 break;
7151 }
Dan Gohman7f321562007-06-25 16:23:39 +00007152 case ISD::BUILD_VECTOR: {
Scott Michelfdc40a02009-02-17 22:15:04 +00007153 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007154 Node->op_begin()+NewNumElts_Lo);
Evan Chenga87008d2009-02-25 22:49:59 +00007155 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007156
Scott Michelfdc40a02009-02-17 22:15:04 +00007157 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007158 Node->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00007159 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007160 break;
7161 }
Dan Gohman7f321562007-06-25 16:23:39 +00007162 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007163 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007164 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7165 if (NewNumSubvectors == 1) {
7166 Lo = Node->getOperand(0);
7167 Hi = Node->getOperand(1);
7168 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007169 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7170 Node->op_begin()+NewNumSubvectors);
Scott Michelfdc40a02009-02-17 22:15:04 +00007171 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007172 &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007173
Mon P Wangaeb06d22008-11-10 04:46:22 +00007174 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007175 Node->op_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00007176 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007177 &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007178 }
Dan Gohman65956352007-06-13 15:12:02 +00007179 break;
7180 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007181 case ISD::EXTRACT_SUBVECTOR: {
7182 SDValue Vec = Op.getOperand(0);
7183 SDValue Idx = Op.getOperand(1);
7184 MVT IdxVT = Idx.getValueType();
7185
Dale Johannesenc6be1102009-02-02 22:49:46 +00007186 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007187 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7188 if (CIdx) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007189 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007190 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7191 IdxVT));
7192 } else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007193 Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007194 DAG.getConstant(NewNumElts_Lo, IdxVT));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007195 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007196 }
7197 break;
7198 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007199 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007200 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007201
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007203 SplitVectorOp(Node->getOperand(1), LL, LH);
7204 SplitVectorOp(Node->getOperand(2), RL, RH);
7205
Duncan Sands83ec4b62008-06-06 12:08:01 +00007206 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007207 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007208 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007209 SplitVectorOp(Cond, CL, CH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007210 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
7211 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007212 } else {
7213 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007214 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
7215 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007216 }
7217 break;
7218 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007219 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007220 SDValue CondLHS = Node->getOperand(0);
7221 SDValue CondRHS = Node->getOperand(1);
7222 SDValue CondCode = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00007223
Dan Gohman475871a2008-07-27 21:46:04 +00007224 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007225 SplitVectorOp(Node->getOperand(2), LL, LH);
7226 SplitVectorOp(Node->getOperand(3), RL, RH);
Scott Michelfdc40a02009-02-17 22:15:04 +00007227
Chris Lattner80c1a562008-06-30 02:43:01 +00007228 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007229 Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007230 LL, RL, CondCode);
Scott Michelfdc40a02009-02-17 22:15:04 +00007231 Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007232 LH, RH, CondCode);
7233 break;
7234 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007235 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007237 SplitVectorOp(Node->getOperand(0), LL, LH);
7238 SplitVectorOp(Node->getOperand(1), RL, RH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007239 Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
7240 Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
Nate Begemanb43e9c12008-05-12 19:40:03 +00007241 break;
7242 }
Dan Gohman7f321562007-06-25 16:23:39 +00007243 case ISD::ADD:
7244 case ISD::SUB:
7245 case ISD::MUL:
7246 case ISD::FADD:
7247 case ISD::FSUB:
7248 case ISD::FMUL:
7249 case ISD::SDIV:
7250 case ISD::UDIV:
7251 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007252 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007253 case ISD::AND:
7254 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007255 case ISD::XOR:
7256 case ISD::UREM:
7257 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007258 case ISD::FREM:
7259 case ISD::SHL:
7260 case ISD::SRA:
7261 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007262 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007263 SplitVectorOp(Node->getOperand(0), LL, LH);
7264 SplitVectorOp(Node->getOperand(1), RL, RH);
Scott Michelfdc40a02009-02-17 22:15:04 +00007265
Dale Johannesenc6be1102009-02-02 22:49:46 +00007266 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
7267 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007268 break;
7269 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007270 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007271 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007272 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007273 SplitVectorOp(Node->getOperand(0), L, H);
7274
Dale Johannesenc6be1102009-02-02 22:49:46 +00007275 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
7276 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007277 break;
7278 }
7279 case ISD::CTTZ:
7280 case ISD::CTLZ:
7281 case ISD::CTPOP:
7282 case ISD::FNEG:
7283 case ISD::FABS:
7284 case ISD::FSQRT:
7285 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007286 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007287 case ISD::FLOG:
7288 case ISD::FLOG2:
7289 case ISD::FLOG10:
7290 case ISD::FEXP:
7291 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007292 case ISD::FP_TO_SINT:
7293 case ISD::FP_TO_UINT:
7294 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007295 case ISD::UINT_TO_FP:
7296 case ISD::TRUNCATE:
7297 case ISD::ANY_EXTEND:
7298 case ISD::SIGN_EXTEND:
7299 case ISD::ZERO_EXTEND:
7300 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007301 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007302 SplitVectorOp(Node->getOperand(0), L, H);
7303
Dale Johannesenc6be1102009-02-02 22:49:46 +00007304 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
7305 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007306 break;
7307 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007308 case ISD::CONVERT_RNDSAT: {
7309 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7310 SDValue L, H;
7311 SplitVectorOp(Node->getOperand(0), L, H);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007312 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7313 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7314 SDValue STyOpL = DAG.getValueType(L.getValueType());
7315 SDValue STyOpH = DAG.getValueType(H.getValueType());
Mon P Wang77cdf302008-11-10 20:54:11 +00007316
7317 SDValue RndOp = Node->getOperand(3);
7318 SDValue SatOp = Node->getOperand(4);
7319
Dale Johannesenc460ae92009-02-04 00:13:36 +00007320 Lo = DAG.getConvertRndSat(NewVT_Lo, dl, L, DTyOpL, STyOpL,
Mon P Wang77cdf302008-11-10 20:54:11 +00007321 RndOp, SatOp, CvtCode);
Dale Johannesenc460ae92009-02-04 00:13:36 +00007322 Hi = DAG.getConvertRndSat(NewVT_Hi, dl, H, DTyOpH, STyOpH,
Mon P Wang77cdf302008-11-10 20:54:11 +00007323 RndOp, SatOp, CvtCode);
7324 break;
7325 }
Dan Gohman7f321562007-06-25 16:23:39 +00007326 case ISD::LOAD: {
7327 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007328 SDValue Ch = LD->getChain();
7329 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007330 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007331 const Value *SV = LD->getSrcValue();
7332 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007333 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007334 unsigned Alignment = LD->getAlignment();
7335 bool isVolatile = LD->isVolatile();
7336
Dan Gohman7f8613e2008-08-14 20:04:46 +00007337 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesene8d72302009-02-06 23:05:02 +00007338 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007339
7340 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7341 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7342 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7343
Dale Johannesenc6be1102009-02-02 22:49:46 +00007344 Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007345 NewVT_Lo, Ch, Ptr, Offset,
7346 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7347 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dale Johannesenc6be1102009-02-02 22:49:46 +00007348 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007349 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007350 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007351 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007352 Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007353 NewVT_Hi, Ch, Ptr, Offset,
7354 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00007355
Chris Lattnerc7029802006-03-18 01:44:44 +00007356 // Build a factor node to remember that this load is independent of the
7357 // other one.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007358 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007359 Hi.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00007360
Chris Lattnerc7029802006-03-18 01:44:44 +00007361 // Remember that we legalized the chain.
7362 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007363 break;
7364 }
Dan Gohman7f321562007-06-25 16:23:39 +00007365 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007366 // We know the result is a vector. The input may be either a vector or a
7367 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007368 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007369 if (!InOp.getValueType().isVector() ||
7370 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007371 // The input is a scalar or single-element vector.
7372 // Lower to a store/load so that it can be split.
7373 // FIXME: this could be improved probably.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007374 unsigned LdAlign = TLI.getTargetData()->
7375 getPrefTypeAlignment(Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007376 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007377 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007378
Dale Johannesenc6be1102009-02-02 22:49:46 +00007379 SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007380 InOp, Ptr,
7381 PseudoSourceValue::getFixedStack(FI), 0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007382 InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007383 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007384 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007385 // Split the vector and convert each of the pieces now.
7386 SplitVectorOp(InOp, Lo, Hi);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007387 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
7388 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007389 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007390 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007392
Chris Lattnerc7029802006-03-18 01:44:44 +00007393 // Remember in a map if the values will be reused later.
Scott Michelfdc40a02009-02-17 22:15:04 +00007394 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007395 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007396 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007397 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007398}
7399
7400
Dan Gohman89b20c02007-06-27 14:06:22 +00007401/// ScalarizeVectorOp - Given an operand of single-element vector type
7402/// (e.g. v1f32), convert it into the equivalent operation that returns a
7403/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007404SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007405 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00007406 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007407 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007408 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007409 MVT NewVT = Op.getValueType().getVectorElementType();
7410 assert(Op.getValueType().getVectorNumElements() == 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007411
Dan Gohman7f321562007-06-25 16:23:39 +00007412 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007413 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007414 if (I != ScalarizedNodes.end()) return I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00007415
Dan Gohman475871a2008-07-27 21:46:04 +00007416 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007417 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007418 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007419#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007420 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007421#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007422 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7423 case ISD::ADD:
7424 case ISD::FADD:
7425 case ISD::SUB:
7426 case ISD::FSUB:
7427 case ISD::MUL:
7428 case ISD::FMUL:
7429 case ISD::SDIV:
7430 case ISD::UDIV:
7431 case ISD::FDIV:
7432 case ISD::SREM:
7433 case ISD::UREM:
7434 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007435 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007436 case ISD::AND:
7437 case ISD::OR:
7438 case ISD::XOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007439 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007440 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007441 ScalarizeVectorOp(Node->getOperand(0)),
7442 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007443 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007444 case ISD::FNEG:
7445 case ISD::FABS:
7446 case ISD::FSQRT:
7447 case ISD::FSIN:
7448 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007449 case ISD::FLOG:
7450 case ISD::FLOG2:
7451 case ISD::FLOG10:
7452 case ISD::FEXP:
7453 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007454 case ISD::FP_TO_SINT:
7455 case ISD::FP_TO_UINT:
7456 case ISD::SINT_TO_FP:
7457 case ISD::UINT_TO_FP:
7458 case ISD::SIGN_EXTEND:
7459 case ISD::ZERO_EXTEND:
7460 case ISD::ANY_EXTEND:
7461 case ISD::TRUNCATE:
7462 case ISD::FP_EXTEND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007463 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007464 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007465 ScalarizeVectorOp(Node->getOperand(0)));
7466 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007467 case ISD::CONVERT_RNDSAT: {
7468 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
Dale Johannesenc460ae92009-02-04 00:13:36 +00007469 Result = DAG.getConvertRndSat(NewVT, dl, Op0,
Mon P Wang77cdf302008-11-10 20:54:11 +00007470 DAG.getValueType(NewVT),
7471 DAG.getValueType(Op0.getValueType()),
7472 Node->getOperand(3),
7473 Node->getOperand(4),
7474 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7475 break;
7476 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007477 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007478 case ISD::FP_ROUND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007479 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007480 NewVT,
Dan Gohman9e04c822007-10-12 14:13:46 +00007481 ScalarizeVectorOp(Node->getOperand(0)),
7482 Node->getOperand(1));
7483 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007484 case ISD::LOAD: {
7485 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007486 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7487 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007488 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007489 const Value *SV = LD->getSrcValue();
7490 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007491 MVT MemoryVT = LD->getMemoryVT();
7492 unsigned Alignment = LD->getAlignment();
7493 bool isVolatile = LD->isVolatile();
7494
7495 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesene8d72302009-02-06 23:05:02 +00007496 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00007497
Dale Johannesenc6be1102009-02-02 22:49:46 +00007498 Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007499 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7500 MemoryVT.getVectorElementType(),
7501 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007502
Chris Lattnerc7029802006-03-18 01:44:44 +00007503 // Remember that we legalized the chain.
7504 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7505 break;
7506 }
Dan Gohman7f321562007-06-25 16:23:39 +00007507 case ISD::BUILD_VECTOR:
7508 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007509 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007510 case ISD::INSERT_VECTOR_ELT:
7511 // Returning the inserted scalar element.
7512 Result = Node->getOperand(1);
7513 break;
7514 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007515 assert(Node->getOperand(0).getValueType() == NewVT &&
7516 "Concat of non-legal vectors not yet supported!");
7517 Result = Node->getOperand(0);
7518 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007519 case ISD::VECTOR_SHUFFLE: {
7520 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007521 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007522 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007523 Result = ScalarizeVectorOp(Node->getOperand(1));
7524 else
7525 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007526 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007527 }
7528 case ISD::EXTRACT_SUBVECTOR:
Scott Michelfdc40a02009-02-17 22:15:04 +00007529 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007530 Node->getOperand(0), Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007531 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007532 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007533 SDValue Op0 = Op.getOperand(0);
Dan Gohman4e3fdf22009-05-11 18:30:42 +00007534 if (Op0.getValueType().isVector() &&
7535 Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007536 Op0 = ScalarizeVectorOp(Op0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007537 Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007538 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007539 }
Dan Gohman7f321562007-06-25 16:23:39 +00007540 case ISD::SELECT:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007541 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007542 ScalarizeVectorOp(Op.getOperand(1)),
7543 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007544 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007545 case ISD::SELECT_CC:
Scott Michelfdc40a02009-02-17 22:15:04 +00007546 Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0),
Chris Lattner80c1a562008-06-30 02:43:01 +00007547 Node->getOperand(1),
7548 ScalarizeVectorOp(Op.getOperand(2)),
7549 ScalarizeVectorOp(Op.getOperand(3)),
7550 Node->getOperand(4));
7551 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007552 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007553 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7554 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007555 Result = DAG.getNode(ISD::SETCC, dl,
7556 TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00007557 Op0, Op1, Op.getOperand(2));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007558 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
Nate Begeman0d1704b2008-05-12 23:09:43 +00007559 DAG.getConstant(-1ULL, NewVT),
7560 DAG.getConstant(0ULL, NewVT));
7561 break;
7562 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007563 }
7564
7565 if (TLI.isTypeLegal(NewVT))
7566 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007567 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7568 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007569 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007570 return Result;
7571}
7572
Chris Lattner3e928bb2005-01-07 07:47:09 +00007573
Mon P Wang0c397192008-10-30 08:01:45 +00007574SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007575 assert(0 && "This should be dead!");
Mon P Wang0c397192008-10-30 08:01:45 +00007576 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7577 if (I != WidenNodes.end()) return I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00007578
Mon P Wang0c397192008-10-30 08:01:45 +00007579 MVT VT = Op.getValueType();
7580 assert(VT.isVector() && "Cannot widen non-vector type!");
7581
7582 SDValue Result;
7583 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007584 DebugLoc dl = Node->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00007585 MVT EVT = VT.getVectorElementType();
7586
7587 unsigned NumElts = VT.getVectorNumElements();
7588 unsigned NewNumElts = WidenVT.getVectorNumElements();
7589 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7590 assert(NewNumElts < 17);
7591
7592 // When widen is called, it is assumed that it is more efficient to use a
7593 // wide type. The default action is to widen to operation to a wider legal
7594 // vector type and then do the operation if it is legal by calling LegalizeOp
7595 // again. If there is no vector equivalent, we will unroll the operation, do
7596 // it, and rebuild the vector. If most of the operations are vectorizible to
7597 // the legal type, the resulting code will be more efficient. If this is not
7598 // the case, the resulting code will preform badly as we end up generating
7599 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007600 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007601 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007602 default:
Mon P Wang0c397192008-10-30 08:01:45 +00007603#ifndef NDEBUG
7604 Node->dump(&DAG);
7605#endif
7606 assert(0 && "Unexpected operation in WidenVectorOp!");
7607 break;
7608 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007609 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007610 case ISD::Constant:
7611 case ISD::ConstantFP:
7612 // To build a vector of these elements, clients should call BuildVector
7613 // and with each element instead of creating a node with a vector type
7614 assert(0 && "Unexpected operation in WidenVectorOp!");
7615 case ISD::VAARG:
7616 // Variable Arguments with vector types doesn't make any sense to me
7617 assert(0 && "Unexpected operation in WidenVectorOp!");
7618 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007619 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00007620 Result = DAG.getUNDEF(WidenVT);
Mon P Wang49292f12008-11-15 06:05:52 +00007621 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007622 case ISD::BUILD_VECTOR: {
7623 // Build a vector with undefined for the new nodes
7624 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7625 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007626 NewOps.push_back(DAG.getUNDEF(EVT));
Mon P Wang0c397192008-10-30 08:01:45 +00007627 }
Evan Chenga87008d2009-02-25 22:49:59 +00007628 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT,
7629 &NewOps[0], NewOps.size());
Mon P Wang0c397192008-10-30 08:01:45 +00007630 break;
7631 }
7632 case ISD::INSERT_VECTOR_ELT: {
7633 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007634 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00007635 Node->getOperand(1), Node->getOperand(2));
7636 break;
7637 }
7638 case ISD::VECTOR_SHUFFLE: {
7639 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7640 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
Nate Begeman9008ca62009-04-27 18:41:29 +00007641 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Node);
7642 SmallVector<int, 8> NewMask;
Mon P Wang0c397192008-10-30 08:01:45 +00007643 for (unsigned i = 0; i < NumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007644 int Idx = SVOp->getMaskElt(i);
7645 if (Idx < (int)NumElts)
7646 NewMask.push_back(Idx);
7647 else
7648 NewMask.push_back(Idx + NewNumElts - NumElts);
Mon P Wang0c397192008-10-30 08:01:45 +00007649 }
Nate Begeman9008ca62009-04-27 18:41:29 +00007650 for (unsigned i = NumElts; i < NewNumElts; ++i)
7651 NewMask.push_back(-1);
7652
7653 Result = DAG.getVectorShuffle(WidenVT, dl, Tmp1, Tmp2, &NewMask[0]);
Mon P Wang0c397192008-10-30 08:01:45 +00007654 break;
7655 }
7656 case ISD::LOAD: {
7657 // If the load widen returns true, we can use a single load for the
7658 // vector. Otherwise, it is returning a token factor for multiple
7659 // loads.
7660 SDValue TFOp;
7661 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7662 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7663 else
7664 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7665 break;
7666 }
7667
7668 case ISD::BIT_CONVERT: {
7669 SDValue Tmp1 = Node->getOperand(0);
7670 // Converts between two different types so we need to determine
7671 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007672 MVT InVT = Tmp1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007673 unsigned WidenSize = WidenVT.getSizeInBits();
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007674 if (InVT.isVector()) {
7675 MVT InEltVT = InVT.getVectorElementType();
7676 unsigned InEltSize = InEltVT.getSizeInBits();
7677 assert(WidenSize % InEltSize == 0 &&
7678 "can not widen bit convert that are not multiple of element type");
7679 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
7680 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
7681 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007682 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007683 } else {
7684 // If the result size is a multiple of the input size, widen the input
7685 // and then convert.
7686 unsigned InSize = InVT.getSizeInBits();
7687 assert(WidenSize % InSize == 0 &&
7688 "can not widen bit convert that are not multiple of element type");
7689 unsigned NewNumElts = WidenSize / InSize;
7690 SmallVector<SDValue, 16> Ops(NewNumElts);
Dale Johannesene8d72302009-02-06 23:05:02 +00007691 SDValue UndefVal = DAG.getUNDEF(InVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007692 Ops[0] = Tmp1;
7693 for (unsigned i = 1; i < NewNumElts; ++i)
7694 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00007695
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007696 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
Evan Chenga87008d2009-02-25 22:49:59 +00007697 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007698 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00007699 }
7700 break;
7701 }
7702
7703 case ISD::SINT_TO_FP:
7704 case ISD::UINT_TO_FP:
7705 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007706 case ISD::FP_TO_UINT:
7707 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00007708 SDValue Tmp1 = Node->getOperand(0);
7709 // Converts between two different types so we need to determine
7710 // the correct widen type for the input operand.
7711 MVT TVT = Tmp1.getValueType();
7712 assert(TVT.isVector() && "can not widen non vector type");
7713 MVT TEVT = TVT.getVectorElementType();
7714 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7715 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7716 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007717 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007718 break;
7719 }
7720
7721 case ISD::FP_EXTEND:
7722 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7723 case ISD::TRUNCATE:
7724 case ISD::SIGN_EXTEND:
7725 case ISD::ZERO_EXTEND:
7726 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00007727 case ISD::SIGN_EXTEND_INREG:
7728 case ISD::FABS:
7729 case ISD::FNEG:
7730 case ISD::FSQRT:
7731 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00007732 case ISD::FCOS:
7733 case ISD::CTPOP:
7734 case ISD::CTTZ:
7735 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00007736 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007737 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00007738 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7739 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007740 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007741 break;
7742 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007743 case ISD::CONVERT_RNDSAT: {
7744 SDValue RndOp = Node->getOperand(3);
7745 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00007746 SDValue SrcOp = Node->getOperand(0);
7747
7748 // Converts between two different types so we need to determine
7749 // the correct widen type for the input operand.
7750 MVT SVT = SrcOp.getValueType();
7751 assert(SVT.isVector() && "can not widen non vector type");
7752 MVT SEVT = SVT.getVectorElementType();
7753 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7754
7755 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7756 assert(SrcOp.getValueType() == WidenVT);
7757 SDValue DTyOp = DAG.getValueType(WidenVT);
7758 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
7759 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7760
Dale Johannesenc460ae92009-02-04 00:13:36 +00007761 Result = DAG.getConvertRndSat(WidenVT, dl, SrcOp, DTyOp, STyOp,
Mon P Wang77cdf302008-11-10 20:54:11 +00007762 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00007763 break;
7764 }
Mon P Wang0c397192008-10-30 08:01:45 +00007765 case ISD::FPOW:
Scott Michelfdc40a02009-02-17 22:15:04 +00007766 case ISD::FPOWI:
Mon P Wang0c397192008-10-30 08:01:45 +00007767 case ISD::ADD:
7768 case ISD::SUB:
7769 case ISD::MUL:
7770 case ISD::MULHS:
7771 case ISD::MULHU:
7772 case ISD::AND:
7773 case ISD::OR:
7774 case ISD::XOR:
7775 case ISD::FADD:
7776 case ISD::FSUB:
7777 case ISD::FMUL:
7778 case ISD::SDIV:
7779 case ISD::SREM:
7780 case ISD::FDIV:
7781 case ISD::FREM:
7782 case ISD::FCOPYSIGN:
7783 case ISD::UDIV:
7784 case ISD::UREM:
7785 case ISD::BSWAP: {
7786 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00007787 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7788 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7789 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007790 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00007791 break;
7792 }
7793
7794 case ISD::SHL:
7795 case ISD::SRA:
7796 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00007797 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7798 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00007799 SDValue ShOp = Node->getOperand(1);
7800 MVT ShVT = ShOp.getValueType();
7801 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
7802 WidenVT.getVectorNumElements());
7803 ShOp = WidenVectorOp(ShOp, NewShVT);
7804 assert(ShOp.getValueType() == NewShVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007805 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00007806 break;
7807 }
Mon P Wangfb13f002008-12-02 07:35:08 +00007808
Mon P Wang0c397192008-10-30 08:01:45 +00007809 case ISD::EXTRACT_VECTOR_ELT: {
7810 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7811 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007812 Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
Mon P Wang0c397192008-10-30 08:01:45 +00007813 break;
7814 }
7815 case ISD::CONCAT_VECTORS: {
7816 // We concurrently support only widen on a multiple of the incoming vector.
7817 // We could widen on a multiple of the incoming operand if necessary.
7818 unsigned NumConcat = NewNumElts / NumElts;
7819 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Dale Johannesene8d72302009-02-06 23:05:02 +00007820 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang0c397192008-10-30 08:01:45 +00007821 SmallVector<SDValue, 8> MOps;
7822 MOps.push_back(Op);
7823 for (unsigned i = 1; i != NumConcat; ++i) {
7824 MOps.push_back(UndefVal);
7825 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007826 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang0c397192008-10-30 08:01:45 +00007827 &MOps[0], MOps.size()));
7828 break;
7829 }
7830 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00007831 SDValue Tmp1 = Node->getOperand(0);
7832 SDValue Idx = Node->getOperand(1);
7833 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7834 if (CIdx && CIdx->getZExtValue() == 0) {
7835 // Since we are access the start of the vector, the incoming
7836 // vector type might be the proper.
7837 MVT Tmp1VT = Tmp1.getValueType();
7838 if (Tmp1VT == WidenVT)
7839 return Tmp1;
7840 else {
7841 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
7842 if (Tmp1VTNumElts < NewNumElts)
7843 Result = WidenVectorOp(Tmp1, WidenVT);
7844 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007845 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
Mon P Wang49292f12008-11-15 06:05:52 +00007846 }
7847 } else if (NewNumElts % NumElts == 0) {
7848 // Widen the extracted subvector.
7849 unsigned NumConcat = NewNumElts / NumElts;
Dale Johannesene8d72302009-02-06 23:05:02 +00007850 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang49292f12008-11-15 06:05:52 +00007851 SmallVector<SDValue, 8> MOps;
7852 MOps.push_back(Op);
7853 for (unsigned i = 1; i != NumConcat; ++i) {
7854 MOps.push_back(UndefVal);
7855 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007856 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang49292f12008-11-15 06:05:52 +00007857 &MOps[0], MOps.size()));
7858 } else {
7859 assert(0 && "can not widen extract subvector");
7860 // This could be implemented using insert and build vector but I would
7861 // like to see when this happens.
7862 }
Mon P Wang0c397192008-10-30 08:01:45 +00007863 break;
7864 }
7865
7866 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00007867 // Determine new condition widen type and widen
7868 SDValue Cond1 = Node->getOperand(0);
7869 MVT CondVT = Cond1.getValueType();
7870 assert(CondVT.isVector() && "can not widen non vector type");
7871 MVT CondEVT = CondVT.getVectorElementType();
7872 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7873 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7874 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
7875
7876 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
7877 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
7878 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007879 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00007880 break;
7881 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007882
Mon P Wang0c397192008-10-30 08:01:45 +00007883 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00007884 // Determine new condition widen type and widen
7885 SDValue Cond1 = Node->getOperand(0);
7886 SDValue Cond2 = Node->getOperand(1);
7887 MVT CondVT = Cond1.getValueType();
7888 assert(CondVT.isVector() && "can not widen non vector type");
7889 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
7890 MVT CondEVT = CondVT.getVectorElementType();
7891 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7892 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7893 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
7894 assert(Cond1.getValueType() == CondWidenVT &&
7895 Cond2.getValueType() == CondWidenVT && "condition not widen");
7896
7897 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
7898 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
7899 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
7900 "operands not widen");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007901 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00007902 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00007903 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00007904 }
7905 case ISD::VSETCC: {
7906 // Determine widen for the operand
7907 SDValue Tmp1 = Node->getOperand(0);
7908 MVT TmpVT = Tmp1.getValueType();
7909 assert(TmpVT.isVector() && "can not widen non vector type");
7910 MVT TmpEVT = TmpVT.getVectorElementType();
7911 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
7912 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
7913 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007914 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00007915 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00007916 break;
7917 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007918 case ISD::ATOMIC_CMP_SWAP:
7919 case ISD::ATOMIC_LOAD_ADD:
7920 case ISD::ATOMIC_LOAD_SUB:
7921 case ISD::ATOMIC_LOAD_AND:
7922 case ISD::ATOMIC_LOAD_OR:
7923 case ISD::ATOMIC_LOAD_XOR:
7924 case ISD::ATOMIC_LOAD_NAND:
7925 case ISD::ATOMIC_LOAD_MIN:
7926 case ISD::ATOMIC_LOAD_MAX:
7927 case ISD::ATOMIC_LOAD_UMIN:
7928 case ISD::ATOMIC_LOAD_UMAX:
7929 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00007930 // For now, we assume that using vectors for these operations don't make
7931 // much sense so we just split it. We return an empty result
7932 SDValue X, Y;
7933 SplitVectorOp(Op, X, Y);
7934 return Result;
7935 break;
7936 }
7937
7938 } // end switch (Node->getOpcode())
7939
Scott Michelfdc40a02009-02-17 22:15:04 +00007940 assert(Result.getNode() && "Didn't set a result!");
Mon P Wang0c397192008-10-30 08:01:45 +00007941 if (Result != Op)
7942 Result = LegalizeOp(Result);
7943
Mon P Wangf007a8b2008-11-06 05:31:54 +00007944 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00007945 return Result;
7946}
7947
7948// Utility function to find a legal vector type and its associated element
7949// type from a preferred width and whose vector type must be the same size
7950// as the VVT.
7951// TLI: Target lowering used to determine legal types
7952// Width: Preferred width of element type
7953// VVT: Vector value type whose size we must match.
7954// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00007955static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00007956 MVT& EVT, MVT& VecEVT) {
7957 // We start with the preferred width, make it a power of 2 and see if
7958 // we can find a vector type of that width. If not, we reduce it by
7959 // another power of 2. If we have widen the type, a vector of bytes should
7960 // always be legal.
7961 assert(TLI.isTypeLegal(VVT));
7962 unsigned EWidth = Width + 1;
7963 do {
7964 assert(EWidth > 0);
7965 EWidth = (1 << Log2_32(EWidth-1));
7966 EVT = MVT::getIntegerVT(EWidth);
7967 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
7968 VecEVT = MVT::getVectorVT(EVT, NumEVT);
7969 } while (!TLI.isTypeLegal(VecEVT) ||
7970 VVT.getSizeInBits() != VecEVT.getSizeInBits());
7971}
7972
7973SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
7974 SDValue Chain,
7975 SDValue BasePtr,
7976 const Value *SV,
7977 int SVOffset,
7978 unsigned Alignment,
7979 bool isVolatile,
7980 unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007981 MVT ResType,
7982 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00007983 // We assume that we have good rules to handle loading power of two loads so
7984 // we break down the operations to power of 2 loads. The strategy is to
7985 // load the largest power of 2 that we can easily transform to a legal vector
7986 // and then insert into that vector, and the cast the result into the legal
7987 // vector that we want. This avoids unnecessary stack converts.
7988 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
7989 // the load is nonvolatile, we an use a wider load for the value.
7990 // Find a vector length we can load a large chunk
7991 MVT EVT, VecEVT;
7992 unsigned EVTWidth;
7993 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
7994 EVTWidth = EVT.getSizeInBits();
7995
Dale Johannesenc6be1102009-02-02 22:49:46 +00007996 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007997 isVolatile, Alignment);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007998 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
Mon P Wang0c397192008-10-30 08:01:45 +00007999 LdChain.push_back(LdOp.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00008000
Mon P Wang0c397192008-10-30 08:01:45 +00008001 // Check if we can load the element with one instruction
8002 if (LdWidth == EVTWidth) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008003 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008004 }
8005
8006 // The vector element order is endianness dependent.
8007 unsigned Idx = 1;
8008 LdWidth -= EVTWidth;
8009 unsigned Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008010
Mon P Wang0c397192008-10-30 08:01:45 +00008011 while (LdWidth > 0) {
8012 unsigned Increment = EVTWidth / 8;
8013 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008014 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008015 DAG.getIntPtrConstant(Increment));
8016
8017 if (LdWidth < EVTWidth) {
8018 // Our current type we are using is too large, use a smaller size by
8019 // using a smaller power of 2
8020 unsigned oEVTWidth = EVTWidth;
8021 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8022 EVTWidth = EVT.getSizeInBits();
8023 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008024 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008025 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008027
Dale Johannesenc6be1102009-02-02 22:49:46 +00008028 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008029 SVOffset+Offset, isVolatile,
8030 MinAlign(Alignment, Offset));
Mon P Wang0c397192008-10-30 08:01:45 +00008031 LdChain.push_back(LdOp.getValue(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008032 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008033 DAG.getIntPtrConstant(Idx++));
Scott Michelfdc40a02009-02-17 22:15:04 +00008034
Mon P Wang0c397192008-10-30 08:01:45 +00008035 LdWidth -= EVTWidth;
8036 }
8037
Dale Johannesenc6be1102009-02-02 22:49:46 +00008038 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008039}
8040
8041bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8042 SDValue& TFOp,
8043 SDValue Op,
8044 MVT NVT) {
8045 // TODO: Add support for ConcatVec and the ability to load many vector
8046 // types (e.g., v4i8). This will not work when a vector register
8047 // to memory mapping is strange (e.g., vector elements are not
8048 // stored in some sequential order).
8049
Scott Michelfdc40a02009-02-17 22:15:04 +00008050 // It must be true that the widen vector type is bigger than where
Mon P Wang0c397192008-10-30 08:01:45 +00008051 // we need to load from.
8052 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8053 MVT LdVT = LD->getMemoryVT();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008054 DebugLoc dl = LD->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008055 assert(LdVT.isVector() && NVT.isVector());
8056 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
Scott Michelfdc40a02009-02-17 22:15:04 +00008057
Mon P Wang0c397192008-10-30 08:01:45 +00008058 // Load information
8059 SDValue Chain = LD->getChain();
8060 SDValue BasePtr = LD->getBasePtr();
8061 int SVOffset = LD->getSrcValueOffset();
8062 unsigned Alignment = LD->getAlignment();
8063 bool isVolatile = LD->isVolatile();
8064 const Value *SV = LD->getSrcValue();
8065 unsigned int LdWidth = LdVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00008066
Mon P Wang0c397192008-10-30 08:01:45 +00008067 // Load value as a large register
8068 SDValueVector LdChain;
8069 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008070 Alignment, isVolatile, LdWidth, NVT, dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008071
8072 if (LdChain.size() == 1) {
8073 TFOp = LdChain[0];
8074 return true;
8075 }
8076 else {
Scott Michelfdc40a02009-02-17 22:15:04 +00008077 TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008078 &LdChain[0], LdChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008079 return false;
8080 }
8081}
8082
8083
8084void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8085 SDValue Chain,
8086 SDValue BasePtr,
8087 const Value *SV,
8088 int SVOffset,
8089 unsigned Alignment,
8090 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008091 SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008092 unsigned StWidth,
8093 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008094 // Breaks the stores into a series of power of 2 width stores. For any
8095 // width, we convert the vector to the vector of element size that we
8096 // want to store. This avoids requiring a stack convert.
Scott Michelfdc40a02009-02-17 22:15:04 +00008097
Mon P Wang0c397192008-10-30 08:01:45 +00008098 // Find a width of the element type we can store with
8099 MVT VVT = ValOp.getValueType();
8100 MVT EVT, VecEVT;
8101 unsigned EVTWidth;
8102 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8103 EVTWidth = EVT.getSizeInBits();
8104
Dale Johannesenc6be1102009-02-02 22:49:46 +00008105 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
8106 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008107 DAG.getIntPtrConstant(0));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008108 SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008109 isVolatile, Alignment);
Mon P Wang0c397192008-10-30 08:01:45 +00008110 StChain.push_back(StOp);
8111
8112 // Check if we are done
8113 if (StWidth == EVTWidth) {
8114 return;
8115 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008116
Mon P Wang0c397192008-10-30 08:01:45 +00008117 unsigned Idx = 1;
8118 StWidth -= EVTWidth;
8119 unsigned Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008120
Mon P Wang0c397192008-10-30 08:01:45 +00008121 while (StWidth > 0) {
8122 unsigned Increment = EVTWidth / 8;
8123 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008124 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008125 DAG.getIntPtrConstant(Increment));
Scott Michelfdc40a02009-02-17 22:15:04 +00008126
Mon P Wang0c397192008-10-30 08:01:45 +00008127 if (StWidth < EVTWidth) {
8128 // Our current type we are using is too large, use a smaller size by
8129 // using a smaller power of 2
8130 unsigned oEVTWidth = EVTWidth;
8131 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8132 EVTWidth = EVT.getSizeInBits();
8133 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008134 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008135 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008137
Dale Johannesenc6be1102009-02-02 22:49:46 +00008138 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008139 DAG.getIntPtrConstant(Idx++));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008140 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008141 SVOffset + Offset, isVolatile,
8142 MinAlign(Alignment, Offset)));
8143 StWidth -= EVTWidth;
8144 }
8145}
8146
8147
8148SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008149 SDValue Chain,
8150 SDValue BasePtr) {
Mon P Wang0c397192008-10-30 08:01:45 +00008151 // TODO: It might be cleaner if we can use SplitVector and have more legal
8152 // vector types that can be stored into memory (e.g., v4xi8 can
8153 // be stored as a word). This will not work when a vector register
8154 // to memory mapping is strange (e.g., vector elements are not
8155 // stored in some sequential order).
Scott Michelfdc40a02009-02-17 22:15:04 +00008156
Mon P Wang0c397192008-10-30 08:01:45 +00008157 MVT StVT = ST->getMemoryVT();
8158 SDValue ValOp = ST->getValue();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008159 DebugLoc dl = ST->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008160
8161 // Check if we have widen this node with another value
8162 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8163 if (I != WidenNodes.end())
8164 ValOp = I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00008165
Mon P Wang0c397192008-10-30 08:01:45 +00008166 MVT VVT = ValOp.getValueType();
8167
8168 // It must be true that we the widen vector type is bigger than where
8169 // we need to store.
8170 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008171 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008172 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8173
8174 // Store value
8175 SDValueVector StChain;
8176 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8177 ST->getSrcValueOffset(), ST->getAlignment(),
Dale Johannesenc6be1102009-02-02 22:49:46 +00008178 ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008179 if (StChain.size() == 1)
8180 return StChain[0];
Scott Michelfdc40a02009-02-17 22:15:04 +00008181 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008182 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8183 &StChain[0], StChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008184}
8185
8186
Chris Lattner3e928bb2005-01-07 07:47:09 +00008187// SelectionDAG::Legalize - This is the entry point for the file.
8188//
Bill Wendling98a366d2009-04-29 23:29:43 +00008189void SelectionDAG::Legalize(bool TypesNeedLegalizing,
8190 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008191 /// run - This is the main entry point to this class.
8192 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00008193 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008194}
8195