blob: 18edb40239e3a872b061b77661f09291f123160b [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel5c6e1e32009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman544ab2c2008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Devang Patel5c6e1e32009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Chris Lattneref598052006-04-02 03:07:27 +000032#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000034#include "llvm/Support/MathExtras.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000035#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000036#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000038#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000039using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
43/// hacks on it until the target machine can handle it. This involves
44/// eliminating value sizes the machine cannot handle (promoting small sizes to
45/// large sizes or splitting up large values into small values) as well as
46/// eliminating operations the machine cannot handle.
47///
48/// This code also does a small amount of optimization and recognition of idioms
49/// as part of its processing. For example, if a target does not support a
50/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
51/// will attempt merge setcc and brc instructions into brcc's.
52///
53namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000054class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000055 TargetLowering &TLI;
56 SelectionDAG &DAG;
Duncan Sandsf312dc72008-12-14 09:43:15 +000057 bool TypesNeedLegalizing;
Chris Lattnerdc750592005-01-07 07:47:09 +000058
Chris Lattner462505f2006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000064 SDValue LastCALLSEQ_END;
Chris Lattner462505f2006-02-13 09:18:02 +000065
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattnerdc750592005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000075 };
Chris Lattner462505f2006-02-13 09:18:02 +000076
Chris Lattnerdc750592005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000081
Chris Lattnerdc750592005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000085 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000086
Chris Lattner1f2c9d82005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000091
Chris Lattner32206f52006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang58c37942008-10-30 08:01:45 +000093 /// which operands are the expanded version of the input. This allows
Chris Lattner32206f52006-03-18 01:44:44 +000094 /// us to avoid expanding the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000095 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000096
Chris Lattner32206f52006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang58c37942008-10-30 08:01:45 +000098 /// which operands are the split version of the input. This allows us
Chris Lattner32206f52006-03-18 01:44:44 +000099 /// to avoid splitting the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000100 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000101
Dan Gohmana8665142007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000105 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000106
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000107 /// WidenNodes - For nodes that need to be widened from one vector type to
108 /// another, this contains the mapping of those that we have already widen.
109 /// This allows us to avoid widening more than once.
Mon P Wang58c37942008-10-30 08:01:45 +0000110 std::map<SDValue, SDValue> WidenNodes;
111
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000112 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000113 LegalizedNodes.insert(std::make_pair(From, To));
114 // If someone requests legalization of the new node, return itself.
115 if (From != To)
116 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000117 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000118 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman38740a92008-07-07 17:46:23 +0000119 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 assert(isNew && "Got into the map somehow?");
Evan Chenga8fd1f22008-11-24 07:09:49 +0000121 isNew = isNew;
Chris Lattner2af3ee42005-12-20 00:53:54 +0000122 // If someone requests legalization of the new node, return itself.
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000124 }
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000125 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang58c37942008-10-30 08:01:45 +0000126 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
127 assert(isNew && "Got into the map somehow?");
Evan Chenga8fd1f22008-11-24 07:09:49 +0000128 isNew = isNew;
Mon P Wang58c37942008-10-30 08:01:45 +0000129 // If someone requests legalization of the new node, return itself.
130 LegalizedNodes.insert(std::make_pair(To, To));
131 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133public:
Duncan Sandsf312dc72008-12-14 09:43:15 +0000134 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Chris Lattnerdc750592005-01-07 07:47:09 +0000135
Chris Lattnerdc750592005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000139 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
Duncan Sands13237ac2008-06-06 12:08:01 +0000145 bool isTypeLegal(MVT VT) const {
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattnerdc750592005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000151private:
Dan Gohmana8665142007-06-25 16:23:39 +0000152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000153 /// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000154 void HandleOp(SDValue Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000159 SDValue LegalizeOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000160
Dan Gohman2a7de412007-10-11 23:57:53 +0000161 /// UnrollVectorOp - We know that the given vector has a legal type, however
162 /// the operation it performs is not legal and is an operation that we have
163 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
164 /// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000165 SDValue UnrollVectorOp(SDValue O);
Nate Begeman6f94f612008-04-25 18:07:40 +0000166
167 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
168 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
169 /// is necessary to spill the vector being inserted into to memory, perform
170 /// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000171 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
172 SDValue Idx);
Dan Gohman2a7de412007-10-11 23:57:53 +0000173
Chris Lattner32206f52006-03-18 01:44:44 +0000174 /// PromoteOp - Given an operation that produces a value in an invalid type,
175 /// promote it to compute the value into a larger type. The produced value
176 /// will have the correct bits for the low portion of the register, but no
177 /// guarantee is made about the top bits: it may be zero, sign-extended, or
178 /// garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000179 SDValue PromoteOp(SDValue O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000180
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000181 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner32206f52006-03-18 01:44:44 +0000182 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman3a293e72008-10-01 15:07:49 +0000183 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattner32206f52006-03-18 01:44:44 +0000184 /// the ExpandedNodes map is filled in for any results that are expanded, and
185 /// the Lo/Hi values are returned. This applies to integer types and Vector
186 /// types.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000187 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000188
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000189 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
190 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
191 /// for the existing elements but no guarantee is made about the new elements
192 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
193 /// when we have an instruction operating on an illegal vector type and we
194 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang58c37942008-10-30 08:01:45 +0000195 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
196
Dan Gohmana8665142007-06-25 16:23:39 +0000197 /// SplitVectorOp - Given an operand of vector type, break it down into
198 /// two smaller values.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000199 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000200
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000201 /// ScalarizeVectorOp - Given an operand of single-element vector type
202 /// (e.g. v1f32), convert it into the equivalent operation that returns a
203 /// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000204 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000205
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000206 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang58c37942008-10-30 08:01:45 +0000207 typedef SmallVector<SDValue, 16> SDValueVector;
208
209 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
210 /// the LdChain contains a single load and false if it contains a token
211 /// factor for multiple loads. It takes
212 /// Result: location to return the result
213 /// LdChain: location to return the load chain
214 /// Op: load operation to widen
215 /// NVT: widen vector result type we want for the load
216 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
217 SDValue Op, MVT NVT);
218
219 /// Helper genWidenVectorLoads - Helper function to generate a set of
220 /// loads to load a vector with a resulting wider type. It takes
221 /// LdChain: list of chains for the load we have generated
222 /// Chain: incoming chain for the ld vector
223 /// BasePtr: base pointer to load from
224 /// SV: memory disambiguation source value
225 /// SVOffset: memory disambiugation offset
226 /// Alignment: alignment of the memory
227 /// isVolatile: volatile load
228 /// LdWidth: width of memory that we want to load
229 /// ResType: the wider result result type for the resulting loaded vector
230 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
231 SDValue BasePtr, const Value *SV,
232 int SVOffset, unsigned Alignment,
233 bool isVolatile, unsigned LdWidth,
234 MVT ResType);
235
236 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
237 /// location. It takes
238 /// ST: store node that we want to replace
239 /// Chain: incoming store chain
240 /// BasePtr: base address of where we want to store into
241 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
242 SDValue BasePtr);
243
244 /// Helper genWidenVectorStores - Helper function to generate a set of
245 /// stores to store a widen vector into non widen memory
246 // It takes
247 // StChain: list of chains for the stores we have generated
248 // Chain: incoming chain for the ld vector
249 // BasePtr: base pointer to load from
250 // SV: memory disambiguation source value
251 // SVOffset: memory disambiugation offset
252 // Alignment: alignment of the memory
253 // isVolatile: volatile lod
254 // ValOp: value to store
255 // StWidth: width of memory that we want to store
256 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
257 SDValue BasePtr, const Value *SV,
258 int SVOffset, unsigned Alignment,
259 bool isVolatile, SDValue ValOp,
260 unsigned StWidth);
261
Duncan Sandsb0e39382008-07-21 10:20:31 +0000262 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner6be79822006-04-04 17:23:26 +0000263 /// specified mask and type. Targets can specify exactly which masks they
264 /// support and the code generator is tasked with not creating illegal masks.
265 ///
266 /// Note that this will also return true for shuffles that are promoted to a
267 /// different type.
268 ///
269 /// If this is a legal shuffle, this method returns the (possibly promoted)
270 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000271 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner6be79822006-04-04 17:23:26 +0000272
Chris Lattner4488f0c2006-07-26 23:55:56 +0000273 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000274 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000275
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000276 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng3b0f5e42008-10-15 02:05:31 +0000277 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
278 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
279 LegalizeSetCCOperands(LHS, RHS, CC);
280 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
281 }
Nate Begeman7e7f4392006-02-01 07:19:44 +0000282
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000283 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
284 SDValue &Hi);
285 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000287 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
288 SDValue ExpandBUILD_VECTOR(SDNode *Node);
289 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman550c9af2008-08-14 20:04:46 +0000290 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000291 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
292 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
293 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000294
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000295 SDValue ExpandBSWAP(SDValue Op);
296 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
297 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
298 SDValue &Lo, SDValue &Hi);
299 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
300 SDValue &Lo, SDValue &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000301
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000302 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
303 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wang4dd832d2008-12-09 05:46:39 +0000304
305 // Returns the legalized (truncated or extended) shift amount.
306 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattnerdc750592005-01-07 07:47:09 +0000307};
308}
309
Chris Lattner6be79822006-04-04 17:23:26 +0000310/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
311/// specified mask and type. Targets can specify exactly which masks they
312/// support and the code generator is tasked with not creating illegal masks.
313///
314/// Note that this will also return true for shuffles that are promoted to a
315/// different type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000316SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner6be79822006-04-04 17:23:26 +0000317 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
318 default: return 0;
319 case TargetLowering::Legal:
320 case TargetLowering::Custom:
321 break;
322 case TargetLowering::Promote: {
323 // If this is promoted to a different type, convert the shuffle mask and
324 // ask if it is legal in the promoted type!
Duncan Sands13237ac2008-06-06 12:08:01 +0000325 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsb0e39382008-07-21 10:20:31 +0000326 MVT EltVT = NVT.getVectorElementType();
Chris Lattner6be79822006-04-04 17:23:26 +0000327
328 // If we changed # elements, change the shuffle mask.
329 unsigned NumEltsGrowth =
Duncan Sands13237ac2008-06-06 12:08:01 +0000330 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner6be79822006-04-04 17:23:26 +0000331 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
332 if (NumEltsGrowth > 1) {
333 // Renumber the elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000334 SmallVector<SDValue, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000335 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000336 SDValue InOp = Mask.getOperand(i);
Chris Lattner6be79822006-04-04 17:23:26 +0000337 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
338 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsb0e39382008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000340 else {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000341 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsb0e39382008-07-21 10:20:31 +0000342 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000343 }
344 }
345 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000346 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000347 }
348 VT = NVT;
349 break;
350 }
351 }
Gabor Greiff304a7a2008-08-28 21:40:38 +0000352 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner6be79822006-04-04 17:23:26 +0000353}
354
Duncan Sandsf312dc72008-12-14 09:43:15 +0000355SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
356 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Chris Lattner4add7e32005-01-23 04:42:50 +0000357 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000358 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000359 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000360}
361
Chris Lattnerdc750592005-01-07 07:47:09 +0000362void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000363 LastCALLSEQ_END = DAG.getEntryNode();
364 IsLegalizingCall = false;
365
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000366 // The legalize process is inherently a bottom-up recursive process (users
367 // legalize their uses before themselves). Given infinite stack space, we
368 // could just start legalizing on the root and traverse the whole graph. In
369 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000370 // blocks. To avoid this problem, compute an ordering of the nodes where each
371 // node is only legalized after all of its operands are legalized.
Dan Gohman86aa16a2008-09-30 18:30:35 +0000372 DAG.AssignTopologicalOrder();
373 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
374 E = prior(DAG.allnodes_end()); I != next(E); ++I)
375 HandleOp(SDValue(I, 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000376
377 // Finally, it's possible the root changed. Get the new root.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000378 SDValue OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000379 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
380 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000381
382 ExpandedNodes.clear();
383 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000384 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000385 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000386 ScalarizedNodes.clear();
Mon P Wang58c37942008-10-30 08:01:45 +0000387 WidenNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000388
389 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000390 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000391}
392
Chris Lattner462505f2006-02-13 09:18:02 +0000393
394/// FindCallEndFromCallStart - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_END node that terminates the call sequence.
396static SDNode *FindCallEndFromCallStart(SDNode *Node) {
397 if (Node->getOpcode() == ISD::CALLSEQ_END)
398 return Node;
399 if (Node->use_empty())
400 return 0; // No CallSeqEnd
401
402 // The chain is usually at the end.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000403 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner462505f2006-02-13 09:18:02 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Sometimes it's at the beginning.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000406 TheChain = SDValue(Node, 0);
Chris Lattner462505f2006-02-13 09:18:02 +0000407 if (TheChain.getValueType() != MVT::Other) {
408 // Otherwise, hunt for it.
409 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
410 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000411 TheChain = SDValue(Node, i);
Chris Lattner462505f2006-02-13 09:18:02 +0000412 break;
413 }
414
415 // Otherwise, we walked into a node without a chain.
416 if (TheChain.getValueType() != MVT::Other)
417 return 0;
418 }
419 }
420
421 for (SDNode::use_iterator UI = Node->use_begin(),
422 E = Node->use_end(); UI != E; ++UI) {
423
424 // Make sure to only follow users of our token chain.
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000425 SDNode *User = *UI;
Chris Lattner462505f2006-02-13 09:18:02 +0000426 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
427 if (User->getOperand(i) == TheChain)
428 if (SDNode *Result = FindCallEndFromCallStart(User))
429 return Result;
430 }
431 return 0;
432}
433
434/// FindCallStartFromCallEnd - Given a chained node that is part of a call
435/// sequence, find the CALLSEQ_START node that initiates the call sequence.
436static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
437 assert(Node && "Didn't find callseq_start for a call??");
438 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
439
440 assert(Node->getOperand(0).getValueType() == MVT::Other &&
441 "Node doesn't have a token chain argument!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000442 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner462505f2006-02-13 09:18:02 +0000443}
444
445/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
446/// see if any uses can reach Dest. If no dest operands can get to dest,
447/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000448///
449/// Keep track of the nodes we fine that actually do lead to Dest in
450/// NodesLeadingTo. This avoids retraversing them exponential number of times.
451///
452bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000453 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000454 if (N == Dest) return true; // N certainly leads to Dest :)
455
Chris Lattner4488f0c2006-07-26 23:55:56 +0000456 // If we've already processed this node and it does lead to Dest, there is no
457 // need to reprocess it.
458 if (NodesLeadingTo.count(N)) return true;
459
Chris Lattner462505f2006-02-13 09:18:02 +0000460 // If the first result of this node has been already legalized, then it cannot
461 // reach N.
462 switch (getTypeAction(N->getValueType(0))) {
463 case Legal:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000464 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000465 break;
466 case Promote:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000467 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000468 break;
469 case Expand:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000470 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000471 break;
472 }
473
474 // Okay, this node has not already been legalized. Check and legalize all
475 // operands. If none lead to Dest, then we can legalize this node.
476 bool OperandsLeadToDest = false;
477 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
478 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000479 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000480
Chris Lattner4488f0c2006-07-26 23:55:56 +0000481 if (OperandsLeadToDest) {
482 NodesLeadingTo.insert(N);
483 return true;
484 }
Chris Lattner462505f2006-02-13 09:18:02 +0000485
486 // Okay, this node looks safe, legalize it and return false.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000487 HandleOp(SDValue(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000488 return false;
489}
490
Mon P Wang58c37942008-10-30 08:01:45 +0000491/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000492/// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000493void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000494 MVT VT = Op.getValueType();
Duncan Sandsf312dc72008-12-14 09:43:15 +0000495 // If the type legalizer was run then we should never see any illegal result
496 // types here except for target constants (the type legalizer does not touch
Mon P Wanga5016402008-12-18 20:03:17 +0000497 // those) or for build vector used as a mask for a vector shuffle.
498 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandsf312dc72008-12-14 09:43:15 +0000499 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wanga5016402008-12-18 20:03:17 +0000500 Op.getOpcode() == ISD::TargetConstant ||
501 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandsf312dc72008-12-14 09:43:15 +0000502 "Illegal type introduced after type legalization?");
Dan Gohmana8665142007-06-25 16:23:39 +0000503 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000504 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000505 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang58c37942008-10-30 08:01:45 +0000506 case Promote:
507 if (!VT.isVector()) {
508 (void)PromoteOp(Op);
509 break;
510 }
511 else {
512 // See if we can widen otherwise use Expand to either scalarize or split
513 MVT WidenVT = TLI.getWidenVectorType(VT);
514 if (WidenVT != MVT::Other) {
515 (void) WidenVectorOp(Op, WidenVT);
516 break;
517 }
518 // else fall thru to expand since we can't widen the vector
519 }
Chris Lattner32206f52006-03-18 01:44:44 +0000520 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +0000521 if (!VT.isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +0000522 // If this is an illegal scalar, expand it into its two component
523 // pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000524 SDValue X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000525 if (Op.getOpcode() == ISD::TargetConstant)
526 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000527 ExpandOp(Op, X, Y);
Duncan Sands13237ac2008-06-06 12:08:01 +0000528 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmana8665142007-06-25 16:23:39 +0000529 // If this is an illegal single element vector, convert it to a
530 // scalar operation.
531 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000532 } else {
Mon P Wang58c37942008-10-30 08:01:45 +0000533 // This is an illegal multiple element vector.
Dan Gohmana8665142007-06-25 16:23:39 +0000534 // Split it in half and legalize both parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000535 SDValue X, Y;
Dan Gohmana8665142007-06-25 16:23:39 +0000536 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000537 }
538 break;
539 }
540}
Chris Lattner462505f2006-02-13 09:18:02 +0000541
Evan Cheng22cf8992006-12-13 20:57:08 +0000542/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
543/// a load from the constant pool.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000544static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000545 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000546 bool Extend = false;
547
548 // If a FP immediate is precise when represented as a float and if the
549 // target can do an extending load from float to double, we put it into
550 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000551 // double. This shrinks FP constants and canonicalizes them for targets where
552 // an FP extending load is the same cost as a normal load (such as on the x87
553 // fp stack or PPC FP unit).
Duncan Sands13237ac2008-06-06 12:08:01 +0000554 MVT VT = CFP->getValueType(0);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000555 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000556 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000557 if (VT!=MVT::f64 && VT!=MVT::f32)
558 assert(0 && "Invalid type expansion");
Dale Johannesen54306fe2008-10-09 18:53:47 +0000559 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng38caf772008-03-04 08:05:30 +0000560 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000561 }
562
Duncan Sands13237ac2008-06-06 12:08:01 +0000563 MVT OrigVT = VT;
564 MVT SVT = VT;
Evan Cheng38caf772008-03-04 08:05:30 +0000565 while (SVT != MVT::f32) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000566 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng38caf772008-03-04 08:05:30 +0000567 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
568 // Only do this if the target has a native EXTLOAD instruction from
569 // smaller type.
Evan Cheng07d53b12008-10-14 21:26:46 +0000570 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000571 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000572 const Type *SType = SVT.getTypeForMVT();
Evan Cheng38caf772008-03-04 08:05:30 +0000573 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
574 VT = SVT;
575 Extend = true;
576 }
Evan Cheng47833a12006-12-12 21:32:44 +0000577 }
578
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000579 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000580 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng38caf772008-03-04 08:05:30 +0000581 if (Extend)
582 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000583 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000584 0, VT, false, Alignment);
Evan Cheng38caf772008-03-04 08:05:30 +0000585 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000586 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +0000587}
588
Chris Lattner462505f2006-02-13 09:18:02 +0000589
Evan Cheng003feb02007-01-04 21:56:39 +0000590/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
591/// operations.
592static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000593SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
594 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000595 MVT VT = Node->getValueType(0);
596 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000597 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
598 "fcopysign expansion only supported for f32 and f64");
Duncan Sands13237ac2008-06-06 12:08:01 +0000599 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000600
Evan Cheng003feb02007-01-04 21:56:39 +0000601 // First get the sign bit of second operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000602 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000603 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
604 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000605 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000606 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000607 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000608 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands13237ac2008-06-06 12:08:01 +0000609 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng003feb02007-01-04 21:56:39 +0000610 if (SizeDiff > 0) {
611 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
612 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
613 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattner87909d02008-07-10 23:46:13 +0000614 } else if (SizeDiff < 0) {
615 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
616 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
617 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
618 }
Evan Cheng3b841dd2007-01-05 21:31:51 +0000619
620 // Clear the sign bit of first operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000621 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng3b841dd2007-01-05 21:31:51 +0000622 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
623 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
624 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000625 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000626 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
627
628 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000629 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
630 return Result;
631}
632
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000633/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
634static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000635SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
636 TargetLowering &TLI) {
637 SDValue Chain = ST->getChain();
638 SDValue Ptr = ST->getBasePtr();
639 SDValue Val = ST->getValue();
Duncan Sands13237ac2008-06-06 12:08:01 +0000640 MVT VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000641 int Alignment = ST->getAlignment();
642 int SVOffset = ST->getSrcValueOffset();
Duncan Sands13237ac2008-06-06 12:08:01 +0000643 if (ST->getMemoryVT().isFloatingPoint() ||
644 ST->getMemoryVT().isVector()) {
Duncan Sands8f352fe2008-12-12 21:47:02 +0000645 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
646 if (TLI.isTypeLegal(intVT)) {
647 // Expand to a bitconvert of the value to the integer type of the
648 // same size, then a (misaligned) int store.
649 // FIXME: Does not handle truncating floating point stores!
650 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
651 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
652 SVOffset, ST->isVolatile(), Alignment);
653 } else {
654 // Do a (aligned) store to a stack slot, then copy from the stack slot
655 // to the final destination using (unaligned) integer loads and stores.
656 MVT StoredVT = ST->getMemoryVT();
657 MVT RegVT =
658 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
659 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
660 unsigned RegBytes = RegVT.getSizeInBits() / 8;
661 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000662
Duncan Sandsb6f09932008-12-13 07:18:38 +0000663 // Make sure the stack slot is also aligned for the register type.
664 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
665
666 // Perform the original store, only redirected to the stack slot.
Duncan Sands8f352fe2008-12-12 21:47:02 +0000667 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
668 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
669 SmallVector<SDValue, 8> Stores;
670 unsigned Offset = 0;
671
672 // Do all but one copies using the full register width.
673 for (unsigned i = 1; i < NumRegs; i++) {
674 // Load one integer register's worth from the stack slot.
675 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
676 // Store it to the final location. Remember the store.
677 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
678 ST->getSrcValue(), SVOffset + Offset,
679 ST->isVolatile(),
680 MinAlign(ST->getAlignment(), Offset)));
681 // Increment the pointers.
682 Offset += RegBytes;
683 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
684 Increment);
685 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
686 }
687
Duncan Sandsb6f09932008-12-13 07:18:38 +0000688 // The last store may be partial. Do a truncating store. On big-endian
689 // machines this requires an extending load from the stack slot to ensure
690 // that the bits are in the right place.
691 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands8f352fe2008-12-12 21:47:02 +0000692
Duncan Sandsb6f09932008-12-13 07:18:38 +0000693 // Load from the stack slot.
694 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
695 NULL, 0, MemVT);
696
Duncan Sands8f352fe2008-12-12 21:47:02 +0000697 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
698 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsb6f09932008-12-13 07:18:38 +0000699 MemVT, ST->isVolatile(),
Duncan Sands8f352fe2008-12-12 21:47:02 +0000700 MinAlign(ST->getAlignment(), Offset)));
701 // The order of the stores doesn't matter - say it with a TokenFactor.
702 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
703 Stores.size());
704 }
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000705 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000706 assert(ST->getMemoryVT().isInteger() &&
707 !ST->getMemoryVT().isVector() &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000708 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000709 // Get the half-size VT
Duncan Sands13237ac2008-06-06 12:08:01 +0000710 MVT NewStoredVT =
711 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
712 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000713 int IncrementSize = NumBits / 8;
714
715 // Divide the stored value in two parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000716 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
717 SDValue Lo = Val;
718 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000719
720 // Store the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000721 SDValue Store1, Store2;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000722 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
723 ST->getSrcValue(), SVOffset, NewStoredVT,
724 ST->isVolatile(), Alignment);
725 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
726 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000727 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000728 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
729 ST->getSrcValue(), SVOffset + IncrementSize,
730 NewStoredVT, ST->isVolatile(), Alignment);
731
732 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
733}
734
735/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
736static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000737SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
738 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000739 int SVOffset = LD->getSrcValueOffset();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000740 SDValue Chain = LD->getChain();
741 SDValue Ptr = LD->getBasePtr();
Duncan Sands13237ac2008-06-06 12:08:01 +0000742 MVT VT = LD->getValueType(0);
743 MVT LoadedVT = LD->getMemoryVT();
744 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands8f352fe2008-12-12 21:47:02 +0000745 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
746 if (TLI.isTypeLegal(intVT)) {
747 // Expand to a (misaligned) integer load of the same size,
748 // then bitconvert to floating point or vector.
749 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
750 SVOffset, LD->isVolatile(),
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000751 LD->getAlignment());
Duncan Sands8f352fe2008-12-12 21:47:02 +0000752 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
753 if (VT.isFloatingPoint() && LoadedVT != VT)
754 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000755
Duncan Sands8f352fe2008-12-12 21:47:02 +0000756 SDValue Ops[] = { Result, Chain };
757 return DAG.getMergeValues(Ops, 2);
758 } else {
759 // Copy the value to a (aligned) stack slot using (unaligned) integer
760 // loads and stores, then do a (aligned) load from the stack slot.
761 MVT RegVT = TLI.getRegisterType(intVT);
762 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
763 unsigned RegBytes = RegVT.getSizeInBits() / 8;
764 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
765
Duncan Sandsb6f09932008-12-13 07:18:38 +0000766 // Make sure the stack slot is also aligned for the register type.
767 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
768
Duncan Sands8f352fe2008-12-12 21:47:02 +0000769 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
770 SmallVector<SDValue, 8> Stores;
771 SDValue StackPtr = StackBase;
772 unsigned Offset = 0;
773
774 // Do all but one copies using the full register width.
775 for (unsigned i = 1; i < NumRegs; i++) {
776 // Load one integer register's worth from the original location.
777 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
778 SVOffset + Offset, LD->isVolatile(),
779 MinAlign(LD->getAlignment(), Offset));
780 // Follow the load with a store to the stack slot. Remember the store.
781 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
782 NULL, 0));
783 // Increment the pointers.
784 Offset += RegBytes;
785 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
786 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
787 Increment);
788 }
789
790 // The last copy may be partial. Do an extending load.
Duncan Sandsb6f09932008-12-13 07:18:38 +0000791 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sands8f352fe2008-12-12 21:47:02 +0000792 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
793 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsb6f09932008-12-13 07:18:38 +0000794 MemVT, LD->isVolatile(),
Duncan Sands8f352fe2008-12-12 21:47:02 +0000795 MinAlign(LD->getAlignment(), Offset));
796 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsb6f09932008-12-13 07:18:38 +0000797 // On big-endian machines this requires a truncating store to ensure
798 // that the bits end up in the right place.
799 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
800 NULL, 0, MemVT));
Duncan Sands8f352fe2008-12-12 21:47:02 +0000801
802 // The order of the stores doesn't matter - say it with a TokenFactor.
803 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
804 Stores.size());
805
806 // Finally, perform the original load only redirected to the stack slot.
807 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
808 NULL, 0, LoadedVT);
809
810 // Callers expect a MERGE_VALUES node.
811 SDValue Ops[] = { Load, TF };
812 return DAG.getMergeValues(Ops, 2);
813 }
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000814 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000815 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner09c03932007-11-19 21:38:03 +0000816 "Unaligned load of unsupported type.");
817
Dale Johannesenbf76a082008-02-27 22:36:00 +0000818 // Compute the new VT that is half the size of the old one. This is an
819 // integer MVT.
Duncan Sands13237ac2008-06-06 12:08:01 +0000820 unsigned NumBits = LoadedVT.getSizeInBits();
821 MVT NewLoadedVT;
822 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000823 NumBits >>= 1;
824
825 unsigned Alignment = LD->getAlignment();
826 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000827 ISD::LoadExtType HiExtType = LD->getExtensionType();
828
829 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
830 if (HiExtType == ISD::NON_EXTLOAD)
831 HiExtType = ISD::ZEXTLOAD;
832
833 // Load the value in two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000834 SDValue Lo, Hi;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000835 if (TLI.isLittleEndian()) {
836 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
837 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
838 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
839 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
840 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
841 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000842 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000843 } else {
844 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
845 NewLoadedVT,LD->isVolatile(), Alignment);
846 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
847 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
848 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
849 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000850 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000851 }
852
853 // aggregate the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000854 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
855 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000856 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
857
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000858 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000859 Hi.getValue(1));
860
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000861 SDValue Ops[] = { Result, TF };
Duncan Sands739a0542008-07-02 17:40:58 +0000862 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000863}
Evan Cheng003feb02007-01-04 21:56:39 +0000864
Dan Gohman2a7de412007-10-11 23:57:53 +0000865/// UnrollVectorOp - We know that the given vector has a legal type, however
866/// the operation it performs is not legal and is an operation that we have
867/// no way of lowering. "Unroll" the vector, splitting out the scalars and
868/// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000869SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000870 MVT VT = Op.getValueType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000871 assert(isTypeLegal(VT) &&
872 "Caller should expand or promote operands that are not legal!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000873 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman2a7de412007-10-11 23:57:53 +0000874 "Can't unroll a vector with multiple results!");
Duncan Sands13237ac2008-06-06 12:08:01 +0000875 unsigned NE = VT.getVectorNumElements();
876 MVT EltVT = VT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000877
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000878 SmallVector<SDValue, 8> Scalars;
879 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman2a7de412007-10-11 23:57:53 +0000880 for (unsigned i = 0; i != NE; ++i) {
881 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000882 SDValue Operand = Op.getOperand(j);
Duncan Sands13237ac2008-06-06 12:08:01 +0000883 MVT OperandVT = Operand.getValueType();
884 if (OperandVT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +0000885 // A vector operand; extract a single element.
Duncan Sands13237ac2008-06-06 12:08:01 +0000886 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000887 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
888 OperandEltVT,
889 Operand,
890 DAG.getConstant(i, MVT::i32));
891 } else {
892 // A scalar operand; just use it as is.
893 Operands[j] = Operand;
894 }
895 }
Mon P Wang4dd832d2008-12-09 05:46:39 +0000896
897 switch (Op.getOpcode()) {
898 default:
899 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
900 &Operands[0], Operands.size()));
901 break;
902 case ISD::SHL:
903 case ISD::SRA:
904 case ISD::SRL:
905 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
906 LegalizeShiftAmount(Operands[1])));
907 break;
908 }
Dan Gohman2a7de412007-10-11 23:57:53 +0000909 }
910
911 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
912}
913
Duncan Sands53c954f2008-01-10 10:28:30 +0000914/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000915static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands53c954f2008-01-10 10:28:30 +0000916 RTLIB::Libcall Call_F32,
917 RTLIB::Libcall Call_F64,
918 RTLIB::Libcall Call_F80,
919 RTLIB::Libcall Call_PPCF128) {
920 return
921 VT == MVT::f32 ? Call_F32 :
922 VT == MVT::f64 ? Call_F64 :
923 VT == MVT::f80 ? Call_F80 :
924 VT == MVT::ppcf128 ? Call_PPCF128 :
925 RTLIB::UNKNOWN_LIBCALL;
926}
927
Nate Begeman6f94f612008-04-25 18:07:40 +0000928/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
929/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
930/// is necessary to spill the vector being inserted into to memory, perform
931/// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000932SDValue SelectionDAGLegalize::
933PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
934 SDValue Tmp1 = Vec;
935 SDValue Tmp2 = Val;
936 SDValue Tmp3 = Idx;
Nate Begeman6f94f612008-04-25 18:07:40 +0000937
938 // If the target doesn't support this, we have to spill the input vector
939 // to a temporary stack slot, update the element, then reload it. This is
940 // badness. We could also load the value into a vector register (either
941 // with a "move to register" or "extload into register" instruction, then
942 // permute it into place, if the idx is a constant and if the idx is
943 // supported by the target.
Duncan Sands13237ac2008-06-06 12:08:01 +0000944 MVT VT = Tmp1.getValueType();
945 MVT EltVT = VT.getVectorElementType();
946 MVT IdxVT = Tmp3.getValueType();
947 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000948 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000949
Gabor Greiff304a7a2008-08-28 21:40:38 +0000950 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman6f94f612008-04-25 18:07:40 +0000951
952 // Store the vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000953 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang58c37942008-10-30 08:01:45 +0000954 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000955
956 // Truncate or zero extend offset to target pointer type.
Duncan Sands11dd4242008-06-08 20:54:56 +0000957 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman6f94f612008-04-25 18:07:40 +0000958 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
959 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +0000960 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman6f94f612008-04-25 18:07:40 +0000961 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000962 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman6f94f612008-04-25 18:07:40 +0000963 // Store the scalar value.
964 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000965 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000966 // Load the updated vector.
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000967 return DAG.getLoad(VT, Ch, StackPtr,
968 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000969}
970
Mon P Wang4dd832d2008-12-09 05:46:39 +0000971SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
972 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
973 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
974
975 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
976 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
977
978 return ShiftAmt;
979}
980
981
Dan Gohmanff727882007-07-13 20:14:11 +0000982/// LegalizeOp - We know that the specified value has a legal type, and
983/// that its operands are legal. Now ensure that the operation itself
984/// is legal, recursively ensuring that the operands' operations remain
985/// legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000986SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000987 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
988 return Op;
989
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000990 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000991 "Caller should expand or promote operands that are not legal!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000992 SDNode *Node = Op.getNode();
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000993
Chris Lattnerdc750592005-01-07 07:47:09 +0000994 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000995 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000996 if (Node->getNumValues() > 1) {
997 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000998 if (getTypeAction(Node->getValueType(i)) != Legal) {
999 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +00001000 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +00001001 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001002 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +00001003 }
1004 }
1005
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001006 // Note that LegalizeOp may be reentered even from single-use nodes, which
1007 // means that we always must cache transformed nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001008 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +00001009 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +00001010
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001011 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1012 SDValue Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001013 bool isCustom = false;
1014
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +00001016 case ISD::FrameIndex:
1017 case ISD::EntryToken:
1018 case ISD::Register:
1019 case ISD::BasicBlock:
1020 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001021 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +00001022 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +00001023 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +00001024 case ISD::TargetConstantPool:
1025 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001026 case ISD::TargetGlobalTLSAddress:
Bill Wendling24c79f22008-09-16 21:48:12 +00001027 case ISD::TargetExternalSymbol:
Chris Lattnereb637512006-01-28 08:31:04 +00001028 case ISD::VALUETYPE:
1029 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +00001030 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +00001031 case ISD::CONDCODE:
Duncan Sandsd97eea32008-03-21 09:14:45 +00001032 case ISD::ARG_FLAGS:
Chris Lattnereb637512006-01-28 08:31:04 +00001033 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +00001034 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +00001035 "This must be legal!");
1036 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001037 default:
Chris Lattner3eb86932005-05-14 06:34:48 +00001038 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1039 // If this is a target node, legalize it by legalizing the operands then
1040 // passing it through.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001041 SmallVector<SDValue, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +00001042 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +00001043 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +00001044
Chris Lattner97af9d52006-08-08 01:09:31 +00001045 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +00001046
1047 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1048 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +00001049 return Result.getValue(Op.getResNo());
Chris Lattner3eb86932005-05-14 06:34:48 +00001050 }
1051 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001052#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00001053 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001054#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00001055 assert(0 && "Do not know how to legalize this operator!");
1056 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +00001057 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +00001058 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001059 case ISD::GlobalTLSAddress:
Bill Wendling24c79f22008-09-16 21:48:12 +00001060 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001061 case ISD::ConstantPool:
1062 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +00001063 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1064 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +00001065 case TargetLowering::Custom:
1066 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001067 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattnereb637512006-01-28 08:31:04 +00001068 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +00001069 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +00001070 break;
1071 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001072 break;
Nate Begemaneda59972007-01-29 22:58:52 +00001073 case ISD::FRAMEADDR:
1074 case ISD::RETURNADDR:
1075 // The only option for these nodes is to custom lower them. If the target
1076 // does not custom lower them, then return zero.
1077 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001078 if (Tmp1.getNode())
Nate Begemaneda59972007-01-29 22:58:52 +00001079 Result = Tmp1;
1080 else
1081 Result = DAG.getConstant(0, TLI.getPointerTy());
1082 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +00001083 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001084 MVT VT = Node->getValueType(0);
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +00001085 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1086 default: assert(0 && "This action is not supported yet!");
1087 case TargetLowering::Custom:
1088 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001089 if (Result.getNode()) break;
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +00001090 // Fall Thru
1091 case TargetLowering::Legal:
1092 Result = DAG.getConstant(0, VT);
1093 break;
1094 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +00001095 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +00001096 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +00001097 case ISD::EXCEPTIONADDR: {
1098 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00001099 MVT VT = Node->getValueType(0);
Jim Laskey7f5872c2007-02-22 15:37:19 +00001100 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1101 default: assert(0 && "This action is not supported yet!");
1102 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +00001103 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +00001104 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +00001105 }
1106 break;
1107 case TargetLowering::Custom:
1108 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001109 if (Result.getNode()) break;
Jim Laskey7f5872c2007-02-22 15:37:19 +00001110 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +00001111 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001112 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands739a0542008-07-02 17:40:58 +00001113 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +00001114 break;
1115 }
1116 }
Chris Lattner1cbe2082007-04-27 17:12:52 +00001117 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001118 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sands57a60f02007-12-31 18:35:50 +00001119
Gabor Greiff304a7a2008-08-28 21:40:38 +00001120 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sands57a60f02007-12-31 18:35:50 +00001121 "Cannot return more than two values!");
1122
1123 // Since we produced two values, make sure to remember that we
1124 // legalized both of them.
1125 Tmp1 = LegalizeOp(Result);
1126 Tmp2 = LegalizeOp(Result.getValue(1));
1127 AddLegalizedOperand(Op.getValue(0), Tmp1);
1128 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001129 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +00001130 case ISD::EHSELECTION: {
1131 Tmp1 = LegalizeOp(Node->getOperand(0));
1132 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00001133 MVT VT = Node->getValueType(0);
Jim Laskey644af6b2007-02-28 20:43:58 +00001134 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1135 default: assert(0 && "This action is not supported yet!");
1136 case TargetLowering::Expand: {
1137 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +00001138 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +00001139 }
1140 break;
1141 case TargetLowering::Custom:
1142 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001143 if (Result.getNode()) break;
Jim Laskey644af6b2007-02-28 20:43:58 +00001144 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +00001145 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001146 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands739a0542008-07-02 17:40:58 +00001147 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +00001148 break;
1149 }
1150 }
Chris Lattner1cbe2082007-04-27 17:12:52 +00001151 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001152 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sands57a60f02007-12-31 18:35:50 +00001153
Gabor Greiff304a7a2008-08-28 21:40:38 +00001154 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sands57a60f02007-12-31 18:35:50 +00001155 "Cannot return more than two values!");
1156
1157 // Since we produced two values, make sure to remember that we
1158 // legalized both of them.
1159 Tmp1 = LegalizeOp(Result);
1160 Tmp2 = LegalizeOp(Result.getValue(1));
1161 AddLegalizedOperand(Op.getValue(0), Tmp1);
1162 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001163 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +00001164 case ISD::EH_RETURN: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001165 MVT VT = Node->getValueType(0);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001166 // The only "good" option for this node is to custom lower it.
1167 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1168 default: assert(0 && "This action is not supported at all!");
1169 case TargetLowering::Custom:
1170 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001171 if (Result.getNode()) break;
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001172 // Fall Thru
1173 case TargetLowering::Legal:
1174 // Target does not know, how to lower this, lower to noop
1175 Result = LegalizeOp(Node->getOperand(0));
1176 break;
1177 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +00001178 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001179 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +00001180 case ISD::AssertSext:
1181 case ISD::AssertZext:
1182 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +00001184 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00001185 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001186 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greifabfdf922008-08-26 22:36:50 +00001187 Result = Node->getOperand(Op.getResNo());
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001188 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +00001189 case ISD::CopyFromReg:
1190 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001191 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001192 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001194 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001195 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001196 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001197 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1199 } else {
1200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1201 }
Chris Lattnere3c67e92005-12-18 15:27:43 +00001202 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1203 }
Chris Lattnereb6614d2005-01-28 06:27:38 +00001204 // Since CopyFromReg produces two values, make sure to remember that we
1205 // legalized both of them.
1206 AddLegalizedOperand(Op.getValue(0), Result);
1207 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001208 return Result.getValue(Op.getResNo());
Nate Begemancda9aa72005-04-01 22:34:39 +00001209 case ISD::UNDEF: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001210 MVT VT = Op.getValueType();
Nate Begemancda9aa72005-04-01 22:34:39 +00001211 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +00001212 default: assert(0 && "This action is not supported yet!");
1213 case TargetLowering::Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00001214 if (VT.isInteger())
Nate Begemancda9aa72005-04-01 22:34:39 +00001215 Result = DAG.getConstant(0, VT);
Duncan Sands13237ac2008-06-06 12:08:01 +00001216 else if (VT.isFloatingPoint())
1217 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf04d37d2007-09-26 17:26:49 +00001218 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00001219 else
1220 assert(0 && "Unknown value type!");
1221 break;
Nate Begeman69d39432005-04-02 00:41:14 +00001222 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +00001223 break;
1224 }
1225 break;
1226 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001227
Chris Lattnere55d1712006-03-28 00:40:33 +00001228 case ISD::INTRINSIC_W_CHAIN:
1229 case ISD::INTRINSIC_WO_CHAIN:
1230 case ISD::INTRINSIC_VOID: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 SmallVector<SDValue, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001232 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1233 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001234 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001235
1236 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001237 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001238 TargetLowering::Custom) {
1239 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001240 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001241 }
1242
Gabor Greiff304a7a2008-08-28 21:40:38 +00001243 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001244
1245 // Must have return value and chain result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001246 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001247 "Cannot return more than two values!");
1248
1249 // Since loads produce two values, make sure to remember that we
1250 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001251 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1252 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001253 return Result.getValue(Op.getResNo());
Chris Lattnera4f68052006-03-24 02:26:29 +00001254 }
Chris Lattner435b4022005-11-29 06:21:05 +00001255
Dan Gohman5c73a882008-06-30 20:59:49 +00001256 case ISD::DBG_STOPPOINT:
1257 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner435b4022005-11-29 06:21:05 +00001258 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1259
Dan Gohman5c73a882008-06-30 20:59:49 +00001260 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner435b4022005-11-29 06:21:05 +00001261 case TargetLowering::Promote:
1262 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001263 case TargetLowering::Expand: {
Devang Patel5c6e1e32009-01-13 00:35:13 +00001264 DwarfWriter *DW = DAG.getDwarfWriter();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001265 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001266 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001267
Dan Gohman5c73a882008-06-30 20:59:49 +00001268 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patel5c6e1e32009-01-13 00:35:13 +00001269 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1270 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1271 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1272 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1273 CU.getFilename());
1274
Dan Gohman5c73a882008-06-30 20:59:49 +00001275 unsigned Line = DSP->getLine();
1276 unsigned Col = DSP->getColumn();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001277
1278 if (useDEBUG_LOC) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001279 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng34ef1db2008-07-08 20:06:39 +00001280 DAG.getConstant(Col, MVT::i32),
1281 DAG.getConstant(SrcFile, MVT::i32) };
1282 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001283 } else {
Devang Patel5c6e1e32009-01-13 00:35:13 +00001284 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001285 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001286 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001287 } else {
1288 Result = Tmp1; // chain
1289 }
Chris Lattner435b4022005-11-29 06:21:05 +00001290 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001291 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001292 case TargetLowering::Legal: {
1293 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1294 if (Action == Legal && Tmp1 == Node->getOperand(0))
1295 break;
1296
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001297 SmallVector<SDValue, 8> Ops;
Evan Cheng34ef1db2008-07-08 20:06:39 +00001298 Ops.push_back(Tmp1);
1299 if (Action == Legal) {
1300 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1301 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1302 } else {
1303 // Otherwise promote them.
1304 Ops.push_back(PromoteOp(Node->getOperand(1)));
1305 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner435b4022005-11-29 06:21:05 +00001306 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001307 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1308 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1309 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001310 break;
1311 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001312 }
Chris Lattner435b4022005-11-29 06:21:05 +00001313 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001314
1315 case ISD::DECLARE:
1316 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1317 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1318 default: assert(0 && "This action is not supported yet!");
1319 case TargetLowering::Legal:
1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1321 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1322 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1324 break;
Chris Lattner9824ffe2008-02-28 05:53:40 +00001325 case TargetLowering::Expand:
1326 Result = LegalizeOp(Node->getOperand(0));
1327 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001328 }
1329 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001330
1331 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001332 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001333 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001334 default: assert(0 && "This action is not supported yet!");
Evan Cheng34ef1db2008-07-08 20:06:39 +00001335 case TargetLowering::Legal: {
1336 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng34ef1db2008-07-08 20:06:39 +00001338 if (Action == Legal && Tmp1 == Node->getOperand(0))
1339 break;
1340 if (Action == Legal) {
1341 Tmp2 = Node->getOperand(1);
1342 Tmp3 = Node->getOperand(2);
1343 Tmp4 = Node->getOperand(3);
1344 } else {
1345 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1346 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1347 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1348 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001350 break;
1351 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001352 }
Jim Laskey762e9ec2006-01-05 01:25:28 +00001353 break;
1354
Dan Gohmanfb19f942008-07-01 00:05:16 +00001355 case ISD::DBG_LABEL:
1356 case ISD::EH_LABEL:
1357 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1358 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001359 default: assert(0 && "This action is not supported yet!");
1360 case TargetLowering::Legal:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfb19f942008-07-01 00:05:16 +00001362 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskey7c462762005-12-16 22:45:29 +00001363 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001364 case TargetLowering::Expand:
1365 Result = LegalizeOp(Node->getOperand(0));
1366 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001367 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001368 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001369
Evan Cheng95cf6612008-03-08 00:58:38 +00001370 case ISD::PREFETCH:
1371 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1372 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1373 default: assert(0 && "This action is not supported yet!");
1374 case TargetLowering::Legal:
1375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1377 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1378 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1380 break;
1381 case TargetLowering::Expand:
1382 // It's a noop.
1383 Result = LegalizeOp(Node->getOperand(0));
1384 break;
1385 }
1386 break;
1387
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001388 case ISD::MEMBARRIER: {
1389 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001390 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1391 default: assert(0 && "This action is not supported yet!");
1392 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001393 SDValue Ops[6];
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001394 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001395 for (int x = 1; x < 6; ++x) {
1396 Ops[x] = Node->getOperand(x);
1397 if (!isTypeLegal(Ops[x].getValueType()))
1398 Ops[x] = PromoteOp(Ops[x]);
1399 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001400 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1401 break;
1402 }
1403 case TargetLowering::Expand:
1404 //There is no libgcc call for this op
1405 Result = Node->getOperand(0); // Noop
1406 break;
1407 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001408 break;
1409 }
1410
Dan Gohman12f24902008-12-23 21:37:04 +00001411 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang3e583932008-05-05 19:05:59 +00001412 unsigned int num_operands = 4;
1413 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001414 SDValue Ops[4];
Mon P Wang3e583932008-05-05 19:05:59 +00001415 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharthd032c332008-03-01 21:52:34 +00001416 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang3e583932008-05-05 19:05:59 +00001417 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1418
1419 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1420 default: assert(0 && "This action is not supported yet!");
1421 case TargetLowering::Custom:
1422 Result = TLI.LowerOperation(Result, DAG);
1423 break;
1424 case TargetLowering::Legal:
1425 break;
1426 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001427 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1428 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001429 return Result.getValue(Op.getResNo());
Duncan Sands93e180342008-07-04 11:47:58 +00001430 }
Dan Gohman12f24902008-12-23 21:37:04 +00001431 case ISD::ATOMIC_LOAD_ADD:
1432 case ISD::ATOMIC_LOAD_SUB:
1433 case ISD::ATOMIC_LOAD_AND:
1434 case ISD::ATOMIC_LOAD_OR:
1435 case ISD::ATOMIC_LOAD_XOR:
1436 case ISD::ATOMIC_LOAD_NAND:
1437 case ISD::ATOMIC_LOAD_MIN:
1438 case ISD::ATOMIC_LOAD_MAX:
1439 case ISD::ATOMIC_LOAD_UMIN:
1440 case ISD::ATOMIC_LOAD_UMAX:
1441 case ISD::ATOMIC_SWAP: {
Mon P Wang3e583932008-05-05 19:05:59 +00001442 unsigned int num_operands = 3;
1443 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001444 SDValue Ops[3];
Mon P Wang3e583932008-05-05 19:05:59 +00001445 for (unsigned int x = 0; x < num_operands; ++x)
1446 Ops[x] = LegalizeOp(Node->getOperand(x));
1447 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands93e180342008-07-04 11:47:58 +00001448
Andrew Lenharthd032c332008-03-01 21:52:34 +00001449 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth95528942008-02-21 06:45:13 +00001450 default: assert(0 && "This action is not supported yet!");
Andrew Lenharthd032c332008-03-01 21:52:34 +00001451 case TargetLowering::Custom:
1452 Result = TLI.LowerOperation(Result, DAG);
1453 break;
1454 case TargetLowering::Legal:
Andrew Lenharth95528942008-02-21 06:45:13 +00001455 break;
1456 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001457 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1458 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001459 return Result.getValue(Op.getResNo());
Duncan Sands93e180342008-07-04 11:47:58 +00001460 }
Scott Michel9d09c5c2007-08-08 23:23:31 +00001461 case ISD::Constant: {
1462 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1463 unsigned opAction =
1464 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1465
Chris Lattnerdc750592005-01-07 07:47:09 +00001466 // We know we don't need to expand constants here, constants only have one
1467 // value and we check that it is fine above.
1468
Scott Michel9d09c5c2007-08-08 23:23:31 +00001469 if (opAction == TargetLowering::Custom) {
1470 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001471 if (Tmp1.getNode())
Scott Michel9d09c5c2007-08-08 23:23:31 +00001472 Result = Tmp1;
1473 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001474 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001475 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001476 case ISD::ConstantFP: {
1477 // Spill FP immediates to the constant pool if the target cannot directly
1478 // codegen them. Targets often have some immediate values that can be
1479 // efficiently generated into an FP register without a load. We explicitly
1480 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001481 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1482
Chris Lattner758b0ac2006-01-29 06:26:56 +00001483 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1484 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001485 case TargetLowering::Legal:
1486 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001487 case TargetLowering::Custom:
1488 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001489 if (Tmp3.getNode()) {
Chris Lattner758b0ac2006-01-29 06:26:56 +00001490 Result = Tmp3;
1491 break;
1492 }
1493 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001494 case TargetLowering::Expand: {
1495 // Check to see if this FP immediate is already legal.
1496 bool isLegal = false;
1497 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1498 E = TLI.legal_fpimm_end(); I != E; ++I) {
1499 if (CFP->isExactlyValue(*I)) {
1500 isLegal = true;
1501 break;
1502 }
1503 }
1504 // If this is a legal constant, turn it into a TargetConstantFP node.
1505 if (isLegal)
1506 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001507 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001508 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001509 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001510 break;
1511 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001512 case ISD::TokenFactor:
1513 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001514 Tmp1 = LegalizeOp(Node->getOperand(0));
1515 Tmp2 = LegalizeOp(Node->getOperand(1));
1516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1517 } else if (Node->getNumOperands() == 3) {
1518 Tmp1 = LegalizeOp(Node->getOperand(0));
1519 Tmp2 = LegalizeOp(Node->getOperand(1));
1520 Tmp3 = LegalizeOp(Node->getOperand(2));
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001522 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001523 SmallVector<SDValue, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001524 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001525 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1526 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001527 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001528 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001529 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001530
1531 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001532 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001533 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001534 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001535 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001536 // A call within a calling sequence must be legalized to something
1537 // other than the normal CALLSEQ_END. Violating this gets Legalize
1538 // into an infinite loop.
1539 assert ((!IsLegalizingCall ||
1540 Node->getOpcode() != ISD::CALL ||
Gabor Greiff304a7a2008-08-28 21:40:38 +00001541 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001542 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001543
1544 // The number of incoming and outgoing values should match; unless the final
1545 // outgoing value is a flag.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001546 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1547 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1548 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendlingf359fed2007-11-13 00:44:25 +00001549 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001550 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001551
Chris Lattneraaa23d92006-05-16 22:53:20 +00001552 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001553 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001554 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1555 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendlingf359fed2007-11-13 00:44:25 +00001556 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001557 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +00001558 if (Op.getResNo() == i)
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001559 Tmp2 = Tmp1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001560 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001561 }
1562 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001563 case ISD::EXTRACT_SUBREG: {
1564 Tmp1 = LegalizeOp(Node->getOperand(0));
1565 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1566 assert(idx && "Operand must be a constant");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001567 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1569 }
1570 break;
1571 case ISD::INSERT_SUBREG: {
1572 Tmp1 = LegalizeOp(Node->getOperand(0));
1573 Tmp2 = LegalizeOp(Node->getOperand(1));
1574 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1575 assert(idx && "Operand must be a constant");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001576 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1578 }
1579 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001580 case ISD::BUILD_VECTOR:
1581 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001582 default: assert(0 && "This action is not supported yet!");
1583 case TargetLowering::Custom:
1584 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001585 if (Tmp3.getNode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00001586 Result = Tmp3;
1587 break;
1588 }
1589 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001590 case TargetLowering::Expand:
Gabor Greiff304a7a2008-08-28 21:40:38 +00001591 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001592 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001593 }
Chris Lattner29b23012006-03-19 01:17:20 +00001594 break;
1595 case ISD::INSERT_VECTOR_ELT:
1596 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001597 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001598
1599 // The type of the value to insert may not be legal, even though the vector
1600 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1601 // here.
1602 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1603 default: assert(0 && "Cannot expand insert element operand");
1604 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1605 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang58c37942008-10-30 08:01:45 +00001606 case Expand:
1607 // FIXME: An alternative would be to check to see if the target is not
1608 // going to custom lower this operation, we could bitcast to half elt
1609 // width and perform two inserts at that width, if that is legal.
1610 Tmp2 = Node->getOperand(1);
1611 break;
Nate Begeman735ab3c2008-02-13 06:43:04 +00001612 }
Chris Lattner29b23012006-03-19 01:17:20 +00001613 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1614
1615 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1616 Node->getValueType(0))) {
1617 default: assert(0 && "This action is not supported yet!");
1618 case TargetLowering::Legal:
1619 break;
1620 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001621 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001622 if (Tmp4.getNode()) {
Nate Begeman5743da52008-01-05 20:47:37 +00001623 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001624 break;
1625 }
1626 // FALLTHROUGH
Mon P Wang58c37942008-10-30 08:01:45 +00001627 case TargetLowering::Promote:
1628 // Fall thru for vector case
Chris Lattner29b23012006-03-19 01:17:20 +00001629 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001630 // If the insert index is a constant, codegen this as a scalar_to_vector,
1631 // then a shuffle that inserts it into the right position in the vector.
1632 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001633 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1634 // match the element type of the vector being created.
1635 if (Tmp2.getValueType() ==
Duncan Sands13237ac2008-06-06 12:08:01 +00001636 Op.getValueType().getVectorElementType()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001637 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001638 Tmp1.getValueType(), Tmp2);
1639
Duncan Sands13237ac2008-06-06 12:08:01 +00001640 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1641 MVT ShufMaskVT =
1642 MVT::getIntVectorWithNumElements(NumElts);
1643 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001644
1645 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1646 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1647 // elt 0 of the RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001648 SmallVector<SDValue, 8> ShufOps;
Nate Begeman735ab3c2008-02-13 06:43:04 +00001649 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001650 if (i != InsertPos->getZExtValue())
Nate Begeman735ab3c2008-02-13 06:43:04 +00001651 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1652 else
1653 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1654 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001655 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001656 &ShufOps[0], ShufOps.size());
1657
1658 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1659 Tmp1, ScVec, ShufMask);
1660 Result = LegalizeOp(Result);
1661 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001662 }
Chris Lattner326870b2006-04-17 19:21:01 +00001663 }
Nate Begeman6f94f612008-04-25 18:07:40 +00001664 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner29b23012006-03-19 01:17:20 +00001665 break;
1666 }
1667 }
1668 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001669 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001670 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1671 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1672 break;
1673 }
1674
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001675 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1677 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1678 Node->getValueType(0))) {
1679 default: assert(0 && "This action is not supported yet!");
1680 case TargetLowering::Legal:
1681 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001682 case TargetLowering::Custom:
1683 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001684 if (Tmp3.getNode()) {
Chris Lattner79fb91c2006-03-19 06:47:21 +00001685 Result = Tmp3;
1686 break;
1687 }
1688 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001689 case TargetLowering::Expand:
1690 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001691 break;
1692 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001693 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001694 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001695 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1696 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1698
1699 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001700 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1701 default: assert(0 && "Unknown operation action!");
1702 case TargetLowering::Legal:
1703 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1704 "vector shuffle should not be created if not legal!");
1705 break;
1706 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001707 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001708 if (Tmp3.getNode()) {
Evan Cheng9fa89592006-04-05 06:07:11 +00001709 Result = Tmp3;
1710 break;
1711 }
1712 // FALLTHROUGH
1713 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001714 MVT VT = Node->getValueType(0);
1715 MVT EltVT = VT.getVectorElementType();
1716 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001717 SDValue Mask = Node->getOperand(2);
Evan Cheng9fa89592006-04-05 06:07:11 +00001718 unsigned NumElems = Mask.getNumOperands();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001719 SmallVector<SDValue,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001720 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001721 SDValue Arg = Mask.getOperand(i);
Evan Cheng9fa89592006-04-05 06:07:11 +00001722 if (Arg.getOpcode() == ISD::UNDEF) {
1723 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1724 } else {
1725 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001726 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng9fa89592006-04-05 06:07:11 +00001727 if (Idx < NumElems)
1728 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1729 DAG.getConstant(Idx, PtrVT)));
1730 else
1731 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1732 DAG.getConstant(Idx - NumElems, PtrVT)));
1733 }
1734 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001735 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001736 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001737 }
Chris Lattner6be79822006-04-04 17:23:26 +00001738 case TargetLowering::Promote: {
1739 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00001740 MVT OVT = Node->getValueType(0);
1741 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner6be79822006-04-04 17:23:26 +00001742
1743 // Cast the two input vectors.
1744 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1745 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1746
1747 // Convert the shuffle mask to the right # elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001748 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001749 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner6be79822006-04-04 17:23:26 +00001750 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1751 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1752 break;
1753 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001754 }
1755 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001756
1757 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001758 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001759 Tmp2 = LegalizeOp(Node->getOperand(1));
1760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001761 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001762 break;
1763
Dan Gohmana8665142007-06-25 16:23:39 +00001764 case ISD::EXTRACT_SUBVECTOR:
1765 Tmp1 = Node->getOperand(0);
1766 Tmp2 = LegalizeOp(Node->getOperand(1));
1767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1768 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001769 break;
1770
Mon P Wang58c37942008-10-30 08:01:45 +00001771 case ISD::CONCAT_VECTORS: {
1772 // Use extract/insert/build vector for now. We might try to be
1773 // more clever later.
1774 MVT PtrVT = TLI.getPointerTy();
1775 SmallVector<SDValue, 8> Ops;
1776 unsigned NumOperands = Node->getNumOperands();
1777 for (unsigned i=0; i < NumOperands; ++i) {
1778 SDValue SubOp = Node->getOperand(i);
1779 MVT VVT = SubOp.getNode()->getValueType(0);
1780 MVT EltVT = VVT.getVectorElementType();
1781 unsigned NumSubElem = VVT.getVectorNumElements();
1782 for (unsigned j=0; j < NumSubElem; ++j) {
1783 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1784 DAG.getConstant(j, PtrVT)));
1785 }
1786 }
1787 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1788 &Ops[0], Ops.size()));
1789 }
1790
Chris Lattner462505f2006-02-13 09:18:02 +00001791 case ISD::CALLSEQ_START: {
1792 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1793
1794 // Recursively Legalize all of the inputs of the call end that do not lead
1795 // to this call start. This ensures that any libcalls that need be inserted
1796 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001797 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001798 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greiff304a7a2008-08-28 21:40:38 +00001799 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattner4488f0c2006-07-26 23:55:56 +00001800 NodesLeadingTo);
1801 }
Chris Lattner462505f2006-02-13 09:18:02 +00001802
1803 // Now that we legalized all of the inputs (which may have inserted
1804 // libcalls) create the new CALLSEQ_START node.
1805 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1806
1807 // Merge in the last call, to ensure that this call start after the last
1808 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001809 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001810 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1811 Tmp1 = LegalizeOp(Tmp1);
1812 }
Chris Lattner462505f2006-02-13 09:18:02 +00001813
1814 // Do not try to legalize the target-specific arguments (#1+).
1815 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001816 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001817 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001818 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001819 }
1820
1821 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001822 AddLegalizedOperand(Op.getValue(0), Result);
1823 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1824 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1825
Chris Lattner462505f2006-02-13 09:18:02 +00001826 // Now that the callseq_start and all of the non-call nodes above this call
1827 // sequence have been legalized, legalize the call itself. During this
1828 // process, no libcalls can/will be inserted, guaranteeing that no calls
1829 // can overlap.
1830 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner462505f2006-02-13 09:18:02 +00001831 // Note that we are selecting this call!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001832 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner462505f2006-02-13 09:18:02 +00001833 IsLegalizingCall = true;
1834
1835 // Legalize the call, starting from the CALLSEQ_END.
1836 LegalizeOp(LastCALLSEQ_END);
1837 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1838 return Result;
1839 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001840 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001841 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1842 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001843 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001844 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1845 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001846 assert(I != LegalizedNodes.end() &&
1847 "Legalizing the call start should have legalized this node!");
1848 return I->second;
1849 }
1850
1851 // Otherwise, the call start has been legalized and everything is going
1852 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001854 // Do not try to legalize the target-specific arguments (#1+), except for
1855 // an optional flag input.
1856 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1857 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001858 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001859 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001860 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001861 }
1862 } else {
1863 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1864 if (Tmp1 != Node->getOperand(0) ||
1865 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001866 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001867 Ops[0] = Tmp1;
1868 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001869 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001870 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001871 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001872 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001873 // This finishes up call legalization.
1874 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001875
1876 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001877 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner8e2ee732006-02-14 00:55:02 +00001878 if (Node->getNumValues() == 2)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001879 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001880 return Result.getValue(Op.getResNo());
Evan Cheng7f4ec822006-01-11 22:14:47 +00001881 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001882 MVT VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1885 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001886 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001887
Chris Lattnerd02b0542006-01-28 10:58:55 +00001888 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001889 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001890 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001891 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001892 case TargetLowering::Expand: {
1893 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1894 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1895 " not tell us which reg is the stack pointer!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001896 SDValue Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001897
1898 // Chain the dynamic stack allocation so that it doesn't modify the stack
1899 // pointer when other instructions are using the stack.
Chris Lattner27539552008-10-11 22:08:30 +00001900 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendlingf359fed2007-11-13 00:44:25 +00001901
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001902 SDValue Size = Tmp2.getOperand(1);
1903 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng631ccc62007-08-16 23:50:06 +00001904 Chain = SP.getValue(1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001905 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng631ccc62007-08-16 23:50:06 +00001906 unsigned StackAlign =
1907 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1908 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001909 SP = DAG.getNode(ISD::AND, VT, SP,
1910 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001911 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001912 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1913
Chris Lattner27539552008-10-11 22:08:30 +00001914 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1915 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendlingf359fed2007-11-13 00:44:25 +00001916
Chris Lattnerd02b0542006-01-28 10:58:55 +00001917 Tmp1 = LegalizeOp(Tmp1);
1918 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001919 break;
1920 }
1921 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001922 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001923 if (Tmp3.getNode()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001924 Tmp1 = LegalizeOp(Tmp3);
1925 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001926 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001927 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001928 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001929 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001930 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001931 // Since this op produce two values, make sure to remember that we
1932 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001933 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1934 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001935 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001936 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001937 case ISD::INLINEASM: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001938 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001939 bool Changed = false;
1940 // Legalize all of the operands of the inline asm, in case they are nodes
1941 // that need to be expanded or something. Note we skip the asm string and
1942 // all of the TargetConstant flags.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001943 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001944 Changed = Op != Ops[0];
1945 Ops[0] = Op;
1946
1947 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1948 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001949 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001950 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001951 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001952 if (Op != Ops[i]) {
1953 Changed = true;
1954 Ops[i] = Op;
1955 }
1956 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001957 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001958
1959 if (HasInFlag) {
1960 Op = LegalizeOp(Ops.back());
1961 Changed |= Op != Ops.back();
1962 Ops.back() = Op;
1963 }
1964
1965 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001966 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001967
1968 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001969 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1970 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001971 return Result.getValue(Op.getResNo());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001972 }
Chris Lattner68a12142005-01-07 22:12:08 +00001973 case ISD::BR:
1974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001975 // Ensure that libcalls are emitted before a branch.
1976 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1977 Tmp1 = LegalizeOp(Tmp1);
1978 LastCALLSEQ_END = DAG.getEntryNode();
1979
Chris Lattnerd02b0542006-01-28 10:58:55 +00001980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001981 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001982 case ISD::BRIND:
1983 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1984 // Ensure that libcalls are emitted before a branch.
1985 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1986 Tmp1 = LegalizeOp(Tmp1);
1987 LastCALLSEQ_END = DAG.getEntryNode();
1988
1989 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1990 default: assert(0 && "Indirect target must be legal type (pointer)!");
1991 case Legal:
1992 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1993 break;
1994 }
1995 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1996 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001997 case ISD::BR_JT:
1998 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1999 // Ensure that libcalls are emitted before a branch.
2000 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2001 Tmp1 = LegalizeOp(Tmp1);
2002 LastCALLSEQ_END = DAG.getEntryNode();
2003
2004 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2005 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2006
2007 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2008 default: assert(0 && "This action is not supported yet!");
2009 case TargetLowering::Legal: break;
2010 case TargetLowering::Custom:
2011 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002012 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng84a28d42006-10-30 08:00:44 +00002013 break;
2014 case TargetLowering::Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002015 SDValue Chain = Result.getOperand(0);
2016 SDValue Table = Result.getOperand(1);
2017 SDValue Index = Result.getOperand(2);
Evan Cheng84a28d42006-10-30 08:00:44 +00002018
Duncan Sands13237ac2008-06-06 12:08:01 +00002019 MVT PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00002020 MachineFunction &MF = DAG.getMachineFunction();
2021 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00002022 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002023 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00002024
Duncan Sandse4bcb8e2008-12-12 08:13:38 +00002025 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2026 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2027 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng797d56f2007-11-09 01:32:10 +00002028 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00002029 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00002030 // For PIC, the sequence is:
2031 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00002032 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng797d56f2007-11-09 01:32:10 +00002033 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2034 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00002035 }
Evan Cheng797d56f2007-11-09 01:32:10 +00002036 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00002037 }
2038 }
2039 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00002040 case ISD::BRCOND:
2041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002042 // Ensure that libcalls are emitted before a return.
2043 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2044 Tmp1 = LegalizeOp(Tmp1);
2045 LastCALLSEQ_END = DAG.getEntryNode();
2046
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002047 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2048 case Expand: assert(0 && "It's impossible to expand bools");
2049 case Legal:
2050 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2051 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002052 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002053 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00002054
2055 // The top bits of the promoted condition are not necessarily zero, ensure
2056 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002057 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002058 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002059 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00002060 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002061 break;
2062 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002063 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002064
2065 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00002067
2068 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2069 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002070 case TargetLowering::Legal: break;
2071 case TargetLowering::Custom:
2072 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002073 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002074 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002075 case TargetLowering::Expand:
2076 // Expand brcond's setcc into its constituent parts and create a BR_CC
2077 // Node.
2078 if (Tmp2.getOpcode() == ISD::SETCC) {
2079 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2080 Tmp2.getOperand(0), Tmp2.getOperand(1),
2081 Node->getOperand(2));
2082 } else {
2083 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2084 DAG.getCondCode(ISD::SETNE), Tmp2,
2085 DAG.getConstant(0, Tmp2.getValueType()),
2086 Node->getOperand(2));
2087 }
2088 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002089 }
2090 break;
2091 case ISD::BR_CC:
2092 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002093 // Ensure that libcalls are emitted before a branch.
2094 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2095 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00002096 Tmp2 = Node->getOperand(2); // LHS
2097 Tmp3 = Node->getOperand(3); // RHS
2098 Tmp4 = Node->getOperand(1); // CC
2099
Duncan Sands8feb6942009-01-01 15:52:00 +00002100 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00002101 LastCALLSEQ_END = DAG.getEntryNode();
2102
Evan Cheng3b0f5e42008-10-15 02:05:31 +00002103 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman7e7f4392006-02-01 07:19:44 +00002104 // the LHS is a legal SETCC itself. In this case, we need to compare
2105 // the result against zero to select between true and false values.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002106 if (Tmp3.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00002107 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2108 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002109 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00002110
2111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2112 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002113
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002114 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2115 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002116 case TargetLowering::Legal: break;
2117 case TargetLowering::Custom:
2118 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002119 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002120 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002121 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00002122 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002123 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002124 LoadSDNode *LD = cast<LoadSDNode>(Node);
2125 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2126 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002127
Evan Chenge71fe34d2006-10-09 20:57:25 +00002128 ISD::LoadExtType ExtType = LD->getExtensionType();
2129 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002130 MVT VT = Node->getValueType(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2132 Tmp3 = Result.getValue(0);
2133 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002134
Evan Chenge71fe34d2006-10-09 20:57:25 +00002135 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2136 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002137 case TargetLowering::Legal:
2138 // If this is an unaligned load and the target doesn't support it,
2139 // expand it.
2140 if (!TLI.allowsUnalignedMemoryAccesses()) {
2141 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002142 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002143 if (LD->getAlignment() < ABIAlignment){
Gabor Greiff304a7a2008-08-28 21:40:38 +00002144 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002145 TLI);
2146 Tmp3 = Result.getOperand(0);
2147 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00002148 Tmp3 = LegalizeOp(Tmp3);
2149 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002150 }
2151 }
2152 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002153 case TargetLowering::Custom:
2154 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002155 if (Tmp1.getNode()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002156 Tmp3 = LegalizeOp(Tmp1);
2157 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002158 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002159 break;
2160 case TargetLowering::Promote: {
2161 // Only promote a load of vector type to another.
Duncan Sands13237ac2008-06-06 12:08:01 +00002162 assert(VT.isVector() && "Cannot promote this load!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00002163 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002164 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002165
2166 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002167 LD->getSrcValueOffset(),
2168 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00002169 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2170 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002171 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00002172 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002173 }
2174 // Since loads produce two values, make sure to remember that we
2175 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002176 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2177 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greifabfdf922008-08-26 22:36:50 +00002178 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002179 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00002180 MVT SrcVT = LD->getMemoryVT();
2181 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands95d46ef2008-01-23 20:39:46 +00002182 int SVOffset = LD->getSrcValueOffset();
2183 unsigned Alignment = LD->getAlignment();
2184 bool isVolatile = LD->isVolatile();
2185
Duncan Sands13237ac2008-06-06 12:08:01 +00002186 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00002187 // Some targets pretend to have an i1 loading operation, and actually
2188 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2189 // bits are guaranteed to be zero; it helps the optimizers understand
2190 // that these bits are zero. It is also useful for EXTLOAD, since it
2191 // tells the optimizers that those bits are undefined. It would be
2192 // nice to have an effective generic way of getting these benefits...
2193 // Until such a way is found, don't insist on promoting i1 here.
2194 (SrcVT != MVT::i1 ||
Evan Cheng07d53b12008-10-14 21:26:46 +00002195 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002196 // Promote to a byte-sized load if not loading an integral number of
2197 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands13237ac2008-06-06 12:08:01 +00002198 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2199 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002200 SDValue Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002201
2202 // The extra bits are guaranteed to be zero, since we stored them that
2203 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2204
2205 ISD::LoadExtType NewExtType =
2206 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2207
2208 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2209 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2210 NVT, isVolatile, Alignment);
2211
2212 Ch = Result.getValue(1); // The chain.
2213
2214 if (ExtType == ISD::SEXTLOAD)
2215 // Having the top bits zero doesn't help when sign extending.
2216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2217 Result, DAG.getValueType(SrcVT));
2218 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2219 // All the top bits are guaranteed to be zero - inform the optimizers.
2220 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2221 DAG.getValueType(SrcVT));
2222
2223 Tmp1 = LegalizeOp(Result);
2224 Tmp2 = LegalizeOp(Ch);
2225 } else if (SrcWidth & (SrcWidth - 1)) {
2226 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands13237ac2008-06-06 12:08:01 +00002227 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00002228 "Unsupported extload!");
2229 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2230 assert(RoundWidth < SrcWidth);
2231 unsigned ExtraWidth = SrcWidth - RoundWidth;
2232 assert(ExtraWidth < RoundWidth);
2233 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2234 "Load size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002235 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2236 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002237 SDValue Lo, Hi, Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002238 unsigned IncrementSize;
2239
2240 if (TLI.isLittleEndian()) {
2241 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2242 // Load the bottom RoundWidth bits.
2243 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2244 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2245 Alignment);
2246
2247 // Load the remaining ExtraWidth bits.
2248 IncrementSize = RoundWidth / 8;
2249 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2250 DAG.getIntPtrConstant(IncrementSize));
2251 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2252 LD->getSrcValue(), SVOffset + IncrementSize,
2253 ExtraVT, isVolatile,
2254 MinAlign(Alignment, IncrementSize));
2255
2256 // Build a factor node to remember that this load is independent of the
2257 // other one.
2258 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2259 Hi.getValue(1));
2260
2261 // Move the top bits to the right place.
2262 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2263 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2264
2265 // Join the hi and lo parts.
2266 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002267 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002268 // Big endian - avoid unaligned loads.
2269 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2270 // Load the top RoundWidth bits.
2271 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2272 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2273 Alignment);
2274
2275 // Load the remaining ExtraWidth bits.
2276 IncrementSize = RoundWidth / 8;
2277 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2278 DAG.getIntPtrConstant(IncrementSize));
2279 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2280 LD->getSrcValue(), SVOffset + IncrementSize,
2281 ExtraVT, isVolatile,
2282 MinAlign(Alignment, IncrementSize));
2283
2284 // Build a factor node to remember that this load is independent of the
2285 // other one.
2286 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2287 Hi.getValue(1));
2288
2289 // Move the top bits to the right place.
2290 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2291 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2292
2293 // Join the hi and lo parts.
2294 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2295 }
2296
2297 Tmp1 = LegalizeOp(Result);
2298 Tmp2 = LegalizeOp(Ch);
2299 } else {
Evan Cheng07d53b12008-10-14 21:26:46 +00002300 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002301 default: assert(0 && "This action is not supported yet!");
2302 case TargetLowering::Custom:
2303 isCustom = true;
2304 // FALLTHROUGH
2305 case TargetLowering::Legal:
2306 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2307 Tmp1 = Result.getValue(0);
2308 Tmp2 = Result.getValue(1);
2309
2310 if (isCustom) {
2311 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002312 if (Tmp3.getNode()) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002313 Tmp1 = LegalizeOp(Tmp3);
2314 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2315 }
2316 } else {
2317 // If this is an unaligned load and the target doesn't support it,
2318 // expand it.
2319 if (!TLI.allowsUnalignedMemoryAccesses()) {
2320 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002321 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands95d46ef2008-01-23 20:39:46 +00002322 if (LD->getAlignment() < ABIAlignment){
Gabor Greiff304a7a2008-08-28 21:40:38 +00002323 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands95d46ef2008-01-23 20:39:46 +00002324 TLI);
2325 Tmp1 = Result.getOperand(0);
2326 Tmp2 = Result.getOperand(1);
2327 Tmp1 = LegalizeOp(Tmp1);
2328 Tmp2 = LegalizeOp(Tmp2);
2329 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002330 }
2331 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002332 break;
2333 case TargetLowering::Expand:
2334 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2335 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002336 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands95d46ef2008-01-23 20:39:46 +00002337 LD->getSrcValueOffset(),
2338 LD->isVolatile(), LD->getAlignment());
2339 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2340 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2341 Tmp2 = LegalizeOp(Load.getValue(1));
2342 break;
2343 }
2344 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2345 // Turn the unsupported load into an EXTLOAD followed by an explicit
2346 // zero/sign extend inreg.
2347 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2348 Tmp1, Tmp2, LD->getSrcValue(),
2349 LD->getSrcValueOffset(), SrcVT,
2350 LD->isVolatile(), LD->getAlignment());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002351 SDValue ValRes;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002352 if (ExtType == ISD::SEXTLOAD)
2353 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2354 Result, DAG.getValueType(SrcVT));
2355 else
2356 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2357 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2358 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002359 break;
2360 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002361 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002362
Evan Chenge71fe34d2006-10-09 20:57:25 +00002363 // Since loads produce two values, make sure to remember that we legalized
2364 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002365 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2366 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00002367 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002368 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002369 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002370 case ISD::EXTRACT_ELEMENT: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002371 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5172ce62005-10-19 00:06:56 +00002372 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002373 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002374 case Legal:
Dan Gohmaneffb8942008-09-12 16:56:44 +00002375 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5172ce62005-10-19 00:06:56 +00002376 // 1 -> Hi
2377 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands13237ac2008-06-06 12:08:01 +00002378 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5172ce62005-10-19 00:06:56 +00002379 TLI.getShiftAmountTy()));
2380 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2381 } else {
2382 // 0 -> Lo
2383 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2384 Node->getOperand(0));
2385 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002386 break;
2387 case Expand:
2388 // Get both the low and high parts.
2389 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002390 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5172ce62005-10-19 00:06:56 +00002391 Result = Tmp2; // 1 -> Hi
2392 else
2393 Result = Tmp1; // 0 -> Lo
2394 break;
2395 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002396 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002397 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002398
2399 case ISD::CopyToReg:
2400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002401
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002402 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002403 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002404 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002405 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002406 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002408 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002409 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002410 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002411 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2413 Tmp3);
2414 } else {
2415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002416 }
2417
2418 // Since this produces two values, make sure to remember that we legalized
2419 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002420 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2421 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002422 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002423 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002424 break;
2425
2426 case ISD::RET:
2427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002428
2429 // Ensure that libcalls are emitted before a return.
2430 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2431 Tmp1 = LegalizeOp(Tmp1);
2432 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002433
Chris Lattnerdc750592005-01-07 07:47:09 +00002434 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002435 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002436 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002437 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002438 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002439 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002440 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002441 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002442 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00002443 if (!Tmp2.getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002444 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002445 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002446
2447 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002448 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002449 std::swap(Lo, Hi);
2450
Gabor Greiff304a7a2008-08-28 21:40:38 +00002451 if (Hi.getNode())
Evan Cheng3432ab92006-12-11 19:27:14 +00002452 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2453 else
2454 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002455 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002456 } else {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002457 SDNode *InVal = Tmp2.getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00002458 int InIx = Tmp2.getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00002459 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2460 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002461
Dan Gohmana8665142007-06-25 16:23:39 +00002462 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002463 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002464 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002465 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002466 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002467 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002469 } else if (NumElems == 1) {
2470 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002471 Tmp2 = ScalarizeVectorOp(Tmp2);
2472 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002474
2475 // FIXME: Returns of gcc generic vectors smaller than a legal type
2476 // should be returned in integer registers!
2477
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002478 // The scalarized value type may not be legal, e.g. it might require
2479 // promotion or expansion. Relegalize the return.
2480 Result = LegalizeOp(Result);
2481 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002482 // FIXME: Returns of gcc generic vectors larger than a legal vector
2483 // type should be returned by reference!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002484 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002485 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002486 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002487 Result = LegalizeOp(Result);
2488 }
2489 }
Misha Brukman835702a2005-04-21 22:36:52 +00002490 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002491 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002492 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002494 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002495 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002496 }
2497 break;
2498 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002499 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002500 break;
2501 default: { // ret <values>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002502 SmallVector<SDValue, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002503 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002504 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002505 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2506 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002507 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002508 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002509 break;
2510 case Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002511 SDValue Lo, Hi;
Duncan Sands13237ac2008-06-06 12:08:01 +00002512 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002513 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002514 ExpandOp(Node->getOperand(i), Lo, Hi);
2515 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002516 NewValues.push_back(Node->getOperand(i+1));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002517 if (Hi.getNode()) {
Evan Cheng3432ab92006-12-11 19:27:14 +00002518 NewValues.push_back(Hi);
2519 NewValues.push_back(Node->getOperand(i+1));
2520 }
Misha Brukman835702a2005-04-21 22:36:52 +00002521 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002522 }
2523 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002524 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002525 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002526
2527 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002528 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002529 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002530 Result = DAG.getNode(ISD::RET, MVT::Other,
2531 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002532 break;
2533 }
2534 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002535
Chris Lattner4d1ea712006-01-29 21:02:23 +00002536 if (Result.getOpcode() == ISD::RET) {
2537 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2538 default: assert(0 && "This action is not supported yet!");
2539 case TargetLowering::Legal: break;
2540 case TargetLowering::Custom:
2541 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002542 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner4d1ea712006-01-29 21:02:23 +00002543 break;
2544 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002545 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002546 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002547 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002548 StoreSDNode *ST = cast<StoreSDNode>(Node);
2549 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2550 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002551 int SVOffset = ST->getSrcValueOffset();
2552 unsigned Alignment = ST->getAlignment();
2553 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002554
Evan Chengab51cf22006-10-13 21:14:26 +00002555 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002556 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2557 // FIXME: We shouldn't do this for TargetConstantFP's.
2558 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2559 // to phase ordering between legalized code and the dag combiner. This
2560 // probably means that we need to integrate dag combiner and legalizer
2561 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002562 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002563 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002564 if (CFP->getValueType(0) == MVT::f32 &&
2565 getTypeAction(MVT::i32) == Legal) {
Dan Gohman10f7d852008-03-11 00:11:06 +00002566 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen54306fe2008-10-09 18:53:47 +00002567 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002568 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002569 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2570 SVOffset, isVolatile, Alignment);
2571 break;
2572 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002573 // If this target supports 64-bit registers, do a single 64-bit store.
2574 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002575 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman10f7d852008-03-11 00:11:06 +00002576 zextOrTrunc(64), MVT::i64);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002577 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2578 SVOffset, isVolatile, Alignment);
2579 break;
Duncan Sands8651e9c2008-06-13 19:07:40 +00002580 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002581 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2582 // stores. If the target supports neither 32- nor 64-bits, this
2583 // xform is certainly not worth it.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002584 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002585 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2586 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002587 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002588
2589 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2590 SVOffset, isVolatile, Alignment);
2591 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002592 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002593 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002594 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002595
2596 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2597 break;
2598 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002599 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002600 }
2601
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002602 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002603 case Legal: {
2604 Tmp3 = LegalizeOp(ST->getValue());
2605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2606 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002607
Duncan Sands13237ac2008-06-06 12:08:01 +00002608 MVT VT = Tmp3.getValueType();
Evan Chengab51cf22006-10-13 21:14:26 +00002609 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2610 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002611 case TargetLowering::Legal:
2612 // If this is an unaligned store and the target doesn't support it,
2613 // expand it.
2614 if (!TLI.allowsUnalignedMemoryAccesses()) {
2615 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002616 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002617 if (ST->getAlignment() < ABIAlignment)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002618 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002619 TLI);
2620 }
2621 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002622 case TargetLowering::Custom:
2623 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002624 if (Tmp1.getNode()) Result = Tmp1;
Evan Chengab51cf22006-10-13 21:14:26 +00002625 break;
2626 case TargetLowering::Promote:
Duncan Sands13237ac2008-06-06 12:08:01 +00002627 assert(VT.isVector() && "Unknown legal promote case!");
Evan Chengab51cf22006-10-13 21:14:26 +00002628 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2629 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2630 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002631 ST->getSrcValue(), SVOffset, isVolatile,
2632 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002633 break;
2634 }
2635 break;
2636 }
2637 case Promote:
Mon P Wang58c37942008-10-30 08:01:45 +00002638 if (!ST->getMemoryVT().isVector()) {
2639 // Truncate the value and store the result.
2640 Tmp3 = PromoteOp(ST->getValue());
2641 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2642 SVOffset, ST->getMemoryVT(),
2643 isVolatile, Alignment);
2644 break;
2645 }
2646 // Fall thru to expand for vector
2647 case Expand: {
Evan Chengab51cf22006-10-13 21:14:26 +00002648 unsigned IncrementSize = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002649 SDValue Lo, Hi;
Evan Chengab51cf22006-10-13 21:14:26 +00002650
2651 // If this is a vector type, then we have to calculate the increment as
2652 // the product of the element size in bytes, and the number of elements
2653 // in the high half of the vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00002654 if (ST->getValue().getValueType().isVector()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002655 SDNode *InVal = ST->getValue().getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00002656 int InIx = ST->getValue().getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00002657 MVT InVT = InVal->getValueType(InIx);
2658 unsigned NumElems = InVT.getVectorNumElements();
2659 MVT EVT = InVT.getVectorElementType();
Evan Chengab51cf22006-10-13 21:14:26 +00002660
Dan Gohmana8665142007-06-25 16:23:39 +00002661 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002662 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002663 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002664 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002665 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002666 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002667 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002668 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002669 Result = LegalizeOp(Result);
2670 break;
2671 } else if (NumElems == 1) {
2672 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002673 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002674 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002675 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002676 // The scalarized value type may not be legal, e.g. it might require
2677 // promotion or expansion. Relegalize the scalar store.
2678 Result = LegalizeOp(Result);
2679 break;
2680 } else {
Mon P Wang58c37942008-10-30 08:01:45 +00002681 // Check if we have widen this node with another value
2682 std::map<SDValue, SDValue>::iterator I =
2683 WidenNodes.find(ST->getValue());
2684 if (I != WidenNodes.end()) {
2685 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2686 break;
2687 }
2688 else {
2689 SplitVectorOp(ST->getValue(), Lo, Hi);
2690 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2691 EVT.getSizeInBits()/8;
2692 }
Evan Chengab51cf22006-10-13 21:14:26 +00002693 }
2694 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002695 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002696 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002697
Richard Pennington4b35e642008-09-25 16:15:10 +00002698 if (Hi.getNode() && TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002699 std::swap(Lo, Hi);
2700 }
2701
2702 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002703 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002704
Gabor Greiff304a7a2008-08-28 21:40:38 +00002705 if (Hi.getNode() == NULL) {
Evan Cheng0076ca02006-12-12 19:53:13 +00002706 // Must be int <-> float one-to-one expansion.
2707 Result = Lo;
2708 break;
2709 }
2710
Evan Chengab51cf22006-10-13 21:14:26 +00002711 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002712 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002713 assert(isTypeLegal(Tmp2.getValueType()) &&
2714 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002715 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002716 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002717 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002718 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002719 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2720 break;
Mon P Wang58c37942008-10-30 08:01:45 +00002721 } // case Expand
Evan Chengab51cf22006-10-13 21:14:26 +00002722 }
2723 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002724 switch (getTypeAction(ST->getValue().getValueType())) {
2725 case Legal:
2726 Tmp3 = LegalizeOp(ST->getValue());
2727 break;
2728 case Promote:
Mon P Wang58c37942008-10-30 08:01:45 +00002729 if (!ST->getValue().getValueType().isVector()) {
2730 // We can promote the value, the truncstore will still take care of it.
2731 Tmp3 = PromoteOp(ST->getValue());
2732 break;
2733 }
2734 // Vector case falls through to expand
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002735 case Expand:
2736 // Just store the low part. This may become a non-trunc store, so make
2737 // sure to use getTruncStore, not UpdateNodeOperands below.
2738 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2739 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2740 SVOffset, MVT::i8, isVolatile, Alignment);
2741 }
Evan Chengab51cf22006-10-13 21:14:26 +00002742
Duncan Sands13237ac2008-06-06 12:08:01 +00002743 MVT StVT = ST->getMemoryVT();
2744 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands88de26c2008-01-22 07:17:34 +00002745
Duncan Sands13237ac2008-06-06 12:08:01 +00002746 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands88de26c2008-01-22 07:17:34 +00002747 // Promote to a byte-sized store with upper bits zero if not
2748 // storing an integral number of bytes. For example, promote
2749 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands13237ac2008-06-06 12:08:01 +00002750 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands88de26c2008-01-22 07:17:34 +00002751 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2752 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2753 SVOffset, NVT, isVolatile, Alignment);
2754 } else if (StWidth & (StWidth - 1)) {
2755 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands13237ac2008-06-06 12:08:01 +00002756 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands88de26c2008-01-22 07:17:34 +00002757 "Unsupported truncstore!");
2758 unsigned RoundWidth = 1 << Log2_32(StWidth);
2759 assert(RoundWidth < StWidth);
2760 unsigned ExtraWidth = StWidth - RoundWidth;
2761 assert(ExtraWidth < RoundWidth);
2762 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2763 "Store size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002764 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2765 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002766 SDValue Lo, Hi;
Duncan Sands88de26c2008-01-22 07:17:34 +00002767 unsigned IncrementSize;
2768
2769 if (TLI.isLittleEndian()) {
2770 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2771 // Store the bottom RoundWidth bits.
2772 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2773 SVOffset, RoundVT,
2774 isVolatile, Alignment);
2775
2776 // Store the remaining ExtraWidth bits.
2777 IncrementSize = RoundWidth / 8;
2778 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2779 DAG.getIntPtrConstant(IncrementSize));
2780 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2781 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2782 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2783 SVOffset + IncrementSize, ExtraVT, isVolatile,
2784 MinAlign(Alignment, IncrementSize));
2785 } else {
2786 // Big endian - avoid unaligned stores.
2787 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2788 // Store the top RoundWidth bits.
2789 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2790 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2791 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2792 RoundVT, isVolatile, Alignment);
2793
2794 // Store the remaining ExtraWidth bits.
2795 IncrementSize = RoundWidth / 8;
2796 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2797 DAG.getIntPtrConstant(IncrementSize));
2798 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2799 SVOffset + IncrementSize, ExtraVT, isVolatile,
2800 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002801 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002802
2803 // The order of the stores doesn't matter.
2804 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2805 } else {
2806 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2807 Tmp2 != ST->getBasePtr())
2808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2809 ST->getOffset());
2810
2811 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2812 default: assert(0 && "This action is not supported yet!");
2813 case TargetLowering::Legal:
2814 // If this is an unaligned store and the target doesn't support it,
2815 // expand it.
2816 if (!TLI.allowsUnalignedMemoryAccesses()) {
2817 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002818 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands88de26c2008-01-22 07:17:34 +00002819 if (ST->getAlignment() < ABIAlignment)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002820 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands88de26c2008-01-22 07:17:34 +00002821 TLI);
2822 }
2823 break;
2824 case TargetLowering::Custom:
2825 Result = TLI.LowerOperation(Result, DAG);
2826 break;
2827 case Expand:
2828 // TRUNCSTORE:i16 i32 -> STORE i16
2829 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2830 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2831 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2832 isVolatile, Alignment);
2833 break;
2834 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002835 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002836 }
2837 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002838 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002839 case ISD::PCMARKER:
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002842 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002843 case ISD::STACKSAVE:
2844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002845 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2846 Tmp1 = Result.getValue(0);
2847 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002848
Chris Lattnerb3266452006-01-13 02:50:02 +00002849 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2850 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002851 case TargetLowering::Legal: break;
2852 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002853 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002854 if (Tmp3.getNode()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002855 Tmp1 = LegalizeOp(Tmp3);
2856 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002857 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002858 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002859 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002860 // Expand to CopyFromReg if the target set
2861 // StackPointerRegisterToSaveRestore.
2862 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002863 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002864 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002865 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002866 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002867 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2868 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002869 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002870 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002871 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002872
2873 // Since stacksave produce two values, make sure to remember that we
2874 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002875 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2876 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00002877 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002878
Chris Lattnerb3266452006-01-13 02:50:02 +00002879 case ISD::STACKRESTORE:
2880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2881 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002883
2884 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2885 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002886 case TargetLowering::Legal: break;
2887 case TargetLowering::Custom:
2888 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002889 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002890 break;
2891 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002892 // Expand to CopyToReg if the target set
2893 // StackPointerRegisterToSaveRestore.
2894 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2895 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2896 } else {
2897 Result = Tmp1;
2898 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002899 break;
2900 }
2901 break;
2902
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002903 case ISD::READCYCLECOUNTER:
2904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002905 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002906 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2907 Node->getValueType(0))) {
2908 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002909 case TargetLowering::Legal:
2910 Tmp1 = Result.getValue(0);
2911 Tmp2 = Result.getValue(1);
2912 break;
Evan Cheng69739932006-11-29 08:26:18 +00002913 case TargetLowering::Custom:
2914 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002915 Tmp1 = LegalizeOp(Result.getValue(0));
2916 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002917 break;
2918 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002919
2920 // Since rdcc produce two values, make sure to remember that we legalized
2921 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002922 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2923 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002924 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002925
Chris Lattner39c67442005-01-14 22:08:15 +00002926 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002927 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2928 case Expand: assert(0 && "It's impossible to expand bools");
2929 case Legal:
2930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2931 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002932 case Promote: {
Mon P Wang58c37942008-10-30 08:01:45 +00002933 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002934 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002935 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002936 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002937 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002938 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002939 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002940 break;
2941 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002942 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002943 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002944 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002945
Chris Lattnerd02b0542006-01-28 10:58:55 +00002946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002947
Nate Begeman987121a2005-08-23 04:29:48 +00002948 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002949 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002950 case TargetLowering::Legal: break;
2951 case TargetLowering::Custom: {
2952 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002953 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002954 break;
2955 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002956 case TargetLowering::Expand:
2957 if (Tmp1.getOpcode() == ISD::SETCC) {
2958 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2959 Tmp2, Tmp3,
2960 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2961 } else {
2962 Result = DAG.getSelectCC(Tmp1,
2963 DAG.getConstant(0, Tmp1.getValueType()),
2964 Tmp2, Tmp3, ISD::SETNE);
2965 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002966 break;
2967 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002968 MVT NVT =
Chris Lattner3c0dd462005-01-16 07:29:19 +00002969 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2970 unsigned ExtOp, TruncOp;
Duncan Sands13237ac2008-06-06 12:08:01 +00002971 if (Tmp2.getValueType().isVector()) {
Evan Chengbe8a8932006-04-12 16:33:18 +00002972 ExtOp = ISD::BIT_CONVERT;
2973 TruncOp = ISD::BIT_CONVERT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002974 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002975 ExtOp = ISD::ANY_EXTEND;
2976 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002977 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002978 ExtOp = ISD::FP_EXTEND;
2979 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002980 }
2981 // Promote each of the values to the new type.
2982 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2983 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2984 // Perform the larger operation, then round down.
2985 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002986 if (TruncOp != ISD::FP_ROUND)
2987 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2988 else
2989 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2990 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002991 break;
2992 }
2993 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002994 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002995 case ISD::SELECT_CC: {
2996 Tmp1 = Node->getOperand(0); // LHS
2997 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002998 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2999 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003000 SDValue CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00003001
Duncan Sands8feb6942009-01-01 15:52:00 +00003002 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003003
Evan Cheng3b0f5e42008-10-15 02:05:31 +00003004 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman7e7f4392006-02-01 07:19:44 +00003005 // the LHS is a legal SETCC itself. In this case, we need to compare
3006 // the result against zero to select between true and false values.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003007 if (Tmp2.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003008 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3009 CC = DAG.getCondCode(ISD::SETNE);
3010 }
3011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3012
3013 // Everything is legal, see if we should expand this op or something.
3014 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3015 default: assert(0 && "This action is not supported yet!");
3016 case TargetLowering::Legal: break;
3017 case TargetLowering::Custom:
3018 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003019 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00003020 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003021 }
3022 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00003023 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003024 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00003025 Tmp1 = Node->getOperand(0);
3026 Tmp2 = Node->getOperand(1);
3027 Tmp3 = Node->getOperand(2);
Evan Cheng3b0f5e42008-10-15 02:05:31 +00003028 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003029
3030 // If we had to Expand the SetCC operands into a SELECT node, then it may
3031 // not always be possible to return a true LHS & RHS. In this case, just
3032 // return the value we legalized, returned in the LHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003033 if (Tmp2.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003034 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00003035 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003036 }
Nate Begeman987121a2005-08-23 04:29:48 +00003037
Chris Lattnerf263a232006-01-30 22:43:50 +00003038 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003039 default: assert(0 && "Cannot handle this action for SETCC yet!");
3040 case TargetLowering::Custom:
3041 isCustom = true;
3042 // FALLTHROUGH.
3043 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003045 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003046 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003047 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003048 }
Nate Begeman987121a2005-08-23 04:29:48 +00003049 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003050 case TargetLowering::Promote: {
3051 // First step, figure out the appropriate operation to use.
3052 // Allow SETCC to not be supported for all legal data types
3053 // Mostly this targets FP
Duncan Sands13237ac2008-06-06 12:08:01 +00003054 MVT NewInTy = Node->getOperand(0).getValueType();
3055 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003056
3057 // Scan for the appropriate larger type to use.
3058 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003059 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003060
Duncan Sands13237ac2008-06-06 12:08:01 +00003061 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003062 "Fell off of the edge of the integer world");
Duncan Sands13237ac2008-06-06 12:08:01 +00003063 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003064 "Fell off of the edge of the floating point world");
3065
3066 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00003067 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003068 break;
3069 }
Duncan Sands13237ac2008-06-06 12:08:01 +00003070 if (NewInTy.isInteger())
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003071 assert(0 && "Cannot promote Legal Integer SETCC yet");
3072 else {
3073 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3074 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3075 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003076 Tmp1 = LegalizeOp(Tmp1);
3077 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00003079 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00003080 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003081 }
Nate Begeman987121a2005-08-23 04:29:48 +00003082 case TargetLowering::Expand:
3083 // Expand a setcc node into a select_cc of the same condition, lhs, and
3084 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands13237ac2008-06-06 12:08:01 +00003085 MVT VT = Node->getValueType(0);
Nate Begeman987121a2005-08-23 04:29:48 +00003086 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3087 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003088 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00003089 break;
3090 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003091 break;
Nate Begemancfcb5602008-05-12 19:40:03 +00003092 case ISD::VSETCC: {
3093 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3094 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003095 SDValue CC = Node->getOperand(2);
Nate Begemancfcb5602008-05-12 19:40:03 +00003096
3097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3098
3099 // Everything is legal, see if we should expand this op or something.
3100 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3101 default: assert(0 && "This action is not supported yet!");
3102 case TargetLowering::Legal: break;
3103 case TargetLowering::Custom:
3104 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003105 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemancfcb5602008-05-12 19:40:03 +00003106 break;
Mon P Wangf95bd202008-12-13 08:15:14 +00003107 case TargetLowering::Expand: {
3108 // Unroll into a nasty set of scalar code for now.
3109 MVT VT = Node->getValueType(0);
3110 unsigned NumElems = VT.getVectorNumElements();
3111 MVT EltVT = VT.getVectorElementType();
3112 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3113 SmallVector<SDValue, 8> Ops(NumElems);
3114 for (unsigned i = 0; i < NumElems; ++i) {
3115 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3116 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands8feb6942009-01-01 15:52:00 +00003117 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang015a7f52008-12-17 08:49:47 +00003118 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3119 Tmp2, DAG.getIntPtrConstant(i)),
3120 CC);
3121 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3122 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3123 DAG.getConstant(0, EltVT));
Mon P Wangf95bd202008-12-13 08:15:14 +00003124 }
3125 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3126 break;
3127 }
Nate Begemancfcb5602008-05-12 19:40:03 +00003128 }
3129 break;
3130 }
Chris Lattner5385db52005-05-09 20:23:03 +00003131
Chris Lattner4157c412005-04-02 04:00:59 +00003132 case ISD::SHL_PARTS:
3133 case ISD::SRA_PARTS:
3134 case ISD::SRL_PARTS: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003135 SmallVector<SDValue, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003136 bool Changed = false;
3137 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3138 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3139 Changed |= Ops.back() != Node->getOperand(i);
3140 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003141 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00003142 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00003143
Evan Cheng870e4f82006-01-09 18:31:59 +00003144 switch (TLI.getOperationAction(Node->getOpcode(),
3145 Node->getValueType(0))) {
3146 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003147 case TargetLowering::Legal: break;
3148 case TargetLowering::Custom:
3149 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003150 if (Tmp1.getNode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003151 SDValue Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00003152 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003153 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003154 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00003155 if (i == Op.getResNo())
Evan Cheng13e8c9d2006-01-19 04:54:52 +00003156 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00003157 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003158 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00003159 return RetVal;
3160 }
Evan Cheng870e4f82006-01-09 18:31:59 +00003161 break;
3162 }
3163
Chris Lattner13fe99c2005-04-02 05:00:07 +00003164 // Since these produce multiple values, make sure to remember that we
3165 // legalized all of them.
3166 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003167 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +00003168 return Result.getValue(Op.getResNo());
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003169 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003170
3171 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00003172 case ISD::ADD:
3173 case ISD::SUB:
3174 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00003175 case ISD::MULHS:
3176 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00003177 case ISD::UDIV:
3178 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00003179 case ISD::AND:
3180 case ISD::OR:
3181 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00003182 case ISD::SHL:
3183 case ISD::SRL:
3184 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003185 case ISD::FADD:
3186 case ISD::FSUB:
3187 case ISD::FMUL:
3188 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00003189 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00003190 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00003191 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3192 case Expand: assert(0 && "Not possible");
3193 case Legal:
3194 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3195 break;
3196 case Promote:
3197 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3198 break;
3199 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003200
3201 if ((Node->getOpcode() == ISD::SHL ||
3202 Node->getOpcode() == ISD::SRL ||
3203 Node->getOpcode() == ISD::SRA) &&
3204 !Node->getValueType(0).isVector()) {
Mon P Wang4dd832d2008-12-09 05:46:39 +00003205 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wangf95bd202008-12-13 08:15:14 +00003206 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003207
Chris Lattnerd02b0542006-01-28 10:58:55 +00003208 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wang25f01062008-11-10 04:46:22 +00003209
Andrew Lenharth72594262005-12-24 23:42:32 +00003210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00003211 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003212 case TargetLowering::Legal: break;
3213 case TargetLowering::Custom:
3214 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003215 if (Tmp1.getNode()) {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003216 Result = Tmp1;
3217 break;
Nate Begeman82f19252008-07-29 19:07:27 +00003218 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003219 // Fall through if the custom lower can't deal with the operation
Chris Lattner87f08092006-04-02 03:57:31 +00003220 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003221 MVT VT = Op.getValueType();
Mon P Wang58c37942008-10-30 08:01:45 +00003222
Dan Gohmana1603612007-10-08 18:33:35 +00003223 // See if multiply or divide can be lowered using two-result operations.
3224 SDVTList VTs = DAG.getVTList(VT, VT);
3225 if (Node->getOpcode() == ISD::MUL) {
3226 // We just need the low half of the multiply; try both the signed
3227 // and unsigned forms. If the target supports both SMUL_LOHI and
3228 // UMUL_LOHI, form a preference by checking which forms of plain
3229 // MULH it supports.
3230 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3231 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3232 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3233 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3234 unsigned OpToUse = 0;
3235 if (HasSMUL_LOHI && !HasMULHS) {
3236 OpToUse = ISD::SMUL_LOHI;
3237 } else if (HasUMUL_LOHI && !HasMULHU) {
3238 OpToUse = ISD::UMUL_LOHI;
3239 } else if (HasSMUL_LOHI) {
3240 OpToUse = ISD::SMUL_LOHI;
3241 } else if (HasUMUL_LOHI) {
3242 OpToUse = ISD::UMUL_LOHI;
3243 }
3244 if (OpToUse) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003245 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003246 break;
3247 }
3248 }
3249 if (Node->getOpcode() == ISD::MULHS &&
3250 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003251 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3252 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003253 break;
3254 }
3255 if (Node->getOpcode() == ISD::MULHU &&
3256 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003257 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3258 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003259 break;
3260 }
3261 if (Node->getOpcode() == ISD::SDIV &&
3262 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003263 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3264 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003265 break;
3266 }
3267 if (Node->getOpcode() == ISD::UDIV &&
3268 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003269 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3270 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003271 break;
3272 }
Mon P Wanga5016402008-12-18 20:03:17 +00003273
Dan Gohman2a7de412007-10-11 23:57:53 +00003274 // Check to see if we have a libcall for this operator.
3275 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3276 bool isSigned = false;
3277 switch (Node->getOpcode()) {
3278 case ISD::UDIV:
3279 case ISD::SDIV:
3280 if (VT == MVT::i32) {
3281 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang58c37942008-10-30 08:01:45 +00003282 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00003283 isSigned = Node->getOpcode() == ISD::SDIV;
3284 }
3285 break;
Chris Lattner24168962008-10-04 21:27:46 +00003286 case ISD::MUL:
3287 if (VT == MVT::i32)
3288 LC = RTLIB::MUL_I32;
Scott Michel0c9259f2008-12-29 03:21:37 +00003289 else if (VT == MVT::i64)
3290 LC = RTLIB::MUL_I64;
Chris Lattner24168962008-10-04 21:27:46 +00003291 break;
Dan Gohman2a7de412007-10-11 23:57:53 +00003292 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3294 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00003295 break;
3296 default: break;
3297 }
3298 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003299 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003300 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003301 break;
3302 }
Mon P Wang58c37942008-10-30 08:01:45 +00003303
Duncan Sands13237ac2008-06-06 12:08:01 +00003304 assert(Node->getValueType(0).isVector() &&
Chris Lattner87f08092006-04-02 03:57:31 +00003305 "Cannot expand this binary operator!");
3306 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003307 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003308 break;
3309 }
Evan Cheng119266e2006-04-12 21:20:24 +00003310 case TargetLowering::Promote: {
3311 switch (Node->getOpcode()) {
3312 default: assert(0 && "Do not know how to promote this BinOp!");
3313 case ISD::AND:
3314 case ISD::OR:
3315 case ISD::XOR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003316 MVT OVT = Node->getValueType(0);
3317 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3318 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Cheng119266e2006-04-12 21:20:24 +00003319 // Bit convert each of the values to the new type.
3320 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3321 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3323 // Bit convert the result back the original type.
3324 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3325 break;
3326 }
3327 }
3328 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003329 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003330 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003331
Dan Gohman12334ac2007-10-05 14:17:22 +00003332 case ISD::SMUL_LOHI:
3333 case ISD::UMUL_LOHI:
3334 case ISD::SDIVREM:
3335 case ISD::UDIVREM:
3336 // These nodes will only be produced by target-specific lowering, so
3337 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003338 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003339 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003340
3341 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3342 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003344 break;
3345
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003346 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3347 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3348 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3349 case Expand: assert(0 && "Not possible");
3350 case Legal:
3351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3352 break;
3353 case Promote:
3354 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3355 break;
3356 }
3357
3358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3359
3360 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3361 default: assert(0 && "Operation not supported");
3362 case TargetLowering::Custom:
3363 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003364 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003365 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003366 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003367 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003368 // If this target supports fabs/fneg natively and select is cheap,
3369 // do this efficiently.
3370 if (!TLI.isSelectExpensive() &&
3371 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3372 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003373 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003374 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003375 // Get the sign bit of the RHS.
Duncan Sands13237ac2008-06-06 12:08:01 +00003376 MVT IVT =
Chris Lattner994d8e62006-03-13 06:08:38 +00003377 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003378 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands8feb6942009-01-01 15:52:00 +00003379 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Chris Lattner994d8e62006-03-13 06:08:38 +00003380 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3381 // Get the absolute value of the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003382 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner994d8e62006-03-13 06:08:38 +00003383 // Select between the nabs and abs value based on the sign bit of
3384 // the input.
3385 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3386 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3387 AbsVal),
3388 AbsVal);
3389 Result = LegalizeOp(Result);
3390 break;
3391 }
3392
3393 // Otherwise, do bitwise ops!
Duncan Sands13237ac2008-06-06 12:08:01 +00003394 MVT NVT =
Evan Cheng003feb02007-01-04 21:56:39 +00003395 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3396 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3397 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3398 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003399 break;
3400 }
Evan Cheng003feb02007-01-04 21:56:39 +00003401 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003402 break;
3403
Nate Begeman5965bd12006-02-17 05:43:56 +00003404 case ISD::ADDC:
3405 case ISD::SUBC:
3406 Tmp1 = LegalizeOp(Node->getOperand(0));
3407 Tmp2 = LegalizeOp(Node->getOperand(1));
3408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003409 Tmp3 = Result.getValue(0);
3410 Tmp4 = Result.getValue(1);
3411
3412 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3413 default: assert(0 && "This action is not supported yet!");
3414 case TargetLowering::Legal:
3415 break;
3416 case TargetLowering::Custom:
3417 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3418 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta7ae1a842008-11-27 05:58:04 +00003419 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003420 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3421 }
3422 break;
3423 }
Nate Begeman5965bd12006-02-17 05:43:56 +00003424 // Since this produces two values, make sure to remember that we legalized
3425 // both of them.
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003426 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3427 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3428 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukman835702a2005-04-21 22:36:52 +00003429
Nate Begeman5965bd12006-02-17 05:43:56 +00003430 case ISD::ADDE:
3431 case ISD::SUBE:
3432 Tmp1 = LegalizeOp(Node->getOperand(0));
3433 Tmp2 = LegalizeOp(Node->getOperand(1));
3434 Tmp3 = LegalizeOp(Node->getOperand(2));
3435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003436 Tmp3 = Result.getValue(0);
3437 Tmp4 = Result.getValue(1);
3438
3439 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3440 default: assert(0 && "This action is not supported yet!");
3441 case TargetLowering::Legal:
3442 break;
3443 case TargetLowering::Custom:
3444 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3445 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta7ae1a842008-11-27 05:58:04 +00003446 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003447 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3448 }
3449 break;
3450 }
Nate Begeman5965bd12006-02-17 05:43:56 +00003451 // Since this produces two values, make sure to remember that we legalized
3452 // both of them.
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003453 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3454 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3455 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman5965bd12006-02-17 05:43:56 +00003456
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003457 case ISD::BUILD_PAIR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003458 MVT PairTy = Node->getValueType(0);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003459 // TODO: handle the case where the Lo and Hi operands are not of legal type
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3462 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003463 case TargetLowering::Promote:
3464 case TargetLowering::Custom:
3465 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003466 case TargetLowering::Legal:
3467 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3468 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3469 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003470 case TargetLowering::Expand:
3471 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3472 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3473 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003474 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003475 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003476 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003477 break;
3478 }
3479 break;
3480 }
3481
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003482 case ISD::UREM:
3483 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003484 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003485 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3486 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003487
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003488 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003489 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3490 case TargetLowering::Custom:
3491 isCustom = true;
3492 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003493 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003494 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003495 if (isCustom) {
3496 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003497 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003498 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003499 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003500 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003501 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003502 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands13237ac2008-06-06 12:08:01 +00003503 MVT VT = Node->getValueType(0);
Dan Gohmana1603612007-10-08 18:33:35 +00003504
3505 // See if remainder can be lowered using two-result operations.
3506 SDVTList VTs = DAG.getVTList(VT, VT);
3507 if (Node->getOpcode() == ISD::SREM &&
3508 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003509 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003510 break;
3511 }
3512 if (Node->getOpcode() == ISD::UREM &&
3513 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003514 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003515 break;
3516 }
3517
Duncan Sands13237ac2008-06-06 12:08:01 +00003518 if (VT.isInteger()) {
Dan Gohmana1603612007-10-08 18:33:35 +00003519 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003520 TargetLowering::Legal) {
3521 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003522 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003523 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3524 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands13237ac2008-06-06 12:08:01 +00003525 } else if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003526 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003527 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003528 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003529 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003530 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3531 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003532 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003533 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003534 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003535 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00003536 assert(VT.isFloatingPoint() &&
Dan Gohmanccfc0282007-11-06 22:11:54 +00003537 "remainder op must have integer or floating-point type");
Duncan Sands13237ac2008-06-06 12:08:01 +00003538 if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003539 Result = LegalizeOp(UnrollVectorOp(Op));
3540 } else {
3541 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003542 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3543 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003544 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003545 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman08143e32007-11-05 23:35:22 +00003546 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003547 }
3548 break;
3549 }
Dan Gohmana1603612007-10-08 18:33:35 +00003550 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003551 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003552 case ISD::VAARG: {
3553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3554 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3555
Duncan Sands13237ac2008-06-06 12:08:01 +00003556 MVT VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003557 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3558 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003559 case TargetLowering::Custom:
3560 isCustom = true;
3561 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003562 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3564 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003565 Tmp1 = Result.getValue(1);
3566
3567 if (isCustom) {
3568 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003569 if (Tmp2.getNode()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003570 Result = LegalizeOp(Tmp2);
3571 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3572 }
3573 }
Nate Begemane74795c2006-01-25 18:21:52 +00003574 break;
3575 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003576 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003577 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003578 // Increment the pointer, VAList, to the next vaarg
Duncan Sands0207a3f2008-11-03 11:51:11 +00003579 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsdc020f92009-01-12 20:38:59 +00003580 DAG.getConstant(TLI.getTargetData()->
3581 getTypePaddedSize(VT.getTypeForMVT()),
3582 TLI.getPointerTy()));
Nate Begemane74795c2006-01-25 18:21:52 +00003583 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003584 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003585 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003586 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003587 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003588 Result = LegalizeOp(Result);
3589 break;
3590 }
3591 }
3592 // Since VAARG produces two values, make sure to remember that we
3593 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003594 AddLegalizedOperand(SDValue(Node, 0), Result);
3595 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greifabfdf922008-08-26 22:36:50 +00003596 return Op.getResNo() ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003597 }
3598
3599 case ISD::VACOPY:
3600 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3601 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3602 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3603
3604 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3605 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003606 case TargetLowering::Custom:
3607 isCustom = true;
3608 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003609 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3611 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003612 if (isCustom) {
3613 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003614 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003615 }
Nate Begemane74795c2006-01-25 18:21:52 +00003616 break;
3617 case TargetLowering::Expand:
3618 // This defaults to loading a pointer from the input and storing it to the
3619 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003620 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3621 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman9752a8f32008-04-17 02:09:26 +00003622 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3623 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003624 break;
3625 }
3626 break;
3627
3628 case ISD::VAEND:
3629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3631
3632 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3633 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003634 case TargetLowering::Custom:
3635 isCustom = true;
3636 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003637 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003639 if (isCustom) {
3640 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003641 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003642 }
Nate Begemane74795c2006-01-25 18:21:52 +00003643 break;
3644 case TargetLowering::Expand:
3645 Result = Tmp1; // Default to a no-op, return the chain
3646 break;
3647 }
3648 break;
3649
3650 case ISD::VASTART:
3651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3652 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3653
Chris Lattnerd02b0542006-01-28 10:58:55 +00003654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3655
Nate Begemane74795c2006-01-25 18:21:52 +00003656 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3657 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003658 case TargetLowering::Legal: break;
3659 case TargetLowering::Custom:
3660 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003661 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003662 break;
3663 }
3664 break;
3665
Nate Begeman1b8121b2006-01-11 21:21:00 +00003666 case ISD::ROTL:
3667 case ISD::ROTR:
3668 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3669 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003671 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3672 default:
3673 assert(0 && "ROTL/ROTR legalize operation not supported");
3674 break;
3675 case TargetLowering::Legal:
3676 break;
3677 case TargetLowering::Custom:
3678 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003679 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel16627a52007-04-02 21:36:32 +00003680 break;
3681 case TargetLowering::Promote:
3682 assert(0 && "Do not know how to promote ROTL/ROTR");
3683 break;
3684 case TargetLowering::Expand:
3685 assert(0 && "Do not know how to expand ROTL/ROTR");
3686 break;
3687 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003688 break;
3689
Nate Begeman2fba8a32006-01-14 03:14:10 +00003690 case ISD::BSWAP:
3691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003693 case TargetLowering::Custom:
3694 assert(0 && "Cannot custom legalize this yet!");
3695 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003696 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003697 break;
3698 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003699 MVT OVT = Tmp1.getValueType();
3700 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3701 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begeman2fba8a32006-01-14 03:14:10 +00003702
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003703 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3704 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3705 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3706 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3707 break;
3708 }
3709 case TargetLowering::Expand:
3710 Result = ExpandBSWAP(Tmp1);
3711 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003712 }
3713 break;
3714
Andrew Lenharth5e177822005-05-03 17:19:30 +00003715 case ISD::CTPOP:
3716 case ISD::CTTZ:
3717 case ISD::CTLZ:
3718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003720 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003721 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003722 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003723 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003724 TargetLowering::Custom) {
3725 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003726 if (Tmp1.getNode()) {
Scott Michel5b80ecb2007-08-02 02:22:46 +00003727 Result = Tmp1;
3728 }
Scott Michel34e2d222007-07-30 21:00:31 +00003729 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003730 break;
3731 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003732 MVT OVT = Tmp1.getValueType();
3733 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003734
3735 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003736 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3737 // Perform the larger operation, then subtract if needed.
3738 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003739 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003740 case ISD::CTPOP:
3741 Result = Tmp1;
3742 break;
3743 case ISD::CTTZ:
3744 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands8feb6942009-01-01 15:52:00 +00003745 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003746 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003747 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003748 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003749 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003750 break;
3751 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003752 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003753 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003754 DAG.getConstant(NVT.getSizeInBits() -
3755 OVT.getSizeInBits(), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003756 break;
3757 }
3758 break;
3759 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003760 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003761 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003762 break;
3763 }
3764 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003765
Chris Lattner13fe99c2005-04-02 05:00:07 +00003766 // Unary operators
3767 case ISD::FABS:
3768 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003769 case ISD::FSQRT:
3770 case ISD::FSIN:
3771 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00003772 case ISD::FLOG:
3773 case ISD::FLOG2:
3774 case ISD::FLOG10:
3775 case ISD::FEXP:
3776 case ISD::FEXP2:
Dan Gohmanc6337ac2008-08-21 17:55:02 +00003777 case ISD::FTRUNC:
3778 case ISD::FFLOOR:
3779 case ISD::FCEIL:
3780 case ISD::FRINT:
3781 case ISD::FNEARBYINT:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003782 Tmp1 = LegalizeOp(Node->getOperand(0));
3783 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003784 case TargetLowering::Promote:
3785 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003786 isCustom = true;
3787 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003788 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003789 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003790 if (isCustom) {
3791 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003792 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng2443ab92006-01-31 18:14:25 +00003793 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003794 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003795 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003796 switch (Node->getOpcode()) {
3797 default: assert(0 && "Unreachable!");
3798 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003799 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3800 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003801 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003802 break;
Chris Lattner80026402005-04-30 04:43:14 +00003803 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003804 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands13237ac2008-06-06 12:08:01 +00003805 MVT VT = Node->getValueType(0);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003806 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands8feb6942009-01-01 15:52:00 +00003807 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3808 Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003809 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3810 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003811 break;
3812 }
Evan Cheng0fff3972008-09-09 23:35:53 +00003813 case ISD::FSQRT:
3814 case ISD::FSIN:
3815 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00003816 case ISD::FLOG:
3817 case ISD::FLOG2:
3818 case ISD::FLOG10:
3819 case ISD::FEXP:
3820 case ISD::FEXP2:
Dan Gohman2af34bd2008-08-21 18:38:14 +00003821 case ISD::FTRUNC:
3822 case ISD::FFLOOR:
3823 case ISD::FCEIL:
3824 case ISD::FRINT:
Evan Cheng0fff3972008-09-09 23:35:53 +00003825 case ISD::FNEARBYINT: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003826 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003827
3828 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003829 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003830 Result = LegalizeOp(UnrollVectorOp(Op));
3831 break;
3832 }
3833
Evan Cheng31cbddf2007-01-12 02:11:51 +00003834 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003835 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003836 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003837 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3838 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003839 break;
3840 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003841 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3842 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003843 break;
3844 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003845 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3846 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003847 break;
Dale Johannesenda2d8062008-09-04 00:47:13 +00003848 case ISD::FLOG:
3849 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3850 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3851 break;
3852 case ISD::FLOG2:
3853 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3854 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3855 break;
3856 case ISD::FLOG10:
3857 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3858 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3859 break;
3860 case ISD::FEXP:
3861 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3862 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3863 break;
3864 case ISD::FEXP2:
3865 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3866 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3867 break;
Dan Gohman2af34bd2008-08-21 18:38:14 +00003868 case ISD::FTRUNC:
3869 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3870 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3871 break;
3872 case ISD::FFLOOR:
3873 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3874 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3875 break;
3876 case ISD::FCEIL:
3877 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3878 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3879 break;
3880 case ISD::FRINT:
3881 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3882 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3883 break;
3884 case ISD::FNEARBYINT:
3885 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3886 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3887 break;
Evan Cheng0fff3972008-09-09 23:35:53 +00003888 break;
Chris Lattner80026402005-04-30 04:43:14 +00003889 default: assert(0 && "Unreachable!");
3890 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003891 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003892 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003893 break;
3894 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003895 }
3896 break;
3897 }
3898 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003899 case ISD::FPOWI: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003900 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003901
3902 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003903 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003904 Result = LegalizeOp(UnrollVectorOp(Op));
3905 break;
3906 }
3907
3908 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003909 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3910 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003911 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003912 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003913 break;
3914 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003915 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003916 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003917 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3918 Node->getValueType(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00003919 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +00003920 // The input has to be a vector type, we have to either scalarize it, pack
3921 // it, or convert it based on whether the input vector type is legal.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003922 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00003923 int InIx = Node->getOperand(0).getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00003924 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3925 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmana8665142007-06-25 16:23:39 +00003926
3927 // Figure out if there is a simple type corresponding to this Vector
3928 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00003929 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00003930 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003931 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003932 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3933 LegalizeOp(Node->getOperand(0)));
3934 break;
3935 } else if (NumElems == 1) {
3936 // Turn this into a bit convert of the scalar input.
3937 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3938 ScalarizeVectorOp(Node->getOperand(0)));
3939 break;
3940 } else {
3941 // FIXME: UNIMP! Store then reload
3942 assert(0 && "Cast from unsupported vector type not implemented yet!");
3943 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003944 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003945 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3946 Node->getOperand(0).getValueType())) {
3947 default: assert(0 && "Unknown operation action!");
3948 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003949 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3950 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003951 break;
3952 case TargetLowering::Legal:
3953 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003954 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003955 break;
3956 }
3957 }
3958 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00003959 case ISD::CONVERT_RNDSAT: {
3960 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3961 switch (CvtCode) {
3962 default: assert(0 && "Unknown cvt code!");
3963 case ISD::CVT_SF:
3964 case ISD::CVT_UF:
Mon P Wang58fb9132008-11-10 20:54:11 +00003965 case ISD::CVT_FF:
Mon P Wang8a5366332008-12-09 07:27:39 +00003966 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00003967 case ISD::CVT_FS:
3968 case ISD::CVT_FU:
3969 case ISD::CVT_SS:
3970 case ISD::CVT_SU:
3971 case ISD::CVT_US:
3972 case ISD::CVT_UU: {
3973 SDValue DTyOp = Node->getOperand(1);
3974 SDValue STyOp = Node->getOperand(2);
3975 SDValue RndOp = Node->getOperand(3);
3976 SDValue SatOp = Node->getOperand(4);
3977 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3978 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3979 case Legal:
3980 Tmp1 = LegalizeOp(Node->getOperand(0));
3981 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3982 RndOp, SatOp);
3983 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3984 TargetLowering::Custom) {
3985 Tmp1 = TLI.LowerOperation(Result, DAG);
3986 if (Tmp1.getNode()) Result = Tmp1;
3987 }
3988 break;
3989 case Promote:
3990 Result = PromoteOp(Node->getOperand(0));
3991 // For FP, make Op1 a i32
3992
Mon P Wang8a5366332008-12-09 07:27:39 +00003993 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang58fb9132008-11-10 20:54:11 +00003994 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3995 break;
3996 }
3997 break;
3998 }
3999 } // end switch CvtCode
4000 break;
4001 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00004002 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00004003 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004004 case ISD::UINT_TO_FP: {
4005 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman550c9af2008-08-14 20:04:46 +00004006 Result = LegalizeINT_TO_FP(Result, isSigned,
4007 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004008 break;
4009 }
4010 case ISD::TRUNCATE:
4011 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4012 case Legal:
4013 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel9b0b28e2008-12-02 19:55:08 +00004014 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4015 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4016 case TargetLowering::Custom:
Mon P Wangc68b3c42008-12-11 00:44:22 +00004017 isCustom = true;
4018 // FALLTHROUGH
Scott Michel9b0b28e2008-12-02 19:55:08 +00004019 case TargetLowering::Legal:
Mon P Wangc68b3c42008-12-11 00:44:22 +00004020 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4021 if (isCustom) {
4022 Tmp1 = TLI.LowerOperation(Result, DAG);
4023 if (Tmp1.getNode()) Result = Tmp1;
4024 }
4025 break;
Mon P Wang9c2d26d2008-12-12 01:25:51 +00004026 case TargetLowering::Expand:
4027 assert(Result.getValueType().isVector() && "must be vector type");
4028 // Unroll the truncate. We should do better.
4029 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Scheller318ccb0e2008-12-02 12:12:25 +00004030 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004031 break;
4032 case Expand:
4033 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4034
4035 // Since the result is legal, we should just be able to truncate the low
4036 // part of the source.
4037 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4038 break;
4039 case Promote:
4040 Result = PromoteOp(Node->getOperand(0));
4041 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4042 break;
4043 }
4044 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004045
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004046 case ISD::FP_TO_SINT:
4047 case ISD::FP_TO_UINT:
4048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4049 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00004050 Tmp1 = LegalizeOp(Node->getOperand(0));
4051
Chris Lattner44fe26f2005-07-29 00:11:56 +00004052 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4053 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004054 case TargetLowering::Custom:
4055 isCustom = true;
4056 // FALLTHROUGH
4057 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004058 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004059 if (isCustom) {
4060 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004061 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004062 }
4063 break;
4064 case TargetLowering::Promote:
4065 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4066 Node->getOpcode() == ISD::FP_TO_SINT);
4067 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00004068 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00004069 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004070 SDValue True, False;
Duncan Sands13237ac2008-06-06 12:08:01 +00004071 MVT VT = Node->getOperand(0).getValueType();
4072 MVT NVT = Node->getValueType(0);
Dale Johannesen7d67e542007-09-19 23:55:34 +00004073 const uint64_t zero[] = {0, 0};
Duncan Sands13237ac2008-06-06 12:08:01 +00004074 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4075 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman837a6dc2008-02-29 01:44:25 +00004076 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00004077 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands8feb6942009-01-01 15:52:00 +00004078 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4079 Tmp2, ISD::SETLT);
Nate Begeman36853ee2005-08-14 01:20:53 +00004080 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4081 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00004082 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00004083 Tmp2));
4084 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman837a6dc2008-02-29 01:44:25 +00004085 DAG.getConstant(x, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004086 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4087 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00004088 } else {
4089 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4090 }
4091 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004092 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004093 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00004094 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004095 MVT VT = Op.getValueType();
4096 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00004097 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00004098 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00004099 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4100 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4101 Node->getOperand(0), DAG.getValueType(MVT::f64));
4102 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4103 DAG.getIntPtrConstant(1));
4104 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4105 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00004106 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4107 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4108 Tmp2 = DAG.getConstantFP(apf, OVT);
4109 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4110 // FIXME: generated code sucks.
4111 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4112 DAG.getNode(ISD::ADD, MVT::i32,
4113 DAG.getNode(ISD::FP_TO_SINT, VT,
4114 DAG.getNode(ISD::FSUB, OVT,
4115 Node->getOperand(0), Tmp2)),
4116 DAG.getConstant(0x80000000, MVT::i32)),
4117 DAG.getNode(ISD::FP_TO_SINT, VT,
4118 Node->getOperand(0)),
4119 DAG.getCondCode(ISD::SETGE));
4120 }
Dale Johannesen666323e2007-10-10 01:01:31 +00004121 break;
4122 }
Dan Gohmanf4300952008-03-10 23:03:31 +00004123 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sands77a3d052008-07-17 02:36:29 +00004124 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4125 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4126 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004127 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00004128 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00004129 break;
4130 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004131 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004132 Tmp1 = PromoteOp(Node->getOperand(0));
4133 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4134 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004135 break;
4136 }
4137 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004138
Chris Lattner91d86242008-01-16 06:57:07 +00004139 case ISD::FP_EXTEND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004140 MVT DstVT = Op.getValueType();
4141 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00004142 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4143 // The only other way we can lower this is to turn it into a STORE,
4144 // LOAD pair, targetting a temporary location (a stack slot).
4145 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4146 break;
Chris Lattner91d86242008-01-16 06:57:07 +00004147 }
4148 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4149 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4150 case Legal:
4151 Tmp1 = LegalizeOp(Node->getOperand(0));
4152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4153 break;
4154 case Promote:
4155 Tmp1 = PromoteOp(Node->getOperand(0));
4156 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4157 break;
4158 }
4159 break;
Chris Lattner72733e52008-01-17 07:00:52 +00004160 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00004161 case ISD::FP_ROUND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004162 MVT DstVT = Op.getValueType();
4163 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00004164 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4165 if (SrcVT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004166 SDValue Lo;
Dale Johannesen949e5a22008-01-20 01:18:38 +00004167 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00004168 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00004169 if (DstVT!=MVT::f64)
4170 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00004171 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00004172 }
Chris Lattner72733e52008-01-17 07:00:52 +00004173 // The only other way we can lower this is to turn it into a STORE,
4174 // LOAD pair, targetting a temporary location (a stack slot).
4175 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4176 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00004177 }
Chris Lattner91d86242008-01-16 06:57:07 +00004178 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4179 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4180 case Legal:
4181 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00004182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00004183 break;
4184 case Promote:
4185 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00004186 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4187 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00004188 break;
4189 }
4190 break;
Chris Lattner72733e52008-01-17 07:00:52 +00004191 }
Chris Lattner7753f172005-09-02 00:18:10 +00004192 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004193 case ISD::ZERO_EXTEND:
4194 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004195 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004196 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004197 case Legal:
4198 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelbe940422008-04-30 00:26:38 +00004199 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michela3cefea2008-02-15 23:05:48 +00004200 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4201 TargetLowering::Custom) {
Scott Michelbe940422008-04-30 00:26:38 +00004202 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004203 if (Tmp1.getNode()) Result = Tmp1;
Scott Michela3cefea2008-02-15 23:05:48 +00004204 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004205 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004206 case Promote:
4207 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00004208 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004209 Tmp1 = PromoteOp(Node->getOperand(0));
4210 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00004211 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004212 case ISD::ZERO_EXTEND:
4213 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00004214 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00004215 Result = DAG.getZeroExtendInReg(Result,
4216 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004217 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004218 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004219 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00004220 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004221 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004222 Result,
4223 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004224 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004225 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004226 }
4227 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004228 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00004229 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004230 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00004231 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00004232
4233 // If this operation is not supported, convert it to a shl/shr or load/store
4234 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00004235 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4236 default: assert(0 && "This action not supported for this op yet!");
4237 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00004239 break;
4240 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00004241 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00004242 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00004243 // NOTE: we could fall back on load/store here too for targets without
4244 // SAR. However, it is doubtful that any exist.
Duncan Sands13237ac2008-06-06 12:08:01 +00004245 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4246 ExtraVT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004247 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00004248 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4249 Node->getOperand(0), ShiftCst);
4250 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4251 Result, ShiftCst);
4252 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00004253 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00004254 // EXTLOAD pair, targetting a temporary location (a stack slot).
4255
4256 // NOTE: there is a choice here between constantly creating new stack
4257 // slots and always reusing the same one. We currently always create
4258 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00004259 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4260 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00004261 } else {
4262 assert(0 && "Unknown op");
4263 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00004264 break;
Chris Lattner99222f72005-01-15 07:15:18 +00004265 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004266 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004267 }
Duncan Sands644f9172007-07-27 12:58:54 +00004268 case ISD::TRAMPOLINE: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004269 SDValue Ops[6];
Duncan Sands644f9172007-07-27 12:58:54 +00004270 for (unsigned i = 0; i != 6; ++i)
4271 Ops[i] = LegalizeOp(Node->getOperand(i));
4272 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4273 // The only option for this node is to custom lower it.
4274 Result = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004275 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00004276
4277 // Since trampoline produces two values, make sure to remember that we
4278 // legalized both of them.
4279 Tmp1 = LegalizeOp(Result.getValue(1));
4280 Result = LegalizeOp(Result);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004281 AddLegalizedOperand(SDValue(Node, 0), Result);
4282 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greifabfdf922008-08-26 22:36:50 +00004283 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00004284 }
Dan Gohmanfd3e3002008-05-14 00:43:10 +00004285 case ISD::FLT_ROUNDS_: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004286 MVT VT = Node->getValueType(0);
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004287 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4288 default: assert(0 && "This action not supported for this op yet!");
4289 case TargetLowering::Custom:
4290 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004291 if (Result.getNode()) break;
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004292 // Fall Thru
4293 case TargetLowering::Legal:
4294 // If this operation is not supported, lower it to constant 1
4295 Result = DAG.getConstant(1, VT);
4296 break;
4297 }
Dan Gohmanecb77382008-05-12 16:07:15 +00004298 break;
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004299 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004300 case ISD::TRAP: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004301 MVT VT = Node->getValueType(0);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004302 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4303 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00004304 case TargetLowering::Legal:
4305 Tmp1 = LegalizeOp(Node->getOperand(0));
4306 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4307 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004308 case TargetLowering::Custom:
4309 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004310 if (Result.getNode()) break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004311 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00004312 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004313 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00004314 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004315 TargetLowering::ArgListTy Args;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004316 std::pair<SDValue,SDValue> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00004317 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen0e32a2c2008-09-26 19:31:26 +00004318 false, false, false, false, CallingConv::C, false,
Bill Wendling24c79f22008-09-16 21:48:12 +00004319 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattnerec224882008-01-15 22:09:33 +00004320 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004321 Result = CallResult.second;
4322 break;
4323 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004324 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004325 }
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004326
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004327 case ISD::SADDO:
4328 case ISD::SSUBO: {
Bill Wendling4498b472008-11-25 08:12:19 +00004329 MVT VT = Node->getValueType(0);
4330 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4331 default: assert(0 && "This action not supported for this op yet!");
4332 case TargetLowering::Custom:
4333 Result = TLI.LowerOperation(Op, DAG);
4334 if (Result.getNode()) break;
4335 // FALLTHROUGH
4336 case TargetLowering::Legal: {
4337 SDValue LHS = LegalizeOp(Node->getOperand(0));
4338 SDValue RHS = LegalizeOp(Node->getOperand(1));
4339
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004340 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4341 ISD::ADD : ISD::SUB, LHS.getValueType(),
4342 LHS, RHS);
Bill Wendling4498b472008-11-25 08:12:19 +00004343 MVT OType = Node->getValueType(1);
4344
Bill Wendlingbf592fc2008-11-25 08:19:22 +00004345 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling4498b472008-11-25 08:12:19 +00004346
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004347 // LHSSign -> LHS >= 0
4348 // RHSSign -> RHS >= 0
4349 // SumSign -> Sum >= 0
4350 //
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004351 // Add:
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004352 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004353 // Sub:
4354 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004355 //
4356 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4357 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004358 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4359 Node->getOpcode() == ISD::SADDO ?
4360 ISD::SETEQ : ISD::SETNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004361
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004362 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4363 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004364
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004365 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004366
4367 MVT ValueVTs[] = { LHS.getValueType(), OType };
4368 SDValue Ops[] = { Sum, Cmp };
4369
Duncan Sands3d960942008-12-01 11:41:29 +00004370 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4371 &Ops[0], 2);
Bill Wendling4498b472008-11-25 08:12:19 +00004372 SDNode *RNode = Result.getNode();
4373 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4374 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4375 break;
4376 }
4377 }
4378
4379 break;
4380 }
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004381 case ISD::UADDO:
4382 case ISD::USUBO: {
Bill Wendling66835472008-11-24 19:21:46 +00004383 MVT VT = Node->getValueType(0);
4384 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4385 default: assert(0 && "This action not supported for this op yet!");
4386 case TargetLowering::Custom:
4387 Result = TLI.LowerOperation(Op, DAG);
4388 if (Result.getNode()) break;
4389 // FALLTHROUGH
4390 case TargetLowering::Legal: {
4391 SDValue LHS = LegalizeOp(Node->getOperand(0));
4392 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004393
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004394 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4395 ISD::ADD : ISD::SUB, LHS.getValueType(),
4396 LHS, RHS);
Bill Wendling66835472008-11-24 19:21:46 +00004397 MVT OType = Node->getValueType(1);
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004398 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4399 Node->getOpcode () == ISD::UADDO ?
4400 ISD::SETULT : ISD::SETUGT);
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004401
Bill Wendling66835472008-11-24 19:21:46 +00004402 MVT ValueVTs[] = { LHS.getValueType(), OType };
4403 SDValue Ops[] = { Sum, Cmp };
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004404
Duncan Sands3d960942008-12-01 11:41:29 +00004405 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4406 &Ops[0], 2);
Bill Wendling66835472008-11-24 19:21:46 +00004407 SDNode *RNode = Result.getNode();
4408 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4409 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4410 break;
4411 }
4412 }
4413
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004414 break;
4415 }
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004416 case ISD::SMULO:
4417 case ISD::UMULO: {
4418 MVT VT = Node->getValueType(0);
4419 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4420 default: assert(0 && "This action is not supported at all!");
4421 case TargetLowering::Custom:
4422 Result = TLI.LowerOperation(Op, DAG);
4423 if (Result.getNode()) break;
4424 // Fall Thru
4425 case TargetLowering::Legal:
4426 // FIXME: According to Hacker's Delight, this can be implemented in
4427 // target independent lowering, but it would be inefficient, since it
Bill Wendlingf482f372008-12-10 02:01:32 +00004428 // requires a division + a branch.
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004429 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4430 break;
4431 }
4432 break;
4433 }
4434
Chris Lattner99222f72005-01-15 07:15:18 +00004435 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004436
Chris Lattner101ea662006-04-08 04:13:17 +00004437 assert(Result.getValueType() == Op.getValueType() &&
4438 "Bad legalization!");
4439
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004440 // Make sure that the generated code is itself legal.
4441 if (Result != Op)
4442 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004443
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004444 // Note that LegalizeOp may be reentered even from single-use nodes, which
4445 // means that we always must cache transformed nodes.
4446 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004447 return Result;
4448}
4449
Chris Lattner4d978642005-01-15 22:16:26 +00004450/// PromoteOp - Given an operation that produces a value in an invalid type,
4451/// promote it to compute the value into a larger type. The produced value will
4452/// have the correct bits for the low portion of the register, but no guarantee
4453/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004454SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00004455 MVT VT = Op.getValueType();
4456 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004457 assert(getTypeAction(VT) == Promote &&
4458 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands11dd4242008-06-08 20:54:56 +00004459 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004460 "Cannot promote to smaller type!");
4461
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004462 SDValue Tmp1, Tmp2, Tmp3;
4463 SDValue Result;
Gabor Greiff304a7a2008-08-28 21:40:38 +00004464 SDNode *Node = Op.getNode();
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004465
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004466 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004467 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004468
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004469 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004470 case ISD::CopyFromReg:
4471 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004472 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004473#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004474 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004475#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004476 assert(0 && "Do not know how to promote this operator!");
4477 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004478 case ISD::UNDEF:
4479 Result = DAG.getNode(ISD::UNDEF, NVT);
4480 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004481 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004482 if (VT != MVT::i1)
4483 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4484 else
4485 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004486 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4487 break;
4488 case ISD::ConstantFP:
4489 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4490 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4491 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004492
Duncan Sands8feb6942009-01-01 15:52:00 +00004493 case ISD::SETCC: {
4494 MVT VT0 = Node->getOperand(0).getValueType();
4495 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman63eb03f2008-03-14 00:53:31 +00004496 && "SetCC type is not legal??");
Duncan Sands8feb6942009-01-01 15:52:00 +00004497 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman63eb03f2008-03-14 00:53:31 +00004498 Node->getOperand(0), Node->getOperand(1),
4499 Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004500 break;
Duncan Sands8feb6942009-01-01 15:52:00 +00004501 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004502 case ISD::TRUNCATE:
4503 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4504 case Legal:
4505 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00004506 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004507 "This truncation doesn't make sense!");
Duncan Sands11dd4242008-06-08 20:54:56 +00004508 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004509 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4510 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004511 case Promote:
4512 // The truncation is not required, because we don't guarantee anything
4513 // about high bits anyway.
4514 Result = PromoteOp(Node->getOperand(0));
4515 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004516 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004517 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4518 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004519 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004520 }
4521 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004522 case ISD::SIGN_EXTEND:
4523 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004524 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004525 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4526 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4527 case Legal:
4528 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004529 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004530 break;
4531 case Promote:
4532 // Promote the reg if it's smaller.
4533 Result = PromoteOp(Node->getOperand(0));
4534 // The high bits are not guaranteed to be anything. Insert an extend.
4535 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004536 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004537 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004538 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004539 Result = DAG.getZeroExtendInReg(Result,
4540 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004541 break;
4542 }
4543 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00004544 case ISD::CONVERT_RNDSAT: {
4545 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4546 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4547 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4548 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4549 "can only promote integers");
4550 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4551 Node->getOperand(1), Node->getOperand(2),
4552 Node->getOperand(3), Node->getOperand(4),
4553 CvtCode);
4554 break;
4555
4556 }
Chris Lattner36e663d2005-12-23 00:16:34 +00004557 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004558 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4559 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004560 Result = PromoteOp(Result);
4561 break;
4562
Chris Lattner4d978642005-01-15 22:16:26 +00004563 case ISD::FP_EXTEND:
4564 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4565 case ISD::FP_ROUND:
4566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4567 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4568 case Promote: assert(0 && "Unreachable with 2 FP types!");
4569 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004570 if (Node->getConstantOperandVal(1) == 0) {
4571 // Input is legal? Do an FP_ROUND_INREG.
4572 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4573 DAG.getValueType(VT));
4574 } else {
4575 // Just remove the truncate, it isn't affecting the value.
4576 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4577 Node->getOperand(1));
4578 }
Chris Lattner4d978642005-01-15 22:16:26 +00004579 break;
4580 }
4581 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004582 case ISD::SINT_TO_FP:
4583 case ISD::UINT_TO_FP:
4584 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4585 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004586 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004587 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004588 break;
4589
4590 case Promote:
4591 Result = PromoteOp(Node->getOperand(0));
4592 if (Node->getOpcode() == ISD::SINT_TO_FP)
4593 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004594 Result,
4595 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004596 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004597 Result = DAG.getZeroExtendInReg(Result,
4598 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004599 // No extra round required here.
4600 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004601 break;
4602 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004603 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4604 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004605 // Round if we cannot tolerate excess precision.
4606 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004607 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4608 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004609 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004610 }
Chris Lattner4d978642005-01-15 22:16:26 +00004611 break;
4612
Chris Lattner268d4572005-12-09 17:32:47 +00004613 case ISD::SIGN_EXTEND_INREG:
4614 Result = PromoteOp(Node->getOperand(0));
4615 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4616 Node->getOperand(1));
4617 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004618 case ISD::FP_TO_SINT:
4619 case ISD::FP_TO_UINT:
4620 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4621 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004622 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004623 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004624 break;
4625 case Promote:
4626 // The input result is prerounded, so we don't have to do anything
4627 // special.
4628 Tmp1 = PromoteOp(Node->getOperand(0));
4629 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004630 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004631 // If we're promoting a UINT to a larger size, check to see if the new node
4632 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4633 // we can use that instead. This allows us to generate better code for
4634 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4635 // legal, such as PowerPC.
4636 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004637 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004638 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4639 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004640 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4641 } else {
4642 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4643 }
Chris Lattner4d978642005-01-15 22:16:26 +00004644 break;
4645
Chris Lattner13fe99c2005-04-02 05:00:07 +00004646 case ISD::FABS:
4647 case ISD::FNEG:
4648 Tmp1 = PromoteOp(Node->getOperand(0));
4649 assert(Tmp1.getValueType() == NVT);
4650 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4651 // NOTE: we do not have to do any extra rounding here for
4652 // NoExcessFPPrecision, because we know the input will have the appropriate
4653 // precision, and these operations don't modify precision at all.
4654 break;
4655
Dale Johannesenda2d8062008-09-04 00:47:13 +00004656 case ISD::FLOG:
4657 case ISD::FLOG2:
4658 case ISD::FLOG10:
4659 case ISD::FEXP:
4660 case ISD::FEXP2:
Chris Lattner9d6fa982005-04-28 21:44:33 +00004661 case ISD::FSQRT:
4662 case ISD::FSIN:
4663 case ISD::FCOS:
Dan Gohman2af34bd2008-08-21 18:38:14 +00004664 case ISD::FTRUNC:
4665 case ISD::FFLOOR:
4666 case ISD::FCEIL:
4667 case ISD::FRINT:
4668 case ISD::FNEARBYINT:
Chris Lattner9d6fa982005-04-28 21:44:33 +00004669 Tmp1 = PromoteOp(Node->getOperand(0));
4670 assert(Tmp1.getValueType() == NVT);
4671 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004672 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004673 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4674 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004675 break;
4676
Evan Cheng0fff3972008-09-09 23:35:53 +00004677 case ISD::FPOW:
Chris Lattnerca401aa2007-03-03 23:43:21 +00004678 case ISD::FPOWI: {
Evan Cheng0fff3972008-09-09 23:35:53 +00004679 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattnerca401aa2007-03-03 23:43:21 +00004680 // directly as well, which may be better.
4681 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng0fff3972008-09-09 23:35:53 +00004682 Tmp2 = Node->getOperand(1);
4683 if (Node->getOpcode() == ISD::FPOW)
4684 Tmp2 = PromoteOp(Tmp2);
Chris Lattnerca401aa2007-03-03 23:43:21 +00004685 assert(Tmp1.getValueType() == NVT);
Evan Cheng0fff3972008-09-09 23:35:53 +00004686 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattnerca401aa2007-03-03 23:43:21 +00004687 if (NoExcessFPPrecision)
4688 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4689 DAG.getValueType(VT));
4690 break;
4691 }
4692
Dan Gohman12f24902008-12-23 21:37:04 +00004693 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang6a490372008-06-25 08:15:39 +00004694 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004695 Tmp2 = PromoteOp(Node->getOperand(2));
4696 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohman12f24902008-12-23 21:37:04 +00004697 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4698 AtomNode->getChain(),
Mon P Wang6a490372008-06-25 08:15:39 +00004699 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004700 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004701 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004702 // Remember that we legalized the chain.
4703 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4704 break;
4705 }
Dan Gohman12f24902008-12-23 21:37:04 +00004706 case ISD::ATOMIC_LOAD_ADD:
4707 case ISD::ATOMIC_LOAD_SUB:
4708 case ISD::ATOMIC_LOAD_AND:
4709 case ISD::ATOMIC_LOAD_OR:
4710 case ISD::ATOMIC_LOAD_XOR:
4711 case ISD::ATOMIC_LOAD_NAND:
4712 case ISD::ATOMIC_LOAD_MIN:
4713 case ISD::ATOMIC_LOAD_MAX:
4714 case ISD::ATOMIC_LOAD_UMIN:
4715 case ISD::ATOMIC_LOAD_UMAX:
4716 case ISD::ATOMIC_SWAP: {
Mon P Wang6a490372008-06-25 08:15:39 +00004717 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004718 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohman12f24902008-12-23 21:37:04 +00004719 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4720 AtomNode->getChain(),
Mon P Wang6a490372008-06-25 08:15:39 +00004721 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004722 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004723 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004724 // Remember that we legalized the chain.
4725 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4726 break;
4727 }
4728
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004729 case ISD::AND:
4730 case ISD::OR:
4731 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004732 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004733 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004734 case ISD::MUL:
4735 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004736 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004737 // that too is okay if they are integer operations.
4738 Tmp1 = PromoteOp(Node->getOperand(0));
4739 Tmp2 = PromoteOp(Node->getOperand(1));
4740 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4741 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004742 break;
4743 case ISD::FADD:
4744 case ISD::FSUB:
4745 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004746 Tmp1 = PromoteOp(Node->getOperand(0));
4747 Tmp2 = PromoteOp(Node->getOperand(1));
4748 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4749 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4750
4751 // Floating point operations will give excess precision that we may not be
4752 // able to tolerate. If we DO allow excess precision, just leave it,
4753 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004754 // FIXME: Why would we need to round FP ops more than integer ones?
4755 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004756 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004757 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4758 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004759 break;
4760
Chris Lattner4d978642005-01-15 22:16:26 +00004761 case ISD::SDIV:
4762 case ISD::SREM:
4763 // These operators require that their input be sign extended.
4764 Tmp1 = PromoteOp(Node->getOperand(0));
4765 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004766 if (NVT.isInteger()) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004767 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4768 DAG.getValueType(VT));
4769 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4770 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004771 }
4772 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4773
4774 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands13237ac2008-06-06 12:08:01 +00004775 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004776 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4777 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004778 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004779 case ISD::FDIV:
4780 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004781 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004782 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004783 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004784 case Expand: assert(0 && "not implemented");
4785 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4786 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004787 }
4788 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004789 case Expand: assert(0 && "not implemented");
4790 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4791 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004792 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004793 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4794
4795 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004796 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004797 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4798 DAG.getValueType(VT));
4799 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004800
4801 case ISD::UDIV:
4802 case ISD::UREM:
4803 // These operators require that their input be zero extended.
4804 Tmp1 = PromoteOp(Node->getOperand(0));
4805 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004806 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004807 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4808 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004809 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4810 break;
4811
4812 case ISD::SHL:
4813 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004814 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004815 break;
4816 case ISD::SRA:
4817 // The input value must be properly sign extended.
4818 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004819 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4820 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004821 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004822 break;
4823 case ISD::SRL:
4824 // The input value must be properly zero extended.
4825 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004826 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004827 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004828 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004829
4830 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004831 Tmp1 = Node->getOperand(0); // Get the chain.
4832 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004833 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4834 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands93e180342008-07-04 11:47:58 +00004835 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman595ec732006-01-28 03:14:31 +00004836 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004837 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004838 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004839 // Increment the pointer, VAList, to the next vaarg
4840 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands13237ac2008-06-06 12:08:01 +00004841 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman595ec732006-01-28 03:14:31 +00004842 TLI.getPointerTy()));
4843 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004844 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004845 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004846 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004847 }
4848 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004849 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004850 break;
4851
Evan Chenge71fe34d2006-10-09 20:57:25 +00004852 case ISD::LOAD: {
4853 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004854 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4855 ? ISD::EXTLOAD : LD->getExtensionType();
4856 Result = DAG.getExtLoad(ExtType, NVT,
4857 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004858 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004859 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004860 LD->isVolatile(),
4861 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004862 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004863 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004864 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004865 }
Scott Micheld831cc42008-06-02 22:18:03 +00004866 case ISD::SELECT: {
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004867 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4868 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Micheld831cc42008-06-02 22:18:03 +00004869
Duncan Sands13237ac2008-06-06 12:08:01 +00004870 MVT VT2 = Tmp2.getValueType();
Scott Micheld831cc42008-06-02 22:18:03 +00004871 assert(VT2 == Tmp3.getValueType()
Scott Michela7d86492008-06-03 19:13:20 +00004872 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4873 // Ensure that the resulting node is at least the same size as the operands'
4874 // value types, because we cannot assume that TLI.getSetCCValueType() is
4875 // constant.
4876 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004877 break;
Scott Micheld831cc42008-06-02 22:18:03 +00004878 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004879 case ISD::SELECT_CC:
4880 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4881 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4882 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004883 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004884 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004885 case ISD::BSWAP:
4886 Tmp1 = Node->getOperand(0);
4887 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4888 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4889 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004890 DAG.getConstant(NVT.getSizeInBits() -
4891 VT.getSizeInBits(),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004892 TLI.getShiftAmountTy()));
4893 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004894 case ISD::CTPOP:
4895 case ISD::CTTZ:
4896 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004897 // Zero extend the argument
4898 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004899 // Perform the larger operation, then subtract if needed.
4900 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004901 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004902 case ISD::CTPOP:
4903 Result = Tmp1;
4904 break;
4905 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004906 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands8feb6942009-01-01 15:52:00 +00004907 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004908 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohman1796f1f2007-05-18 17:52:13 +00004909 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004910 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00004911 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004912 break;
4913 case ISD::CTLZ:
4914 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004915 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004916 DAG.getConstant(NVT.getSizeInBits() -
4917 VT.getSizeInBits(), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004918 break;
4919 }
4920 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004921 case ISD::EXTRACT_SUBVECTOR:
4922 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004923 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004924 case ISD::EXTRACT_VECTOR_ELT:
4925 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4926 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004927 }
4928
Gabor Greiff304a7a2008-08-28 21:40:38 +00004929 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004930
4931 // Make sure the result is itself legal.
4932 Result = LegalizeOp(Result);
4933
4934 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004935 AddPromotedOperand(Op, Result);
4936 return Result;
4937}
Chris Lattnerdc750592005-01-07 07:47:09 +00004938
Dan Gohmana8665142007-06-25 16:23:39 +00004939/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4940/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4941/// based on the vector type. The return type of this matches the element type
4942/// of the vector, which may not be legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004943SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004944 // We know that operand #0 is the Vec vector. If the index is a constant
4945 // or if the invec is a supported hardware type, we can use it. Otherwise,
4946 // lower to a store then an indexed load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004947 SDValue Vec = Op.getOperand(0);
4948 SDValue Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004949
Duncan Sands13237ac2008-06-06 12:08:01 +00004950 MVT TVT = Vec.getValueType();
4951 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner6f423252006-03-31 17:55:51 +00004952
Dan Gohmana8665142007-06-25 16:23:39 +00004953 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4954 default: assert(0 && "This action is not supported yet!");
4955 case TargetLowering::Custom: {
4956 Vec = LegalizeOp(Vec);
4957 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004958 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004959 if (Tmp3.getNode())
Dan Gohmana8665142007-06-25 16:23:39 +00004960 return Tmp3;
4961 break;
4962 }
4963 case TargetLowering::Legal:
4964 if (isTypeLegal(TVT)) {
4965 Vec = LegalizeOp(Vec);
4966 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004967 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004968 }
4969 break;
Mon P Wang58c37942008-10-30 08:01:45 +00004970 case TargetLowering::Promote:
4971 assert(TVT.isVector() && "not vector type");
4972 // fall thru to expand since vectors are by default are promote
Dan Gohmana8665142007-06-25 16:23:39 +00004973 case TargetLowering::Expand:
4974 break;
4975 }
4976
4977 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004978 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004979 Op = ScalarizeVectorOp(Vec);
4980 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004981 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004982 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004983 SDValue Lo, Hi;
Chris Lattner6f423252006-03-31 17:55:51 +00004984 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00004985 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004986 Vec = Lo;
4987 } else {
4988 Vec = Hi;
Dan Gohmaneffb8942008-09-12 16:56:44 +00004989 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004990 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004991 }
Dan Gohmana8665142007-06-25 16:23:39 +00004992
Chris Lattner6f423252006-03-31 17:55:51 +00004993 // It's now an extract from the appropriate high or low part. Recurse.
4994 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004995 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004996 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004997 // Store the value to a temporary stack slot, then LOAD the scalar
4998 // element back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004999 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5000 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmana8665142007-06-25 16:23:39 +00005001
5002 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +00005003 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmana8665142007-06-25 16:23:39 +00005004 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5005 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00005006
Duncan Sands11dd4242008-06-08 20:54:56 +00005007 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00005008 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00005009 else
Chris Lattner064c31e2007-10-19 16:47:35 +00005010 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00005011
Dan Gohmana8665142007-06-25 16:23:39 +00005012 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5013
5014 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00005015 }
Dan Gohmana8665142007-06-25 16:23:39 +00005016 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00005017}
5018
Dan Gohmana8665142007-06-25 16:23:39 +00005019/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00005020/// we assume the operation can be split if it is not already legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005021SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00005022 // We know that operand #0 is the Vec vector. For now we assume the index
5023 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005024 SDValue Vec = Op.getOperand(0);
5025 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman26455c42007-06-13 15:12:02 +00005026
Duncan Sands13237ac2008-06-06 12:08:01 +00005027 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman26455c42007-06-13 15:12:02 +00005028
Duncan Sands13237ac2008-06-06 12:08:01 +00005029 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman26455c42007-06-13 15:12:02 +00005030 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00005031 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00005032 }
5033
5034 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005035 SDValue Lo, Hi;
Dan Gohman26455c42007-06-13 15:12:02 +00005036 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00005037 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman26455c42007-06-13 15:12:02 +00005038 Vec = Lo;
5039 } else {
5040 Vec = Hi;
Dan Gohmaneffb8942008-09-12 16:56:44 +00005041 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5042 Idx.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00005043 }
5044
5045 // It's now an extract from the appropriate high or low part. Recurse.
5046 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00005047 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00005048}
5049
Nate Begeman7e7f4392006-02-01 07:19:44 +00005050/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5051/// with condition CC on the current target. This usually involves legalizing
5052/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5053/// there may be no choice but to create a new SetCC node to represent the
5054/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005055/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5056void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5057 SDValue &RHS,
5058 SDValue &CC) {
5059 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00005060
5061 switch (getTypeAction(LHS.getValueType())) {
5062 case Legal:
5063 Tmp1 = LegalizeOp(LHS); // LHS
5064 Tmp2 = LegalizeOp(RHS); // RHS
5065 break;
5066 case Promote:
5067 Tmp1 = PromoteOp(LHS); // LHS
5068 Tmp2 = PromoteOp(RHS); // RHS
5069
5070 // If this is an FP compare, the operands have already been extended.
Duncan Sands13237ac2008-06-06 12:08:01 +00005071 if (LHS.getValueType().isInteger()) {
5072 MVT VT = LHS.getValueType();
5073 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman7e7f4392006-02-01 07:19:44 +00005074
5075 // Otherwise, we have to insert explicit sign or zero extends. Note
5076 // that we could insert sign extends for ALL conditions, but zero extend
5077 // is cheaper on many machines (an AND instead of two shifts), so prefer
5078 // it.
5079 switch (cast<CondCodeSDNode>(CC)->get()) {
5080 default: assert(0 && "Unknown integer comparison!");
5081 case ISD::SETEQ:
5082 case ISD::SETNE:
5083 case ISD::SETUGE:
5084 case ISD::SETUGT:
5085 case ISD::SETULE:
5086 case ISD::SETULT:
5087 // ALL of these operations will work if we either sign or zero extend
5088 // the operands (including the unsigned comparisons!). Zero extend is
5089 // usually a simpler/cheaper operation, so prefer it.
5090 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5091 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5092 break;
5093 case ISD::SETGE:
5094 case ISD::SETGT:
5095 case ISD::SETLT:
5096 case ISD::SETLE:
5097 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5098 DAG.getValueType(VT));
5099 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5100 DAG.getValueType(VT));
Evan Chengda9b7522008-10-13 18:46:18 +00005101 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5102 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman7e7f4392006-02-01 07:19:44 +00005103 break;
5104 }
5105 }
5106 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005107 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00005108 MVT VT = LHS.getValueType();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005109 if (VT == MVT::f32 || VT == MVT::f64) {
5110 // Expand into one or more soft-fp libcall(s).
Evan Cheng4c609ab2008-07-01 21:35:46 +00005111 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005112 switch (cast<CondCodeSDNode>(CC)->get()) {
5113 case ISD::SETEQ:
5114 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005115 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005116 break;
5117 case ISD::SETNE:
5118 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005119 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005120 break;
5121 case ISD::SETGE:
5122 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005123 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005124 break;
5125 case ISD::SETLT:
5126 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005127 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005128 break;
5129 case ISD::SETLE:
5130 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005131 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005132 break;
5133 case ISD::SETGT:
5134 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005135 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005136 break;
5137 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00005138 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5139 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005140 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00005141 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005142 break;
5143 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005144 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005145 switch (cast<CondCodeSDNode>(CC)->get()) {
5146 case ISD::SETONE:
5147 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00005148 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005149 // Fallthrough
5150 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005151 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005152 break;
5153 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005154 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005155 break;
5156 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005157 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005158 break;
5159 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005160 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005161 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005162 case ISD::SETUEQ:
5163 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005164 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005165 default: assert(0 && "Unsupported FP setcc!");
5166 }
5167 }
Duncan Sands1ae6ef82008-06-30 10:19:09 +00005168
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005169 SDValue Dummy;
5170 SDValue Ops[2] = { LHS, RHS };
Gabor Greiff304a7a2008-08-28 21:40:38 +00005171 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencer791864c2007-01-03 04:22:32 +00005172 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005173 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00005174 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00005175 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands8feb6942009-01-01 15:52:00 +00005176 Tmp1 = DAG.getNode(ISD::SETCC,
5177 TLI.getSetCCResultType(Tmp1.getValueType()),
5178 Tmp1, Tmp2, CC);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005179 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencer791864c2007-01-03 04:22:32 +00005180 false /*sign irrelevant*/, Dummy);
Duncan Sands8feb6942009-01-01 15:52:00 +00005181 Tmp2 = DAG.getNode(ISD::SETCC,
5182 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5183 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005184 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005185 Tmp2 = SDValue();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005186 }
Evan Chengd8b83e12008-07-07 07:18:09 +00005187 LHS = LegalizeOp(Tmp1);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005188 RHS = Tmp2;
5189 return;
5190 }
5191
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005192 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman7e7f4392006-02-01 07:19:44 +00005193 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005194 ExpandOp(RHS, RHSLo, RHSHi);
5195 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5196
5197 if (VT==MVT::ppcf128) {
5198 // FIXME: This generated code sucks. We want to generate
Dale Johannesenbaf67622008-09-12 00:30:56 +00005199 // FCMPU crN, hi1, hi2
Dale Johannesenf864ac92007-10-06 01:24:11 +00005200 // BNE crN, L:
Dale Johannesenbaf67622008-09-12 00:30:56 +00005201 // FCMPU crN, lo1, lo2
Dale Johannesenf864ac92007-10-06 01:24:11 +00005202 // The following can be improved, but not that much.
Duncan Sands8feb6942009-01-01 15:52:00 +00005203 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5204 LHSHi, RHSHi, ISD::SETOEQ);
5205 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5206 LHSLo, RHSLo, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005207 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands8feb6942009-01-01 15:52:00 +00005208 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5209 LHSHi, RHSHi, ISD::SETUNE);
5210 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5211 LHSHi, RHSHi, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005212 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5213 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005214 Tmp2 = SDValue();
Dale Johannesenf864ac92007-10-06 01:24:11 +00005215 break;
5216 }
5217
5218 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00005219 case ISD::SETEQ:
5220 case ISD::SETNE:
5221 if (RHSLo == RHSHi)
5222 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5223 if (RHSCST->isAllOnesValue()) {
5224 // Comparison to -1.
5225 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5226 Tmp2 = RHSLo;
5227 break;
5228 }
5229
5230 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5231 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5232 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5233 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5234 break;
5235 default:
5236 // If this is a comparison of the sign bit, just look at the top part.
5237 // X > -1, x < 0
5238 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5239 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00005240 CST->isNullValue()) || // X < 0
Nate Begeman7e7f4392006-02-01 07:19:44 +00005241 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5242 CST->isAllOnesValue())) { // X > -1
5243 Tmp1 = LHSHi;
5244 Tmp2 = RHSHi;
5245 break;
5246 }
5247
5248 // FIXME: This generated code sucks.
5249 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00005250 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00005251 default: assert(0 && "Unknown integer setcc!");
5252 case ISD::SETLT:
5253 case ISD::SETULT: LowCC = ISD::SETULT; break;
5254 case ISD::SETGT:
5255 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5256 case ISD::SETLE:
5257 case ISD::SETULE: LowCC = ISD::SETULE; break;
5258 case ISD::SETGE:
5259 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5260 }
5261
5262 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5263 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5264 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5265
5266 // NOTE: on targets without efficient SELECT of bools, we can always use
5267 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00005268 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands8feb6942009-01-01 15:52:00 +00005269 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5270 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005271 if (!Tmp1.getNode())
Duncan Sands8feb6942009-01-01 15:52:00 +00005272 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5273 LHSLo, RHSLo, LowCC);
5274 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5275 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005276 if (!Tmp2.getNode())
Duncan Sands8feb6942009-01-01 15:52:00 +00005277 Tmp2 = DAG.getNode(ISD::SETCC,
5278 TLI.getSetCCResultType(LHSHi.getValueType()),
5279 LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00005280
Gabor Greiff304a7a2008-08-28 21:40:38 +00005281 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5282 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohmanb72127a2008-03-13 22:13:53 +00005283 if ((Tmp1C && Tmp1C->isNullValue()) ||
5284 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng93049452007-02-08 22:16:19 +00005285 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5286 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohmanb72127a2008-03-13 22:13:53 +00005287 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng93049452007-02-08 22:16:19 +00005288 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5289 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5290 // low part is known false, returns high part.
5291 // For LE / GE, if high part is known false, ignore the low part.
5292 // For LT / GT, if high part is known true, ignore the low part.
5293 Tmp1 = Tmp2;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00005295 } else {
Duncan Sands8feb6942009-01-01 15:52:00 +00005296 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5297 LHSHi, RHSHi, ISD::SETEQ, false,
5298 DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005299 if (!Result.getNode())
Duncan Sands8feb6942009-01-01 15:52:00 +00005300 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5301 LHSHi, RHSHi, ISD::SETEQ);
Evan Cheng93049452007-02-08 22:16:19 +00005302 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5303 Result, Tmp1, Tmp2));
5304 Tmp1 = Result;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005305 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00005306 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00005307 }
5308 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005309 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00005310 LHS = Tmp1;
5311 RHS = Tmp2;
5312}
5313
Evan Cheng3b0f5e42008-10-15 02:05:31 +00005314/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5315/// condition code CC on the current target. This routine assumes LHS and rHS
5316/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5317/// illegal condition code into AND / OR of multiple SETCC values.
5318void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5319 SDValue &LHS, SDValue &RHS,
5320 SDValue &CC) {
5321 MVT OpVT = LHS.getValueType();
5322 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5323 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5324 default: assert(0 && "Unknown condition code action!");
5325 case TargetLowering::Legal:
5326 // Nothing to do.
5327 break;
5328 case TargetLowering::Expand: {
5329 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5330 unsigned Opc = 0;
5331 switch (CCCode) {
5332 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman97d3f6c2008-10-21 03:12:54 +00005333 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5334 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5335 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5336 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5337 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5338 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5339 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5340 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5341 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5342 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5343 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5344 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00005345 // FIXME: Implement more expansions.
5346 }
5347
5348 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5349 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5350 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5351 RHS = SDValue();
5352 CC = SDValue();
5353 break;
5354 }
5355 }
5356}
5357
Chris Lattner87bc3e72008-01-16 07:45:30 +00005358/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5359/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5360/// a load from the stack slot to DestVT, extending it if needed.
5361/// The resultant code need not be legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005362SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5363 MVT SlotVT,
5364 MVT DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00005365 // Create the stack frame object.
Mon P Wang5c755ff2008-07-05 20:40:31 +00005366 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5367 SrcOp.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005368 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang5c755ff2008-07-05 20:40:31 +00005369
Dan Gohman54d3b5a2008-02-11 18:58:42 +00005370 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00005371 int SPFI = StackPtrFI->getIndex();
Mon P Wang5c755ff2008-07-05 20:40:31 +00005372
Duncan Sands13237ac2008-06-06 12:08:01 +00005373 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5374 unsigned SlotSize = SlotVT.getSizeInBits();
5375 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang5c755ff2008-07-05 20:40:31 +00005376 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5377 DestVT.getTypeForMVT());
Chris Lattner36e663d2005-12-23 00:16:34 +00005378
Chris Lattner87bc3e72008-01-16 07:45:30 +00005379 // Emit a store to the stack slot. Use a truncstore if the input value is
5380 // later than DestVT.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005381 SDValue Store;
Mon P Wang5c755ff2008-07-05 20:40:31 +00005382
Chris Lattner87bc3e72008-01-16 07:45:30 +00005383 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00005384 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005385 PseudoSourceValue::getFixedStack(SPFI), 0,
5386 SlotVT, false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005387 else {
5388 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00005389 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005390 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang5c755ff2008-07-05 20:40:31 +00005391 false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005392 }
5393
Chris Lattner36e663d2005-12-23 00:16:34 +00005394 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00005395 if (SlotSize == DestSize)
Mon P Wang5c755ff2008-07-05 20:40:31 +00005396 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005397
5398 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang5c755ff2008-07-05 20:40:31 +00005399 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5400 false, DestAlign);
Chris Lattner36e663d2005-12-23 00:16:34 +00005401}
5402
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005403SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner6be79822006-04-04 17:23:26 +00005404 // Create a vector sized/aligned stack slot, store the value to element #0,
5405 // then load the whole vector back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005406 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00005407
Dan Gohman54d3b5a2008-02-11 18:58:42 +00005408 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00005409 int SPFI = StackPtrFI->getIndex();
5410
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005411 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005412 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00005413 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005414 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner6be79822006-04-04 17:23:26 +00005415}
5416
5417
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005418/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00005419/// support the operation, but do support the resultant vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005420SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005421
5422 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00005423 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00005424 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005425 bool isOnlyLowElement = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005426 SDValue SplatValue = Node->getOperand(0);
Chris Lattner322c8262008-03-09 00:29:42 +00005427
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005428 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattner322c8262008-03-09 00:29:42 +00005429 // and use a bitmask instead of a list of elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005430 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng1d2e9952006-03-24 01:17:21 +00005431 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005432 bool isConstant = true;
5433 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5434 SplatValue.getOpcode() != ISD::UNDEF)
5435 isConstant = false;
5436
Evan Cheng1d2e9952006-03-24 01:17:21 +00005437 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005438 SDValue V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00005439 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005440 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005441 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00005442 if (SplatValue != V)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005443 SplatValue = SDValue(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005444
5445 // If this isn't a constant element or an undef, we can't use a constant
5446 // pool load.
5447 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5448 V.getOpcode() != ISD::UNDEF)
5449 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005450 }
5451
5452 if (isOnlyLowElement) {
5453 // If the low element is an undef too, then this whole things is an undef.
5454 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5455 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5456 // Otherwise, turn this into a scalar_to_vector node.
5457 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5458 Node->getOperand(0));
5459 }
5460
Chris Lattner77e271c2006-03-24 07:29:17 +00005461 // If all elements are constants, create a load from the constant pool.
5462 if (isConstant) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005463 MVT VT = Node->getValueType(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005464 std::vector<Constant*> CV;
5465 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5466 if (ConstantFPSDNode *V =
5467 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00005468 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00005469 } else if (ConstantSDNode *V =
Chris Lattner3b187622008-04-20 00:41:09 +00005470 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00005471 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00005472 } else {
5473 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner3b187622008-04-20 00:41:09 +00005474 const Type *OpNTy =
Duncan Sands13237ac2008-06-06 12:08:01 +00005475 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner77e271c2006-03-24 07:29:17 +00005476 CV.push_back(UndefValue::get(OpNTy));
5477 }
5478 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00005479 Constant *CP = ConstantVector::get(CV);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005480 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005481 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman2d489b52008-02-06 22:27:42 +00005482 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005483 PseudoSourceValue::getConstantPool(), 0,
5484 false, Alignment);
Chris Lattner77e271c2006-03-24 07:29:17 +00005485 }
5486
Gabor Greiff304a7a2008-08-28 21:40:38 +00005487 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner21e68c82006-03-20 01:52:29 +00005488 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands13237ac2008-06-06 12:08:01 +00005489 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005490 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5491 std::vector<SDValue> ZeroVec(NumElems, Zero);
5492 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005493 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00005494
5495 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005496 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00005497 // Get the splatted value into the low element of a vector register.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005498 SDValue LowValVec =
Chris Lattner21e68c82006-03-20 01:52:29 +00005499 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5500
5501 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5502 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5503 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5504 SplatMask);
5505 }
5506 }
5507
Evan Cheng1d2e9952006-03-24 01:17:21 +00005508 // If there are only two unique elements, we may be able to turn this into a
5509 // vector shuffle.
5510 if (Values.size() == 2) {
Chris Lattner322c8262008-03-09 00:29:42 +00005511 // Get the two values in deterministic order.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005512 SDValue Val1 = Node->getOperand(1);
5513 SDValue Val2;
5514 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattner322c8262008-03-09 00:29:42 +00005515 if (MI->first != Val1)
5516 Val2 = MI->first;
5517 else
5518 Val2 = (++MI)->first;
5519
5520 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5521 // vector shuffle has the undef vector on the RHS.
5522 if (Val1.getOpcode() == ISD::UNDEF)
5523 std::swap(Val1, Val2);
5524
Evan Cheng1d2e9952006-03-24 01:17:21 +00005525 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands13237ac2008-06-06 12:08:01 +00005526 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5527 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005528 std::vector<SDValue> MaskVec(NumElems);
Chris Lattner322c8262008-03-09 00:29:42 +00005529
5530 // Set elements of the shuffle mask for Val1.
5531 std::vector<unsigned> &Val1Elts = Values[Val1];
5532 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5533 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5534
5535 // Set elements of the shuffle mask for Val2.
5536 std::vector<unsigned> &Val2Elts = Values[Val2];
5537 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5538 if (Val2.getOpcode() != ISD::UNDEF)
5539 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5540 else
5541 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005543 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005544 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00005545
Chris Lattner322c8262008-03-09 00:29:42 +00005546 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005547 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5548 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattner322c8262008-03-09 00:29:42 +00005549 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5550 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005551 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng1d2e9952006-03-24 01:17:21 +00005552
5553 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattner322c8262008-03-09 00:29:42 +00005554 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005555 }
5556 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005557
5558 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5559 // aligned object on the stack, store each element into it, then load
5560 // the result as a vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00005561 MVT VT = Node->getValueType(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005562 // Create the stack frame object.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005563 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005564
5565 // Emit a store of each element to the stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005566 SmallVector<SDValue, 8> Stores;
Duncan Sands13237ac2008-06-06 12:08:01 +00005567 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005568 // Store (in the right endianness) the elements to memory.
5569 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5570 // Ignore undef elements.
5571 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5572
Chris Lattner8fa445a2006-03-22 01:46:54 +00005573 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005574
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005575 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005576 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5577
Evan Chengdf9ac472006-10-05 23:01:46 +00005578 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005579 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005580 }
5581
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005582 SDValue StoreChain;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005583 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005584 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5585 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005586 else
5587 StoreChain = DAG.getEntryNode();
5588
5589 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005590 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005591}
5592
Chris Lattner4157c412005-04-02 04:00:59 +00005593void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005594 SDValue Op, SDValue Amt,
5595 SDValue &Lo, SDValue &Hi) {
Chris Lattner4157c412005-04-02 04:00:59 +00005596 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005597 SDValue LHSL, LHSH;
Chris Lattner4157c412005-04-02 04:00:59 +00005598 ExpandOp(Op, LHSL, LHSH);
5599
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005600 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands13237ac2008-06-06 12:08:01 +00005601 MVT VT = LHSL.getValueType();
Chris Lattnerbd887772006-08-14 23:53:35 +00005602 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005603 Hi = Lo.getValue(1);
5604}
5605
5606
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005607/// ExpandShift - Try to find a clever way to expand this shift operation out to
5608/// smaller elements. If we can't find a way that is more efficient than a
5609/// libcall on this target, return false. Otherwise, return true with the
5610/// low-parts expanded into Lo and Hi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005611bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5612 SDValue &Lo, SDValue &Hi) {
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005613 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5614 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005615
Duncan Sands13237ac2008-06-06 12:08:01 +00005616 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005617 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands13237ac2008-06-06 12:08:01 +00005618 MVT ShTy = ShAmt.getValueType();
5619 unsigned ShBits = ShTy.getSizeInBits();
5620 unsigned VTBits = Op.getValueType().getSizeInBits();
5621 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanb0674922005-04-06 21:13:14 +00005622
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005623 // Handle the case when Amt is an immediate.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005624 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005625 unsigned Cst = CN->getZExtValue();
Nate Begemanb0674922005-04-06 21:13:14 +00005626 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005627 SDValue InL, InH;
Nate Begemanb0674922005-04-06 21:13:14 +00005628 ExpandOp(Op, InL, InH);
5629 switch(Opc) {
5630 case ISD::SHL:
5631 if (Cst > VTBits) {
5632 Lo = DAG.getConstant(0, NVT);
5633 Hi = DAG.getConstant(0, NVT);
5634 } else if (Cst > NVTBits) {
5635 Lo = DAG.getConstant(0, NVT);
5636 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005637 } else if (Cst == NVTBits) {
5638 Lo = DAG.getConstant(0, NVT);
5639 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005640 } else {
5641 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5642 Hi = DAG.getNode(ISD::OR, NVT,
5643 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5644 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5645 }
5646 return true;
5647 case ISD::SRL:
5648 if (Cst > VTBits) {
5649 Lo = DAG.getConstant(0, NVT);
5650 Hi = DAG.getConstant(0, NVT);
5651 } else if (Cst > NVTBits) {
5652 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5653 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005654 } else if (Cst == NVTBits) {
5655 Lo = InH;
5656 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005657 } else {
5658 Lo = DAG.getNode(ISD::OR, NVT,
5659 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5660 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5661 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5662 }
5663 return true;
5664 case ISD::SRA:
5665 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005666 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005667 DAG.getConstant(NVTBits-1, ShTy));
5668 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005669 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005670 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005671 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005672 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005673 } else if (Cst == NVTBits) {
5674 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005675 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005676 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005677 } else {
5678 Lo = DAG.getNode(ISD::OR, NVT,
5679 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5680 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5681 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5682 }
5683 return true;
5684 }
5685 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005686
5687 // Okay, the shift amount isn't constant. However, if we can tell that it is
5688 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005689 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5690 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005691 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005692
Dan Gohmanf3057a92008-02-22 01:12:31 +00005693 // If we know that if any of the high bits of the shift amount are one, then
5694 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005695 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005696 // Mask out the high bit, which we know is set.
5697 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005698 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005699
5700 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005701 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005702 ExpandOp(Op, InL, InH);
5703 switch(Opc) {
5704 case ISD::SHL:
5705 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5706 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5707 return true;
5708 case ISD::SRL:
5709 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5710 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5711 return true;
5712 case ISD::SRA:
5713 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5714 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5715 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5716 return true;
5717 }
5718 }
5719
Dan Gohmanf3057a92008-02-22 01:12:31 +00005720 // If we know that the high bits of the shift amount are all zero, then we can
5721 // do this as a couple of simple shifts.
5722 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005723 // Compute 32-amt.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005724 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner875ea0c2006-09-20 03:38:48 +00005725 DAG.getConstant(NVTBits, Amt.getValueType()),
5726 Amt);
5727
5728 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005729 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005730 ExpandOp(Op, InL, InH);
5731 switch(Opc) {
5732 case ISD::SHL:
5733 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5734 Hi = DAG.getNode(ISD::OR, NVT,
5735 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5736 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5737 return true;
5738 case ISD::SRL:
5739 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5740 Lo = DAG.getNode(ISD::OR, NVT,
5741 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5742 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5743 return true;
5744 case ISD::SRA:
5745 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5746 Lo = DAG.getNode(ISD::OR, NVT,
5747 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5748 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5749 return true;
5750 }
5751 }
5752
Nate Begemanb0674922005-04-06 21:13:14 +00005753 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005754}
Chris Lattneraac464e2005-01-21 06:05:23 +00005755
Chris Lattner4add7e32005-01-23 04:42:50 +00005756
Chris Lattneraac464e2005-01-21 06:05:23 +00005757// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5758// does not fit into a register, return the lo part and set the hi part to the
5759// by-reg argument. If it does fit into a single register, return the result
5760// and leave the Hi part unset.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005761SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5762 bool isSigned, SDValue &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005763 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5764 // The input chain to this libcall is the entry node of the function.
5765 // Legalizing the call will automatically add the previous call to the
5766 // dependence.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005767 SDValue InChain = DAG.getEntryNode();
Chris Lattner462505f2006-02-13 09:18:02 +00005768
Chris Lattneraac464e2005-01-21 06:05:23 +00005769 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005770 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005771 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005772 MVT ArgVT = Node->getOperand(i).getValueType();
5773 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencere63b6512006-12-31 05:55:36 +00005774 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005775 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005776 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005777 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005778 }
Bill Wendling24c79f22008-09-16 21:48:12 +00005779 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang58c37942008-10-30 08:01:45 +00005780 TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005781
Chris Lattner06bbeb62005-05-11 19:02:11 +00005782 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands13237ac2008-06-06 12:08:01 +00005783 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005784 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen0e32a2c2008-09-26 19:31:26 +00005785 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5786 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005787
Chris Lattner462505f2006-02-13 09:18:02 +00005788 // Legalize the call sequence, starting with the chain. This will advance
5789 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5790 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5791 LegalizeOp(CallInfo.second);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005792 SDValue Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005793 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005794 default: assert(0 && "Unknown thing");
5795 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005796 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005797 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005798 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005799 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005800 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005801 }
Chris Lattner63022662005-09-02 20:26:58 +00005802 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005803}
5804
Dan Gohman550c9af2008-08-14 20:04:46 +00005805/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5806///
5807SDValue SelectionDAGLegalize::
5808LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5809 bool isCustom = false;
5810 SDValue Tmp1;
5811 switch (getTypeAction(Op.getValueType())) {
5812 case Legal:
5813 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5814 Op.getValueType())) {
5815 default: assert(0 && "Unknown operation action!");
5816 case TargetLowering::Custom:
5817 isCustom = true;
5818 // FALLTHROUGH
5819 case TargetLowering::Legal:
5820 Tmp1 = LegalizeOp(Op);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005821 if (Result.getNode())
Dan Gohman550c9af2008-08-14 20:04:46 +00005822 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5823 else
5824 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5825 DestTy, Tmp1);
5826 if (isCustom) {
5827 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005828 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman550c9af2008-08-14 20:04:46 +00005829 }
5830 break;
5831 case TargetLowering::Expand:
5832 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5833 break;
5834 case TargetLowering::Promote:
5835 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5836 break;
5837 }
5838 break;
5839 case Expand:
5840 Result = ExpandIntToFP(isSigned, DestTy, Op);
5841 break;
5842 case Promote:
5843 Tmp1 = PromoteOp(Op);
5844 if (isSigned) {
5845 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5846 Tmp1, DAG.getValueType(Op.getValueType()));
5847 } else {
5848 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5849 Op.getValueType());
5850 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00005851 if (Result.getNode())
Dan Gohman550c9af2008-08-14 20:04:46 +00005852 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5853 else
5854 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5855 DestTy, Tmp1);
5856 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5857 break;
5858 }
5859 return Result;
5860}
Chris Lattner4add7e32005-01-23 04:42:50 +00005861
Evan Chengbf535fc2007-04-27 07:33:31 +00005862/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5863///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005864SDValue SelectionDAGLegalize::
5865ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005866 MVT SourceVT = Source.getValueType();
Dan Gohmand6819da2008-03-11 01:59:03 +00005867 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattneraac464e2005-01-21 06:05:23 +00005868
Dan Gohman550c9af2008-08-14 20:04:46 +00005869 // Expand unsupported int-to-fp vector casts by unrolling them.
5870 if (DestTy.isVector()) {
5871 if (!ExpandSource)
5872 return LegalizeOp(UnrollVectorOp(Source));
5873 MVT DestEltTy = DestTy.getVectorElementType();
5874 if (DestTy.getVectorNumElements() == 1) {
5875 SDValue Scalar = ScalarizeVectorOp(Source);
5876 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5877 DestEltTy, Scalar);
5878 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5879 }
5880 SDValue Lo, Hi;
5881 SplitVectorOp(Source, Lo, Hi);
5882 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5883 DestTy.getVectorNumElements() / 2);
5884 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5885 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengda9b7522008-10-13 18:46:18 +00005886 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5887 HiResult));
Dan Gohman550c9af2008-08-14 20:04:46 +00005888 }
5889
Evan Cheng0bd72c52008-04-01 02:18:22 +00005890 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5891 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmanda7897c2008-03-05 02:07:31 +00005892 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005893 // incoming integer is set. To handle this, we dynamically test to see if
5894 // it is set, and, if so, add a fudge factor.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005895 SDValue Hi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005896 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005897 SDValue Lo;
Dan Gohmand6819da2008-03-11 01:59:03 +00005898 ExpandOp(Source, Lo, Hi);
5899 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5900 } else {
5901 // The comparison for the sign bit will use the entire operand.
5902 Hi = Source;
5903 }
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005904
Dale Johannesendb6b9562008-11-04 20:52:49 +00005905 // Check to see if the target has a custom way to lower this. If so, use
5906 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen28929582008-10-21 20:50:01 +00005907 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5908 default: assert(0 && "This action not implemented for this operation!");
5909 case TargetLowering::Legal:
5910 case TargetLowering::Expand:
5911 break; // This case is handled below.
5912 case TargetLowering::Custom: {
5913 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5914 Source), DAG);
5915 if (NV.getNode())
5916 return LegalizeOp(NV);
5917 break; // The target decided this was legal after all
5918 }
5919 }
5920
Chris Lattner2a4f7312005-05-13 04:45:13 +00005921 // If this is unsigned, and not supported, first perform the conversion to
5922 // signed, then adjust the result if the sign bit is set.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005923 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner2a4f7312005-05-13 04:45:13 +00005924
Duncan Sands8feb6942009-01-01 15:52:00 +00005925 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5926 Hi, DAG.getConstant(0, Hi.getValueType()),
5927 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005928 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5929 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005930 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005931 uint64_t FF = 0x5f800000ULL;
5932 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmanda7897c2008-03-05 02:07:31 +00005933 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005934
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005935 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005936 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005937 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmane2947e12008-09-22 22:40:08 +00005938 Alignment = std::min(Alignment, 4u);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005939 SDValue FudgeInReg;
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005940 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005941 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005942 PseudoSourceValue::getConstantPool(), 0,
5943 false, Alignment);
Duncan Sands11dd4242008-06-08 20:54:56 +00005944 else if (DestTy.bitsGT(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005945 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005946 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005947 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005948 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005949 MVT::f32, false, Alignment);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005950 else
5951 assert(0 && "Unexpected conversion");
5952
Duncan Sands13237ac2008-06-06 12:08:01 +00005953 MVT SCVT = SignedConv.getValueType();
Evan Chengbf535fc2007-04-27 07:33:31 +00005954 if (SCVT != DestTy) {
5955 // Destination type needs to be expanded as well. The FADD now we are
5956 // constructing will be expanded into a libcall.
Duncan Sands13237ac2008-06-06 12:08:01 +00005957 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5958 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand9d874b2008-03-05 01:08:17 +00005959 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Chengbf535fc2007-04-27 07:33:31 +00005960 SignedConv, SignedConv.getValue(1));
5961 }
5962 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5963 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005964 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005965 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005966
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005967 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand9d874b2008-03-05 01:08:17 +00005968 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005969 default: assert(0 && "This action not implemented for this operation!");
5970 case TargetLowering::Legal:
5971 case TargetLowering::Expand:
5972 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005973 case TargetLowering::Custom: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005974 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005975 Source), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005976 if (NV.getNode())
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005977 return LegalizeOp(NV);
5978 break; // The target decided this was legal after all
5979 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005980 }
5981
Chris Lattner153587e2005-05-12 07:00:44 +00005982 // Expand the source, then glue it back together for the call. We must expand
5983 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohmand6819da2008-03-11 01:59:03 +00005984 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005985 SDValue SrcLo, SrcHi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005986 ExpandOp(Source, SrcLo, SrcHi);
5987 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5988 }
Chris Lattner153587e2005-05-12 07:00:44 +00005989
Duncan Sands77a3d052008-07-17 02:36:29 +00005990 RTLIB::Libcall LC = isSigned ?
5991 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5992 RTLIB::getUINTTOFP(SourceVT, DestTy);
5993 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5994
Chris Lattner462505f2006-02-13 09:18:02 +00005995 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005996 SDValue HiPart;
Gabor Greiff304a7a2008-08-28 21:40:38 +00005997 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5998 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanf4300952008-03-10 23:03:31 +00005999 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6000 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00006001}
Misha Brukman835702a2005-04-21 22:36:52 +00006002
Chris Lattner689bdcc2006-01-28 08:25:58 +00006003/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6004/// INT_TO_FP operation of the specified operand when the target requests that
6005/// we expand it. At this point, we know that the result and operand types are
6006/// legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006007SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6008 SDValue Op0,
6009 MVT DestVT) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006010 if (Op0.getValueType() == MVT::i32) {
6011 // simple 32-bit [signed|unsigned] integer to float/double expansion
6012
Chris Lattnera2c7ff32008-01-16 07:03:22 +00006013 // Get the stack frame index of a 8 byte buffer.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006014 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattnera2c7ff32008-01-16 07:03:22 +00006015
Chris Lattner689bdcc2006-01-28 08:25:58 +00006016 // word offset constant for Hi/Lo address computation
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006017 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner689bdcc2006-01-28 08:25:58 +00006018 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006019 SDValue Hi = StackSlot;
6020 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00006021 if (TLI.isLittleEndian())
6022 std::swap(Hi, Lo);
6023
Chris Lattner689bdcc2006-01-28 08:25:58 +00006024 // if signed map to unsigned space
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006025 SDValue Op0Mapped;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006026 if (isSigned) {
6027 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006028 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006029 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6030 } else {
6031 Op0Mapped = Op0;
6032 }
6033 // store the lo of the constructed double - based on integer input
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006034 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00006035 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006036 // initial hi portion of constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006037 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006038 // store the hi of the constructed double - biased exponent
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006039 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006040 // load the constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006041 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006042 // FP constant to bias correct the final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006043 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner689bdcc2006-01-28 08:25:58 +00006044 BitsToDouble(0x4330000080000000ULL)
6045 : BitsToDouble(0x4330000000000000ULL),
6046 MVT::f64);
6047 // subtract the bias
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006048 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006049 // final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006050 SDValue Result;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006051 // handle final rounding
6052 if (DestVT == MVT::f64) {
6053 // do nothing
6054 Result = Sub;
Duncan Sands11dd4242008-06-08 20:54:56 +00006055 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00006056 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6057 DAG.getIntPtrConstant(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00006058 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen7f724e92007-09-16 16:51:49 +00006059 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006060 }
6061 return Result;
6062 }
6063 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006064 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006065
Duncan Sands8feb6942009-01-01 15:52:00 +00006066 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6067 Op0, DAG.getConstant(0, Op0.getValueType()),
6068 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006069 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6070 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner689bdcc2006-01-28 08:25:58 +00006071 SignSet, Four, Zero);
6072
6073 // If the sign bit of the integer is set, the large number will be treated
6074 // as a negative number. To counteract this, the dynamic code adds an
6075 // offset depending on the data type.
6076 uint64_t FF;
Duncan Sands13237ac2008-06-06 12:08:01 +00006077 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006078 default: assert(0 && "Unsupported integer type!");
6079 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6080 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6081 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6082 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6083 }
6084 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00006085 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006086
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006087 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006088 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006089 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmane2947e12008-09-22 22:40:08 +00006090 Alignment = std::min(Alignment, 4u);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006091 SDValue FudgeInReg;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006092 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00006093 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006094 PseudoSourceValue::getConstantPool(), 0,
6095 false, Alignment);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006096 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00006097 FudgeInReg =
6098 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6099 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006100 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006101 MVT::f32, false, Alignment));
Chris Lattner689bdcc2006-01-28 08:25:58 +00006102 }
6103
6104 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6105}
6106
6107/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6108/// *INT_TO_FP operation of the specified operand when the target requests that
6109/// we promote it. At this point, we know that the result and operand types are
6110/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6111/// operation that takes a larger input.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006112SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6113 MVT DestVT,
6114 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006115 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00006116 MVT NewInTy = LegalOp.getValueType();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006117
6118 unsigned OpToUse = 0;
6119
6120 // Scan for the appropriate larger type to use.
6121 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006122 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6123 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00006124
6125 // If the target supports SINT_TO_FP of this type, use it.
6126 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6127 default: break;
6128 case TargetLowering::Legal:
6129 if (!TLI.isTypeLegal(NewInTy))
6130 break; // Can't use this datatype.
6131 // FALL THROUGH.
6132 case TargetLowering::Custom:
6133 OpToUse = ISD::SINT_TO_FP;
6134 break;
6135 }
6136 if (OpToUse) break;
6137 if (isSigned) continue;
6138
6139 // If the target supports UINT_TO_FP of this type, use it.
6140 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6141 default: break;
6142 case TargetLowering::Legal:
6143 if (!TLI.isTypeLegal(NewInTy))
6144 break; // Can't use this datatype.
6145 // FALL THROUGH.
6146 case TargetLowering::Custom:
6147 OpToUse = ISD::UINT_TO_FP;
6148 break;
6149 }
6150 if (OpToUse) break;
6151
6152 // Otherwise, try a larger type.
6153 }
6154
6155 // Okay, we found the operation and type to use. Zero extend our input to the
6156 // desired type then run the operation on it.
6157 return DAG.getNode(OpToUse, DestVT,
6158 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6159 NewInTy, LegalOp));
6160}
6161
6162/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6163/// FP_TO_*INT operation of the specified operand when the target requests that
6164/// we promote it. At this point, we know that the result and operand types are
6165/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6166/// operation that returns a larger result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006167SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6168 MVT DestVT,
6169 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006170 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00006171 MVT NewOutTy = DestVT;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006172
6173 unsigned OpToUse = 0;
6174
6175 // Scan for the appropriate larger type to use.
6176 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006177 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6178 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00006179
6180 // If the target supports FP_TO_SINT returning this type, use it.
6181 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6182 default: break;
6183 case TargetLowering::Legal:
6184 if (!TLI.isTypeLegal(NewOutTy))
6185 break; // Can't use this datatype.
6186 // FALL THROUGH.
6187 case TargetLowering::Custom:
6188 OpToUse = ISD::FP_TO_SINT;
6189 break;
6190 }
6191 if (OpToUse) break;
6192
6193 // If the target supports FP_TO_UINT of this type, use it.
6194 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6195 default: break;
6196 case TargetLowering::Legal:
6197 if (!TLI.isTypeLegal(NewOutTy))
6198 break; // Can't use this datatype.
6199 // FALL THROUGH.
6200 case TargetLowering::Custom:
6201 OpToUse = ISD::FP_TO_UINT;
6202 break;
6203 }
6204 if (OpToUse) break;
6205
6206 // Otherwise, try a larger type.
6207 }
6208
Chris Lattnerf81d5882007-11-24 07:07:01 +00006209
6210 // Okay, we found the operation and type to use.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006211 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands93e180342008-07-04 11:47:58 +00006212
Chris Lattnerf81d5882007-11-24 07:07:01 +00006213 // If the operation produces an invalid type, it must be custom lowered. Use
6214 // the target lowering hooks to expand it. Just keep the low part of the
6215 // expanded operation, we know that we're truncating anyway.
6216 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006217 SmallVector<SDValue, 2> Results;
6218 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6219 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6220 Operation = Results[0];
Chris Lattnerf81d5882007-11-24 07:07:01 +00006221 }
Duncan Sands93e180342008-07-04 11:47:58 +00006222
Chris Lattnerf81d5882007-11-24 07:07:01 +00006223 // Truncate the result of the extended FP_TO_*INT operation to the desired
6224 // size.
6225 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006226}
6227
6228/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6229///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006230SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006231 MVT VT = Op.getValueType();
6232 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006233 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands13237ac2008-06-06 12:08:01 +00006234 switch (VT.getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006235 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6236 case MVT::i16:
6237 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6238 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6239 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6240 case MVT::i32:
6241 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6242 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6243 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6244 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6245 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6246 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6247 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6248 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6249 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6250 case MVT::i64:
6251 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6252 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6253 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6254 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6255 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6256 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6257 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6258 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6259 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6260 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6261 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6262 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6263 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6264 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6265 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6266 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6267 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6268 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6269 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6270 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6271 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6272 }
6273}
6274
6275/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6276///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006277SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006278 switch (Opc) {
6279 default: assert(0 && "Cannot expand this yet!");
6280 case ISD::CTPOP: {
6281 static const uint64_t mask[6] = {
6282 0x5555555555555555ULL, 0x3333333333333333ULL,
6283 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6284 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6285 };
Duncan Sands13237ac2008-06-06 12:08:01 +00006286 MVT VT = Op.getValueType();
6287 MVT ShVT = TLI.getShiftAmountTy();
6288 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006289 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6290 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006291 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6292 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006293 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6294 DAG.getNode(ISD::AND, VT,
6295 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6296 }
6297 return Op;
6298 }
6299 case ISD::CTLZ: {
6300 // for now, we do this:
6301 // x = x | (x >> 1);
6302 // x = x | (x >> 2);
6303 // ...
6304 // x = x | (x >>16);
6305 // x = x | (x >>32); // for 64-bit input
6306 // return popcount(~x);
6307 //
6308 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00006309 MVT VT = Op.getValueType();
6310 MVT ShVT = TLI.getShiftAmountTy();
6311 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006312 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006313 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006314 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6315 }
6316 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6317 return DAG.getNode(ISD::CTPOP, VT, Op);
6318 }
6319 case ISD::CTTZ: {
6320 // for now, we use: { return popcount(~x & (x - 1)); }
6321 // unless the target has ctlz but not ctpop, in which case we use:
6322 // { return 32 - nlz(~x & (x-1)); }
6323 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00006324 MVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006325 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6326 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00006327 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6328 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6329 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6330 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6331 TLI.isOperationLegal(ISD::CTLZ, VT))
6332 return DAG.getNode(ISD::SUB, VT,
Duncan Sands13237ac2008-06-06 12:08:01 +00006333 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00006334 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6335 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6336 }
6337 }
6338}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006339
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006340/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerdc750592005-01-07 07:47:09 +00006341/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman3a293e72008-10-01 15:07:49 +00006342/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattnerdc750592005-01-07 07:47:09 +00006343/// ExpandedNodes map is filled in for any results that are expanded, and the
6344/// Lo/Hi values are returned.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006345void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands13237ac2008-06-06 12:08:01 +00006346 MVT VT = Op.getValueType();
6347 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006348 SDNode *Node = Op.getNode();
Chris Lattnerdc750592005-01-07 07:47:09 +00006349 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00006350 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands13237ac2008-06-06 12:08:01 +00006351 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattnerdc750592005-01-07 07:47:09 +00006352
Chris Lattner1a570f12005-09-02 20:32:45 +00006353 // See if we already expanded it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006354 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00006355 = ExpandedNodes.find(Op);
6356 if (I != ExpandedNodes.end()) {
6357 Lo = I->second.first;
6358 Hi = I->second.second;
6359 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00006360 }
6361
Chris Lattnerdc750592005-01-07 07:47:09 +00006362 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00006363 case ISD::CopyFromReg:
6364 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00006365 case ISD::FP_ROUND_INREG:
6366 if (VT == MVT::ppcf128 &&
6367 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6368 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006369 SDValue SrcLo, SrcHi, Src;
Dale Johannesen6472eb62007-10-11 23:32:15 +00006370 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6371 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006372 SDValue Result = TLI.LowerOperation(
Dale Johannesen6472eb62007-10-11 23:32:15 +00006373 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006374 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6375 Lo = Result.getNode()->getOperand(0);
6376 Hi = Result.getNode()->getOperand(1);
Dale Johannesen666323e2007-10-10 01:01:31 +00006377 break;
6378 }
6379 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00006380 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006381#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006382 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006383#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00006384 assert(0 && "Do not know how to expand this operator!");
6385 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00006386 case ISD::EXTRACT_ELEMENT:
6387 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00006388 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman66272a52008-02-27 01:52:30 +00006389 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00006390 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00006391 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesenb066c1f2007-10-31 00:32:36 +00006392 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6393 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6394 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00006395 case ISD::UNDEF:
6396 Lo = DAG.getNode(ISD::UNDEF, NVT);
6397 Hi = DAG.getNode(ISD::UNDEF, NVT);
6398 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006399 case ISD::Constant: {
Duncan Sands13237ac2008-06-06 12:08:01 +00006400 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf2bbfa32008-03-03 22:20:46 +00006401 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6402 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6403 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattnerdc750592005-01-07 07:47:09 +00006404 break;
6405 }
Evan Cheng47833a12006-12-12 21:32:44 +00006406 case ISD::ConstantFP: {
6407 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00006408 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00006409 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen007aa372007-10-11 18:07:22 +00006410 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6411 MVT::f64);
6412 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6413 MVT::f64);
6414 break;
6415 }
Evan Cheng3766fc602006-12-12 22:19:28 +00006416 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00006417 if (getTypeAction(Lo.getValueType()) == Expand)
6418 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00006419 break;
6420 }
Chris Lattner32e08b72005-03-28 22:03:13 +00006421 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006422 // Return the operands.
6423 Lo = Node->getOperand(0);
6424 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00006425 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006426
6427 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00006428 if (Node->getNumValues() == 1) {
6429 ExpandOp(Op.getOperand(0), Lo, Hi);
6430 break;
6431 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00006432 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greifabfdf922008-08-26 22:36:50 +00006433 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00006434 Op.getValue(1).getValueType() == MVT::Other &&
6435 "unhandled MERGE_VALUES");
6436 ExpandOp(Op.getOperand(0), Lo, Hi);
6437 // Remember that we legalized the chain.
6438 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6439 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006440
6441 case ISD::SIGN_EXTEND_INREG:
6442 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00006443 // sext_inreg the low part if needed.
6444 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6445
6446 // The high part gets the sign extension from the lo-part. This handles
6447 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006448 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands13237ac2008-06-06 12:08:01 +00006449 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006450 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006451 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00006452
Nate Begeman2fba8a32006-01-14 03:14:10 +00006453 case ISD::BSWAP: {
6454 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006455 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begeman2fba8a32006-01-14 03:14:10 +00006456 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6457 Lo = TempLo;
6458 break;
6459 }
6460
Chris Lattner55e9cde2005-05-11 04:51:16 +00006461 case ISD::CTPOP:
6462 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00006463 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6464 DAG.getNode(ISD::CTPOP, NVT, Lo),
6465 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00006466 Hi = DAG.getConstant(0, NVT);
6467 break;
6468
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006469 case ISD::CTLZ: {
6470 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00006471 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006472 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6473 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands8feb6942009-01-01 15:52:00 +00006474 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6475 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006476 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006477 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6478
6479 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6480 Hi = DAG.getConstant(0, NVT);
6481 break;
6482 }
6483
6484 case ISD::CTTZ: {
6485 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00006486 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006487 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6488 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands8feb6942009-01-01 15:52:00 +00006489 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6490 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006491 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006492 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6493
6494 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6495 Hi = DAG.getConstant(0, NVT);
6496 break;
6497 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00006498
Nate Begemane74795c2006-01-25 18:21:52 +00006499 case ISD::VAARG: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006500 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6501 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00006502 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6503 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6504
6505 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006506 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00006507 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00006508 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00006509 std::swap(Lo, Hi);
6510 break;
6511 }
6512
Chris Lattnerdc750592005-01-07 07:47:09 +00006513 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00006514 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006515 SDValue Ch = LD->getChain(); // Legalize the chain.
6516 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Chenge71fe34d2006-10-09 20:57:25 +00006517 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman550c9af2008-08-14 20:04:46 +00006518 const Value *SV = LD->getSrcValue();
Dan Gohman2af30632007-07-09 22:18:38 +00006519 int SVOffset = LD->getSrcValueOffset();
6520 unsigned Alignment = LD->getAlignment();
6521 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00006522
Evan Chenge71fe34d2006-10-09 20:57:25 +00006523 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman550c9af2008-08-14 20:04:46 +00006524 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohman2af30632007-07-09 22:18:38 +00006525 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00006526 if (VT == MVT::f32 || VT == MVT::f64) {
6527 // f32->i32 or f64->i64 one to one expansion.
6528 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006529 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00006530 // Recursively expand the new load.
6531 if (getTypeAction(NVT) == Expand)
6532 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00006533 break;
6534 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00006535
Evan Chenge71fe34d2006-10-09 20:57:25 +00006536 // Increment the pointer to the other half.
Duncan Sands13237ac2008-06-06 12:08:01 +00006537 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Chenge71fe34d2006-10-09 20:57:25 +00006538 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006539 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006540 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006541 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman550c9af2008-08-14 20:04:46 +00006542 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohman2af30632007-07-09 22:18:38 +00006543 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00006544
Evan Chenge71fe34d2006-10-09 20:57:25 +00006545 // Build a factor node to remember that this load is independent of the
6546 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006547 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Chenge71fe34d2006-10-09 20:57:25 +00006548 Hi.getValue(1));
6549
6550 // Remember that we legalized the chain.
6551 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00006552 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00006553 std::swap(Lo, Hi);
6554 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00006555 MVT EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00006556
Dale Johannesen6802d0c2007-10-19 20:29:00 +00006557 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6558 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00006559 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman550c9af2008-08-14 20:04:46 +00006560 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006561 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00006562 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006563 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Chenge370e0e2006-12-13 03:19:57 +00006564 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6565 break;
6566 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00006567
6568 if (EVT == NVT)
Dan Gohman550c9af2008-08-14 20:04:46 +00006569 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006570 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006571 else
Dan Gohman550c9af2008-08-14 20:04:46 +00006572 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006573 SVOffset, EVT, isVolatile,
6574 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006575
6576 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006577 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Chenge71fe34d2006-10-09 20:57:25 +00006578
6579 if (ExtType == ISD::SEXTLOAD) {
6580 // The high part is obtained by SRA'ing all but one of the bits of the
6581 // lo part.
Duncan Sands13237ac2008-06-06 12:08:01 +00006582 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Chenge71fe34d2006-10-09 20:57:25 +00006583 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6584 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6585 } else if (ExtType == ISD::ZEXTLOAD) {
6586 // The high part is just a zero.
6587 Hi = DAG.getConstant(0, NVT);
6588 } else /* if (ExtType == ISD::EXTLOAD) */ {
6589 // The high part is undefined.
6590 Hi = DAG.getNode(ISD::UNDEF, NVT);
6591 }
6592 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006593 break;
6594 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006595 case ISD::AND:
6596 case ISD::OR:
6597 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006598 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00006599 ExpandOp(Node->getOperand(0), LL, LH);
6600 ExpandOp(Node->getOperand(1), RL, RH);
6601 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6602 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6603 break;
6604 }
6605 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006606 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00006607 ExpandOp(Node->getOperand(1), LL, LH);
6608 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00006609 if (getTypeAction(NVT) == Expand)
6610 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006611 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00006612 if (VT != MVT::f32)
6613 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00006614 break;
6615 }
Nate Begemane5b86d72005-08-10 20:51:12 +00006616 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006617 SDValue TL, TH, FL, FH;
Nate Begemane5b86d72005-08-10 20:51:12 +00006618 ExpandOp(Node->getOperand(2), TL, TH);
6619 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00006620 if (getTypeAction(NVT) == Expand)
6621 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00006622 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6623 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00006624 if (VT != MVT::f32)
6625 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6626 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00006627 break;
6628 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006629 case ISD::ANY_EXTEND:
6630 // The low part is any extension of the input (which degenerates to a copy).
6631 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6632 // The high part is undefined.
6633 Hi = DAG.getNode(ISD::UNDEF, NVT);
6634 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006635 case ISD::SIGN_EXTEND: {
6636 // The low part is just a sign extension of the input (which degenerates to
6637 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006638 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006639
Chris Lattnerdc750592005-01-07 07:47:09 +00006640 // The high part is obtained by SRA'ing all but one of the bits of the lo
6641 // part.
Duncan Sands13237ac2008-06-06 12:08:01 +00006642 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006643 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6644 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00006645 break;
6646 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006647 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00006648 // The low part is just a zero extension of the input (which degenerates to
6649 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006650 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006651
Chris Lattnerdc750592005-01-07 07:47:09 +00006652 // The high part is just a zero.
6653 Hi = DAG.getConstant(0, NVT);
6654 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006655
Chris Lattner59b27fa372007-02-13 23:55:16 +00006656 case ISD::TRUNCATE: {
6657 // The input value must be larger than this value. Expand *it*.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006658 SDValue NewLo;
Chris Lattner59b27fa372007-02-13 23:55:16 +00006659 ExpandOp(Node->getOperand(0), NewLo, Hi);
6660
6661 // The low part is now either the right size, or it is closer. If not the
6662 // right size, make an illegal truncate so we recursively expand it.
6663 if (NewLo.getValueType() != Node->getValueType(0))
6664 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6665 ExpandOp(NewLo, Lo, Hi);
6666 break;
6667 }
6668
Chris Lattner36e663d2005-12-23 00:16:34 +00006669 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006670 SDValue Tmp;
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006671 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6672 // If the target wants to, allow it to lower this itself.
6673 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6674 case Expand: assert(0 && "cannot expand FP!");
6675 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6676 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6677 }
6678 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6679 }
6680
Evan Cheng4eee7242006-12-09 02:42:38 +00006681 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006682 if (VT == MVT::f32 || VT == MVT::f64) {
6683 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006684 if (getTypeAction(NVT) == Expand)
6685 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006686 break;
6687 }
6688
6689 // If source operand will be expanded to the same type as VT, i.e.
6690 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands13237ac2008-06-06 12:08:01 +00006691 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng0076ca02006-12-12 19:53:13 +00006692 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6693 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006694 break;
6695 }
6696
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006697 // Turn this into a load/store pair by default.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006698 if (Tmp.getNode() == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006699 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006700
Chris Lattner36e663d2005-12-23 00:16:34 +00006701 ExpandOp(Tmp, Lo, Hi);
6702 break;
6703 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006704
Chris Lattnerf81d5882007-11-24 07:07:01 +00006705 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006706 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6707 TargetLowering::Custom &&
6708 "Must custom expand ReadCycleCounter");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006709 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006710 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerf81d5882007-11-24 07:07:01 +00006711 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006712 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006713 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006714 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006715 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006716
Dan Gohman12f24902008-12-23 21:37:04 +00006717 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006718 // This operation does not need a loop.
6719 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6720 assert(Tmp.getNode() && "Node must be custom expanded!");
6721 ExpandOp(Tmp.getValue(0), Lo, Hi);
6722 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6723 LegalizeOp(Tmp.getValue(1)));
6724 break;
6725 }
6726
Dan Gohman12f24902008-12-23 21:37:04 +00006727 case ISD::ATOMIC_LOAD_ADD:
6728 case ISD::ATOMIC_LOAD_SUB:
6729 case ISD::ATOMIC_LOAD_AND:
6730 case ISD::ATOMIC_LOAD_OR:
6731 case ISD::ATOMIC_LOAD_XOR:
6732 case ISD::ATOMIC_LOAD_NAND:
6733 case ISD::ATOMIC_SWAP: {
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006734 // These operations require a loop to be generated. We can't do that yet,
6735 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen867d5492008-10-02 18:53:47 +00006736 SDValue In2Lo, In2Hi, In2;
6737 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6738 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006739 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6740 SDValue Replace =
Dan Gohman12f24902008-12-23 21:37:04 +00006741 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6742 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006743 Anode->getSrcValue(), Anode->getAlignment());
6744 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen867d5492008-10-02 18:53:47 +00006745 ExpandOp(Result.getValue(0), Lo, Hi);
6746 // Remember that we legalized the chain.
6747 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth357061a2008-03-05 01:15:49 +00006748 break;
6749 }
6750
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006751 // These operators cannot be expanded directly, emit them as calls to
6752 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006753 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006754 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006755 SDValue Op;
Chris Lattner941d84a2005-07-30 01:40:57 +00006756 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6757 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006758 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6759 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006760 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006761
Chris Lattner941d84a2005-07-30 01:40:57 +00006762 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6763
Chris Lattnerfe68d752005-07-29 00:33:32 +00006764 // Now that the custom expander is done, expand the result, which is still
6765 // VT.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006766 if (Op.getNode()) {
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006767 ExpandOp(Op, Lo, Hi);
6768 break;
6769 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006770 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006771
Duncan Sands77a3d052008-07-17 02:36:29 +00006772 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6773 VT);
6774 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6775 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006776 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006777 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006778
Evan Cheng31cbddf2007-01-12 02:11:51 +00006779 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006780 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006781 SDValue Op;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006782 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6783 case Expand: assert(0 && "cannot expand FP!");
6784 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6785 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6786 }
6787
6788 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6789
6790 // Now that the custom expander is done, expand the result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006791 if (Op.getNode()) {
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006792 ExpandOp(Op, Lo, Hi);
6793 break;
6794 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006795 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006796
Duncan Sands77a3d052008-07-17 02:36:29 +00006797 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6798 VT);
6799 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6800 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006801 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006802 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006803
Evan Cheng870e4f82006-01-09 18:31:59 +00006804 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006805 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006806 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006807 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006808 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006809 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006810 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006811 // Now that the custom expander is done, expand the result, which is
6812 // still VT.
6813 ExpandOp(Op, Lo, Hi);
6814 break;
6815 }
6816 }
6817
Chris Lattner72b503b2006-09-13 03:50:39 +00006818 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6819 // this X << 1 as X+X.
6820 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00006821 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner72b503b2006-09-13 03:50:39 +00006822 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006823 SDValue LoOps[2], HiOps[3];
Chris Lattner72b503b2006-09-13 03:50:39 +00006824 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6825 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6826 LoOps[1] = LoOps[0];
6827 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6828
6829 HiOps[1] = HiOps[0];
6830 HiOps[2] = Lo.getValue(1);
6831 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6832 break;
6833 }
6834 }
6835
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006836 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006837 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006838 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006839
6840 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006841 TargetLowering::LegalizeAction Action =
6842 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6843 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6844 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006845 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006846 break;
6847 }
6848
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006849 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006850 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006851 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006852 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006853
Evan Cheng870e4f82006-01-09 18:31:59 +00006854 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006855 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006856 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006857 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006858 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006859 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006860 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006861 // Now that the custom expander is done, expand the result, which is
6862 // still VT.
6863 ExpandOp(Op, Lo, Hi);
6864 break;
6865 }
6866 }
6867
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006868 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006869 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006870 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006871
6872 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006873 TargetLowering::LegalizeAction Action =
6874 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6875 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6876 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006877 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006878 break;
6879 }
6880
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006881 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006882 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006883 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006884 }
6885
6886 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006887 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006888 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006889 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006890 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006891 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006892 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006893 // Now that the custom expander is done, expand the result, which is
6894 // still VT.
6895 ExpandOp(Op, Lo, Hi);
6896 break;
6897 }
6898 }
6899
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006900 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006901 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006902 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006903
6904 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006905 TargetLowering::LegalizeAction Action =
6906 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6907 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6908 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006909 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006910 break;
6911 }
6912
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006913 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006914 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006915 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006916 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006917
Misha Brukman835702a2005-04-21 22:36:52 +00006918 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006919 case ISD::SUB: {
6920 // If the target wants to custom expand this, let them.
6921 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6922 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006923 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006924 if (Result.getNode()) {
Duncan Sands5fb92e52008-06-22 09:42:16 +00006925 ExpandOp(Result, Lo, Hi);
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006926 break;
6927 }
6928 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006929 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006930 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006931 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6932 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006933 SDValue LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006934 LoOps[0] = LHSL;
6935 LoOps[1] = RHSL;
6936 HiOps[0] = LHSH;
6937 HiOps[1] = RHSH;
Andrew Lenharth21dca9c2008-10-07 18:27:23 +00006938
Andrew Lenharth6606f172008-10-07 17:03:15 +00006939 //cascaded check to see if any smaller size has a a carry flag.
6940 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6941 bool hasCarry = false;
Andrew Lenharth21dca9c2008-10-07 18:27:23 +00006942 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6943 MVT AVT = MVT::getIntegerVT(BitSize);
6944 if (TLI.isOperationLegal(OpV, AVT)) {
6945 hasCarry = true;
6946 break;
6947 }
6948 }
6949
Andrew Lenharth6606f172008-10-07 17:03:15 +00006950 if(hasCarry) {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006951 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006952 if (Node->getOpcode() == ISD::ADD) {
6953 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6954 HiOps[2] = Lo.getValue(1);
6955 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6956 } else {
6957 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6958 HiOps[2] = Lo.getValue(1);
6959 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6960 }
6961 break;
Nate Begeman5965bd12006-02-17 05:43:56 +00006962 } else {
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006963 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006964 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6965 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands8feb6942009-01-01 15:52:00 +00006966 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth6606f172008-10-07 17:03:15 +00006967 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006968 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6969 DAG.getConstant(1, NVT),
6970 DAG.getConstant(0, NVT));
Duncan Sands8feb6942009-01-01 15:52:00 +00006971 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth6606f172008-10-07 17:03:15 +00006972 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006973 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6974 DAG.getConstant(1, NVT),
6975 Carry1);
6976 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6977 } else {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006978 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6979 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006980 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6981 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6982 DAG.getConstant(1, NVT),
6983 DAG.getConstant(0, NVT));
6984 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6985 }
6986 break;
Nate Begeman5965bd12006-02-17 05:43:56 +00006987 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006988 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006989
6990 case ISD::ADDC:
6991 case ISD::SUBC: {
6992 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006993 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00006994 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6995 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6996 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006997 SDValue LoOps[2] = { LHSL, RHSL };
6998 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00006999
7000 if (Node->getOpcode() == ISD::ADDC) {
7001 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7002 HiOps[2] = Lo.getValue(1);
7003 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7004 } else {
7005 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7006 HiOps[2] = Lo.getValue(1);
7007 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7008 }
7009 // Remember that we legalized the flag.
7010 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7011 break;
7012 }
7013 case ISD::ADDE:
7014 case ISD::SUBE: {
7015 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007016 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00007017 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7018 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7019 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007020 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7021 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00007022
7023 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7024 HiOps[2] = Lo.getValue(1);
7025 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7026
7027 // Remember that we legalized the flag.
7028 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7029 break;
7030 }
Nate Begemanadd0c632005-04-11 03:01:51 +00007031 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00007032 // If the target wants to custom expand this, let them.
7033 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007034 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007035 if (New.getNode()) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00007036 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00007037 break;
7038 }
7039 }
7040
Evan Chenge93762d2006-09-01 18:17:58 +00007041 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7042 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00007043 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7044 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7045 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007046 SDValue LL, LH, RL, RH;
Nate Begemanadd0c632005-04-11 03:01:51 +00007047 ExpandOp(Node->getOperand(0), LL, LH);
7048 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00007049 unsigned OuterBitSize = Op.getValueSizeInBits();
7050 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00007051 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7052 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman272e2342008-03-10 20:42:19 +00007053 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7054 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7055 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohmana1603612007-10-08 18:33:35 +00007056 // The inputs are both zero-extended.
7057 if (HasUMUL_LOHI) {
7058 // We can emit a umul_lohi.
7059 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007060 Hi = SDValue(Lo.getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00007061 break;
7062 }
7063 if (HasMULHU) {
7064 // We can emit a mulhu+mul.
7065 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7066 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7067 break;
7068 }
Dan Gohmana1603612007-10-08 18:33:35 +00007069 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00007070 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00007071 // The input values are both sign-extended.
7072 if (HasSMUL_LOHI) {
7073 // We can emit a smul_lohi.
7074 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007075 Hi = SDValue(Lo.getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00007076 break;
7077 }
7078 if (HasMULHS) {
7079 // We can emit a mulhs+mul.
7080 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7081 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7082 break;
7083 }
7084 }
7085 if (HasUMUL_LOHI) {
7086 // Lo,Hi = umul LHS, RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007087 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohmana1603612007-10-08 18:33:35 +00007088 DAG.getVTList(NVT, NVT), LL, RL);
7089 Lo = UMulLOHI;
7090 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00007091 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7092 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7093 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7094 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00007095 break;
Nate Begeman43144a22005-08-30 02:44:00 +00007096 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00007097 if (HasMULHU) {
7098 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7099 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7100 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7101 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7102 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7103 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7104 break;
7105 }
Nate Begemanadd0c632005-04-11 03:01:51 +00007106 }
Evan Chenge93762d2006-09-01 18:17:58 +00007107
Dan Gohmana1603612007-10-08 18:33:35 +00007108 // If nothing else, we can make a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00007109 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00007110 break;
7111 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00007112 case ISD::SDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007113 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007114 break;
7115 case ISD::UDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007116 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007117 break;
7118 case ISD::SREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00007119 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007120 break;
7121 case ISD::UREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00007122 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007123 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00007124
Evan Cheng4eee7242006-12-09 02:42:38 +00007125 case ISD::FADD:
Duncan Sands844d55a2008-04-12 17:14:18 +00007126 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7127 RTLIB::ADD_F64,
7128 RTLIB::ADD_F80,
7129 RTLIB::ADD_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007130 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007131 break;
7132 case ISD::FSUB:
Duncan Sands844d55a2008-04-12 17:14:18 +00007133 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7134 RTLIB::SUB_F64,
7135 RTLIB::SUB_F80,
7136 RTLIB::SUB_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007137 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007138 break;
7139 case ISD::FMUL:
Duncan Sands844d55a2008-04-12 17:14:18 +00007140 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7141 RTLIB::MUL_F64,
7142 RTLIB::MUL_F80,
7143 RTLIB::MUL_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007144 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007145 break;
7146 case ISD::FDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007147 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7148 RTLIB::DIV_F64,
7149 RTLIB::DIV_F80,
7150 RTLIB::DIV_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007151 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007152 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007153 case ISD::FP_EXTEND: {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007154 if (VT == MVT::ppcf128) {
7155 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7156 Node->getOperand(0).getValueType()==MVT::f64);
7157 const uint64_t zero = 0;
7158 if (Node->getOperand(0).getValueType()==MVT::f32)
7159 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7160 else
7161 Hi = Node->getOperand(0);
7162 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7163 break;
7164 }
Duncan Sands77a3d052008-07-17 02:36:29 +00007165 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7166 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7167 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007168 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007169 }
7170 case ISD::FP_ROUND: {
7171 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7172 VT);
7173 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7174 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007175 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007176 }
Evan Chengf4e5de42008-09-09 23:02:14 +00007177 case ISD::FSQRT:
7178 case ISD::FSIN:
7179 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007180 case ISD::FLOG:
7181 case ISD::FLOG2:
7182 case ISD::FLOG10:
7183 case ISD::FEXP:
7184 case ISD::FEXP2:
Dan Gohman2af34bd2008-08-21 18:38:14 +00007185 case ISD::FTRUNC:
7186 case ISD::FFLOOR:
7187 case ISD::FCEIL:
7188 case ISD::FRINT:
7189 case ISD::FNEARBYINT:
Evan Cheng0fff3972008-09-09 23:35:53 +00007190 case ISD::FPOW:
7191 case ISD::FPOWI: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00007192 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00007193 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00007194 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00007195 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7196 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007197 break;
7198 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00007199 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7200 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007201 break;
7202 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00007203 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7204 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007205 break;
Dale Johannesenda2d8062008-09-04 00:47:13 +00007206 case ISD::FLOG:
7207 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7208 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7209 break;
7210 case ISD::FLOG2:
7211 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7212 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7213 break;
7214 case ISD::FLOG10:
7215 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7216 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7217 break;
7218 case ISD::FEXP:
7219 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7220 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7221 break;
7222 case ISD::FEXP2:
7223 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7224 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7225 break;
Dan Gohman2af34bd2008-08-21 18:38:14 +00007226 case ISD::FTRUNC:
7227 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7228 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7229 break;
7230 case ISD::FFLOOR:
7231 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7232 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7233 break;
7234 case ISD::FCEIL:
7235 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7236 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7237 break;
7238 case ISD::FRINT:
7239 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7240 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7241 break;
7242 case ISD::FNEARBYINT:
7243 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7244 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7245 break;
Evan Chengf4e5de42008-09-09 23:02:14 +00007246 case ISD::FPOW:
7247 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7248 RTLIB::POW_PPCF128);
7249 break;
7250 case ISD::FPOWI:
7251 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7252 RTLIB::POWI_PPCF128);
7253 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00007254 default: assert(0 && "Unreachable!");
7255 }
Duncan Sands844d55a2008-04-12 17:14:18 +00007256 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00007257 break;
7258 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00007259 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00007260 if (VT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007261 SDValue Tmp;
Dale Johannesen61c574f2007-10-12 19:02:17 +00007262 ExpandOp(Node->getOperand(0), Lo, Tmp);
7263 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7264 // lo = hi==fabs(hi) ? lo : -lo;
7265 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7266 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7267 DAG.getCondCode(ISD::SETEQ));
7268 break;
7269 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007270 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00007271 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7272 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7273 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7274 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7275 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7276 if (getTypeAction(NVT) == Expand)
7277 ExpandOp(Lo, Lo, Hi);
7278 break;
7279 }
7280 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00007281 if (VT == MVT::ppcf128) {
7282 ExpandOp(Node->getOperand(0), Lo, Hi);
7283 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7284 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7285 break;
7286 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007287 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00007288 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7289 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7290 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7291 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7292 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7293 if (getTypeAction(NVT) == Expand)
7294 ExpandOp(Lo, Lo, Hi);
7295 break;
7296 }
Evan Cheng003feb02007-01-04 21:56:39 +00007297 case ISD::FCOPYSIGN: {
7298 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7299 if (getTypeAction(NVT) == Expand)
7300 ExpandOp(Lo, Lo, Hi);
7301 break;
7302 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007303 case ISD::SINT_TO_FP:
7304 case ISD::UINT_TO_FP: {
7305 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands13237ac2008-06-06 12:08:01 +00007306 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen12c76db2008-03-18 17:28:38 +00007307
7308 // Promote the operand if needed. Do this before checking for
7309 // ppcf128 so conversions of i16 and i8 work.
7310 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007311 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen12c76db2008-03-18 17:28:38 +00007312 Tmp = isSigned
7313 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7314 DAG.getValueType(SrcVT))
7315 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007316 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen12c76db2008-03-18 17:28:38 +00007317 SrcVT = Node->getOperand(0).getValueType();
7318 }
7319
Dan Gohmanf4300952008-03-10 23:03:31 +00007320 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman432e4a62008-02-25 21:39:34 +00007321 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007322 if (isSigned) {
7323 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7324 Node->getOperand(0)));
7325 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7326 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00007327 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007328 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7329 Node->getOperand(0)));
7330 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7331 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007332 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007333 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7334 DAG.getConstant(0, MVT::i32),
7335 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7336 DAG.getConstantFP(
7337 APFloat(APInt(128, 2, TwoE32)),
7338 MVT::ppcf128)),
7339 Hi,
7340 DAG.getCondCode(ISD::SETLT)),
7341 Lo, Hi);
7342 }
7343 break;
7344 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007345 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7346 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00007347 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007348 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7349 Lo, Hi);
7350 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7351 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7352 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7353 DAG.getConstant(0, MVT::i64),
7354 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7355 DAG.getConstantFP(
7356 APFloat(APInt(128, 2, TwoE64)),
7357 MVT::ppcf128)),
7358 Hi,
7359 DAG.getCondCode(ISD::SETLT)),
7360 Lo, Hi);
7361 break;
7362 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007363
Dan Gohmanf4300952008-03-10 23:03:31 +00007364 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7365 Node->getOperand(0));
Evan Cheng86e476b2008-04-01 01:50:16 +00007366 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng611abc02008-04-01 01:51:26 +00007367 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng86e476b2008-04-01 01:50:16 +00007368 ExpandOp(Lo, Lo, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007369 break;
7370 }
Evan Chengf3a80c62006-12-13 02:38:13 +00007371 }
Chris Lattnerdc750592005-01-07 07:47:09 +00007372
Chris Lattnerac12f682005-12-21 18:02:52 +00007373 // Make sure the resultant values have been legalized themselves, unless this
7374 // is a type that requires multi-step expansion.
7375 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7376 Lo = LegalizeOp(Lo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007377 if (Hi.getNode())
Evan Cheng3432ab92006-12-11 19:27:14 +00007378 // Don't legalize the high part if it is expanded to a single node.
7379 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00007380 }
Evan Cheng870e4f82006-01-09 18:31:59 +00007381
7382 // Remember in a map if the values will be reused later.
Dan Gohman38740a92008-07-07 17:46:23 +00007383 bool isNew =
7384 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng870e4f82006-01-09 18:31:59 +00007385 assert(isNew && "Value already expanded?!?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007386 isNew = isNew;
Chris Lattnerdc750592005-01-07 07:47:09 +00007387}
7388
Dan Gohmana8665142007-06-25 16:23:39 +00007389/// SplitVectorOp - Given an operand of vector type, break it down into
7390/// two smaller values, still of vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007391void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7392 SDValue &Hi) {
Duncan Sands13237ac2008-06-06 12:08:01 +00007393 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00007394 SDNode *Node = Op.getNode();
Duncan Sands13237ac2008-06-06 12:08:01 +00007395 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattner32206f52006-03-18 01:44:44 +00007396 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00007397
Duncan Sands13237ac2008-06-06 12:08:01 +00007398 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begemanbd117f02007-11-15 21:15:26 +00007399
7400 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7401 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7402
Duncan Sands13237ac2008-06-06 12:08:01 +00007403 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7404 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00007405
Chris Lattner32206f52006-03-18 01:44:44 +00007406 // See if we already split it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007407 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner32206f52006-03-18 01:44:44 +00007408 = SplitNodes.find(Op);
7409 if (I != SplitNodes.end()) {
7410 Lo = I->second.first;
7411 Hi = I->second.second;
7412 return;
7413 }
7414
7415 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007416 default:
7417#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00007418 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007419#endif
7420 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00007421 case ISD::UNDEF:
7422 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7423 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7424 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007425 case ISD::BUILD_PAIR:
7426 Lo = Node->getOperand(0);
7427 Hi = Node->getOperand(1);
7428 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00007429 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman6f94f612008-04-25 18:07:40 +00007430 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7431 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00007432 unsigned Index = Idx->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007433 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman6f94f612008-04-25 18:07:40 +00007434 if (Index < NewNumElts_Lo)
7435 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7436 DAG.getIntPtrConstant(Index));
7437 else
7438 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7439 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7440 break;
7441 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007442 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman6f94f612008-04-25 18:07:40 +00007443 Node->getOperand(1),
7444 Node->getOperand(2));
7445 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmana90183e2007-09-28 23:53:40 +00007446 break;
7447 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007448 case ISD::VECTOR_SHUFFLE: {
7449 // Build the low part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007450 SDValue Mask = Node->getOperand(2);
7451 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00007452 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007453
7454 // Insert all of the elements from the input that are needed. We use
7455 // buildvector of extractelement here because the input vectors will have
7456 // to be legalized, so this makes the code simpler.
7457 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007458 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00007459 if (IdxNode.getOpcode() == ISD::UNDEF) {
7460 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7461 continue;
7462 }
Dan Gohmaneffb8942008-09-12 16:56:44 +00007463 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007464 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007465 if (Idx >= NumElements) {
7466 InVec = Node->getOperand(1);
7467 Idx -= NumElements;
7468 }
7469 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7470 DAG.getConstant(Idx, PtrVT)));
7471 }
7472 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7473 Ops.clear();
7474
7475 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007476 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00007477 if (IdxNode.getOpcode() == ISD::UNDEF) {
7478 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7479 continue;
7480 }
Dan Gohmaneffb8942008-09-12 16:56:44 +00007481 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007482 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007483 if (Idx >= NumElements) {
7484 InVec = Node->getOperand(1);
7485 Idx -= NumElements;
7486 }
7487 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7488 DAG.getConstant(Idx, PtrVT)));
7489 }
Mon P Wang73343502008-07-25 01:30:26 +00007490 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007491 break;
7492 }
Dan Gohmana8665142007-06-25 16:23:39 +00007493 case ISD::BUILD_VECTOR: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007494 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00007495 Node->op_begin()+NewNumElts_Lo);
7496 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00007497
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007498 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00007499 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00007500 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00007501 break;
7502 }
Dan Gohmana8665142007-06-25 16:23:39 +00007503 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00007504 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00007505 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7506 if (NewNumSubvectors == 1) {
7507 Lo = Node->getOperand(0);
7508 Hi = Node->getOperand(1);
7509 } else {
Mon P Wang25f01062008-11-10 04:46:22 +00007510 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7511 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00007512 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00007513
Mon P Wang25f01062008-11-10 04:46:22 +00007514 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmana8665142007-06-25 16:23:39 +00007515 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00007516 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00007517 }
Dan Gohman26455c42007-06-13 15:12:02 +00007518 break;
7519 }
Mon P Wang25f01062008-11-10 04:46:22 +00007520 case ISD::EXTRACT_SUBVECTOR: {
7521 SDValue Vec = Op.getOperand(0);
7522 SDValue Idx = Op.getOperand(1);
7523 MVT IdxVT = Idx.getValueType();
7524
7525 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7526 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7527 if (CIdx) {
7528 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7529 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7530 IdxVT));
7531 } else {
7532 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7533 DAG.getConstant(NewNumElts_Lo, IdxVT));
7534 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7535 }
7536 break;
7537 }
Dan Gohman8f518b982007-10-17 14:48:28 +00007538 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007539 SDValue Cond = Node->getOperand(0);
Dan Gohman8f518b982007-10-17 14:48:28 +00007540
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007541 SDValue LL, LH, RL, RH;
Dan Gohman8f518b982007-10-17 14:48:28 +00007542 SplitVectorOp(Node->getOperand(1), LL, LH);
7543 SplitVectorOp(Node->getOperand(2), RL, RH);
7544
Duncan Sands13237ac2008-06-06 12:08:01 +00007545 if (Cond.getValueType().isVector()) {
Dan Gohman8f518b982007-10-17 14:48:28 +00007546 // Handle a vector merge.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007547 SDValue CL, CH;
Dan Gohman8f518b982007-10-17 14:48:28 +00007548 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00007549 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7550 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00007551 } else {
7552 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00007553 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7554 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00007555 }
7556 break;
7557 }
Chris Lattner9d3740e2008-06-30 02:43:01 +00007558 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007559 SDValue CondLHS = Node->getOperand(0);
7560 SDValue CondRHS = Node->getOperand(1);
7561 SDValue CondCode = Node->getOperand(4);
Chris Lattner9d3740e2008-06-30 02:43:01 +00007562
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007563 SDValue LL, LH, RL, RH;
Chris Lattner9d3740e2008-06-30 02:43:01 +00007564 SplitVectorOp(Node->getOperand(2), LL, LH);
7565 SplitVectorOp(Node->getOperand(3), RL, RH);
7566
7567 // Handle a simple select with vector operands.
7568 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7569 LL, RL, CondCode);
7570 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7571 LH, RH, CondCode);
7572 break;
7573 }
Nate Begemancfcb5602008-05-12 19:40:03 +00007574 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007575 SDValue LL, LH, RL, RH;
Nate Begemancfcb5602008-05-12 19:40:03 +00007576 SplitVectorOp(Node->getOperand(0), LL, LH);
7577 SplitVectorOp(Node->getOperand(1), RL, RH);
7578 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7579 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7580 break;
7581 }
Dan Gohmana8665142007-06-25 16:23:39 +00007582 case ISD::ADD:
7583 case ISD::SUB:
7584 case ISD::MUL:
7585 case ISD::FADD:
7586 case ISD::FSUB:
7587 case ISD::FMUL:
7588 case ISD::SDIV:
7589 case ISD::UDIV:
7590 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00007591 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00007592 case ISD::AND:
7593 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00007594 case ISD::XOR:
7595 case ISD::UREM:
7596 case ISD::SREM:
Mon P Wanga5016402008-12-18 20:03:17 +00007597 case ISD::FREM:
7598 case ISD::SHL:
7599 case ISD::SRA:
7600 case ISD::SRL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007601 SDValue LL, LH, RL, RH;
Chris Lattner32206f52006-03-18 01:44:44 +00007602 SplitVectorOp(Node->getOperand(0), LL, LH);
7603 SplitVectorOp(Node->getOperand(1), RL, RH);
7604
Nate Begemanbd117f02007-11-15 21:15:26 +00007605 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7606 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00007607 break;
7608 }
Dan Gohman550c9af2008-08-14 20:04:46 +00007609 case ISD::FP_ROUND:
Dan Gohman2a7de412007-10-11 23:57:53 +00007610 case ISD::FPOWI: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007611 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00007612 SplitVectorOp(Node->getOperand(0), L, H);
7613
Nate Begemanbd117f02007-11-15 21:15:26 +00007614 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7615 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00007616 break;
7617 }
7618 case ISD::CTTZ:
7619 case ISD::CTLZ:
7620 case ISD::CTPOP:
7621 case ISD::FNEG:
7622 case ISD::FABS:
7623 case ISD::FSQRT:
7624 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00007625 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007626 case ISD::FLOG:
7627 case ISD::FLOG2:
7628 case ISD::FLOG10:
7629 case ISD::FEXP:
7630 case ISD::FEXP2:
Nate Begemand4d45c22007-11-17 03:58:34 +00007631 case ISD::FP_TO_SINT:
7632 case ISD::FP_TO_UINT:
7633 case ISD::SINT_TO_FP:
Dan Gohman550c9af2008-08-14 20:04:46 +00007634 case ISD::UINT_TO_FP:
7635 case ISD::TRUNCATE:
7636 case ISD::ANY_EXTEND:
7637 case ISD::SIGN_EXTEND:
7638 case ISD::ZERO_EXTEND:
7639 case ISD::FP_EXTEND: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007640 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00007641 SplitVectorOp(Node->getOperand(0), L, H);
7642
Nate Begemanbd117f02007-11-15 21:15:26 +00007643 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7644 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00007645 break;
7646 }
Mon P Wang58fb9132008-11-10 20:54:11 +00007647 case ISD::CONVERT_RNDSAT: {
7648 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7649 SDValue L, H;
7650 SplitVectorOp(Node->getOperand(0), L, H);
7651 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7652 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7653 SDValue STyOpL = DAG.getValueType(L.getValueType());
7654 SDValue STyOpH = DAG.getValueType(H.getValueType());
7655
7656 SDValue RndOp = Node->getOperand(3);
7657 SDValue SatOp = Node->getOperand(4);
7658
7659 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7660 RndOp, SatOp, CvtCode);
7661 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7662 RndOp, SatOp, CvtCode);
7663 break;
7664 }
Dan Gohmana8665142007-06-25 16:23:39 +00007665 case ISD::LOAD: {
7666 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007667 SDValue Ch = LD->getChain();
7668 SDValue Ptr = LD->getBasePtr();
Dan Gohman550c9af2008-08-14 20:04:46 +00007669 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmana8665142007-06-25 16:23:39 +00007670 const Value *SV = LD->getSrcValue();
7671 int SVOffset = LD->getSrcValueOffset();
Dan Gohman550c9af2008-08-14 20:04:46 +00007672 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmana8665142007-06-25 16:23:39 +00007673 unsigned Alignment = LD->getAlignment();
7674 bool isVolatile = LD->isVolatile();
7675
Dan Gohman550c9af2008-08-14 20:04:46 +00007676 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7677 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7678
7679 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7680 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7681 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7682
7683 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7684 NewVT_Lo, Ch, Ptr, Offset,
7685 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7686 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattner32206f52006-03-18 01:44:44 +00007687 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00007688 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00007689 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00007690 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman550c9af2008-08-14 20:04:46 +00007691 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7692 NewVT_Hi, Ch, Ptr, Offset,
7693 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00007694
7695 // Build a factor node to remember that this load is independent of the
7696 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007697 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattner32206f52006-03-18 01:44:44 +00007698 Hi.getValue(1));
7699
7700 // Remember that we legalized the chain.
7701 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00007702 break;
7703 }
Dan Gohmana8665142007-06-25 16:23:39 +00007704 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007705 // We know the result is a vector. The input may be either a vector or a
7706 // scalar value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007707 SDValue InOp = Node->getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00007708 if (!InOp.getValueType().isVector() ||
7709 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman0de76942007-06-29 00:09:08 +00007710 // The input is a scalar or single-element vector.
7711 // Lower to a store/load so that it can be split.
7712 // FIXME: this could be improved probably.
Mon P Wang97432f42008-07-15 05:28:34 +00007713 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7714 Op.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007715 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007716 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007717
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007718 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00007719 InOp, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00007720 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00007721 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00007722 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007723 }
Dan Gohman0de76942007-06-29 00:09:08 +00007724 // Split the vector and convert each of the pieces now.
7725 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00007726 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7727 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00007728 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007729 }
Chris Lattner32206f52006-03-18 01:44:44 +00007730 }
7731
7732 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00007733 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00007734 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00007735 assert(isNew && "Value already split?!?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007736 isNew = isNew;
Chris Lattner32206f52006-03-18 01:44:44 +00007737}
7738
7739
Dan Gohmanf4e86da2007-06-27 14:06:22 +00007740/// ScalarizeVectorOp - Given an operand of single-element vector type
7741/// (e.g. v1f32), convert it into the equivalent operation that returns a
7742/// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007743SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00007744 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00007745 SDNode *Node = Op.getNode();
Duncan Sands13237ac2008-06-06 12:08:01 +00007746 MVT NewVT = Op.getValueType().getVectorElementType();
7747 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00007748
Dan Gohmana8665142007-06-25 16:23:39 +00007749 // See if we already scalarized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007750 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmana8665142007-06-25 16:23:39 +00007751 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00007752
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007753 SDValue Result;
Chris Lattner32206f52006-03-18 01:44:44 +00007754 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00007755 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007756#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00007757 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007758#endif
Dan Gohmana8665142007-06-25 16:23:39 +00007759 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7760 case ISD::ADD:
7761 case ISD::FADD:
7762 case ISD::SUB:
7763 case ISD::FSUB:
7764 case ISD::MUL:
7765 case ISD::FMUL:
7766 case ISD::SDIV:
7767 case ISD::UDIV:
7768 case ISD::FDIV:
7769 case ISD::SREM:
7770 case ISD::UREM:
7771 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00007772 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00007773 case ISD::AND:
7774 case ISD::OR:
7775 case ISD::XOR:
7776 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00007777 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00007778 ScalarizeVectorOp(Node->getOperand(0)),
7779 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00007780 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007781 case ISD::FNEG:
7782 case ISD::FABS:
7783 case ISD::FSQRT:
7784 case ISD::FSIN:
7785 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007786 case ISD::FLOG:
7787 case ISD::FLOG2:
7788 case ISD::FLOG10:
7789 case ISD::FEXP:
7790 case ISD::FEXP2:
Dan Gohman550c9af2008-08-14 20:04:46 +00007791 case ISD::FP_TO_SINT:
7792 case ISD::FP_TO_UINT:
7793 case ISD::SINT_TO_FP:
7794 case ISD::UINT_TO_FP:
7795 case ISD::SIGN_EXTEND:
7796 case ISD::ZERO_EXTEND:
7797 case ISD::ANY_EXTEND:
7798 case ISD::TRUNCATE:
7799 case ISD::FP_EXTEND:
Dan Gohmana8665142007-06-25 16:23:39 +00007800 Result = DAG.getNode(Node->getOpcode(),
7801 NewVT,
7802 ScalarizeVectorOp(Node->getOperand(0)));
7803 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00007804 case ISD::CONVERT_RNDSAT: {
7805 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7806 Result = DAG.getConvertRndSat(NewVT, Op0,
7807 DAG.getValueType(NewVT),
7808 DAG.getValueType(Op0.getValueType()),
7809 Node->getOperand(3),
7810 Node->getOperand(4),
7811 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7812 break;
7813 }
Dan Gohman4f056f32007-10-12 14:13:46 +00007814 case ISD::FPOWI:
Dan Gohman550c9af2008-08-14 20:04:46 +00007815 case ISD::FP_ROUND:
Dan Gohman4f056f32007-10-12 14:13:46 +00007816 Result = DAG.getNode(Node->getOpcode(),
7817 NewVT,
7818 ScalarizeVectorOp(Node->getOperand(0)),
7819 Node->getOperand(1));
7820 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007821 case ISD::LOAD: {
7822 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007823 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7824 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman550c9af2008-08-14 20:04:46 +00007825 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmana8665142007-06-25 16:23:39 +00007826 const Value *SV = LD->getSrcValue();
7827 int SVOffset = LD->getSrcValueOffset();
Dan Gohman550c9af2008-08-14 20:04:46 +00007828 MVT MemoryVT = LD->getMemoryVT();
7829 unsigned Alignment = LD->getAlignment();
7830 bool isVolatile = LD->isVolatile();
7831
7832 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7833 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7834
7835 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7836 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7837 MemoryVT.getVectorElementType(),
7838 isVolatile, Alignment);
Dan Gohmana8665142007-06-25 16:23:39 +00007839
Chris Lattner32206f52006-03-18 01:44:44 +00007840 // Remember that we legalized the chain.
7841 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7842 break;
7843 }
Dan Gohmana8665142007-06-25 16:23:39 +00007844 case ISD::BUILD_VECTOR:
7845 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00007846 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007847 case ISD::INSERT_VECTOR_ELT:
7848 // Returning the inserted scalar element.
7849 Result = Node->getOperand(1);
7850 break;
7851 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00007852 assert(Node->getOperand(0).getValueType() == NewVT &&
7853 "Concat of non-legal vectors not yet supported!");
7854 Result = Node->getOperand(0);
7855 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007856 case ISD::VECTOR_SHUFFLE: {
7857 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007858 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +00007859 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmana8665142007-06-25 16:23:39 +00007860 Result = ScalarizeVectorOp(Node->getOperand(1));
7861 else
7862 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00007863 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007864 }
7865 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang5ca2ec62008-11-06 22:52:21 +00007866 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wang25f01062008-11-10 04:46:22 +00007867 Node->getOperand(1));
Dan Gohman26455c42007-06-13 15:12:02 +00007868 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007869 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007870 SDValue Op0 = Op.getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00007871 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng9ac36312008-05-16 17:19:05 +00007872 Op0 = ScalarizeVectorOp(Op0);
7873 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00007874 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007875 }
Dan Gohmana8665142007-06-25 16:23:39 +00007876 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007877 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007878 ScalarizeVectorOp(Op.getOperand(1)),
7879 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007880 break;
Chris Lattner9d3740e2008-06-30 02:43:01 +00007881 case ISD::SELECT_CC:
7882 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7883 Node->getOperand(1),
7884 ScalarizeVectorOp(Op.getOperand(2)),
7885 ScalarizeVectorOp(Op.getOperand(3)),
7886 Node->getOperand(4));
7887 break;
Nate Begemanb87e63a2008-05-12 23:09:43 +00007888 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007889 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7890 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands8feb6942009-01-01 15:52:00 +00007891 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7892 Op0, Op1, Op.getOperand(2));
Nate Begemanb87e63a2008-05-12 23:09:43 +00007893 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7894 DAG.getConstant(-1ULL, NewVT),
7895 DAG.getConstant(0ULL, NewVT));
7896 break;
7897 }
Chris Lattner32206f52006-03-18 01:44:44 +00007898 }
7899
7900 if (TLI.isTypeLegal(NewVT))
7901 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007902 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7903 assert(isNew && "Value already scalarized?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007904 isNew = isNew;
Chris Lattner32206f52006-03-18 01:44:44 +00007905 return Result;
7906}
7907
Chris Lattnerdc750592005-01-07 07:47:09 +00007908
Mon P Wang58c37942008-10-30 08:01:45 +00007909SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7910 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7911 if (I != WidenNodes.end()) return I->second;
7912
7913 MVT VT = Op.getValueType();
7914 assert(VT.isVector() && "Cannot widen non-vector type!");
7915
7916 SDValue Result;
7917 SDNode *Node = Op.getNode();
7918 MVT EVT = VT.getVectorElementType();
7919
7920 unsigned NumElts = VT.getVectorNumElements();
7921 unsigned NewNumElts = WidenVT.getVectorNumElements();
7922 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7923 assert(NewNumElts < 17);
7924
7925 // When widen is called, it is assumed that it is more efficient to use a
7926 // wide type. The default action is to widen to operation to a wider legal
7927 // vector type and then do the operation if it is legal by calling LegalizeOp
7928 // again. If there is no vector equivalent, we will unroll the operation, do
7929 // it, and rebuild the vector. If most of the operations are vectorizible to
7930 // the legal type, the resulting code will be more efficient. If this is not
7931 // the case, the resulting code will preform badly as we end up generating
7932 // code to pack/unpack the results. It is the function that calls widen
Mon P Wang9a8d60a2008-11-06 05:31:54 +00007933 // that is responsible for seeing this doesn't happen.
Mon P Wang58c37942008-10-30 08:01:45 +00007934 switch (Node->getOpcode()) {
7935 default:
7936#ifndef NDEBUG
7937 Node->dump(&DAG);
7938#endif
7939 assert(0 && "Unexpected operation in WidenVectorOp!");
7940 break;
7941 case ISD::CopyFromReg:
Mon P Wangf414cbc2008-11-15 06:05:52 +00007942 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang58c37942008-10-30 08:01:45 +00007943 case ISD::Constant:
7944 case ISD::ConstantFP:
7945 // To build a vector of these elements, clients should call BuildVector
7946 // and with each element instead of creating a node with a vector type
7947 assert(0 && "Unexpected operation in WidenVectorOp!");
7948 case ISD::VAARG:
7949 // Variable Arguments with vector types doesn't make any sense to me
7950 assert(0 && "Unexpected operation in WidenVectorOp!");
7951 break;
Mon P Wangf414cbc2008-11-15 06:05:52 +00007952 case ISD::UNDEF:
7953 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7954 break;
Mon P Wang58c37942008-10-30 08:01:45 +00007955 case ISD::BUILD_VECTOR: {
7956 // Build a vector with undefined for the new nodes
7957 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7958 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7959 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7960 }
7961 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7962 break;
7963 }
7964 case ISD::INSERT_VECTOR_ELT: {
7965 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7966 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7967 Node->getOperand(1), Node->getOperand(2));
7968 break;
7969 }
7970 case ISD::VECTOR_SHUFFLE: {
7971 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7972 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7973 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7974 // used as permutation array. We build the vector here instead of widening
7975 // because we don't want to legalize and have it turned to something else.
7976 SDValue PermOp = Node->getOperand(2);
7977 SDValueVector NewOps;
7978 MVT PVT = PermOp.getValueType().getVectorElementType();
7979 for (unsigned i = 0; i < NumElts; ++i) {
7980 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7981 NewOps.push_back(PermOp.getOperand(i));
7982 } else {
7983 unsigned Idx =
Mon P Wangf95bd202008-12-13 08:15:14 +00007984 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang58c37942008-10-30 08:01:45 +00007985 if (Idx < NumElts) {
7986 NewOps.push_back(PermOp.getOperand(i));
7987 }
7988 else {
7989 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7990 PermOp.getOperand(i).getValueType()));
7991 }
7992 }
7993 }
7994 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7995 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7996 }
7997
7998 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7999 MVT::getVectorVT(PVT, NewOps.size()),
8000 &NewOps[0], NewOps.size());
8001
8002 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8003 break;
8004 }
8005 case ISD::LOAD: {
8006 // If the load widen returns true, we can use a single load for the
8007 // vector. Otherwise, it is returning a token factor for multiple
8008 // loads.
8009 SDValue TFOp;
8010 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8011 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8012 else
8013 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8014 break;
8015 }
8016
8017 case ISD::BIT_CONVERT: {
8018 SDValue Tmp1 = Node->getOperand(0);
8019 // Converts between two different types so we need to determine
8020 // the correct widen type for the input operand.
Mon P Wanga5016402008-12-18 20:03:17 +00008021 MVT InVT = Tmp1.getValueType();
8022 unsigned WidenSize = WidenVT.getSizeInBits();
8023 if (InVT.isVector()) {
8024 MVT InEltVT = InVT.getVectorElementType();
8025 unsigned InEltSize = InEltVT.getSizeInBits();
8026 assert(WidenSize % InEltSize == 0 &&
8027 "can not widen bit convert that are not multiple of element type");
8028 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8029 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8030 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8031 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8032 } else {
8033 // If the result size is a multiple of the input size, widen the input
8034 // and then convert.
8035 unsigned InSize = InVT.getSizeInBits();
8036 assert(WidenSize % InSize == 0 &&
8037 "can not widen bit convert that are not multiple of element type");
8038 unsigned NewNumElts = WidenSize / InSize;
8039 SmallVector<SDValue, 16> Ops(NewNumElts);
8040 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8041 Ops[0] = Tmp1;
8042 for (unsigned i = 1; i < NewNumElts; ++i)
8043 Ops[i] = UndefVal;
Mon P Wang58c37942008-10-30 08:01:45 +00008044
Mon P Wanga5016402008-12-18 20:03:17 +00008045 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8046 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8047 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang58c37942008-10-30 08:01:45 +00008048 }
8049 break;
8050 }
8051
8052 case ISD::SINT_TO_FP:
8053 case ISD::UINT_TO_FP:
8054 case ISD::FP_TO_SINT:
Mon P Wanga5016402008-12-18 20:03:17 +00008055 case ISD::FP_TO_UINT:
8056 case ISD::FP_ROUND: {
Mon P Wang58c37942008-10-30 08:01:45 +00008057 SDValue Tmp1 = Node->getOperand(0);
8058 // Converts between two different types so we need to determine
8059 // the correct widen type for the input operand.
8060 MVT TVT = Tmp1.getValueType();
8061 assert(TVT.isVector() && "can not widen non vector type");
8062 MVT TEVT = TVT.getVectorElementType();
8063 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8064 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8065 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8066 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang58c37942008-10-30 08:01:45 +00008067 break;
8068 }
8069
8070 case ISD::FP_EXTEND:
8071 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8072 case ISD::TRUNCATE:
8073 case ISD::SIGN_EXTEND:
8074 case ISD::ZERO_EXTEND:
8075 case ISD::ANY_EXTEND:
Mon P Wang58c37942008-10-30 08:01:45 +00008076 case ISD::SIGN_EXTEND_INREG:
8077 case ISD::FABS:
8078 case ISD::FNEG:
8079 case ISD::FSQRT:
8080 case ISD::FSIN:
Mon P Wangf414cbc2008-11-15 06:05:52 +00008081 case ISD::FCOS:
8082 case ISD::CTPOP:
8083 case ISD::CTTZ:
8084 case ISD::CTLZ: {
Mon P Wang58c37942008-10-30 08:01:45 +00008085 // Unary op widening
Mon P Wanga5016402008-12-18 20:03:17 +00008086 SDValue Tmp1;
Mon P Wang58c37942008-10-30 08:01:45 +00008087 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8088 assert(Tmp1.getValueType() == WidenVT);
8089 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang58c37942008-10-30 08:01:45 +00008090 break;
8091 }
Mon P Wang58fb9132008-11-10 20:54:11 +00008092 case ISD::CONVERT_RNDSAT: {
8093 SDValue RndOp = Node->getOperand(3);
8094 SDValue SatOp = Node->getOperand(4);
Mon P Wang58fb9132008-11-10 20:54:11 +00008095 SDValue SrcOp = Node->getOperand(0);
8096
8097 // Converts between two different types so we need to determine
8098 // the correct widen type for the input operand.
8099 MVT SVT = SrcOp.getValueType();
8100 assert(SVT.isVector() && "can not widen non vector type");
8101 MVT SEVT = SVT.getVectorElementType();
8102 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8103
8104 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8105 assert(SrcOp.getValueType() == WidenVT);
8106 SDValue DTyOp = DAG.getValueType(WidenVT);
8107 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8108 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8109
8110 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8111 RndOp, SatOp, CvtCode);
Mon P Wang58fb9132008-11-10 20:54:11 +00008112 break;
8113 }
Mon P Wang58c37942008-10-30 08:01:45 +00008114 case ISD::FPOW:
8115 case ISD::FPOWI:
8116 case ISD::ADD:
8117 case ISD::SUB:
8118 case ISD::MUL:
8119 case ISD::MULHS:
8120 case ISD::MULHU:
8121 case ISD::AND:
8122 case ISD::OR:
8123 case ISD::XOR:
8124 case ISD::FADD:
8125 case ISD::FSUB:
8126 case ISD::FMUL:
8127 case ISD::SDIV:
8128 case ISD::SREM:
8129 case ISD::FDIV:
8130 case ISD::FREM:
8131 case ISD::FCOPYSIGN:
8132 case ISD::UDIV:
8133 case ISD::UREM:
8134 case ISD::BSWAP: {
8135 // Binary op widening
Mon P Wang58c37942008-10-30 08:01:45 +00008136 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8137 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8138 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8139 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang58c37942008-10-30 08:01:45 +00008140 break;
8141 }
8142
8143 case ISD::SHL:
8144 case ISD::SRA:
8145 case ISD::SRL: {
Mon P Wang58c37942008-10-30 08:01:45 +00008146 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8147 assert(Tmp1.getValueType() == WidenVT);
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008148 SDValue ShOp = Node->getOperand(1);
8149 MVT ShVT = ShOp.getValueType();
8150 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8151 WidenVT.getVectorNumElements());
8152 ShOp = WidenVectorOp(ShOp, NewShVT);
8153 assert(ShOp.getValueType() == NewShVT);
8154 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang58c37942008-10-30 08:01:45 +00008155 break;
8156 }
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008157
Mon P Wang58c37942008-10-30 08:01:45 +00008158 case ISD::EXTRACT_VECTOR_ELT: {
8159 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8160 assert(Tmp1.getValueType() == WidenVT);
8161 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8162 break;
8163 }
8164 case ISD::CONCAT_VECTORS: {
8165 // We concurrently support only widen on a multiple of the incoming vector.
8166 // We could widen on a multiple of the incoming operand if necessary.
8167 unsigned NumConcat = NewNumElts / NumElts;
8168 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008169 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang58c37942008-10-30 08:01:45 +00008170 SmallVector<SDValue, 8> MOps;
8171 MOps.push_back(Op);
8172 for (unsigned i = 1; i != NumConcat; ++i) {
8173 MOps.push_back(UndefVal);
8174 }
8175 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8176 &MOps[0], MOps.size()));
8177 break;
8178 }
8179 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wangf414cbc2008-11-15 06:05:52 +00008180 SDValue Tmp1 = Node->getOperand(0);
8181 SDValue Idx = Node->getOperand(1);
8182 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8183 if (CIdx && CIdx->getZExtValue() == 0) {
8184 // Since we are access the start of the vector, the incoming
8185 // vector type might be the proper.
8186 MVT Tmp1VT = Tmp1.getValueType();
8187 if (Tmp1VT == WidenVT)
8188 return Tmp1;
8189 else {
8190 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8191 if (Tmp1VTNumElts < NewNumElts)
8192 Result = WidenVectorOp(Tmp1, WidenVT);
8193 else
8194 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8195 }
8196 } else if (NewNumElts % NumElts == 0) {
8197 // Widen the extracted subvector.
8198 unsigned NumConcat = NewNumElts / NumElts;
8199 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8200 SmallVector<SDValue, 8> MOps;
8201 MOps.push_back(Op);
8202 for (unsigned i = 1; i != NumConcat; ++i) {
8203 MOps.push_back(UndefVal);
8204 }
8205 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8206 &MOps[0], MOps.size()));
8207 } else {
8208 assert(0 && "can not widen extract subvector");
8209 // This could be implemented using insert and build vector but I would
8210 // like to see when this happens.
8211 }
Mon P Wang58c37942008-10-30 08:01:45 +00008212 break;
8213 }
8214
8215 case ISD::SELECT: {
Mon P Wang58c37942008-10-30 08:01:45 +00008216 // Determine new condition widen type and widen
8217 SDValue Cond1 = Node->getOperand(0);
8218 MVT CondVT = Cond1.getValueType();
8219 assert(CondVT.isVector() && "can not widen non vector type");
8220 MVT CondEVT = CondVT.getVectorElementType();
8221 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8222 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8223 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8224
8225 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8226 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8227 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8228 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang58c37942008-10-30 08:01:45 +00008229 break;
8230 }
8231
8232 case ISD::SELECT_CC: {
Mon P Wang58c37942008-10-30 08:01:45 +00008233 // Determine new condition widen type and widen
8234 SDValue Cond1 = Node->getOperand(0);
8235 SDValue Cond2 = Node->getOperand(1);
8236 MVT CondVT = Cond1.getValueType();
8237 assert(CondVT.isVector() && "can not widen non vector type");
8238 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8239 MVT CondEVT = CondVT.getVectorElementType();
8240 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8241 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8242 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8243 assert(Cond1.getValueType() == CondWidenVT &&
8244 Cond2.getValueType() == CondWidenVT && "condition not widen");
8245
8246 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8247 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8248 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8249 "operands not widen");
8250 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8251 Tmp2, Node->getOperand(4));
Mon P Wang58c37942008-10-30 08:01:45 +00008252 break;
Mon P Wang01b8a5a2008-10-30 18:21:52 +00008253 }
8254 case ISD::VSETCC: {
8255 // Determine widen for the operand
8256 SDValue Tmp1 = Node->getOperand(0);
8257 MVT TmpVT = Tmp1.getValueType();
8258 assert(TmpVT.isVector() && "can not widen non vector type");
8259 MVT TmpEVT = TmpVT.getVectorElementType();
8260 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8261 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8262 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wanga5016402008-12-18 20:03:17 +00008263 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang01b8a5a2008-10-30 18:21:52 +00008264 Node->getOperand(2));
Mon P Wang58c37942008-10-30 08:01:45 +00008265 break;
8266 }
Dan Gohman12f24902008-12-23 21:37:04 +00008267 case ISD::ATOMIC_CMP_SWAP:
8268 case ISD::ATOMIC_LOAD_ADD:
8269 case ISD::ATOMIC_LOAD_SUB:
8270 case ISD::ATOMIC_LOAD_AND:
8271 case ISD::ATOMIC_LOAD_OR:
8272 case ISD::ATOMIC_LOAD_XOR:
8273 case ISD::ATOMIC_LOAD_NAND:
8274 case ISD::ATOMIC_LOAD_MIN:
8275 case ISD::ATOMIC_LOAD_MAX:
8276 case ISD::ATOMIC_LOAD_UMIN:
8277 case ISD::ATOMIC_LOAD_UMAX:
8278 case ISD::ATOMIC_SWAP: {
Mon P Wang58c37942008-10-30 08:01:45 +00008279 // For now, we assume that using vectors for these operations don't make
8280 // much sense so we just split it. We return an empty result
8281 SDValue X, Y;
8282 SplitVectorOp(Op, X, Y);
8283 return Result;
8284 break;
8285 }
8286
8287 } // end switch (Node->getOpcode())
8288
8289 assert(Result.getNode() && "Didn't set a result!");
8290 if (Result != Op)
8291 Result = LegalizeOp(Result);
8292
Mon P Wang9a8d60a2008-11-06 05:31:54 +00008293 AddWidenedOperand(Op, Result);
Mon P Wang58c37942008-10-30 08:01:45 +00008294 return Result;
8295}
8296
8297// Utility function to find a legal vector type and its associated element
8298// type from a preferred width and whose vector type must be the same size
8299// as the VVT.
8300// TLI: Target lowering used to determine legal types
8301// Width: Preferred width of element type
8302// VVT: Vector value type whose size we must match.
8303// Returns VecEVT and EVT - the vector type and its associated element type
8304static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8305 MVT& EVT, MVT& VecEVT) {
8306 // We start with the preferred width, make it a power of 2 and see if
8307 // we can find a vector type of that width. If not, we reduce it by
8308 // another power of 2. If we have widen the type, a vector of bytes should
8309 // always be legal.
8310 assert(TLI.isTypeLegal(VVT));
8311 unsigned EWidth = Width + 1;
8312 do {
8313 assert(EWidth > 0);
8314 EWidth = (1 << Log2_32(EWidth-1));
8315 EVT = MVT::getIntegerVT(EWidth);
8316 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8317 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8318 } while (!TLI.isTypeLegal(VecEVT) ||
8319 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8320}
8321
8322SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8323 SDValue Chain,
8324 SDValue BasePtr,
8325 const Value *SV,
8326 int SVOffset,
8327 unsigned Alignment,
8328 bool isVolatile,
8329 unsigned LdWidth,
8330 MVT ResType) {
8331 // We assume that we have good rules to handle loading power of two loads so
8332 // we break down the operations to power of 2 loads. The strategy is to
8333 // load the largest power of 2 that we can easily transform to a legal vector
8334 // and then insert into that vector, and the cast the result into the legal
8335 // vector that we want. This avoids unnecessary stack converts.
8336 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8337 // the load is nonvolatile, we an use a wider load for the value.
8338 // Find a vector length we can load a large chunk
8339 MVT EVT, VecEVT;
8340 unsigned EVTWidth;
8341 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8342 EVTWidth = EVT.getSizeInBits();
8343
8344 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8345 isVolatile, Alignment);
8346 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8347 LdChain.push_back(LdOp.getValue(1));
8348
8349 // Check if we can load the element with one instruction
8350 if (LdWidth == EVTWidth) {
8351 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8352 }
8353
8354 // The vector element order is endianness dependent.
8355 unsigned Idx = 1;
8356 LdWidth -= EVTWidth;
8357 unsigned Offset = 0;
8358
8359 while (LdWidth > 0) {
8360 unsigned Increment = EVTWidth / 8;
8361 Offset += Increment;
8362 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8363 DAG.getIntPtrConstant(Increment));
8364
8365 if (LdWidth < EVTWidth) {
8366 // Our current type we are using is too large, use a smaller size by
8367 // using a smaller power of 2
8368 unsigned oEVTWidth = EVTWidth;
8369 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8370 EVTWidth = EVT.getSizeInBits();
8371 // Readjust position and vector position based on new load type
Mon P Wangf414cbc2008-11-15 06:05:52 +00008372 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang58c37942008-10-30 08:01:45 +00008373 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8374 }
8375
8376 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8377 SVOffset+Offset, isVolatile,
8378 MinAlign(Alignment, Offset));
8379 LdChain.push_back(LdOp.getValue(1));
8380 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8381 DAG.getIntPtrConstant(Idx++));
8382
8383 LdWidth -= EVTWidth;
8384 }
8385
8386 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8387}
8388
8389bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8390 SDValue& TFOp,
8391 SDValue Op,
8392 MVT NVT) {
8393 // TODO: Add support for ConcatVec and the ability to load many vector
8394 // types (e.g., v4i8). This will not work when a vector register
8395 // to memory mapping is strange (e.g., vector elements are not
8396 // stored in some sequential order).
8397
8398 // It must be true that the widen vector type is bigger than where
8399 // we need to load from.
8400 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8401 MVT LdVT = LD->getMemoryVT();
8402 assert(LdVT.isVector() && NVT.isVector());
8403 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8404
8405 // Load information
8406 SDValue Chain = LD->getChain();
8407 SDValue BasePtr = LD->getBasePtr();
8408 int SVOffset = LD->getSrcValueOffset();
8409 unsigned Alignment = LD->getAlignment();
8410 bool isVolatile = LD->isVolatile();
8411 const Value *SV = LD->getSrcValue();
8412 unsigned int LdWidth = LdVT.getSizeInBits();
8413
8414 // Load value as a large register
8415 SDValueVector LdChain;
8416 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8417 Alignment, isVolatile, LdWidth, NVT);
8418
8419 if (LdChain.size() == 1) {
8420 TFOp = LdChain[0];
8421 return true;
8422 }
8423 else {
8424 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8425 return false;
8426 }
8427}
8428
8429
8430void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8431 SDValue Chain,
8432 SDValue BasePtr,
8433 const Value *SV,
8434 int SVOffset,
8435 unsigned Alignment,
8436 bool isVolatile,
Mon P Wangf414cbc2008-11-15 06:05:52 +00008437 SDValue ValOp,
Mon P Wang58c37942008-10-30 08:01:45 +00008438 unsigned StWidth) {
8439 // Breaks the stores into a series of power of 2 width stores. For any
8440 // width, we convert the vector to the vector of element size that we
8441 // want to store. This avoids requiring a stack convert.
8442
8443 // Find a width of the element type we can store with
8444 MVT VVT = ValOp.getValueType();
8445 MVT EVT, VecEVT;
8446 unsigned EVTWidth;
8447 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8448 EVTWidth = EVT.getSizeInBits();
8449
8450 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8451 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang5ca2ec62008-11-06 22:52:21 +00008452 DAG.getIntPtrConstant(0));
Mon P Wang58c37942008-10-30 08:01:45 +00008453 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8454 isVolatile, Alignment);
8455 StChain.push_back(StOp);
8456
8457 // Check if we are done
8458 if (StWidth == EVTWidth) {
8459 return;
8460 }
8461
8462 unsigned Idx = 1;
8463 StWidth -= EVTWidth;
8464 unsigned Offset = 0;
8465
8466 while (StWidth > 0) {
8467 unsigned Increment = EVTWidth / 8;
8468 Offset += Increment;
8469 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8470 DAG.getIntPtrConstant(Increment));
8471
8472 if (StWidth < EVTWidth) {
8473 // Our current type we are using is too large, use a smaller size by
8474 // using a smaller power of 2
8475 unsigned oEVTWidth = EVTWidth;
8476 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8477 EVTWidth = EVT.getSizeInBits();
8478 // Readjust position and vector position based on new load type
Mon P Wangf414cbc2008-11-15 06:05:52 +00008479 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang58c37942008-10-30 08:01:45 +00008480 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8481 }
8482
8483 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wangf414cbc2008-11-15 06:05:52 +00008484 DAG.getIntPtrConstant(Idx++));
Mon P Wang58c37942008-10-30 08:01:45 +00008485 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8486 SVOffset + Offset, isVolatile,
8487 MinAlign(Alignment, Offset)));
8488 StWidth -= EVTWidth;
8489 }
8490}
8491
8492
8493SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8494 SDValue Chain,
8495 SDValue BasePtr) {
8496 // TODO: It might be cleaner if we can use SplitVector and have more legal
8497 // vector types that can be stored into memory (e.g., v4xi8 can
8498 // be stored as a word). This will not work when a vector register
8499 // to memory mapping is strange (e.g., vector elements are not
8500 // stored in some sequential order).
8501
8502 MVT StVT = ST->getMemoryVT();
8503 SDValue ValOp = ST->getValue();
8504
8505 // Check if we have widen this node with another value
8506 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8507 if (I != WidenNodes.end())
8508 ValOp = I->second;
8509
8510 MVT VVT = ValOp.getValueType();
8511
8512 // It must be true that we the widen vector type is bigger than where
8513 // we need to store.
8514 assert(StVT.isVector() && VVT.isVector());
8515 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8516 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8517
8518 // Store value
8519 SDValueVector StChain;
8520 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8521 ST->getSrcValueOffset(), ST->getAlignment(),
8522 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8523 if (StChain.size() == 1)
8524 return StChain[0];
8525 else
8526 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8527}
8528
8529
Chris Lattnerdc750592005-01-07 07:47:09 +00008530// SelectionDAG::Legalize - This is the entry point for the file.
8531//
Duncan Sandsf312dc72008-12-14 09:43:15 +00008532void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattnerdc750592005-01-07 07:47:09 +00008533 /// run - This is the main entry point to this class.
8534 ///
Duncan Sandsf312dc72008-12-14 09:43:15 +00008535 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00008536}
8537