blob: c80b125228d13dcdb2bb24448d745e77e377c5b1 [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: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00003145 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00003146 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00003147 "Ugly special-cased code!");
3148 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00003149 SDValue SignBit;
3150 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
3151 if (isTypeLegal(IVT)) {
3152 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
3153 } else {
3154 assert(isTypeLegal(TLI.getPointerTy()) &&
3155 (TLI.getPointerTy() == MVT::i32 ||
3156 TLI.getPointerTy() == MVT::i64) &&
3157 "Legal type for load?!");
3158 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
3159 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
3160 SDValue Ch =
3161 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
3162 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
3163 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
3164 LoadPtr, DAG.getIntPtrConstant(4));
3165 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
3166 Ch, LoadPtr, NULL, 0, MVT::i32);
3167 }
3168 SignBit =
3169 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
3170 SignBit, DAG.getConstant(0, SignBit.getValueType()),
3171 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00003172 // Get the absolute value of the result.
3173 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
3174 // Select between the nabs and abs value based on the sign bit of
3175 // the input.
3176 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
3177 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
3178 AbsVal),
3179 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00003180 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003181 break;
3182 }
Evan Cheng912095b2007-01-04 21:56:39 +00003183 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003184 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003185
Nate Begeman551bf3f2006-02-17 05:43:56 +00003186 case ISD::ADDC:
3187 case ISD::SUBC:
3188 Tmp1 = LegalizeOp(Node->getOperand(0));
3189 Tmp2 = LegalizeOp(Node->getOperand(1));
3190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003191 Tmp3 = Result.getValue(0);
3192 Tmp4 = Result.getValue(1);
3193
3194 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3195 default: assert(0 && "This action is not supported yet!");
3196 case TargetLowering::Legal:
3197 break;
3198 case TargetLowering::Custom:
3199 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3200 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003201 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003202 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3203 }
3204 break;
3205 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003206 // Since this produces two values, make sure to remember that we legalized
3207 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003208 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3209 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3210 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003211
Nate Begeman551bf3f2006-02-17 05:43:56 +00003212 case ISD::ADDE:
3213 case ISD::SUBE:
3214 Tmp1 = LegalizeOp(Node->getOperand(0));
3215 Tmp2 = LegalizeOp(Node->getOperand(1));
3216 Tmp3 = LegalizeOp(Node->getOperand(2));
3217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003218 Tmp3 = Result.getValue(0);
3219 Tmp4 = Result.getValue(1);
3220
3221 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3222 default: assert(0 && "This action is not supported yet!");
3223 case TargetLowering::Legal:
3224 break;
3225 case TargetLowering::Custom:
3226 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3227 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003228 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003229 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3230 }
3231 break;
3232 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003233 // Since this produces two values, make sure to remember that we legalized
3234 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003235 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3236 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3237 return Op.getResNo() ? Tmp4 : Tmp3;
Scott Michelfdc40a02009-02-17 22:15:04 +00003238
Nate Begeman419f8b62005-10-18 00:27:41 +00003239 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003240 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003241 // TODO: handle the case where the Lo and Hi operands are not of legal type
3242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3244 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003245 case TargetLowering::Promote:
3246 case TargetLowering::Custom:
3247 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003248 case TargetLowering::Legal:
3249 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00003250 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003251 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003252 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00003253 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
3254 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
3255 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003256 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003257 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00003258 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003259 break;
3260 }
3261 break;
3262 }
3263
Nate Begemanc105e192005-04-06 00:23:54 +00003264 case ISD::UREM:
3265 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003266 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003267 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3268 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003269
Nate Begemanc105e192005-04-06 00:23:54 +00003270 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003271 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3272 case TargetLowering::Custom:
3273 isCustom = true;
3274 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003275 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003276 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003277 if (isCustom) {
3278 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003279 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003280 }
Nate Begemanc105e192005-04-06 00:23:54 +00003281 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003282 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003283 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003284 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003285 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003286
Dan Gohman525178c2007-10-08 18:33:35 +00003287 // See if remainder can be lowered using two-result operations.
3288 SDVTList VTs = DAG.getVTList(VT, VT);
3289 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003290 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003291 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00003292 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003293 break;
3294 }
3295 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003296 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003297 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3298 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003299 break;
3300 }
3301
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00003302 if (VT.isInteger() &&
3303 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
3304 // X % Y -> X-X/Y*Y
3305 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3306 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3307 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
3308 break;
Nate Begemanc105e192005-04-06 00:23:54 +00003309 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00003310
3311 // Check to see if we have a libcall for this operator.
3312 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3313 switch (Node->getOpcode()) {
3314 default: break;
3315 case ISD::UREM:
3316 case ISD::SREM:
3317 if (VT == MVT::i16)
3318 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
3319 else if (VT == MVT::i32)
3320 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
3321 else if (VT == MVT::i64)
3322 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
3323 else if (VT == MVT::i128)
3324 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
3325 break;
3326 case ISD::FREM:
3327 // Floating point mod -> fmod libcall.
3328 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3329 RTLIB::REM_F80, RTLIB::REM_PPCF128);
3330 break;
3331 }
3332
3333 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3334 SDValue Dummy;
3335 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
3336 break;
3337 }
3338
3339 assert(VT.isVector() &&
3340 "Cannot expand this binary operator!");
3341 // Expand the operation into a bunch of nasty scalar code.
3342 Result = LegalizeOp(UnrollVectorOp(Op));
Nate Begemanc105e192005-04-06 00:23:54 +00003343 break;
3344 }
Dan Gohman525178c2007-10-08 18:33:35 +00003345 }
Nate Begemanc105e192005-04-06 00:23:54 +00003346 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003347 case ISD::VAARG: {
3348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3349 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3350
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003352 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3353 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003354 case TargetLowering::Custom:
3355 isCustom = true;
3356 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003357 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3359 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003360 Tmp1 = Result.getValue(1);
3361
3362 if (isCustom) {
3363 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003364 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003365 Result = LegalizeOp(Tmp2);
3366 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3367 }
3368 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003369 break;
3370 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003371 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003372 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003373 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00003374 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003375 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00003376 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003377 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003378 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00003379 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003380 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00003381 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003382 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003383 Result = LegalizeOp(Result);
3384 break;
3385 }
3386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003387 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00003388 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003389 AddLegalizedOperand(SDValue(Node, 0), Result);
3390 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003391 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003393
3394 case ISD::VACOPY:
Nate Begemanacc398c2006-01-25 18:21:52 +00003395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3396 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3397 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3398
3399 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3400 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003401 case TargetLowering::Custom:
3402 isCustom = true;
3403 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003404 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003405 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3406 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003407 if (isCustom) {
3408 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003409 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003410 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003411 break;
3412 case TargetLowering::Expand:
3413 // This defaults to loading a pointer from the input and storing it to the
3414 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003415 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3416 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003417 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3418 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003419 break;
3420 }
3421 break;
3422
Scott Michelfdc40a02009-02-17 22:15:04 +00003423 case ISD::VAEND:
Nate Begemanacc398c2006-01-25 18:21:52 +00003424 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3425 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3426
3427 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3428 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003429 case TargetLowering::Custom:
3430 isCustom = true;
3431 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003432 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003433 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003434 if (isCustom) {
3435 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003436 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003437 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003438 break;
3439 case TargetLowering::Expand:
3440 Result = Tmp1; // Default to a no-op, return the chain
3441 break;
3442 }
3443 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003444
3445 case ISD::VASTART:
Nate Begemanacc398c2006-01-25 18:21:52 +00003446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3448
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003449 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Nate Begemanacc398c2006-01-25 18:21:52 +00003451 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3452 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003453 case TargetLowering::Legal: break;
3454 case TargetLowering::Custom:
3455 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003456 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003457 break;
3458 }
3459 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003460
Nate Begeman35ef9132006-01-11 21:21:00 +00003461 case ISD::ROTL:
3462 case ISD::ROTR:
3463 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003464 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003466 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3467 default:
3468 assert(0 && "ROTL/ROTR legalize operation not supported");
3469 break;
3470 case TargetLowering::Legal:
3471 break;
3472 case TargetLowering::Custom:
3473 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003474 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003475 break;
3476 case TargetLowering::Promote:
3477 assert(0 && "Do not know how to promote ROTL/ROTR");
3478 break;
3479 case TargetLowering::Expand:
3480 assert(0 && "Do not know how to expand ROTL/ROTR");
3481 break;
3482 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003483 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003484
Nate Begemand88fc032006-01-14 03:14:10 +00003485 case ISD::BSWAP:
3486 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3487 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003488 case TargetLowering::Custom:
3489 assert(0 && "Cannot custom legalize this yet!");
3490 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003491 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003492 break;
3493 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003494 MVT OVT = Tmp1.getValueType();
3495 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3496 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003497
Dale Johannesenca57b842009-02-02 23:46:53 +00003498 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3499 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3500 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3502 break;
3503 }
3504 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003505 Result = ExpandBSWAP(Tmp1, dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00003506 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003507 }
3508 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003509
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003510 case ISD::CTPOP:
3511 case ISD::CTTZ:
3512 case ISD::CTLZ:
3513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3514 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003515 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003516 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003517 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003518 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003519 TargetLowering::Custom) {
3520 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003521 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003522 Result = Tmp1;
3523 }
Scott Michel910b66d2007-07-30 21:00:31 +00003524 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003525 break;
3526 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527 MVT OVT = Tmp1.getValueType();
3528 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003529
3530 // Zero extend the argument.
Dale Johannesenca57b842009-02-02 23:46:53 +00003531 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003532 // Perform the larger operation, then subtract if needed.
Dale Johannesenca57b842009-02-02 23:46:53 +00003533 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003534 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003535 case ISD::CTPOP:
3536 Result = Tmp1;
3537 break;
3538 case ISD::CTTZ:
3539 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenca57b842009-02-02 23:46:53 +00003540 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3541 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003542 ISD::SETEQ);
Dale Johannesenca57b842009-02-02 23:46:53 +00003543 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003545 break;
3546 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003547 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenca57b842009-02-02 23:46:53 +00003548 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 DAG.getConstant(NVT.getSizeInBits() -
3550 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003551 break;
3552 }
3553 break;
3554 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003555 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003556 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003557 break;
3558 }
3559 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003560
Chris Lattner2c8086f2005-04-02 05:00:07 +00003561 // Unary operators
3562 case ISD::FABS:
3563 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003564 case ISD::FSQRT:
3565 case ISD::FSIN:
3566 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003567 case ISD::FLOG:
3568 case ISD::FLOG2:
3569 case ISD::FLOG10:
3570 case ISD::FEXP:
3571 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003572 case ISD::FTRUNC:
3573 case ISD::FFLOOR:
3574 case ISD::FCEIL:
3575 case ISD::FRINT:
3576 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003577 Tmp1 = LegalizeOp(Node->getOperand(0));
3578 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003579 case TargetLowering::Promote:
3580 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003581 isCustom = true;
3582 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003583 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003584 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003585 if (isCustom) {
3586 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003587 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003588 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003589 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003590 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003591 switch (Node->getOpcode()) {
3592 default: assert(0 && "Unreachable!");
3593 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003594 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3595 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00003596 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003597 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003598 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003599 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003600 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003601 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003602 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003603 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003604 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3605 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003606 break;
3607 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003608 case ISD::FSQRT:
3609 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00003610 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003611 case ISD::FLOG:
3612 case ISD::FLOG2:
3613 case ISD::FLOG10:
3614 case ISD::FEXP:
3615 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003616 case ISD::FTRUNC:
3617 case ISD::FFLOOR:
3618 case ISD::FCEIL:
3619 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003620 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003621 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003622
3623 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003624 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003625 Result = LegalizeOp(UnrollVectorOp(Op));
3626 break;
3627 }
3628
Evan Cheng56966222007-01-12 02:11:51 +00003629 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003630 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003631 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003632 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3633 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003634 break;
3635 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003636 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3637 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003638 break;
3639 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003640 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3641 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003642 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003643 case ISD::FLOG:
3644 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3645 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3646 break;
3647 case ISD::FLOG2:
3648 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3649 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3650 break;
3651 case ISD::FLOG10:
3652 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3653 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3654 break;
3655 case ISD::FEXP:
3656 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3657 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3658 break;
3659 case ISD::FEXP2:
3660 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3661 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3662 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003663 case ISD::FTRUNC:
3664 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3665 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3666 break;
3667 case ISD::FFLOOR:
3668 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3669 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3670 break;
3671 case ISD::FCEIL:
3672 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3673 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3674 break;
3675 case ISD::FRINT:
3676 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3677 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3678 break;
3679 case ISD::FNEARBYINT:
3680 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3681 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3682 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003683 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003684 default: assert(0 && "Unreachable!");
3685 }
Dan Gohman475871a2008-07-27 21:46:04 +00003686 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003687 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003688 break;
3689 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003690 }
3691 break;
3692 }
3693 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003694 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003695 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003696
3697 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003698 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003699 Result = LegalizeOp(UnrollVectorOp(Op));
3700 break;
3701 }
3702
3703 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003704 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3705 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003706 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003707 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003708 break;
3709 }
Chris Lattner35481892005-12-23 00:16:34 +00003710 case ISD::BIT_CONVERT:
Eli Friedman1fde9c52009-05-24 02:46:31 +00003711 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3712 Node->getOperand(0).getValueType())) {
3713 default: assert(0 && "Unknown operation action!");
3714 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003715 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003716 Node->getValueType(0), dl);
Eli Friedman1fde9c52009-05-24 02:46:31 +00003717 break;
3718 case TargetLowering::Legal:
3719 Tmp1 = LegalizeOp(Node->getOperand(0));
3720 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3721 break;
Chris Lattner35481892005-12-23 00:16:34 +00003722 }
3723 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003724 case ISD::CONVERT_RNDSAT: {
3725 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3726 switch (CvtCode) {
3727 default: assert(0 && "Unknown cvt code!");
3728 case ISD::CVT_SF:
3729 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003730 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003731 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003732 case ISD::CVT_FS:
3733 case ISD::CVT_FU:
3734 case ISD::CVT_SS:
3735 case ISD::CVT_SU:
3736 case ISD::CVT_US:
3737 case ISD::CVT_UU: {
3738 SDValue DTyOp = Node->getOperand(1);
3739 SDValue STyOp = Node->getOperand(2);
3740 SDValue RndOp = Node->getOperand(3);
3741 SDValue SatOp = Node->getOperand(4);
Eli Friedman957bffa2009-05-24 08:42:01 +00003742 Tmp1 = LegalizeOp(Node->getOperand(0));
3743 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3744 RndOp, SatOp);
3745 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3746 TargetLowering::Custom) {
3747 Tmp1 = TLI.LowerOperation(Result, DAG);
3748 if (Tmp1.getNode()) Result = Tmp1;
Mon P Wang77cdf302008-11-10 20:54:11 +00003749 }
3750 break;
3751 }
3752 } // end switch CvtCode
3753 break;
3754 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003755 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003756 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003757 case ISD::UINT_TO_FP: {
3758 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003759 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00003760 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003761 break;
3762 }
3763 case ISD::TRUNCATE:
Eli Friedman957bffa2009-05-24 08:42:01 +00003764 Tmp1 = LegalizeOp(Node->getOperand(0));
3765 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3766 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3767 case TargetLowering::Custom:
3768 isCustom = true;
3769 // FALLTHROUGH
3770 case TargetLowering::Legal:
3771 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3772 if (isCustom) {
3773 Tmp1 = TLI.LowerOperation(Result, DAG);
3774 if (Tmp1.getNode()) Result = Tmp1;
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00003775 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003776 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003777 case TargetLowering::Expand:
3778 assert(Result.getValueType().isVector() && "must be vector type");
3779 // Unroll the truncate. We should do better.
3780 Result = LegalizeOp(UnrollVectorOp(Result));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003781 break;
3782 }
3783 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003784
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003785 case ISD::FP_TO_SINT:
3786 case ISD::FP_TO_UINT:
Eli Friedman957bffa2009-05-24 08:42:01 +00003787 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003788
Eli Friedman957bffa2009-05-24 08:42:01 +00003789 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3790 default: assert(0 && "Unknown operation action!");
3791 case TargetLowering::Custom:
3792 isCustom = true;
3793 // FALLTHROUGH
3794 case TargetLowering::Legal:
3795 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3796 if (isCustom) {
3797 Tmp1 = TLI.LowerOperation(Result, DAG);
3798 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner07dffd62005-08-26 00:14:16 +00003799 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003800 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003801 case TargetLowering::Promote:
3802 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3803 Node->getOpcode() == ISD::FP_TO_SINT,
3804 dl);
Evan Cheng6af00d52006-12-13 01:57:55 +00003805 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003806 case TargetLowering::Expand:
3807 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3808 SDValue True, False;
3809 MVT VT = Node->getOperand(0).getValueType();
3810 MVT NVT = Node->getValueType(0);
3811 const uint64_t zero[] = {0, 0};
3812 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3813 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3814 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3815 Tmp2 = DAG.getConstantFP(apf, VT);
3816 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3817 Node->getOperand(0),
3818 Tmp2, ISD::SETLT);
3819 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3820 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3821 DAG.getNode(ISD::FSUB, dl, VT,
3822 Node->getOperand(0), Tmp2));
3823 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3824 DAG.getConstant(x, NVT));
3825 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
3826 } else {
3827 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3828 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003829 break;
3830 }
3831 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003832
Chris Lattnerf2670a82008-01-16 06:57:07 +00003833 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003834 MVT DstVT = Op.getValueType();
3835 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003836 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3837 // The only other way we can lower this is to turn it into a STORE,
3838 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003839 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003840 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003841 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003842 Tmp1 = LegalizeOp(Node->getOperand(0));
3843 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003844 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003845 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003846 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 MVT DstVT = Op.getValueType();
3848 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003849 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3850 if (SrcVT == MVT::ppcf128) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00003851 // FIXME: Figure out how to extract the double without
3852 // help from type legalization
Dale Johannesen5411a392007-08-09 01:04:01 +00003853 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003854 // The only other way we can lower this is to turn it into a STORE,
3855 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003856 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003857 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003858 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003859 Tmp1 = LegalizeOp(Node->getOperand(0));
3860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003861 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003862 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003863 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003864 case ISD::ZERO_EXTEND:
3865 case ISD::SIGN_EXTEND:
Eli Friedman957bffa2009-05-24 08:42:01 +00003866 Tmp1 = LegalizeOp(Node->getOperand(0));
3867 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3868 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3869 TargetLowering::Custom) {
3870 Tmp1 = TLI.LowerOperation(Result, DAG);
3871 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003872 }
3873 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003874 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003875 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003876 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003878
3879 // If this operation is not supported, convert it to a shl/shr or load/store
3880 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003881 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3882 default: assert(0 && "This action not supported for this op yet!");
3883 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003885 break;
3886 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003887 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003888 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003889 // NOTE: we could fall back on load/store here too for targets without
3890 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3892 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003893 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenca57b842009-02-02 23:46:53 +00003894 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003895 Node->getOperand(0), ShiftCst);
Dale Johannesenca57b842009-02-02 23:46:53 +00003896 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003897 Result, ShiftCst);
3898 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003899 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003900 // EXTLOAD pair, targetting a temporary location (a stack slot).
3901
3902 // NOTE: there is a choice here between constantly creating new stack
3903 // slots and always reusing the same one. We currently always create
3904 // new ones, as reuse may inhibit scheduling.
Scott Michelfdc40a02009-02-17 22:15:04 +00003905 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003906 Node->getValueType(0), dl);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003907 } else {
3908 assert(0 && "Unknown op");
3909 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003910 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003911 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003912 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003913 }
Duncan Sands36397f52007-07-27 12:58:54 +00003914 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00003916 for (unsigned i = 0; i != 6; ++i)
3917 Ops[i] = LegalizeOp(Node->getOperand(i));
3918 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3919 // The only option for this node is to custom lower it.
3920 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003921 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003922
3923 // Since trampoline produces two values, make sure to remember that we
3924 // legalized both of them.
3925 Tmp1 = LegalizeOp(Result.getValue(1));
3926 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00003927 AddLegalizedOperand(SDValue(Node, 0), Result);
3928 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003929 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003930 }
Dan Gohman9c78a392008-05-14 00:43:10 +00003931 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003933 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3934 default: assert(0 && "This action not supported for this op yet!");
3935 case TargetLowering::Custom:
3936 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003938 // Fall Thru
3939 case TargetLowering::Legal:
3940 // If this operation is not supported, lower it to constant 1
3941 Result = DAG.getConstant(1, VT);
3942 break;
3943 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00003944 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003945 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003946 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003948 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3949 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003950 case TargetLowering::Legal:
3951 Tmp1 = LegalizeOp(Node->getOperand(0));
3952 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3953 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003954 case TargetLowering::Custom:
3955 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003956 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003957 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003958 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003959 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003960 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003961 TargetLowering::ArgListTy Args;
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003962 std::pair<SDValue, SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00003963 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003964 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003965 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003966 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003967 Result = CallResult.second;
3968 break;
3969 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003970 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003971 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003972
Bill Wendling74c37652008-12-09 22:08:41 +00003973 case ISD::SADDO:
3974 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003975 MVT VT = Node->getValueType(0);
3976 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3977 default: assert(0 && "This action not supported for this op yet!");
3978 case TargetLowering::Custom:
3979 Result = TLI.LowerOperation(Op, DAG);
3980 if (Result.getNode()) break;
3981 // FALLTHROUGH
3982 case TargetLowering::Legal: {
3983 SDValue LHS = LegalizeOp(Node->getOperand(0));
3984 SDValue RHS = LegalizeOp(Node->getOperand(1));
3985
Scott Michelfdc40a02009-02-17 22:15:04 +00003986 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00003987 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00003988 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003989 MVT OType = Node->getValueType(1);
3990
Bill Wendlinga6af91a2008-11-25 08:19:22 +00003991 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003992
Bill Wendling740464e2008-11-25 19:40:17 +00003993 // LHSSign -> LHS >= 0
3994 // RHSSign -> RHS >= 0
3995 // SumSign -> Sum >= 0
3996 //
Bill Wendling74c37652008-12-09 22:08:41 +00003997 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00003998 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00003999 // Sub:
4000 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004001 //
Dale Johannesenca57b842009-02-02 23:46:53 +00004002 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
4003 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00004004 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
4005 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00004006 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004007
Dale Johannesenca57b842009-02-02 23:46:53 +00004008 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
4009 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004010
Dale Johannesenca57b842009-02-02 23:46:53 +00004011 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004012
4013 MVT ValueVTs[] = { LHS.getValueType(), OType };
4014 SDValue Ops[] = { Sum, Cmp };
4015
Scott Michelfdc40a02009-02-17 22:15:04 +00004016 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00004017 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004018 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004019 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00004020 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004021 break;
4022 }
4023 }
4024
4025 break;
4026 }
Bill Wendling74c37652008-12-09 22:08:41 +00004027 case ISD::UADDO:
4028 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004029 MVT VT = Node->getValueType(0);
4030 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4031 default: assert(0 && "This action not supported for this op yet!");
4032 case TargetLowering::Custom:
4033 Result = TLI.LowerOperation(Op, DAG);
4034 if (Result.getNode()) break;
4035 // FALLTHROUGH
4036 case TargetLowering::Legal: {
4037 SDValue LHS = LegalizeOp(Node->getOperand(0));
4038 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004039
Bill Wendling74c37652008-12-09 22:08:41 +00004040 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004041 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004042 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004043 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00004044 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00004045 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00004046 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004047
Bill Wendling41ea7e72008-11-24 19:21:46 +00004048 MVT ValueVTs[] = { LHS.getValueType(), OType };
4049 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004050
Scott Michelfdc40a02009-02-17 22:15:04 +00004051 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00004052 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004053 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004054 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00004055 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004056 break;
4057 }
4058 }
4059
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004060 break;
4061 }
Bill Wendling74c37652008-12-09 22:08:41 +00004062 case ISD::SMULO:
4063 case ISD::UMULO: {
4064 MVT VT = Node->getValueType(0);
4065 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4066 default: assert(0 && "This action is not supported at all!");
4067 case TargetLowering::Custom:
4068 Result = TLI.LowerOperation(Op, DAG);
4069 if (Result.getNode()) break;
4070 // Fall Thru
4071 case TargetLowering::Legal:
4072 // FIXME: According to Hacker's Delight, this can be implemented in
4073 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004074 // requires a division + a branch.
Scott Michelfdc40a02009-02-17 22:15:04 +00004075 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
Bill Wendling74c37652008-12-09 22:08:41 +00004076 break;
4077 }
4078 break;
4079 }
4080
Chris Lattner45b8caf2005-01-15 07:15:18 +00004081 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004082
Chris Lattner4ddd2832006-04-08 04:13:17 +00004083 assert(Result.getValueType() == Op.getValueType() &&
4084 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004085
Chris Lattner456a93a2006-01-28 07:39:30 +00004086 // Make sure that the generated code is itself legal.
4087 if (Result != Op)
4088 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004089
Chris Lattner45982da2005-05-12 16:53:42 +00004090 // Note that LegalizeOp may be reentered even from single-use nodes, which
4091 // means that we always must cache transformed nodes.
4092 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004093 return Result;
4094}
4095
Chris Lattner8b6fa222005-01-15 22:16:26 +00004096/// PromoteOp - Given an operation that produces a value in an invalid type,
4097/// promote it to compute the value into a larger type. The produced value will
4098/// have the correct bits for the low portion of the register, but no guarantee
4099/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004100SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00004101 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004102 MVT VT = Op.getValueType();
4103 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004104 assert(getTypeAction(VT) == Promote &&
4105 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004106 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004107 "Cannot promote to smaller type!");
4108
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue Tmp1, Tmp2, Tmp3;
4110 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004111 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004112 DebugLoc dl = Node->getDebugLoc();
Chris Lattner03c85462005-01-15 05:21:40 +00004113
Dan Gohman475871a2008-07-27 21:46:04 +00004114 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004115 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004116
Chris Lattner03c85462005-01-15 05:21:40 +00004117 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004118 case ISD::CopyFromReg:
4119 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004120 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004121#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004122 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004123#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004124 assert(0 && "Do not know how to promote this operator!");
4125 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004126 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00004127 Result = DAG.getUNDEF(NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004128 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004129 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004130 if (VT != MVT::i1)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004131 Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
Chris Lattnerec176e32005-08-30 16:56:19 +00004132 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004133 Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004134 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4135 break;
4136 case ISD::ConstantFP:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004137 Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004138 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4139 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004140
Duncan Sands5480c042009-01-01 15:52:00 +00004141 case ISD::SETCC: {
4142 MVT VT0 = Node->getOperand(0).getValueType();
4143 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004144 && "SetCC type is not legal??");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004145 Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004146 Node->getOperand(0), Node->getOperand(1),
4147 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004148 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004149 }
Chris Lattner03c85462005-01-15 05:21:40 +00004150 case ISD::TRUNCATE:
4151 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4152 case Legal:
4153 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004154 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004155 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004156 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dale Johannesen8a782a22009-02-02 22:12:50 +00004157 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
Chris Lattner03c85462005-01-15 05:21:40 +00004158 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004159 case Promote:
4160 // The truncation is not required, because we don't guarantee anything
4161 // about high bits anyway.
4162 Result = PromoteOp(Node->getOperand(0));
4163 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004164 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004165 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4166 // Truncate the low part of the expanded value to the result type
Dale Johannesen8a782a22009-02-02 22:12:50 +00004167 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004168 }
4169 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004170 case ISD::SIGN_EXTEND:
4171 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004172 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4174 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4175 case Legal:
4176 // Input is legal? Just do extend all the way to the larger type.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004177 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004178 break;
4179 case Promote:
4180 // Promote the reg if it's smaller.
4181 Result = PromoteOp(Node->getOperand(0));
4182 // The high bits are not guaranteed to be anything. Insert an extend.
4183 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004184 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004185 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004186 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004187 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004188 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004189 break;
4190 }
4191 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004192 case ISD::CONVERT_RNDSAT: {
4193 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4194 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4195 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4196 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4197 "can only promote integers");
Dale Johannesenc460ae92009-02-04 00:13:36 +00004198 Result = DAG.getConvertRndSat(NVT, dl, Node->getOperand(0),
Mon P Wang77cdf302008-11-10 20:54:11 +00004199 Node->getOperand(1), Node->getOperand(2),
4200 Node->getOperand(3), Node->getOperand(4),
4201 CvtCode);
4202 break;
4203
4204 }
Chris Lattner35481892005-12-23 00:16:34 +00004205 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004206 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004207 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00004208 Result = PromoteOp(Result);
4209 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004210
Chris Lattner8b6fa222005-01-15 22:16:26 +00004211 case ISD::FP_EXTEND:
4212 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4213 case ISD::FP_ROUND:
4214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4215 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4216 case Promote: assert(0 && "Unreachable with 2 FP types!");
4217 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004218 if (Node->getConstantOperandVal(1) == 0) {
4219 // Input is legal? Do an FP_ROUND_INREG.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004220 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004221 DAG.getValueType(VT));
4222 } else {
4223 // Just remove the truncate, it isn't affecting the value.
Scott Michelfdc40a02009-02-17 22:15:04 +00004224 Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004225 Node->getOperand(1));
4226 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004227 break;
4228 }
4229 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004230 case ISD::SINT_TO_FP:
4231 case ISD::UINT_TO_FP:
4232 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4233 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004234 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004235 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004236 break;
4237
4238 case Promote:
4239 Result = PromoteOp(Node->getOperand(0));
4240 if (Node->getOpcode() == ISD::SINT_TO_FP)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004241 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004242 Result,
4243 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004244 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004245 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004246 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004247 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004248 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004249 break;
4250 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004251 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004252 Node->getOperand(0), dl);
Chris Lattner77e77a62005-01-21 06:05:23 +00004253 // Round if we cannot tolerate excess precision.
4254 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004255 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004256 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004257 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004258 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004259 break;
4260
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004261 case ISD::SIGN_EXTEND_INREG:
4262 Result = PromoteOp(Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004263 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004264 Node->getOperand(1));
4265 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004266 case ISD::FP_TO_SINT:
4267 case ISD::FP_TO_UINT:
4268 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4269 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004270 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004271 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004272 break;
4273 case Promote:
4274 // The input result is prerounded, so we don't have to do anything
4275 // special.
4276 Tmp1 = PromoteOp(Node->getOperand(0));
4277 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004278 }
Nate Begemand2558e32005-08-14 01:20:53 +00004279 // If we're promoting a UINT to a larger size, check to see if the new node
4280 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4281 // we can use that instead. This allows us to generate better code for
4282 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4283 // legal, such as PowerPC.
Scott Michelfdc40a02009-02-17 22:15:04 +00004284 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004285 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4286 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004287 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Dale Johannesen8a782a22009-02-02 22:12:50 +00004288 Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004289 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004290 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004291 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004292 break;
4293
Chris Lattner2c8086f2005-04-02 05:00:07 +00004294 case ISD::FABS:
4295 case ISD::FNEG:
4296 Tmp1 = PromoteOp(Node->getOperand(0));
4297 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004298 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00004299 // NOTE: we do not have to do any extra rounding here for
4300 // NoExcessFPPrecision, because we know the input will have the appropriate
4301 // precision, and these operations don't modify precision at all.
4302 break;
4303
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004304 case ISD::FLOG:
4305 case ISD::FLOG2:
4306 case ISD::FLOG10:
4307 case ISD::FEXP:
4308 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004309 case ISD::FSQRT:
4310 case ISD::FSIN:
4311 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004312 case ISD::FTRUNC:
4313 case ISD::FFLOOR:
4314 case ISD::FCEIL:
4315 case ISD::FRINT:
4316 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004317 Tmp1 = PromoteOp(Node->getOperand(0));
4318 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004319 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004320 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004321 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004322 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004323 break;
4324
Evan Cheng9d24ac52008-09-09 23:35:53 +00004325 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004326 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004327 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004328 // directly as well, which may be better.
4329 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004330 Tmp2 = Node->getOperand(1);
4331 if (Node->getOpcode() == ISD::FPOW)
4332 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004333 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004334 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004335 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004336 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004337 DAG.getValueType(VT));
4338 break;
4339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004340
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004341 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004342 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004343 Tmp2 = PromoteOp(Node->getOperand(2));
4344 Tmp3 = PromoteOp(Node->getOperand(3));
Scott Michelfdc40a02009-02-17 22:15:04 +00004345 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
4346 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004347 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004348 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004349 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004350 // Remember that we legalized the chain.
4351 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4352 break;
4353 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004354 case ISD::ATOMIC_LOAD_ADD:
4355 case ISD::ATOMIC_LOAD_SUB:
4356 case ISD::ATOMIC_LOAD_AND:
4357 case ISD::ATOMIC_LOAD_OR:
4358 case ISD::ATOMIC_LOAD_XOR:
4359 case ISD::ATOMIC_LOAD_NAND:
4360 case ISD::ATOMIC_LOAD_MIN:
4361 case ISD::ATOMIC_LOAD_MAX:
4362 case ISD::ATOMIC_LOAD_UMIN:
4363 case ISD::ATOMIC_LOAD_UMAX:
4364 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004365 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004366 Tmp2 = PromoteOp(Node->getOperand(2));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004367 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004368 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004369 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004370 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004371 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004372 // Remember that we legalized the chain.
4373 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4374 break;
4375 }
4376
Chris Lattner03c85462005-01-15 05:21:40 +00004377 case ISD::AND:
4378 case ISD::OR:
4379 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004380 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004381 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004382 case ISD::MUL:
4383 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004384 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004385 // that too is okay if they are integer operations.
4386 Tmp1 = PromoteOp(Node->getOperand(0));
4387 Tmp2 = PromoteOp(Node->getOperand(1));
4388 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004389 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004390 break;
4391 case ISD::FADD:
4392 case ISD::FSUB:
4393 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004394 Tmp1 = PromoteOp(Node->getOperand(0));
4395 Tmp2 = PromoteOp(Node->getOperand(1));
4396 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004397 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004398
Chris Lattner01b3d732005-09-28 22:28:18 +00004399 // Floating point operations will give excess precision that we may not be
4400 // able to tolerate. If we DO allow excess precision, just leave it,
4401 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004402 // FIXME: Why would we need to round FP ops more than integer ones?
4403 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004404 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004405 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004406 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004407 break;
4408
Chris Lattner8b6fa222005-01-15 22:16:26 +00004409 case ISD::SDIV:
4410 case ISD::SREM:
4411 // These operators require that their input be sign extended.
4412 Tmp1 = PromoteOp(Node->getOperand(0));
4413 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004414 if (NVT.isInteger()) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004415 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004416 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004417 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Chris Lattner15e4b012005-07-10 00:07:11 +00004418 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004419 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004420 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004421
4422 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004423 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004424 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004425 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004426 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004427 case ISD::FDIV:
4428 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004429 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004430 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004431 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004432 case Expand: assert(0 && "not implemented");
4433 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4434 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004435 }
4436 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004437 case Expand: assert(0 && "not implemented");
4438 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4439 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004440 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004441 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004442
Chris Lattner01b3d732005-09-28 22:28:18 +00004443 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004444 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004445 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner01b3d732005-09-28 22:28:18 +00004446 DAG.getValueType(VT));
4447 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004448
4449 case ISD::UDIV:
4450 case ISD::UREM:
4451 // These operators require that their input be zero extended.
4452 Tmp1 = PromoteOp(Node->getOperand(0));
4453 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004454 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004455 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4456 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
4457 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004458 break;
4459
4460 case ISD::SHL:
4461 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004462 Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004463 break;
4464 case ISD::SRA:
4465 // The input value must be properly sign extended.
4466 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004467 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004468 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004469 Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004470 break;
4471 case ISD::SRL:
4472 // The input value must be properly zero extended.
4473 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004474 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4475 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004476 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004477
4478 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004479 Tmp1 = Node->getOperand(0); // Get the chain.
4480 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004481 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00004482 Tmp3 = DAG.getVAArg(VT, dl, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004483 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004484 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004485 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenc460ae92009-02-04 00:13:36 +00004486 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004487 // Increment the pointer, VAList, to the next vaarg
Scott Michelfdc40a02009-02-17 22:15:04 +00004488 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004489 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004490 TLI.getPointerTy()));
4491 // Store the incremented VAList to the legalized pointer
Dale Johannesen8a782a22009-02-02 22:12:50 +00004492 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004493 // Load the actual argument out of the pointer VAList
Dale Johannesen8a782a22009-02-02 22:12:50 +00004494 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004495 }
4496 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004497 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004498 break;
4499
Evan Cheng466685d2006-10-09 20:57:25 +00004500 case ISD::LOAD: {
4501 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004502 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4503 ? ISD::EXTLOAD : LD->getExtensionType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004504 Result = DAG.getExtLoad(ExtType, dl, NVT,
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004505 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004506 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004507 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004508 LD->isVolatile(),
4509 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004510 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004511 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004512 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004513 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004514 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004515 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4516 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004517
Duncan Sands83ec4b62008-06-06 12:08:01 +00004518 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004519 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004520 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4521 // Ensure that the resulting node is at least the same size as the operands'
4522 // value types, because we cannot assume that TLI.getSetCCValueType() is
4523 // constant.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004524 Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004525 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004526 }
Nate Begeman9373a812005-08-10 20:51:12 +00004527 case ISD::SELECT_CC:
4528 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4529 Tmp3 = PromoteOp(Node->getOperand(3)); // False
Dale Johannesen8a782a22009-02-02 22:12:50 +00004530 Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004531 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004532 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004533 case ISD::BSWAP:
4534 Tmp1 = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004535 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
4536 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4537 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004538 DAG.getConstant(NVT.getSizeInBits() -
4539 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004540 TLI.getShiftAmountTy()));
4541 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004542 case ISD::CTPOP:
4543 case ISD::CTTZ:
4544 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004545 // Zero extend the argument
Dale Johannesen8a782a22009-02-02 22:12:50 +00004546 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004547 // Perform the larger operation, then subtract if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004548 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004549 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004550 case ISD::CTPOP:
4551 Result = Tmp1;
4552 break;
4553 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004554 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004555 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004556 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004557 ISD::SETEQ);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004558 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004559 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004560 break;
4561 case ISD::CTLZ:
4562 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00004563 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004564 DAG.getConstant(NVT.getSizeInBits() -
4565 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004566 break;
4567 }
4568 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004569 case ISD::EXTRACT_SUBVECTOR:
4570 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004571 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004572 case ISD::EXTRACT_VECTOR_ELT:
4573 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4574 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004575 }
4576
Gabor Greifba36cb52008-08-28 21:40:38 +00004577 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004578
4579 // Make sure the result is itself legal.
4580 Result = LegalizeOp(Result);
Scott Michelfdc40a02009-02-17 22:15:04 +00004581
Chris Lattner456a93a2006-01-28 07:39:30 +00004582 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004583 AddPromotedOperand(Op, Result);
4584 return Result;
4585}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004586
Dan Gohman7f321562007-06-25 16:23:39 +00004587/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4588/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4589/// based on the vector type. The return type of this matches the element type
4590/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004591SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004592 // We know that operand #0 is the Vec vector. If the index is a constant
4593 // or if the invec is a supported hardware type, we can use it. Otherwise,
4594 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue Vec = Op.getOperand(0);
4596 SDValue Idx = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004597 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004598
Duncan Sands83ec4b62008-06-06 12:08:01 +00004599 MVT TVT = Vec.getValueType();
4600 unsigned NumElems = TVT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Dan Gohman7f321562007-06-25 16:23:39 +00004602 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4603 default: assert(0 && "This action is not supported yet!");
4604 case TargetLowering::Custom: {
4605 Vec = LegalizeOp(Vec);
4606 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004608 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004609 return Tmp3;
4610 break;
4611 }
4612 case TargetLowering::Legal:
4613 if (isTypeLegal(TVT)) {
4614 Vec = LegalizeOp(Vec);
4615 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004616 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004617 }
4618 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004619 case TargetLowering::Promote:
4620 assert(TVT.isVector() && "not vector type");
4621 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004622 case TargetLowering::Expand:
4623 break;
4624 }
4625
Eli Friedman1fde9c52009-05-24 02:46:31 +00004626 if (NumElems == 1)
Chris Lattner15972212006-03-31 17:55:51 +00004627 // This must be an access of the only element. Return it.
Eli Friedman1fde9c52009-05-24 02:46:31 +00004628 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Vec);
4629 else
4630 return ExpandExtractFromVectorThroughStack(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004631}
4632
Eli Friedman3d43b3f2009-05-23 22:37:25 +00004633SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
4634 SDValue Vec = Op.getOperand(0);
4635 SDValue Idx = Op.getOperand(1);
4636 DebugLoc dl = Op.getDebugLoc();
4637 // Store the value to a temporary stack slot, then LOAD the returned part.
4638 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4639 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
4640
4641 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00004642 unsigned EltSize =
4643 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00004644 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
4645 DAG.getConstant(EltSize, Idx.getValueType()));
4646
4647 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
4648 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
4649 else
4650 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
4651
4652 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
4653
4654 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
4655}
4656
Dan Gohman7f321562007-06-25 16:23:39 +00004657/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004658/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004659SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004660 // We know that operand #0 is the Vec vector. For now we assume the index
4661 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004662 SDValue Vec = Op.getOperand(0);
4663 SDValue Idx = LegalizeOp(Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004664
Duncan Sands83ec4b62008-06-06 12:08:01 +00004665 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00004666
Duncan Sands83ec4b62008-06-06 12:08:01 +00004667 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004668 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004669 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004670 }
4671
4672 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004674 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004675 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004676 Vec = Lo;
4677 } else {
4678 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004679 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4680 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004681 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004682
Dan Gohman65956352007-06-13 15:12:02 +00004683 // It's now an extract from the appropriate high or low part. Recurse.
4684 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004685 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004686}
4687
Nate Begeman750ac1b2006-02-01 07:19:44 +00004688/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4689/// with condition CC on the current target. This usually involves legalizing
4690/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4691/// there may be no choice but to create a new SetCC node to represent the
4692/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004693/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4694void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4695 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00004696 SDValue &CC,
4697 DebugLoc dl) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004698 SDValue Tmp1, Tmp2, Tmp3, Result;
4699
Nate Begeman750ac1b2006-02-01 07:19:44 +00004700 switch (getTypeAction(LHS.getValueType())) {
4701 case Legal:
4702 Tmp1 = LegalizeOp(LHS); // LHS
4703 Tmp2 = LegalizeOp(RHS); // RHS
4704 break;
4705 case Promote:
4706 Tmp1 = PromoteOp(LHS); // LHS
4707 Tmp2 = PromoteOp(RHS); // RHS
4708
4709 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004710 if (LHS.getValueType().isInteger()) {
4711 MVT VT = LHS.getValueType();
4712 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004713
4714 // Otherwise, we have to insert explicit sign or zero extends. Note
4715 // that we could insert sign extends for ALL conditions, but zero extend
4716 // is cheaper on many machines (an AND instead of two shifts), so prefer
4717 // it.
4718 switch (cast<CondCodeSDNode>(CC)->get()) {
4719 default: assert(0 && "Unknown integer comparison!");
4720 case ISD::SETEQ:
4721 case ISD::SETNE:
4722 case ISD::SETUGE:
4723 case ISD::SETUGT:
4724 case ISD::SETULE:
4725 case ISD::SETULT:
4726 // ALL of these operations will work if we either sign or zero extend
4727 // the operands (including the unsigned comparisons!). Zero extend is
4728 // usually a simpler/cheaper operation, so prefer it.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004729 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4730 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004731 break;
4732 case ISD::SETGE:
4733 case ISD::SETGT:
4734 case ISD::SETLT:
4735 case ISD::SETLE:
Dale Johannesenbb5da912009-02-02 20:41:04 +00004736 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Nate Begeman750ac1b2006-02-01 07:19:44 +00004737 DAG.getValueType(VT));
Dale Johannesenbb5da912009-02-02 20:41:04 +00004738 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Nate Begeman750ac1b2006-02-01 07:19:44 +00004739 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00004740 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4741 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00004742 break;
4743 }
4744 }
4745 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004746 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004747 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004748 if (VT == MVT::f32 || VT == MVT::f64) {
4749 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004750 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004751 switch (cast<CondCodeSDNode>(CC)->get()) {
4752 case ISD::SETEQ:
4753 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004754 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004755 break;
4756 case ISD::SETNE:
4757 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004758 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004759 break;
4760 case ISD::SETGE:
4761 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004762 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004763 break;
4764 case ISD::SETLT:
4765 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004766 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004767 break;
4768 case ISD::SETLE:
4769 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004770 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004771 break;
4772 case ISD::SETGT:
4773 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004774 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004775 break;
4776 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004777 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4778 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004779 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004780 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004781 break;
4782 default:
Evan Cheng56966222007-01-12 02:11:51 +00004783 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004784 switch (cast<CondCodeSDNode>(CC)->get()) {
4785 case ISD::SETONE:
4786 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004787 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004788 // Fallthrough
4789 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004790 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004791 break;
4792 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004793 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004794 break;
4795 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004796 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004797 break;
4798 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004799 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004800 break;
Evan Cheng56966222007-01-12 02:11:51 +00004801 case ISD::SETUEQ:
4802 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004803 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004804 default: assert(0 && "Unsupported FP setcc!");
4805 }
4806 }
Duncan Sandsf9516202008-06-30 10:19:09 +00004807
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue Dummy;
4809 SDValue Ops[2] = { LHS, RHS };
Dale Johannesenbb5da912009-02-02 20:41:04 +00004810 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004811 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004812 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004813 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004814 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesenbb5da912009-02-02 20:41:04 +00004815 Tmp1 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004816 TLI.getSetCCResultType(Tmp1.getValueType()),
4817 Tmp1, Tmp2, CC);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004818 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004819 false /*sign irrelevant*/, Dummy);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004820 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004821 TLI.getSetCCResultType(LHS.getValueType()), LHS,
4822 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesenbb5da912009-02-02 20:41:04 +00004823 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00004824 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00004825 }
Evan Cheng21cacc42008-07-07 07:18:09 +00004826 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00004827 RHS = Tmp2;
4828 return;
4829 }
4830
Dan Gohman475871a2008-07-27 21:46:04 +00004831 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004832 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004833 ExpandOp(RHS, RHSLo, RHSHi);
4834 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4835
4836 if (VT==MVT::ppcf128) {
4837 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00004838 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004839 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00004840 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004841 // The following can be improved, but not that much.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004842 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004843 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004844 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004845 LHSLo, RHSLo, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004846 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
4847 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004848 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004849 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004850 LHSHi, RHSHi, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004851 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
4852 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00004853 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00004854 break;
4855 }
4856
4857 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004858 case ISD::SETEQ:
4859 case ISD::SETNE:
4860 if (RHSLo == RHSHi)
4861 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4862 if (RHSCST->isAllOnesValue()) {
4863 // Comparison to -1.
Dale Johannesenbb5da912009-02-02 20:41:04 +00004864 Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004865 Tmp2 = RHSLo;
4866 break;
4867 }
4868
Dale Johannesenbb5da912009-02-02 20:41:04 +00004869 Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
4870 Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
4871 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004872 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4873 break;
4874 default:
4875 // If this is a comparison of the sign bit, just look at the top part.
4876 // X > -1, x < 0
4877 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
Scott Michelfdc40a02009-02-17 22:15:04 +00004878 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004879 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004880 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4881 CST->isAllOnesValue())) { // X > -1
4882 Tmp1 = LHSHi;
4883 Tmp2 = RHSHi;
4884 break;
4885 }
4886
4887 // FIXME: This generated code sucks.
4888 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004889 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004890 default: assert(0 && "Unknown integer setcc!");
4891 case ISD::SETLT:
4892 case ISD::SETULT: LowCC = ISD::SETULT; break;
4893 case ISD::SETGT:
4894 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4895 case ISD::SETLE:
4896 case ISD::SETULE: LowCC = ISD::SETULE; break;
4897 case ISD::SETGE:
4898 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4899 }
4900
4901 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4902 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4903 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4904
4905 // NOTE: on targets without efficient SELECT of bools, we can always use
4906 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004907 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00004908 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004909 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004910 if (!Tmp1.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004911 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004912 LHSLo, RHSLo, LowCC);
4913 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004914 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004915 if (!Tmp2.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004916 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00004917 TLI.getSetCCResultType(LHSHi.getValueType()),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004918 LHSHi, RHSHi, CC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004919
Gabor Greifba36cb52008-08-28 21:40:38 +00004920 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
4921 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00004922 if ((Tmp1C && Tmp1C->isNullValue()) ||
4923 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004924 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4925 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004926 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004927 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4928 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4929 // low part is known false, returns high part.
4930 // For LE / GE, if high part is known false, ignore the low part.
4931 // For LT / GT, if high part is known true, ignore the low part.
4932 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00004933 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004934 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00004935 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
4936 LHSHi, RHSHi, ISD::SETEQ, false,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004937 DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004938 if (!Result.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00004939 Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004940 LHSHi, RHSHi, ISD::SETEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00004941 Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
Evan Cheng2e677812007-02-08 22:16:19 +00004942 Result, Tmp1, Tmp2));
4943 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00004944 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004945 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004946 }
4947 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004948 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004949 LHS = Tmp1;
4950 RHS = Tmp2;
4951}
4952
Evan Cheng7f042682008-10-15 02:05:31 +00004953/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
4954/// condition code CC on the current target. This routine assumes LHS and rHS
4955/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
4956/// illegal condition code into AND / OR of multiple SETCC values.
4957void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
4958 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00004959 SDValue &CC,
4960 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00004961 MVT OpVT = LHS.getValueType();
4962 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4963 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
4964 default: assert(0 && "Unknown condition code action!");
4965 case TargetLowering::Legal:
4966 // Nothing to do.
4967 break;
4968 case TargetLowering::Expand: {
4969 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
4970 unsigned Opc = 0;
4971 switch (CCCode) {
4972 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00004973 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
4974 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
4975 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4976 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
4977 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4978 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
4979 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4980 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4981 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4982 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4983 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
4984 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00004985 // FIXME: Implement more expansions.
4986 }
4987
Dale Johannesenbb5da912009-02-02 20:41:04 +00004988 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
4989 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
4990 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00004991 RHS = SDValue();
4992 CC = SDValue();
4993 break;
4994 }
4995 }
4996}
4997
Chris Lattner1401d152008-01-16 07:45:30 +00004998/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4999/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5000/// a load from the stack slot to DestVT, extending it if needed.
5001/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005002SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5003 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005004 MVT DestVT,
5005 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00005006 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005007 unsigned SrcAlign =
5008 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
5009 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005010 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00005011
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005012 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005013 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00005014 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5015
Duncan Sands83ec4b62008-06-06 12:08:01 +00005016 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5017 unsigned SlotSize = SlotVT.getSizeInBits();
5018 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005019 unsigned DestAlign =
5020 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00005021
Chris Lattner1401d152008-01-16 07:45:30 +00005022 // Emit a store to the stack slot. Use a truncstore if the input value is
5023 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005024 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00005025
Chris Lattner1401d152008-01-16 07:45:30 +00005026 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005027 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005028 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005029 else {
5030 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005031 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005032 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005033 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005034
Chris Lattner35481892005-12-23 00:16:34 +00005035 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005036 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005037 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00005038
Chris Lattner1401d152008-01-16 07:45:30 +00005039 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005040 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00005041 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005042}
5043
Dan Gohman475871a2008-07-27 21:46:04 +00005044SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005045 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00005046 // Create a vector sized/aligned stack slot, store the value to element #0,
5047 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005048 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005049
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005050 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005051 int SPFI = StackPtrFI->getIndex();
5052
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005053 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
5054 StackPtr,
5055 PseudoSourceValue::getFixedStack(SPFI), 0,
5056 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005057 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005058 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005059}
5060
5061
Chris Lattnerce872152006-03-19 06:31:19 +00005062/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005063/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005064SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005065 unsigned NumElems = Node->getNumOperands();
5066 SDValue SplatValue = Node->getOperand(0);
5067 DebugLoc dl = Node->getDebugLoc();
5068 MVT VT = Node->getValueType(0);
5069 MVT OpVT = SplatValue.getValueType();
5070 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005071
5072 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005073 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00005074 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005075
Dan Gohman475871a2008-07-27 21:46:04 +00005076 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005077 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00005078 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00005079 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005080 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005081 bool isConstant = true;
5082 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5083 SplatValue.getOpcode() != ISD::UNDEF)
5084 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005085
Evan Cheng033e6812006-03-24 01:17:21 +00005086 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005087 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005088 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005089 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005090 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005091 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005092 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005093
5094 // If this isn't a constant element or an undef, we can't use a constant
5095 // pool load.
5096 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5097 V.getOpcode() != ISD::UNDEF)
5098 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005100
Chris Lattnerce872152006-03-19 06:31:19 +00005101 if (isOnlyLowElement) {
5102 // If the low element is an undef too, then this whole things is an undef.
5103 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005104 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005105 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005106 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00005107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005108
Chris Lattner2eb86532006-03-24 07:29:17 +00005109 // If all elements are constants, create a load from the constant pool.
5110 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00005111 std::vector<Constant*> CV;
5112 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005113 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00005114 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005115 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00005116 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005117 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005118 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005119 } else {
5120 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005121 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005122 CV.push_back(UndefValue::get(OpNTy));
5123 }
5124 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005125 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005127 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005128 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005129 PseudoSourceValue::getConstantPool(), 0,
5130 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005132
Gabor Greifba36cb52008-08-28 21:40:38 +00005133 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005134 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00005135 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00005136
5137 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00005138 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00005139 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00005140 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005141 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattner87100e02006-03-20 01:52:29 +00005143 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00005144 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
5145 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00005146 }
5147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005148
Evan Cheng033e6812006-03-24 01:17:21 +00005149 // If there are only two unique elements, we may be able to turn this into a
5150 // vector shuffle.
5151 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005152 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005153 SDValue Val1 = Node->getOperand(1);
5154 SDValue Val2;
5155 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005156 if (MI->first != Val1)
5157 Val2 = MI->first;
5158 else
5159 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00005160
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005161 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005162 // vector shuffle has the undef vector on the RHS.
5163 if (Val1.getOpcode() == ISD::UNDEF)
5164 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005165
Evan Cheng033e6812006-03-24 01:17:21 +00005166 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00005167 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005168
5169 // Set elements of the shuffle mask for Val1.
5170 std::vector<unsigned> &Val1Elts = Values[Val1];
5171 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005172 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005173
5174 // Set elements of the shuffle mask for Val2.
5175 std::vector<unsigned> &Val2Elts = Values[Val2];
5176 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5177 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00005178 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00005179
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005180 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005181 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00005182 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005183 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
5184 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005185 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00005186 }
5187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005188
Chris Lattnerce872152006-03-19 06:31:19 +00005189 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5190 // aligned object on the stack, store each element into it, then load
5191 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00005192 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005193 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00005194 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5195 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5196
Chris Lattnerce872152006-03-19 06:31:19 +00005197 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00005199 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00005200 // Store (in the right endianness) the elements to memory.
5201 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5202 // Ignore undef elements.
5203 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005204
Chris Lattner841c8822006-03-22 01:46:54 +00005205 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00005206
Dan Gohman475871a2008-07-27 21:46:04 +00005207 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005208 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005209
Dale Johannesen8a782a22009-02-02 22:12:50 +00005210 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
5211 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00005212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005213
Dan Gohman475871a2008-07-27 21:46:04 +00005214 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005215 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00005216 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005217 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005218 else
5219 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005220
Chris Lattnerce872152006-03-19 06:31:19 +00005221 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005222 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005223}
5224
Chris Lattner5b359c62005-04-02 04:00:59 +00005225void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005227 SDValue &Lo, SDValue &Hi,
5228 DebugLoc dl) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005229 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005230 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005231 ExpandOp(Op, LHSL, LHSH);
5232
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005234 MVT VT = LHSL.getValueType();
Dan Gohmanfc166572009-04-09 23:54:40 +00005235 Lo = DAG.getNode(NodeOp, dl, DAG.getVTList(VT, VT), Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005236 Hi = Lo.getValue(1);
5237}
5238
5239
Chris Lattnere34b3962005-01-19 04:19:40 +00005240/// ExpandShift - Try to find a clever way to expand this shift operation out to
5241/// smaller elements. If we can't find a way that is more efficient than a
5242/// libcall on this target, return false. Otherwise, return true with the
5243/// low-parts expanded into Lo and Hi.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005244bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005245 SDValue &Lo, SDValue &Hi,
5246 DebugLoc dl) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005247 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5248 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005249
Duncan Sands83ec4b62008-06-06 12:08:01 +00005250 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005252 MVT ShTy = ShAmt.getValueType();
5253 unsigned ShBits = ShTy.getSizeInBits();
5254 unsigned VTBits = Op.getValueType().getSizeInBits();
5255 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005256
Chris Lattner7a3c8552007-10-14 20:35:12 +00005257 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005258 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005259 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005260 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005261 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005262 ExpandOp(Op, InL, InH);
5263 switch(Opc) {
5264 case ISD::SHL:
5265 if (Cst > VTBits) {
5266 Lo = DAG.getConstant(0, NVT);
5267 Hi = DAG.getConstant(0, NVT);
5268 } else if (Cst > NVTBits) {
5269 Lo = DAG.getConstant(0, NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005270 Hi = DAG.getNode(ISD::SHL, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005271 NVT, InL, DAG.getConstant(Cst-NVTBits, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005272 } else if (Cst == NVTBits) {
5273 Lo = DAG.getConstant(0, NVT);
5274 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005275 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005276 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
5277 Hi = DAG.getNode(ISD::OR, dl, NVT,
5278 DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005279 DAG.getNode(ISD::SRL, dl, NVT, InL,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005280 DAG.getConstant(NVTBits-Cst, ShTy)));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005281 }
5282 return true;
5283 case ISD::SRL:
5284 if (Cst > VTBits) {
5285 Lo = DAG.getConstant(0, NVT);
5286 Hi = DAG.getConstant(0, NVT);
5287 } else if (Cst > NVTBits) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005288 Lo = DAG.getNode(ISD::SRL, dl, NVT,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005289 InH, DAG.getConstant(Cst-NVTBits, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005290 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005291 } else if (Cst == NVTBits) {
5292 Lo = InH;
5293 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005294 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005295 Lo = DAG.getNode(ISD::OR, dl, NVT,
5296 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005297 DAG.getNode(ISD::SHL, dl, NVT, InH,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005298 DAG.getConstant(NVTBits-Cst, ShTy)));
5299 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005300 }
5301 return true;
5302 case ISD::SRA:
5303 if (Cst > VTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005304 Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005305 DAG.getConstant(NVTBits-1, ShTy));
5306 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005307 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005308 DAG.getConstant(Cst-NVTBits, ShTy));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005309 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005310 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005311 } else if (Cst == NVTBits) {
5312 Lo = InH;
Dale Johannesen8a782a22009-02-02 22:12:50 +00005313 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005314 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005315 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005316 Lo = DAG.getNode(ISD::OR, dl, NVT,
5317 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
Scott Michelfdc40a02009-02-17 22:15:04 +00005318 DAG.getNode(ISD::SHL, dl,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005319 NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5320 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005321 }
5322 return true;
5323 }
5324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005325
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005326 // Okay, the shift amount isn't constant. However, if we can tell that it is
5327 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005328 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5329 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005330 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00005331
Dan Gohman9e255b72008-02-22 01:12:31 +00005332 // If we know that if any of the high bits of the shift amount are one, then
5333 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005334 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005335 // Mask out the high bit, which we know is set.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005336 Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005337 DAG.getConstant(~Mask, Amt.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00005338
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005339 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005341 ExpandOp(Op, InL, InH);
5342 switch(Opc) {
5343 case ISD::SHL:
5344 Lo = DAG.getConstant(0, NVT); // Low part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005345 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005346 return true;
5347 case ISD::SRL:
5348 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005349 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005350 return true;
5351 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005352 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005353 DAG.getConstant(NVTBits-1, Amt.getValueType()));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005354 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005355 return true;
5356 }
5357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Dan Gohman9e255b72008-02-22 01:12:31 +00005359 // If we know that the high bits of the shift amount are all zero, then we can
5360 // do this as a couple of simple shifts.
5361 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005362 // Compute 32-amt.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005363 SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005364 DAG.getConstant(NVTBits, Amt.getValueType()),
5365 Amt);
Scott Michelfdc40a02009-02-17 22:15:04 +00005366
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005367 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005368 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005369 ExpandOp(Op, InL, InH);
5370 switch(Opc) {
5371 case ISD::SHL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005372 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
5373 Hi = DAG.getNode(ISD::OR, dl, NVT,
5374 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
5375 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005376 return true;
5377 case ISD::SRL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005378 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
5379 Lo = DAG.getNode(ISD::OR, dl, NVT,
5380 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5381 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005382 return true;
5383 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005384 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
5385 Lo = DAG.getNode(ISD::OR, dl, NVT,
5386 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5387 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005388 return true;
5389 }
5390 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005391
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005392 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005393}
Chris Lattner77e77a62005-01-21 06:05:23 +00005394
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005395
Chris Lattner77e77a62005-01-21 06:05:23 +00005396// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5397// does not fit into a register, return the lo part and set the hi part to the
5398// by-reg argument. If it does fit into a single register, return the result
5399// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005400SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5401 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005402 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005403 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00005404 // Legalizing the call will automatically add the previous call to the
5405 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005406 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Chris Lattner77e77a62005-01-21 06:05:23 +00005408 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005409 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005410 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005411 MVT ArgVT = Node->getOperand(i).getValueType();
5412 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00005413 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005414 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005415 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005416 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005417 }
Bill Wendling056292f2008-09-16 21:48:12 +00005418 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005419 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005420
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005421 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005422 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005423 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005424 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005425 CallingConv::C, false, Callee, Args, DAG,
5426 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005427
Chris Lattner6831a812006-02-13 09:18:02 +00005428 // Legalize the call sequence, starting with the chain. This will advance
5429 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5430 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5431 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005433 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005434 default: assert(0 && "Unknown thing");
5435 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005436 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005437 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005438 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005439 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005440 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005441 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005442 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005443}
5444
Dan Gohman7f8613e2008-08-14 20:04:46 +00005445/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5446///
5447SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005448LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5449 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00005450 bool isCustom = false;
5451 SDValue Tmp1;
5452 switch (getTypeAction(Op.getValueType())) {
5453 case Legal:
5454 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5455 Op.getValueType())) {
5456 default: assert(0 && "Unknown operation action!");
5457 case TargetLowering::Custom:
5458 isCustom = true;
5459 // FALLTHROUGH
5460 case TargetLowering::Legal:
5461 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005462 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005463 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5464 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005465 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005466 DestTy, Tmp1);
5467 if (isCustom) {
5468 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005469 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005470 }
5471 break;
5472 case TargetLowering::Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005473 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005474 break;
5475 case TargetLowering::Promote:
Dale Johannesenaf435272009-02-02 19:03:57 +00005476 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005477 break;
5478 }
5479 break;
5480 case Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005481 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005482 break;
5483 case Promote:
5484 Tmp1 = PromoteOp(Op);
5485 if (isSigned) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005486 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005487 Tmp1, DAG.getValueType(Op.getValueType()));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005488 } else {
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005489 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, Op.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00005490 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005491 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005492 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5493 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005494 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005495 DestTy, Tmp1);
5496 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5497 break;
5498 }
5499 return Result;
5500}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005501
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005502/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5503///
Dan Gohman475871a2008-07-27 21:46:04 +00005504SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005505ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005506 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005507 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005508
Dan Gohman7f8613e2008-08-14 20:04:46 +00005509 // Expand unsupported int-to-fp vector casts by unrolling them.
5510 if (DestTy.isVector()) {
5511 if (!ExpandSource)
5512 return LegalizeOp(UnrollVectorOp(Source));
5513 MVT DestEltTy = DestTy.getVectorElementType();
5514 if (DestTy.getVectorNumElements() == 1) {
5515 SDValue Scalar = ScalarizeVectorOp(Source);
5516 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00005517 DestEltTy, Scalar, dl);
Evan Chenga87008d2009-02-25 22:49:59 +00005518 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005519 }
5520 SDValue Lo, Hi;
5521 SplitVectorOp(Source, Lo, Hi);
5522 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5523 DestTy.getVectorNumElements() / 2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005524 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +00005525 Lo, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005526 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +00005527 Hi, dl);
5528 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengefa53392008-10-13 18:46:18 +00005529 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005530 }
5531
Evan Cheng9845eb52008-04-01 02:18:22 +00005532 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5533 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005534 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005535 // incoming integer is set. To handle this, we dynamically test to see if
5536 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005537 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005538 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005540 ExpandOp(Source, Lo, Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005541 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005542 } else {
5543 // The comparison for the sign bit will use the entire operand.
5544 Hi = Source;
5545 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005546
Dale Johannesen53997b02008-11-04 20:52:49 +00005547 // Check to see if the target has a custom way to lower this. If so, use
5548 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005549 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5550 default: assert(0 && "This action not implemented for this operation!");
5551 case TargetLowering::Legal:
5552 case TargetLowering::Expand:
5553 break; // This case is handled below.
5554 case TargetLowering::Custom: {
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005555 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, dl, DestTy,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005556 Source), DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005557 if (NV.getNode())
5558 return LegalizeOp(NV);
5559 break; // The target decided this was legal after all
5560 }
5561 }
5562
Chris Lattner66de05b2005-05-13 04:45:13 +00005563 // If this is unsigned, and not supported, first perform the conversion to
5564 // signed, then adjust the result if the sign bit is set.
Dale Johannesenaf435272009-02-02 19:03:57 +00005565 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Chris Lattner66de05b2005-05-13 04:45:13 +00005566
Scott Michelfdc40a02009-02-17 22:15:04 +00005567 SDValue SignSet = DAG.getSetCC(dl,
Dale Johannesenaf435272009-02-02 19:03:57 +00005568 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005569 Hi, DAG.getConstant(0, Hi.getValueType()),
5570 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005571 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00005572 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005573 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005574 uint64_t FF = 0x5f800000ULL;
5575 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00005576 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005577
Dan Gohman475871a2008-07-27 21:46:04 +00005578 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005579 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00005580 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005581 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005582 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005583 if (DestTy == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00005584 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005585 PseudoSourceValue::getConstantPool(), 0,
5586 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005587 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005588 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenaf435272009-02-02 19:03:57 +00005589 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005590 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005591 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005592 MVT::f32, false, Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00005593 else
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005594 assert(0 && "Unexpected conversion");
5595
Duncan Sands83ec4b62008-06-06 12:08:01 +00005596 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005597 if (SCVT != DestTy) {
5598 // Destination type needs to be expanded as well. The FADD now we are
5599 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005600 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5601 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesenaf435272009-02-02 19:03:57 +00005602 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005603 SignedConv, SignedConv.getValue(1));
5604 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005605 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005606 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005607 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005608 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005609
Chris Lattnera88a2602005-05-14 05:33:54 +00005610 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005611 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005612 default: assert(0 && "This action not implemented for this operation!");
5613 case TargetLowering::Legal:
5614 case TargetLowering::Expand:
5615 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005616 case TargetLowering::Custom: {
Dale Johannesenaf435272009-02-02 19:03:57 +00005617 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005618 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005619 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005620 return LegalizeOp(NV);
5621 break; // The target decided this was legal after all
5622 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005623 }
5624
Chris Lattner13689e22005-05-12 07:00:44 +00005625 // Expand the source, then glue it back together for the call. We must expand
5626 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005627 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005628 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005629 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005630 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005631 }
Chris Lattner13689e22005-05-12 07:00:44 +00005632
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005633 RTLIB::Libcall LC = isSigned ?
5634 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5635 RTLIB::getUINTTOFP(SourceVT, DestTy);
5636 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5637
Dale Johannesenaf435272009-02-02 19:03:57 +00005638 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005640 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5641 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesenaf435272009-02-02 19:03:57 +00005642 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmana2e94852008-03-10 23:03:31 +00005643 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005644}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005645
Chris Lattner22cde6a2006-01-28 08:25:58 +00005646/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5647/// INT_TO_FP operation of the specified operand when the target requests that
5648/// we expand it. At this point, we know that the result and operand types are
5649/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005650SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5651 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00005652 MVT DestVT,
5653 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005654 if (Op0.getValueType() == MVT::i32) {
5655 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00005656
Chris Lattner23594d42008-01-16 07:03:22 +00005657 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005658 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00005659
Chris Lattner22cde6a2006-01-28 08:25:58 +00005660 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005662 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005663 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005664 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005665 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005666 if (TLI.isLittleEndian())
5667 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00005668
Chris Lattner22cde6a2006-01-28 08:25:58 +00005669 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005670 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005671 if (isSigned) {
5672 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005673 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00005674 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005675 } else {
5676 Op0Mapped = Op0;
5677 }
5678 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00005679 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005680 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005681 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005682 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005683 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00005684 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005685 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00005686 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005687 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00005689 BitsToDouble(0x4330000080000000ULL) :
5690 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005691 MVT::f64);
5692 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00005693 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005694 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005695 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005696 // handle final rounding
5697 if (DestVT == MVT::f64) {
5698 // do nothing
5699 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005700 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005701 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00005702 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005703 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005704 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005705 }
5706 return Result;
5707 }
5708 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00005709 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005710
Dale Johannesenaf435272009-02-02 19:03:57 +00005711 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005712 Op0, DAG.getConstant(0, Op0.getValueType()),
5713 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005714 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00005715 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005716 SignSet, Four, Zero);
5717
5718 // If the sign bit of the integer is set, the large number will be treated
5719 // as a negative number. To counteract this, the dynamic code adds an
5720 // offset depending on the data type.
5721 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005722 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005723 default: assert(0 && "Unsupported integer type!");
5724 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5725 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5726 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5727 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5728 }
5729 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00005730 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005731
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00005733 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00005734 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005735 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005737 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00005738 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005739 PseudoSourceValue::getConstantPool(), 0,
5740 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005741 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005742 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00005743 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00005744 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005745 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005746 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005747 }
5748
Dale Johannesenaf435272009-02-02 19:03:57 +00005749 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005750}
5751
5752/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5753/// *INT_TO_FP operation of the specified operand when the target requests that
5754/// we promote it. At this point, we know that the result and operand types are
5755/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5756/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005757SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5758 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00005759 bool isSigned,
5760 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005761 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005762 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005763
5764 unsigned OpToUse = 0;
5765
5766 // Scan for the appropriate larger type to use.
5767 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005768 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5769 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005770
5771 // If the target supports SINT_TO_FP of this type, use it.
5772 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5773 default: break;
5774 case TargetLowering::Legal:
5775 if (!TLI.isTypeLegal(NewInTy))
5776 break; // Can't use this datatype.
5777 // FALL THROUGH.
5778 case TargetLowering::Custom:
5779 OpToUse = ISD::SINT_TO_FP;
5780 break;
5781 }
5782 if (OpToUse) break;
5783 if (isSigned) continue;
5784
5785 // If the target supports UINT_TO_FP of this type, use it.
5786 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5787 default: break;
5788 case TargetLowering::Legal:
5789 if (!TLI.isTypeLegal(NewInTy))
5790 break; // Can't use this datatype.
5791 // FALL THROUGH.
5792 case TargetLowering::Custom:
5793 OpToUse = ISD::UINT_TO_FP;
5794 break;
5795 }
5796 if (OpToUse) break;
5797
5798 // Otherwise, try a larger type.
5799 }
5800
5801 // Okay, we found the operation and type to use. Zero extend our input to the
5802 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00005803 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005804 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00005805 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005806}
5807
5808/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5809/// FP_TO_*INT operation of the specified operand when the target requests that
5810/// we promote it. At this point, we know that the result and operand types are
5811/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5812/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00005813SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5814 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00005815 bool isSigned,
5816 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005817 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005818 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005819
5820 unsigned OpToUse = 0;
5821
5822 // Scan for the appropriate larger type to use.
5823 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005824 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5825 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005826
5827 // If the target supports FP_TO_SINT returning this type, use it.
5828 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5829 default: break;
5830 case TargetLowering::Legal:
5831 if (!TLI.isTypeLegal(NewOutTy))
5832 break; // Can't use this datatype.
5833 // FALL THROUGH.
5834 case TargetLowering::Custom:
5835 OpToUse = ISD::FP_TO_SINT;
5836 break;
5837 }
5838 if (OpToUse) break;
5839
5840 // If the target supports FP_TO_UINT of this type, use it.
5841 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5842 default: break;
5843 case TargetLowering::Legal:
5844 if (!TLI.isTypeLegal(NewOutTy))
5845 break; // Can't use this datatype.
5846 // FALL THROUGH.
5847 case TargetLowering::Custom:
5848 OpToUse = ISD::FP_TO_UINT;
5849 break;
5850 }
5851 if (OpToUse) break;
5852
5853 // Otherwise, try a larger type.
5854 }
5855
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Chris Lattner27a6c732007-11-24 07:07:01 +00005857 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00005858 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00005859
Chris Lattner27a6c732007-11-24 07:07:01 +00005860 // If the operation produces an invalid type, it must be custom lowered. Use
5861 // the target lowering hooks to expand it. Just keep the low part of the
5862 // expanded operation, we know that we're truncating anyway.
5863 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00005864 SmallVector<SDValue, 2> Results;
5865 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
5866 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
5867 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00005868 }
Duncan Sands126d9072008-07-04 11:47:58 +00005869
Chris Lattner27a6c732007-11-24 07:07:01 +00005870 // Truncate the result of the extended FP_TO_*INT operation to the desired
5871 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00005872 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005873}
5874
5875/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5876///
Dale Johannesen8a782a22009-02-02 22:12:50 +00005877SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005878 MVT VT = Op.getValueType();
5879 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005880 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005881 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005882 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5883 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005884 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5885 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5886 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005887 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005888 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
5889 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5890 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5891 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
5892 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5893 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5894 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
5895 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
5896 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005897 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005898 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
5899 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
5900 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
5901 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
5902 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
5903 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
5904 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
5905 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
5906 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5907 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5908 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5909 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5910 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5911 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5912 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
5913 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
5914 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
5915 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
5916 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
5917 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
5918 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005919 }
5920}
5921
5922/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5923///
Scott Michelfdc40a02009-02-17 22:15:04 +00005924SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005925 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005926 switch (Opc) {
5927 default: assert(0 && "Cannot expand this yet!");
5928 case ISD::CTPOP: {
5929 static const uint64_t mask[6] = {
5930 0x5555555555555555ULL, 0x3333333333333333ULL,
5931 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5932 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5933 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005934 MVT VT = Op.getValueType();
5935 MVT ShVT = TLI.getShiftAmountTy();
5936 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005937 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5938 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005939 unsigned EltSize = VT.isVector() ?
5940 VT.getVectorElementType().getSizeInBits() : len;
5941 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005942 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005943 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00005944 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00005945 DAG.getNode(ISD::AND, dl, VT,
5946 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
5947 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005948 }
5949 return Op;
5950 }
5951 case ISD::CTLZ: {
5952 // for now, we do this:
5953 // x = x | (x >> 1);
5954 // x = x | (x >> 2);
5955 // ...
5956 // x = x | (x >>16);
5957 // x = x | (x >>32); // for 64-bit input
5958 // return popcount(~x);
5959 //
5960 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005961 MVT VT = Op.getValueType();
5962 MVT ShVT = TLI.getShiftAmountTy();
5963 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005964 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005965 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005966 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00005967 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005968 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00005969 Op = DAG.getNOT(dl, Op, VT);
5970 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005971 }
5972 case ISD::CTTZ: {
5973 // for now, we use: { return popcount(~x & (x - 1)); }
5974 // unless the target has ctlz but not ctpop, in which case we use:
5975 // { return 32 - nlz(~x & (x-1)); }
5976 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005977 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005978 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
5979 DAG.getNOT(dl, Op, VT),
5980 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00005981 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005982 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005983 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
5984 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00005985 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005986 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00005987 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
5988 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005989 }
5990 }
5991}
Chris Lattnere34b3962005-01-19 04:19:40 +00005992
Dan Gohman475871a2008-07-27 21:46:04 +00005993/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00005994/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00005995/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00005996/// ExpandedNodes map is filled in for any results that are expanded, and the
5997/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00005998void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Eli Friedman1fde9c52009-05-24 02:46:31 +00005999 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00006000 MVT VT = Op.getValueType();
6001 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006002 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006003 DebugLoc dl = Node->getDebugLoc();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006004 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006005 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006006 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006007
Chris Lattner6fdcb252005-09-02 20:32:45 +00006008 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006009 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006010 = ExpandedNodes.find(Op);
6011 if (I != ExpandedNodes.end()) {
6012 Lo = I->second.first;
6013 Hi = I->second.second;
6014 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006015 }
6016
Chris Lattner3e928bb2005-01-07 07:47:09 +00006017 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006018 case ISD::CopyFromReg:
6019 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006020 case ISD::FP_ROUND_INREG:
Scott Michelfdc40a02009-02-17 22:15:04 +00006021 if (VT == MVT::ppcf128 &&
6022 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006023 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006024 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006025 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006026 Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006027 SDValue Result =
6028 TLI.LowerOperation(DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src,
6029 Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006030 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6031 Lo = Result.getNode()->getOperand(0);
6032 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006033 break;
6034 }
6035 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006036 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006037#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006038 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006039#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006040 assert(0 && "Do not know how to expand this operator!");
6041 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006042 case ISD::EXTRACT_ELEMENT:
6043 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006044 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006045 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006046 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006047 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006048 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6049 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6050 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006051 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00006052 Lo = DAG.getUNDEF(NVT);
6053 Hi = DAG.getUNDEF(NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006054 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006055 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006056 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006057 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6058 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6059 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006060 break;
6061 }
Evan Cheng00495212006-12-12 21:32:44 +00006062 case ISD::ConstantFP: {
6063 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006064 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006065 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006066 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6067 MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00006068 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
Dale Johannesena471c2e2007-10-11 18:07:22 +00006069 MVT::f64);
6070 break;
6071 }
Evan Cheng279101e2006-12-12 22:19:28 +00006072 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006073 if (getTypeAction(Lo.getValueType()) == Expand)
6074 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006075 break;
6076 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006077 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006078 // Return the operands.
6079 Lo = Node->getOperand(0);
6080 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006081 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006082
Chris Lattner27a6c732007-11-24 07:07:01 +00006083 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006084 if (Node->getNumValues() == 1) {
6085 ExpandOp(Op.getOperand(0), Lo, Hi);
6086 break;
6087 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006088 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006089 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006090 Op.getValue(1).getValueType() == MVT::Other &&
6091 "unhandled MERGE_VALUES");
6092 ExpandOp(Op.getOperand(0), Lo, Hi);
6093 // Remember that we legalized the chain.
6094 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6095 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006096
Chris Lattner58f79632005-12-12 22:27:43 +00006097 case ISD::SIGN_EXTEND_INREG:
6098 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006099 // sext_inreg the low part if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006100 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006101
Chris Lattner4bdd2752006-10-06 17:34:12 +00006102 // The high part gets the sign extension from the lo-part. This handles
6103 // things like sextinreg V:i64 from i8.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006104 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006105 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006106 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006107 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006108
Nate Begemand88fc032006-01-14 03:14:10 +00006109 case ISD::BSWAP: {
6110 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006111 SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
6112 Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
Nate Begemand88fc032006-01-14 03:14:10 +00006113 Lo = TempLo;
6114 break;
6115 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006116
Chris Lattneredb1add2005-05-11 04:51:16 +00006117 case ISD::CTPOP:
6118 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006119 Lo = DAG.getNode(ISD::ADD, dl, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6120 DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
6121 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006122 Hi = DAG.getConstant(0, NVT);
6123 break;
6124
Chris Lattner39a8f332005-05-12 19:05:01 +00006125 case ISD::CTLZ: {
6126 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006127 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006128 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006129 SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
6130 SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
6131 BitsC, ISD::SETNE);
6132 SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
6133 LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006134
Dale Johannesen8a782a22009-02-02 22:12:50 +00006135 Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006136 Hi = DAG.getConstant(0, NVT);
6137 break;
6138 }
6139
6140 case ISD::CTTZ: {
6141 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006142 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006143 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006144 SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
6145 SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
6146 BitsC, ISD::SETNE);
6147 SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
6148 HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006149
Dale Johannesen8a782a22009-02-02 22:12:50 +00006150 Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006151 Hi = DAG.getConstant(0, NVT);
6152 break;
6153 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006154
Nate Begemanacc398c2006-01-25 18:21:52 +00006155 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006156 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6157 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dale Johannesenc460ae92009-02-04 00:13:36 +00006158 Lo = DAG.getVAArg(NVT, dl, Ch, Ptr, Node->getOperand(2));
6159 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00006160
6161 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006162 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006163 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006164 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006165 std::swap(Lo, Hi);
6166 break;
6167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006168
Chris Lattner3e928bb2005-01-07 07:47:09 +00006169 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006170 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006171 SDValue Ch = LD->getChain(); // Legalize the chain.
6172 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006173 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006174 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006175 int SVOffset = LD->getSrcValueOffset();
6176 unsigned Alignment = LD->getAlignment();
6177 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006178
Evan Cheng466685d2006-10-09 20:57:25 +00006179 if (ExtType == ISD::NON_EXTLOAD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006180 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006181 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006182 if (VT == MVT::f32 || VT == MVT::f64) {
6183 // f32->i32 or f64->i64 one to one expansion.
6184 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006185 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006186 // Recursively expand the new load.
6187 if (getTypeAction(NVT) == Expand)
6188 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006189 break;
6190 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006191
Evan Cheng466685d2006-10-09 20:57:25 +00006192 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006193 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006194 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006195 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006196 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006197 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006198 Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006199 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006200
Evan Cheng466685d2006-10-09 20:57:25 +00006201 // Build a factor node to remember that this load is independent of the
6202 // other one.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006203 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006204 Hi.getValue(1));
Evan Cheng466685d2006-10-09 20:57:25 +00006205
6206 // Remember that we legalized the chain.
6207 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006208 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006209 std::swap(Lo, Hi);
6210 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006211 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006212
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006213 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6214 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006215 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dale Johannesen8a782a22009-02-02 22:12:50 +00006216 SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006217 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006218 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006219 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006220 ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
Evan Cheng548f6112006-12-13 03:19:57 +00006221 break;
6222 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006223
Evan Cheng466685d2006-10-09 20:57:25 +00006224 if (EVT == NVT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006225 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006226 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006227 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00006228 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006229 SVOffset, EVT, isVolatile,
6230 Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00006231
Evan Cheng466685d2006-10-09 20:57:25 +00006232 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006233 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006234
6235 if (ExtType == ISD::SEXTLOAD) {
6236 // The high part is obtained by SRA'ing all but one of the bits of the
6237 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006238 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006239 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Evan Cheng466685d2006-10-09 20:57:25 +00006240 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6241 } else if (ExtType == ISD::ZEXTLOAD) {
6242 // The high part is just a zero.
6243 Hi = DAG.getConstant(0, NVT);
6244 } else /* if (ExtType == ISD::EXTLOAD) */ {
6245 // The high part is undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00006246 Hi = DAG.getUNDEF(NVT);
Evan Cheng466685d2006-10-09 20:57:25 +00006247 }
6248 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006249 break;
6250 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006251 case ISD::AND:
6252 case ISD::OR:
6253 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006254 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006255 ExpandOp(Node->getOperand(0), LL, LH);
6256 ExpandOp(Node->getOperand(1), RL, RH);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006257 Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
6258 Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006259 break;
6260 }
6261 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006262 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006263 ExpandOp(Node->getOperand(1), LL, LH);
6264 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006265 if (getTypeAction(NVT) == Expand)
6266 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006267 Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006268 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006269 Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006270 break;
6271 }
Nate Begeman9373a812005-08-10 20:51:12 +00006272 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006273 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006274 ExpandOp(Node->getOperand(2), TL, TH);
6275 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006276 if (getTypeAction(NVT) == Expand)
6277 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006278 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Nate Begeman9373a812005-08-10 20:51:12 +00006279 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006280 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006281 Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Evan Cheng19103b12006-12-15 22:42:55 +00006282 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006283 break;
6284 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006285 case ISD::ANY_EXTEND:
6286 // The low part is any extension of the input (which degenerates to a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006287 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
Chris Lattner8137c9e2006-01-28 05:07:51 +00006288 // The high part is undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00006289 Hi = DAG.getUNDEF(NVT);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006290 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006291 case ISD::SIGN_EXTEND: {
6292 // The low part is just a sign extension of the input (which degenerates to
6293 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006294 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006295
Chris Lattner3e928bb2005-01-07 07:47:09 +00006296 // The high part is obtained by SRA'ing all but one of the bits of the lo
6297 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006298 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006299 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Chris Lattner8137c9e2006-01-28 05:07:51 +00006300 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006301 break;
6302 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006303 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006304 // The low part is just a zero extension of the input (which degenerates to
6305 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006306 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006307
Chris Lattner3e928bb2005-01-07 07:47:09 +00006308 // The high part is just a zero.
6309 Hi = DAG.getConstant(0, NVT);
6310 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006311
Chris Lattner4c948eb2007-02-13 23:55:16 +00006312 case ISD::TRUNCATE: {
6313 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006314 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006315 ExpandOp(Node->getOperand(0), NewLo, Hi);
Scott Michelfdc40a02009-02-17 22:15:04 +00006316
Chris Lattner4c948eb2007-02-13 23:55:16 +00006317 // The low part is now either the right size, or it is closer. If not the
6318 // right size, make an illegal truncate so we recursively expand it.
6319 if (NewLo.getValueType() != Node->getValueType(0))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006320 NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
Chris Lattner4c948eb2007-02-13 23:55:16 +00006321 ExpandOp(NewLo, Lo, Hi);
6322 break;
6323 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006324
Chris Lattner35481892005-12-23 00:16:34 +00006325 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006326 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006327 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6328 // If the target wants to, allow it to lower this itself.
6329 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6330 case Expand: assert(0 && "cannot expand FP!");
6331 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6332 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6333 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006334 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006335 }
6336
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006337 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006338 if (VT == MVT::f32 || VT == MVT::f64) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006339 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006340 if (getTypeAction(NVT) == Expand)
6341 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006342 break;
6343 }
6344
6345 // If source operand will be expanded to the same type as VT, i.e.
6346 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006347 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006348 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6349 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006350 break;
6351 }
6352
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006353 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006354 if (Tmp.getNode() == 0)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006355 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00006356
Chris Lattner35481892005-12-23 00:16:34 +00006357 ExpandOp(Tmp, Lo, Hi);
6358 break;
6359 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006360
Chris Lattner27a6c732007-11-24 07:07:01 +00006361 case ISD::READCYCLECOUNTER: {
Scott Michelfdc40a02009-02-17 22:15:04 +00006362 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
Chris Lattner308575b2005-11-20 22:56:56 +00006363 TargetLowering::Custom &&
6364 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006365 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006366 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006367 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006368 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006369 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006370 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006371 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006372
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006373 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006374 // This operation does not need a loop.
6375 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6376 assert(Tmp.getNode() && "Node must be custom expanded!");
6377 ExpandOp(Tmp.getValue(0), Lo, Hi);
6378 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6379 LegalizeOp(Tmp.getValue(1)));
6380 break;
6381 }
6382
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006383 case ISD::ATOMIC_LOAD_ADD:
6384 case ISD::ATOMIC_LOAD_SUB:
6385 case ISD::ATOMIC_LOAD_AND:
6386 case ISD::ATOMIC_LOAD_OR:
6387 case ISD::ATOMIC_LOAD_XOR:
6388 case ISD::ATOMIC_LOAD_NAND:
6389 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006390 // These operations require a loop to be generated. We can't do that yet,
6391 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006392 SDValue In2Lo, In2Hi, In2;
6393 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006394 In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006395 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00006396 SDValue Replace =
Dale Johannesen8a782a22009-02-02 22:12:50 +00006397 DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006398 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006399 Anode->getSrcValue(), Anode->getAlignment());
6400 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006401 ExpandOp(Result.getValue(0), Lo, Hi);
6402 // Remember that we legalized the chain.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006403 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006404 break;
6405 }
6406
Chris Lattner4e6c7462005-01-08 19:27:05 +00006407 // These operators cannot be expanded directly, emit them as calls to
6408 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006409 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006410 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006411 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006412 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6413 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006414 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6415 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006416 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006417
Dale Johannesen8a782a22009-02-02 22:12:50 +00006418 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
Chris Lattnerf20d1832005-07-30 01:40:57 +00006419
Chris Lattner80a3e942005-07-29 00:33:32 +00006420 // Now that the custom expander is done, expand the result, which is still
6421 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006422 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006423 ExpandOp(Op, Lo, Hi);
6424 break;
6425 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006426 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006427
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006428 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6429 VT);
6430 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6431 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006432 break;
Evan Cheng56966222007-01-12 02:11:51 +00006433 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006434
Evan Cheng56966222007-01-12 02:11:51 +00006435 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006436 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006437 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006438 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6439 case Expand: assert(0 && "cannot expand FP!");
6440 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6441 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6442 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006443
Dale Johannesen8a782a22009-02-02 22:12:50 +00006444 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006445
6446 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006447 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006448 ExpandOp(Op, Lo, Hi);
6449 break;
6450 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006451 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006452
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006453 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6454 VT);
6455 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6456 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006457 break;
Evan Cheng56966222007-01-12 02:11:51 +00006458 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006459
Evan Cheng05a2d562006-01-09 18:31:59 +00006460 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006461 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006462 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006463 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006464 SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006465 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006466 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006467 // Now that the custom expander is done, expand the result, which is
6468 // still VT.
6469 ExpandOp(Op, Lo, Hi);
6470 break;
6471 }
6472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006473
6474 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
Chris Lattner79980b02006-09-13 03:50:39 +00006475 // this X << 1 as X+X.
6476 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006477 if (ShAmt->getAPIntValue() == 1 &&
Scott Michelfdc40a02009-02-17 22:15:04 +00006478 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006479 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006480 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006481 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6482 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6483 LoOps[1] = LoOps[0];
Dale Johannesen8a782a22009-02-02 22:12:50 +00006484 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattner79980b02006-09-13 03:50:39 +00006485
6486 HiOps[1] = HiOps[0];
6487 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006488 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattner79980b02006-09-13 03:50:39 +00006489 break;
6490 }
6491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006492
Chris Lattnere34b3962005-01-19 04:19:40 +00006493 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006494 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006495 break;
Chris Lattner47599822005-04-02 03:38:53 +00006496
6497 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006498 TargetLowering::LegalizeAction Action =
6499 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6500 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6501 Action == TargetLowering::Custom) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006502 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006503 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006504 break;
6505 }
6506
Chris Lattnere34b3962005-01-19 04:19:40 +00006507 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006508 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006509 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006510 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006511
Evan Cheng05a2d562006-01-09 18:31:59 +00006512 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006513 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006514 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006515 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dale Johannesene72c5962009-02-06 21:55:48 +00006516 SDValue Op = DAG.getNode(ISD::SRA, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006517 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006518 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006519 // Now that the custom expander is done, expand the result, which is
6520 // still VT.
6521 ExpandOp(Op, Lo, Hi);
6522 break;
6523 }
6524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006525
Chris Lattnere34b3962005-01-19 04:19:40 +00006526 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006527 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006528 break;
Chris Lattner47599822005-04-02 03:38:53 +00006529
6530 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006531 TargetLowering::LegalizeAction Action =
6532 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6533 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6534 Action == TargetLowering::Custom) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006535 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006536 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006537 break;
6538 }
6539
Chris Lattnere34b3962005-01-19 04:19:40 +00006540 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006541 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006542 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006543 }
6544
6545 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006546 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006547 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006548 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006549 SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006550 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006551 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006552 // Now that the custom expander is done, expand the result, which is
6553 // still VT.
6554 ExpandOp(Op, Lo, Hi);
6555 break;
6556 }
6557 }
6558
Chris Lattnere34b3962005-01-19 04:19:40 +00006559 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006560 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006561 break;
Chris Lattner47599822005-04-02 03:38:53 +00006562
6563 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006564 TargetLowering::LegalizeAction Action =
6565 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6566 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6567 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006568 ExpandShiftParts(ISD::SRL_PARTS,
6569 Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006570 break;
6571 }
6572
Chris Lattnere34b3962005-01-19 04:19:40 +00006573 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006574 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006575 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006576 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006577
Misha Brukmanedf128a2005-04-21 22:36:52 +00006578 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006579 case ISD::SUB: {
6580 // If the target wants to custom expand this, let them.
6581 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6582 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006584 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006585 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006586 break;
6587 }
6588 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006589 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006590 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006591 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6592 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006593 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006594 LoOps[0] = LHSL;
6595 LoOps[1] = RHSL;
6596 HiOps[0] = LHSH;
6597 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006598
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006599 //cascaded check to see if any smaller size has a a carry flag.
6600 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6601 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006602 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6603 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006604 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006605 hasCarry = true;
6606 break;
6607 }
6608 }
6609
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006610 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006611 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006612 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006613 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006614 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006615 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006616 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006617 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006618 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006619 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006620 }
6621 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006622 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006623 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006624 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
6625 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
6626 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006627 Lo, LoOps[0], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006628 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
Scott Michelfdc40a02009-02-17 22:15:04 +00006629 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006630 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006631 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006632 Lo, LoOps[1], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006633 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
Scott Michelfdc40a02009-02-17 22:15:04 +00006634 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006635 Carry1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006636 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006637 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006638 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
6639 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
6640 SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
6641 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
Scott Michelfdc40a02009-02-17 22:15:04 +00006642 DAG.getConstant(1, NVT),
Andrew Lenharth40d51392008-10-07 14:15:42 +00006643 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006644 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006645 }
6646 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006647 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006648 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006649
Chris Lattnerb429f732007-05-17 18:15:41 +00006650 case ISD::ADDC:
6651 case ISD::SUBC: {
6652 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006654 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6655 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6656 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006657 SDValue LoOps[2] = { LHSL, RHSL };
6658 SDValue HiOps[3] = { LHSH, RHSH };
Scott Michelfdc40a02009-02-17 22:15:04 +00006659
Chris Lattnerb429f732007-05-17 18:15:41 +00006660 if (Node->getOpcode() == ISD::ADDC) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006661 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00006662 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006663 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006664 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006665 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00006666 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006667 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006668 }
6669 // Remember that we legalized the flag.
6670 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6671 break;
6672 }
6673 case ISD::ADDE:
6674 case ISD::SUBE: {
6675 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006676 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006677 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6678 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6679 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006680 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6681 SDValue HiOps[3] = { LHSH, RHSH };
Scott Michelfdc40a02009-02-17 22:15:04 +00006682
Dale Johannesen8a782a22009-02-02 22:12:50 +00006683 Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00006684 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006685 Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006686
Chris Lattnerb429f732007-05-17 18:15:41 +00006687 // Remember that we legalized the flag.
6688 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6689 break;
6690 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006691 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006692 // If the target wants to custom expand this, let them.
6693 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006694 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006695 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006696 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006697 break;
6698 }
6699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006700
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006701 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
6702 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
6703 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
6704 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006705 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006707 ExpandOp(Node->getOperand(0), LL, LH);
6708 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006709 unsigned OuterBitSize = Op.getValueSizeInBits();
6710 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006711 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6712 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006713 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6714 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6715 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006716 // The inputs are both zero-extended.
6717 if (HasUMUL_LOHI) {
6718 // We can emit a umul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006719 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006720 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006721 break;
6722 }
6723 if (HasMULHU) {
6724 // We can emit a mulhu+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006725 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6726 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00006727 break;
6728 }
Dan Gohman525178c2007-10-08 18:33:35 +00006729 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006730 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006731 // The input values are both sign-extended.
6732 if (HasSMUL_LOHI) {
6733 // We can emit a smul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006734 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006735 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006736 break;
6737 }
6738 if (HasMULHS) {
6739 // We can emit a mulhs+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006740 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6741 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00006742 break;
6743 }
6744 }
6745 if (HasUMUL_LOHI) {
6746 // Lo,Hi = umul LHS, RHS.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006747 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00006748 DAG.getVTList(NVT, NVT), LL, RL);
6749 Lo = UMulLOHI;
6750 Hi = UMulLOHI.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006751 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
6752 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
6753 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
6754 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006755 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006756 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006757 if (HasMULHU) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006758 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
6759 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
6760 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
6761 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
6762 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
6763 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006764 break;
6765 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006766 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006767
Dan Gohman525178c2007-10-08 18:33:35 +00006768 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006769 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006770 break;
6771 }
Evan Cheng56966222007-01-12 02:11:51 +00006772 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006773 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006774 break;
6775 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006776 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006777 break;
6778 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006779 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006780 break;
6781 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006782 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006783 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006784
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006785 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006786 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6787 RTLIB::ADD_F64,
6788 RTLIB::ADD_F80,
6789 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006790 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006791 break;
6792 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006793 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6794 RTLIB::SUB_F64,
6795 RTLIB::SUB_F80,
6796 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006797 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006798 break;
6799 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00006800 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6801 RTLIB::MUL_F64,
6802 RTLIB::MUL_F80,
6803 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006804 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006805 break;
6806 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006807 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6808 RTLIB::DIV_F64,
6809 RTLIB::DIV_F80,
6810 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006811 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006812 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006813 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006814 if (VT == MVT::ppcf128) {
6815 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6816 Node->getOperand(0).getValueType()==MVT::f64);
6817 const uint64_t zero = 0;
6818 if (Node->getOperand(0).getValueType()==MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006819 Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006820 else
6821 Hi = Node->getOperand(0);
6822 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6823 break;
6824 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006825 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6826 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6827 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006828 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006829 }
6830 case ISD::FP_ROUND: {
6831 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6832 VT);
6833 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6834 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006835 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006836 }
Evan Cheng4b887022008-09-09 23:02:14 +00006837 case ISD::FSQRT:
6838 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00006839 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006840 case ISD::FLOG:
6841 case ISD::FLOG2:
6842 case ISD::FLOG10:
6843 case ISD::FEXP:
6844 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006845 case ISD::FTRUNC:
6846 case ISD::FFLOOR:
6847 case ISD::FCEIL:
6848 case ISD::FRINT:
6849 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00006850 case ISD::FPOW:
6851 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00006852 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006853 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006854 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006855 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6856 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006857 break;
6858 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006859 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6860 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006861 break;
6862 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006863 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6864 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006865 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006866 case ISD::FLOG:
6867 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
6868 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
6869 break;
6870 case ISD::FLOG2:
6871 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
6872 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
6873 break;
6874 case ISD::FLOG10:
6875 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
6876 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
6877 break;
6878 case ISD::FEXP:
6879 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
6880 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
6881 break;
6882 case ISD::FEXP2:
6883 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
6884 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
6885 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006886 case ISD::FTRUNC:
6887 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
6888 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
6889 break;
6890 case ISD::FFLOOR:
6891 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
6892 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
6893 break;
6894 case ISD::FCEIL:
6895 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
6896 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
6897 break;
6898 case ISD::FRINT:
6899 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
6900 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
6901 break;
6902 case ISD::FNEARBYINT:
6903 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
6904 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
6905 break;
Evan Cheng4b887022008-09-09 23:02:14 +00006906 case ISD::FPOW:
6907 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
6908 RTLIB::POW_PPCF128);
6909 break;
6910 case ISD::FPOWI:
6911 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
6912 RTLIB::POWI_PPCF128);
6913 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006914 default: assert(0 && "Unreachable!");
6915 }
Duncan Sands460a14e2008-04-12 17:14:18 +00006916 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006917 break;
6918 }
Evan Cheng966bf242006-12-16 00:52:40 +00006919 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006920 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006921 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00006922 ExpandOp(Node->getOperand(0), Lo, Tmp);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006923 Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
Dale Johannesenf6467742007-10-12 19:02:17 +00006924 // lo = hi==fabs(hi) ? lo : -lo;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006925 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006926 Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
6927 DAG.getCondCode(ISD::SETEQ));
Dale Johannesenf6467742007-10-12 19:02:17 +00006928 break;
6929 }
Dan Gohman475871a2008-07-27 21:46:04 +00006930 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006931 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6932 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006933 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
6934 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
6935 Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00006936 if (getTypeAction(NVT) == Expand)
6937 ExpandOp(Lo, Lo, Hi);
6938 break;
6939 }
6940 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006941 if (VT == MVT::ppcf128) {
6942 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006943 Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
6944 Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
Dale Johannesenf6467742007-10-12 19:02:17 +00006945 break;
6946 }
Dan Gohman475871a2008-07-27 21:46:04 +00006947 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006948 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6949 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006950 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
6951 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
6952 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00006953 if (getTypeAction(NVT) == Expand)
6954 ExpandOp(Lo, Lo, Hi);
6955 break;
6956 }
Evan Cheng912095b2007-01-04 21:56:39 +00006957 case ISD::FCOPYSIGN: {
6958 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6959 if (getTypeAction(NVT) == Expand)
6960 ExpandOp(Lo, Lo, Hi);
6961 break;
6962 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006963 case ISD::SINT_TO_FP:
6964 case ISD::UINT_TO_FP: {
6965 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006966 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00006967
6968 // Promote the operand if needed. Do this before checking for
6969 // ppcf128 so conversions of i16 and i8 work.
6970 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00006971 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00006972 Tmp = isSigned
Dale Johannesen8a782a22009-02-02 22:12:50 +00006973 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
Dale Johannesencf498192008-03-18 17:28:38 +00006974 DAG.getValueType(SrcVT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006975 : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006976 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00006977 SrcVT = Node->getOperand(0).getValueType();
6978 }
6979
Dan Gohmana2e94852008-03-10 23:03:31 +00006980 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006981 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006982 if (isSigned) {
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);
6986 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006987 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Scott Michelfdc40a02009-02-17 22:15:04 +00006988 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006989 Node->getOperand(0)));
6990 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006991 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006992 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen8a782a22009-02-02 22:12:50 +00006993 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
6994 MVT::ppcf128, Node->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00006995 DAG.getConstant(0, MVT::i32),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006996 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00006997 DAG.getConstantFP
6998 (APFloat(APInt(128, 2, TwoE32)),
6999 MVT::ppcf128)),
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007000 Hi,
7001 DAG.getCondCode(ISD::SETLT)),
7002 Lo, Hi);
7003 }
7004 break;
7005 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007006 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7007 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007008 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007009 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
7010 Node->getOperand(0)), Lo, Hi);
7011 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007012 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
Dale Johannesen8a782a22009-02-02 22:12:50 +00007013 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
7014 Node->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00007015 DAG.getConstant(0, MVT::i64),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007016 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007017 DAG.getConstantFP
7018 (APFloat(APInt(128, 2, TwoE64)),
7019 MVT::ppcf128)),
Dale Johannesen6e63e092007-10-12 17:52:03 +00007020 Hi,
7021 DAG.getCondCode(ISD::SETLT)),
7022 Lo, Hi);
7023 break;
7024 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007025
Dan Gohmana2e94852008-03-10 23:03:31 +00007026 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00007027 Node->getOperand(0), dl);
Evan Cheng110cf482008-04-01 01:50:16 +00007028 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007029 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007030 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007031 break;
7032 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007033 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007034
Chris Lattner83397362005-12-21 18:02:52 +00007035 // Make sure the resultant values have been legalized themselves, unless this
7036 // is a type that requires multi-step expansion.
7037 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7038 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007039 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007040 // Don't legalize the high part if it is expanded to a single node.
7041 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007042 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007043
7044 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007045 bool isNew =
7046 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007047 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007048 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007049}
7050
Dan Gohman7f321562007-06-25 16:23:39 +00007051/// SplitVectorOp - Given an operand of vector type, break it down into
7052/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007053void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7054 SDValue &Hi) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007055 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00007056 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007057 SDNode *Node = Op.getNode();
Dale Johannesenbb5da912009-02-02 20:41:04 +00007058 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007059 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007060 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007061
Duncan Sands83ec4b62008-06-06 12:08:01 +00007062 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007063
7064 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7065 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7066
Duncan Sands83ec4b62008-06-06 12:08:01 +00007067 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7068 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007069
Chris Lattnerc7029802006-03-18 01:44:44 +00007070 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007071 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007072 = SplitNodes.find(Op);
7073 if (I != SplitNodes.end()) {
7074 Lo = I->second.first;
7075 Hi = I->second.second;
7076 return;
7077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007078
Chris Lattnerc7029802006-03-18 01:44:44 +00007079 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007080 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007081#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007082 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007083#endif
7084 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007085 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00007086 Lo = DAG.getUNDEF(NewVT_Lo);
7087 Hi = DAG.getUNDEF(NewVT_Hi);
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007088 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007089 case ISD::BUILD_PAIR:
7090 Lo = Node->getOperand(0);
7091 Hi = Node->getOperand(1);
7092 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007093 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007094 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7095 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007096 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007097 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007098 if (Index < NewNumElts_Lo)
Dale Johannesenc6be1102009-02-02 22:49:46 +00007099 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007100 DAG.getIntPtrConstant(Index));
7101 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007102 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007103 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7104 break;
7105 }
Dan Gohman475871a2008-07-27 21:46:04 +00007106 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007107 Node->getOperand(1),
7108 Node->getOperand(2), dl);
Nate Begeman68679912008-04-25 18:07:40 +00007109 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007110 break;
7111 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007112 case ISD::VECTOR_SHUFFLE: {
7113 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue Mask = Node->getOperand(2);
7115 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007116 MVT PtrVT = TLI.getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00007117
7118 // Insert all of the elements from the input that are needed. We use
Chris Lattner6c9c6802007-11-19 21:16:54 +00007119 // buildvector of extractelement here because the input vectors will have
7120 // to be legalized, so this makes the code simpler.
7121 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007123 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007124 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007125 continue;
7126 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007127 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007128 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007129 if (Idx >= NumElements) {
7130 InVec = Node->getOperand(1);
7131 Idx -= NumElements;
7132 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007133 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007134 DAG.getConstant(Idx, PtrVT)));
7135 }
Evan Chenga87008d2009-02-25 22:49:59 +00007136 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007137 Ops.clear();
Scott Michelfdc40a02009-02-17 22:15:04 +00007138
Chris Lattner6c9c6802007-11-19 21:16:54 +00007139 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007140 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007141 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007142 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007143 continue;
7144 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007145 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007147 if (Idx >= NumElements) {
7148 InVec = Node->getOperand(1);
7149 Idx -= NumElements;
7150 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007151 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007152 DAG.getConstant(Idx, PtrVT)));
7153 }
Evan Chenga87008d2009-02-25 22:49:59 +00007154 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007155 break;
7156 }
Dan Gohman7f321562007-06-25 16:23:39 +00007157 case ISD::BUILD_VECTOR: {
Scott Michelfdc40a02009-02-17 22:15:04 +00007158 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007159 Node->op_begin()+NewNumElts_Lo);
Evan Chenga87008d2009-02-25 22:49:59 +00007160 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007161
Scott Michelfdc40a02009-02-17 22:15:04 +00007162 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007163 Node->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00007164 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007165 break;
7166 }
Dan Gohman7f321562007-06-25 16:23:39 +00007167 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007168 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007169 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7170 if (NewNumSubvectors == 1) {
7171 Lo = Node->getOperand(0);
7172 Hi = Node->getOperand(1);
7173 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007174 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7175 Node->op_begin()+NewNumSubvectors);
Scott Michelfdc40a02009-02-17 22:15:04 +00007176 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007177 &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007178
Mon P Wangaeb06d22008-11-10 04:46:22 +00007179 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007180 Node->op_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00007181 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007182 &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007183 }
Dan Gohman65956352007-06-13 15:12:02 +00007184 break;
7185 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007186 case ISD::EXTRACT_SUBVECTOR: {
7187 SDValue Vec = Op.getOperand(0);
7188 SDValue Idx = Op.getOperand(1);
7189 MVT IdxVT = Idx.getValueType();
7190
Dale Johannesenc6be1102009-02-02 22:49:46 +00007191 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007192 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7193 if (CIdx) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007194 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007195 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7196 IdxVT));
7197 } else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007198 Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007199 DAG.getConstant(NewNumElts_Lo, IdxVT));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007200 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007201 }
7202 break;
7203 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007204 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007205 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007206
Dan Gohman475871a2008-07-27 21:46:04 +00007207 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007208 SplitVectorOp(Node->getOperand(1), LL, LH);
7209 SplitVectorOp(Node->getOperand(2), RL, RH);
7210
Duncan Sands83ec4b62008-06-06 12:08:01 +00007211 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007212 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007214 SplitVectorOp(Cond, CL, CH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007215 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
7216 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007217 } else {
7218 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007219 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
7220 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007221 }
7222 break;
7223 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007224 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007225 SDValue CondLHS = Node->getOperand(0);
7226 SDValue CondRHS = Node->getOperand(1);
7227 SDValue CondCode = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00007228
Dan Gohman475871a2008-07-27 21:46:04 +00007229 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007230 SplitVectorOp(Node->getOperand(2), LL, LH);
7231 SplitVectorOp(Node->getOperand(3), RL, RH);
Scott Michelfdc40a02009-02-17 22:15:04 +00007232
Chris Lattner80c1a562008-06-30 02:43:01 +00007233 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007234 Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007235 LL, RL, CondCode);
Scott Michelfdc40a02009-02-17 22:15:04 +00007236 Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007237 LH, RH, CondCode);
7238 break;
7239 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007240 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007241 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007242 SplitVectorOp(Node->getOperand(0), LL, LH);
7243 SplitVectorOp(Node->getOperand(1), RL, RH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007244 Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
7245 Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
Nate Begemanb43e9c12008-05-12 19:40:03 +00007246 break;
7247 }
Dan Gohman7f321562007-06-25 16:23:39 +00007248 case ISD::ADD:
7249 case ISD::SUB:
7250 case ISD::MUL:
7251 case ISD::FADD:
7252 case ISD::FSUB:
7253 case ISD::FMUL:
7254 case ISD::SDIV:
7255 case ISD::UDIV:
7256 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007257 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007258 case ISD::AND:
7259 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007260 case ISD::XOR:
7261 case ISD::UREM:
7262 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007263 case ISD::FREM:
7264 case ISD::SHL:
7265 case ISD::SRA:
7266 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007267 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007268 SplitVectorOp(Node->getOperand(0), LL, LH);
7269 SplitVectorOp(Node->getOperand(1), RL, RH);
Scott Michelfdc40a02009-02-17 22:15:04 +00007270
Dale Johannesenc6be1102009-02-02 22:49:46 +00007271 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
7272 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007273 break;
7274 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007275 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007276 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007277 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007278 SplitVectorOp(Node->getOperand(0), L, H);
7279
Dale Johannesenc6be1102009-02-02 22:49:46 +00007280 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
7281 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007282 break;
7283 }
7284 case ISD::CTTZ:
7285 case ISD::CTLZ:
7286 case ISD::CTPOP:
7287 case ISD::FNEG:
7288 case ISD::FABS:
7289 case ISD::FSQRT:
7290 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007291 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007292 case ISD::FLOG:
7293 case ISD::FLOG2:
7294 case ISD::FLOG10:
7295 case ISD::FEXP:
7296 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007297 case ISD::FP_TO_SINT:
7298 case ISD::FP_TO_UINT:
7299 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007300 case ISD::UINT_TO_FP:
7301 case ISD::TRUNCATE:
7302 case ISD::ANY_EXTEND:
7303 case ISD::SIGN_EXTEND:
7304 case ISD::ZERO_EXTEND:
7305 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007306 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007307 SplitVectorOp(Node->getOperand(0), L, H);
7308
Dale Johannesenc6be1102009-02-02 22:49:46 +00007309 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
7310 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007311 break;
7312 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007313 case ISD::CONVERT_RNDSAT: {
7314 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7315 SDValue L, H;
7316 SplitVectorOp(Node->getOperand(0), L, H);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007317 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7318 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7319 SDValue STyOpL = DAG.getValueType(L.getValueType());
7320 SDValue STyOpH = DAG.getValueType(H.getValueType());
Mon P Wang77cdf302008-11-10 20:54:11 +00007321
7322 SDValue RndOp = Node->getOperand(3);
7323 SDValue SatOp = Node->getOperand(4);
7324
Dale Johannesenc460ae92009-02-04 00:13:36 +00007325 Lo = DAG.getConvertRndSat(NewVT_Lo, dl, L, DTyOpL, STyOpL,
Mon P Wang77cdf302008-11-10 20:54:11 +00007326 RndOp, SatOp, CvtCode);
Dale Johannesenc460ae92009-02-04 00:13:36 +00007327 Hi = DAG.getConvertRndSat(NewVT_Hi, dl, H, DTyOpH, STyOpH,
Mon P Wang77cdf302008-11-10 20:54:11 +00007328 RndOp, SatOp, CvtCode);
7329 break;
7330 }
Dan Gohman7f321562007-06-25 16:23:39 +00007331 case ISD::LOAD: {
7332 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007333 SDValue Ch = LD->getChain();
7334 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007335 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007336 const Value *SV = LD->getSrcValue();
7337 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007338 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007339 unsigned Alignment = LD->getAlignment();
7340 bool isVolatile = LD->isVolatile();
7341
Dan Gohman7f8613e2008-08-14 20:04:46 +00007342 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesene8d72302009-02-06 23:05:02 +00007343 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007344
7345 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7346 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7347 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7348
Dale Johannesenc6be1102009-02-02 22:49:46 +00007349 Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007350 NewVT_Lo, Ch, Ptr, Offset,
7351 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7352 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dale Johannesenc6be1102009-02-02 22:49:46 +00007353 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007354 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007355 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007356 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007357 Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007358 NewVT_Hi, Ch, Ptr, Offset,
7359 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Scott Michelfdc40a02009-02-17 22:15:04 +00007360
Chris Lattnerc7029802006-03-18 01:44:44 +00007361 // Build a factor node to remember that this load is independent of the
7362 // other one.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007363 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007364 Hi.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00007365
Chris Lattnerc7029802006-03-18 01:44:44 +00007366 // Remember that we legalized the chain.
7367 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007368 break;
7369 }
Dan Gohman7f321562007-06-25 16:23:39 +00007370 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007371 // We know the result is a vector. The input may be either a vector or a
7372 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007373 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007374 if (!InOp.getValueType().isVector() ||
7375 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007376 // The input is a scalar or single-element vector.
7377 // Lower to a store/load so that it can be split.
7378 // FIXME: this could be improved probably.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007379 unsigned LdAlign = TLI.getTargetData()->
7380 getPrefTypeAlignment(Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007381 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007382 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007383
Dale Johannesenc6be1102009-02-02 22:49:46 +00007384 SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00007385 InOp, Ptr,
7386 PseudoSourceValue::getFixedStack(FI), 0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007387 InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007388 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007389 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007390 // Split the vector and convert each of the pieces now.
7391 SplitVectorOp(InOp, Lo, Hi);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007392 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
7393 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007394 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007395 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007396 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007397
Chris Lattnerc7029802006-03-18 01:44:44 +00007398 // Remember in a map if the values will be reused later.
Scott Michelfdc40a02009-02-17 22:15:04 +00007399 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007400 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007401 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007402 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007403}
7404
7405
Dan Gohman89b20c02007-06-27 14:06:22 +00007406/// ScalarizeVectorOp - Given an operand of single-element vector type
7407/// (e.g. v1f32), convert it into the equivalent operation that returns a
7408/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007409SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007410 assert(0 && "This should be dead!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00007411 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007412 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007413 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007414 MVT NewVT = Op.getValueType().getVectorElementType();
7415 assert(Op.getValueType().getVectorNumElements() == 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007416
Dan Gohman7f321562007-06-25 16:23:39 +00007417 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007418 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007419 if (I != ScalarizedNodes.end()) return I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00007420
Dan Gohman475871a2008-07-27 21:46:04 +00007421 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007422 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007423 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007424#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007425 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007426#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007427 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7428 case ISD::ADD:
7429 case ISD::FADD:
7430 case ISD::SUB:
7431 case ISD::FSUB:
7432 case ISD::MUL:
7433 case ISD::FMUL:
7434 case ISD::SDIV:
7435 case ISD::UDIV:
7436 case ISD::FDIV:
7437 case ISD::SREM:
7438 case ISD::UREM:
7439 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007440 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007441 case ISD::AND:
7442 case ISD::OR:
7443 case ISD::XOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007444 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007445 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007446 ScalarizeVectorOp(Node->getOperand(0)),
7447 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007448 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007449 case ISD::FNEG:
7450 case ISD::FABS:
7451 case ISD::FSQRT:
7452 case ISD::FSIN:
7453 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007454 case ISD::FLOG:
7455 case ISD::FLOG2:
7456 case ISD::FLOG10:
7457 case ISD::FEXP:
7458 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007459 case ISD::FP_TO_SINT:
7460 case ISD::FP_TO_UINT:
7461 case ISD::SINT_TO_FP:
7462 case ISD::UINT_TO_FP:
7463 case ISD::SIGN_EXTEND:
7464 case ISD::ZERO_EXTEND:
7465 case ISD::ANY_EXTEND:
7466 case ISD::TRUNCATE:
7467 case ISD::FP_EXTEND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007468 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007469 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007470 ScalarizeVectorOp(Node->getOperand(0)));
7471 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007472 case ISD::CONVERT_RNDSAT: {
7473 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
Dale Johannesenc460ae92009-02-04 00:13:36 +00007474 Result = DAG.getConvertRndSat(NewVT, dl, Op0,
Mon P Wang77cdf302008-11-10 20:54:11 +00007475 DAG.getValueType(NewVT),
7476 DAG.getValueType(Op0.getValueType()),
7477 Node->getOperand(3),
7478 Node->getOperand(4),
7479 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7480 break;
7481 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007482 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007483 case ISD::FP_ROUND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007484 Result = DAG.getNode(Node->getOpcode(), dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00007485 NewVT,
Dan Gohman9e04c822007-10-12 14:13:46 +00007486 ScalarizeVectorOp(Node->getOperand(0)),
7487 Node->getOperand(1));
7488 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007489 case ISD::LOAD: {
7490 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007491 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7492 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007493 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007494 const Value *SV = LD->getSrcValue();
7495 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007496 MVT MemoryVT = LD->getMemoryVT();
7497 unsigned Alignment = LD->getAlignment();
7498 bool isVolatile = LD->isVolatile();
7499
7500 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesene8d72302009-02-06 23:05:02 +00007501 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00007502
Dale Johannesenc6be1102009-02-02 22:49:46 +00007503 Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007504 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7505 MemoryVT.getVectorElementType(),
7506 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007507
Chris Lattnerc7029802006-03-18 01:44:44 +00007508 // Remember that we legalized the chain.
7509 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7510 break;
7511 }
Dan Gohman7f321562007-06-25 16:23:39 +00007512 case ISD::BUILD_VECTOR:
7513 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007514 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007515 case ISD::INSERT_VECTOR_ELT:
7516 // Returning the inserted scalar element.
7517 Result = Node->getOperand(1);
7518 break;
7519 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007520 assert(Node->getOperand(0).getValueType() == NewVT &&
7521 "Concat of non-legal vectors not yet supported!");
7522 Result = Node->getOperand(0);
7523 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007524 case ISD::VECTOR_SHUFFLE: {
7525 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007526 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007527 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007528 Result = ScalarizeVectorOp(Node->getOperand(1));
7529 else
7530 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007531 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007532 }
7533 case ISD::EXTRACT_SUBVECTOR:
Scott Michelfdc40a02009-02-17 22:15:04 +00007534 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007535 Node->getOperand(0), Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007536 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007537 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007538 SDValue Op0 = Op.getOperand(0);
Dan Gohman4e3fdf22009-05-11 18:30:42 +00007539 if (Op0.getValueType().isVector() &&
7540 Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007541 Op0 = ScalarizeVectorOp(Op0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007542 Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007543 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007544 }
Dan Gohman7f321562007-06-25 16:23:39 +00007545 case ISD::SELECT:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007546 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007547 ScalarizeVectorOp(Op.getOperand(1)),
7548 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007549 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007550 case ISD::SELECT_CC:
Scott Michelfdc40a02009-02-17 22:15:04 +00007551 Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0),
Chris Lattner80c1a562008-06-30 02:43:01 +00007552 Node->getOperand(1),
7553 ScalarizeVectorOp(Op.getOperand(2)),
7554 ScalarizeVectorOp(Op.getOperand(3)),
7555 Node->getOperand(4));
7556 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007557 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007558 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7559 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007560 Result = DAG.getNode(ISD::SETCC, dl,
7561 TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00007562 Op0, Op1, Op.getOperand(2));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007563 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
Nate Begeman0d1704b2008-05-12 23:09:43 +00007564 DAG.getConstant(-1ULL, NewVT),
7565 DAG.getConstant(0ULL, NewVT));
7566 break;
7567 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007568 }
7569
7570 if (TLI.isTypeLegal(NewVT))
7571 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007572 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7573 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007574 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007575 return Result;
7576}
7577
Chris Lattner3e928bb2005-01-07 07:47:09 +00007578
Mon P Wang0c397192008-10-30 08:01:45 +00007579SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00007580 assert(0 && "This should be dead!");
Mon P Wang0c397192008-10-30 08:01:45 +00007581 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7582 if (I != WidenNodes.end()) return I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00007583
Mon P Wang0c397192008-10-30 08:01:45 +00007584 MVT VT = Op.getValueType();
7585 assert(VT.isVector() && "Cannot widen non-vector type!");
7586
7587 SDValue Result;
7588 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007589 DebugLoc dl = Node->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00007590 MVT EVT = VT.getVectorElementType();
7591
7592 unsigned NumElts = VT.getVectorNumElements();
7593 unsigned NewNumElts = WidenVT.getVectorNumElements();
7594 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7595 assert(NewNumElts < 17);
7596
7597 // When widen is called, it is assumed that it is more efficient to use a
7598 // wide type. The default action is to widen to operation to a wider legal
7599 // vector type and then do the operation if it is legal by calling LegalizeOp
7600 // again. If there is no vector equivalent, we will unroll the operation, do
7601 // it, and rebuild the vector. If most of the operations are vectorizible to
7602 // the legal type, the resulting code will be more efficient. If this is not
7603 // the case, the resulting code will preform badly as we end up generating
7604 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007605 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007606 switch (Node->getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007607 default:
Mon P Wang0c397192008-10-30 08:01:45 +00007608#ifndef NDEBUG
7609 Node->dump(&DAG);
7610#endif
7611 assert(0 && "Unexpected operation in WidenVectorOp!");
7612 break;
7613 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007614 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007615 case ISD::Constant:
7616 case ISD::ConstantFP:
7617 // To build a vector of these elements, clients should call BuildVector
7618 // and with each element instead of creating a node with a vector type
7619 assert(0 && "Unexpected operation in WidenVectorOp!");
7620 case ISD::VAARG:
7621 // Variable Arguments with vector types doesn't make any sense to me
7622 assert(0 && "Unexpected operation in WidenVectorOp!");
7623 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007624 case ISD::UNDEF:
Dale Johannesene8d72302009-02-06 23:05:02 +00007625 Result = DAG.getUNDEF(WidenVT);
Mon P Wang49292f12008-11-15 06:05:52 +00007626 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007627 case ISD::BUILD_VECTOR: {
7628 // Build a vector with undefined for the new nodes
7629 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7630 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007631 NewOps.push_back(DAG.getUNDEF(EVT));
Mon P Wang0c397192008-10-30 08:01:45 +00007632 }
Evan Chenga87008d2009-02-25 22:49:59 +00007633 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT,
7634 &NewOps[0], NewOps.size());
Mon P Wang0c397192008-10-30 08:01:45 +00007635 break;
7636 }
7637 case ISD::INSERT_VECTOR_ELT: {
7638 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007639 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00007640 Node->getOperand(1), Node->getOperand(2));
7641 break;
7642 }
7643 case ISD::VECTOR_SHUFFLE: {
7644 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7645 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
Nate Begeman9008ca62009-04-27 18:41:29 +00007646 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Node);
7647 SmallVector<int, 8> NewMask;
Mon P Wang0c397192008-10-30 08:01:45 +00007648 for (unsigned i = 0; i < NumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007649 int Idx = SVOp->getMaskElt(i);
7650 if (Idx < (int)NumElts)
7651 NewMask.push_back(Idx);
7652 else
7653 NewMask.push_back(Idx + NewNumElts - NumElts);
Mon P Wang0c397192008-10-30 08:01:45 +00007654 }
Nate Begeman9008ca62009-04-27 18:41:29 +00007655 for (unsigned i = NumElts; i < NewNumElts; ++i)
7656 NewMask.push_back(-1);
7657
7658 Result = DAG.getVectorShuffle(WidenVT, dl, Tmp1, Tmp2, &NewMask[0]);
Mon P Wang0c397192008-10-30 08:01:45 +00007659 break;
7660 }
7661 case ISD::LOAD: {
7662 // If the load widen returns true, we can use a single load for the
7663 // vector. Otherwise, it is returning a token factor for multiple
7664 // loads.
7665 SDValue TFOp;
7666 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7667 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7668 else
7669 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7670 break;
7671 }
7672
7673 case ISD::BIT_CONVERT: {
7674 SDValue Tmp1 = Node->getOperand(0);
7675 // Converts between two different types so we need to determine
7676 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007677 MVT InVT = Tmp1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007678 unsigned WidenSize = WidenVT.getSizeInBits();
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007679 if (InVT.isVector()) {
7680 MVT InEltVT = InVT.getVectorElementType();
7681 unsigned InEltSize = InEltVT.getSizeInBits();
7682 assert(WidenSize % InEltSize == 0 &&
7683 "can not widen bit convert that are not multiple of element type");
7684 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
7685 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
7686 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007687 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007688 } else {
7689 // If the result size is a multiple of the input size, widen the input
7690 // and then convert.
7691 unsigned InSize = InVT.getSizeInBits();
7692 assert(WidenSize % InSize == 0 &&
7693 "can not widen bit convert that are not multiple of element type");
7694 unsigned NewNumElts = WidenSize / InSize;
7695 SmallVector<SDValue, 16> Ops(NewNumElts);
Dale Johannesene8d72302009-02-06 23:05:02 +00007696 SDValue UndefVal = DAG.getUNDEF(InVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007697 Ops[0] = Tmp1;
7698 for (unsigned i = 1; i < NewNumElts; ++i)
7699 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00007700
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007701 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
Evan Chenga87008d2009-02-25 22:49:59 +00007702 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007703 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00007704 }
7705 break;
7706 }
7707
7708 case ISD::SINT_TO_FP:
7709 case ISD::UINT_TO_FP:
7710 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007711 case ISD::FP_TO_UINT:
7712 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00007713 SDValue Tmp1 = Node->getOperand(0);
7714 // Converts between two different types so we need to determine
7715 // the correct widen type for the input operand.
7716 MVT TVT = Tmp1.getValueType();
7717 assert(TVT.isVector() && "can not widen non vector type");
7718 MVT TEVT = TVT.getVectorElementType();
7719 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7720 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7721 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007722 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007723 break;
7724 }
7725
7726 case ISD::FP_EXTEND:
7727 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7728 case ISD::TRUNCATE:
7729 case ISD::SIGN_EXTEND:
7730 case ISD::ZERO_EXTEND:
7731 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00007732 case ISD::SIGN_EXTEND_INREG:
7733 case ISD::FABS:
7734 case ISD::FNEG:
7735 case ISD::FSQRT:
7736 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00007737 case ISD::FCOS:
7738 case ISD::CTPOP:
7739 case ISD::CTTZ:
7740 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00007741 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007742 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00007743 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7744 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007745 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007746 break;
7747 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007748 case ISD::CONVERT_RNDSAT: {
7749 SDValue RndOp = Node->getOperand(3);
7750 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00007751 SDValue SrcOp = Node->getOperand(0);
7752
7753 // Converts between two different types so we need to determine
7754 // the correct widen type for the input operand.
7755 MVT SVT = SrcOp.getValueType();
7756 assert(SVT.isVector() && "can not widen non vector type");
7757 MVT SEVT = SVT.getVectorElementType();
7758 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7759
7760 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7761 assert(SrcOp.getValueType() == WidenVT);
7762 SDValue DTyOp = DAG.getValueType(WidenVT);
7763 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
7764 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7765
Dale Johannesenc460ae92009-02-04 00:13:36 +00007766 Result = DAG.getConvertRndSat(WidenVT, dl, SrcOp, DTyOp, STyOp,
Mon P Wang77cdf302008-11-10 20:54:11 +00007767 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00007768 break;
7769 }
Mon P Wang0c397192008-10-30 08:01:45 +00007770 case ISD::FPOW:
Scott Michelfdc40a02009-02-17 22:15:04 +00007771 case ISD::FPOWI:
Mon P Wang0c397192008-10-30 08:01:45 +00007772 case ISD::ADD:
7773 case ISD::SUB:
7774 case ISD::MUL:
7775 case ISD::MULHS:
7776 case ISD::MULHU:
7777 case ISD::AND:
7778 case ISD::OR:
7779 case ISD::XOR:
7780 case ISD::FADD:
7781 case ISD::FSUB:
7782 case ISD::FMUL:
7783 case ISD::SDIV:
7784 case ISD::SREM:
7785 case ISD::FDIV:
7786 case ISD::FREM:
7787 case ISD::FCOPYSIGN:
7788 case ISD::UDIV:
7789 case ISD::UREM:
7790 case ISD::BSWAP: {
7791 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00007792 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7793 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7794 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007795 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00007796 break;
7797 }
7798
7799 case ISD::SHL:
7800 case ISD::SRA:
7801 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00007802 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7803 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00007804 SDValue ShOp = Node->getOperand(1);
7805 MVT ShVT = ShOp.getValueType();
7806 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
7807 WidenVT.getVectorNumElements());
7808 ShOp = WidenVectorOp(ShOp, NewShVT);
7809 assert(ShOp.getValueType() == NewShVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007810 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00007811 break;
7812 }
Mon P Wangfb13f002008-12-02 07:35:08 +00007813
Mon P Wang0c397192008-10-30 08:01:45 +00007814 case ISD::EXTRACT_VECTOR_ELT: {
7815 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7816 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007817 Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
Mon P Wang0c397192008-10-30 08:01:45 +00007818 break;
7819 }
7820 case ISD::CONCAT_VECTORS: {
7821 // We concurrently support only widen on a multiple of the incoming vector.
7822 // We could widen on a multiple of the incoming operand if necessary.
7823 unsigned NumConcat = NewNumElts / NumElts;
7824 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Dale Johannesene8d72302009-02-06 23:05:02 +00007825 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang0c397192008-10-30 08:01:45 +00007826 SmallVector<SDValue, 8> MOps;
7827 MOps.push_back(Op);
7828 for (unsigned i = 1; i != NumConcat; ++i) {
7829 MOps.push_back(UndefVal);
7830 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007831 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang0c397192008-10-30 08:01:45 +00007832 &MOps[0], MOps.size()));
7833 break;
7834 }
7835 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00007836 SDValue Tmp1 = Node->getOperand(0);
7837 SDValue Idx = Node->getOperand(1);
7838 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7839 if (CIdx && CIdx->getZExtValue() == 0) {
7840 // Since we are access the start of the vector, the incoming
7841 // vector type might be the proper.
7842 MVT Tmp1VT = Tmp1.getValueType();
7843 if (Tmp1VT == WidenVT)
7844 return Tmp1;
7845 else {
7846 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
7847 if (Tmp1VTNumElts < NewNumElts)
7848 Result = WidenVectorOp(Tmp1, WidenVT);
7849 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007850 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
Mon P Wang49292f12008-11-15 06:05:52 +00007851 }
7852 } else if (NewNumElts % NumElts == 0) {
7853 // Widen the extracted subvector.
7854 unsigned NumConcat = NewNumElts / NumElts;
Dale Johannesene8d72302009-02-06 23:05:02 +00007855 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang49292f12008-11-15 06:05:52 +00007856 SmallVector<SDValue, 8> MOps;
7857 MOps.push_back(Op);
7858 for (unsigned i = 1; i != NumConcat; ++i) {
7859 MOps.push_back(UndefVal);
7860 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007861 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang49292f12008-11-15 06:05:52 +00007862 &MOps[0], MOps.size()));
7863 } else {
7864 assert(0 && "can not widen extract subvector");
7865 // This could be implemented using insert and build vector but I would
7866 // like to see when this happens.
7867 }
Mon P Wang0c397192008-10-30 08:01:45 +00007868 break;
7869 }
7870
7871 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00007872 // Determine new condition widen type and widen
7873 SDValue Cond1 = Node->getOperand(0);
7874 MVT CondVT = Cond1.getValueType();
7875 assert(CondVT.isVector() && "can not widen non vector type");
7876 MVT CondEVT = CondVT.getVectorElementType();
7877 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7878 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7879 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
7880
7881 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
7882 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
7883 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007884 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00007885 break;
7886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007887
Mon P Wang0c397192008-10-30 08:01:45 +00007888 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00007889 // Determine new condition widen type and widen
7890 SDValue Cond1 = Node->getOperand(0);
7891 SDValue Cond2 = Node->getOperand(1);
7892 MVT CondVT = Cond1.getValueType();
7893 assert(CondVT.isVector() && "can not widen non vector type");
7894 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
7895 MVT CondEVT = CondVT.getVectorElementType();
7896 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7897 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7898 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
7899 assert(Cond1.getValueType() == CondWidenVT &&
7900 Cond2.getValueType() == CondWidenVT && "condition not widen");
7901
7902 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
7903 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
7904 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
7905 "operands not widen");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007906 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00007907 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00007908 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00007909 }
7910 case ISD::VSETCC: {
7911 // Determine widen for the operand
7912 SDValue Tmp1 = Node->getOperand(0);
7913 MVT TmpVT = Tmp1.getValueType();
7914 assert(TmpVT.isVector() && "can not widen non vector type");
7915 MVT TmpEVT = TmpVT.getVectorElementType();
7916 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
7917 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
7918 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007919 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00007920 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00007921 break;
7922 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007923 case ISD::ATOMIC_CMP_SWAP:
7924 case ISD::ATOMIC_LOAD_ADD:
7925 case ISD::ATOMIC_LOAD_SUB:
7926 case ISD::ATOMIC_LOAD_AND:
7927 case ISD::ATOMIC_LOAD_OR:
7928 case ISD::ATOMIC_LOAD_XOR:
7929 case ISD::ATOMIC_LOAD_NAND:
7930 case ISD::ATOMIC_LOAD_MIN:
7931 case ISD::ATOMIC_LOAD_MAX:
7932 case ISD::ATOMIC_LOAD_UMIN:
7933 case ISD::ATOMIC_LOAD_UMAX:
7934 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00007935 // For now, we assume that using vectors for these operations don't make
7936 // much sense so we just split it. We return an empty result
7937 SDValue X, Y;
7938 SplitVectorOp(Op, X, Y);
7939 return Result;
7940 break;
7941 }
7942
7943 } // end switch (Node->getOpcode())
7944
Scott Michelfdc40a02009-02-17 22:15:04 +00007945 assert(Result.getNode() && "Didn't set a result!");
Mon P Wang0c397192008-10-30 08:01:45 +00007946 if (Result != Op)
7947 Result = LegalizeOp(Result);
7948
Mon P Wangf007a8b2008-11-06 05:31:54 +00007949 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00007950 return Result;
7951}
7952
7953// Utility function to find a legal vector type and its associated element
7954// type from a preferred width and whose vector type must be the same size
7955// as the VVT.
7956// TLI: Target lowering used to determine legal types
7957// Width: Preferred width of element type
7958// VVT: Vector value type whose size we must match.
7959// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00007960static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00007961 MVT& EVT, MVT& VecEVT) {
7962 // We start with the preferred width, make it a power of 2 and see if
7963 // we can find a vector type of that width. If not, we reduce it by
7964 // another power of 2. If we have widen the type, a vector of bytes should
7965 // always be legal.
7966 assert(TLI.isTypeLegal(VVT));
7967 unsigned EWidth = Width + 1;
7968 do {
7969 assert(EWidth > 0);
7970 EWidth = (1 << Log2_32(EWidth-1));
7971 EVT = MVT::getIntegerVT(EWidth);
7972 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
7973 VecEVT = MVT::getVectorVT(EVT, NumEVT);
7974 } while (!TLI.isTypeLegal(VecEVT) ||
7975 VVT.getSizeInBits() != VecEVT.getSizeInBits());
7976}
7977
7978SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
7979 SDValue Chain,
7980 SDValue BasePtr,
7981 const Value *SV,
7982 int SVOffset,
7983 unsigned Alignment,
7984 bool isVolatile,
7985 unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +00007986 MVT ResType,
7987 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00007988 // We assume that we have good rules to handle loading power of two loads so
7989 // we break down the operations to power of 2 loads. The strategy is to
7990 // load the largest power of 2 that we can easily transform to a legal vector
7991 // and then insert into that vector, and the cast the result into the legal
7992 // vector that we want. This avoids unnecessary stack converts.
7993 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
7994 // the load is nonvolatile, we an use a wider load for the value.
7995 // Find a vector length we can load a large chunk
7996 MVT EVT, VecEVT;
7997 unsigned EVTWidth;
7998 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
7999 EVTWidth = EVT.getSizeInBits();
8000
Dale Johannesenc6be1102009-02-02 22:49:46 +00008001 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008002 isVolatile, Alignment);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008003 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008004 LdChain.push_back(LdOp.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00008005
Mon P Wang0c397192008-10-30 08:01:45 +00008006 // Check if we can load the element with one instruction
8007 if (LdWidth == EVTWidth) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008008 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008009 }
8010
8011 // The vector element order is endianness dependent.
8012 unsigned Idx = 1;
8013 LdWidth -= EVTWidth;
8014 unsigned Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008015
Mon P Wang0c397192008-10-30 08:01:45 +00008016 while (LdWidth > 0) {
8017 unsigned Increment = EVTWidth / 8;
8018 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008019 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008020 DAG.getIntPtrConstant(Increment));
8021
8022 if (LdWidth < EVTWidth) {
8023 // Our current type we are using is too large, use a smaller size by
8024 // using a smaller power of 2
8025 unsigned oEVTWidth = EVTWidth;
8026 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8027 EVTWidth = EVT.getSizeInBits();
8028 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008029 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008030 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008031 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008032
Dale Johannesenc6be1102009-02-02 22:49:46 +00008033 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008034 SVOffset+Offset, isVolatile,
8035 MinAlign(Alignment, Offset));
Mon P Wang0c397192008-10-30 08:01:45 +00008036 LdChain.push_back(LdOp.getValue(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008037 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008038 DAG.getIntPtrConstant(Idx++));
Scott Michelfdc40a02009-02-17 22:15:04 +00008039
Mon P Wang0c397192008-10-30 08:01:45 +00008040 LdWidth -= EVTWidth;
8041 }
8042
Dale Johannesenc6be1102009-02-02 22:49:46 +00008043 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008044}
8045
8046bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8047 SDValue& TFOp,
8048 SDValue Op,
8049 MVT NVT) {
8050 // TODO: Add support for ConcatVec and the ability to load many vector
8051 // types (e.g., v4i8). This will not work when a vector register
8052 // to memory mapping is strange (e.g., vector elements are not
8053 // stored in some sequential order).
8054
Scott Michelfdc40a02009-02-17 22:15:04 +00008055 // It must be true that the widen vector type is bigger than where
Mon P Wang0c397192008-10-30 08:01:45 +00008056 // we need to load from.
8057 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8058 MVT LdVT = LD->getMemoryVT();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008059 DebugLoc dl = LD->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008060 assert(LdVT.isVector() && NVT.isVector());
8061 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
Scott Michelfdc40a02009-02-17 22:15:04 +00008062
Mon P Wang0c397192008-10-30 08:01:45 +00008063 // Load information
8064 SDValue Chain = LD->getChain();
8065 SDValue BasePtr = LD->getBasePtr();
8066 int SVOffset = LD->getSrcValueOffset();
8067 unsigned Alignment = LD->getAlignment();
8068 bool isVolatile = LD->isVolatile();
8069 const Value *SV = LD->getSrcValue();
8070 unsigned int LdWidth = LdVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00008071
Mon P Wang0c397192008-10-30 08:01:45 +00008072 // Load value as a large register
8073 SDValueVector LdChain;
8074 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008075 Alignment, isVolatile, LdWidth, NVT, dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008076
8077 if (LdChain.size() == 1) {
8078 TFOp = LdChain[0];
8079 return true;
8080 }
8081 else {
Scott Michelfdc40a02009-02-17 22:15:04 +00008082 TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008083 &LdChain[0], LdChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008084 return false;
8085 }
8086}
8087
8088
8089void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8090 SDValue Chain,
8091 SDValue BasePtr,
8092 const Value *SV,
8093 int SVOffset,
8094 unsigned Alignment,
8095 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008096 SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008097 unsigned StWidth,
8098 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008099 // Breaks the stores into a series of power of 2 width stores. For any
8100 // width, we convert the vector to the vector of element size that we
8101 // want to store. This avoids requiring a stack convert.
Scott Michelfdc40a02009-02-17 22:15:04 +00008102
Mon P Wang0c397192008-10-30 08:01:45 +00008103 // Find a width of the element type we can store with
8104 MVT VVT = ValOp.getValueType();
8105 MVT EVT, VecEVT;
8106 unsigned EVTWidth;
8107 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8108 EVTWidth = EVT.getSizeInBits();
8109
Dale Johannesenc6be1102009-02-02 22:49:46 +00008110 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
8111 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008112 DAG.getIntPtrConstant(0));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008113 SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008114 isVolatile, Alignment);
Mon P Wang0c397192008-10-30 08:01:45 +00008115 StChain.push_back(StOp);
8116
8117 // Check if we are done
8118 if (StWidth == EVTWidth) {
8119 return;
8120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008121
Mon P Wang0c397192008-10-30 08:01:45 +00008122 unsigned Idx = 1;
8123 StWidth -= EVTWidth;
8124 unsigned Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008125
Mon P Wang0c397192008-10-30 08:01:45 +00008126 while (StWidth > 0) {
8127 unsigned Increment = EVTWidth / 8;
8128 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008129 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008130 DAG.getIntPtrConstant(Increment));
Scott Michelfdc40a02009-02-17 22:15:04 +00008131
Mon P Wang0c397192008-10-30 08:01:45 +00008132 if (StWidth < EVTWidth) {
8133 // Our current type we are using is too large, use a smaller size by
8134 // using a smaller power of 2
8135 unsigned oEVTWidth = EVTWidth;
8136 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8137 EVTWidth = EVT.getSizeInBits();
8138 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008139 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008140 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008141 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008142
Dale Johannesenc6be1102009-02-02 22:49:46 +00008143 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008144 DAG.getIntPtrConstant(Idx++));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008145 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008146 SVOffset + Offset, isVolatile,
8147 MinAlign(Alignment, Offset)));
8148 StWidth -= EVTWidth;
8149 }
8150}
8151
8152
8153SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00008154 SDValue Chain,
8155 SDValue BasePtr) {
Mon P Wang0c397192008-10-30 08:01:45 +00008156 // TODO: It might be cleaner if we can use SplitVector and have more legal
8157 // vector types that can be stored into memory (e.g., v4xi8 can
8158 // be stored as a word). This will not work when a vector register
8159 // to memory mapping is strange (e.g., vector elements are not
8160 // stored in some sequential order).
Scott Michelfdc40a02009-02-17 22:15:04 +00008161
Mon P Wang0c397192008-10-30 08:01:45 +00008162 MVT StVT = ST->getMemoryVT();
8163 SDValue ValOp = ST->getValue();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008164 DebugLoc dl = ST->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008165
8166 // Check if we have widen this node with another value
8167 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8168 if (I != WidenNodes.end())
8169 ValOp = I->second;
Scott Michelfdc40a02009-02-17 22:15:04 +00008170
Mon P Wang0c397192008-10-30 08:01:45 +00008171 MVT VVT = ValOp.getValueType();
8172
8173 // It must be true that we the widen vector type is bigger than where
8174 // we need to store.
8175 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008176 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008177 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8178
8179 // Store value
8180 SDValueVector StChain;
8181 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8182 ST->getSrcValueOffset(), ST->getAlignment(),
Dale Johannesenc6be1102009-02-02 22:49:46 +00008183 ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008184 if (StChain.size() == 1)
8185 return StChain[0];
Scott Michelfdc40a02009-02-17 22:15:04 +00008186 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008187 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8188 &StChain[0], StChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008189}
8190
8191
Chris Lattner3e928bb2005-01-07 07:47:09 +00008192// SelectionDAG::Legalize - This is the entry point for the file.
8193//
Bill Wendling98a366d2009-04-29 23:29:43 +00008194void SelectionDAG::Legalize(bool TypesNeedLegalizing,
8195 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008196 /// run - This is the main entry point to this class.
8197 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00008198 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008199}
8200