blob: 0a10e4f6d81326e2ab9794475a4158e307388e0d [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"
Dan Gohman2d489b52008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Dan Gohman544ab2c2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000035#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner462505f2006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner462505f2006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattnerdc750592005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000071 };
Chris Lattner462505f2006-02-13 09:18:02 +000072
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000077
Chris Lattnerdc750592005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000087
Chris Lattner32206f52006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang58c37942008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Chris Lattner32206f52006-03-18 01:44:44 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000092
Chris Lattner32206f52006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang58c37942008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Chris Lattner32206f52006-03-18 01:44:44 +000095 /// to avoid splitting the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattner32206f52006-03-18 01:44:44 +000097
Dan Gohmana8665142007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000102
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang58c37942008-10-30 08:01:45 +0000106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000113 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman38740a92008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Evan Chenga8fd1f22008-11-24 07:09:49 +0000117 isNew = isNew;
Chris Lattner2af3ee42005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 }
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000121 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang58c37942008-10-30 08:01:45 +0000122 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
123 assert(isNew && "Got into the map somehow?");
Evan Chenga8fd1f22008-11-24 07:09:49 +0000124 isNew = isNew;
Mon P Wang58c37942008-10-30 08:01:45 +0000125 // If someone requests legalization of the new node, return itself.
126 LegalizedNodes.insert(std::make_pair(To, To));
127 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000128
Chris Lattnerdc750592005-01-07 07:47:09 +0000129public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000130 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000131
Chris Lattnerdc750592005-01-07 07:47:09 +0000132 /// getTypeAction - Return how we should legalize values of this type, either
133 /// it is already legal or we need to expand it into multiple registers of
134 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000135 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000136 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000137 }
138
139 /// isTypeLegal - Return true if this type is legal on this target.
140 ///
Duncan Sands13237ac2008-06-06 12:08:01 +0000141 bool isTypeLegal(MVT VT) const {
Chris Lattnerdc750592005-01-07 07:47:09 +0000142 return getTypeAction(VT) == Legal;
143 }
144
Chris Lattnerdc750592005-01-07 07:47:09 +0000145 void LegalizeDAG();
146
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000147private:
Dan Gohmana8665142007-06-25 16:23:39 +0000148 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000149 /// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000150 void HandleOp(SDValue Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000151
152 /// LegalizeOp - We know that the specified value has a legal type.
153 /// Recursively ensure that the operands have legal types, then return the
154 /// result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000155 SDValue LegalizeOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000156
Dan Gohman2a7de412007-10-11 23:57:53 +0000157 /// UnrollVectorOp - We know that the given vector has a legal type, however
158 /// the operation it performs is not legal and is an operation that we have
159 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
160 /// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000161 SDValue UnrollVectorOp(SDValue O);
Nate Begeman6f94f612008-04-25 18:07:40 +0000162
163 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
164 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
165 /// is necessary to spill the vector being inserted into to memory, perform
166 /// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000167 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
168 SDValue Idx);
Dan Gohman2a7de412007-10-11 23:57:53 +0000169
Chris Lattner32206f52006-03-18 01:44:44 +0000170 /// PromoteOp - Given an operation that produces a value in an invalid type,
171 /// promote it to compute the value into a larger type. The produced value
172 /// will have the correct bits for the low portion of the register, but no
173 /// guarantee is made about the top bits: it may be zero, sign-extended, or
174 /// garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000175 SDValue PromoteOp(SDValue O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000176
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000177 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner32206f52006-03-18 01:44:44 +0000178 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman3a293e72008-10-01 15:07:49 +0000179 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattner32206f52006-03-18 01:44:44 +0000180 /// the ExpandedNodes map is filled in for any results that are expanded, and
181 /// the Lo/Hi values are returned. This applies to integer types and Vector
182 /// types.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000183 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000184
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000185 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
186 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
187 /// for the existing elements but no guarantee is made about the new elements
188 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
189 /// when we have an instruction operating on an illegal vector type and we
190 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang58c37942008-10-30 08:01:45 +0000191 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
192
Dan Gohmana8665142007-06-25 16:23:39 +0000193 /// SplitVectorOp - Given an operand of vector type, break it down into
194 /// two smaller values.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000195 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000196
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000197 /// ScalarizeVectorOp - Given an operand of single-element vector type
198 /// (e.g. v1f32), convert it into the equivalent operation that returns a
199 /// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000200 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000201
Mon P Wang9a8d60a2008-11-06 05:31:54 +0000202 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang58c37942008-10-30 08:01:45 +0000203 typedef SmallVector<SDValue, 16> SDValueVector;
204
205 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
206 /// the LdChain contains a single load and false if it contains a token
207 /// factor for multiple loads. It takes
208 /// Result: location to return the result
209 /// LdChain: location to return the load chain
210 /// Op: load operation to widen
211 /// NVT: widen vector result type we want for the load
212 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
213 SDValue Op, MVT NVT);
214
215 /// Helper genWidenVectorLoads - Helper function to generate a set of
216 /// loads to load a vector with a resulting wider type. It takes
217 /// LdChain: list of chains for the load we have generated
218 /// Chain: incoming chain for the ld vector
219 /// BasePtr: base pointer to load from
220 /// SV: memory disambiguation source value
221 /// SVOffset: memory disambiugation offset
222 /// Alignment: alignment of the memory
223 /// isVolatile: volatile load
224 /// LdWidth: width of memory that we want to load
225 /// ResType: the wider result result type for the resulting loaded vector
226 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
227 SDValue BasePtr, const Value *SV,
228 int SVOffset, unsigned Alignment,
229 bool isVolatile, unsigned LdWidth,
230 MVT ResType);
231
232 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
233 /// location. It takes
234 /// ST: store node that we want to replace
235 /// Chain: incoming store chain
236 /// BasePtr: base address of where we want to store into
237 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
238 SDValue BasePtr);
239
240 /// Helper genWidenVectorStores - Helper function to generate a set of
241 /// stores to store a widen vector into non widen memory
242 // It takes
243 // StChain: list of chains for the stores we have generated
244 // Chain: incoming chain for the ld vector
245 // BasePtr: base pointer to load from
246 // SV: memory disambiguation source value
247 // SVOffset: memory disambiugation offset
248 // Alignment: alignment of the memory
249 // isVolatile: volatile lod
250 // ValOp: value to store
251 // StWidth: width of memory that we want to store
252 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
253 SDValue BasePtr, const Value *SV,
254 int SVOffset, unsigned Alignment,
255 bool isVolatile, SDValue ValOp,
256 unsigned StWidth);
257
Duncan Sandsb0e39382008-07-21 10:20:31 +0000258 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner6be79822006-04-04 17:23:26 +0000259 /// specified mask and type. Targets can specify exactly which masks they
260 /// support and the code generator is tasked with not creating illegal masks.
261 ///
262 /// Note that this will also return true for shuffles that are promoted to a
263 /// different type.
264 ///
265 /// If this is a legal shuffle, this method returns the (possibly promoted)
266 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000267 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner6be79822006-04-04 17:23:26 +0000268
Chris Lattner4488f0c2006-07-26 23:55:56 +0000269 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000270 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000271
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000272 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng3b0f5e42008-10-15 02:05:31 +0000273 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
274 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
275 LegalizeSetCCOperands(LHS, RHS, CC);
276 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
277 }
Nate Begeman7e7f4392006-02-01 07:19:44 +0000278
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000279 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
280 SDValue &Hi);
281 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000282
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000283 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
284 SDValue ExpandBUILD_VECTOR(SDNode *Node);
285 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman550c9af2008-08-14 20:04:46 +0000286 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000287 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
288 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
289 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000290
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000291 SDValue ExpandBSWAP(SDValue Op);
292 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
293 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
295 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
296 SDValue &Lo, SDValue &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000297
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000298 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
299 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wang4dd832d2008-12-09 05:46:39 +0000300
301 // Returns the legalized (truncated or extended) shift amount.
302 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattnerdc750592005-01-07 07:47:09 +0000303};
304}
305
Chris Lattner6be79822006-04-04 17:23:26 +0000306/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
307/// specified mask and type. Targets can specify exactly which masks they
308/// support and the code generator is tasked with not creating illegal masks.
309///
310/// Note that this will also return true for shuffles that are promoted to a
311/// different type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000312SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner6be79822006-04-04 17:23:26 +0000313 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
314 default: return 0;
315 case TargetLowering::Legal:
316 case TargetLowering::Custom:
317 break;
318 case TargetLowering::Promote: {
319 // If this is promoted to a different type, convert the shuffle mask and
320 // ask if it is legal in the promoted type!
Duncan Sands13237ac2008-06-06 12:08:01 +0000321 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsb0e39382008-07-21 10:20:31 +0000322 MVT EltVT = NVT.getVectorElementType();
Chris Lattner6be79822006-04-04 17:23:26 +0000323
324 // If we changed # elements, change the shuffle mask.
325 unsigned NumEltsGrowth =
Duncan Sands13237ac2008-06-06 12:08:01 +0000326 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner6be79822006-04-04 17:23:26 +0000327 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
328 if (NumEltsGrowth > 1) {
329 // Renumber the elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000330 SmallVector<SDValue, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000331 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000332 SDValue InOp = Mask.getOperand(i);
Chris Lattner6be79822006-04-04 17:23:26 +0000333 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
334 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsb0e39382008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000336 else {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000337 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsb0e39382008-07-21 10:20:31 +0000338 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000339 }
340 }
341 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000342 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000343 }
344 VT = NVT;
345 break;
346 }
347 }
Gabor Greiff304a7a2008-08-28 21:40:38 +0000348 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner6be79822006-04-04 17:23:26 +0000349}
350
Chris Lattner4add7e32005-01-23 04:42:50 +0000351SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
352 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
353 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000354 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000355 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000356}
357
Chris Lattnerdc750592005-01-07 07:47:09 +0000358void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000359 LastCALLSEQ_END = DAG.getEntryNode();
360 IsLegalizingCall = false;
361
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000362 // The legalize process is inherently a bottom-up recursive process (users
363 // legalize their uses before themselves). Given infinite stack space, we
364 // could just start legalizing on the root and traverse the whole graph. In
365 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000366 // blocks. To avoid this problem, compute an ordering of the nodes where each
367 // node is only legalized after all of its operands are legalized.
Dan Gohman86aa16a2008-09-30 18:30:35 +0000368 DAG.AssignTopologicalOrder();
369 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
370 E = prior(DAG.allnodes_end()); I != next(E); ++I)
371 HandleOp(SDValue(I, 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000372
373 // Finally, it's possible the root changed. Get the new root.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000374 SDValue OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000375 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
376 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000377
378 ExpandedNodes.clear();
379 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000380 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000381 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000382 ScalarizedNodes.clear();
Mon P Wang58c37942008-10-30 08:01:45 +0000383 WidenNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000384
385 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000386 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000387}
388
Chris Lattner462505f2006-02-13 09:18:02 +0000389
390/// FindCallEndFromCallStart - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_END node that terminates the call sequence.
392static SDNode *FindCallEndFromCallStart(SDNode *Node) {
393 if (Node->getOpcode() == ISD::CALLSEQ_END)
394 return Node;
395 if (Node->use_empty())
396 return 0; // No CallSeqEnd
397
398 // The chain is usually at the end.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000399 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner462505f2006-02-13 09:18:02 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Sometimes it's at the beginning.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, 0);
Chris Lattner462505f2006-02-13 09:18:02 +0000403 if (TheChain.getValueType() != MVT::Other) {
404 // Otherwise, hunt for it.
405 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
406 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000407 TheChain = SDValue(Node, i);
Chris Lattner462505f2006-02-13 09:18:02 +0000408 break;
409 }
410
411 // Otherwise, we walked into a node without a chain.
412 if (TheChain.getValueType() != MVT::Other)
413 return 0;
414 }
415 }
416
417 for (SDNode::use_iterator UI = Node->use_begin(),
418 E = Node->use_end(); UI != E; ++UI) {
419
420 // Make sure to only follow users of our token chain.
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000421 SDNode *User = *UI;
Chris Lattner462505f2006-02-13 09:18:02 +0000422 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
423 if (User->getOperand(i) == TheChain)
424 if (SDNode *Result = FindCallEndFromCallStart(User))
425 return Result;
426 }
427 return 0;
428}
429
430/// FindCallStartFromCallEnd - Given a chained node that is part of a call
431/// sequence, find the CALLSEQ_START node that initiates the call sequence.
432static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
433 assert(Node && "Didn't find callseq_start for a call??");
434 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
435
436 assert(Node->getOperand(0).getValueType() == MVT::Other &&
437 "Node doesn't have a token chain argument!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000438 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner462505f2006-02-13 09:18:02 +0000439}
440
441/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
442/// see if any uses can reach Dest. If no dest operands can get to dest,
443/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000444///
445/// Keep track of the nodes we fine that actually do lead to Dest in
446/// NodesLeadingTo. This avoids retraversing them exponential number of times.
447///
448bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000449 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000450 if (N == Dest) return true; // N certainly leads to Dest :)
451
Chris Lattner4488f0c2006-07-26 23:55:56 +0000452 // If we've already processed this node and it does lead to Dest, there is no
453 // need to reprocess it.
454 if (NodesLeadingTo.count(N)) return true;
455
Chris Lattner462505f2006-02-13 09:18:02 +0000456 // If the first result of this node has been already legalized, then it cannot
457 // reach N.
458 switch (getTypeAction(N->getValueType(0))) {
459 case Legal:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000460 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000461 break;
462 case Promote:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000463 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000464 break;
465 case Expand:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000466 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000467 break;
468 }
469
470 // Okay, this node has not already been legalized. Check and legalize all
471 // operands. If none lead to Dest, then we can legalize this node.
472 bool OperandsLeadToDest = false;
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
474 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000475 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000476
Chris Lattner4488f0c2006-07-26 23:55:56 +0000477 if (OperandsLeadToDest) {
478 NodesLeadingTo.insert(N);
479 return true;
480 }
Chris Lattner462505f2006-02-13 09:18:02 +0000481
482 // Okay, this node looks safe, legalize it and return false.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000483 HandleOp(SDValue(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000484 return false;
485}
486
Mon P Wang58c37942008-10-30 08:01:45 +0000487/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000488/// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000489void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000490 MVT VT = Op.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000491 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000492 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000493 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang58c37942008-10-30 08:01:45 +0000494 case Promote:
495 if (!VT.isVector()) {
496 (void)PromoteOp(Op);
497 break;
498 }
499 else {
500 // See if we can widen otherwise use Expand to either scalarize or split
501 MVT WidenVT = TLI.getWidenVectorType(VT);
502 if (WidenVT != MVT::Other) {
503 (void) WidenVectorOp(Op, WidenVT);
504 break;
505 }
506 // else fall thru to expand since we can't widen the vector
507 }
Chris Lattner32206f52006-03-18 01:44:44 +0000508 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +0000509 if (!VT.isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +0000510 // If this is an illegal scalar, expand it into its two component
511 // pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000512 SDValue X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000513 if (Op.getOpcode() == ISD::TargetConstant)
514 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000515 ExpandOp(Op, X, Y);
Duncan Sands13237ac2008-06-06 12:08:01 +0000516 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmana8665142007-06-25 16:23:39 +0000517 // If this is an illegal single element vector, convert it to a
518 // scalar operation.
519 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000520 } else {
Mon P Wang58c37942008-10-30 08:01:45 +0000521 // This is an illegal multiple element vector.
Dan Gohmana8665142007-06-25 16:23:39 +0000522 // Split it in half and legalize both parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000523 SDValue X, Y;
Dan Gohmana8665142007-06-25 16:23:39 +0000524 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000525 }
526 break;
527 }
528}
Chris Lattner462505f2006-02-13 09:18:02 +0000529
Evan Cheng22cf8992006-12-13 20:57:08 +0000530/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
531/// a load from the constant pool.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000532static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000533 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000534 bool Extend = false;
535
536 // If a FP immediate is precise when represented as a float and if the
537 // target can do an extending load from float to double, we put it into
538 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000539 // double. This shrinks FP constants and canonicalizes them for targets where
540 // an FP extending load is the same cost as a normal load (such as on the x87
541 // fp stack or PPC FP unit).
Duncan Sands13237ac2008-06-06 12:08:01 +0000542 MVT VT = CFP->getValueType(0);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000543 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000544 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000545 if (VT!=MVT::f64 && VT!=MVT::f32)
546 assert(0 && "Invalid type expansion");
Dale Johannesen54306fe2008-10-09 18:53:47 +0000547 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng38caf772008-03-04 08:05:30 +0000548 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000549 }
550
Duncan Sands13237ac2008-06-06 12:08:01 +0000551 MVT OrigVT = VT;
552 MVT SVT = VT;
Evan Cheng38caf772008-03-04 08:05:30 +0000553 while (SVT != MVT::f32) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000554 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng38caf772008-03-04 08:05:30 +0000555 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
556 // Only do this if the target has a native EXTLOAD instruction from
557 // smaller type.
Evan Cheng07d53b12008-10-14 21:26:46 +0000558 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000559 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000560 const Type *SType = SVT.getTypeForMVT();
Evan Cheng38caf772008-03-04 08:05:30 +0000561 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
562 VT = SVT;
563 Extend = true;
564 }
Evan Cheng47833a12006-12-12 21:32:44 +0000565 }
566
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000567 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000568 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng38caf772008-03-04 08:05:30 +0000569 if (Extend)
570 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000571 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000572 0, VT, false, Alignment);
Evan Cheng38caf772008-03-04 08:05:30 +0000573 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +0000574 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +0000575}
576
Chris Lattner462505f2006-02-13 09:18:02 +0000577
Evan Cheng003feb02007-01-04 21:56:39 +0000578/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
579/// operations.
580static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000581SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
582 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000583 MVT VT = Node->getValueType(0);
584 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000585 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
586 "fcopysign expansion only supported for f32 and f64");
Duncan Sands13237ac2008-06-06 12:08:01 +0000587 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000588
Evan Cheng003feb02007-01-04 21:56:39 +0000589 // First get the sign bit of second operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000590 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000591 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
592 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000593 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000594 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000595 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000596 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands13237ac2008-06-06 12:08:01 +0000597 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng003feb02007-01-04 21:56:39 +0000598 if (SizeDiff > 0) {
599 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
600 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
601 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattner87909d02008-07-10 23:46:13 +0000602 } else if (SizeDiff < 0) {
603 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
604 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
605 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
606 }
Evan Cheng3b841dd2007-01-05 21:31:51 +0000607
608 // Clear the sign bit of first operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000609 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng3b841dd2007-01-05 21:31:51 +0000610 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
611 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
612 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000613 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000614 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
615
616 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000617 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
618 return Result;
619}
620
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000621/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
622static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000623SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
624 TargetLowering &TLI) {
625 SDValue Chain = ST->getChain();
626 SDValue Ptr = ST->getBasePtr();
627 SDValue Val = ST->getValue();
Duncan Sands13237ac2008-06-06 12:08:01 +0000628 MVT VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000629 int Alignment = ST->getAlignment();
630 int SVOffset = ST->getSrcValueOffset();
Duncan Sands13237ac2008-06-06 12:08:01 +0000631 if (ST->getMemoryVT().isFloatingPoint() ||
632 ST->getMemoryVT().isVector()) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000633 // Expand to a bitconvert of the value to the integer type of the
634 // same size, then a (misaligned) int store.
Duncan Sands13237ac2008-06-06 12:08:01 +0000635 MVT intVT;
636 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000637 intVT = MVT::i128;
Duncan Sands13237ac2008-06-06 12:08:01 +0000638 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000639 intVT = MVT::i64;
640 else if (VT==MVT::f32)
641 intVT = MVT::i32;
642 else
Dale Johannesenc4c3de22008-02-28 18:36:51 +0000643 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000645 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000646 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
647 SVOffset, ST->isVolatile(), Alignment);
648 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000649 assert(ST->getMemoryVT().isInteger() &&
650 !ST->getMemoryVT().isVector() &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000651 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000652 // Get the half-size VT
Duncan Sands13237ac2008-06-06 12:08:01 +0000653 MVT NewStoredVT =
654 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
655 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000656 int IncrementSize = NumBits / 8;
657
658 // Divide the stored value in two parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000659 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
660 SDValue Lo = Val;
661 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000662
663 // Store the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000664 SDValue Store1, Store2;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000665 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
666 ST->getSrcValue(), SVOffset, NewStoredVT,
667 ST->isVolatile(), Alignment);
668 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
669 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000670 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000671 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
672 ST->getSrcValue(), SVOffset + IncrementSize,
673 NewStoredVT, ST->isVolatile(), Alignment);
674
675 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
676}
677
678/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
679static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000680SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
681 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000682 int SVOffset = LD->getSrcValueOffset();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000683 SDValue Chain = LD->getChain();
684 SDValue Ptr = LD->getBasePtr();
Duncan Sands13237ac2008-06-06 12:08:01 +0000685 MVT VT = LD->getValueType(0);
686 MVT LoadedVT = LD->getMemoryVT();
687 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000688 // Expand to a (misaligned) integer load of the same size,
Dale Johannesenbf76a082008-02-27 22:36:00 +0000689 // then bitconvert to floating point or vector.
Duncan Sands13237ac2008-06-06 12:08:01 +0000690 MVT intVT;
691 if (LoadedVT.is128BitVector() ||
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000692 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000693 intVT = MVT::i128;
Duncan Sands13237ac2008-06-06 12:08:01 +0000694 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000695 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000696 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000697 intVT = MVT::i32;
698 else
Dale Johannesenbf76a082008-02-27 22:36:00 +0000699 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000700
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000701 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000702 SVOffset, LD->isVolatile(),
703 LD->getAlignment());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000704 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands13237ac2008-06-06 12:08:01 +0000705 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000706 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
707
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000708 SDValue Ops[] = { Result, Chain };
Duncan Sands739a0542008-07-02 17:40:58 +0000709 return DAG.getMergeValues(Ops, 2);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000710 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000711 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner09c03932007-11-19 21:38:03 +0000712 "Unaligned load of unsupported type.");
713
Dale Johannesenbf76a082008-02-27 22:36:00 +0000714 // Compute the new VT that is half the size of the old one. This is an
715 // integer MVT.
Duncan Sands13237ac2008-06-06 12:08:01 +0000716 unsigned NumBits = LoadedVT.getSizeInBits();
717 MVT NewLoadedVT;
718 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000719 NumBits >>= 1;
720
721 unsigned Alignment = LD->getAlignment();
722 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000723 ISD::LoadExtType HiExtType = LD->getExtensionType();
724
725 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
726 if (HiExtType == ISD::NON_EXTLOAD)
727 HiExtType = ISD::ZEXTLOAD;
728
729 // Load the value in two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000730 SDValue Lo, Hi;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000731 if (TLI.isLittleEndian()) {
732 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
733 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
734 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
735 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
736 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
737 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000738 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000739 } else {
740 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
741 NewLoadedVT,LD->isVolatile(), Alignment);
742 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
743 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
744 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
745 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000746 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000747 }
748
749 // aggregate the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000750 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
751 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000752 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
753
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000754 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000755 Hi.getValue(1));
756
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000757 SDValue Ops[] = { Result, TF };
Duncan Sands739a0542008-07-02 17:40:58 +0000758 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000759}
Evan Cheng003feb02007-01-04 21:56:39 +0000760
Dan Gohman2a7de412007-10-11 23:57:53 +0000761/// UnrollVectorOp - We know that the given vector has a legal type, however
762/// the operation it performs is not legal and is an operation that we have
763/// no way of lowering. "Unroll" the vector, splitting out the scalars and
764/// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000765SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000766 MVT VT = Op.getValueType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000767 assert(isTypeLegal(VT) &&
768 "Caller should expand or promote operands that are not legal!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000769 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman2a7de412007-10-11 23:57:53 +0000770 "Can't unroll a vector with multiple results!");
Duncan Sands13237ac2008-06-06 12:08:01 +0000771 unsigned NE = VT.getVectorNumElements();
772 MVT EltVT = VT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000773
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000774 SmallVector<SDValue, 8> Scalars;
775 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman2a7de412007-10-11 23:57:53 +0000776 for (unsigned i = 0; i != NE; ++i) {
777 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000778 SDValue Operand = Op.getOperand(j);
Duncan Sands13237ac2008-06-06 12:08:01 +0000779 MVT OperandVT = Operand.getValueType();
780 if (OperandVT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +0000781 // A vector operand; extract a single element.
Duncan Sands13237ac2008-06-06 12:08:01 +0000782 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000783 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
784 OperandEltVT,
785 Operand,
786 DAG.getConstant(i, MVT::i32));
787 } else {
788 // A scalar operand; just use it as is.
789 Operands[j] = Operand;
790 }
791 }
Mon P Wang4dd832d2008-12-09 05:46:39 +0000792
793 switch (Op.getOpcode()) {
794 default:
795 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
796 &Operands[0], Operands.size()));
797 break;
798 case ISD::SHL:
799 case ISD::SRA:
800 case ISD::SRL:
801 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
802 LegalizeShiftAmount(Operands[1])));
803 break;
804 }
Dan Gohman2a7de412007-10-11 23:57:53 +0000805 }
806
807 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
808}
809
Duncan Sands53c954f2008-01-10 10:28:30 +0000810/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000811static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands53c954f2008-01-10 10:28:30 +0000812 RTLIB::Libcall Call_F32,
813 RTLIB::Libcall Call_F64,
814 RTLIB::Libcall Call_F80,
815 RTLIB::Libcall Call_PPCF128) {
816 return
817 VT == MVT::f32 ? Call_F32 :
818 VT == MVT::f64 ? Call_F64 :
819 VT == MVT::f80 ? Call_F80 :
820 VT == MVT::ppcf128 ? Call_PPCF128 :
821 RTLIB::UNKNOWN_LIBCALL;
822}
823
Nate Begeman6f94f612008-04-25 18:07:40 +0000824/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
825/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
826/// is necessary to spill the vector being inserted into to memory, perform
827/// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000828SDValue SelectionDAGLegalize::
829PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
830 SDValue Tmp1 = Vec;
831 SDValue Tmp2 = Val;
832 SDValue Tmp3 = Idx;
Nate Begeman6f94f612008-04-25 18:07:40 +0000833
834 // If the target doesn't support this, we have to spill the input vector
835 // to a temporary stack slot, update the element, then reload it. This is
836 // badness. We could also load the value into a vector register (either
837 // with a "move to register" or "extload into register" instruction, then
838 // permute it into place, if the idx is a constant and if the idx is
839 // supported by the target.
Duncan Sands13237ac2008-06-06 12:08:01 +0000840 MVT VT = Tmp1.getValueType();
841 MVT EltVT = VT.getVectorElementType();
842 MVT IdxVT = Tmp3.getValueType();
843 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000844 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000845
Gabor Greiff304a7a2008-08-28 21:40:38 +0000846 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman6f94f612008-04-25 18:07:40 +0000847
848 // Store the vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000849 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang58c37942008-10-30 08:01:45 +0000850 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000851
852 // Truncate or zero extend offset to target pointer type.
Duncan Sands11dd4242008-06-08 20:54:56 +0000853 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman6f94f612008-04-25 18:07:40 +0000854 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
855 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +0000856 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman6f94f612008-04-25 18:07:40 +0000857 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000858 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman6f94f612008-04-25 18:07:40 +0000859 // Store the scalar value.
860 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000861 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000862 // Load the updated vector.
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000863 return DAG.getLoad(VT, Ch, StackPtr,
864 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000865}
866
Mon P Wang4dd832d2008-12-09 05:46:39 +0000867SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
868 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
869 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
870
871 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
872 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
873
874 return ShiftAmt;
875}
876
877
Dan Gohmanff727882007-07-13 20:14:11 +0000878/// LegalizeOp - We know that the specified value has a legal type, and
879/// that its operands are legal. Now ensure that the operation itself
880/// is legal, recursively ensuring that the operands' operations remain
881/// legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000882SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000883 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
884 return Op;
885
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000886 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000887 "Caller should expand or promote operands that are not legal!");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000888 SDNode *Node = Op.getNode();
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000889
Chris Lattnerdc750592005-01-07 07:47:09 +0000890 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000891 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000892 if (Node->getNumValues() > 1) {
893 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000894 if (getTypeAction(Node->getValueType(i)) != Legal) {
895 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000896 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000897 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000898 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000899 }
900 }
901
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000902 // Note that LegalizeOp may be reentered even from single-use nodes, which
903 // means that we always must cache transformed nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000904 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000905 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000906
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000907 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
908 SDValue Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000909 bool isCustom = false;
910
Chris Lattnerdc750592005-01-07 07:47:09 +0000911 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000912 case ISD::FrameIndex:
913 case ISD::EntryToken:
914 case ISD::Register:
915 case ISD::BasicBlock:
916 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000917 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000918 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000919 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000920 case ISD::TargetConstantPool:
921 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000922 case ISD::TargetGlobalTLSAddress:
Bill Wendling24c79f22008-09-16 21:48:12 +0000923 case ISD::TargetExternalSymbol:
Chris Lattnereb637512006-01-28 08:31:04 +0000924 case ISD::VALUETYPE:
925 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +0000926 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +0000927 case ISD::CONDCODE:
Duncan Sandsd97eea32008-03-21 09:14:45 +0000928 case ISD::ARG_FLAGS:
Chris Lattnereb637512006-01-28 08:31:04 +0000929 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000930 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000931 "This must be legal!");
932 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000933 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000934 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
935 // If this is a target node, legalize it by legalizing the operands then
936 // passing it through.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000937 SmallVector<SDValue, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000938 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000939 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000940
Chris Lattner97af9d52006-08-08 01:09:31 +0000941 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000942
943 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
944 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +0000945 return Result.getValue(Op.getResNo());
Chris Lattner3eb86932005-05-14 06:34:48 +0000946 }
947 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000948#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000949 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000950#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000951 assert(0 && "Do not know how to legalize this operator!");
952 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000953 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000954 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000955 case ISD::GlobalTLSAddress:
Bill Wendling24c79f22008-09-16 21:48:12 +0000956 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000957 case ISD::ConstantPool:
958 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
960 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000961 case TargetLowering::Custom:
962 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000963 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattnereb637512006-01-28 08:31:04 +0000964 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000965 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000966 break;
967 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000968 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000969 case ISD::FRAMEADDR:
970 case ISD::RETURNADDR:
971 // The only option for these nodes is to custom lower them. If the target
972 // does not custom lower them, then return zero.
973 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000974 if (Tmp1.getNode())
Nate Begemaneda59972007-01-29 22:58:52 +0000975 Result = Tmp1;
976 else
977 Result = DAG.getConstant(0, TLI.getPointerTy());
978 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000979 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands13237ac2008-06-06 12:08:01 +0000980 MVT VT = Node->getValueType(0);
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000981 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
982 default: assert(0 && "This action is not supported yet!");
983 case TargetLowering::Custom:
984 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000985 if (Result.getNode()) break;
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000986 // Fall Thru
987 case TargetLowering::Legal:
988 Result = DAG.getConstant(0, VT);
989 break;
990 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000991 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000992 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000993 case ISD::EXCEPTIONADDR: {
994 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +0000995 MVT VT = Node->getValueType(0);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000996 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
997 default: assert(0 && "This action is not supported yet!");
998 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000999 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +00001000 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +00001001 }
1002 break;
1003 case TargetLowering::Custom:
1004 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001005 if (Result.getNode()) break;
Jim Laskey7f5872c2007-02-22 15:37:19 +00001006 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +00001007 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001008 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands739a0542008-07-02 17:40:58 +00001009 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +00001010 break;
1011 }
1012 }
Chris Lattner1cbe2082007-04-27 17:12:52 +00001013 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001014 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sands57a60f02007-12-31 18:35:50 +00001015
Gabor Greiff304a7a2008-08-28 21:40:38 +00001016 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sands57a60f02007-12-31 18:35:50 +00001017 "Cannot return more than two values!");
1018
1019 // Since we produced two values, make sure to remember that we
1020 // legalized both of them.
1021 Tmp1 = LegalizeOp(Result);
1022 Tmp2 = LegalizeOp(Result.getValue(1));
1023 AddLegalizedOperand(Op.getValue(0), Tmp1);
1024 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001025 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +00001026 case ISD::EHSELECTION: {
1027 Tmp1 = LegalizeOp(Node->getOperand(0));
1028 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00001029 MVT VT = Node->getValueType(0);
Jim Laskey644af6b2007-02-28 20:43:58 +00001030 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1031 default: assert(0 && "This action is not supported yet!");
1032 case TargetLowering::Expand: {
1033 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +00001034 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +00001035 }
1036 break;
1037 case TargetLowering::Custom:
1038 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001039 if (Result.getNode()) break;
Jim Laskey644af6b2007-02-28 20:43:58 +00001040 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +00001041 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001042 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands739a0542008-07-02 17:40:58 +00001043 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +00001044 break;
1045 }
1046 }
Chris Lattner1cbe2082007-04-27 17:12:52 +00001047 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001048 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sands57a60f02007-12-31 18:35:50 +00001049
Gabor Greiff304a7a2008-08-28 21:40:38 +00001050 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sands57a60f02007-12-31 18:35:50 +00001051 "Cannot return more than two values!");
1052
1053 // Since we produced two values, make sure to remember that we
1054 // legalized both of them.
1055 Tmp1 = LegalizeOp(Result);
1056 Tmp2 = LegalizeOp(Result.getValue(1));
1057 AddLegalizedOperand(Op.getValue(0), Tmp1);
1058 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001059 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +00001060 case ISD::EH_RETURN: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001061 MVT VT = Node->getValueType(0);
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001062 // The only "good" option for this node is to custom lower it.
1063 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1064 default: assert(0 && "This action is not supported at all!");
1065 case TargetLowering::Custom:
1066 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001067 if (Result.getNode()) break;
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001068 // Fall Thru
1069 case TargetLowering::Legal:
1070 // Target does not know, how to lower this, lower to noop
1071 Result = LegalizeOp(Node->getOperand(0));
1072 break;
1073 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +00001074 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +00001075 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +00001076 case ISD::AssertSext:
1077 case ISD::AssertZext:
1078 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +00001080 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00001081 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001082 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greifabfdf922008-08-26 22:36:50 +00001083 Result = Node->getOperand(Op.getResNo());
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001084 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +00001085 case ISD::CopyFromReg:
1086 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001087 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001088 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001089 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001090 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001091 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001092 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001093 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001094 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1095 } else {
1096 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1097 }
Chris Lattnere3c67e92005-12-18 15:27:43 +00001098 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1099 }
Chris Lattnereb6614d2005-01-28 06:27:38 +00001100 // Since CopyFromReg produces two values, make sure to remember that we
1101 // legalized both of them.
1102 AddLegalizedOperand(Op.getValue(0), Result);
1103 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001104 return Result.getValue(Op.getResNo());
Nate Begemancda9aa72005-04-01 22:34:39 +00001105 case ISD::UNDEF: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001106 MVT VT = Op.getValueType();
Nate Begemancda9aa72005-04-01 22:34:39 +00001107 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +00001108 default: assert(0 && "This action is not supported yet!");
1109 case TargetLowering::Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00001110 if (VT.isInteger())
Nate Begemancda9aa72005-04-01 22:34:39 +00001111 Result = DAG.getConstant(0, VT);
Duncan Sands13237ac2008-06-06 12:08:01 +00001112 else if (VT.isFloatingPoint())
1113 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf04d37d2007-09-26 17:26:49 +00001114 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00001115 else
1116 assert(0 && "Unknown value type!");
1117 break;
Nate Begeman69d39432005-04-02 00:41:14 +00001118 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +00001119 break;
1120 }
1121 break;
1122 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001123
Chris Lattnere55d1712006-03-28 00:40:33 +00001124 case ISD::INTRINSIC_W_CHAIN:
1125 case ISD::INTRINSIC_WO_CHAIN:
1126 case ISD::INTRINSIC_VOID: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001127 SmallVector<SDValue, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001128 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1129 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001130 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001131
1132 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001133 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001134 TargetLowering::Custom) {
1135 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001136 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001137 }
1138
Gabor Greiff304a7a2008-08-28 21:40:38 +00001139 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001140
1141 // Must have return value and chain result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001142 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001143 "Cannot return more than two values!");
1144
1145 // Since loads produce two values, make sure to remember that we
1146 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001147 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1148 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001149 return Result.getValue(Op.getResNo());
Chris Lattnera4f68052006-03-24 02:26:29 +00001150 }
Chris Lattner435b4022005-11-29 06:21:05 +00001151
Dan Gohman5c73a882008-06-30 20:59:49 +00001152 case ISD::DBG_STOPPOINT:
1153 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner435b4022005-11-29 06:21:05 +00001154 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1155
Dan Gohman5c73a882008-06-30 20:59:49 +00001156 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner435b4022005-11-29 06:21:05 +00001157 case TargetLowering::Promote:
1158 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001159 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001160 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001161 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001162 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001163
Dan Gohman5c73a882008-06-30 20:59:49 +00001164 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskeyc56315c2007-01-26 21:22:28 +00001165 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman5c73a882008-06-30 20:59:49 +00001166 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1167 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001168
Dan Gohman5c73a882008-06-30 20:59:49 +00001169 unsigned Line = DSP->getLine();
1170 unsigned Col = DSP->getColumn();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001171
1172 if (useDEBUG_LOC) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001173 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng34ef1db2008-07-08 20:06:39 +00001174 DAG.getConstant(Col, MVT::i32),
1175 DAG.getConstant(SrcFile, MVT::i32) };
1176 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001177 } else {
Evan Cheng263070e2008-02-01 02:05:57 +00001178 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001179 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001180 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001181 } else {
1182 Result = Tmp1; // chain
1183 }
Chris Lattner435b4022005-11-29 06:21:05 +00001184 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001185 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001186 case TargetLowering::Legal: {
1187 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1188 if (Action == Legal && Tmp1 == Node->getOperand(0))
1189 break;
1190
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001191 SmallVector<SDValue, 8> Ops;
Evan Cheng34ef1db2008-07-08 20:06:39 +00001192 Ops.push_back(Tmp1);
1193 if (Action == Legal) {
1194 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1195 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1196 } else {
1197 // Otherwise promote them.
1198 Ops.push_back(PromoteOp(Node->getOperand(1)));
1199 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner435b4022005-11-29 06:21:05 +00001200 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001201 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1202 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1203 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001204 break;
1205 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001206 }
Chris Lattner435b4022005-11-29 06:21:05 +00001207 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001208
1209 case ISD::DECLARE:
1210 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1211 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1212 default: assert(0 && "This action is not supported yet!");
1213 case TargetLowering::Legal:
1214 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1215 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1216 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1218 break;
Chris Lattner9824ffe2008-02-28 05:53:40 +00001219 case TargetLowering::Expand:
1220 Result = LegalizeOp(Node->getOperand(0));
1221 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001222 }
1223 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001224
1225 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001226 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001227 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001228 default: assert(0 && "This action is not supported yet!");
Evan Cheng34ef1db2008-07-08 20:06:39 +00001229 case TargetLowering::Legal: {
1230 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001231 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng34ef1db2008-07-08 20:06:39 +00001232 if (Action == Legal && Tmp1 == Node->getOperand(0))
1233 break;
1234 if (Action == Legal) {
1235 Tmp2 = Node->getOperand(1);
1236 Tmp3 = Node->getOperand(2);
1237 Tmp4 = Node->getOperand(3);
1238 } else {
1239 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1240 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1241 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1242 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001244 break;
1245 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001246 }
Jim Laskey762e9ec2006-01-05 01:25:28 +00001247 break;
1248
Dan Gohmanfb19f942008-07-01 00:05:16 +00001249 case ISD::DBG_LABEL:
1250 case ISD::EH_LABEL:
1251 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1252 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001253 default: assert(0 && "This action is not supported yet!");
1254 case TargetLowering::Legal:
1255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfb19f942008-07-01 00:05:16 +00001256 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskey7c462762005-12-16 22:45:29 +00001257 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001258 case TargetLowering::Expand:
1259 Result = LegalizeOp(Node->getOperand(0));
1260 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001261 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001262 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001263
Evan Cheng95cf6612008-03-08 00:58:38 +00001264 case ISD::PREFETCH:
1265 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1266 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Legal:
1269 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1270 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1271 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1272 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1274 break;
1275 case TargetLowering::Expand:
1276 // It's a noop.
1277 Result = LegalizeOp(Node->getOperand(0));
1278 break;
1279 }
1280 break;
1281
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001282 case ISD::MEMBARRIER: {
1283 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001284 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1285 default: assert(0 && "This action is not supported yet!");
1286 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001287 SDValue Ops[6];
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001288 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001289 for (int x = 1; x < 6; ++x) {
1290 Ops[x] = Node->getOperand(x);
1291 if (!isTypeLegal(Ops[x].getValueType()))
1292 Ops[x] = PromoteOp(Ops[x]);
1293 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001294 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1295 break;
1296 }
1297 case TargetLowering::Expand:
1298 //There is no libgcc call for this op
1299 Result = Node->getOperand(0); // Noop
1300 break;
1301 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001302 break;
1303 }
1304
Dale Johannesen41be0d42008-08-28 02:44:49 +00001305 case ISD::ATOMIC_CMP_SWAP_8:
1306 case ISD::ATOMIC_CMP_SWAP_16:
1307 case ISD::ATOMIC_CMP_SWAP_32:
1308 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang3e583932008-05-05 19:05:59 +00001309 unsigned int num_operands = 4;
1310 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001311 SDValue Ops[4];
Mon P Wang3e583932008-05-05 19:05:59 +00001312 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharthd032c332008-03-01 21:52:34 +00001313 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang3e583932008-05-05 19:05:59 +00001314 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1315
1316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1317 default: assert(0 && "This action is not supported yet!");
1318 case TargetLowering::Custom:
1319 Result = TLI.LowerOperation(Result, DAG);
1320 break;
1321 case TargetLowering::Legal:
1322 break;
1323 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001324 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1325 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001326 return Result.getValue(Op.getResNo());
Duncan Sands93e180342008-07-04 11:47:58 +00001327 }
Dale Johannesen41be0d42008-08-28 02:44:49 +00001328 case ISD::ATOMIC_LOAD_ADD_8:
1329 case ISD::ATOMIC_LOAD_SUB_8:
1330 case ISD::ATOMIC_LOAD_AND_8:
1331 case ISD::ATOMIC_LOAD_OR_8:
1332 case ISD::ATOMIC_LOAD_XOR_8:
1333 case ISD::ATOMIC_LOAD_NAND_8:
1334 case ISD::ATOMIC_LOAD_MIN_8:
1335 case ISD::ATOMIC_LOAD_MAX_8:
1336 case ISD::ATOMIC_LOAD_UMIN_8:
1337 case ISD::ATOMIC_LOAD_UMAX_8:
1338 case ISD::ATOMIC_SWAP_8:
1339 case ISD::ATOMIC_LOAD_ADD_16:
1340 case ISD::ATOMIC_LOAD_SUB_16:
1341 case ISD::ATOMIC_LOAD_AND_16:
1342 case ISD::ATOMIC_LOAD_OR_16:
1343 case ISD::ATOMIC_LOAD_XOR_16:
1344 case ISD::ATOMIC_LOAD_NAND_16:
1345 case ISD::ATOMIC_LOAD_MIN_16:
1346 case ISD::ATOMIC_LOAD_MAX_16:
1347 case ISD::ATOMIC_LOAD_UMIN_16:
1348 case ISD::ATOMIC_LOAD_UMAX_16:
1349 case ISD::ATOMIC_SWAP_16:
1350 case ISD::ATOMIC_LOAD_ADD_32:
1351 case ISD::ATOMIC_LOAD_SUB_32:
1352 case ISD::ATOMIC_LOAD_AND_32:
1353 case ISD::ATOMIC_LOAD_OR_32:
1354 case ISD::ATOMIC_LOAD_XOR_32:
1355 case ISD::ATOMIC_LOAD_NAND_32:
1356 case ISD::ATOMIC_LOAD_MIN_32:
1357 case ISD::ATOMIC_LOAD_MAX_32:
1358 case ISD::ATOMIC_LOAD_UMIN_32:
1359 case ISD::ATOMIC_LOAD_UMAX_32:
1360 case ISD::ATOMIC_SWAP_32:
1361 case ISD::ATOMIC_LOAD_ADD_64:
1362 case ISD::ATOMIC_LOAD_SUB_64:
1363 case ISD::ATOMIC_LOAD_AND_64:
1364 case ISD::ATOMIC_LOAD_OR_64:
1365 case ISD::ATOMIC_LOAD_XOR_64:
1366 case ISD::ATOMIC_LOAD_NAND_64:
1367 case ISD::ATOMIC_LOAD_MIN_64:
1368 case ISD::ATOMIC_LOAD_MAX_64:
1369 case ISD::ATOMIC_LOAD_UMIN_64:
1370 case ISD::ATOMIC_LOAD_UMAX_64:
1371 case ISD::ATOMIC_SWAP_64: {
Mon P Wang3e583932008-05-05 19:05:59 +00001372 unsigned int num_operands = 3;
1373 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001374 SDValue Ops[3];
Mon P Wang3e583932008-05-05 19:05:59 +00001375 for (unsigned int x = 0; x < num_operands; ++x)
1376 Ops[x] = LegalizeOp(Node->getOperand(x));
1377 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands93e180342008-07-04 11:47:58 +00001378
Andrew Lenharthd032c332008-03-01 21:52:34 +00001379 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth95528942008-02-21 06:45:13 +00001380 default: assert(0 && "This action is not supported yet!");
Andrew Lenharthd032c332008-03-01 21:52:34 +00001381 case TargetLowering::Custom:
1382 Result = TLI.LowerOperation(Result, DAG);
1383 break;
1384 case TargetLowering::Legal:
Andrew Lenharth95528942008-02-21 06:45:13 +00001385 break;
1386 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001387 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1388 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001389 return Result.getValue(Op.getResNo());
Duncan Sands93e180342008-07-04 11:47:58 +00001390 }
Scott Michel9d09c5c2007-08-08 23:23:31 +00001391 case ISD::Constant: {
1392 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1393 unsigned opAction =
1394 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1395
Chris Lattnerdc750592005-01-07 07:47:09 +00001396 // We know we don't need to expand constants here, constants only have one
1397 // value and we check that it is fine above.
1398
Scott Michel9d09c5c2007-08-08 23:23:31 +00001399 if (opAction == TargetLowering::Custom) {
1400 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001401 if (Tmp1.getNode())
Scott Michel9d09c5c2007-08-08 23:23:31 +00001402 Result = Tmp1;
1403 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001404 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001405 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001406 case ISD::ConstantFP: {
1407 // Spill FP immediates to the constant pool if the target cannot directly
1408 // codegen them. Targets often have some immediate values that can be
1409 // efficiently generated into an FP register without a load. We explicitly
1410 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001411 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1412
Chris Lattner758b0ac2006-01-29 06:26:56 +00001413 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1414 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001415 case TargetLowering::Legal:
1416 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001417 case TargetLowering::Custom:
1418 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001419 if (Tmp3.getNode()) {
Chris Lattner758b0ac2006-01-29 06:26:56 +00001420 Result = Tmp3;
1421 break;
1422 }
1423 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001424 case TargetLowering::Expand: {
1425 // Check to see if this FP immediate is already legal.
1426 bool isLegal = false;
1427 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1428 E = TLI.legal_fpimm_end(); I != E; ++I) {
1429 if (CFP->isExactlyValue(*I)) {
1430 isLegal = true;
1431 break;
1432 }
1433 }
1434 // If this is a legal constant, turn it into a TargetConstantFP node.
1435 if (isLegal)
1436 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001437 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001438 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001439 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001440 break;
1441 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001442 case ISD::TokenFactor:
1443 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001444 Tmp1 = LegalizeOp(Node->getOperand(0));
1445 Tmp2 = LegalizeOp(Node->getOperand(1));
1446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1447 } else if (Node->getNumOperands() == 3) {
1448 Tmp1 = LegalizeOp(Node->getOperand(0));
1449 Tmp2 = LegalizeOp(Node->getOperand(1));
1450 Tmp3 = LegalizeOp(Node->getOperand(2));
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001452 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001453 SmallVector<SDValue, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001454 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001455 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1456 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001457 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001458 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001459 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001460
1461 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001462 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001463 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001464 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001465 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001466 // A call within a calling sequence must be legalized to something
1467 // other than the normal CALLSEQ_END. Violating this gets Legalize
1468 // into an infinite loop.
1469 assert ((!IsLegalizingCall ||
1470 Node->getOpcode() != ISD::CALL ||
Gabor Greiff304a7a2008-08-28 21:40:38 +00001471 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001472 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001473
1474 // The number of incoming and outgoing values should match; unless the final
1475 // outgoing value is a flag.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001476 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1477 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1478 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendlingf359fed2007-11-13 00:44:25 +00001479 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001480 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001481
Chris Lattneraaa23d92006-05-16 22:53:20 +00001482 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001483 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001484 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1485 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendlingf359fed2007-11-13 00:44:25 +00001486 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001487 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +00001488 if (Op.getResNo() == i)
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001489 Tmp2 = Tmp1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001490 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001491 }
1492 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001493 case ISD::EXTRACT_SUBREG: {
1494 Tmp1 = LegalizeOp(Node->getOperand(0));
1495 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1496 assert(idx && "Operand must be a constant");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001497 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1499 }
1500 break;
1501 case ISD::INSERT_SUBREG: {
1502 Tmp1 = LegalizeOp(Node->getOperand(0));
1503 Tmp2 = LegalizeOp(Node->getOperand(1));
1504 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1505 assert(idx && "Operand must be a constant");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001506 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1508 }
1509 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001510 case ISD::BUILD_VECTOR:
1511 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001512 default: assert(0 && "This action is not supported yet!");
1513 case TargetLowering::Custom:
1514 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001515 if (Tmp3.getNode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00001516 Result = Tmp3;
1517 break;
1518 }
1519 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001520 case TargetLowering::Expand:
Gabor Greiff304a7a2008-08-28 21:40:38 +00001521 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001522 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001523 }
Chris Lattner29b23012006-03-19 01:17:20 +00001524 break;
1525 case ISD::INSERT_VECTOR_ELT:
1526 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001527 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001528
1529 // The type of the value to insert may not be legal, even though the vector
1530 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1531 // here.
1532 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1533 default: assert(0 && "Cannot expand insert element operand");
1534 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1535 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang58c37942008-10-30 08:01:45 +00001536 case Expand:
1537 // FIXME: An alternative would be to check to see if the target is not
1538 // going to custom lower this operation, we could bitcast to half elt
1539 // width and perform two inserts at that width, if that is legal.
1540 Tmp2 = Node->getOperand(1);
1541 break;
Nate Begeman735ab3c2008-02-13 06:43:04 +00001542 }
Chris Lattner29b23012006-03-19 01:17:20 +00001543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1544
1545 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1546 Node->getValueType(0))) {
1547 default: assert(0 && "This action is not supported yet!");
1548 case TargetLowering::Legal:
1549 break;
1550 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001551 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001552 if (Tmp4.getNode()) {
Nate Begeman5743da52008-01-05 20:47:37 +00001553 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001554 break;
1555 }
1556 // FALLTHROUGH
Mon P Wang58c37942008-10-30 08:01:45 +00001557 case TargetLowering::Promote:
1558 // Fall thru for vector case
Chris Lattner29b23012006-03-19 01:17:20 +00001559 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001560 // If the insert index is a constant, codegen this as a scalar_to_vector,
1561 // then a shuffle that inserts it into the right position in the vector.
1562 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001563 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1564 // match the element type of the vector being created.
1565 if (Tmp2.getValueType() ==
Duncan Sands13237ac2008-06-06 12:08:01 +00001566 Op.getValueType().getVectorElementType()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001567 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001568 Tmp1.getValueType(), Tmp2);
1569
Duncan Sands13237ac2008-06-06 12:08:01 +00001570 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1571 MVT ShufMaskVT =
1572 MVT::getIntVectorWithNumElements(NumElts);
1573 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001574
1575 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1576 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1577 // elt 0 of the RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001578 SmallVector<SDValue, 8> ShufOps;
Nate Begeman735ab3c2008-02-13 06:43:04 +00001579 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001580 if (i != InsertPos->getZExtValue())
Nate Begeman735ab3c2008-02-13 06:43:04 +00001581 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1582 else
1583 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1584 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001585 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001586 &ShufOps[0], ShufOps.size());
1587
1588 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1589 Tmp1, ScVec, ShufMask);
1590 Result = LegalizeOp(Result);
1591 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001592 }
Chris Lattner326870b2006-04-17 19:21:01 +00001593 }
Nate Begeman6f94f612008-04-25 18:07:40 +00001594 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner29b23012006-03-19 01:17:20 +00001595 break;
1596 }
1597 }
1598 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001599 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001600 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1601 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1602 break;
1603 }
1604
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001605 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1606 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1607 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1608 Node->getValueType(0))) {
1609 default: assert(0 && "This action is not supported yet!");
1610 case TargetLowering::Legal:
1611 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001612 case TargetLowering::Custom:
1613 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001614 if (Tmp3.getNode()) {
Chris Lattner79fb91c2006-03-19 06:47:21 +00001615 Result = Tmp3;
1616 break;
1617 }
1618 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001619 case TargetLowering::Expand:
1620 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001621 break;
1622 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001623 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001624 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1626 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1628
1629 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001630 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1631 default: assert(0 && "Unknown operation action!");
1632 case TargetLowering::Legal:
1633 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1634 "vector shuffle should not be created if not legal!");
1635 break;
1636 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001637 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001638 if (Tmp3.getNode()) {
Evan Cheng9fa89592006-04-05 06:07:11 +00001639 Result = Tmp3;
1640 break;
1641 }
1642 // FALLTHROUGH
1643 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001644 MVT VT = Node->getValueType(0);
1645 MVT EltVT = VT.getVectorElementType();
1646 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001647 SDValue Mask = Node->getOperand(2);
Evan Cheng9fa89592006-04-05 06:07:11 +00001648 unsigned NumElems = Mask.getNumOperands();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001649 SmallVector<SDValue,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001650 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001651 SDValue Arg = Mask.getOperand(i);
Evan Cheng9fa89592006-04-05 06:07:11 +00001652 if (Arg.getOpcode() == ISD::UNDEF) {
1653 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1654 } else {
1655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00001656 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng9fa89592006-04-05 06:07:11 +00001657 if (Idx < NumElems)
1658 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1659 DAG.getConstant(Idx, PtrVT)));
1660 else
1661 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1662 DAG.getConstant(Idx - NumElems, PtrVT)));
1663 }
1664 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001665 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001666 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001667 }
Chris Lattner6be79822006-04-04 17:23:26 +00001668 case TargetLowering::Promote: {
1669 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00001670 MVT OVT = Node->getValueType(0);
1671 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner6be79822006-04-04 17:23:26 +00001672
1673 // Cast the two input vectors.
1674 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1675 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1676
1677 // Convert the shuffle mask to the right # elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001678 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001679 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner6be79822006-04-04 17:23:26 +00001680 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1681 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1682 break;
1683 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001684 }
1685 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001686
1687 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001688 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001689 Tmp2 = LegalizeOp(Node->getOperand(1));
1690 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001691 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001692 break;
1693
Dan Gohmana8665142007-06-25 16:23:39 +00001694 case ISD::EXTRACT_SUBVECTOR:
1695 Tmp1 = Node->getOperand(0);
1696 Tmp2 = LegalizeOp(Node->getOperand(1));
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1698 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001699 break;
1700
Mon P Wang58c37942008-10-30 08:01:45 +00001701 case ISD::CONCAT_VECTORS: {
1702 // Use extract/insert/build vector for now. We might try to be
1703 // more clever later.
1704 MVT PtrVT = TLI.getPointerTy();
1705 SmallVector<SDValue, 8> Ops;
1706 unsigned NumOperands = Node->getNumOperands();
1707 for (unsigned i=0; i < NumOperands; ++i) {
1708 SDValue SubOp = Node->getOperand(i);
1709 MVT VVT = SubOp.getNode()->getValueType(0);
1710 MVT EltVT = VVT.getVectorElementType();
1711 unsigned NumSubElem = VVT.getVectorNumElements();
1712 for (unsigned j=0; j < NumSubElem; ++j) {
1713 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1714 DAG.getConstant(j, PtrVT)));
1715 }
1716 }
1717 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1718 &Ops[0], Ops.size()));
1719 }
1720
Chris Lattner462505f2006-02-13 09:18:02 +00001721 case ISD::CALLSEQ_START: {
1722 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1723
1724 // Recursively Legalize all of the inputs of the call end that do not lead
1725 // to this call start. This ensures that any libcalls that need be inserted
1726 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001727 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001728 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greiff304a7a2008-08-28 21:40:38 +00001729 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattner4488f0c2006-07-26 23:55:56 +00001730 NodesLeadingTo);
1731 }
Chris Lattner462505f2006-02-13 09:18:02 +00001732
1733 // Now that we legalized all of the inputs (which may have inserted
1734 // libcalls) create the new CALLSEQ_START node.
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1736
1737 // Merge in the last call, to ensure that this call start after the last
1738 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001739 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
1742 }
Chris Lattner462505f2006-02-13 09:18:02 +00001743
1744 // Do not try to legalize the target-specific arguments (#1+).
1745 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001746 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001747 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001748 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001749 }
1750
1751 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001752 AddLegalizedOperand(Op.getValue(0), Result);
1753 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1754 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1755
Chris Lattner462505f2006-02-13 09:18:02 +00001756 // Now that the callseq_start and all of the non-call nodes above this call
1757 // sequence have been legalized, legalize the call itself. During this
1758 // process, no libcalls can/will be inserted, guaranteeing that no calls
1759 // can overlap.
1760 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner462505f2006-02-13 09:18:02 +00001761 // Note that we are selecting this call!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001762 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner462505f2006-02-13 09:18:02 +00001763 IsLegalizingCall = true;
1764
1765 // Legalize the call, starting from the CALLSEQ_END.
1766 LegalizeOp(LastCALLSEQ_END);
1767 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1768 return Result;
1769 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001770 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001771 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1772 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001773 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001774 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1775 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001776 assert(I != LegalizedNodes.end() &&
1777 "Legalizing the call start should have legalized this node!");
1778 return I->second;
1779 }
1780
1781 // Otherwise, the call start has been legalized and everything is going
1782 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001784 // Do not try to legalize the target-specific arguments (#1+), except for
1785 // an optional flag input.
1786 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1787 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001788 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001789 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001790 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001791 }
1792 } else {
1793 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1794 if (Tmp1 != Node->getOperand(0) ||
1795 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001796 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001797 Ops[0] = Tmp1;
1798 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001799 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001800 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001801 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001802 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001803 // This finishes up call legalization.
1804 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001805
1806 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001807 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner8e2ee732006-02-14 00:55:02 +00001808 if (Node->getNumValues() == 2)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001809 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001810 return Result.getValue(Op.getResNo());
Evan Cheng7f4ec822006-01-11 22:14:47 +00001811 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001812 MVT VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1814 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1815 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001817
Chris Lattnerd02b0542006-01-28 10:58:55 +00001818 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001819 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001820 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001821 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001822 case TargetLowering::Expand: {
1823 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1824 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1825 " not tell us which reg is the stack pointer!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001826 SDValue Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001827
1828 // Chain the dynamic stack allocation so that it doesn't modify the stack
1829 // pointer when other instructions are using the stack.
Chris Lattner27539552008-10-11 22:08:30 +00001830 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendlingf359fed2007-11-13 00:44:25 +00001831
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001832 SDValue Size = Tmp2.getOperand(1);
1833 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng631ccc62007-08-16 23:50:06 +00001834 Chain = SP.getValue(1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001835 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng631ccc62007-08-16 23:50:06 +00001836 unsigned StackAlign =
1837 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1838 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001839 SP = DAG.getNode(ISD::AND, VT, SP,
1840 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001841 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001842 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1843
Chris Lattner27539552008-10-11 22:08:30 +00001844 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1845 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendlingf359fed2007-11-13 00:44:25 +00001846
Chris Lattnerd02b0542006-01-28 10:58:55 +00001847 Tmp1 = LegalizeOp(Tmp1);
1848 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001849 break;
1850 }
1851 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001852 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001853 if (Tmp3.getNode()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001854 Tmp1 = LegalizeOp(Tmp3);
1855 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001856 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001857 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001858 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001859 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001860 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001861 // Since this op produce two values, make sure to remember that we
1862 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001863 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1864 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00001865 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001866 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001867 case ISD::INLINEASM: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001868 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001869 bool Changed = false;
1870 // Legalize all of the operands of the inline asm, in case they are nodes
1871 // that need to be expanded or something. Note we skip the asm string and
1872 // all of the TargetConstant flags.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001873 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001874 Changed = Op != Ops[0];
1875 Ops[0] = Op;
1876
1877 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1878 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001879 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001880 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001881 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001882 if (Op != Ops[i]) {
1883 Changed = true;
1884 Ops[i] = Op;
1885 }
1886 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001887 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001888
1889 if (HasInFlag) {
1890 Op = LegalizeOp(Ops.back());
1891 Changed |= Op != Ops.back();
1892 Ops.back() = Op;
1893 }
1894
1895 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001896 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001897
1898 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001899 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1900 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greifabfdf922008-08-26 22:36:50 +00001901 return Result.getValue(Op.getResNo());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001902 }
Chris Lattner68a12142005-01-07 22:12:08 +00001903 case ISD::BR:
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001905 // Ensure that libcalls are emitted before a branch.
1906 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1907 Tmp1 = LegalizeOp(Tmp1);
1908 LastCALLSEQ_END = DAG.getEntryNode();
1909
Chris Lattnerd02b0542006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001911 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001912 case ISD::BRIND:
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1914 // Ensure that libcalls are emitted before a branch.
1915 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1916 Tmp1 = LegalizeOp(Tmp1);
1917 LastCALLSEQ_END = DAG.getEntryNode();
1918
1919 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1920 default: assert(0 && "Indirect target must be legal type (pointer)!");
1921 case Legal:
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1923 break;
1924 }
1925 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1926 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001927 case ISD::BR_JT:
1928 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1929 // Ensure that libcalls are emitted before a branch.
1930 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1931 Tmp1 = LegalizeOp(Tmp1);
1932 LastCALLSEQ_END = DAG.getEntryNode();
1933
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1936
1937 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1938 default: assert(0 && "This action is not supported yet!");
1939 case TargetLowering::Legal: break;
1940 case TargetLowering::Custom:
1941 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001942 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng84a28d42006-10-30 08:00:44 +00001943 break;
1944 case TargetLowering::Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001945 SDValue Chain = Result.getOperand(0);
1946 SDValue Table = Result.getOperand(1);
1947 SDValue Index = Result.getOperand(2);
Evan Cheng84a28d42006-10-30 08:00:44 +00001948
Duncan Sands13237ac2008-06-06 12:08:01 +00001949 MVT PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001950 MachineFunction &MF = DAG.getMachineFunction();
1951 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001952 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001953 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001954
Duncan Sandse4bcb8e2008-12-12 08:13:38 +00001955 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
1956 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
1957 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng797d56f2007-11-09 01:32:10 +00001958 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001959 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001960 // For PIC, the sequence is:
1961 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001962 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng797d56f2007-11-09 01:32:10 +00001963 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1964 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001965 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001966 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001967 }
1968 }
1969 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001970 case ISD::BRCOND:
1971 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001972 // Ensure that libcalls are emitted before a return.
1973 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1974 Tmp1 = LegalizeOp(Tmp1);
1975 LastCALLSEQ_END = DAG.getEntryNode();
1976
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001977 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1978 case Expand: assert(0 && "It's impossible to expand bools");
1979 case Legal:
1980 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1981 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00001982 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001983 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001984
1985 // The top bits of the promoted condition are not necessarily zero, ensure
1986 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001987 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001988 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00001989 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00001990 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001991 break;
1992 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00001993 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001994
1995 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001997
1998 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1999 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002000 case TargetLowering::Legal: break;
2001 case TargetLowering::Custom:
2002 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002003 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002004 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002005 case TargetLowering::Expand:
2006 // Expand brcond's setcc into its constituent parts and create a BR_CC
2007 // Node.
2008 if (Tmp2.getOpcode() == ISD::SETCC) {
2009 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2010 Tmp2.getOperand(0), Tmp2.getOperand(1),
2011 Node->getOperand(2));
2012 } else {
2013 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2014 DAG.getCondCode(ISD::SETNE), Tmp2,
2015 DAG.getConstant(0, Tmp2.getValueType()),
2016 Node->getOperand(2));
2017 }
2018 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002019 }
2020 break;
2021 case ISD::BR_CC:
2022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002023 // Ensure that libcalls are emitted before a branch.
2024 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2025 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00002026 Tmp2 = Node->getOperand(2); // LHS
2027 Tmp3 = Node->getOperand(3); // RHS
2028 Tmp4 = Node->getOperand(1); // CC
2029
Dale Johannesen160be0f2008-11-07 22:54:33 +00002030 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00002031 LastCALLSEQ_END = DAG.getEntryNode();
2032
Evan Cheng3b0f5e42008-10-15 02:05:31 +00002033 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman7e7f4392006-02-01 07:19:44 +00002034 // the LHS is a legal SETCC itself. In this case, we need to compare
2035 // the result against zero to select between true and false values.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002036 if (Tmp3.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00002037 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2038 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002039 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00002040
2041 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2042 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002043
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002044 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2045 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002046 case TargetLowering::Legal: break;
2047 case TargetLowering::Custom:
2048 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002049 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00002050 break;
Nate Begeman371e4952005-08-16 19:49:35 +00002051 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00002052 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002053 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002054 LoadSDNode *LD = cast<LoadSDNode>(Node);
2055 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2056 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002057
Evan Chenge71fe34d2006-10-09 20:57:25 +00002058 ISD::LoadExtType ExtType = LD->getExtensionType();
2059 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002060 MVT VT = Node->getValueType(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2062 Tmp3 = Result.getValue(0);
2063 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002064
Evan Chenge71fe34d2006-10-09 20:57:25 +00002065 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2066 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002067 case TargetLowering::Legal:
2068 // If this is an unaligned load and the target doesn't support it,
2069 // expand it.
2070 if (!TLI.allowsUnalignedMemoryAccesses()) {
2071 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002072 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002073 if (LD->getAlignment() < ABIAlignment){
Gabor Greiff304a7a2008-08-28 21:40:38 +00002074 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002075 TLI);
2076 Tmp3 = Result.getOperand(0);
2077 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00002078 Tmp3 = LegalizeOp(Tmp3);
2079 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002080 }
2081 }
2082 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002083 case TargetLowering::Custom:
2084 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002085 if (Tmp1.getNode()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002086 Tmp3 = LegalizeOp(Tmp1);
2087 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002088 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002089 break;
2090 case TargetLowering::Promote: {
2091 // Only promote a load of vector type to another.
Duncan Sands13237ac2008-06-06 12:08:01 +00002092 assert(VT.isVector() && "Cannot promote this load!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00002093 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002094 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002095
2096 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002097 LD->getSrcValueOffset(),
2098 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00002099 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2100 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002101 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00002102 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002103 }
2104 // Since loads produce two values, make sure to remember that we
2105 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002106 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2107 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greifabfdf922008-08-26 22:36:50 +00002108 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002109 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00002110 MVT SrcVT = LD->getMemoryVT();
2111 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands95d46ef2008-01-23 20:39:46 +00002112 int SVOffset = LD->getSrcValueOffset();
2113 unsigned Alignment = LD->getAlignment();
2114 bool isVolatile = LD->isVolatile();
2115
Duncan Sands13237ac2008-06-06 12:08:01 +00002116 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00002117 // Some targets pretend to have an i1 loading operation, and actually
2118 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2119 // bits are guaranteed to be zero; it helps the optimizers understand
2120 // that these bits are zero. It is also useful for EXTLOAD, since it
2121 // tells the optimizers that those bits are undefined. It would be
2122 // nice to have an effective generic way of getting these benefits...
2123 // Until such a way is found, don't insist on promoting i1 here.
2124 (SrcVT != MVT::i1 ||
Evan Cheng07d53b12008-10-14 21:26:46 +00002125 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002126 // Promote to a byte-sized load if not loading an integral number of
2127 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands13237ac2008-06-06 12:08:01 +00002128 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2129 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002130 SDValue Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002131
2132 // The extra bits are guaranteed to be zero, since we stored them that
2133 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2134
2135 ISD::LoadExtType NewExtType =
2136 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2137
2138 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2139 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2140 NVT, isVolatile, Alignment);
2141
2142 Ch = Result.getValue(1); // The chain.
2143
2144 if (ExtType == ISD::SEXTLOAD)
2145 // Having the top bits zero doesn't help when sign extending.
2146 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2147 Result, DAG.getValueType(SrcVT));
2148 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2149 // All the top bits are guaranteed to be zero - inform the optimizers.
2150 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2151 DAG.getValueType(SrcVT));
2152
2153 Tmp1 = LegalizeOp(Result);
2154 Tmp2 = LegalizeOp(Ch);
2155 } else if (SrcWidth & (SrcWidth - 1)) {
2156 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands13237ac2008-06-06 12:08:01 +00002157 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00002158 "Unsupported extload!");
2159 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2160 assert(RoundWidth < SrcWidth);
2161 unsigned ExtraWidth = SrcWidth - RoundWidth;
2162 assert(ExtraWidth < RoundWidth);
2163 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2164 "Load size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002165 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2166 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002167 SDValue Lo, Hi, Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002168 unsigned IncrementSize;
2169
2170 if (TLI.isLittleEndian()) {
2171 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2172 // Load the bottom RoundWidth bits.
2173 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2174 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2175 Alignment);
2176
2177 // Load the remaining ExtraWidth bits.
2178 IncrementSize = RoundWidth / 8;
2179 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2180 DAG.getIntPtrConstant(IncrementSize));
2181 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2182 LD->getSrcValue(), SVOffset + IncrementSize,
2183 ExtraVT, isVolatile,
2184 MinAlign(Alignment, IncrementSize));
2185
2186 // Build a factor node to remember that this load is independent of the
2187 // other one.
2188 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2189 Hi.getValue(1));
2190
2191 // Move the top bits to the right place.
2192 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2193 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2194
2195 // Join the hi and lo parts.
2196 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002197 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002198 // Big endian - avoid unaligned loads.
2199 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2200 // Load the top RoundWidth bits.
2201 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2202 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2203 Alignment);
2204
2205 // Load the remaining ExtraWidth bits.
2206 IncrementSize = RoundWidth / 8;
2207 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2208 DAG.getIntPtrConstant(IncrementSize));
2209 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2210 LD->getSrcValue(), SVOffset + IncrementSize,
2211 ExtraVT, isVolatile,
2212 MinAlign(Alignment, IncrementSize));
2213
2214 // Build a factor node to remember that this load is independent of the
2215 // other one.
2216 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2217 Hi.getValue(1));
2218
2219 // Move the top bits to the right place.
2220 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2221 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2222
2223 // Join the hi and lo parts.
2224 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2225 }
2226
2227 Tmp1 = LegalizeOp(Result);
2228 Tmp2 = LegalizeOp(Ch);
2229 } else {
Evan Cheng07d53b12008-10-14 21:26:46 +00002230 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002231 default: assert(0 && "This action is not supported yet!");
2232 case TargetLowering::Custom:
2233 isCustom = true;
2234 // FALLTHROUGH
2235 case TargetLowering::Legal:
2236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2237 Tmp1 = Result.getValue(0);
2238 Tmp2 = Result.getValue(1);
2239
2240 if (isCustom) {
2241 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002242 if (Tmp3.getNode()) {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002243 Tmp1 = LegalizeOp(Tmp3);
2244 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2245 }
2246 } else {
2247 // If this is an unaligned load and the target doesn't support it,
2248 // expand it.
2249 if (!TLI.allowsUnalignedMemoryAccesses()) {
2250 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002251 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands95d46ef2008-01-23 20:39:46 +00002252 if (LD->getAlignment() < ABIAlignment){
Gabor Greiff304a7a2008-08-28 21:40:38 +00002253 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands95d46ef2008-01-23 20:39:46 +00002254 TLI);
2255 Tmp1 = Result.getOperand(0);
2256 Tmp2 = Result.getOperand(1);
2257 Tmp1 = LegalizeOp(Tmp1);
2258 Tmp2 = LegalizeOp(Tmp2);
2259 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002260 }
2261 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002262 break;
2263 case TargetLowering::Expand:
2264 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2265 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002266 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands95d46ef2008-01-23 20:39:46 +00002267 LD->getSrcValueOffset(),
2268 LD->isVolatile(), LD->getAlignment());
2269 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2270 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2271 Tmp2 = LegalizeOp(Load.getValue(1));
2272 break;
2273 }
2274 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2275 // Turn the unsupported load into an EXTLOAD followed by an explicit
2276 // zero/sign extend inreg.
2277 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2278 Tmp1, Tmp2, LD->getSrcValue(),
2279 LD->getSrcValueOffset(), SrcVT,
2280 LD->isVolatile(), LD->getAlignment());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002281 SDValue ValRes;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002282 if (ExtType == ISD::SEXTLOAD)
2283 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2284 Result, DAG.getValueType(SrcVT));
2285 else
2286 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2287 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2288 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002289 break;
2290 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002291 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002292
Evan Chenge71fe34d2006-10-09 20:57:25 +00002293 // Since loads produce two values, make sure to remember that we legalized
2294 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002295 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2296 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00002297 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002298 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002299 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002300 case ISD::EXTRACT_ELEMENT: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002301 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5172ce62005-10-19 00:06:56 +00002302 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002303 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002304 case Legal:
Dan Gohmaneffb8942008-09-12 16:56:44 +00002305 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5172ce62005-10-19 00:06:56 +00002306 // 1 -> Hi
2307 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands13237ac2008-06-06 12:08:01 +00002308 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5172ce62005-10-19 00:06:56 +00002309 TLI.getShiftAmountTy()));
2310 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2311 } else {
2312 // 0 -> Lo
2313 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2314 Node->getOperand(0));
2315 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002316 break;
2317 case Expand:
2318 // Get both the low and high parts.
2319 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002320 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5172ce62005-10-19 00:06:56 +00002321 Result = Tmp2; // 1 -> Hi
2322 else
2323 Result = Tmp1; // 0 -> Lo
2324 break;
2325 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002326 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002327 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002328
2329 case ISD::CopyToReg:
2330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002331
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002332 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002333 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002334 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002335 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002336 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002338 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002339 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002340 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002341 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002342 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2343 Tmp3);
2344 } else {
2345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002346 }
2347
2348 // Since this produces two values, make sure to remember that we legalized
2349 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002350 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2351 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002352 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002353 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002354 break;
2355
2356 case ISD::RET:
2357 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002358
2359 // Ensure that libcalls are emitted before a return.
2360 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2361 Tmp1 = LegalizeOp(Tmp1);
2362 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002363
Chris Lattnerdc750592005-01-07 07:47:09 +00002364 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002365 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002366 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002367 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002368 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002369 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002370 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002371 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002372 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00002373 if (!Tmp2.getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002374 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002375 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002376
2377 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002378 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002379 std::swap(Lo, Hi);
2380
Gabor Greiff304a7a2008-08-28 21:40:38 +00002381 if (Hi.getNode())
Evan Cheng3432ab92006-12-11 19:27:14 +00002382 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2383 else
2384 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002385 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002386 } else {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002387 SDNode *InVal = Tmp2.getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00002388 int InIx = Tmp2.getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00002389 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2390 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002391
Dan Gohmana8665142007-06-25 16:23:39 +00002392 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002393 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002394 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002395 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002396 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002397 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002398 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002399 } else if (NumElems == 1) {
2400 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002401 Tmp2 = ScalarizeVectorOp(Tmp2);
2402 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002404
2405 // FIXME: Returns of gcc generic vectors smaller than a legal type
2406 // should be returned in integer registers!
2407
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002408 // The scalarized value type may not be legal, e.g. it might require
2409 // promotion or expansion. Relegalize the return.
2410 Result = LegalizeOp(Result);
2411 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002412 // FIXME: Returns of gcc generic vectors larger than a legal vector
2413 // type should be returned by reference!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002414 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002415 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002416 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002417 Result = LegalizeOp(Result);
2418 }
2419 }
Misha Brukman835702a2005-04-21 22:36:52 +00002420 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002421 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002422 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002423 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002424 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002425 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002426 }
2427 break;
2428 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002429 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002430 break;
2431 default: { // ret <values>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432 SmallVector<SDValue, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002433 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002434 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002435 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2436 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002437 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002438 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002439 break;
2440 case Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002441 SDValue Lo, Hi;
Duncan Sands13237ac2008-06-06 12:08:01 +00002442 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002443 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002444 ExpandOp(Node->getOperand(i), Lo, Hi);
2445 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002446 NewValues.push_back(Node->getOperand(i+1));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002447 if (Hi.getNode()) {
Evan Cheng3432ab92006-12-11 19:27:14 +00002448 NewValues.push_back(Hi);
2449 NewValues.push_back(Node->getOperand(i+1));
2450 }
Misha Brukman835702a2005-04-21 22:36:52 +00002451 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002452 }
2453 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002454 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002455 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002456
2457 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002458 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002459 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002460 Result = DAG.getNode(ISD::RET, MVT::Other,
2461 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002462 break;
2463 }
2464 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002465
Chris Lattner4d1ea712006-01-29 21:02:23 +00002466 if (Result.getOpcode() == ISD::RET) {
2467 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2468 default: assert(0 && "This action is not supported yet!");
2469 case TargetLowering::Legal: break;
2470 case TargetLowering::Custom:
2471 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002472 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner4d1ea712006-01-29 21:02:23 +00002473 break;
2474 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002475 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002476 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002477 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002478 StoreSDNode *ST = cast<StoreSDNode>(Node);
2479 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2480 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002481 int SVOffset = ST->getSrcValueOffset();
2482 unsigned Alignment = ST->getAlignment();
2483 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002484
Evan Chengab51cf22006-10-13 21:14:26 +00002485 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002486 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2487 // FIXME: We shouldn't do this for TargetConstantFP's.
2488 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2489 // to phase ordering between legalized code and the dag combiner. This
2490 // probably means that we need to integrate dag combiner and legalizer
2491 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002492 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002493 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002494 if (CFP->getValueType(0) == MVT::f32 &&
2495 getTypeAction(MVT::i32) == Legal) {
Dan Gohman10f7d852008-03-11 00:11:06 +00002496 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen54306fe2008-10-09 18:53:47 +00002497 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002498 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002499 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2500 SVOffset, isVolatile, Alignment);
2501 break;
2502 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002503 // If this target supports 64-bit registers, do a single 64-bit store.
2504 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00002505 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman10f7d852008-03-11 00:11:06 +00002506 zextOrTrunc(64), MVT::i64);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002507 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, isVolatile, Alignment);
2509 break;
Duncan Sands8651e9c2008-06-13 19:07:40 +00002510 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002511 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2512 // stores. If the target supports neither 32- nor 64-bits, this
2513 // xform is certainly not worth it.
Dale Johannesen54306fe2008-10-09 18:53:47 +00002514 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2516 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002517 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002518
2519 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2520 SVOffset, isVolatile, Alignment);
2521 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002522 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002523 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002524 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002525
2526 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2527 break;
2528 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002529 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002530 }
2531
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002532 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002533 case Legal: {
2534 Tmp3 = LegalizeOp(ST->getValue());
2535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2536 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002537
Duncan Sands13237ac2008-06-06 12:08:01 +00002538 MVT VT = Tmp3.getValueType();
Evan Chengab51cf22006-10-13 21:14:26 +00002539 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2540 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002541 case TargetLowering::Legal:
2542 // If this is an unaligned store and the target doesn't support it,
2543 // expand it.
2544 if (!TLI.allowsUnalignedMemoryAccesses()) {
2545 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002546 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002547 if (ST->getAlignment() < ABIAlignment)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002548 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002549 TLI);
2550 }
2551 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002552 case TargetLowering::Custom:
2553 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002554 if (Tmp1.getNode()) Result = Tmp1;
Evan Chengab51cf22006-10-13 21:14:26 +00002555 break;
2556 case TargetLowering::Promote:
Duncan Sands13237ac2008-06-06 12:08:01 +00002557 assert(VT.isVector() && "Unknown legal promote case!");
Evan Chengab51cf22006-10-13 21:14:26 +00002558 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2559 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2560 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002561 ST->getSrcValue(), SVOffset, isVolatile,
2562 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002563 break;
2564 }
2565 break;
2566 }
2567 case Promote:
Mon P Wang58c37942008-10-30 08:01:45 +00002568 if (!ST->getMemoryVT().isVector()) {
2569 // Truncate the value and store the result.
2570 Tmp3 = PromoteOp(ST->getValue());
2571 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2572 SVOffset, ST->getMemoryVT(),
2573 isVolatile, Alignment);
2574 break;
2575 }
2576 // Fall thru to expand for vector
2577 case Expand: {
Evan Chengab51cf22006-10-13 21:14:26 +00002578 unsigned IncrementSize = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002579 SDValue Lo, Hi;
Evan Chengab51cf22006-10-13 21:14:26 +00002580
2581 // If this is a vector type, then we have to calculate the increment as
2582 // the product of the element size in bytes, and the number of elements
2583 // in the high half of the vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00002584 if (ST->getValue().getValueType().isVector()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002585 SDNode *InVal = ST->getValue().getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00002586 int InIx = ST->getValue().getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00002587 MVT InVT = InVal->getValueType(InIx);
2588 unsigned NumElems = InVT.getVectorNumElements();
2589 MVT EVT = InVT.getVectorElementType();
Evan Chengab51cf22006-10-13 21:14:26 +00002590
Dan Gohmana8665142007-06-25 16:23:39 +00002591 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002592 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002593 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002594 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002595 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002596 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002597 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002598 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002599 Result = LegalizeOp(Result);
2600 break;
2601 } else if (NumElems == 1) {
2602 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002603 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002604 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002605 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002606 // The scalarized value type may not be legal, e.g. it might require
2607 // promotion or expansion. Relegalize the scalar store.
2608 Result = LegalizeOp(Result);
2609 break;
2610 } else {
Mon P Wang58c37942008-10-30 08:01:45 +00002611 // Check if we have widen this node with another value
2612 std::map<SDValue, SDValue>::iterator I =
2613 WidenNodes.find(ST->getValue());
2614 if (I != WidenNodes.end()) {
2615 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2616 break;
2617 }
2618 else {
2619 SplitVectorOp(ST->getValue(), Lo, Hi);
2620 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2621 EVT.getSizeInBits()/8;
2622 }
Evan Chengab51cf22006-10-13 21:14:26 +00002623 }
2624 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002625 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002626 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002627
Richard Pennington4b35e642008-09-25 16:15:10 +00002628 if (Hi.getNode() && TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002629 std::swap(Lo, Hi);
2630 }
2631
2632 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002633 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002634
Gabor Greiff304a7a2008-08-28 21:40:38 +00002635 if (Hi.getNode() == NULL) {
Evan Cheng0076ca02006-12-12 19:53:13 +00002636 // Must be int <-> float one-to-one expansion.
2637 Result = Lo;
2638 break;
2639 }
2640
Evan Chengab51cf22006-10-13 21:14:26 +00002641 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002642 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002643 assert(isTypeLegal(Tmp2.getValueType()) &&
2644 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002645 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002646 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002647 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002648 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002649 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2650 break;
Mon P Wang58c37942008-10-30 08:01:45 +00002651 } // case Expand
Evan Chengab51cf22006-10-13 21:14:26 +00002652 }
2653 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002654 switch (getTypeAction(ST->getValue().getValueType())) {
2655 case Legal:
2656 Tmp3 = LegalizeOp(ST->getValue());
2657 break;
2658 case Promote:
Mon P Wang58c37942008-10-30 08:01:45 +00002659 if (!ST->getValue().getValueType().isVector()) {
2660 // We can promote the value, the truncstore will still take care of it.
2661 Tmp3 = PromoteOp(ST->getValue());
2662 break;
2663 }
2664 // Vector case falls through to expand
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002665 case Expand:
2666 // Just store the low part. This may become a non-trunc store, so make
2667 // sure to use getTruncStore, not UpdateNodeOperands below.
2668 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2669 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2670 SVOffset, MVT::i8, isVolatile, Alignment);
2671 }
Evan Chengab51cf22006-10-13 21:14:26 +00002672
Duncan Sands13237ac2008-06-06 12:08:01 +00002673 MVT StVT = ST->getMemoryVT();
2674 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands88de26c2008-01-22 07:17:34 +00002675
Duncan Sands13237ac2008-06-06 12:08:01 +00002676 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands88de26c2008-01-22 07:17:34 +00002677 // Promote to a byte-sized store with upper bits zero if not
2678 // storing an integral number of bytes. For example, promote
2679 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands13237ac2008-06-06 12:08:01 +00002680 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands88de26c2008-01-22 07:17:34 +00002681 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2682 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2683 SVOffset, NVT, isVolatile, Alignment);
2684 } else if (StWidth & (StWidth - 1)) {
2685 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands13237ac2008-06-06 12:08:01 +00002686 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands88de26c2008-01-22 07:17:34 +00002687 "Unsupported truncstore!");
2688 unsigned RoundWidth = 1 << Log2_32(StWidth);
2689 assert(RoundWidth < StWidth);
2690 unsigned ExtraWidth = StWidth - RoundWidth;
2691 assert(ExtraWidth < RoundWidth);
2692 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2693 "Store size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002694 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2695 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002696 SDValue Lo, Hi;
Duncan Sands88de26c2008-01-22 07:17:34 +00002697 unsigned IncrementSize;
2698
2699 if (TLI.isLittleEndian()) {
2700 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2701 // Store the bottom RoundWidth bits.
2702 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2703 SVOffset, RoundVT,
2704 isVolatile, Alignment);
2705
2706 // Store the remaining ExtraWidth bits.
2707 IncrementSize = RoundWidth / 8;
2708 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2709 DAG.getIntPtrConstant(IncrementSize));
2710 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2711 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2712 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2713 SVOffset + IncrementSize, ExtraVT, isVolatile,
2714 MinAlign(Alignment, IncrementSize));
2715 } else {
2716 // Big endian - avoid unaligned stores.
2717 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2718 // Store the top RoundWidth bits.
2719 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2720 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2721 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2722 RoundVT, isVolatile, Alignment);
2723
2724 // Store the remaining ExtraWidth bits.
2725 IncrementSize = RoundWidth / 8;
2726 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2727 DAG.getIntPtrConstant(IncrementSize));
2728 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2729 SVOffset + IncrementSize, ExtraVT, isVolatile,
2730 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002731 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002732
2733 // The order of the stores doesn't matter.
2734 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2735 } else {
2736 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2737 Tmp2 != ST->getBasePtr())
2738 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2739 ST->getOffset());
2740
2741 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2742 default: assert(0 && "This action is not supported yet!");
2743 case TargetLowering::Legal:
2744 // If this is an unaligned store and the target doesn't support it,
2745 // expand it.
2746 if (!TLI.allowsUnalignedMemoryAccesses()) {
2747 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002748 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands88de26c2008-01-22 07:17:34 +00002749 if (ST->getAlignment() < ABIAlignment)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002750 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands88de26c2008-01-22 07:17:34 +00002751 TLI);
2752 }
2753 break;
2754 case TargetLowering::Custom:
2755 Result = TLI.LowerOperation(Result, DAG);
2756 break;
2757 case Expand:
2758 // TRUNCSTORE:i16 i32 -> STORE i16
2759 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2760 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2761 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2762 isVolatile, Alignment);
2763 break;
2764 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002765 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002766 }
2767 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002768 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002769 case ISD::PCMARKER:
2770 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002772 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002773 case ISD::STACKSAVE:
2774 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002775 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2776 Tmp1 = Result.getValue(0);
2777 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002778
Chris Lattnerb3266452006-01-13 02:50:02 +00002779 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2780 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002781 case TargetLowering::Legal: break;
2782 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002783 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002784 if (Tmp3.getNode()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002785 Tmp1 = LegalizeOp(Tmp3);
2786 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002787 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002788 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002789 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002790 // Expand to CopyFromReg if the target set
2791 // StackPointerRegisterToSaveRestore.
2792 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002793 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002794 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002795 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002796 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002797 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2798 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002799 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002801 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002802
2803 // Since stacksave produce two values, make sure to remember that we
2804 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002805 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2806 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00002807 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002808
Chris Lattnerb3266452006-01-13 02:50:02 +00002809 case ISD::STACKRESTORE:
2810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2811 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002813
2814 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2815 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002816 case TargetLowering::Legal: break;
2817 case TargetLowering::Custom:
2818 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002819 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002820 break;
2821 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002822 // Expand to CopyToReg if the target set
2823 // StackPointerRegisterToSaveRestore.
2824 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2825 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2826 } else {
2827 Result = Tmp1;
2828 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002829 break;
2830 }
2831 break;
2832
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002833 case ISD::READCYCLECOUNTER:
2834 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002835 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002836 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2837 Node->getValueType(0))) {
2838 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002839 case TargetLowering::Legal:
2840 Tmp1 = Result.getValue(0);
2841 Tmp2 = Result.getValue(1);
2842 break;
Evan Cheng69739932006-11-29 08:26:18 +00002843 case TargetLowering::Custom:
2844 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002845 Tmp1 = LegalizeOp(Result.getValue(0));
2846 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002847 break;
2848 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002849
2850 // Since rdcc produce two values, make sure to remember that we legalized
2851 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002852 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2853 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002854 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002855
Chris Lattner39c67442005-01-14 22:08:15 +00002856 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002857 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2858 case Expand: assert(0 && "It's impossible to expand bools");
2859 case Legal:
2860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2861 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002862 case Promote: {
Mon P Wang58c37942008-10-30 08:01:45 +00002863 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002864 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002865 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002866 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002867 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002868 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002869 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002870 break;
2871 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002872 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002873 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002874 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002875
Chris Lattnerd02b0542006-01-28 10:58:55 +00002876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002877
Nate Begeman987121a2005-08-23 04:29:48 +00002878 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002879 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002880 case TargetLowering::Legal: break;
2881 case TargetLowering::Custom: {
2882 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002883 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002884 break;
2885 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002886 case TargetLowering::Expand:
2887 if (Tmp1.getOpcode() == ISD::SETCC) {
2888 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2889 Tmp2, Tmp3,
2890 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2891 } else {
2892 Result = DAG.getSelectCC(Tmp1,
2893 DAG.getConstant(0, Tmp1.getValueType()),
2894 Tmp2, Tmp3, ISD::SETNE);
2895 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002896 break;
2897 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002898 MVT NVT =
Chris Lattner3c0dd462005-01-16 07:29:19 +00002899 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2900 unsigned ExtOp, TruncOp;
Duncan Sands13237ac2008-06-06 12:08:01 +00002901 if (Tmp2.getValueType().isVector()) {
Evan Chengbe8a8932006-04-12 16:33:18 +00002902 ExtOp = ISD::BIT_CONVERT;
2903 TruncOp = ISD::BIT_CONVERT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002904 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002905 ExtOp = ISD::ANY_EXTEND;
2906 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002907 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002908 ExtOp = ISD::FP_EXTEND;
2909 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002910 }
2911 // Promote each of the values to the new type.
2912 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2913 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2914 // Perform the larger operation, then round down.
2915 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002916 if (TruncOp != ISD::FP_ROUND)
2917 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2918 else
2919 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2920 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002921 break;
2922 }
2923 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002924 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002925 case ISD::SELECT_CC: {
2926 Tmp1 = Node->getOperand(0); // LHS
2927 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002928 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2929 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002930 SDValue CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002931
Dale Johannesen160be0f2008-11-07 22:54:33 +00002932 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman7e7f4392006-02-01 07:19:44 +00002933
Evan Cheng3b0f5e42008-10-15 02:05:31 +00002934 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman7e7f4392006-02-01 07:19:44 +00002935 // the LHS is a legal SETCC itself. In this case, we need to compare
2936 // the result against zero to select between true and false values.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002937 if (Tmp2.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00002938 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2939 CC = DAG.getCondCode(ISD::SETNE);
2940 }
2941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2942
2943 // Everything is legal, see if we should expand this op or something.
2944 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2945 default: assert(0 && "This action is not supported yet!");
2946 case TargetLowering::Legal: break;
2947 case TargetLowering::Custom:
2948 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002949 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002950 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002951 }
2952 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002953 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002954 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002955 Tmp1 = Node->getOperand(0);
2956 Tmp2 = Node->getOperand(1);
2957 Tmp3 = Node->getOperand(2);
Evan Cheng3b0f5e42008-10-15 02:05:31 +00002958 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman7e7f4392006-02-01 07:19:44 +00002959
2960 // If we had to Expand the SetCC operands into a SELECT node, then it may
2961 // not always be possible to return a true LHS & RHS. In this case, just
2962 // return the value we legalized, returned in the LHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00002963 if (Tmp2.getNode() == 0) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00002964 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002965 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002966 }
Nate Begeman987121a2005-08-23 04:29:48 +00002967
Chris Lattnerf263a232006-01-30 22:43:50 +00002968 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002969 default: assert(0 && "Cannot handle this action for SETCC yet!");
2970 case TargetLowering::Custom:
2971 isCustom = true;
2972 // FALLTHROUGH.
2973 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002975 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002976 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002977 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002978 }
Nate Begeman987121a2005-08-23 04:29:48 +00002979 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002980 case TargetLowering::Promote: {
2981 // First step, figure out the appropriate operation to use.
2982 // Allow SETCC to not be supported for all legal data types
2983 // Mostly this targets FP
Duncan Sands13237ac2008-06-06 12:08:01 +00002984 MVT NewInTy = Node->getOperand(0).getValueType();
2985 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002986
2987 // Scan for the appropriate larger type to use.
2988 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002989 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002990
Duncan Sands13237ac2008-06-06 12:08:01 +00002991 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002992 "Fell off of the edge of the integer world");
Duncan Sands13237ac2008-06-06 12:08:01 +00002993 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002994 "Fell off of the edge of the floating point world");
2995
2996 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002997 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002998 break;
2999 }
Duncan Sands13237ac2008-06-06 12:08:01 +00003000 if (NewInTy.isInteger())
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003001 assert(0 && "Cannot promote Legal Integer SETCC yet");
3002 else {
3003 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3004 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3005 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003006 Tmp1 = LegalizeOp(Tmp1);
3007 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00003009 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00003010 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00003011 }
Nate Begeman987121a2005-08-23 04:29:48 +00003012 case TargetLowering::Expand:
3013 // Expand a setcc node into a select_cc of the same condition, lhs, and
3014 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands13237ac2008-06-06 12:08:01 +00003015 MVT VT = Node->getValueType(0);
Nate Begeman987121a2005-08-23 04:29:48 +00003016 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3017 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003018 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00003019 break;
3020 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003021 break;
Nate Begemancfcb5602008-05-12 19:40:03 +00003022 case ISD::VSETCC: {
3023 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003025 SDValue CC = Node->getOperand(2);
Nate Begemancfcb5602008-05-12 19:40:03 +00003026
3027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3028
3029 // Everything is legal, see if we should expand this op or something.
3030 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3031 default: assert(0 && "This action is not supported yet!");
3032 case TargetLowering::Legal: break;
3033 case TargetLowering::Custom:
3034 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003035 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemancfcb5602008-05-12 19:40:03 +00003036 break;
3037 }
3038 break;
3039 }
Chris Lattner5385db52005-05-09 20:23:03 +00003040
Chris Lattner4157c412005-04-02 04:00:59 +00003041 case ISD::SHL_PARTS:
3042 case ISD::SRA_PARTS:
3043 case ISD::SRL_PARTS: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003044 SmallVector<SDValue, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003045 bool Changed = false;
3046 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3047 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3048 Changed |= Ops.back() != Node->getOperand(i);
3049 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003050 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00003051 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00003052
Evan Cheng870e4f82006-01-09 18:31:59 +00003053 switch (TLI.getOperationAction(Node->getOpcode(),
3054 Node->getValueType(0))) {
3055 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003056 case TargetLowering::Legal: break;
3057 case TargetLowering::Custom:
3058 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003059 if (Tmp1.getNode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003060 SDValue Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00003061 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003062 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003063 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greifabfdf922008-08-26 22:36:50 +00003064 if (i == Op.getResNo())
Evan Cheng13e8c9d2006-01-19 04:54:52 +00003065 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00003066 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003067 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00003068 return RetVal;
3069 }
Evan Cheng870e4f82006-01-09 18:31:59 +00003070 break;
3071 }
3072
Chris Lattner13fe99c2005-04-02 05:00:07 +00003073 // Since these produce multiple values, make sure to remember that we
3074 // legalized all of them.
3075 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003076 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greifabfdf922008-08-26 22:36:50 +00003077 return Result.getValue(Op.getResNo());
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003078 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003079
3080 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00003081 case ISD::ADD:
3082 case ISD::SUB:
3083 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00003084 case ISD::MULHS:
3085 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00003086 case ISD::UDIV:
3087 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00003088 case ISD::AND:
3089 case ISD::OR:
3090 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00003091 case ISD::SHL:
3092 case ISD::SRL:
3093 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003094 case ISD::FADD:
3095 case ISD::FSUB:
3096 case ISD::FMUL:
3097 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00003098 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00003099 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00003100 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3101 case Expand: assert(0 && "Not possible");
3102 case Legal:
3103 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3104 break;
3105 case Promote:
3106 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3107 break;
3108 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003109
3110 if ((Node->getOpcode() == ISD::SHL ||
3111 Node->getOpcode() == ISD::SRL ||
3112 Node->getOpcode() == ISD::SRA) &&
3113 !Node->getValueType(0).isVector()) {
Mon P Wang4dd832d2008-12-09 05:46:39 +00003114 Tmp2 = LegalizeShiftAmount(Tmp2);
3115 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003116
Chris Lattnerd02b0542006-01-28 10:58:55 +00003117 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wang25f01062008-11-10 04:46:22 +00003118
Andrew Lenharth72594262005-12-24 23:42:32 +00003119 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00003120 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003121 case TargetLowering::Legal: break;
3122 case TargetLowering::Custom:
3123 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003124 if (Tmp1.getNode()) {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003125 Result = Tmp1;
3126 break;
Nate Begeman82f19252008-07-29 19:07:27 +00003127 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00003128 // Fall through if the custom lower can't deal with the operation
Chris Lattner87f08092006-04-02 03:57:31 +00003129 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003130 MVT VT = Op.getValueType();
Mon P Wang58c37942008-10-30 08:01:45 +00003131
Dan Gohmana1603612007-10-08 18:33:35 +00003132 // See if multiply or divide can be lowered using two-result operations.
3133 SDVTList VTs = DAG.getVTList(VT, VT);
3134 if (Node->getOpcode() == ISD::MUL) {
3135 // We just need the low half of the multiply; try both the signed
3136 // and unsigned forms. If the target supports both SMUL_LOHI and
3137 // UMUL_LOHI, form a preference by checking which forms of plain
3138 // MULH it supports.
3139 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3140 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3141 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3142 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3143 unsigned OpToUse = 0;
3144 if (HasSMUL_LOHI && !HasMULHS) {
3145 OpToUse = ISD::SMUL_LOHI;
3146 } else if (HasUMUL_LOHI && !HasMULHU) {
3147 OpToUse = ISD::UMUL_LOHI;
3148 } else if (HasSMUL_LOHI) {
3149 OpToUse = ISD::SMUL_LOHI;
3150 } else if (HasUMUL_LOHI) {
3151 OpToUse = ISD::UMUL_LOHI;
3152 }
3153 if (OpToUse) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003154 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003155 break;
3156 }
3157 }
3158 if (Node->getOpcode() == ISD::MULHS &&
3159 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003160 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3161 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003162 break;
3163 }
3164 if (Node->getOpcode() == ISD::MULHU &&
3165 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003166 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3167 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003168 break;
3169 }
3170 if (Node->getOpcode() == ISD::SDIV &&
3171 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003172 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3173 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003174 break;
3175 }
3176 if (Node->getOpcode() == ISD::UDIV &&
3177 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner24168962008-10-04 21:27:46 +00003178 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3179 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003180 break;
3181 }
Mon P Wang58c37942008-10-30 08:01:45 +00003182
Dan Gohman2a7de412007-10-11 23:57:53 +00003183 // Check to see if we have a libcall for this operator.
3184 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3185 bool isSigned = false;
3186 switch (Node->getOpcode()) {
3187 case ISD::UDIV:
3188 case ISD::SDIV:
3189 if (VT == MVT::i32) {
3190 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang58c37942008-10-30 08:01:45 +00003191 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00003192 isSigned = Node->getOpcode() == ISD::SDIV;
3193 }
3194 break;
Chris Lattner24168962008-10-04 21:27:46 +00003195 case ISD::MUL:
3196 if (VT == MVT::i32)
3197 LC = RTLIB::MUL_I32;
3198 break;
Dan Gohman2a7de412007-10-11 23:57:53 +00003199 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00003200 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3201 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00003202 break;
3203 default: break;
3204 }
3205 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003206 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003207 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003208 break;
3209 }
Mon P Wang58c37942008-10-30 08:01:45 +00003210
Duncan Sands13237ac2008-06-06 12:08:01 +00003211 assert(Node->getValueType(0).isVector() &&
Chris Lattner87f08092006-04-02 03:57:31 +00003212 "Cannot expand this binary operator!");
3213 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003214 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003215 break;
3216 }
Evan Cheng119266e2006-04-12 21:20:24 +00003217 case TargetLowering::Promote: {
3218 switch (Node->getOpcode()) {
3219 default: assert(0 && "Do not know how to promote this BinOp!");
3220 case ISD::AND:
3221 case ISD::OR:
3222 case ISD::XOR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003223 MVT OVT = Node->getValueType(0);
3224 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3225 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Cheng119266e2006-04-12 21:20:24 +00003226 // Bit convert each of the values to the new type.
3227 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3228 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3229 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3230 // Bit convert the result back the original type.
3231 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3232 break;
3233 }
3234 }
3235 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003236 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003237 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003238
Dan Gohman12334ac2007-10-05 14:17:22 +00003239 case ISD::SMUL_LOHI:
3240 case ISD::UMUL_LOHI:
3241 case ISD::SDIVREM:
3242 case ISD::UDIVREM:
3243 // These nodes will only be produced by target-specific lowering, so
3244 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003245 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003246 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003247
3248 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3249 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003251 break;
3252
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003253 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3254 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3255 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3256 case Expand: assert(0 && "Not possible");
3257 case Legal:
3258 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3259 break;
3260 case Promote:
3261 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3262 break;
3263 }
3264
3265 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3266
3267 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3268 default: assert(0 && "Operation not supported");
3269 case TargetLowering::Custom:
3270 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003271 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003272 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003273 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003274 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003275 // If this target supports fabs/fneg natively and select is cheap,
3276 // do this efficiently.
3277 if (!TLI.isSelectExpensive() &&
3278 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3279 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003280 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003281 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003282 // Get the sign bit of the RHS.
Duncan Sands13237ac2008-06-06 12:08:01 +00003283 MVT IVT =
Chris Lattner994d8e62006-03-13 06:08:38 +00003284 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003285 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00003286 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner994d8e62006-03-13 06:08:38 +00003287 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3288 // Get the absolute value of the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003289 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner994d8e62006-03-13 06:08:38 +00003290 // Select between the nabs and abs value based on the sign bit of
3291 // the input.
3292 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3293 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3294 AbsVal),
3295 AbsVal);
3296 Result = LegalizeOp(Result);
3297 break;
3298 }
3299
3300 // Otherwise, do bitwise ops!
Duncan Sands13237ac2008-06-06 12:08:01 +00003301 MVT NVT =
Evan Cheng003feb02007-01-04 21:56:39 +00003302 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3303 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3304 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3305 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003306 break;
3307 }
Evan Cheng003feb02007-01-04 21:56:39 +00003308 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003309 break;
3310
Nate Begeman5965bd12006-02-17 05:43:56 +00003311 case ISD::ADDC:
3312 case ISD::SUBC:
3313 Tmp1 = LegalizeOp(Node->getOperand(0));
3314 Tmp2 = LegalizeOp(Node->getOperand(1));
3315 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003316 Tmp3 = Result.getValue(0);
3317 Tmp4 = Result.getValue(1);
3318
3319 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3320 default: assert(0 && "This action is not supported yet!");
3321 case TargetLowering::Legal:
3322 break;
3323 case TargetLowering::Custom:
3324 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3325 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta7ae1a842008-11-27 05:58:04 +00003326 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003327 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3328 }
3329 break;
3330 }
Nate Begeman5965bd12006-02-17 05:43:56 +00003331 // Since this produces two values, make sure to remember that we legalized
3332 // both of them.
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003333 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3334 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3335 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukman835702a2005-04-21 22:36:52 +00003336
Nate Begeman5965bd12006-02-17 05:43:56 +00003337 case ISD::ADDE:
3338 case ISD::SUBE:
3339 Tmp1 = LegalizeOp(Node->getOperand(0));
3340 Tmp2 = LegalizeOp(Node->getOperand(1));
3341 Tmp3 = LegalizeOp(Node->getOperand(2));
3342 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003343 Tmp3 = Result.getValue(0);
3344 Tmp4 = Result.getValue(1);
3345
3346 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3347 default: assert(0 && "This action is not supported yet!");
3348 case TargetLowering::Legal:
3349 break;
3350 case TargetLowering::Custom:
3351 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3352 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta7ae1a842008-11-27 05:58:04 +00003353 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003354 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3355 }
3356 break;
3357 }
Nate Begeman5965bd12006-02-17 05:43:56 +00003358 // Since this produces two values, make sure to remember that we legalized
3359 // both of them.
Sanjiv Gupta80810f82008-11-26 11:19:00 +00003360 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3361 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3362 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman5965bd12006-02-17 05:43:56 +00003363
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003364 case ISD::BUILD_PAIR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003365 MVT PairTy = Node->getValueType(0);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003366 // TODO: handle the case where the Lo and Hi operands are not of legal type
3367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3369 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003370 case TargetLowering::Promote:
3371 case TargetLowering::Custom:
3372 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003373 case TargetLowering::Legal:
3374 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3375 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3376 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003377 case TargetLowering::Expand:
3378 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3379 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3380 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003381 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003382 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003383 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003384 break;
3385 }
3386 break;
3387 }
3388
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003389 case ISD::UREM:
3390 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003391 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003392 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3393 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003394
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003395 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003396 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3397 case TargetLowering::Custom:
3398 isCustom = true;
3399 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003400 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003402 if (isCustom) {
3403 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003404 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003405 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003406 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003407 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003408 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003409 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands13237ac2008-06-06 12:08:01 +00003410 MVT VT = Node->getValueType(0);
Dan Gohmana1603612007-10-08 18:33:35 +00003411
3412 // See if remainder can be lowered using two-result operations.
3413 SDVTList VTs = DAG.getVTList(VT, VT);
3414 if (Node->getOpcode() == ISD::SREM &&
3415 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003416 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003417 break;
3418 }
3419 if (Node->getOpcode() == ISD::UREM &&
3420 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003421 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003422 break;
3423 }
3424
Duncan Sands13237ac2008-06-06 12:08:01 +00003425 if (VT.isInteger()) {
Dan Gohmana1603612007-10-08 18:33:35 +00003426 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003427 TargetLowering::Legal) {
3428 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003429 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003430 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3431 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands13237ac2008-06-06 12:08:01 +00003432 } else if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003433 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003434 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003435 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003436 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003437 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3438 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003439 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003440 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003441 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003442 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00003443 assert(VT.isFloatingPoint() &&
Dan Gohmanccfc0282007-11-06 22:11:54 +00003444 "remainder op must have integer or floating-point type");
Duncan Sands13237ac2008-06-06 12:08:01 +00003445 if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003446 Result = LegalizeOp(UnrollVectorOp(Op));
3447 } else {
3448 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003449 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3450 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003451 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003452 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman08143e32007-11-05 23:35:22 +00003453 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003454 }
3455 break;
3456 }
Dan Gohmana1603612007-10-08 18:33:35 +00003457 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003458 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003459 case ISD::VAARG: {
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3462
Duncan Sands13237ac2008-06-06 12:08:01 +00003463 MVT VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003464 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3465 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003466 case TargetLowering::Custom:
3467 isCustom = true;
3468 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003469 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3471 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003472 Tmp1 = Result.getValue(1);
3473
3474 if (isCustom) {
3475 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003476 if (Tmp2.getNode()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003477 Result = LegalizeOp(Tmp2);
3478 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3479 }
3480 }
Nate Begemane74795c2006-01-25 18:21:52 +00003481 break;
3482 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003483 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003484 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003485 // Increment the pointer, VAList, to the next vaarg
Duncan Sands0207a3f2008-11-03 11:51:11 +00003486 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3487 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3488 TLI.getPointerTy()));
Nate Begemane74795c2006-01-25 18:21:52 +00003489 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003490 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003491 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003492 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003493 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003494 Result = LegalizeOp(Result);
3495 break;
3496 }
3497 }
3498 // Since VAARG produces two values, make sure to remember that we
3499 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003500 AddLegalizedOperand(SDValue(Node, 0), Result);
3501 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greifabfdf922008-08-26 22:36:50 +00003502 return Op.getResNo() ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003503 }
3504
3505 case ISD::VACOPY:
3506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3508 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3509
3510 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3511 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003512 case TargetLowering::Custom:
3513 isCustom = true;
3514 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003515 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3517 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003518 if (isCustom) {
3519 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003520 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003521 }
Nate Begemane74795c2006-01-25 18:21:52 +00003522 break;
3523 case TargetLowering::Expand:
3524 // This defaults to loading a pointer from the input and storing it to the
3525 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003526 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3527 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman9752a8f32008-04-17 02:09:26 +00003528 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3529 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003530 break;
3531 }
3532 break;
3533
3534 case ISD::VAEND:
3535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3536 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3537
3538 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3539 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003540 case TargetLowering::Custom:
3541 isCustom = true;
3542 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003543 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003544 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003545 if (isCustom) {
3546 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003547 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003548 }
Nate Begemane74795c2006-01-25 18:21:52 +00003549 break;
3550 case TargetLowering::Expand:
3551 Result = Tmp1; // Default to a no-op, return the chain
3552 break;
3553 }
3554 break;
3555
3556 case ISD::VASTART:
3557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3559
Chris Lattnerd02b0542006-01-28 10:58:55 +00003560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3561
Nate Begemane74795c2006-01-25 18:21:52 +00003562 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3563 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003564 case TargetLowering::Legal: break;
3565 case TargetLowering::Custom:
3566 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003567 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003568 break;
3569 }
3570 break;
3571
Nate Begeman1b8121b2006-01-11 21:21:00 +00003572 case ISD::ROTL:
3573 case ISD::ROTR:
3574 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3575 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003577 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3578 default:
3579 assert(0 && "ROTL/ROTR legalize operation not supported");
3580 break;
3581 case TargetLowering::Legal:
3582 break;
3583 case TargetLowering::Custom:
3584 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003585 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel16627a52007-04-02 21:36:32 +00003586 break;
3587 case TargetLowering::Promote:
3588 assert(0 && "Do not know how to promote ROTL/ROTR");
3589 break;
3590 case TargetLowering::Expand:
3591 assert(0 && "Do not know how to expand ROTL/ROTR");
3592 break;
3593 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003594 break;
3595
Nate Begeman2fba8a32006-01-14 03:14:10 +00003596 case ISD::BSWAP:
3597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3598 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003599 case TargetLowering::Custom:
3600 assert(0 && "Cannot custom legalize this yet!");
3601 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003602 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003603 break;
3604 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003605 MVT OVT = Tmp1.getValueType();
3606 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3607 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begeman2fba8a32006-01-14 03:14:10 +00003608
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003609 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3610 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3611 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3612 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3613 break;
3614 }
3615 case TargetLowering::Expand:
3616 Result = ExpandBSWAP(Tmp1);
3617 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003618 }
3619 break;
3620
Andrew Lenharth5e177822005-05-03 17:19:30 +00003621 case ISD::CTPOP:
3622 case ISD::CTTZ:
3623 case ISD::CTLZ:
3624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3625 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003626 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003627 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003628 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003629 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003630 TargetLowering::Custom) {
3631 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003632 if (Tmp1.getNode()) {
Scott Michel5b80ecb2007-08-02 02:22:46 +00003633 Result = Tmp1;
3634 }
Scott Michel34e2d222007-07-30 21:00:31 +00003635 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003636 break;
3637 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003638 MVT OVT = Tmp1.getValueType();
3639 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003640
3641 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003642 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3643 // Perform the larger operation, then subtract if needed.
3644 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003645 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003646 case ISD::CTPOP:
3647 Result = Tmp1;
3648 break;
3649 case ISD::CTTZ:
3650 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00003651 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003652 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003653 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003654 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003655 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003656 break;
3657 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003658 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003659 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003660 DAG.getConstant(NVT.getSizeInBits() -
3661 OVT.getSizeInBits(), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003662 break;
3663 }
3664 break;
3665 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003666 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003667 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003668 break;
3669 }
3670 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003671
Chris Lattner13fe99c2005-04-02 05:00:07 +00003672 // Unary operators
3673 case ISD::FABS:
3674 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003675 case ISD::FSQRT:
3676 case ISD::FSIN:
3677 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00003678 case ISD::FLOG:
3679 case ISD::FLOG2:
3680 case ISD::FLOG10:
3681 case ISD::FEXP:
3682 case ISD::FEXP2:
Dan Gohmanc6337ac2008-08-21 17:55:02 +00003683 case ISD::FTRUNC:
3684 case ISD::FFLOOR:
3685 case ISD::FCEIL:
3686 case ISD::FRINT:
3687 case ISD::FNEARBYINT:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003688 Tmp1 = LegalizeOp(Node->getOperand(0));
3689 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003690 case TargetLowering::Promote:
3691 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003692 isCustom = true;
3693 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003694 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003696 if (isCustom) {
3697 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003698 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng2443ab92006-01-31 18:14:25 +00003699 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003700 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003701 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003702 switch (Node->getOpcode()) {
3703 default: assert(0 && "Unreachable!");
3704 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003705 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3706 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003707 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003708 break;
Chris Lattner80026402005-04-30 04:43:14 +00003709 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003710 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands13237ac2008-06-06 12:08:01 +00003711 MVT VT = Node->getValueType(0);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003712 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003713 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00003714 ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003715 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3716 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003717 break;
3718 }
Evan Cheng0fff3972008-09-09 23:35:53 +00003719 case ISD::FSQRT:
3720 case ISD::FSIN:
3721 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00003722 case ISD::FLOG:
3723 case ISD::FLOG2:
3724 case ISD::FLOG10:
3725 case ISD::FEXP:
3726 case ISD::FEXP2:
Dan Gohman2af34bd2008-08-21 18:38:14 +00003727 case ISD::FTRUNC:
3728 case ISD::FFLOOR:
3729 case ISD::FCEIL:
3730 case ISD::FRINT:
Evan Cheng0fff3972008-09-09 23:35:53 +00003731 case ISD::FNEARBYINT: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003732 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003733
3734 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003735 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003736 Result = LegalizeOp(UnrollVectorOp(Op));
3737 break;
3738 }
3739
Evan Cheng31cbddf2007-01-12 02:11:51 +00003740 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003741 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003742 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003743 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3744 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003745 break;
3746 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003747 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3748 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003749 break;
3750 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003751 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3752 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003753 break;
Dale Johannesenda2d8062008-09-04 00:47:13 +00003754 case ISD::FLOG:
3755 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3756 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3757 break;
3758 case ISD::FLOG2:
3759 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3760 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3761 break;
3762 case ISD::FLOG10:
3763 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3764 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3765 break;
3766 case ISD::FEXP:
3767 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3768 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3769 break;
3770 case ISD::FEXP2:
3771 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3772 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3773 break;
Dan Gohman2af34bd2008-08-21 18:38:14 +00003774 case ISD::FTRUNC:
3775 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3776 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3777 break;
3778 case ISD::FFLOOR:
3779 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3780 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3781 break;
3782 case ISD::FCEIL:
3783 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3784 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3785 break;
3786 case ISD::FRINT:
3787 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3788 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3789 break;
3790 case ISD::FNEARBYINT:
3791 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3792 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3793 break;
Evan Cheng0fff3972008-09-09 23:35:53 +00003794 break;
Chris Lattner80026402005-04-30 04:43:14 +00003795 default: assert(0 && "Unreachable!");
3796 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003797 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003798 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003799 break;
3800 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003801 }
3802 break;
3803 }
3804 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003805 case ISD::FPOWI: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003806 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003807
3808 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003809 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003810 Result = LegalizeOp(UnrollVectorOp(Op));
3811 break;
3812 }
3813
3814 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003815 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3816 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003817 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003818 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003819 break;
3820 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003821 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003822 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003823 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3824 Node->getValueType(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00003825 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +00003826 // The input has to be a vector type, we have to either scalarize it, pack
3827 // it, or convert it based on whether the input vector type is legal.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003828 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greifabfdf922008-08-26 22:36:50 +00003829 int InIx = Node->getOperand(0).getResNo();
Duncan Sands13237ac2008-06-06 12:08:01 +00003830 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3831 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmana8665142007-06-25 16:23:39 +00003832
3833 // Figure out if there is a simple type corresponding to this Vector
3834 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00003835 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00003836 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003837 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003838 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3839 LegalizeOp(Node->getOperand(0)));
3840 break;
3841 } else if (NumElems == 1) {
3842 // Turn this into a bit convert of the scalar input.
3843 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3844 ScalarizeVectorOp(Node->getOperand(0)));
3845 break;
3846 } else {
3847 // FIXME: UNIMP! Store then reload
3848 assert(0 && "Cast from unsupported vector type not implemented yet!");
3849 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003850 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003851 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3852 Node->getOperand(0).getValueType())) {
3853 default: assert(0 && "Unknown operation action!");
3854 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003855 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3856 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003857 break;
3858 case TargetLowering::Legal:
3859 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003861 break;
3862 }
3863 }
3864 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00003865 case ISD::CONVERT_RNDSAT: {
3866 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3867 switch (CvtCode) {
3868 default: assert(0 && "Unknown cvt code!");
3869 case ISD::CVT_SF:
3870 case ISD::CVT_UF:
Mon P Wang58fb9132008-11-10 20:54:11 +00003871 case ISD::CVT_FF:
Mon P Wang8a5366332008-12-09 07:27:39 +00003872 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00003873 case ISD::CVT_FS:
3874 case ISD::CVT_FU:
3875 case ISD::CVT_SS:
3876 case ISD::CVT_SU:
3877 case ISD::CVT_US:
3878 case ISD::CVT_UU: {
3879 SDValue DTyOp = Node->getOperand(1);
3880 SDValue STyOp = Node->getOperand(2);
3881 SDValue RndOp = Node->getOperand(3);
3882 SDValue SatOp = Node->getOperand(4);
3883 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3884 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3885 case Legal:
3886 Tmp1 = LegalizeOp(Node->getOperand(0));
3887 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3888 RndOp, SatOp);
3889 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3890 TargetLowering::Custom) {
3891 Tmp1 = TLI.LowerOperation(Result, DAG);
3892 if (Tmp1.getNode()) Result = Tmp1;
3893 }
3894 break;
3895 case Promote:
3896 Result = PromoteOp(Node->getOperand(0));
3897 // For FP, make Op1 a i32
3898
Mon P Wang8a5366332008-12-09 07:27:39 +00003899 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang58fb9132008-11-10 20:54:11 +00003900 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3901 break;
3902 }
3903 break;
3904 }
3905 } // end switch CvtCode
3906 break;
3907 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003908 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003909 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003910 case ISD::UINT_TO_FP: {
3911 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman550c9af2008-08-14 20:04:46 +00003912 Result = LegalizeINT_TO_FP(Result, isSigned,
3913 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003914 break;
3915 }
3916 case ISD::TRUNCATE:
3917 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3918 case Legal:
3919 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel9b0b28e2008-12-02 19:55:08 +00003920 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3921 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3922 case TargetLowering::Custom:
Mon P Wangc68b3c42008-12-11 00:44:22 +00003923 isCustom = true;
3924 // FALLTHROUGH
Scott Michel9b0b28e2008-12-02 19:55:08 +00003925 case TargetLowering::Legal:
Mon P Wangc68b3c42008-12-11 00:44:22 +00003926 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3927 if (isCustom) {
3928 Tmp1 = TLI.LowerOperation(Result, DAG);
3929 if (Tmp1.getNode()) Result = Tmp1;
3930 }
3931 break;
Mon P Wang9c2d26d2008-12-12 01:25:51 +00003932 case TargetLowering::Expand:
3933 assert(Result.getValueType().isVector() && "must be vector type");
3934 // Unroll the truncate. We should do better.
3935 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Scheller318ccb0e2008-12-02 12:12:25 +00003936 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003937 break;
3938 case Expand:
3939 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3940
3941 // Since the result is legal, we should just be able to truncate the low
3942 // part of the source.
3943 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3944 break;
3945 case Promote:
3946 Result = PromoteOp(Node->getOperand(0));
3947 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3948 break;
3949 }
3950 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003951
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003952 case ISD::FP_TO_SINT:
3953 case ISD::FP_TO_UINT:
3954 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3955 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003956 Tmp1 = LegalizeOp(Node->getOperand(0));
3957
Chris Lattner44fe26f2005-07-29 00:11:56 +00003958 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3959 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003960 case TargetLowering::Custom:
3961 isCustom = true;
3962 // FALLTHROUGH
3963 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003964 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003965 if (isCustom) {
3966 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003967 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003968 }
3969 break;
3970 case TargetLowering::Promote:
3971 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3972 Node->getOpcode() == ISD::FP_TO_SINT);
3973 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003974 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003975 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003976 SDValue True, False;
Duncan Sands13237ac2008-06-06 12:08:01 +00003977 MVT VT = Node->getOperand(0).getValueType();
3978 MVT NVT = Node->getValueType(0);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003979 const uint64_t zero[] = {0, 0};
Duncan Sands13237ac2008-06-06 12:08:01 +00003980 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3981 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman837a6dc2008-02-29 01:44:25 +00003982 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003983 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003984 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begeman36853ee2005-08-14 01:20:53 +00003985 Node->getOperand(0), Tmp2, ISD::SETLT);
3986 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3987 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003988 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003989 Tmp2));
3990 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman837a6dc2008-02-29 01:44:25 +00003991 DAG.getConstant(x, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003992 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3993 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003994 } else {
3995 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3996 }
3997 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003998 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003999 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00004000 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004001 MVT VT = Op.getValueType();
4002 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00004003 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00004004 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00004005 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4006 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4007 Node->getOperand(0), DAG.getValueType(MVT::f64));
4008 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4009 DAG.getIntPtrConstant(1));
4010 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4011 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00004012 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4013 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4014 Tmp2 = DAG.getConstantFP(apf, OVT);
4015 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4016 // FIXME: generated code sucks.
4017 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4018 DAG.getNode(ISD::ADD, MVT::i32,
4019 DAG.getNode(ISD::FP_TO_SINT, VT,
4020 DAG.getNode(ISD::FSUB, OVT,
4021 Node->getOperand(0), Tmp2)),
4022 DAG.getConstant(0x80000000, MVT::i32)),
4023 DAG.getNode(ISD::FP_TO_SINT, VT,
4024 Node->getOperand(0)),
4025 DAG.getCondCode(ISD::SETGE));
4026 }
Dale Johannesen666323e2007-10-10 01:01:31 +00004027 break;
4028 }
Dan Gohmanf4300952008-03-10 23:03:31 +00004029 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sands77a3d052008-07-17 02:36:29 +00004030 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4031 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4032 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004033 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00004034 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00004035 break;
4036 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004037 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004038 Tmp1 = PromoteOp(Node->getOperand(0));
4039 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4040 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004041 break;
4042 }
4043 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004044
Chris Lattner91d86242008-01-16 06:57:07 +00004045 case ISD::FP_EXTEND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004046 MVT DstVT = Op.getValueType();
4047 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00004048 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4049 // The only other way we can lower this is to turn it into a STORE,
4050 // LOAD pair, targetting a temporary location (a stack slot).
4051 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4052 break;
Chris Lattner91d86242008-01-16 06:57:07 +00004053 }
4054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4055 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4056 case Legal:
4057 Tmp1 = LegalizeOp(Node->getOperand(0));
4058 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4059 break;
4060 case Promote:
4061 Tmp1 = PromoteOp(Node->getOperand(0));
4062 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4063 break;
4064 }
4065 break;
Chris Lattner72733e52008-01-17 07:00:52 +00004066 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00004067 case ISD::FP_ROUND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004068 MVT DstVT = Op.getValueType();
4069 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00004070 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4071 if (SrcVT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004072 SDValue Lo;
Dale Johannesen949e5a22008-01-20 01:18:38 +00004073 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00004074 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00004075 if (DstVT!=MVT::f64)
4076 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00004077 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00004078 }
Chris Lattner72733e52008-01-17 07:00:52 +00004079 // The only other way we can lower this is to turn it into a STORE,
4080 // LOAD pair, targetting a temporary location (a stack slot).
4081 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4082 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00004083 }
Chris Lattner91d86242008-01-16 06:57:07 +00004084 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4085 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4086 case Legal:
4087 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00004088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00004089 break;
4090 case Promote:
4091 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00004092 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4093 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00004094 break;
4095 }
4096 break;
Chris Lattner72733e52008-01-17 07:00:52 +00004097 }
Chris Lattner7753f172005-09-02 00:18:10 +00004098 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004099 case ISD::ZERO_EXTEND:
4100 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004101 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004102 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004103 case Legal:
4104 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelbe940422008-04-30 00:26:38 +00004105 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michela3cefea2008-02-15 23:05:48 +00004106 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4107 TargetLowering::Custom) {
Scott Michelbe940422008-04-30 00:26:38 +00004108 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004109 if (Tmp1.getNode()) Result = Tmp1;
Scott Michela3cefea2008-02-15 23:05:48 +00004110 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00004111 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004112 case Promote:
4113 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00004114 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004115 Tmp1 = PromoteOp(Node->getOperand(0));
4116 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00004117 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004118 case ISD::ZERO_EXTEND:
4119 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00004120 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00004121 Result = DAG.getZeroExtendInReg(Result,
4122 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004123 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004124 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004125 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00004126 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004128 Result,
4129 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00004130 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004131 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004132 }
4133 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004134 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00004135 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004136 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00004137 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00004138
4139 // If this operation is not supported, convert it to a shl/shr or load/store
4140 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00004141 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4142 default: assert(0 && "This action not supported for this op yet!");
4143 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00004144 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00004145 break;
4146 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00004147 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00004148 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00004149 // NOTE: we could fall back on load/store here too for targets without
4150 // SAR. However, it is doubtful that any exist.
Duncan Sands13237ac2008-06-06 12:08:01 +00004151 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4152 ExtraVT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004153 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00004154 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4155 Node->getOperand(0), ShiftCst);
4156 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4157 Result, ShiftCst);
4158 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00004159 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00004160 // EXTLOAD pair, targetting a temporary location (a stack slot).
4161
4162 // NOTE: there is a choice here between constantly creating new stack
4163 // slots and always reusing the same one. We currently always create
4164 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00004165 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4166 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00004167 } else {
4168 assert(0 && "Unknown op");
4169 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00004170 break;
Chris Lattner99222f72005-01-15 07:15:18 +00004171 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004172 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004173 }
Duncan Sands644f9172007-07-27 12:58:54 +00004174 case ISD::TRAMPOLINE: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004175 SDValue Ops[6];
Duncan Sands644f9172007-07-27 12:58:54 +00004176 for (unsigned i = 0; i != 6; ++i)
4177 Ops[i] = LegalizeOp(Node->getOperand(i));
4178 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4179 // The only option for this node is to custom lower it.
4180 Result = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004181 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00004182
4183 // Since trampoline produces two values, make sure to remember that we
4184 // legalized both of them.
4185 Tmp1 = LegalizeOp(Result.getValue(1));
4186 Result = LegalizeOp(Result);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004187 AddLegalizedOperand(SDValue(Node, 0), Result);
4188 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greifabfdf922008-08-26 22:36:50 +00004189 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00004190 }
Dan Gohmanfd3e3002008-05-14 00:43:10 +00004191 case ISD::FLT_ROUNDS_: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004192 MVT VT = Node->getValueType(0);
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004193 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4194 default: assert(0 && "This action not supported for this op yet!");
4195 case TargetLowering::Custom:
4196 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004197 if (Result.getNode()) break;
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004198 // Fall Thru
4199 case TargetLowering::Legal:
4200 // If this operation is not supported, lower it to constant 1
4201 Result = DAG.getConstant(1, VT);
4202 break;
4203 }
Dan Gohmanecb77382008-05-12 16:07:15 +00004204 break;
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004205 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004206 case ISD::TRAP: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004207 MVT VT = Node->getValueType(0);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004208 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4209 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00004210 case TargetLowering::Legal:
4211 Tmp1 = LegalizeOp(Node->getOperand(0));
4212 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4213 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004214 case TargetLowering::Custom:
4215 Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004216 if (Result.getNode()) break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004217 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00004218 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004219 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00004220 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004221 TargetLowering::ArgListTy Args;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004222 std::pair<SDValue,SDValue> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00004223 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen0e32a2c2008-09-26 19:31:26 +00004224 false, false, false, false, CallingConv::C, false,
Bill Wendling24c79f22008-09-16 21:48:12 +00004225 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattnerec224882008-01-15 22:09:33 +00004226 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004227 Result = CallResult.second;
4228 break;
4229 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004230 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004231 }
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004232
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004233 case ISD::SADDO:
4234 case ISD::SSUBO: {
Bill Wendling4498b472008-11-25 08:12:19 +00004235 MVT VT = Node->getValueType(0);
4236 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4237 default: assert(0 && "This action not supported for this op yet!");
4238 case TargetLowering::Custom:
4239 Result = TLI.LowerOperation(Op, DAG);
4240 if (Result.getNode()) break;
4241 // FALLTHROUGH
4242 case TargetLowering::Legal: {
4243 SDValue LHS = LegalizeOp(Node->getOperand(0));
4244 SDValue RHS = LegalizeOp(Node->getOperand(1));
4245
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004246 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4247 ISD::ADD : ISD::SUB, LHS.getValueType(),
4248 LHS, RHS);
Bill Wendling4498b472008-11-25 08:12:19 +00004249 MVT OType = Node->getValueType(1);
4250
Bill Wendlingbf592fc2008-11-25 08:19:22 +00004251 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling4498b472008-11-25 08:12:19 +00004252
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004253 // LHSSign -> LHS >= 0
4254 // RHSSign -> RHS >= 0
4255 // SumSign -> Sum >= 0
4256 //
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004257 // Add:
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004258 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004259 // Sub:
4260 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004261 //
4262 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4263 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004264 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4265 Node->getOpcode() == ISD::SADDO ?
4266 ISD::SETEQ : ISD::SETNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004267
Bill Wendlingb4ff5322008-11-25 19:40:17 +00004268 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4269 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004270
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004271 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling4498b472008-11-25 08:12:19 +00004272
4273 MVT ValueVTs[] = { LHS.getValueType(), OType };
4274 SDValue Ops[] = { Sum, Cmp };
4275
Duncan Sands3d960942008-12-01 11:41:29 +00004276 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4277 &Ops[0], 2);
Bill Wendling4498b472008-11-25 08:12:19 +00004278 SDNode *RNode = Result.getNode();
4279 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4280 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4281 break;
4282 }
4283 }
4284
4285 break;
4286 }
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004287 case ISD::UADDO:
4288 case ISD::USUBO: {
Bill Wendling66835472008-11-24 19:21:46 +00004289 MVT VT = Node->getValueType(0);
4290 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4291 default: assert(0 && "This action not supported for this op yet!");
4292 case TargetLowering::Custom:
4293 Result = TLI.LowerOperation(Op, DAG);
4294 if (Result.getNode()) break;
4295 // FALLTHROUGH
4296 case TargetLowering::Legal: {
4297 SDValue LHS = LegalizeOp(Node->getOperand(0));
4298 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004299
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004300 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4301 ISD::ADD : ISD::SUB, LHS.getValueType(),
4302 LHS, RHS);
Bill Wendling66835472008-11-24 19:21:46 +00004303 MVT OType = Node->getValueType(1);
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004304 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4305 Node->getOpcode () == ISD::UADDO ?
4306 ISD::SETULT : ISD::SETUGT);
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004307
Bill Wendling66835472008-11-24 19:21:46 +00004308 MVT ValueVTs[] = { LHS.getValueType(), OType };
4309 SDValue Ops[] = { Sum, Cmp };
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004310
Duncan Sands3d960942008-12-01 11:41:29 +00004311 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4312 &Ops[0], 2);
Bill Wendling66835472008-11-24 19:21:46 +00004313 SDNode *RNode = Result.getNode();
4314 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4315 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4316 break;
4317 }
4318 }
4319
Bill Wendlingbe8e7f82008-11-22 00:22:52 +00004320 break;
4321 }
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004322 case ISD::SMULO:
4323 case ISD::UMULO: {
4324 MVT VT = Node->getValueType(0);
4325 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4326 default: assert(0 && "This action is not supported at all!");
4327 case TargetLowering::Custom:
4328 Result = TLI.LowerOperation(Op, DAG);
4329 if (Result.getNode()) break;
4330 // Fall Thru
4331 case TargetLowering::Legal:
4332 // FIXME: According to Hacker's Delight, this can be implemented in
4333 // target independent lowering, but it would be inefficient, since it
Bill Wendlingf482f372008-12-10 02:01:32 +00004334 // requires a division + a branch.
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00004335 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4336 break;
4337 }
4338 break;
4339 }
4340
Chris Lattner99222f72005-01-15 07:15:18 +00004341 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004342
Chris Lattner101ea662006-04-08 04:13:17 +00004343 assert(Result.getValueType() == Op.getValueType() &&
4344 "Bad legalization!");
4345
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004346 // Make sure that the generated code is itself legal.
4347 if (Result != Op)
4348 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004349
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004350 // Note that LegalizeOp may be reentered even from single-use nodes, which
4351 // means that we always must cache transformed nodes.
4352 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004353 return Result;
4354}
4355
Chris Lattner4d978642005-01-15 22:16:26 +00004356/// PromoteOp - Given an operation that produces a value in an invalid type,
4357/// promote it to compute the value into a larger type. The produced value will
4358/// have the correct bits for the low portion of the register, but no guarantee
4359/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004360SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00004361 MVT VT = Op.getValueType();
4362 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004363 assert(getTypeAction(VT) == Promote &&
4364 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands11dd4242008-06-08 20:54:56 +00004365 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004366 "Cannot promote to smaller type!");
4367
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004368 SDValue Tmp1, Tmp2, Tmp3;
4369 SDValue Result;
Gabor Greiff304a7a2008-08-28 21:40:38 +00004370 SDNode *Node = Op.getNode();
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004371
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004372 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004373 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004374
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004375 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004376 case ISD::CopyFromReg:
4377 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004378 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004379#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004380 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004381#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004382 assert(0 && "Do not know how to promote this operator!");
4383 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004384 case ISD::UNDEF:
4385 Result = DAG.getNode(ISD::UNDEF, NVT);
4386 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004387 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004388 if (VT != MVT::i1)
4389 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4390 else
4391 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004392 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4393 break;
4394 case ISD::ConstantFP:
4395 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4396 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4397 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004398
Chris Lattner2cb338d2005-01-18 02:59:52 +00004399 case ISD::SETCC:
Scott Michela6729e82008-03-10 15:42:14 +00004400 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman63eb03f2008-03-14 00:53:31 +00004401 && "SetCC type is not legal??");
Scott Michela6729e82008-03-10 15:42:14 +00004402 Result = DAG.getNode(ISD::SETCC,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004403 TLI.getSetCCResultType(Node->getOperand(0)),
4404 Node->getOperand(0), Node->getOperand(1),
4405 Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004406 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00004407
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004408 case ISD::TRUNCATE:
4409 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4410 case Legal:
4411 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00004412 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004413 "This truncation doesn't make sense!");
Duncan Sands11dd4242008-06-08 20:54:56 +00004414 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004415 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4416 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004417 case Promote:
4418 // The truncation is not required, because we don't guarantee anything
4419 // about high bits anyway.
4420 Result = PromoteOp(Node->getOperand(0));
4421 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004422 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004423 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4424 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004425 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004426 }
4427 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004428 case ISD::SIGN_EXTEND:
4429 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004430 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004431 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4432 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4433 case Legal:
4434 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004435 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004436 break;
4437 case Promote:
4438 // Promote the reg if it's smaller.
4439 Result = PromoteOp(Node->getOperand(0));
4440 // The high bits are not guaranteed to be anything. Insert an extend.
4441 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004442 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004443 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004444 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004445 Result = DAG.getZeroExtendInReg(Result,
4446 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004447 break;
4448 }
4449 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00004450 case ISD::CONVERT_RNDSAT: {
4451 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4452 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4453 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4454 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4455 "can only promote integers");
4456 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4457 Node->getOperand(1), Node->getOperand(2),
4458 Node->getOperand(3), Node->getOperand(4),
4459 CvtCode);
4460 break;
4461
4462 }
Chris Lattner36e663d2005-12-23 00:16:34 +00004463 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004464 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4465 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004466 Result = PromoteOp(Result);
4467 break;
4468
Chris Lattner4d978642005-01-15 22:16:26 +00004469 case ISD::FP_EXTEND:
4470 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4471 case ISD::FP_ROUND:
4472 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4473 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4474 case Promote: assert(0 && "Unreachable with 2 FP types!");
4475 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004476 if (Node->getConstantOperandVal(1) == 0) {
4477 // Input is legal? Do an FP_ROUND_INREG.
4478 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4479 DAG.getValueType(VT));
4480 } else {
4481 // Just remove the truncate, it isn't affecting the value.
4482 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4483 Node->getOperand(1));
4484 }
Chris Lattner4d978642005-01-15 22:16:26 +00004485 break;
4486 }
4487 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004488 case ISD::SINT_TO_FP:
4489 case ISD::UINT_TO_FP:
4490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4491 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004492 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004493 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004494 break;
4495
4496 case Promote:
4497 Result = PromoteOp(Node->getOperand(0));
4498 if (Node->getOpcode() == ISD::SINT_TO_FP)
4499 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004500 Result,
4501 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004502 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004503 Result = DAG.getZeroExtendInReg(Result,
4504 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004505 // No extra round required here.
4506 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004507 break;
4508 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004509 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4510 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004511 // Round if we cannot tolerate excess precision.
4512 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004513 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4514 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004515 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004516 }
Chris Lattner4d978642005-01-15 22:16:26 +00004517 break;
4518
Chris Lattner268d4572005-12-09 17:32:47 +00004519 case ISD::SIGN_EXTEND_INREG:
4520 Result = PromoteOp(Node->getOperand(0));
4521 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4522 Node->getOperand(1));
4523 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004524 case ISD::FP_TO_SINT:
4525 case ISD::FP_TO_UINT:
4526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4527 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004528 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004529 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004530 break;
4531 case Promote:
4532 // The input result is prerounded, so we don't have to do anything
4533 // special.
4534 Tmp1 = PromoteOp(Node->getOperand(0));
4535 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004536 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004537 // If we're promoting a UINT to a larger size, check to see if the new node
4538 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4539 // we can use that instead. This allows us to generate better code for
4540 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4541 // legal, such as PowerPC.
4542 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004543 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004544 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4545 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004546 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4547 } else {
4548 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4549 }
Chris Lattner4d978642005-01-15 22:16:26 +00004550 break;
4551
Chris Lattner13fe99c2005-04-02 05:00:07 +00004552 case ISD::FABS:
4553 case ISD::FNEG:
4554 Tmp1 = PromoteOp(Node->getOperand(0));
4555 assert(Tmp1.getValueType() == NVT);
4556 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4557 // NOTE: we do not have to do any extra rounding here for
4558 // NoExcessFPPrecision, because we know the input will have the appropriate
4559 // precision, and these operations don't modify precision at all.
4560 break;
4561
Dale Johannesenda2d8062008-09-04 00:47:13 +00004562 case ISD::FLOG:
4563 case ISD::FLOG2:
4564 case ISD::FLOG10:
4565 case ISD::FEXP:
4566 case ISD::FEXP2:
Chris Lattner9d6fa982005-04-28 21:44:33 +00004567 case ISD::FSQRT:
4568 case ISD::FSIN:
4569 case ISD::FCOS:
Dan Gohman2af34bd2008-08-21 18:38:14 +00004570 case ISD::FTRUNC:
4571 case ISD::FFLOOR:
4572 case ISD::FCEIL:
4573 case ISD::FRINT:
4574 case ISD::FNEARBYINT:
Chris Lattner9d6fa982005-04-28 21:44:33 +00004575 Tmp1 = PromoteOp(Node->getOperand(0));
4576 assert(Tmp1.getValueType() == NVT);
4577 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004578 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004579 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4580 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004581 break;
4582
Evan Cheng0fff3972008-09-09 23:35:53 +00004583 case ISD::FPOW:
Chris Lattnerca401aa2007-03-03 23:43:21 +00004584 case ISD::FPOWI: {
Evan Cheng0fff3972008-09-09 23:35:53 +00004585 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattnerca401aa2007-03-03 23:43:21 +00004586 // directly as well, which may be better.
4587 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng0fff3972008-09-09 23:35:53 +00004588 Tmp2 = Node->getOperand(1);
4589 if (Node->getOpcode() == ISD::FPOW)
4590 Tmp2 = PromoteOp(Tmp2);
Chris Lattnerca401aa2007-03-03 23:43:21 +00004591 assert(Tmp1.getValueType() == NVT);
Evan Cheng0fff3972008-09-09 23:35:53 +00004592 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattnerca401aa2007-03-03 23:43:21 +00004593 if (NoExcessFPPrecision)
4594 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4595 DAG.getValueType(VT));
4596 break;
4597 }
4598
Dale Johannesen41be0d42008-08-28 02:44:49 +00004599 case ISD::ATOMIC_CMP_SWAP_8:
4600 case ISD::ATOMIC_CMP_SWAP_16:
4601 case ISD::ATOMIC_CMP_SWAP_32:
4602 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6a490372008-06-25 08:15:39 +00004603 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004604 Tmp2 = PromoteOp(Node->getOperand(2));
4605 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6a490372008-06-25 08:15:39 +00004606 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4607 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004608 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004609 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004610 // Remember that we legalized the chain.
4611 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4612 break;
4613 }
Dale Johannesen41be0d42008-08-28 02:44:49 +00004614 case ISD::ATOMIC_LOAD_ADD_8:
4615 case ISD::ATOMIC_LOAD_SUB_8:
4616 case ISD::ATOMIC_LOAD_AND_8:
4617 case ISD::ATOMIC_LOAD_OR_8:
4618 case ISD::ATOMIC_LOAD_XOR_8:
4619 case ISD::ATOMIC_LOAD_NAND_8:
4620 case ISD::ATOMIC_LOAD_MIN_8:
4621 case ISD::ATOMIC_LOAD_MAX_8:
4622 case ISD::ATOMIC_LOAD_UMIN_8:
4623 case ISD::ATOMIC_LOAD_UMAX_8:
4624 case ISD::ATOMIC_SWAP_8:
4625 case ISD::ATOMIC_LOAD_ADD_16:
4626 case ISD::ATOMIC_LOAD_SUB_16:
4627 case ISD::ATOMIC_LOAD_AND_16:
4628 case ISD::ATOMIC_LOAD_OR_16:
4629 case ISD::ATOMIC_LOAD_XOR_16:
4630 case ISD::ATOMIC_LOAD_NAND_16:
4631 case ISD::ATOMIC_LOAD_MIN_16:
4632 case ISD::ATOMIC_LOAD_MAX_16:
4633 case ISD::ATOMIC_LOAD_UMIN_16:
4634 case ISD::ATOMIC_LOAD_UMAX_16:
4635 case ISD::ATOMIC_SWAP_16:
4636 case ISD::ATOMIC_LOAD_ADD_32:
4637 case ISD::ATOMIC_LOAD_SUB_32:
4638 case ISD::ATOMIC_LOAD_AND_32:
4639 case ISD::ATOMIC_LOAD_OR_32:
4640 case ISD::ATOMIC_LOAD_XOR_32:
4641 case ISD::ATOMIC_LOAD_NAND_32:
4642 case ISD::ATOMIC_LOAD_MIN_32:
4643 case ISD::ATOMIC_LOAD_MAX_32:
4644 case ISD::ATOMIC_LOAD_UMIN_32:
4645 case ISD::ATOMIC_LOAD_UMAX_32:
4646 case ISD::ATOMIC_SWAP_32:
4647 case ISD::ATOMIC_LOAD_ADD_64:
4648 case ISD::ATOMIC_LOAD_SUB_64:
4649 case ISD::ATOMIC_LOAD_AND_64:
4650 case ISD::ATOMIC_LOAD_OR_64:
4651 case ISD::ATOMIC_LOAD_XOR_64:
4652 case ISD::ATOMIC_LOAD_NAND_64:
4653 case ISD::ATOMIC_LOAD_MIN_64:
4654 case ISD::ATOMIC_LOAD_MAX_64:
4655 case ISD::ATOMIC_LOAD_UMIN_64:
4656 case ISD::ATOMIC_LOAD_UMAX_64:
4657 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6a490372008-06-25 08:15:39 +00004658 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004659 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6a490372008-06-25 08:15:39 +00004660 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4661 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004662 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004663 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004664 // Remember that we legalized the chain.
4665 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4666 break;
4667 }
4668
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004669 case ISD::AND:
4670 case ISD::OR:
4671 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004672 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004673 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004674 case ISD::MUL:
4675 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004676 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004677 // that too is okay if they are integer operations.
4678 Tmp1 = PromoteOp(Node->getOperand(0));
4679 Tmp2 = PromoteOp(Node->getOperand(1));
4680 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4681 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004682 break;
4683 case ISD::FADD:
4684 case ISD::FSUB:
4685 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004686 Tmp1 = PromoteOp(Node->getOperand(0));
4687 Tmp2 = PromoteOp(Node->getOperand(1));
4688 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4689 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4690
4691 // Floating point operations will give excess precision that we may not be
4692 // able to tolerate. If we DO allow excess precision, just leave it,
4693 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004694 // FIXME: Why would we need to round FP ops more than integer ones?
4695 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004696 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004697 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4698 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004699 break;
4700
Chris Lattner4d978642005-01-15 22:16:26 +00004701 case ISD::SDIV:
4702 case ISD::SREM:
4703 // These operators require that their input be sign extended.
4704 Tmp1 = PromoteOp(Node->getOperand(0));
4705 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004706 if (NVT.isInteger()) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004707 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4708 DAG.getValueType(VT));
4709 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4710 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004711 }
4712 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4713
4714 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands13237ac2008-06-06 12:08:01 +00004715 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004716 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4717 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004718 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004719 case ISD::FDIV:
4720 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004721 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004722 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004723 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004724 case Expand: assert(0 && "not implemented");
4725 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4726 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004727 }
4728 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004729 case Expand: assert(0 && "not implemented");
4730 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4731 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004732 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004733 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4734
4735 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004736 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004737 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4738 DAG.getValueType(VT));
4739 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004740
4741 case ISD::UDIV:
4742 case ISD::UREM:
4743 // These operators require that their input be zero extended.
4744 Tmp1 = PromoteOp(Node->getOperand(0));
4745 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004746 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004747 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4748 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004749 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4750 break;
4751
4752 case ISD::SHL:
4753 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004754 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004755 break;
4756 case ISD::SRA:
4757 // The input value must be properly sign extended.
4758 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004759 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4760 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004761 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004762 break;
4763 case ISD::SRL:
4764 // The input value must be properly zero extended.
4765 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004766 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004767 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004768 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004769
4770 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004771 Tmp1 = Node->getOperand(0); // Get the chain.
4772 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004773 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4774 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands93e180342008-07-04 11:47:58 +00004775 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman595ec732006-01-28 03:14:31 +00004776 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004777 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004778 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004779 // Increment the pointer, VAList, to the next vaarg
4780 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands13237ac2008-06-06 12:08:01 +00004781 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman595ec732006-01-28 03:14:31 +00004782 TLI.getPointerTy()));
4783 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004784 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004785 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004786 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004787 }
4788 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004789 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004790 break;
4791
Evan Chenge71fe34d2006-10-09 20:57:25 +00004792 case ISD::LOAD: {
4793 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004794 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4795 ? ISD::EXTLOAD : LD->getExtensionType();
4796 Result = DAG.getExtLoad(ExtType, NVT,
4797 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004798 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004799 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004800 LD->isVolatile(),
4801 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004802 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004803 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004804 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004805 }
Scott Micheld831cc42008-06-02 22:18:03 +00004806 case ISD::SELECT: {
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004807 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4808 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Micheld831cc42008-06-02 22:18:03 +00004809
Duncan Sands13237ac2008-06-06 12:08:01 +00004810 MVT VT2 = Tmp2.getValueType();
Scott Micheld831cc42008-06-02 22:18:03 +00004811 assert(VT2 == Tmp3.getValueType()
Scott Michela7d86492008-06-03 19:13:20 +00004812 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4813 // Ensure that the resulting node is at least the same size as the operands'
4814 // value types, because we cannot assume that TLI.getSetCCValueType() is
4815 // constant.
4816 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004817 break;
Scott Micheld831cc42008-06-02 22:18:03 +00004818 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004819 case ISD::SELECT_CC:
4820 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4821 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4822 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004823 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004824 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004825 case ISD::BSWAP:
4826 Tmp1 = Node->getOperand(0);
4827 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4828 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4829 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004830 DAG.getConstant(NVT.getSizeInBits() -
4831 VT.getSizeInBits(),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004832 TLI.getShiftAmountTy()));
4833 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004834 case ISD::CTPOP:
4835 case ISD::CTTZ:
4836 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004837 // Zero extend the argument
4838 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004839 // Perform the larger operation, then subtract if needed.
4840 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004841 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004842 case ISD::CTPOP:
4843 Result = Tmp1;
4844 break;
4845 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004846 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00004847 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004848 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohman1796f1f2007-05-18 17:52:13 +00004849 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004850 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00004851 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004852 break;
4853 case ISD::CTLZ:
4854 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004855 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004856 DAG.getConstant(NVT.getSizeInBits() -
4857 VT.getSizeInBits(), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004858 break;
4859 }
4860 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004861 case ISD::EXTRACT_SUBVECTOR:
4862 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004863 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004864 case ISD::EXTRACT_VECTOR_ELT:
4865 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4866 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004867 }
4868
Gabor Greiff304a7a2008-08-28 21:40:38 +00004869 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004870
4871 // Make sure the result is itself legal.
4872 Result = LegalizeOp(Result);
4873
4874 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004875 AddPromotedOperand(Op, Result);
4876 return Result;
4877}
Chris Lattnerdc750592005-01-07 07:47:09 +00004878
Dan Gohmana8665142007-06-25 16:23:39 +00004879/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4880/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4881/// based on the vector type. The return type of this matches the element type
4882/// of the vector, which may not be legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004883SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004884 // We know that operand #0 is the Vec vector. If the index is a constant
4885 // or if the invec is a supported hardware type, we can use it. Otherwise,
4886 // lower to a store then an indexed load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004887 SDValue Vec = Op.getOperand(0);
4888 SDValue Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004889
Duncan Sands13237ac2008-06-06 12:08:01 +00004890 MVT TVT = Vec.getValueType();
4891 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner6f423252006-03-31 17:55:51 +00004892
Dan Gohmana8665142007-06-25 16:23:39 +00004893 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4894 default: assert(0 && "This action is not supported yet!");
4895 case TargetLowering::Custom: {
4896 Vec = LegalizeOp(Vec);
4897 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004898 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004899 if (Tmp3.getNode())
Dan Gohmana8665142007-06-25 16:23:39 +00004900 return Tmp3;
4901 break;
4902 }
4903 case TargetLowering::Legal:
4904 if (isTypeLegal(TVT)) {
4905 Vec = LegalizeOp(Vec);
4906 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004907 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004908 }
4909 break;
Mon P Wang58c37942008-10-30 08:01:45 +00004910 case TargetLowering::Promote:
4911 assert(TVT.isVector() && "not vector type");
4912 // fall thru to expand since vectors are by default are promote
Dan Gohmana8665142007-06-25 16:23:39 +00004913 case TargetLowering::Expand:
4914 break;
4915 }
4916
4917 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004918 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004919 Op = ScalarizeVectorOp(Vec);
4920 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004921 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004922 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004923 SDValue Lo, Hi;
Chris Lattner6f423252006-03-31 17:55:51 +00004924 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00004925 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004926 Vec = Lo;
4927 } else {
4928 Vec = Hi;
Dan Gohmaneffb8942008-09-12 16:56:44 +00004929 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004930 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004931 }
Dan Gohmana8665142007-06-25 16:23:39 +00004932
Chris Lattner6f423252006-03-31 17:55:51 +00004933 // It's now an extract from the appropriate high or low part. Recurse.
4934 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004935 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004936 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004937 // Store the value to a temporary stack slot, then LOAD the scalar
4938 // element back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004939 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4940 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmana8665142007-06-25 16:23:39 +00004941
4942 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +00004943 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmana8665142007-06-25 16:23:39 +00004944 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4945 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004946
Duncan Sands11dd4242008-06-08 20:54:56 +00004947 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004948 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004949 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004950 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004951
Dan Gohmana8665142007-06-25 16:23:39 +00004952 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4953
4954 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004955 }
Dan Gohmana8665142007-06-25 16:23:39 +00004956 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004957}
4958
Dan Gohmana8665142007-06-25 16:23:39 +00004959/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004960/// we assume the operation can be split if it is not already legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004961SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004962 // We know that operand #0 is the Vec vector. For now we assume the index
4963 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004964 SDValue Vec = Op.getOperand(0);
4965 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman26455c42007-06-13 15:12:02 +00004966
Duncan Sands13237ac2008-06-06 12:08:01 +00004967 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman26455c42007-06-13 15:12:02 +00004968
Duncan Sands13237ac2008-06-06 12:08:01 +00004969 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman26455c42007-06-13 15:12:02 +00004970 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004971 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004972 }
4973
4974 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004975 SDValue Lo, Hi;
Dan Gohman26455c42007-06-13 15:12:02 +00004976 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00004977 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman26455c42007-06-13 15:12:02 +00004978 Vec = Lo;
4979 } else {
4980 Vec = Hi;
Dan Gohmaneffb8942008-09-12 16:56:44 +00004981 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4982 Idx.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004983 }
4984
4985 // It's now an extract from the appropriate high or low part. Recurse.
4986 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004987 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004988}
4989
Nate Begeman7e7f4392006-02-01 07:19:44 +00004990/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4991/// with condition CC on the current target. This usually involves legalizing
4992/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4993/// there may be no choice but to create a new SetCC node to represent the
4994/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004995/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4996void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4997 SDValue &RHS,
4998 SDValue &CC) {
4999 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00005000
5001 switch (getTypeAction(LHS.getValueType())) {
5002 case Legal:
5003 Tmp1 = LegalizeOp(LHS); // LHS
5004 Tmp2 = LegalizeOp(RHS); // RHS
5005 break;
5006 case Promote:
5007 Tmp1 = PromoteOp(LHS); // LHS
5008 Tmp2 = PromoteOp(RHS); // RHS
5009
5010 // If this is an FP compare, the operands have already been extended.
Duncan Sands13237ac2008-06-06 12:08:01 +00005011 if (LHS.getValueType().isInteger()) {
5012 MVT VT = LHS.getValueType();
5013 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman7e7f4392006-02-01 07:19:44 +00005014
5015 // Otherwise, we have to insert explicit sign or zero extends. Note
5016 // that we could insert sign extends for ALL conditions, but zero extend
5017 // is cheaper on many machines (an AND instead of two shifts), so prefer
5018 // it.
5019 switch (cast<CondCodeSDNode>(CC)->get()) {
5020 default: assert(0 && "Unknown integer comparison!");
5021 case ISD::SETEQ:
5022 case ISD::SETNE:
5023 case ISD::SETUGE:
5024 case ISD::SETUGT:
5025 case ISD::SETULE:
5026 case ISD::SETULT:
5027 // ALL of these operations will work if we either sign or zero extend
5028 // the operands (including the unsigned comparisons!). Zero extend is
5029 // usually a simpler/cheaper operation, so prefer it.
5030 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5031 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5032 break;
5033 case ISD::SETGE:
5034 case ISD::SETGT:
5035 case ISD::SETLT:
5036 case ISD::SETLE:
5037 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5038 DAG.getValueType(VT));
5039 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5040 DAG.getValueType(VT));
Evan Chengda9b7522008-10-13 18:46:18 +00005041 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5042 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman7e7f4392006-02-01 07:19:44 +00005043 break;
5044 }
5045 }
5046 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005047 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00005048 MVT VT = LHS.getValueType();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005049 if (VT == MVT::f32 || VT == MVT::f64) {
5050 // Expand into one or more soft-fp libcall(s).
Evan Cheng4c609ab2008-07-01 21:35:46 +00005051 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005052 switch (cast<CondCodeSDNode>(CC)->get()) {
5053 case ISD::SETEQ:
5054 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005055 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005056 break;
5057 case ISD::SETNE:
5058 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005059 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005060 break;
5061 case ISD::SETGE:
5062 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005063 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005064 break;
5065 case ISD::SETLT:
5066 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005067 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005068 break;
5069 case ISD::SETLE:
5070 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005071 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005072 break;
5073 case ISD::SETGT:
5074 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005075 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005076 break;
5077 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00005078 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5079 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005080 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00005081 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005082 break;
5083 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005084 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005085 switch (cast<CondCodeSDNode>(CC)->get()) {
5086 case ISD::SETONE:
5087 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00005088 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005089 // Fallthrough
5090 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005091 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005092 break;
5093 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005094 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005095 break;
5096 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005097 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005098 break;
5099 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005100 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005101 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005102 case ISD::SETUEQ:
5103 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005104 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005105 default: assert(0 && "Unsupported FP setcc!");
5106 }
5107 }
Duncan Sands1ae6ef82008-06-30 10:19:09 +00005108
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005109 SDValue Dummy;
5110 SDValue Ops[2] = { LHS, RHS };
Gabor Greiff304a7a2008-08-28 21:40:38 +00005111 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencer791864c2007-01-03 04:22:32 +00005112 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005113 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00005114 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00005115 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michela6729e82008-03-10 15:42:14 +00005116 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00005117 CC);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005118 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencer791864c2007-01-03 04:22:32 +00005119 false /*sign irrelevant*/, Dummy);
Scott Michela6729e82008-03-10 15:42:14 +00005120 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00005121 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005122 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005123 Tmp2 = SDValue();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005124 }
Evan Chengd8b83e12008-07-07 07:18:09 +00005125 LHS = LegalizeOp(Tmp1);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005126 RHS = Tmp2;
5127 return;
5128 }
5129
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005130 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman7e7f4392006-02-01 07:19:44 +00005131 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005132 ExpandOp(RHS, RHSLo, RHSHi);
5133 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5134
5135 if (VT==MVT::ppcf128) {
5136 // FIXME: This generated code sucks. We want to generate
Dale Johannesenbaf67622008-09-12 00:30:56 +00005137 // FCMPU crN, hi1, hi2
Dale Johannesenf864ac92007-10-06 01:24:11 +00005138 // BNE crN, L:
Dale Johannesenbaf67622008-09-12 00:30:56 +00005139 // FCMPU crN, lo1, lo2
Dale Johannesenf864ac92007-10-06 01:24:11 +00005140 // The following can be improved, but not that much.
Dale Johannesenbaf67622008-09-12 00:30:56 +00005141 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5142 ISD::SETOEQ);
Scott Michela6729e82008-03-10 15:42:14 +00005143 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005144 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesenbaf67622008-09-12 00:30:56 +00005145 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5146 ISD::SETUNE);
Scott Michela6729e82008-03-10 15:42:14 +00005147 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00005148 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5149 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005150 Tmp2 = SDValue();
Dale Johannesenf864ac92007-10-06 01:24:11 +00005151 break;
5152 }
5153
5154 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00005155 case ISD::SETEQ:
5156 case ISD::SETNE:
5157 if (RHSLo == RHSHi)
5158 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5159 if (RHSCST->isAllOnesValue()) {
5160 // Comparison to -1.
5161 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5162 Tmp2 = RHSLo;
5163 break;
5164 }
5165
5166 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5167 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5168 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5169 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5170 break;
5171 default:
5172 // If this is a comparison of the sign bit, just look at the top part.
5173 // X > -1, x < 0
5174 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5175 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00005176 CST->isNullValue()) || // X < 0
Nate Begeman7e7f4392006-02-01 07:19:44 +00005177 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5178 CST->isAllOnesValue())) { // X > -1
5179 Tmp1 = LHSHi;
5180 Tmp2 = RHSHi;
5181 break;
5182 }
5183
5184 // FIXME: This generated code sucks.
5185 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00005186 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00005187 default: assert(0 && "Unknown integer setcc!");
5188 case ISD::SETLT:
5189 case ISD::SETULT: LowCC = ISD::SETULT; break;
5190 case ISD::SETGT:
5191 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5192 case ISD::SETLE:
5193 case ISD::SETULE: LowCC = ISD::SETULE; break;
5194 case ISD::SETGE:
5195 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5196 }
5197
5198 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5199 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5200 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5201
5202 // NOTE: on targets without efficient SELECT of bools, we can always use
5203 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00005204 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michela6729e82008-03-10 15:42:14 +00005205 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman63eb03f2008-03-14 00:53:31 +00005206 LowCC, false, DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005207 if (!Tmp1.getNode())
Scott Michela6729e82008-03-10 15:42:14 +00005208 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5209 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00005210 CCCode, false, DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005211 if (!Tmp2.getNode())
Scott Michela6729e82008-03-10 15:42:14 +00005212 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00005213 RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00005214
Gabor Greiff304a7a2008-08-28 21:40:38 +00005215 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5216 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohmanb72127a2008-03-13 22:13:53 +00005217 if ((Tmp1C && Tmp1C->isNullValue()) ||
5218 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng93049452007-02-08 22:16:19 +00005219 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5220 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohmanb72127a2008-03-13 22:13:53 +00005221 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng93049452007-02-08 22:16:19 +00005222 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5223 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5224 // low part is known false, returns high part.
5225 // For LE / GE, if high part is known false, ignore the low part.
5226 // For LT / GT, if high part is known true, ignore the low part.
5227 Tmp1 = Tmp2;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005228 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00005229 } else {
Scott Michela6729e82008-03-10 15:42:14 +00005230 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00005231 ISD::SETEQ, false, DagCombineInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005232 if (!Result.getNode())
Scott Michela6729e82008-03-10 15:42:14 +00005233 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00005234 ISD::SETEQ);
Evan Cheng93049452007-02-08 22:16:19 +00005235 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5236 Result, Tmp1, Tmp2));
5237 Tmp1 = Result;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005238 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00005239 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00005240 }
5241 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00005242 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00005243 LHS = Tmp1;
5244 RHS = Tmp2;
5245}
5246
Evan Cheng3b0f5e42008-10-15 02:05:31 +00005247/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5248/// condition code CC on the current target. This routine assumes LHS and rHS
5249/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5250/// illegal condition code into AND / OR of multiple SETCC values.
5251void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5252 SDValue &LHS, SDValue &RHS,
5253 SDValue &CC) {
5254 MVT OpVT = LHS.getValueType();
5255 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5256 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5257 default: assert(0 && "Unknown condition code action!");
5258 case TargetLowering::Legal:
5259 // Nothing to do.
5260 break;
5261 case TargetLowering::Expand: {
5262 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5263 unsigned Opc = 0;
5264 switch (CCCode) {
5265 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman97d3f6c2008-10-21 03:12:54 +00005266 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5267 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5268 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5269 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5270 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5271 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5272 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5273 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5274 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5275 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5276 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5277 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00005278 // FIXME: Implement more expansions.
5279 }
5280
5281 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5282 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5283 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5284 RHS = SDValue();
5285 CC = SDValue();
5286 break;
5287 }
5288 }
5289}
5290
Chris Lattner87bc3e72008-01-16 07:45:30 +00005291/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5292/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5293/// a load from the stack slot to DestVT, extending it if needed.
5294/// The resultant code need not be legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005295SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5296 MVT SlotVT,
5297 MVT DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00005298 // Create the stack frame object.
Mon P Wang5c755ff2008-07-05 20:40:31 +00005299 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5300 SrcOp.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005301 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang5c755ff2008-07-05 20:40:31 +00005302
Dan Gohman54d3b5a2008-02-11 18:58:42 +00005303 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00005304 int SPFI = StackPtrFI->getIndex();
Mon P Wang5c755ff2008-07-05 20:40:31 +00005305
Duncan Sands13237ac2008-06-06 12:08:01 +00005306 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5307 unsigned SlotSize = SlotVT.getSizeInBits();
5308 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang5c755ff2008-07-05 20:40:31 +00005309 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5310 DestVT.getTypeForMVT());
Chris Lattner36e663d2005-12-23 00:16:34 +00005311
Chris Lattner87bc3e72008-01-16 07:45:30 +00005312 // Emit a store to the stack slot. Use a truncstore if the input value is
5313 // later than DestVT.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005314 SDValue Store;
Mon P Wang5c755ff2008-07-05 20:40:31 +00005315
Chris Lattner87bc3e72008-01-16 07:45:30 +00005316 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00005317 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005318 PseudoSourceValue::getFixedStack(SPFI), 0,
5319 SlotVT, false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005320 else {
5321 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00005322 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005323 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang5c755ff2008-07-05 20:40:31 +00005324 false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005325 }
5326
Chris Lattner36e663d2005-12-23 00:16:34 +00005327 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00005328 if (SlotSize == DestSize)
Mon P Wang5c755ff2008-07-05 20:40:31 +00005329 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00005330
5331 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang5c755ff2008-07-05 20:40:31 +00005332 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5333 false, DestAlign);
Chris Lattner36e663d2005-12-23 00:16:34 +00005334}
5335
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005336SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner6be79822006-04-04 17:23:26 +00005337 // Create a vector sized/aligned stack slot, store the value to element #0,
5338 // then load the whole vector back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005339 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00005340
Dan Gohman54d3b5a2008-02-11 18:58:42 +00005341 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00005342 int SPFI = StackPtrFI->getIndex();
5343
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005344 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005345 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00005346 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00005347 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner6be79822006-04-04 17:23:26 +00005348}
5349
5350
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005351/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00005352/// support the operation, but do support the resultant vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005353SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005354
5355 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00005356 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00005357 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005358 bool isOnlyLowElement = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005359 SDValue SplatValue = Node->getOperand(0);
Chris Lattner322c8262008-03-09 00:29:42 +00005360
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattner322c8262008-03-09 00:29:42 +00005362 // and use a bitmask instead of a list of elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005363 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng1d2e9952006-03-24 01:17:21 +00005364 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005365 bool isConstant = true;
5366 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5367 SplatValue.getOpcode() != ISD::UNDEF)
5368 isConstant = false;
5369
Evan Cheng1d2e9952006-03-24 01:17:21 +00005370 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005371 SDValue V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00005372 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005373 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005374 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00005375 if (SplatValue != V)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005376 SplatValue = SDValue(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005377
5378 // If this isn't a constant element or an undef, we can't use a constant
5379 // pool load.
5380 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5381 V.getOpcode() != ISD::UNDEF)
5382 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005383 }
5384
5385 if (isOnlyLowElement) {
5386 // If the low element is an undef too, then this whole things is an undef.
5387 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5388 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5389 // Otherwise, turn this into a scalar_to_vector node.
5390 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5391 Node->getOperand(0));
5392 }
5393
Chris Lattner77e271c2006-03-24 07:29:17 +00005394 // If all elements are constants, create a load from the constant pool.
5395 if (isConstant) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005396 MVT VT = Node->getValueType(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005397 std::vector<Constant*> CV;
5398 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5399 if (ConstantFPSDNode *V =
5400 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00005401 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00005402 } else if (ConstantSDNode *V =
Chris Lattner3b187622008-04-20 00:41:09 +00005403 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00005404 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00005405 } else {
5406 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner3b187622008-04-20 00:41:09 +00005407 const Type *OpNTy =
Duncan Sands13237ac2008-06-06 12:08:01 +00005408 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner77e271c2006-03-24 07:29:17 +00005409 CV.push_back(UndefValue::get(OpNTy));
5410 }
5411 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00005412 Constant *CP = ConstantVector::get(CV);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005413 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005414 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman2d489b52008-02-06 22:27:42 +00005415 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005416 PseudoSourceValue::getConstantPool(), 0,
5417 false, Alignment);
Chris Lattner77e271c2006-03-24 07:29:17 +00005418 }
5419
Gabor Greiff304a7a2008-08-28 21:40:38 +00005420 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner21e68c82006-03-20 01:52:29 +00005421 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands13237ac2008-06-06 12:08:01 +00005422 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005423 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5424 std::vector<SDValue> ZeroVec(NumElems, Zero);
5425 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005426 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00005427
5428 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005429 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00005430 // Get the splatted value into the low element of a vector register.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005431 SDValue LowValVec =
Chris Lattner21e68c82006-03-20 01:52:29 +00005432 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5433
5434 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5435 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5436 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5437 SplatMask);
5438 }
5439 }
5440
Evan Cheng1d2e9952006-03-24 01:17:21 +00005441 // If there are only two unique elements, we may be able to turn this into a
5442 // vector shuffle.
5443 if (Values.size() == 2) {
Chris Lattner322c8262008-03-09 00:29:42 +00005444 // Get the two values in deterministic order.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005445 SDValue Val1 = Node->getOperand(1);
5446 SDValue Val2;
5447 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattner322c8262008-03-09 00:29:42 +00005448 if (MI->first != Val1)
5449 Val2 = MI->first;
5450 else
5451 Val2 = (++MI)->first;
5452
5453 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5454 // vector shuffle has the undef vector on the RHS.
5455 if (Val1.getOpcode() == ISD::UNDEF)
5456 std::swap(Val1, Val2);
5457
Evan Cheng1d2e9952006-03-24 01:17:21 +00005458 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands13237ac2008-06-06 12:08:01 +00005459 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5460 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005461 std::vector<SDValue> MaskVec(NumElems);
Chris Lattner322c8262008-03-09 00:29:42 +00005462
5463 // Set elements of the shuffle mask for Val1.
5464 std::vector<unsigned> &Val1Elts = Values[Val1];
5465 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5466 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5467
5468 // Set elements of the shuffle mask for Val2.
5469 std::vector<unsigned> &Val2Elts = Values[Val2];
5470 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5471 if (Val2.getOpcode() != ISD::UNDEF)
5472 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5473 else
5474 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5475
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005476 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005477 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00005478
Chris Lattner322c8262008-03-09 00:29:42 +00005479 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005480 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5481 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattner322c8262008-03-09 00:29:42 +00005482 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5483 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005484 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng1d2e9952006-03-24 01:17:21 +00005485
5486 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattner322c8262008-03-09 00:29:42 +00005487 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005488 }
5489 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005490
5491 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5492 // aligned object on the stack, store each element into it, then load
5493 // the result as a vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00005494 MVT VT = Node->getValueType(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005495 // Create the stack frame object.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005496 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005497
5498 // Emit a store of each element to the stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005499 SmallVector<SDValue, 8> Stores;
Duncan Sands13237ac2008-06-06 12:08:01 +00005500 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005501 // Store (in the right endianness) the elements to memory.
5502 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5503 // Ignore undef elements.
5504 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5505
Chris Lattner8fa445a2006-03-22 01:46:54 +00005506 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005507
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005508 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005509 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5510
Evan Chengdf9ac472006-10-05 23:01:46 +00005511 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005512 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005513 }
5514
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005515 SDValue StoreChain;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005516 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005517 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5518 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005519 else
5520 StoreChain = DAG.getEntryNode();
5521
5522 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005523 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005524}
5525
Chris Lattner4157c412005-04-02 04:00:59 +00005526void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005527 SDValue Op, SDValue Amt,
5528 SDValue &Lo, SDValue &Hi) {
Chris Lattner4157c412005-04-02 04:00:59 +00005529 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005530 SDValue LHSL, LHSH;
Chris Lattner4157c412005-04-02 04:00:59 +00005531 ExpandOp(Op, LHSL, LHSH);
5532
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005533 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands13237ac2008-06-06 12:08:01 +00005534 MVT VT = LHSL.getValueType();
Chris Lattnerbd887772006-08-14 23:53:35 +00005535 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005536 Hi = Lo.getValue(1);
5537}
5538
5539
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005540/// ExpandShift - Try to find a clever way to expand this shift operation out to
5541/// smaller elements. If we can't find a way that is more efficient than a
5542/// libcall on this target, return false. Otherwise, return true with the
5543/// low-parts expanded into Lo and Hi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005544bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5545 SDValue &Lo, SDValue &Hi) {
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005546 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5547 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005548
Duncan Sands13237ac2008-06-06 12:08:01 +00005549 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005550 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands13237ac2008-06-06 12:08:01 +00005551 MVT ShTy = ShAmt.getValueType();
5552 unsigned ShBits = ShTy.getSizeInBits();
5553 unsigned VTBits = Op.getValueType().getSizeInBits();
5554 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanb0674922005-04-06 21:13:14 +00005555
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005556 // Handle the case when Amt is an immediate.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005558 unsigned Cst = CN->getZExtValue();
Nate Begemanb0674922005-04-06 21:13:14 +00005559 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005560 SDValue InL, InH;
Nate Begemanb0674922005-04-06 21:13:14 +00005561 ExpandOp(Op, InL, InH);
5562 switch(Opc) {
5563 case ISD::SHL:
5564 if (Cst > VTBits) {
5565 Lo = DAG.getConstant(0, NVT);
5566 Hi = DAG.getConstant(0, NVT);
5567 } else if (Cst > NVTBits) {
5568 Lo = DAG.getConstant(0, NVT);
5569 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005570 } else if (Cst == NVTBits) {
5571 Lo = DAG.getConstant(0, NVT);
5572 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005573 } else {
5574 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5575 Hi = DAG.getNode(ISD::OR, NVT,
5576 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5577 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5578 }
5579 return true;
5580 case ISD::SRL:
5581 if (Cst > VTBits) {
5582 Lo = DAG.getConstant(0, NVT);
5583 Hi = DAG.getConstant(0, NVT);
5584 } else if (Cst > NVTBits) {
5585 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5586 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005587 } else if (Cst == NVTBits) {
5588 Lo = InH;
5589 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005590 } else {
5591 Lo = DAG.getNode(ISD::OR, NVT,
5592 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5593 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5594 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5595 }
5596 return true;
5597 case ISD::SRA:
5598 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005599 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005600 DAG.getConstant(NVTBits-1, ShTy));
5601 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005602 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005603 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005604 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005605 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005606 } else if (Cst == NVTBits) {
5607 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005608 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005609 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005610 } else {
5611 Lo = DAG.getNode(ISD::OR, NVT,
5612 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5613 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5614 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5615 }
5616 return true;
5617 }
5618 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005619
5620 // Okay, the shift amount isn't constant. However, if we can tell that it is
5621 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005622 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5623 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005624 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005625
Dan Gohmanf3057a92008-02-22 01:12:31 +00005626 // If we know that if any of the high bits of the shift amount are one, then
5627 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005628 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005629 // Mask out the high bit, which we know is set.
5630 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005631 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005632
5633 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005634 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005635 ExpandOp(Op, InL, InH);
5636 switch(Opc) {
5637 case ISD::SHL:
5638 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5639 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5640 return true;
5641 case ISD::SRL:
5642 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5643 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5644 return true;
5645 case ISD::SRA:
5646 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5647 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5648 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5649 return true;
5650 }
5651 }
5652
Dan Gohmanf3057a92008-02-22 01:12:31 +00005653 // If we know that the high bits of the shift amount are all zero, then we can
5654 // do this as a couple of simple shifts.
5655 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005656 // Compute 32-amt.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005657 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner875ea0c2006-09-20 03:38:48 +00005658 DAG.getConstant(NVTBits, Amt.getValueType()),
5659 Amt);
5660
5661 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005662 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005663 ExpandOp(Op, InL, InH);
5664 switch(Opc) {
5665 case ISD::SHL:
5666 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5667 Hi = DAG.getNode(ISD::OR, NVT,
5668 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5669 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5670 return true;
5671 case ISD::SRL:
5672 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5673 Lo = DAG.getNode(ISD::OR, NVT,
5674 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5675 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5676 return true;
5677 case ISD::SRA:
5678 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5679 Lo = DAG.getNode(ISD::OR, NVT,
5680 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5681 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5682 return true;
5683 }
5684 }
5685
Nate Begemanb0674922005-04-06 21:13:14 +00005686 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005687}
Chris Lattneraac464e2005-01-21 06:05:23 +00005688
Chris Lattner4add7e32005-01-23 04:42:50 +00005689
Chris Lattneraac464e2005-01-21 06:05:23 +00005690// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5691// does not fit into a register, return the lo part and set the hi part to the
5692// by-reg argument. If it does fit into a single register, return the result
5693// and leave the Hi part unset.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005694SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5695 bool isSigned, SDValue &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005696 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5697 // The input chain to this libcall is the entry node of the function.
5698 // Legalizing the call will automatically add the previous call to the
5699 // dependence.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005700 SDValue InChain = DAG.getEntryNode();
Chris Lattner462505f2006-02-13 09:18:02 +00005701
Chris Lattneraac464e2005-01-21 06:05:23 +00005702 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005703 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005705 MVT ArgVT = Node->getOperand(i).getValueType();
5706 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencere63b6512006-12-31 05:55:36 +00005707 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005708 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005709 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005710 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005711 }
Bill Wendling24c79f22008-09-16 21:48:12 +00005712 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang58c37942008-10-30 08:01:45 +00005713 TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005714
Chris Lattner06bbeb62005-05-11 19:02:11 +00005715 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands13237ac2008-06-06 12:08:01 +00005716 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005717 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen0e32a2c2008-09-26 19:31:26 +00005718 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5719 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005720
Chris Lattner462505f2006-02-13 09:18:02 +00005721 // Legalize the call sequence, starting with the chain. This will advance
5722 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5723 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5724 LegalizeOp(CallInfo.second);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005725 SDValue Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005726 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005727 default: assert(0 && "Unknown thing");
5728 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005729 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005730 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005731 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005732 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005733 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005734 }
Chris Lattner63022662005-09-02 20:26:58 +00005735 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005736}
5737
Dan Gohman550c9af2008-08-14 20:04:46 +00005738/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5739///
5740SDValue SelectionDAGLegalize::
5741LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5742 bool isCustom = false;
5743 SDValue Tmp1;
5744 switch (getTypeAction(Op.getValueType())) {
5745 case Legal:
5746 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5747 Op.getValueType())) {
5748 default: assert(0 && "Unknown operation action!");
5749 case TargetLowering::Custom:
5750 isCustom = true;
5751 // FALLTHROUGH
5752 case TargetLowering::Legal:
5753 Tmp1 = LegalizeOp(Op);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005754 if (Result.getNode())
Dan Gohman550c9af2008-08-14 20:04:46 +00005755 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5756 else
5757 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5758 DestTy, Tmp1);
5759 if (isCustom) {
5760 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005761 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman550c9af2008-08-14 20:04:46 +00005762 }
5763 break;
5764 case TargetLowering::Expand:
5765 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5766 break;
5767 case TargetLowering::Promote:
5768 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5769 break;
5770 }
5771 break;
5772 case Expand:
5773 Result = ExpandIntToFP(isSigned, DestTy, Op);
5774 break;
5775 case Promote:
5776 Tmp1 = PromoteOp(Op);
5777 if (isSigned) {
5778 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5779 Tmp1, DAG.getValueType(Op.getValueType()));
5780 } else {
5781 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5782 Op.getValueType());
5783 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00005784 if (Result.getNode())
Dan Gohman550c9af2008-08-14 20:04:46 +00005785 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5786 else
5787 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5788 DestTy, Tmp1);
5789 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5790 break;
5791 }
5792 return Result;
5793}
Chris Lattner4add7e32005-01-23 04:42:50 +00005794
Evan Chengbf535fc2007-04-27 07:33:31 +00005795/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5796///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005797SDValue SelectionDAGLegalize::
5798ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005799 MVT SourceVT = Source.getValueType();
Dan Gohmand6819da2008-03-11 01:59:03 +00005800 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattneraac464e2005-01-21 06:05:23 +00005801
Dan Gohman550c9af2008-08-14 20:04:46 +00005802 // Expand unsupported int-to-fp vector casts by unrolling them.
5803 if (DestTy.isVector()) {
5804 if (!ExpandSource)
5805 return LegalizeOp(UnrollVectorOp(Source));
5806 MVT DestEltTy = DestTy.getVectorElementType();
5807 if (DestTy.getVectorNumElements() == 1) {
5808 SDValue Scalar = ScalarizeVectorOp(Source);
5809 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5810 DestEltTy, Scalar);
5811 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5812 }
5813 SDValue Lo, Hi;
5814 SplitVectorOp(Source, Lo, Hi);
5815 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5816 DestTy.getVectorNumElements() / 2);
5817 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5818 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengda9b7522008-10-13 18:46:18 +00005819 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5820 HiResult));
Dan Gohman550c9af2008-08-14 20:04:46 +00005821 }
5822
Evan Cheng0bd72c52008-04-01 02:18:22 +00005823 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5824 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmanda7897c2008-03-05 02:07:31 +00005825 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005826 // incoming integer is set. To handle this, we dynamically test to see if
5827 // it is set, and, if so, add a fudge factor.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005828 SDValue Hi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005829 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005830 SDValue Lo;
Dan Gohmand6819da2008-03-11 01:59:03 +00005831 ExpandOp(Source, Lo, Hi);
5832 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5833 } else {
5834 // The comparison for the sign bit will use the entire operand.
5835 Hi = Source;
5836 }
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005837
Dale Johannesendb6b9562008-11-04 20:52:49 +00005838 // Check to see if the target has a custom way to lower this. If so, use
5839 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen28929582008-10-21 20:50:01 +00005840 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5841 default: assert(0 && "This action not implemented for this operation!");
5842 case TargetLowering::Legal:
5843 case TargetLowering::Expand:
5844 break; // This case is handled below.
5845 case TargetLowering::Custom: {
5846 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5847 Source), DAG);
5848 if (NV.getNode())
5849 return LegalizeOp(NV);
5850 break; // The target decided this was legal after all
5851 }
5852 }
5853
Chris Lattner2a4f7312005-05-13 04:45:13 +00005854 // If this is unsigned, and not supported, first perform the conversion to
5855 // signed, then adjust the result if the sign bit is set.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005856 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner2a4f7312005-05-13 04:45:13 +00005857
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005858 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005859 DAG.getConstant(0, Hi.getValueType()),
5860 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005861 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5862 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005863 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005864 uint64_t FF = 0x5f800000ULL;
5865 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmanda7897c2008-03-05 02:07:31 +00005866 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005868 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005869 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005870 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmane2947e12008-09-22 22:40:08 +00005871 Alignment = std::min(Alignment, 4u);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005872 SDValue FudgeInReg;
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005873 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005874 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005875 PseudoSourceValue::getConstantPool(), 0,
5876 false, Alignment);
Duncan Sands11dd4242008-06-08 20:54:56 +00005877 else if (DestTy.bitsGT(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005878 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005879 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005880 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005881 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00005882 MVT::f32, false, Alignment);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005883 else
5884 assert(0 && "Unexpected conversion");
5885
Duncan Sands13237ac2008-06-06 12:08:01 +00005886 MVT SCVT = SignedConv.getValueType();
Evan Chengbf535fc2007-04-27 07:33:31 +00005887 if (SCVT != DestTy) {
5888 // Destination type needs to be expanded as well. The FADD now we are
5889 // constructing will be expanded into a libcall.
Duncan Sands13237ac2008-06-06 12:08:01 +00005890 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5891 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand9d874b2008-03-05 01:08:17 +00005892 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Chengbf535fc2007-04-27 07:33:31 +00005893 SignedConv, SignedConv.getValue(1));
5894 }
5895 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5896 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005897 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005898 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005899
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005900 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand9d874b2008-03-05 01:08:17 +00005901 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005902 default: assert(0 && "This action not implemented for this operation!");
5903 case TargetLowering::Legal:
5904 case TargetLowering::Expand:
5905 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005906 case TargetLowering::Custom: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005907 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005908 Source), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005909 if (NV.getNode())
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005910 return LegalizeOp(NV);
5911 break; // The target decided this was legal after all
5912 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005913 }
5914
Chris Lattner153587e2005-05-12 07:00:44 +00005915 // Expand the source, then glue it back together for the call. We must expand
5916 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohmand6819da2008-03-11 01:59:03 +00005917 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005918 SDValue SrcLo, SrcHi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005919 ExpandOp(Source, SrcLo, SrcHi);
5920 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5921 }
Chris Lattner153587e2005-05-12 07:00:44 +00005922
Duncan Sands77a3d052008-07-17 02:36:29 +00005923 RTLIB::Libcall LC = isSigned ?
5924 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5925 RTLIB::getUINTTOFP(SourceVT, DestTy);
5926 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5927
Chris Lattner462505f2006-02-13 09:18:02 +00005928 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005929 SDValue HiPart;
Gabor Greiff304a7a2008-08-28 21:40:38 +00005930 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5931 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanf4300952008-03-10 23:03:31 +00005932 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5933 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005934}
Misha Brukman835702a2005-04-21 22:36:52 +00005935
Chris Lattner689bdcc2006-01-28 08:25:58 +00005936/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5937/// INT_TO_FP operation of the specified operand when the target requests that
5938/// we expand it. At this point, we know that the result and operand types are
5939/// legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005940SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5941 SDValue Op0,
5942 MVT DestVT) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005943 if (Op0.getValueType() == MVT::i32) {
5944 // simple 32-bit [signed|unsigned] integer to float/double expansion
5945
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005946 // Get the stack frame index of a 8 byte buffer.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005947 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005948
Chris Lattner689bdcc2006-01-28 08:25:58 +00005949 // word offset constant for Hi/Lo address computation
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005950 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner689bdcc2006-01-28 08:25:58 +00005951 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005952 SDValue Hi = StackSlot;
5953 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005954 if (TLI.isLittleEndian())
5955 std::swap(Hi, Lo);
5956
Chris Lattner689bdcc2006-01-28 08:25:58 +00005957 // if signed map to unsigned space
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005958 SDValue Op0Mapped;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005959 if (isSigned) {
5960 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005961 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005962 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5963 } else {
5964 Op0Mapped = Op0;
5965 }
5966 // store the lo of the constructed double - based on integer input
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005967 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005968 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005969 // initial hi portion of constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005970 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005971 // store the hi of the constructed double - biased exponent
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005972 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005973 // load the constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005974 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005975 // FP constant to bias correct the final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005976 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner689bdcc2006-01-28 08:25:58 +00005977 BitsToDouble(0x4330000080000000ULL)
5978 : BitsToDouble(0x4330000000000000ULL),
5979 MVT::f64);
5980 // subtract the bias
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005981 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005982 // final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005983 SDValue Result;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005984 // handle final rounding
5985 if (DestVT == MVT::f64) {
5986 // do nothing
5987 Result = Sub;
Duncan Sands11dd4242008-06-08 20:54:56 +00005988 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00005989 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5990 DAG.getIntPtrConstant(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00005991 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen7f724e92007-09-16 16:51:49 +00005992 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005993 }
5994 return Result;
5995 }
5996 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005997 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005998
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005999 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner689bdcc2006-01-28 08:25:58 +00006000 DAG.getConstant(0, Op0.getValueType()),
6001 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006002 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6003 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner689bdcc2006-01-28 08:25:58 +00006004 SignSet, Four, Zero);
6005
6006 // If the sign bit of the integer is set, the large number will be treated
6007 // as a negative number. To counteract this, the dynamic code adds an
6008 // offset depending on the data type.
6009 uint64_t FF;
Duncan Sands13237ac2008-06-06 12:08:01 +00006010 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006011 default: assert(0 && "Unsupported integer type!");
6012 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6013 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6014 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6015 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6016 }
6017 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00006018 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006019
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006020 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006021 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006022 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmane2947e12008-09-22 22:40:08 +00006023 Alignment = std::min(Alignment, 4u);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006024 SDValue FudgeInReg;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006025 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00006026 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006027 PseudoSourceValue::getConstantPool(), 0,
6028 false, Alignment);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006029 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00006030 FudgeInReg =
6031 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6032 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006033 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman64d6c6f2008-09-16 22:05:41 +00006034 MVT::f32, false, Alignment));
Chris Lattner689bdcc2006-01-28 08:25:58 +00006035 }
6036
6037 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6038}
6039
6040/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6041/// *INT_TO_FP operation of the specified operand when the target requests that
6042/// we promote it. At this point, we know that the result and operand types are
6043/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6044/// operation that takes a larger input.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006045SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6046 MVT DestVT,
6047 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006048 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00006049 MVT NewInTy = LegalOp.getValueType();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006050
6051 unsigned OpToUse = 0;
6052
6053 // Scan for the appropriate larger type to use.
6054 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006055 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6056 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00006057
6058 // If the target supports SINT_TO_FP of this type, use it.
6059 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6060 default: break;
6061 case TargetLowering::Legal:
6062 if (!TLI.isTypeLegal(NewInTy))
6063 break; // Can't use this datatype.
6064 // FALL THROUGH.
6065 case TargetLowering::Custom:
6066 OpToUse = ISD::SINT_TO_FP;
6067 break;
6068 }
6069 if (OpToUse) break;
6070 if (isSigned) continue;
6071
6072 // If the target supports UINT_TO_FP of this type, use it.
6073 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6074 default: break;
6075 case TargetLowering::Legal:
6076 if (!TLI.isTypeLegal(NewInTy))
6077 break; // Can't use this datatype.
6078 // FALL THROUGH.
6079 case TargetLowering::Custom:
6080 OpToUse = ISD::UINT_TO_FP;
6081 break;
6082 }
6083 if (OpToUse) break;
6084
6085 // Otherwise, try a larger type.
6086 }
6087
6088 // Okay, we found the operation and type to use. Zero extend our input to the
6089 // desired type then run the operation on it.
6090 return DAG.getNode(OpToUse, DestVT,
6091 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6092 NewInTy, LegalOp));
6093}
6094
6095/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6096/// FP_TO_*INT operation of the specified operand when the target requests that
6097/// we promote it. At this point, we know that the result and operand types are
6098/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6099/// operation that returns a larger result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006100SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6101 MVT DestVT,
6102 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006103 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00006104 MVT NewOutTy = DestVT;
Chris Lattner689bdcc2006-01-28 08:25:58 +00006105
6106 unsigned OpToUse = 0;
6107
6108 // Scan for the appropriate larger type to use.
6109 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006110 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6111 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00006112
6113 // If the target supports FP_TO_SINT returning this type, use it.
6114 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6115 default: break;
6116 case TargetLowering::Legal:
6117 if (!TLI.isTypeLegal(NewOutTy))
6118 break; // Can't use this datatype.
6119 // FALL THROUGH.
6120 case TargetLowering::Custom:
6121 OpToUse = ISD::FP_TO_SINT;
6122 break;
6123 }
6124 if (OpToUse) break;
6125
6126 // If the target supports FP_TO_UINT of this type, use it.
6127 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6128 default: break;
6129 case TargetLowering::Legal:
6130 if (!TLI.isTypeLegal(NewOutTy))
6131 break; // Can't use this datatype.
6132 // FALL THROUGH.
6133 case TargetLowering::Custom:
6134 OpToUse = ISD::FP_TO_UINT;
6135 break;
6136 }
6137 if (OpToUse) break;
6138
6139 // Otherwise, try a larger type.
6140 }
6141
Chris Lattnerf81d5882007-11-24 07:07:01 +00006142
6143 // Okay, we found the operation and type to use.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006144 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands93e180342008-07-04 11:47:58 +00006145
Chris Lattnerf81d5882007-11-24 07:07:01 +00006146 // If the operation produces an invalid type, it must be custom lowered. Use
6147 // the target lowering hooks to expand it. Just keep the low part of the
6148 // expanded operation, we know that we're truncating anyway.
6149 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006150 SmallVector<SDValue, 2> Results;
6151 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6152 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6153 Operation = Results[0];
Chris Lattnerf81d5882007-11-24 07:07:01 +00006154 }
Duncan Sands93e180342008-07-04 11:47:58 +00006155
Chris Lattnerf81d5882007-11-24 07:07:01 +00006156 // Truncate the result of the extended FP_TO_*INT operation to the desired
6157 // size.
6158 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006159}
6160
6161/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6162///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006163SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006164 MVT VT = Op.getValueType();
6165 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006166 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands13237ac2008-06-06 12:08:01 +00006167 switch (VT.getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006168 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6169 case MVT::i16:
6170 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6171 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6172 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6173 case MVT::i32:
6174 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6175 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6176 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6177 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6178 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6179 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6180 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6181 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6182 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6183 case MVT::i64:
6184 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6185 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6186 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6187 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6188 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6189 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6190 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6191 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6192 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6193 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6194 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6195 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6196 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6197 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6198 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6199 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6200 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6201 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6202 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6203 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6204 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6205 }
6206}
6207
6208/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6209///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006210SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00006211 switch (Opc) {
6212 default: assert(0 && "Cannot expand this yet!");
6213 case ISD::CTPOP: {
6214 static const uint64_t mask[6] = {
6215 0x5555555555555555ULL, 0x3333333333333333ULL,
6216 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6217 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6218 };
Duncan Sands13237ac2008-06-06 12:08:01 +00006219 MVT VT = Op.getValueType();
6220 MVT ShVT = TLI.getShiftAmountTy();
6221 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006222 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6223 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006224 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6225 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006226 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6227 DAG.getNode(ISD::AND, VT,
6228 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6229 }
6230 return Op;
6231 }
6232 case ISD::CTLZ: {
6233 // for now, we do this:
6234 // x = x | (x >> 1);
6235 // x = x | (x >> 2);
6236 // ...
6237 // x = x | (x >>16);
6238 // x = x | (x >>32); // for 64-bit input
6239 // return popcount(~x);
6240 //
6241 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00006242 MVT VT = Op.getValueType();
6243 MVT ShVT = TLI.getShiftAmountTy();
6244 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00006245 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006246 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00006247 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6248 }
6249 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6250 return DAG.getNode(ISD::CTPOP, VT, Op);
6251 }
6252 case ISD::CTTZ: {
6253 // for now, we use: { return popcount(~x & (x - 1)); }
6254 // unless the target has ctlz but not ctpop, in which case we use:
6255 // { return 32 - nlz(~x & (x-1)); }
6256 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00006257 MVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006258 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6259 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00006260 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6261 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6262 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6263 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6264 TLI.isOperationLegal(ISD::CTLZ, VT))
6265 return DAG.getNode(ISD::SUB, VT,
Duncan Sands13237ac2008-06-06 12:08:01 +00006266 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00006267 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6268 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6269 }
6270 }
6271}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006273/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerdc750592005-01-07 07:47:09 +00006274/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman3a293e72008-10-01 15:07:49 +00006275/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattnerdc750592005-01-07 07:47:09 +00006276/// ExpandedNodes map is filled in for any results that are expanded, and the
6277/// Lo/Hi values are returned.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006278void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands13237ac2008-06-06 12:08:01 +00006279 MVT VT = Op.getValueType();
6280 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006281 SDNode *Node = Op.getNode();
Chris Lattnerdc750592005-01-07 07:47:09 +00006282 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00006283 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands13237ac2008-06-06 12:08:01 +00006284 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattnerdc750592005-01-07 07:47:09 +00006285
Chris Lattner1a570f12005-09-02 20:32:45 +00006286 // See if we already expanded it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006287 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00006288 = ExpandedNodes.find(Op);
6289 if (I != ExpandedNodes.end()) {
6290 Lo = I->second.first;
6291 Hi = I->second.second;
6292 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00006293 }
6294
Chris Lattnerdc750592005-01-07 07:47:09 +00006295 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00006296 case ISD::CopyFromReg:
6297 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00006298 case ISD::FP_ROUND_INREG:
6299 if (VT == MVT::ppcf128 &&
6300 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6301 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006302 SDValue SrcLo, SrcHi, Src;
Dale Johannesen6472eb62007-10-11 23:32:15 +00006303 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6304 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006305 SDValue Result = TLI.LowerOperation(
Dale Johannesen6472eb62007-10-11 23:32:15 +00006306 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006307 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6308 Lo = Result.getNode()->getOperand(0);
6309 Hi = Result.getNode()->getOperand(1);
Dale Johannesen666323e2007-10-10 01:01:31 +00006310 break;
6311 }
6312 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00006313 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006314#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006315 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006316#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00006317 assert(0 && "Do not know how to expand this operator!");
6318 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00006319 case ISD::EXTRACT_ELEMENT:
6320 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00006321 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman66272a52008-02-27 01:52:30 +00006322 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00006323 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00006324 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesenb066c1f2007-10-31 00:32:36 +00006325 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6326 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6327 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00006328 case ISD::UNDEF:
6329 Lo = DAG.getNode(ISD::UNDEF, NVT);
6330 Hi = DAG.getNode(ISD::UNDEF, NVT);
6331 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006332 case ISD::Constant: {
Duncan Sands13237ac2008-06-06 12:08:01 +00006333 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf2bbfa32008-03-03 22:20:46 +00006334 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6335 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6336 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattnerdc750592005-01-07 07:47:09 +00006337 break;
6338 }
Evan Cheng47833a12006-12-12 21:32:44 +00006339 case ISD::ConstantFP: {
6340 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00006341 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00006342 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen007aa372007-10-11 18:07:22 +00006343 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6344 MVT::f64);
6345 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6346 MVT::f64);
6347 break;
6348 }
Evan Cheng3766fc602006-12-12 22:19:28 +00006349 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00006350 if (getTypeAction(Lo.getValueType()) == Expand)
6351 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00006352 break;
6353 }
Chris Lattner32e08b72005-03-28 22:03:13 +00006354 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006355 // Return the operands.
6356 Lo = Node->getOperand(0);
6357 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00006358 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006359
6360 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00006361 if (Node->getNumValues() == 1) {
6362 ExpandOp(Op.getOperand(0), Lo, Hi);
6363 break;
6364 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00006365 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greifabfdf922008-08-26 22:36:50 +00006366 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00006367 Op.getValue(1).getValueType() == MVT::Other &&
6368 "unhandled MERGE_VALUES");
6369 ExpandOp(Op.getOperand(0), Lo, Hi);
6370 // Remember that we legalized the chain.
6371 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6372 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006373
6374 case ISD::SIGN_EXTEND_INREG:
6375 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00006376 // sext_inreg the low part if needed.
6377 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6378
6379 // The high part gets the sign extension from the lo-part. This handles
6380 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006381 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands13237ac2008-06-06 12:08:01 +00006382 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006383 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00006384 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00006385
Nate Begeman2fba8a32006-01-14 03:14:10 +00006386 case ISD::BSWAP: {
6387 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006388 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begeman2fba8a32006-01-14 03:14:10 +00006389 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6390 Lo = TempLo;
6391 break;
6392 }
6393
Chris Lattner55e9cde2005-05-11 04:51:16 +00006394 case ISD::CTPOP:
6395 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00006396 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6397 DAG.getNode(ISD::CTPOP, NVT, Lo),
6398 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00006399 Hi = DAG.getConstant(0, NVT);
6400 break;
6401
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006402 case ISD::CTLZ: {
6403 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00006404 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006405 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6406 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6407 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00006408 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006409 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006410 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6411
6412 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6413 Hi = DAG.getConstant(0, NVT);
6414 break;
6415 }
6416
6417 case ISD::CTTZ: {
6418 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00006419 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006420 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6421 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6422 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00006423 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006424 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00006425 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6426
6427 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6428 Hi = DAG.getConstant(0, NVT);
6429 break;
6430 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00006431
Nate Begemane74795c2006-01-25 18:21:52 +00006432 case ISD::VAARG: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006433 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6434 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00006435 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6436 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6437
6438 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006439 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00006440 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00006441 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00006442 std::swap(Lo, Hi);
6443 break;
6444 }
6445
Chris Lattnerdc750592005-01-07 07:47:09 +00006446 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00006447 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006448 SDValue Ch = LD->getChain(); // Legalize the chain.
6449 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Chenge71fe34d2006-10-09 20:57:25 +00006450 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman550c9af2008-08-14 20:04:46 +00006451 const Value *SV = LD->getSrcValue();
Dan Gohman2af30632007-07-09 22:18:38 +00006452 int SVOffset = LD->getSrcValueOffset();
6453 unsigned Alignment = LD->getAlignment();
6454 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00006455
Evan Chenge71fe34d2006-10-09 20:57:25 +00006456 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman550c9af2008-08-14 20:04:46 +00006457 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohman2af30632007-07-09 22:18:38 +00006458 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00006459 if (VT == MVT::f32 || VT == MVT::f64) {
6460 // f32->i32 or f64->i64 one to one expansion.
6461 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006462 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00006463 // Recursively expand the new load.
6464 if (getTypeAction(NVT) == Expand)
6465 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00006466 break;
6467 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00006468
Evan Chenge71fe34d2006-10-09 20:57:25 +00006469 // Increment the pointer to the other half.
Duncan Sands13237ac2008-06-06 12:08:01 +00006470 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Chenge71fe34d2006-10-09 20:57:25 +00006471 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006472 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006473 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006474 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman550c9af2008-08-14 20:04:46 +00006475 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohman2af30632007-07-09 22:18:38 +00006476 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00006477
Evan Chenge71fe34d2006-10-09 20:57:25 +00006478 // Build a factor node to remember that this load is independent of the
6479 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006480 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Chenge71fe34d2006-10-09 20:57:25 +00006481 Hi.getValue(1));
6482
6483 // Remember that we legalized the chain.
6484 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00006485 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00006486 std::swap(Lo, Hi);
6487 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00006488 MVT EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00006489
Dale Johannesen6802d0c2007-10-19 20:29:00 +00006490 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6491 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00006492 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman550c9af2008-08-14 20:04:46 +00006493 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006494 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00006495 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006496 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Chenge370e0e2006-12-13 03:19:57 +00006497 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6498 break;
6499 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00006500
6501 if (EVT == NVT)
Dan Gohman550c9af2008-08-14 20:04:46 +00006502 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006503 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006504 else
Dan Gohman550c9af2008-08-14 20:04:46 +00006505 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohman2af30632007-07-09 22:18:38 +00006506 SVOffset, EVT, isVolatile,
6507 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006508
6509 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006510 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Chenge71fe34d2006-10-09 20:57:25 +00006511
6512 if (ExtType == ISD::SEXTLOAD) {
6513 // The high part is obtained by SRA'ing all but one of the bits of the
6514 // lo part.
Duncan Sands13237ac2008-06-06 12:08:01 +00006515 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Chenge71fe34d2006-10-09 20:57:25 +00006516 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6517 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6518 } else if (ExtType == ISD::ZEXTLOAD) {
6519 // The high part is just a zero.
6520 Hi = DAG.getConstant(0, NVT);
6521 } else /* if (ExtType == ISD::EXTLOAD) */ {
6522 // The high part is undefined.
6523 Hi = DAG.getNode(ISD::UNDEF, NVT);
6524 }
6525 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006526 break;
6527 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006528 case ISD::AND:
6529 case ISD::OR:
6530 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006531 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00006532 ExpandOp(Node->getOperand(0), LL, LH);
6533 ExpandOp(Node->getOperand(1), RL, RH);
6534 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6535 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6536 break;
6537 }
6538 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006539 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00006540 ExpandOp(Node->getOperand(1), LL, LH);
6541 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00006542 if (getTypeAction(NVT) == Expand)
6543 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006544 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00006545 if (VT != MVT::f32)
6546 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00006547 break;
6548 }
Nate Begemane5b86d72005-08-10 20:51:12 +00006549 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006550 SDValue TL, TH, FL, FH;
Nate Begemane5b86d72005-08-10 20:51:12 +00006551 ExpandOp(Node->getOperand(2), TL, TH);
6552 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00006553 if (getTypeAction(NVT) == Expand)
6554 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00006555 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6556 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00006557 if (VT != MVT::f32)
6558 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6559 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00006560 break;
6561 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006562 case ISD::ANY_EXTEND:
6563 // The low part is any extension of the input (which degenerates to a copy).
6564 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6565 // The high part is undefined.
6566 Hi = DAG.getNode(ISD::UNDEF, NVT);
6567 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006568 case ISD::SIGN_EXTEND: {
6569 // The low part is just a sign extension of the input (which degenerates to
6570 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006571 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006572
Chris Lattnerdc750592005-01-07 07:47:09 +00006573 // The high part is obtained by SRA'ing all but one of the bits of the lo
6574 // part.
Duncan Sands13237ac2008-06-06 12:08:01 +00006575 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006576 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6577 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00006578 break;
6579 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006580 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00006581 // The low part is just a zero extension of the input (which degenerates to
6582 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006583 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006584
Chris Lattnerdc750592005-01-07 07:47:09 +00006585 // The high part is just a zero.
6586 Hi = DAG.getConstant(0, NVT);
6587 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006588
Chris Lattner59b27fa372007-02-13 23:55:16 +00006589 case ISD::TRUNCATE: {
6590 // The input value must be larger than this value. Expand *it*.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006591 SDValue NewLo;
Chris Lattner59b27fa372007-02-13 23:55:16 +00006592 ExpandOp(Node->getOperand(0), NewLo, Hi);
6593
6594 // The low part is now either the right size, or it is closer. If not the
6595 // right size, make an illegal truncate so we recursively expand it.
6596 if (NewLo.getValueType() != Node->getValueType(0))
6597 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6598 ExpandOp(NewLo, Lo, Hi);
6599 break;
6600 }
6601
Chris Lattner36e663d2005-12-23 00:16:34 +00006602 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006603 SDValue Tmp;
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006604 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6605 // If the target wants to, allow it to lower this itself.
6606 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6607 case Expand: assert(0 && "cannot expand FP!");
6608 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6609 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6610 }
6611 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6612 }
6613
Evan Cheng4eee7242006-12-09 02:42:38 +00006614 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006615 if (VT == MVT::f32 || VT == MVT::f64) {
6616 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006617 if (getTypeAction(NVT) == Expand)
6618 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006619 break;
6620 }
6621
6622 // If source operand will be expanded to the same type as VT, i.e.
6623 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands13237ac2008-06-06 12:08:01 +00006624 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng0076ca02006-12-12 19:53:13 +00006625 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6626 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006627 break;
6628 }
6629
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006630 // Turn this into a load/store pair by default.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006631 if (Tmp.getNode() == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006632 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006633
Chris Lattner36e663d2005-12-23 00:16:34 +00006634 ExpandOp(Tmp, Lo, Hi);
6635 break;
6636 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006637
Chris Lattnerf81d5882007-11-24 07:07:01 +00006638 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006639 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6640 TargetLowering::Custom &&
6641 "Must custom expand ReadCycleCounter");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006642 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006643 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerf81d5882007-11-24 07:07:01 +00006644 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006645 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006646 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006647 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006648 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006649
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006650 case ISD::ATOMIC_CMP_SWAP_64: {
6651 // This operation does not need a loop.
6652 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6653 assert(Tmp.getNode() && "Node must be custom expanded!");
6654 ExpandOp(Tmp.getValue(0), Lo, Hi);
6655 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6656 LegalizeOp(Tmp.getValue(1)));
6657 break;
6658 }
6659
Dale Johannesen867d5492008-10-02 18:53:47 +00006660 case ISD::ATOMIC_LOAD_ADD_64:
6661 case ISD::ATOMIC_LOAD_SUB_64:
6662 case ISD::ATOMIC_LOAD_AND_64:
6663 case ISD::ATOMIC_LOAD_OR_64:
6664 case ISD::ATOMIC_LOAD_XOR_64:
6665 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006666 case ISD::ATOMIC_SWAP_64: {
6667 // These operations require a loop to be generated. We can't do that yet,
6668 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen867d5492008-10-02 18:53:47 +00006669 SDValue In2Lo, In2Hi, In2;
6670 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6671 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen5d60c1e2008-10-03 19:41:08 +00006672 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6673 SDValue Replace =
6674 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6675 Anode->getSrcValue(), Anode->getAlignment());
6676 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen867d5492008-10-02 18:53:47 +00006677 ExpandOp(Result.getValue(0), Lo, Hi);
6678 // Remember that we legalized the chain.
6679 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth357061a2008-03-05 01:15:49 +00006680 break;
6681 }
6682
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006683 // These operators cannot be expanded directly, emit them as calls to
6684 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006685 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006686 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006687 SDValue Op;
Chris Lattner941d84a2005-07-30 01:40:57 +00006688 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6689 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006690 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6691 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006692 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006693
Chris Lattner941d84a2005-07-30 01:40:57 +00006694 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6695
Chris Lattnerfe68d752005-07-29 00:33:32 +00006696 // Now that the custom expander is done, expand the result, which is still
6697 // VT.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006698 if (Op.getNode()) {
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006699 ExpandOp(Op, Lo, Hi);
6700 break;
6701 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006702 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006703
Duncan Sands77a3d052008-07-17 02:36:29 +00006704 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6705 VT);
6706 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6707 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006708 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006709 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006710
Evan Cheng31cbddf2007-01-12 02:11:51 +00006711 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006712 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006713 SDValue Op;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006714 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6715 case Expand: assert(0 && "cannot expand FP!");
6716 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6717 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6718 }
6719
6720 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6721
6722 // Now that the custom expander is done, expand the result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006723 if (Op.getNode()) {
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006724 ExpandOp(Op, Lo, Hi);
6725 break;
6726 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006727 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006728
Duncan Sands77a3d052008-07-17 02:36:29 +00006729 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6730 VT);
6731 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6732 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006733 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006734 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006735
Evan Cheng870e4f82006-01-09 18:31:59 +00006736 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006737 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006738 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006739 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006740 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006741 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006742 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006743 // Now that the custom expander is done, expand the result, which is
6744 // still VT.
6745 ExpandOp(Op, Lo, Hi);
6746 break;
6747 }
6748 }
6749
Chris Lattner72b503b2006-09-13 03:50:39 +00006750 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6751 // this X << 1 as X+X.
6752 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00006753 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner72b503b2006-09-13 03:50:39 +00006754 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006755 SDValue LoOps[2], HiOps[3];
Chris Lattner72b503b2006-09-13 03:50:39 +00006756 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6757 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6758 LoOps[1] = LoOps[0];
6759 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6760
6761 HiOps[1] = HiOps[0];
6762 HiOps[2] = Lo.getValue(1);
6763 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6764 break;
6765 }
6766 }
6767
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006768 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006769 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006770 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006771
6772 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006773 TargetLowering::LegalizeAction Action =
6774 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6775 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6776 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006777 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006778 break;
6779 }
6780
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006781 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006782 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006783 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006784 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006785
Evan Cheng870e4f82006-01-09 18:31:59 +00006786 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006787 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006788 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006789 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006790 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006791 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006792 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006793 // Now that the custom expander is done, expand the result, which is
6794 // still VT.
6795 ExpandOp(Op, Lo, Hi);
6796 break;
6797 }
6798 }
6799
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006800 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006801 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006802 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006803
6804 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006805 TargetLowering::LegalizeAction Action =
6806 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6807 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6808 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006809 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006810 break;
6811 }
6812
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006813 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006814 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006815 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006816 }
6817
6818 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006819 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006820 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006821 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006822 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006823 Op = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006824 if (Op.getNode()) {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006825 // Now that the custom expander is done, expand the result, which is
6826 // still VT.
6827 ExpandOp(Op, Lo, Hi);
6828 break;
6829 }
6830 }
6831
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006832 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006833 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006834 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006835
6836 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006837 TargetLowering::LegalizeAction Action =
6838 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6839 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6840 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006841 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006842 break;
6843 }
6844
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006845 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006846 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006847 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006848 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006849
Misha Brukman835702a2005-04-21 22:36:52 +00006850 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006851 case ISD::SUB: {
6852 // If the target wants to custom expand this, let them.
6853 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6854 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006855 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006856 if (Result.getNode()) {
Duncan Sands5fb92e52008-06-22 09:42:16 +00006857 ExpandOp(Result, Lo, Hi);
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006858 break;
6859 }
6860 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006861 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006862 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006863 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6864 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006865 SDValue LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006866 LoOps[0] = LHSL;
6867 LoOps[1] = RHSL;
6868 HiOps[0] = LHSH;
6869 HiOps[1] = RHSH;
Andrew Lenharth21dca9c2008-10-07 18:27:23 +00006870
Andrew Lenharth6606f172008-10-07 17:03:15 +00006871 //cascaded check to see if any smaller size has a a carry flag.
6872 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6873 bool hasCarry = false;
Andrew Lenharth21dca9c2008-10-07 18:27:23 +00006874 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6875 MVT AVT = MVT::getIntegerVT(BitSize);
6876 if (TLI.isOperationLegal(OpV, AVT)) {
6877 hasCarry = true;
6878 break;
6879 }
6880 }
6881
Andrew Lenharth6606f172008-10-07 17:03:15 +00006882 if(hasCarry) {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006883 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006884 if (Node->getOpcode() == ISD::ADD) {
6885 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6886 HiOps[2] = Lo.getValue(1);
6887 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6888 } else {
6889 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6890 HiOps[2] = Lo.getValue(1);
6891 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6892 }
6893 break;
Nate Begeman5965bd12006-02-17 05:43:56 +00006894 } else {
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006895 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006896 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6897 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Andrew Lenharth6606f172008-10-07 17:03:15 +00006898 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6899 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006900 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6901 DAG.getConstant(1, NVT),
6902 DAG.getConstant(0, NVT));
Andrew Lenharth6606f172008-10-07 17:03:15 +00006903 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6904 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006905 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6906 DAG.getConstant(1, NVT),
6907 Carry1);
6908 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6909 } else {
Evan Cheng3270a1d2008-12-12 18:49:09 +00006910 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6911 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth3a9be152008-10-07 14:15:42 +00006912 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6913 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6914 DAG.getConstant(1, NVT),
6915 DAG.getConstant(0, NVT));
6916 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6917 }
6918 break;
Nate Begeman5965bd12006-02-17 05:43:56 +00006919 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006920 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006921
6922 case ISD::ADDC:
6923 case ISD::SUBC: {
6924 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006925 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00006926 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6927 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6928 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006929 SDValue LoOps[2] = { LHSL, RHSL };
6930 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00006931
6932 if (Node->getOpcode() == ISD::ADDC) {
6933 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6934 HiOps[2] = Lo.getValue(1);
6935 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6936 } else {
6937 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6938 HiOps[2] = Lo.getValue(1);
6939 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6940 }
6941 // Remember that we legalized the flag.
6942 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6943 break;
6944 }
6945 case ISD::ADDE:
6946 case ISD::SUBE: {
6947 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006948 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00006949 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6950 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6951 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006952 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6953 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00006954
6955 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6956 HiOps[2] = Lo.getValue(1);
6957 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6958
6959 // Remember that we legalized the flag.
6960 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6961 break;
6962 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006963 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006964 // If the target wants to custom expand this, let them.
6965 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006966 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006967 if (New.getNode()) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00006968 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006969 break;
6970 }
6971 }
6972
Evan Chenge93762d2006-09-01 18:17:58 +00006973 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6974 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006975 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6976 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6977 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006978 SDValue LL, LH, RL, RH;
Nate Begemanadd0c632005-04-11 03:01:51 +00006979 ExpandOp(Node->getOperand(0), LL, LH);
6980 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00006981 unsigned OuterBitSize = Op.getValueSizeInBits();
6982 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00006983 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6984 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman272e2342008-03-10 20:42:19 +00006985 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6986 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6987 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohmana1603612007-10-08 18:33:35 +00006988 // The inputs are both zero-extended.
6989 if (HasUMUL_LOHI) {
6990 // We can emit a umul_lohi.
6991 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006992 Hi = SDValue(Lo.getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00006993 break;
6994 }
6995 if (HasMULHU) {
6996 // We can emit a mulhu+mul.
6997 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6998 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6999 break;
7000 }
Dan Gohmana1603612007-10-08 18:33:35 +00007001 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00007002 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00007003 // The input values are both sign-extended.
7004 if (HasSMUL_LOHI) {
7005 // We can emit a smul_lohi.
7006 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007007 Hi = SDValue(Lo.getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00007008 break;
7009 }
7010 if (HasMULHS) {
7011 // We can emit a mulhs+mul.
7012 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7013 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7014 break;
7015 }
7016 }
7017 if (HasUMUL_LOHI) {
7018 // Lo,Hi = umul LHS, RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007019 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohmana1603612007-10-08 18:33:35 +00007020 DAG.getVTList(NVT, NVT), LL, RL);
7021 Lo = UMulLOHI;
7022 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00007023 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7024 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7025 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7026 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00007027 break;
Nate Begeman43144a22005-08-30 02:44:00 +00007028 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00007029 if (HasMULHU) {
7030 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7031 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7032 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7033 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7034 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7035 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7036 break;
7037 }
Nate Begemanadd0c632005-04-11 03:01:51 +00007038 }
Evan Chenge93762d2006-09-01 18:17:58 +00007039
Dan Gohmana1603612007-10-08 18:33:35 +00007040 // If nothing else, we can make a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00007041 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00007042 break;
7043 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00007044 case ISD::SDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007045 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007046 break;
7047 case ISD::UDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007048 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007049 break;
7050 case ISD::SREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00007051 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007052 break;
7053 case ISD::UREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00007054 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007055 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00007056
Evan Cheng4eee7242006-12-09 02:42:38 +00007057 case ISD::FADD:
Duncan Sands844d55a2008-04-12 17:14:18 +00007058 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7059 RTLIB::ADD_F64,
7060 RTLIB::ADD_F80,
7061 RTLIB::ADD_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007062 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007063 break;
7064 case ISD::FSUB:
Duncan Sands844d55a2008-04-12 17:14:18 +00007065 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7066 RTLIB::SUB_F64,
7067 RTLIB::SUB_F80,
7068 RTLIB::SUB_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007069 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007070 break;
7071 case ISD::FMUL:
Duncan Sands844d55a2008-04-12 17:14:18 +00007072 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7073 RTLIB::MUL_F64,
7074 RTLIB::MUL_F80,
7075 RTLIB::MUL_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007076 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007077 break;
7078 case ISD::FDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00007079 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7080 RTLIB::DIV_F64,
7081 RTLIB::DIV_F80,
7082 RTLIB::DIV_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00007083 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007084 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007085 case ISD::FP_EXTEND: {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007086 if (VT == MVT::ppcf128) {
7087 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7088 Node->getOperand(0).getValueType()==MVT::f64);
7089 const uint64_t zero = 0;
7090 if (Node->getOperand(0).getValueType()==MVT::f32)
7091 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7092 else
7093 Hi = Node->getOperand(0);
7094 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7095 break;
7096 }
Duncan Sands77a3d052008-07-17 02:36:29 +00007097 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7098 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7099 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007100 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007101 }
7102 case ISD::FP_ROUND: {
7103 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7104 VT);
7105 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7106 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00007107 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00007108 }
Evan Chengf4e5de42008-09-09 23:02:14 +00007109 case ISD::FSQRT:
7110 case ISD::FSIN:
7111 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007112 case ISD::FLOG:
7113 case ISD::FLOG2:
7114 case ISD::FLOG10:
7115 case ISD::FEXP:
7116 case ISD::FEXP2:
Dan Gohman2af34bd2008-08-21 18:38:14 +00007117 case ISD::FTRUNC:
7118 case ISD::FFLOOR:
7119 case ISD::FCEIL:
7120 case ISD::FRINT:
7121 case ISD::FNEARBYINT:
Evan Cheng0fff3972008-09-09 23:35:53 +00007122 case ISD::FPOW:
7123 case ISD::FPOWI: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00007124 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00007125 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00007126 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00007127 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7128 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007129 break;
7130 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00007131 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7132 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007133 break;
7134 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00007135 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7136 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00007137 break;
Dale Johannesenda2d8062008-09-04 00:47:13 +00007138 case ISD::FLOG:
7139 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7140 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7141 break;
7142 case ISD::FLOG2:
7143 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7144 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7145 break;
7146 case ISD::FLOG10:
7147 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7148 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7149 break;
7150 case ISD::FEXP:
7151 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7152 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7153 break;
7154 case ISD::FEXP2:
7155 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7156 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7157 break;
Dan Gohman2af34bd2008-08-21 18:38:14 +00007158 case ISD::FTRUNC:
7159 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7160 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7161 break;
7162 case ISD::FFLOOR:
7163 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7164 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7165 break;
7166 case ISD::FCEIL:
7167 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7168 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7169 break;
7170 case ISD::FRINT:
7171 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7172 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7173 break;
7174 case ISD::FNEARBYINT:
7175 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7176 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7177 break;
Evan Chengf4e5de42008-09-09 23:02:14 +00007178 case ISD::FPOW:
7179 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7180 RTLIB::POW_PPCF128);
7181 break;
7182 case ISD::FPOWI:
7183 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7184 RTLIB::POWI_PPCF128);
7185 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00007186 default: assert(0 && "Unreachable!");
7187 }
Duncan Sands844d55a2008-04-12 17:14:18 +00007188 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00007189 break;
7190 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00007191 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00007192 if (VT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007193 SDValue Tmp;
Dale Johannesen61c574f2007-10-12 19:02:17 +00007194 ExpandOp(Node->getOperand(0), Lo, Tmp);
7195 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7196 // lo = hi==fabs(hi) ? lo : -lo;
7197 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7198 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7199 DAG.getCondCode(ISD::SETEQ));
7200 break;
7201 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007202 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00007203 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7204 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7205 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7206 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7207 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7208 if (getTypeAction(NVT) == Expand)
7209 ExpandOp(Lo, Lo, Hi);
7210 break;
7211 }
7212 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00007213 if (VT == MVT::ppcf128) {
7214 ExpandOp(Node->getOperand(0), Lo, Hi);
7215 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7216 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7217 break;
7218 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007219 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00007220 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7221 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7222 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7223 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7224 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7225 if (getTypeAction(NVT) == Expand)
7226 ExpandOp(Lo, Lo, Hi);
7227 break;
7228 }
Evan Cheng003feb02007-01-04 21:56:39 +00007229 case ISD::FCOPYSIGN: {
7230 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7231 if (getTypeAction(NVT) == Expand)
7232 ExpandOp(Lo, Lo, Hi);
7233 break;
7234 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007235 case ISD::SINT_TO_FP:
7236 case ISD::UINT_TO_FP: {
7237 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands13237ac2008-06-06 12:08:01 +00007238 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen12c76db2008-03-18 17:28:38 +00007239
7240 // Promote the operand if needed. Do this before checking for
7241 // ppcf128 so conversions of i16 and i8 work.
7242 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007243 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen12c76db2008-03-18 17:28:38 +00007244 Tmp = isSigned
7245 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7246 DAG.getValueType(SrcVT))
7247 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007248 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen12c76db2008-03-18 17:28:38 +00007249 SrcVT = Node->getOperand(0).getValueType();
7250 }
7251
Dan Gohmanf4300952008-03-10 23:03:31 +00007252 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman432e4a62008-02-25 21:39:34 +00007253 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007254 if (isSigned) {
7255 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7256 Node->getOperand(0)));
7257 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7258 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00007259 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007260 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7261 Node->getOperand(0)));
7262 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7263 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007264 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00007265 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7266 DAG.getConstant(0, MVT::i32),
7267 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7268 DAG.getConstantFP(
7269 APFloat(APInt(128, 2, TwoE32)),
7270 MVT::ppcf128)),
7271 Hi,
7272 DAG.getCondCode(ISD::SETLT)),
7273 Lo, Hi);
7274 }
7275 break;
7276 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007277 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7278 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00007279 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00007280 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7281 Lo, Hi);
7282 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7283 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7284 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7285 DAG.getConstant(0, MVT::i64),
7286 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7287 DAG.getConstantFP(
7288 APFloat(APInt(128, 2, TwoE64)),
7289 MVT::ppcf128)),
7290 Hi,
7291 DAG.getCondCode(ISD::SETLT)),
7292 Lo, Hi);
7293 break;
7294 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007295
Dan Gohmanf4300952008-03-10 23:03:31 +00007296 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7297 Node->getOperand(0));
Evan Cheng86e476b2008-04-01 01:50:16 +00007298 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng611abc02008-04-01 01:51:26 +00007299 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng86e476b2008-04-01 01:50:16 +00007300 ExpandOp(Lo, Lo, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00007301 break;
7302 }
Evan Chengf3a80c62006-12-13 02:38:13 +00007303 }
Chris Lattnerdc750592005-01-07 07:47:09 +00007304
Chris Lattnerac12f682005-12-21 18:02:52 +00007305 // Make sure the resultant values have been legalized themselves, unless this
7306 // is a type that requires multi-step expansion.
7307 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7308 Lo = LegalizeOp(Lo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007309 if (Hi.getNode())
Evan Cheng3432ab92006-12-11 19:27:14 +00007310 // Don't legalize the high part if it is expanded to a single node.
7311 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00007312 }
Evan Cheng870e4f82006-01-09 18:31:59 +00007313
7314 // Remember in a map if the values will be reused later.
Dan Gohman38740a92008-07-07 17:46:23 +00007315 bool isNew =
7316 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng870e4f82006-01-09 18:31:59 +00007317 assert(isNew && "Value already expanded?!?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007318 isNew = isNew;
Chris Lattnerdc750592005-01-07 07:47:09 +00007319}
7320
Dan Gohmana8665142007-06-25 16:23:39 +00007321/// SplitVectorOp - Given an operand of vector type, break it down into
7322/// two smaller values, still of vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007323void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7324 SDValue &Hi) {
Duncan Sands13237ac2008-06-06 12:08:01 +00007325 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00007326 SDNode *Node = Op.getNode();
Duncan Sands13237ac2008-06-06 12:08:01 +00007327 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattner32206f52006-03-18 01:44:44 +00007328 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00007329
Duncan Sands13237ac2008-06-06 12:08:01 +00007330 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begemanbd117f02007-11-15 21:15:26 +00007331
7332 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7333 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7334
Duncan Sands13237ac2008-06-06 12:08:01 +00007335 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7336 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00007337
Chris Lattner32206f52006-03-18 01:44:44 +00007338 // See if we already split it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007339 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner32206f52006-03-18 01:44:44 +00007340 = SplitNodes.find(Op);
7341 if (I != SplitNodes.end()) {
7342 Lo = I->second.first;
7343 Hi = I->second.second;
7344 return;
7345 }
7346
7347 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007348 default:
7349#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00007350 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007351#endif
7352 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00007353 case ISD::UNDEF:
7354 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7355 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7356 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007357 case ISD::BUILD_PAIR:
7358 Lo = Node->getOperand(0);
7359 Hi = Node->getOperand(1);
7360 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00007361 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman6f94f612008-04-25 18:07:40 +00007362 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7363 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmaneffb8942008-09-12 16:56:44 +00007364 unsigned Index = Idx->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007365 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman6f94f612008-04-25 18:07:40 +00007366 if (Index < NewNumElts_Lo)
7367 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7368 DAG.getIntPtrConstant(Index));
7369 else
7370 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7371 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7372 break;
7373 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007374 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman6f94f612008-04-25 18:07:40 +00007375 Node->getOperand(1),
7376 Node->getOperand(2));
7377 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmana90183e2007-09-28 23:53:40 +00007378 break;
7379 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007380 case ISD::VECTOR_SHUFFLE: {
7381 // Build the low part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007382 SDValue Mask = Node->getOperand(2);
7383 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00007384 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007385
7386 // Insert all of the elements from the input that are needed. We use
7387 // buildvector of extractelement here because the input vectors will have
7388 // to be legalized, so this makes the code simpler.
7389 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007390 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00007391 if (IdxNode.getOpcode() == ISD::UNDEF) {
7392 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7393 continue;
7394 }
Dan Gohmaneffb8942008-09-12 16:56:44 +00007395 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007396 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007397 if (Idx >= NumElements) {
7398 InVec = Node->getOperand(1);
7399 Idx -= NumElements;
7400 }
7401 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7402 DAG.getConstant(Idx, PtrVT)));
7403 }
7404 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7405 Ops.clear();
7406
7407 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007408 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00007409 if (IdxNode.getOpcode() == ISD::UNDEF) {
7410 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7411 continue;
7412 }
Dan Gohmaneffb8942008-09-12 16:56:44 +00007413 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007414 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007415 if (Idx >= NumElements) {
7416 InVec = Node->getOperand(1);
7417 Idx -= NumElements;
7418 }
7419 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7420 DAG.getConstant(Idx, PtrVT)));
7421 }
Mon P Wang73343502008-07-25 01:30:26 +00007422 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6fa95ec2007-11-19 21:16:54 +00007423 break;
7424 }
Dan Gohmana8665142007-06-25 16:23:39 +00007425 case ISD::BUILD_VECTOR: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007426 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00007427 Node->op_begin()+NewNumElts_Lo);
7428 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00007429
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007430 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00007431 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00007432 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00007433 break;
7434 }
Dan Gohmana8665142007-06-25 16:23:39 +00007435 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00007436 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00007437 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7438 if (NewNumSubvectors == 1) {
7439 Lo = Node->getOperand(0);
7440 Hi = Node->getOperand(1);
7441 } else {
Mon P Wang25f01062008-11-10 04:46:22 +00007442 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7443 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00007444 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00007445
Mon P Wang25f01062008-11-10 04:46:22 +00007446 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmana8665142007-06-25 16:23:39 +00007447 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00007448 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00007449 }
Dan Gohman26455c42007-06-13 15:12:02 +00007450 break;
7451 }
Mon P Wang25f01062008-11-10 04:46:22 +00007452 case ISD::EXTRACT_SUBVECTOR: {
7453 SDValue Vec = Op.getOperand(0);
7454 SDValue Idx = Op.getOperand(1);
7455 MVT IdxVT = Idx.getValueType();
7456
7457 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7458 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7459 if (CIdx) {
7460 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7461 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7462 IdxVT));
7463 } else {
7464 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7465 DAG.getConstant(NewNumElts_Lo, IdxVT));
7466 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7467 }
7468 break;
7469 }
Dan Gohman8f518b982007-10-17 14:48:28 +00007470 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007471 SDValue Cond = Node->getOperand(0);
Dan Gohman8f518b982007-10-17 14:48:28 +00007472
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007473 SDValue LL, LH, RL, RH;
Dan Gohman8f518b982007-10-17 14:48:28 +00007474 SplitVectorOp(Node->getOperand(1), LL, LH);
7475 SplitVectorOp(Node->getOperand(2), RL, RH);
7476
Duncan Sands13237ac2008-06-06 12:08:01 +00007477 if (Cond.getValueType().isVector()) {
Dan Gohman8f518b982007-10-17 14:48:28 +00007478 // Handle a vector merge.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007479 SDValue CL, CH;
Dan Gohman8f518b982007-10-17 14:48:28 +00007480 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00007481 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7482 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00007483 } else {
7484 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00007485 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7486 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00007487 }
7488 break;
7489 }
Chris Lattner9d3740e2008-06-30 02:43:01 +00007490 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007491 SDValue CondLHS = Node->getOperand(0);
7492 SDValue CondRHS = Node->getOperand(1);
7493 SDValue CondCode = Node->getOperand(4);
Chris Lattner9d3740e2008-06-30 02:43:01 +00007494
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007495 SDValue LL, LH, RL, RH;
Chris Lattner9d3740e2008-06-30 02:43:01 +00007496 SplitVectorOp(Node->getOperand(2), LL, LH);
7497 SplitVectorOp(Node->getOperand(3), RL, RH);
7498
7499 // Handle a simple select with vector operands.
7500 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7501 LL, RL, CondCode);
7502 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7503 LH, RH, CondCode);
7504 break;
7505 }
Nate Begemancfcb5602008-05-12 19:40:03 +00007506 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007507 SDValue LL, LH, RL, RH;
Nate Begemancfcb5602008-05-12 19:40:03 +00007508 SplitVectorOp(Node->getOperand(0), LL, LH);
7509 SplitVectorOp(Node->getOperand(1), RL, RH);
7510 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7511 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7512 break;
7513 }
Dan Gohmana8665142007-06-25 16:23:39 +00007514 case ISD::ADD:
7515 case ISD::SUB:
7516 case ISD::MUL:
7517 case ISD::FADD:
7518 case ISD::FSUB:
7519 case ISD::FMUL:
7520 case ISD::SDIV:
7521 case ISD::UDIV:
7522 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00007523 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00007524 case ISD::AND:
7525 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00007526 case ISD::XOR:
7527 case ISD::UREM:
7528 case ISD::SREM:
7529 case ISD::FREM: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007530 SDValue LL, LH, RL, RH;
Chris Lattner32206f52006-03-18 01:44:44 +00007531 SplitVectorOp(Node->getOperand(0), LL, LH);
7532 SplitVectorOp(Node->getOperand(1), RL, RH);
7533
Nate Begemanbd117f02007-11-15 21:15:26 +00007534 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7535 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00007536 break;
7537 }
Dan Gohman550c9af2008-08-14 20:04:46 +00007538 case ISD::FP_ROUND:
Dan Gohman2a7de412007-10-11 23:57:53 +00007539 case ISD::FPOWI: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007540 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00007541 SplitVectorOp(Node->getOperand(0), L, H);
7542
Nate Begemanbd117f02007-11-15 21:15:26 +00007543 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7544 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00007545 break;
7546 }
7547 case ISD::CTTZ:
7548 case ISD::CTLZ:
7549 case ISD::CTPOP:
7550 case ISD::FNEG:
7551 case ISD::FABS:
7552 case ISD::FSQRT:
7553 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00007554 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007555 case ISD::FLOG:
7556 case ISD::FLOG2:
7557 case ISD::FLOG10:
7558 case ISD::FEXP:
7559 case ISD::FEXP2:
Nate Begemand4d45c22007-11-17 03:58:34 +00007560 case ISD::FP_TO_SINT:
7561 case ISD::FP_TO_UINT:
7562 case ISD::SINT_TO_FP:
Dan Gohman550c9af2008-08-14 20:04:46 +00007563 case ISD::UINT_TO_FP:
7564 case ISD::TRUNCATE:
7565 case ISD::ANY_EXTEND:
7566 case ISD::SIGN_EXTEND:
7567 case ISD::ZERO_EXTEND:
7568 case ISD::FP_EXTEND: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007569 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00007570 SplitVectorOp(Node->getOperand(0), L, H);
7571
Nate Begemanbd117f02007-11-15 21:15:26 +00007572 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7573 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00007574 break;
7575 }
Mon P Wang58fb9132008-11-10 20:54:11 +00007576 case ISD::CONVERT_RNDSAT: {
7577 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7578 SDValue L, H;
7579 SplitVectorOp(Node->getOperand(0), L, H);
7580 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7581 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7582 SDValue STyOpL = DAG.getValueType(L.getValueType());
7583 SDValue STyOpH = DAG.getValueType(H.getValueType());
7584
7585 SDValue RndOp = Node->getOperand(3);
7586 SDValue SatOp = Node->getOperand(4);
7587
7588 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7589 RndOp, SatOp, CvtCode);
7590 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7591 RndOp, SatOp, CvtCode);
7592 break;
7593 }
Dan Gohmana8665142007-06-25 16:23:39 +00007594 case ISD::LOAD: {
7595 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007596 SDValue Ch = LD->getChain();
7597 SDValue Ptr = LD->getBasePtr();
Dan Gohman550c9af2008-08-14 20:04:46 +00007598 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmana8665142007-06-25 16:23:39 +00007599 const Value *SV = LD->getSrcValue();
7600 int SVOffset = LD->getSrcValueOffset();
Dan Gohman550c9af2008-08-14 20:04:46 +00007601 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmana8665142007-06-25 16:23:39 +00007602 unsigned Alignment = LD->getAlignment();
7603 bool isVolatile = LD->isVolatile();
7604
Dan Gohman550c9af2008-08-14 20:04:46 +00007605 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7606 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7607
7608 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7609 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7610 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7611
7612 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7613 NewVT_Lo, Ch, Ptr, Offset,
7614 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7615 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattner32206f52006-03-18 01:44:44 +00007616 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00007617 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00007618 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00007619 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman550c9af2008-08-14 20:04:46 +00007620 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7621 NewVT_Hi, Ch, Ptr, Offset,
7622 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00007623
7624 // Build a factor node to remember that this load is independent of the
7625 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007626 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattner32206f52006-03-18 01:44:44 +00007627 Hi.getValue(1));
7628
7629 // Remember that we legalized the chain.
7630 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00007631 break;
7632 }
Dan Gohmana8665142007-06-25 16:23:39 +00007633 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007634 // We know the result is a vector. The input may be either a vector or a
7635 // scalar value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007636 SDValue InOp = Node->getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00007637 if (!InOp.getValueType().isVector() ||
7638 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman0de76942007-06-29 00:09:08 +00007639 // The input is a scalar or single-element vector.
7640 // Lower to a store/load so that it can be split.
7641 // FIXME: this could be improved probably.
Mon P Wang97432f42008-07-15 05:28:34 +00007642 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7643 Op.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007644 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007645 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007646
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007647 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00007648 InOp, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00007649 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00007650 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00007651 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007652 }
Dan Gohman0de76942007-06-29 00:09:08 +00007653 // Split the vector and convert each of the pieces now.
7654 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00007655 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7656 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00007657 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00007658 }
Chris Lattner32206f52006-03-18 01:44:44 +00007659 }
7660
7661 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00007662 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00007663 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00007664 assert(isNew && "Value already split?!?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007665 isNew = isNew;
Chris Lattner32206f52006-03-18 01:44:44 +00007666}
7667
7668
Dan Gohmanf4e86da2007-06-27 14:06:22 +00007669/// ScalarizeVectorOp - Given an operand of single-element vector type
7670/// (e.g. v1f32), convert it into the equivalent operation that returns a
7671/// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007672SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00007673 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00007674 SDNode *Node = Op.getNode();
Duncan Sands13237ac2008-06-06 12:08:01 +00007675 MVT NewVT = Op.getValueType().getVectorElementType();
7676 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00007677
Dan Gohmana8665142007-06-25 16:23:39 +00007678 // See if we already scalarized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007679 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmana8665142007-06-25 16:23:39 +00007680 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00007681
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007682 SDValue Result;
Chris Lattner32206f52006-03-18 01:44:44 +00007683 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00007684 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007685#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00007686 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007687#endif
Dan Gohmana8665142007-06-25 16:23:39 +00007688 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7689 case ISD::ADD:
7690 case ISD::FADD:
7691 case ISD::SUB:
7692 case ISD::FSUB:
7693 case ISD::MUL:
7694 case ISD::FMUL:
7695 case ISD::SDIV:
7696 case ISD::UDIV:
7697 case ISD::FDIV:
7698 case ISD::SREM:
7699 case ISD::UREM:
7700 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00007701 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00007702 case ISD::AND:
7703 case ISD::OR:
7704 case ISD::XOR:
7705 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00007706 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00007707 ScalarizeVectorOp(Node->getOperand(0)),
7708 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00007709 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007710 case ISD::FNEG:
7711 case ISD::FABS:
7712 case ISD::FSQRT:
7713 case ISD::FSIN:
7714 case ISD::FCOS:
Dale Johannesenda2d8062008-09-04 00:47:13 +00007715 case ISD::FLOG:
7716 case ISD::FLOG2:
7717 case ISD::FLOG10:
7718 case ISD::FEXP:
7719 case ISD::FEXP2:
Dan Gohman550c9af2008-08-14 20:04:46 +00007720 case ISD::FP_TO_SINT:
7721 case ISD::FP_TO_UINT:
7722 case ISD::SINT_TO_FP:
7723 case ISD::UINT_TO_FP:
7724 case ISD::SIGN_EXTEND:
7725 case ISD::ZERO_EXTEND:
7726 case ISD::ANY_EXTEND:
7727 case ISD::TRUNCATE:
7728 case ISD::FP_EXTEND:
Dan Gohmana8665142007-06-25 16:23:39 +00007729 Result = DAG.getNode(Node->getOpcode(),
7730 NewVT,
7731 ScalarizeVectorOp(Node->getOperand(0)));
7732 break;
Mon P Wang58fb9132008-11-10 20:54:11 +00007733 case ISD::CONVERT_RNDSAT: {
7734 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7735 Result = DAG.getConvertRndSat(NewVT, Op0,
7736 DAG.getValueType(NewVT),
7737 DAG.getValueType(Op0.getValueType()),
7738 Node->getOperand(3),
7739 Node->getOperand(4),
7740 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7741 break;
7742 }
Dan Gohman4f056f32007-10-12 14:13:46 +00007743 case ISD::FPOWI:
Dan Gohman550c9af2008-08-14 20:04:46 +00007744 case ISD::FP_ROUND:
Dan Gohman4f056f32007-10-12 14:13:46 +00007745 Result = DAG.getNode(Node->getOpcode(),
7746 NewVT,
7747 ScalarizeVectorOp(Node->getOperand(0)),
7748 Node->getOperand(1));
7749 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007750 case ISD::LOAD: {
7751 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007752 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7753 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman550c9af2008-08-14 20:04:46 +00007754 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmana8665142007-06-25 16:23:39 +00007755 const Value *SV = LD->getSrcValue();
7756 int SVOffset = LD->getSrcValueOffset();
Dan Gohman550c9af2008-08-14 20:04:46 +00007757 MVT MemoryVT = LD->getMemoryVT();
7758 unsigned Alignment = LD->getAlignment();
7759 bool isVolatile = LD->isVolatile();
7760
7761 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7762 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7763
7764 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7765 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7766 MemoryVT.getVectorElementType(),
7767 isVolatile, Alignment);
Dan Gohmana8665142007-06-25 16:23:39 +00007768
Chris Lattner32206f52006-03-18 01:44:44 +00007769 // Remember that we legalized the chain.
7770 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7771 break;
7772 }
Dan Gohmana8665142007-06-25 16:23:39 +00007773 case ISD::BUILD_VECTOR:
7774 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00007775 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007776 case ISD::INSERT_VECTOR_ELT:
7777 // Returning the inserted scalar element.
7778 Result = Node->getOperand(1);
7779 break;
7780 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00007781 assert(Node->getOperand(0).getValueType() == NewVT &&
7782 "Concat of non-legal vectors not yet supported!");
7783 Result = Node->getOperand(0);
7784 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007785 case ISD::VECTOR_SHUFFLE: {
7786 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007787 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +00007788 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmana8665142007-06-25 16:23:39 +00007789 Result = ScalarizeVectorOp(Node->getOperand(1));
7790 else
7791 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00007792 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007793 }
7794 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang5ca2ec62008-11-06 22:52:21 +00007795 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wang25f01062008-11-10 04:46:22 +00007796 Node->getOperand(1));
Dan Gohman26455c42007-06-13 15:12:02 +00007797 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007798 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007799 SDValue Op0 = Op.getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00007800 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng9ac36312008-05-16 17:19:05 +00007801 Op0 = ScalarizeVectorOp(Op0);
7802 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00007803 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007804 }
Dan Gohmana8665142007-06-25 16:23:39 +00007805 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007806 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007807 ScalarizeVectorOp(Op.getOperand(1)),
7808 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007809 break;
Chris Lattner9d3740e2008-06-30 02:43:01 +00007810 case ISD::SELECT_CC:
7811 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7812 Node->getOperand(1),
7813 ScalarizeVectorOp(Op.getOperand(2)),
7814 ScalarizeVectorOp(Op.getOperand(3)),
7815 Node->getOperand(4));
7816 break;
Nate Begemanb87e63a2008-05-12 23:09:43 +00007817 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007818 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7819 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begemanb87e63a2008-05-12 23:09:43 +00007820 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7821 Op.getOperand(2));
7822 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7823 DAG.getConstant(-1ULL, NewVT),
7824 DAG.getConstant(0ULL, NewVT));
7825 break;
7826 }
Chris Lattner32206f52006-03-18 01:44:44 +00007827 }
7828
7829 if (TLI.isTypeLegal(NewVT))
7830 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007831 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7832 assert(isNew && "Value already scalarized?");
Evan Chenga8fd1f22008-11-24 07:09:49 +00007833 isNew = isNew;
Chris Lattner32206f52006-03-18 01:44:44 +00007834 return Result;
7835}
7836
Chris Lattnerdc750592005-01-07 07:47:09 +00007837
Mon P Wang58c37942008-10-30 08:01:45 +00007838SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7839 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7840 if (I != WidenNodes.end()) return I->second;
7841
7842 MVT VT = Op.getValueType();
7843 assert(VT.isVector() && "Cannot widen non-vector type!");
7844
7845 SDValue Result;
7846 SDNode *Node = Op.getNode();
7847 MVT EVT = VT.getVectorElementType();
7848
7849 unsigned NumElts = VT.getVectorNumElements();
7850 unsigned NewNumElts = WidenVT.getVectorNumElements();
7851 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7852 assert(NewNumElts < 17);
7853
7854 // When widen is called, it is assumed that it is more efficient to use a
7855 // wide type. The default action is to widen to operation to a wider legal
7856 // vector type and then do the operation if it is legal by calling LegalizeOp
7857 // again. If there is no vector equivalent, we will unroll the operation, do
7858 // it, and rebuild the vector. If most of the operations are vectorizible to
7859 // the legal type, the resulting code will be more efficient. If this is not
7860 // the case, the resulting code will preform badly as we end up generating
7861 // code to pack/unpack the results. It is the function that calls widen
Mon P Wang9a8d60a2008-11-06 05:31:54 +00007862 // that is responsible for seeing this doesn't happen.
Mon P Wang58c37942008-10-30 08:01:45 +00007863 switch (Node->getOpcode()) {
7864 default:
7865#ifndef NDEBUG
7866 Node->dump(&DAG);
7867#endif
7868 assert(0 && "Unexpected operation in WidenVectorOp!");
7869 break;
7870 case ISD::CopyFromReg:
Mon P Wangf414cbc2008-11-15 06:05:52 +00007871 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang58c37942008-10-30 08:01:45 +00007872 case ISD::Constant:
7873 case ISD::ConstantFP:
7874 // To build a vector of these elements, clients should call BuildVector
7875 // and with each element instead of creating a node with a vector type
7876 assert(0 && "Unexpected operation in WidenVectorOp!");
7877 case ISD::VAARG:
7878 // Variable Arguments with vector types doesn't make any sense to me
7879 assert(0 && "Unexpected operation in WidenVectorOp!");
7880 break;
Mon P Wangf414cbc2008-11-15 06:05:52 +00007881 case ISD::UNDEF:
7882 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7883 break;
Mon P Wang58c37942008-10-30 08:01:45 +00007884 case ISD::BUILD_VECTOR: {
7885 // Build a vector with undefined for the new nodes
7886 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7887 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7888 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7889 }
7890 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7891 break;
7892 }
7893 case ISD::INSERT_VECTOR_ELT: {
7894 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7895 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7896 Node->getOperand(1), Node->getOperand(2));
7897 break;
7898 }
7899 case ISD::VECTOR_SHUFFLE: {
7900 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7901 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7902 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7903 // used as permutation array. We build the vector here instead of widening
7904 // because we don't want to legalize and have it turned to something else.
7905 SDValue PermOp = Node->getOperand(2);
7906 SDValueVector NewOps;
7907 MVT PVT = PermOp.getValueType().getVectorElementType();
7908 for (unsigned i = 0; i < NumElts; ++i) {
7909 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7910 NewOps.push_back(PermOp.getOperand(i));
7911 } else {
7912 unsigned Idx =
7913 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7914 if (Idx < NumElts) {
7915 NewOps.push_back(PermOp.getOperand(i));
7916 }
7917 else {
7918 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7919 PermOp.getOperand(i).getValueType()));
7920 }
7921 }
7922 }
7923 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7924 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7925 }
7926
7927 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7928 MVT::getVectorVT(PVT, NewOps.size()),
7929 &NewOps[0], NewOps.size());
7930
7931 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7932 break;
7933 }
7934 case ISD::LOAD: {
7935 // If the load widen returns true, we can use a single load for the
7936 // vector. Otherwise, it is returning a token factor for multiple
7937 // loads.
7938 SDValue TFOp;
7939 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7940 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7941 else
7942 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7943 break;
7944 }
7945
7946 case ISD::BIT_CONVERT: {
7947 SDValue Tmp1 = Node->getOperand(0);
7948 // Converts between two different types so we need to determine
7949 // the correct widen type for the input operand.
7950 MVT TVT = Tmp1.getValueType();
7951 assert(TVT.isVector() && "can not widen non vector type");
7952 MVT TEVT = TVT.getVectorElementType();
7953 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7954 "can not widen bit bit convert that are not multiple of element type");
7955 MVT TWidenVT = MVT::getVectorVT(TEVT,
7956 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7957 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7958 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7959 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7960
7961 TargetLowering::LegalizeAction action =
7962 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7963 switch (action) {
7964 default: assert(0 && "action not supported");
7965 case TargetLowering::Legal:
7966 break;
7967 case TargetLowering::Promote:
7968 // We defer the promotion to when we legalize the op
7969 break;
7970 case TargetLowering::Expand:
7971 // Expand the operation into a bunch of nasty scalar code.
7972 Result = LegalizeOp(UnrollVectorOp(Result));
7973 break;
7974 }
7975 break;
7976 }
7977
7978 case ISD::SINT_TO_FP:
7979 case ISD::UINT_TO_FP:
7980 case ISD::FP_TO_SINT:
7981 case ISD::FP_TO_UINT: {
7982 SDValue Tmp1 = Node->getOperand(0);
7983 // Converts between two different types so we need to determine
7984 // the correct widen type for the input operand.
7985 MVT TVT = Tmp1.getValueType();
7986 assert(TVT.isVector() && "can not widen non vector type");
7987 MVT TEVT = TVT.getVectorElementType();
7988 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7989 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7990 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7991 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang58c37942008-10-30 08:01:45 +00007992 break;
7993 }
7994
7995 case ISD::FP_EXTEND:
7996 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7997 case ISD::TRUNCATE:
7998 case ISD::SIGN_EXTEND:
7999 case ISD::ZERO_EXTEND:
8000 case ISD::ANY_EXTEND:
8001 case ISD::FP_ROUND:
8002 case ISD::SIGN_EXTEND_INREG:
8003 case ISD::FABS:
8004 case ISD::FNEG:
8005 case ISD::FSQRT:
8006 case ISD::FSIN:
Mon P Wangf414cbc2008-11-15 06:05:52 +00008007 case ISD::FCOS:
8008 case ISD::CTPOP:
8009 case ISD::CTTZ:
8010 case ISD::CTLZ: {
Mon P Wang58c37942008-10-30 08:01:45 +00008011 // Unary op widening
8012 SDValue Tmp1;
Mon P Wang58c37942008-10-30 08:01:45 +00008013 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8014 assert(Tmp1.getValueType() == WidenVT);
8015 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang58c37942008-10-30 08:01:45 +00008016 break;
8017 }
Mon P Wang58fb9132008-11-10 20:54:11 +00008018 case ISD::CONVERT_RNDSAT: {
8019 SDValue RndOp = Node->getOperand(3);
8020 SDValue SatOp = Node->getOperand(4);
Mon P Wang58fb9132008-11-10 20:54:11 +00008021 SDValue SrcOp = Node->getOperand(0);
8022
8023 // Converts between two different types so we need to determine
8024 // the correct widen type for the input operand.
8025 MVT SVT = SrcOp.getValueType();
8026 assert(SVT.isVector() && "can not widen non vector type");
8027 MVT SEVT = SVT.getVectorElementType();
8028 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8029
8030 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8031 assert(SrcOp.getValueType() == WidenVT);
8032 SDValue DTyOp = DAG.getValueType(WidenVT);
8033 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8034 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8035
8036 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8037 RndOp, SatOp, CvtCode);
Mon P Wang58fb9132008-11-10 20:54:11 +00008038 break;
8039 }
Mon P Wang58c37942008-10-30 08:01:45 +00008040 case ISD::FPOW:
8041 case ISD::FPOWI:
8042 case ISD::ADD:
8043 case ISD::SUB:
8044 case ISD::MUL:
8045 case ISD::MULHS:
8046 case ISD::MULHU:
8047 case ISD::AND:
8048 case ISD::OR:
8049 case ISD::XOR:
8050 case ISD::FADD:
8051 case ISD::FSUB:
8052 case ISD::FMUL:
8053 case ISD::SDIV:
8054 case ISD::SREM:
8055 case ISD::FDIV:
8056 case ISD::FREM:
8057 case ISD::FCOPYSIGN:
8058 case ISD::UDIV:
8059 case ISD::UREM:
8060 case ISD::BSWAP: {
8061 // Binary op widening
Mon P Wang58c37942008-10-30 08:01:45 +00008062 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8063 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8064 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8065 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang58c37942008-10-30 08:01:45 +00008066 break;
8067 }
8068
8069 case ISD::SHL:
8070 case ISD::SRA:
8071 case ISD::SRL: {
Mon P Wang58c37942008-10-30 08:01:45 +00008072 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8073 assert(Tmp1.getValueType() == WidenVT);
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008074 SDValue ShOp = Node->getOperand(1);
8075 MVT ShVT = ShOp.getValueType();
8076 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8077 WidenVT.getVectorNumElements());
8078 ShOp = WidenVectorOp(ShOp, NewShVT);
8079 assert(ShOp.getValueType() == NewShVT);
8080 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang58c37942008-10-30 08:01:45 +00008081 break;
8082 }
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008083
Mon P Wang58c37942008-10-30 08:01:45 +00008084 case ISD::EXTRACT_VECTOR_ELT: {
8085 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8086 assert(Tmp1.getValueType() == WidenVT);
8087 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8088 break;
8089 }
8090 case ISD::CONCAT_VECTORS: {
8091 // We concurrently support only widen on a multiple of the incoming vector.
8092 // We could widen on a multiple of the incoming operand if necessary.
8093 unsigned NumConcat = NewNumElts / NumElts;
8094 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wang6e1c6ad2008-12-02 07:35:08 +00008095 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang58c37942008-10-30 08:01:45 +00008096 SmallVector<SDValue, 8> MOps;
8097 MOps.push_back(Op);
8098 for (unsigned i = 1; i != NumConcat; ++i) {
8099 MOps.push_back(UndefVal);
8100 }
8101 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8102 &MOps[0], MOps.size()));
8103 break;
8104 }
8105 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wangf414cbc2008-11-15 06:05:52 +00008106 SDValue Tmp1 = Node->getOperand(0);
8107 SDValue Idx = Node->getOperand(1);
8108 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8109 if (CIdx && CIdx->getZExtValue() == 0) {
8110 // Since we are access the start of the vector, the incoming
8111 // vector type might be the proper.
8112 MVT Tmp1VT = Tmp1.getValueType();
8113 if (Tmp1VT == WidenVT)
8114 return Tmp1;
8115 else {
8116 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8117 if (Tmp1VTNumElts < NewNumElts)
8118 Result = WidenVectorOp(Tmp1, WidenVT);
8119 else
8120 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8121 }
8122 } else if (NewNumElts % NumElts == 0) {
8123 // Widen the extracted subvector.
8124 unsigned NumConcat = NewNumElts / NumElts;
8125 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8126 SmallVector<SDValue, 8> MOps;
8127 MOps.push_back(Op);
8128 for (unsigned i = 1; i != NumConcat; ++i) {
8129 MOps.push_back(UndefVal);
8130 }
8131 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8132 &MOps[0], MOps.size()));
8133 } else {
8134 assert(0 && "can not widen extract subvector");
8135 // This could be implemented using insert and build vector but I would
8136 // like to see when this happens.
8137 }
Mon P Wang58c37942008-10-30 08:01:45 +00008138 break;
8139 }
8140
8141 case ISD::SELECT: {
Mon P Wang58c37942008-10-30 08:01:45 +00008142 // Determine new condition widen type and widen
8143 SDValue Cond1 = Node->getOperand(0);
8144 MVT CondVT = Cond1.getValueType();
8145 assert(CondVT.isVector() && "can not widen non vector type");
8146 MVT CondEVT = CondVT.getVectorElementType();
8147 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8148 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8149 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8150
8151 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8152 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8153 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8154 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang58c37942008-10-30 08:01:45 +00008155 break;
8156 }
8157
8158 case ISD::SELECT_CC: {
Mon P Wang58c37942008-10-30 08:01:45 +00008159 // Determine new condition widen type and widen
8160 SDValue Cond1 = Node->getOperand(0);
8161 SDValue Cond2 = Node->getOperand(1);
8162 MVT CondVT = Cond1.getValueType();
8163 assert(CondVT.isVector() && "can not widen non vector type");
8164 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8165 MVT CondEVT = CondVT.getVectorElementType();
8166 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8167 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8168 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8169 assert(Cond1.getValueType() == CondWidenVT &&
8170 Cond2.getValueType() == CondWidenVT && "condition not widen");
8171
8172 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8173 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8174 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8175 "operands not widen");
8176 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8177 Tmp2, Node->getOperand(4));
Mon P Wang58c37942008-10-30 08:01:45 +00008178 break;
Mon P Wang01b8a5a2008-10-30 18:21:52 +00008179 }
8180 case ISD::VSETCC: {
8181 // Determine widen for the operand
8182 SDValue Tmp1 = Node->getOperand(0);
8183 MVT TmpVT = Tmp1.getValueType();
8184 assert(TmpVT.isVector() && "can not widen non vector type");
8185 MVT TmpEVT = TmpVT.getVectorElementType();
8186 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8187 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8188 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8189 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8190 Node->getOperand(2));
Mon P Wang58c37942008-10-30 08:01:45 +00008191 break;
8192 }
Mon P Wang58c37942008-10-30 08:01:45 +00008193 case ISD::ATOMIC_CMP_SWAP_8:
8194 case ISD::ATOMIC_CMP_SWAP_16:
8195 case ISD::ATOMIC_CMP_SWAP_32:
8196 case ISD::ATOMIC_CMP_SWAP_64:
8197 case ISD::ATOMIC_LOAD_ADD_8:
8198 case ISD::ATOMIC_LOAD_SUB_8:
8199 case ISD::ATOMIC_LOAD_AND_8:
8200 case ISD::ATOMIC_LOAD_OR_8:
8201 case ISD::ATOMIC_LOAD_XOR_8:
8202 case ISD::ATOMIC_LOAD_NAND_8:
8203 case ISD::ATOMIC_LOAD_MIN_8:
8204 case ISD::ATOMIC_LOAD_MAX_8:
8205 case ISD::ATOMIC_LOAD_UMIN_8:
8206 case ISD::ATOMIC_LOAD_UMAX_8:
8207 case ISD::ATOMIC_SWAP_8:
8208 case ISD::ATOMIC_LOAD_ADD_16:
8209 case ISD::ATOMIC_LOAD_SUB_16:
8210 case ISD::ATOMIC_LOAD_AND_16:
8211 case ISD::ATOMIC_LOAD_OR_16:
8212 case ISD::ATOMIC_LOAD_XOR_16:
8213 case ISD::ATOMIC_LOAD_NAND_16:
8214 case ISD::ATOMIC_LOAD_MIN_16:
8215 case ISD::ATOMIC_LOAD_MAX_16:
8216 case ISD::ATOMIC_LOAD_UMIN_16:
8217 case ISD::ATOMIC_LOAD_UMAX_16:
8218 case ISD::ATOMIC_SWAP_16:
8219 case ISD::ATOMIC_LOAD_ADD_32:
8220 case ISD::ATOMIC_LOAD_SUB_32:
8221 case ISD::ATOMIC_LOAD_AND_32:
8222 case ISD::ATOMIC_LOAD_OR_32:
8223 case ISD::ATOMIC_LOAD_XOR_32:
8224 case ISD::ATOMIC_LOAD_NAND_32:
8225 case ISD::ATOMIC_LOAD_MIN_32:
8226 case ISD::ATOMIC_LOAD_MAX_32:
8227 case ISD::ATOMIC_LOAD_UMIN_32:
8228 case ISD::ATOMIC_LOAD_UMAX_32:
8229 case ISD::ATOMIC_SWAP_32:
8230 case ISD::ATOMIC_LOAD_ADD_64:
8231 case ISD::ATOMIC_LOAD_SUB_64:
8232 case ISD::ATOMIC_LOAD_AND_64:
8233 case ISD::ATOMIC_LOAD_OR_64:
8234 case ISD::ATOMIC_LOAD_XOR_64:
8235 case ISD::ATOMIC_LOAD_NAND_64:
8236 case ISD::ATOMIC_LOAD_MIN_64:
8237 case ISD::ATOMIC_LOAD_MAX_64:
8238 case ISD::ATOMIC_LOAD_UMIN_64:
8239 case ISD::ATOMIC_LOAD_UMAX_64:
8240 case ISD::ATOMIC_SWAP_64: {
8241 // For now, we assume that using vectors for these operations don't make
8242 // much sense so we just split it. We return an empty result
8243 SDValue X, Y;
8244 SplitVectorOp(Op, X, Y);
8245 return Result;
8246 break;
8247 }
8248
8249 } // end switch (Node->getOpcode())
8250
8251 assert(Result.getNode() && "Didn't set a result!");
8252 if (Result != Op)
8253 Result = LegalizeOp(Result);
8254
Mon P Wang9a8d60a2008-11-06 05:31:54 +00008255 AddWidenedOperand(Op, Result);
Mon P Wang58c37942008-10-30 08:01:45 +00008256 return Result;
8257}
8258
8259// Utility function to find a legal vector type and its associated element
8260// type from a preferred width and whose vector type must be the same size
8261// as the VVT.
8262// TLI: Target lowering used to determine legal types
8263// Width: Preferred width of element type
8264// VVT: Vector value type whose size we must match.
8265// Returns VecEVT and EVT - the vector type and its associated element type
8266static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8267 MVT& EVT, MVT& VecEVT) {
8268 // We start with the preferred width, make it a power of 2 and see if
8269 // we can find a vector type of that width. If not, we reduce it by
8270 // another power of 2. If we have widen the type, a vector of bytes should
8271 // always be legal.
8272 assert(TLI.isTypeLegal(VVT));
8273 unsigned EWidth = Width + 1;
8274 do {
8275 assert(EWidth > 0);
8276 EWidth = (1 << Log2_32(EWidth-1));
8277 EVT = MVT::getIntegerVT(EWidth);
8278 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8279 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8280 } while (!TLI.isTypeLegal(VecEVT) ||
8281 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8282}
8283
8284SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8285 SDValue Chain,
8286 SDValue BasePtr,
8287 const Value *SV,
8288 int SVOffset,
8289 unsigned Alignment,
8290 bool isVolatile,
8291 unsigned LdWidth,
8292 MVT ResType) {
8293 // We assume that we have good rules to handle loading power of two loads so
8294 // we break down the operations to power of 2 loads. The strategy is to
8295 // load the largest power of 2 that we can easily transform to a legal vector
8296 // and then insert into that vector, and the cast the result into the legal
8297 // vector that we want. This avoids unnecessary stack converts.
8298 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8299 // the load is nonvolatile, we an use a wider load for the value.
8300 // Find a vector length we can load a large chunk
8301 MVT EVT, VecEVT;
8302 unsigned EVTWidth;
8303 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8304 EVTWidth = EVT.getSizeInBits();
8305
8306 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8307 isVolatile, Alignment);
8308 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8309 LdChain.push_back(LdOp.getValue(1));
8310
8311 // Check if we can load the element with one instruction
8312 if (LdWidth == EVTWidth) {
8313 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8314 }
8315
8316 // The vector element order is endianness dependent.
8317 unsigned Idx = 1;
8318 LdWidth -= EVTWidth;
8319 unsigned Offset = 0;
8320
8321 while (LdWidth > 0) {
8322 unsigned Increment = EVTWidth / 8;
8323 Offset += Increment;
8324 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8325 DAG.getIntPtrConstant(Increment));
8326
8327 if (LdWidth < EVTWidth) {
8328 // Our current type we are using is too large, use a smaller size by
8329 // using a smaller power of 2
8330 unsigned oEVTWidth = EVTWidth;
8331 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8332 EVTWidth = EVT.getSizeInBits();
8333 // Readjust position and vector position based on new load type
Mon P Wangf414cbc2008-11-15 06:05:52 +00008334 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang58c37942008-10-30 08:01:45 +00008335 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8336 }
8337
8338 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8339 SVOffset+Offset, isVolatile,
8340 MinAlign(Alignment, Offset));
8341 LdChain.push_back(LdOp.getValue(1));
8342 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8343 DAG.getIntPtrConstant(Idx++));
8344
8345 LdWidth -= EVTWidth;
8346 }
8347
8348 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8349}
8350
8351bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8352 SDValue& TFOp,
8353 SDValue Op,
8354 MVT NVT) {
8355 // TODO: Add support for ConcatVec and the ability to load many vector
8356 // types (e.g., v4i8). This will not work when a vector register
8357 // to memory mapping is strange (e.g., vector elements are not
8358 // stored in some sequential order).
8359
8360 // It must be true that the widen vector type is bigger than where
8361 // we need to load from.
8362 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8363 MVT LdVT = LD->getMemoryVT();
8364 assert(LdVT.isVector() && NVT.isVector());
8365 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8366
8367 // Load information
8368 SDValue Chain = LD->getChain();
8369 SDValue BasePtr = LD->getBasePtr();
8370 int SVOffset = LD->getSrcValueOffset();
8371 unsigned Alignment = LD->getAlignment();
8372 bool isVolatile = LD->isVolatile();
8373 const Value *SV = LD->getSrcValue();
8374 unsigned int LdWidth = LdVT.getSizeInBits();
8375
8376 // Load value as a large register
8377 SDValueVector LdChain;
8378 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8379 Alignment, isVolatile, LdWidth, NVT);
8380
8381 if (LdChain.size() == 1) {
8382 TFOp = LdChain[0];
8383 return true;
8384 }
8385 else {
8386 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8387 return false;
8388 }
8389}
8390
8391
8392void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8393 SDValue Chain,
8394 SDValue BasePtr,
8395 const Value *SV,
8396 int SVOffset,
8397 unsigned Alignment,
8398 bool isVolatile,
Mon P Wangf414cbc2008-11-15 06:05:52 +00008399 SDValue ValOp,
Mon P Wang58c37942008-10-30 08:01:45 +00008400 unsigned StWidth) {
8401 // Breaks the stores into a series of power of 2 width stores. For any
8402 // width, we convert the vector to the vector of element size that we
8403 // want to store. This avoids requiring a stack convert.
8404
8405 // Find a width of the element type we can store with
8406 MVT VVT = ValOp.getValueType();
8407 MVT EVT, VecEVT;
8408 unsigned EVTWidth;
8409 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8410 EVTWidth = EVT.getSizeInBits();
8411
8412 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8413 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang5ca2ec62008-11-06 22:52:21 +00008414 DAG.getIntPtrConstant(0));
Mon P Wang58c37942008-10-30 08:01:45 +00008415 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8416 isVolatile, Alignment);
8417 StChain.push_back(StOp);
8418
8419 // Check if we are done
8420 if (StWidth == EVTWidth) {
8421 return;
8422 }
8423
8424 unsigned Idx = 1;
8425 StWidth -= EVTWidth;
8426 unsigned Offset = 0;
8427
8428 while (StWidth > 0) {
8429 unsigned Increment = EVTWidth / 8;
8430 Offset += Increment;
8431 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8432 DAG.getIntPtrConstant(Increment));
8433
8434 if (StWidth < EVTWidth) {
8435 // Our current type we are using is too large, use a smaller size by
8436 // using a smaller power of 2
8437 unsigned oEVTWidth = EVTWidth;
8438 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8439 EVTWidth = EVT.getSizeInBits();
8440 // Readjust position and vector position based on new load type
Mon P Wangf414cbc2008-11-15 06:05:52 +00008441 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang58c37942008-10-30 08:01:45 +00008442 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8443 }
8444
8445 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wangf414cbc2008-11-15 06:05:52 +00008446 DAG.getIntPtrConstant(Idx++));
Mon P Wang58c37942008-10-30 08:01:45 +00008447 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8448 SVOffset + Offset, isVolatile,
8449 MinAlign(Alignment, Offset)));
8450 StWidth -= EVTWidth;
8451 }
8452}
8453
8454
8455SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8456 SDValue Chain,
8457 SDValue BasePtr) {
8458 // TODO: It might be cleaner if we can use SplitVector and have more legal
8459 // vector types that can be stored into memory (e.g., v4xi8 can
8460 // be stored as a word). This will not work when a vector register
8461 // to memory mapping is strange (e.g., vector elements are not
8462 // stored in some sequential order).
8463
8464 MVT StVT = ST->getMemoryVT();
8465 SDValue ValOp = ST->getValue();
8466
8467 // Check if we have widen this node with another value
8468 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8469 if (I != WidenNodes.end())
8470 ValOp = I->second;
8471
8472 MVT VVT = ValOp.getValueType();
8473
8474 // It must be true that we the widen vector type is bigger than where
8475 // we need to store.
8476 assert(StVT.isVector() && VVT.isVector());
8477 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8478 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8479
8480 // Store value
8481 SDValueVector StChain;
8482 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8483 ST->getSrcValueOffset(), ST->getAlignment(),
8484 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8485 if (StChain.size() == 1)
8486 return StChain[0];
8487 else
8488 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8489}
8490
8491
Chris Lattnerdc750592005-01-07 07:47:09 +00008492// SelectionDAG::Legalize - This is the entry point for the file.
8493//
Chris Lattner4add7e32005-01-23 04:42:50 +00008494void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00008495 /// run - This is the main entry point to this class.
8496 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00008497 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00008498}
8499