blob: 27340f19923f1bed47fa9d7d0767e63d6dfc8cd0 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000031#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000032using namespace llvm;
33
Chris Lattner5e46a192006-04-02 03:07:27 +000034#ifndef NDEBUG
35static cl::opt<bool>
36ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
37 cl::desc("Pop up a window to show dags before legalize"));
38#else
39static const bool ViewLegalizeDAGs = 0;
40#endif
41
Chris Lattner718071c2007-02-04 00:50:02 +000042namespace llvm {
43template<>
44struct DenseMapKeyInfo<SDOperand> {
45 static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); }
46 static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);}
47 static unsigned getHashValue(const SDOperand &Val) {
48 return DenseMapKeyInfo<void*>::getHashValue(Val.Val) + Val.ResNo;
49 }
50 static bool isPod() { return true; }
51};
52}
53
Chris Lattner3e928bb2005-01-07 07:47:09 +000054//===----------------------------------------------------------------------===//
55/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
56/// hacks on it until the target machine can handle it. This involves
57/// eliminating value sizes the machine cannot handle (promoting small sizes to
58/// large sizes or splitting up large values into small values) as well as
59/// eliminating operations the machine cannot handle.
60///
61/// This code also does a small amount of optimization and recognition of idioms
62/// as part of its processing. For example, if a target does not support a
63/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
64/// will attempt merge setcc and brc instructions into brcc's.
65///
66namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000067class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000068 TargetLowering &TLI;
69 SelectionDAG &DAG;
70
Chris Lattner6831a812006-02-13 09:18:02 +000071 // Libcall insertion helpers.
72
73 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
74 /// legalized. We use this to ensure that calls are properly serialized
75 /// against each other, including inserted libcalls.
76 SDOperand LastCALLSEQ_END;
77
78 /// IsLegalizingCall - This member is used *only* for purposes of providing
79 /// helpful assertions that a libcall isn't created while another call is
80 /// being legalized (which could lead to non-serialized call sequences).
81 bool IsLegalizingCall;
82
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000084 Legal, // The target natively supports this operation.
85 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000086 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000087 };
Chris Lattner6831a812006-02-13 09:18:02 +000088
Chris Lattner3e928bb2005-01-07 07:47:09 +000089 /// ValueTypeActions - This is a bitvector that contains two bits for each
90 /// value type, where the two bits correspond to the LegalizeAction enum.
91 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000092 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000093
Chris Lattner3e928bb2005-01-07 07:47:09 +000094 /// LegalizedNodes - For nodes that are of legal width, and that have more
95 /// than one use, this map indicates what regularized operand to use. This
96 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000097 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000098
Chris Lattner03c85462005-01-15 05:21:40 +000099 /// PromotedNodes - For nodes that are below legal width, and that have more
100 /// than one use, this map indicates what promoted value to use. This allows
101 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +0000102 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +0000103
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// ExpandedNodes - For nodes that need to be expanded this map indicates
105 /// which which operands are the expanded version of the input. This allows
106 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +0000107 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108
Chris Lattnerc7029802006-03-18 01:44:44 +0000109 /// SplitNodes - For vector nodes that need to be split, this map indicates
110 /// which which operands are the split version of the input. This allows us
111 /// to avoid splitting the same node more than once.
112 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
113
Dan Gohman7f321562007-06-25 16:23:39 +0000114 /// ScalarizedNodes - For nodes that need to be converted from vector types to
115 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000116 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000117 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000118
Chris Lattner8afc48e2005-01-07 22:28:47 +0000119 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000120 LegalizedNodes.insert(std::make_pair(From, To));
121 // If someone requests legalization of the new node, return itself.
122 if (From != To)
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000124 }
Chris Lattner03c85462005-01-15 05:21:40 +0000125 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000126 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000127 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000128 // If someone requests legalization of the new node, return itself.
129 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000130 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000131
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132public:
133
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000134 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000135
Chris Lattner3e928bb2005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
139 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
145 bool isTypeLegal(MVT::ValueType VT) const {
146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner456a93a2006-01-28 07:39:30 +0000151private:
Dan Gohman7f321562007-06-25 16:23:39 +0000152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000153 /// appropriate for its type.
154 void HandleOp(SDOperand Op);
155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000159 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000160
161 /// PromoteOp - Given an operation that produces a value in an invalid type,
162 /// promote it to compute the value into a larger type. The produced value
163 /// will have the correct bits for the low portion of the register, but no
164 /// guarantee is made about the top bits: it may be zero, sign-extended, or
165 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000166 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000167
Chris Lattnerc7029802006-03-18 01:44:44 +0000168 /// ExpandOp - Expand the specified SDOperand into its two component pieces
169 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
170 /// the LegalizeNodes map is filled in for any results that are not expanded,
171 /// the ExpandedNodes map is filled in for any results that are expanded, and
172 /// the Lo/Hi values are returned. This applies to integer types and Vector
173 /// types.
174 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
Dan Gohman7f321562007-06-25 16:23:39 +0000176 /// SplitVectorOp - Given an operand of vector type, break it down into
177 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000178 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
179
Dan Gohman89b20c02007-06-27 14:06:22 +0000180 /// ScalarizeVectorOp - Given an operand of single-element vector type
181 /// (e.g. v1f32), convert it into the equivalent operation that returns a
182 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000183 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000184
Chris Lattner4352cc92006-04-04 17:23:26 +0000185 /// isShuffleLegal - Return true if a vector shuffle is legal with the
186 /// specified mask and type. Targets can specify exactly which masks they
187 /// support and the code generator is tasked with not creating illegal masks.
188 ///
189 /// Note that this will also return true for shuffles that are promoted to a
190 /// different type.
191 ///
192 /// If this is a legal shuffle, this method returns the (possibly promoted)
193 /// build_vector Mask. If it's not a legal shuffle, it returns null.
194 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
195
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000196 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000197 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000198
Nate Begeman750ac1b2006-02-01 07:19:44 +0000199 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
200
Chris Lattnerce872152006-03-19 06:31:19 +0000201 SDOperand CreateStackTemporary(MVT::ValueType VT);
202
Reid Spencer47857812006-12-31 05:55:36 +0000203 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000204 SDOperand &Hi);
205 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
206 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000207
Chris Lattner35481892005-12-23 00:16:34 +0000208 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000209 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000211 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
212 SDOperand LegalOp,
213 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000214 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
215 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000216 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
217 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000218
Chris Lattner456a93a2006-01-28 07:39:30 +0000219 SDOperand ExpandBSWAP(SDOperand Op);
220 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000221 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
222 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000223 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
224 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000225
Dan Gohman7f321562007-06-25 16:23:39 +0000226 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000227 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000228
Chris Lattner3e928bb2005-01-07 07:47:09 +0000229 SDOperand getIntPtrConstant(uint64_t Val) {
230 return DAG.getConstant(Val, TLI.getPointerTy());
231 }
232};
233}
234
Chris Lattner4352cc92006-04-04 17:23:26 +0000235/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
236/// specified mask and type. Targets can specify exactly which masks they
237/// support and the code generator is tasked with not creating illegal masks.
238///
239/// Note that this will also return true for shuffles that are promoted to a
240/// different type.
241SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
242 SDOperand Mask) const {
243 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
244 default: return 0;
245 case TargetLowering::Legal:
246 case TargetLowering::Custom:
247 break;
248 case TargetLowering::Promote: {
249 // If this is promoted to a different type, convert the shuffle mask and
250 // ask if it is legal in the promoted type!
251 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
252
253 // If we changed # elements, change the shuffle mask.
254 unsigned NumEltsGrowth =
255 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
256 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
257 if (NumEltsGrowth > 1) {
258 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000259 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000260 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
261 SDOperand InOp = Mask.getOperand(i);
262 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
263 if (InOp.getOpcode() == ISD::UNDEF)
264 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
265 else {
266 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
267 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
268 }
269 }
270 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000271 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000272 }
273 VT = NVT;
274 break;
275 }
276 }
277 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
278}
279
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000280SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
281 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
282 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000283 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000285}
286
Chris Lattnere10e6f72007-06-18 21:28:10 +0000287/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
288/// contains all of a nodes operands before it contains the node.
289static void ComputeTopDownOrdering(SelectionDAG &DAG,
290 SmallVector<SDNode*, 64> &Order) {
291
292 DenseMap<SDNode*, unsigned> Visited;
293 std::vector<SDNode*> Worklist;
294 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000295
Chris Lattnere10e6f72007-06-18 21:28:10 +0000296 // Compute ordering from all of the leaves in the graphs, those (like the
297 // entry node) that have no operands.
298 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
299 E = DAG.allnodes_end(); I != E; ++I) {
300 if (I->getNumOperands() == 0) {
301 Visited[I] = 0 - 1U;
302 Worklist.push_back(I);
303 }
Chris Lattner32fca002005-10-06 01:20:27 +0000304 }
305
Chris Lattnere10e6f72007-06-18 21:28:10 +0000306 while (!Worklist.empty()) {
307 SDNode *N = Worklist.back();
308 Worklist.pop_back();
309
310 if (++Visited[N] != N->getNumOperands())
311 continue; // Haven't visited all operands yet
312
313 Order.push_back(N);
314
315 // Now that we have N in, add anything that uses it if all of their operands
316 // are now done.
317 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
318 UI != E; ++UI)
319 Worklist.push_back(*UI);
320 }
321
322 assert(Order.size() == Visited.size() &&
323 Order.size() ==
324 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
325 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000326}
327
Chris Lattner1618beb2005-07-29 00:11:56 +0000328
Chris Lattner3e928bb2005-01-07 07:47:09 +0000329void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000330 LastCALLSEQ_END = DAG.getEntryNode();
331 IsLegalizingCall = false;
332
Chris Lattnerab510a72005-10-02 17:49:46 +0000333 // The legalize process is inherently a bottom-up recursive process (users
334 // legalize their uses before themselves). Given infinite stack space, we
335 // could just start legalizing on the root and traverse the whole graph. In
336 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000337 // blocks. To avoid this problem, compute an ordering of the nodes where each
338 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000339 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000340 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000341
Chris Lattnerc7029802006-03-18 01:44:44 +0000342 for (unsigned i = 0, e = Order.size(); i != e; ++i)
343 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000344
345 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000347 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
348 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349
350 ExpandedNodes.clear();
351 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000352 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000353 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000354 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355
356 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000357 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358}
359
Chris Lattner6831a812006-02-13 09:18:02 +0000360
361/// FindCallEndFromCallStart - Given a chained node that is part of a call
362/// sequence, find the CALLSEQ_END node that terminates the call sequence.
363static SDNode *FindCallEndFromCallStart(SDNode *Node) {
364 if (Node->getOpcode() == ISD::CALLSEQ_END)
365 return Node;
366 if (Node->use_empty())
367 return 0; // No CallSeqEnd
368
369 // The chain is usually at the end.
370 SDOperand TheChain(Node, Node->getNumValues()-1);
371 if (TheChain.getValueType() != MVT::Other) {
372 // Sometimes it's at the beginning.
373 TheChain = SDOperand(Node, 0);
374 if (TheChain.getValueType() != MVT::Other) {
375 // Otherwise, hunt for it.
376 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
377 if (Node->getValueType(i) == MVT::Other) {
378 TheChain = SDOperand(Node, i);
379 break;
380 }
381
382 // Otherwise, we walked into a node without a chain.
383 if (TheChain.getValueType() != MVT::Other)
384 return 0;
385 }
386 }
387
388 for (SDNode::use_iterator UI = Node->use_begin(),
389 E = Node->use_end(); UI != E; ++UI) {
390
391 // Make sure to only follow users of our token chain.
392 SDNode *User = *UI;
393 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
394 if (User->getOperand(i) == TheChain)
395 if (SDNode *Result = FindCallEndFromCallStart(User))
396 return Result;
397 }
398 return 0;
399}
400
401/// FindCallStartFromCallEnd - Given a chained node that is part of a call
402/// sequence, find the CALLSEQ_START node that initiates the call sequence.
403static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
404 assert(Node && "Didn't find callseq_start for a call??");
405 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
406
407 assert(Node->getOperand(0).getValueType() == MVT::Other &&
408 "Node doesn't have a token chain argument!");
409 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
410}
411
412/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
413/// see if any uses can reach Dest. If no dest operands can get to dest,
414/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000415///
416/// Keep track of the nodes we fine that actually do lead to Dest in
417/// NodesLeadingTo. This avoids retraversing them exponential number of times.
418///
419bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000420 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000421 if (N == Dest) return true; // N certainly leads to Dest :)
422
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000423 // If we've already processed this node and it does lead to Dest, there is no
424 // need to reprocess it.
425 if (NodesLeadingTo.count(N)) return true;
426
Chris Lattner6831a812006-02-13 09:18:02 +0000427 // If the first result of this node has been already legalized, then it cannot
428 // reach N.
429 switch (getTypeAction(N->getValueType(0))) {
430 case Legal:
431 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 case Promote:
434 if (PromotedNodes.count(SDOperand(N, 0))) return false;
435 break;
436 case Expand:
437 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
438 break;
439 }
440
441 // Okay, this node has not already been legalized. Check and legalize all
442 // operands. If none lead to Dest, then we can legalize this node.
443 bool OperandsLeadToDest = false;
444 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
445 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000446 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000447
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000448 if (OperandsLeadToDest) {
449 NodesLeadingTo.insert(N);
450 return true;
451 }
Chris Lattner6831a812006-02-13 09:18:02 +0000452
453 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000454 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000455 return false;
456}
457
Dan Gohman7f321562007-06-25 16:23:39 +0000458/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000459/// appropriate for its type.
460void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000461 MVT::ValueType VT = Op.getValueType();
462 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000463 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000464 case Legal: (void)LegalizeOp(Op); break;
465 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000466 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000467 if (!MVT::isVector(VT)) {
468 // If this is an illegal scalar, expand it into its two component
469 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000470 SDOperand X, Y;
471 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000472 } else if (MVT::getVectorNumElements(VT) == 1) {
473 // If this is an illegal single element vector, convert it to a
474 // scalar operation.
475 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000476 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000477 // Otherwise, this is an illegal multiple element vector.
478 // Split it in half and legalize both parts.
479 SDOperand X, Y;
480 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000481 }
482 break;
483 }
484}
Chris Lattner6831a812006-02-13 09:18:02 +0000485
Evan Cheng9f877882006-12-13 20:57:08 +0000486/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
487/// a load from the constant pool.
488static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000489 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000490 bool Extend = false;
491
492 // If a FP immediate is precise when represented as a float and if the
493 // target can do an extending load from float to double, we put it into
494 // the constant pool as a float, even if it's is statically typed as a
495 // double.
496 MVT::ValueType VT = CFP->getValueType(0);
497 bool isDouble = VT == MVT::f64;
498 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
499 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000500 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000501 double Val = LLVMC->getValue();
502 return isDouble
503 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
504 : DAG.getConstant(FloatToBits(Val), MVT::i32);
505 }
506
Evan Cheng00495212006-12-12 21:32:44 +0000507 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
508 // Only do this if the target has a native EXTLOAD instruction from f32.
509 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
510 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
511 VT = MVT::f32;
512 Extend = true;
513 }
514
515 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
516 if (Extend) {
517 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
518 CPIdx, NULL, 0, MVT::f32);
519 } else {
520 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
521 }
522}
523
Chris Lattner6831a812006-02-13 09:18:02 +0000524
Evan Cheng912095b2007-01-04 21:56:39 +0000525/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
526/// operations.
527static
528SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
529 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000530 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000531 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000532 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
533 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000534 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000535
Evan Cheng912095b2007-01-04 21:56:39 +0000536 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000537 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000538 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
539 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000541 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000542 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000543 // Shift right or sign-extend it if the two operands have different types.
544 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
545 if (SizeDiff > 0) {
546 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
547 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
548 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
549 } else if (SizeDiff < 0)
550 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000551
552 // Clear the sign bit of first operand.
553 SDOperand Mask2 = (VT == MVT::f64)
554 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
555 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
556 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000557 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000558 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
559
560 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000561 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
562 return Result;
563}
564
565
Chris Lattnerc7029802006-03-18 01:44:44 +0000566/// LegalizeOp - We know that the specified value has a legal type.
567/// Recursively ensure that the operands have legal types, then return the
568/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000570 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000571 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000572 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000573
Chris Lattner3e928bb2005-01-07 07:47:09 +0000574 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000575 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000576 if (Node->getNumValues() > 1) {
577 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000578 if (getTypeAction(Node->getValueType(i)) != Legal) {
579 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000581 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000582 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000583 }
584 }
585
Chris Lattner45982da2005-05-12 16:53:42 +0000586 // Note that LegalizeOp may be reentered even from single-use nodes, which
587 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000588 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000589 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000590
Nate Begeman9373a812005-08-10 20:51:12 +0000591 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000592 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000593 bool isCustom = false;
594
Chris Lattner3e928bb2005-01-07 07:47:09 +0000595 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000596 case ISD::FrameIndex:
597 case ISD::EntryToken:
598 case ISD::Register:
599 case ISD::BasicBlock:
600 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000601 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000602 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000603 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000604 case ISD::TargetConstantPool:
605 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000606 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000607 case ISD::TargetExternalSymbol:
608 case ISD::VALUETYPE:
609 case ISD::SRCVALUE:
610 case ISD::STRING:
611 case ISD::CONDCODE:
612 // Primitives must all be legal.
613 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
614 "This must be legal!");
615 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000616 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000617 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
618 // If this is a target node, legalize it by legalizing the operands then
619 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000620 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000621 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000622 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000623
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000624 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000625
626 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
627 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
628 return Result.getValue(Op.ResNo);
629 }
630 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000631#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000632 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000633#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000634 assert(0 && "Do not know how to legalize this operator!");
635 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000636 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000637 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000638 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000639 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000640 case ISD::ConstantPool:
641 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000642 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
643 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000644 case TargetLowering::Custom:
645 Tmp1 = TLI.LowerOperation(Op, DAG);
646 if (Tmp1.Val) Result = Tmp1;
647 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000648 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000649 break;
650 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000651 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000652 case ISD::FRAMEADDR:
653 case ISD::RETURNADDR:
654 // The only option for these nodes is to custom lower them. If the target
655 // does not custom lower them, then return zero.
656 Tmp1 = TLI.LowerOperation(Op, DAG);
657 if (Tmp1.Val)
658 Result = Tmp1;
659 else
660 Result = DAG.getConstant(0, TLI.getPointerTy());
661 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000662 case ISD::EXCEPTIONADDR: {
663 Tmp1 = LegalizeOp(Node->getOperand(0));
664 MVT::ValueType VT = Node->getValueType(0);
665 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
666 default: assert(0 && "This action is not supported yet!");
667 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000668 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000669 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
670 }
671 break;
672 case TargetLowering::Custom:
673 Result = TLI.LowerOperation(Op, DAG);
674 if (Result.Val) break;
675 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000676 case TargetLowering::Legal: {
677 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
678 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
679 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000680 break;
681 }
682 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000683 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000684 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000685 case ISD::EHSELECTION: {
686 Tmp1 = LegalizeOp(Node->getOperand(0));
687 Tmp2 = LegalizeOp(Node->getOperand(1));
688 MVT::ValueType VT = Node->getValueType(0);
689 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
690 default: assert(0 && "This action is not supported yet!");
691 case TargetLowering::Expand: {
692 unsigned Reg = TLI.getExceptionSelectorRegister();
693 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
694 }
695 break;
696 case TargetLowering::Custom:
697 Result = TLI.LowerOperation(Op, DAG);
698 if (Result.Val) break;
699 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000700 case TargetLowering::Legal: {
701 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
702 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
703 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000704 break;
705 }
706 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000707 }
Jim Laskey8782d482007-02-28 20:43:58 +0000708 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000709 case ISD::AssertSext:
710 case ISD::AssertZext:
711 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000713 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000714 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000715 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000716 Result = Node->getOperand(Op.ResNo);
717 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000718 case ISD::CopyFromReg:
719 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000720 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000721 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000722 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000723 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000724 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000725 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000726 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000727 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
728 } else {
729 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
730 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000731 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
732 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000733 // Since CopyFromReg produces two values, make sure to remember that we
734 // legalized both of them.
735 AddLegalizedOperand(Op.getValue(0), Result);
736 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
737 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000738 case ISD::UNDEF: {
739 MVT::ValueType VT = Op.getValueType();
740 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000741 default: assert(0 && "This action is not supported yet!");
742 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000743 if (MVT::isInteger(VT))
744 Result = DAG.getConstant(0, VT);
745 else if (MVT::isFloatingPoint(VT))
746 Result = DAG.getConstantFP(0, VT);
747 else
748 assert(0 && "Unknown value type!");
749 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000750 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000751 break;
752 }
753 break;
754 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000755
Chris Lattner48b61a72006-03-28 00:40:33 +0000756 case ISD::INTRINSIC_W_CHAIN:
757 case ISD::INTRINSIC_WO_CHAIN:
758 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000759 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000760 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
761 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000762 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000763
764 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000765 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000766 TargetLowering::Custom) {
767 Tmp3 = TLI.LowerOperation(Result, DAG);
768 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000769 }
770
771 if (Result.Val->getNumValues() == 1) break;
772
773 // Must have return value and chain result.
774 assert(Result.Val->getNumValues() == 2 &&
775 "Cannot return more than two values!");
776
777 // Since loads produce two values, make sure to remember that we
778 // legalized both of them.
779 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
780 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
781 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000782 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000783
784 case ISD::LOCATION:
785 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
787
788 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
789 case TargetLowering::Promote:
790 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000791 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000792 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000793 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000794 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000795
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000796 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000797 const std::string &FName =
798 cast<StringSDNode>(Node->getOperand(3))->getValue();
799 const std::string &DirName =
800 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000801 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000802
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000803 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000804 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000805 SDOperand LineOp = Node->getOperand(1);
806 SDOperand ColOp = Node->getOperand(2);
807
808 if (useDEBUG_LOC) {
809 Ops.push_back(LineOp); // line #
810 Ops.push_back(ColOp); // col #
811 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000812 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000813 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000814 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
815 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000816 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000817 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000818 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000819 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000820 } else {
821 Result = Tmp1; // chain
822 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000823 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000824 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000825 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000826 if (Tmp1 != Node->getOperand(0) ||
827 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000828 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000829 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000830 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
831 Ops.push_back(Node->getOperand(1)); // line # must be legal.
832 Ops.push_back(Node->getOperand(2)); // col # must be legal.
833 } else {
834 // Otherwise promote them.
835 Ops.push_back(PromoteOp(Node->getOperand(1)));
836 Ops.push_back(PromoteOp(Node->getOperand(2)));
837 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000838 Ops.push_back(Node->getOperand(3)); // filename must be legal.
839 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000840 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000841 }
842 break;
843 }
844 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000845
846 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000847 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000848 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000849 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000850 case TargetLowering::Legal:
851 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
852 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
853 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
854 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000855 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000856 break;
857 }
858 break;
859
Jim Laskey1ee29252007-01-26 14:34:52 +0000860 case ISD::LABEL:
861 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
862 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000863 default: assert(0 && "This action is not supported yet!");
864 case TargetLowering::Legal:
865 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
866 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000867 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000868 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000869 case TargetLowering::Expand:
870 Result = LegalizeOp(Node->getOperand(0));
871 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000872 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000873 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000874
Chris Lattner3e928bb2005-01-07 07:47:09 +0000875 case ISD::Constant:
876 // We know we don't need to expand constants here, constants only have one
877 // value and we check that it is fine above.
878
879 // FIXME: Maybe we should handle things like targets that don't support full
880 // 32-bit immediates?
881 break;
882 case ISD::ConstantFP: {
883 // Spill FP immediates to the constant pool if the target cannot directly
884 // codegen them. Targets often have some immediate values that can be
885 // efficiently generated into an FP register without a load. We explicitly
886 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000887 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
888
889 // Check to see if this FP immediate is already legal.
890 bool isLegal = false;
891 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
892 E = TLI.legal_fpimm_end(); I != E; ++I)
893 if (CFP->isExactlyValue(*I)) {
894 isLegal = true;
895 break;
896 }
897
Chris Lattner3181a772006-01-29 06:26:56 +0000898 // If this is a legal constant, turn it into a TargetConstantFP node.
899 if (isLegal) {
900 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
901 break;
902 }
903
904 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
905 default: assert(0 && "This action is not supported yet!");
906 case TargetLowering::Custom:
907 Tmp3 = TLI.LowerOperation(Result, DAG);
908 if (Tmp3.Val) {
909 Result = Tmp3;
910 break;
911 }
912 // FALLTHROUGH
913 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000914 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000915 }
916 break;
917 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000918 case ISD::TokenFactor:
919 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000920 Tmp1 = LegalizeOp(Node->getOperand(0));
921 Tmp2 = LegalizeOp(Node->getOperand(1));
922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
923 } else if (Node->getNumOperands() == 3) {
924 Tmp1 = LegalizeOp(Node->getOperand(0));
925 Tmp2 = LegalizeOp(Node->getOperand(1));
926 Tmp3 = LegalizeOp(Node->getOperand(2));
927 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000928 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000929 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000930 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000931 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
932 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000933 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000934 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000935 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000936
937 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000938 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000939 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000940 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
941 assert(Tmp3.Val && "Target didn't custom lower this node!");
942 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
943 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000944
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000945 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000946 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000947 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
948 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000949 if (Op.ResNo == i)
950 Tmp2 = Tmp1;
951 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
952 }
953 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000954
Chris Lattnerb2827b02006-03-19 00:52:58 +0000955 case ISD::BUILD_VECTOR:
956 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000957 default: assert(0 && "This action is not supported yet!");
958 case TargetLowering::Custom:
959 Tmp3 = TLI.LowerOperation(Result, DAG);
960 if (Tmp3.Val) {
961 Result = Tmp3;
962 break;
963 }
964 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000965 case TargetLowering::Expand:
966 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000967 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000968 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000969 break;
970 case ISD::INSERT_VECTOR_ELT:
971 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
972 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
973 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
975
976 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
977 Node->getValueType(0))) {
978 default: assert(0 && "This action is not supported yet!");
979 case TargetLowering::Legal:
980 break;
981 case TargetLowering::Custom:
982 Tmp3 = TLI.LowerOperation(Result, DAG);
983 if (Tmp3.Val) {
984 Result = Tmp3;
985 break;
986 }
987 // FALLTHROUGH
988 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000989 // If the insert index is a constant, codegen this as a scalar_to_vector,
990 // then a shuffle that inserts it into the right position in the vector.
991 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
992 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
993 Tmp1.getValueType(), Tmp2);
994
995 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
996 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +0000997 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +0000998
999 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1000 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1001 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001002 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001003 for (unsigned i = 0; i != NumElts; ++i) {
1004 if (i != InsertPos->getValue())
1005 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1006 else
1007 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1008 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001009 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1010 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001011
1012 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1013 Tmp1, ScVec, ShufMask);
1014 Result = LegalizeOp(Result);
1015 break;
1016 }
1017
Chris Lattner2332b9f2006-03-19 01:17:20 +00001018 // If the target doesn't support this, we have to spill the input vector
1019 // to a temporary stack slot, update the element, then reload it. This is
1020 // badness. We could also load the value into a vector register (either
1021 // with a "move to register" or "extload into register" instruction, then
1022 // permute it into place, if the idx is a constant and if the idx is
1023 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001024 MVT::ValueType VT = Tmp1.getValueType();
1025 MVT::ValueType EltVT = Tmp2.getValueType();
1026 MVT::ValueType IdxVT = Tmp3.getValueType();
1027 MVT::ValueType PtrVT = TLI.getPointerTy();
1028 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001029 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001030 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001031
1032 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001033 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1034 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001035 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001036 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1037 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1038 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001039 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001040 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001041 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001042 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001043 break;
1044 }
1045 }
1046 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001047 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001048 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1049 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1050 break;
1051 }
1052
Chris Lattnerce872152006-03-19 06:31:19 +00001053 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1054 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1055 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1056 Node->getValueType(0))) {
1057 default: assert(0 && "This action is not supported yet!");
1058 case TargetLowering::Legal:
1059 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001060 case TargetLowering::Custom:
1061 Tmp3 = TLI.LowerOperation(Result, DAG);
1062 if (Tmp3.Val) {
1063 Result = Tmp3;
1064 break;
1065 }
1066 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001067 case TargetLowering::Expand:
1068 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001069 break;
1070 }
Chris Lattnerce872152006-03-19 06:31:19 +00001071 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001072 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1074 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1076
1077 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001078 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1079 default: assert(0 && "Unknown operation action!");
1080 case TargetLowering::Legal:
1081 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1082 "vector shuffle should not be created if not legal!");
1083 break;
1084 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001085 Tmp3 = TLI.LowerOperation(Result, DAG);
1086 if (Tmp3.Val) {
1087 Result = Tmp3;
1088 break;
1089 }
1090 // FALLTHROUGH
1091 case TargetLowering::Expand: {
1092 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001093 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001094 MVT::ValueType PtrVT = TLI.getPointerTy();
1095 SDOperand Mask = Node->getOperand(2);
1096 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001097 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001098 for (unsigned i = 0; i != NumElems; ++i) {
1099 SDOperand Arg = Mask.getOperand(i);
1100 if (Arg.getOpcode() == ISD::UNDEF) {
1101 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1102 } else {
1103 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1104 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1105 if (Idx < NumElems)
1106 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1107 DAG.getConstant(Idx, PtrVT)));
1108 else
1109 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1110 DAG.getConstant(Idx - NumElems, PtrVT)));
1111 }
1112 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001113 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001114 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001115 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001116 case TargetLowering::Promote: {
1117 // Change base type to a different vector type.
1118 MVT::ValueType OVT = Node->getValueType(0);
1119 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1120
1121 // Cast the two input vectors.
1122 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1123 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1124
1125 // Convert the shuffle mask to the right # elements.
1126 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1127 assert(Tmp3.Val && "Shuffle not legal?");
1128 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1129 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1130 break;
1131 }
Chris Lattner87100e02006-03-20 01:52:29 +00001132 }
1133 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001134
1135 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001136 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001137 Tmp2 = LegalizeOp(Node->getOperand(1));
1138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001139 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001140 break;
1141
Dan Gohman7f321562007-06-25 16:23:39 +00001142 case ISD::EXTRACT_SUBVECTOR:
1143 Tmp1 = Node->getOperand(0);
1144 Tmp2 = LegalizeOp(Node->getOperand(1));
1145 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1146 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001147 break;
1148
Chris Lattner6831a812006-02-13 09:18:02 +00001149 case ISD::CALLSEQ_START: {
1150 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1151
1152 // Recursively Legalize all of the inputs of the call end that do not lead
1153 // to this call start. This ensures that any libcalls that need be inserted
1154 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001155 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001156 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001157 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1158 NodesLeadingTo);
1159 }
Chris Lattner6831a812006-02-13 09:18:02 +00001160
1161 // Now that we legalized all of the inputs (which may have inserted
1162 // libcalls) create the new CALLSEQ_START node.
1163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1164
1165 // Merge in the last call, to ensure that this call start after the last
1166 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001167 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001168 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1169 Tmp1 = LegalizeOp(Tmp1);
1170 }
Chris Lattner6831a812006-02-13 09:18:02 +00001171
1172 // Do not try to legalize the target-specific arguments (#1+).
1173 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001174 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001175 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001177 }
1178
1179 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001180 AddLegalizedOperand(Op.getValue(0), Result);
1181 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1182 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1183
Chris Lattner6831a812006-02-13 09:18:02 +00001184 // Now that the callseq_start and all of the non-call nodes above this call
1185 // sequence have been legalized, legalize the call itself. During this
1186 // process, no libcalls can/will be inserted, guaranteeing that no calls
1187 // can overlap.
1188 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1189 SDOperand InCallSEQ = LastCALLSEQ_END;
1190 // Note that we are selecting this call!
1191 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1192 IsLegalizingCall = true;
1193
1194 // Legalize the call, starting from the CALLSEQ_END.
1195 LegalizeOp(LastCALLSEQ_END);
1196 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1197 return Result;
1198 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001199 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001200 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1201 // will cause this node to be legalized as well as handling libcalls right.
1202 if (LastCALLSEQ_END.Val != Node) {
1203 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001204 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001205 assert(I != LegalizedNodes.end() &&
1206 "Legalizing the call start should have legalized this node!");
1207 return I->second;
1208 }
1209
1210 // Otherwise, the call start has been legalized and everything is going
1211 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001212 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001213 // Do not try to legalize the target-specific arguments (#1+), except for
1214 // an optional flag input.
1215 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1216 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001217 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001218 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001219 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001220 }
1221 } else {
1222 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1223 if (Tmp1 != Node->getOperand(0) ||
1224 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001225 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001226 Ops[0] = Tmp1;
1227 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001228 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001229 }
Chris Lattner6a542892006-01-24 05:48:21 +00001230 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001231 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001232 // This finishes up call legalization.
1233 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001234
1235 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1236 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1237 if (Node->getNumValues() == 2)
1238 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1239 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001240 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1242 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1243 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001245
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001246 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001247 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001248 switch (TLI.getOperationAction(Node->getOpcode(),
1249 Node->getValueType(0))) {
1250 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001251 case TargetLowering::Expand: {
1252 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1253 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1254 " not tell us which reg is the stack pointer!");
1255 SDOperand Chain = Tmp1.getOperand(0);
1256 SDOperand Size = Tmp2.getOperand(1);
1257 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1258 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1259 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001260 Tmp1 = LegalizeOp(Tmp1);
1261 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001262 break;
1263 }
1264 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001265 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1266 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001267 Tmp1 = LegalizeOp(Tmp3);
1268 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001269 }
Chris Lattner903d2782006-01-15 08:54:32 +00001270 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001271 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001272 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001273 }
Chris Lattner903d2782006-01-15 08:54:32 +00001274 // Since this op produce two values, make sure to remember that we
1275 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001276 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1277 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001278 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001279 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001280 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001281 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001282 bool Changed = false;
1283 // Legalize all of the operands of the inline asm, in case they are nodes
1284 // that need to be expanded or something. Note we skip the asm string and
1285 // all of the TargetConstant flags.
1286 SDOperand Op = LegalizeOp(Ops[0]);
1287 Changed = Op != Ops[0];
1288 Ops[0] = Op;
1289
1290 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1291 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1292 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1293 for (++i; NumVals; ++i, --NumVals) {
1294 SDOperand Op = LegalizeOp(Ops[i]);
1295 if (Op != Ops[i]) {
1296 Changed = true;
1297 Ops[i] = Op;
1298 }
1299 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001300 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001301
1302 if (HasInFlag) {
1303 Op = LegalizeOp(Ops.back());
1304 Changed |= Op != Ops.back();
1305 Ops.back() = Op;
1306 }
1307
1308 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001309 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001310
1311 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001312 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001313 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1314 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001315 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001316 case ISD::BR:
1317 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001318 // Ensure that libcalls are emitted before a branch.
1319 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1320 Tmp1 = LegalizeOp(Tmp1);
1321 LastCALLSEQ_END = DAG.getEntryNode();
1322
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001324 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001325 case ISD::BRIND:
1326 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1327 // Ensure that libcalls are emitted before a branch.
1328 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1329 Tmp1 = LegalizeOp(Tmp1);
1330 LastCALLSEQ_END = DAG.getEntryNode();
1331
1332 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1333 default: assert(0 && "Indirect target must be legal type (pointer)!");
1334 case Legal:
1335 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1336 break;
1337 }
1338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1339 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001340 case ISD::BR_JT:
1341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1342 // Ensure that libcalls are emitted before a branch.
1343 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1344 Tmp1 = LegalizeOp(Tmp1);
1345 LastCALLSEQ_END = DAG.getEntryNode();
1346
1347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1349
1350 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1351 default: assert(0 && "This action is not supported yet!");
1352 case TargetLowering::Legal: break;
1353 case TargetLowering::Custom:
1354 Tmp1 = TLI.LowerOperation(Result, DAG);
1355 if (Tmp1.Val) Result = Tmp1;
1356 break;
1357 case TargetLowering::Expand: {
1358 SDOperand Chain = Result.getOperand(0);
1359 SDOperand Table = Result.getOperand(1);
1360 SDOperand Index = Result.getOperand(2);
1361
1362 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001363 MachineFunction &MF = DAG.getMachineFunction();
1364 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001365 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1366 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001367
1368 SDOperand LD;
1369 switch (EntrySize) {
1370 default: assert(0 && "Size of jump table not supported yet."); break;
1371 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1372 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1373 }
1374
1375 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001376 // For PIC, the sequence is:
1377 // BRIND(load(Jumptable + index) + RelocBase)
1378 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1379 SDOperand Reloc;
1380 if (TLI.usesGlobalOffsetTable())
1381 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1382 else
1383 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001384 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001385 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1386 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1387 } else {
1388 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1389 }
1390 }
1391 }
1392 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001393 case ISD::BRCOND:
1394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001395 // Ensure that libcalls are emitted before a return.
1396 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1397 Tmp1 = LegalizeOp(Tmp1);
1398 LastCALLSEQ_END = DAG.getEntryNode();
1399
Chris Lattner47e92232005-01-18 19:27:06 +00001400 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1401 case Expand: assert(0 && "It's impossible to expand bools");
1402 case Legal:
1403 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1404 break;
1405 case Promote:
1406 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001407
1408 // The top bits of the promoted condition are not necessarily zero, ensure
1409 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001410 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001411 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1412 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001413 break;
1414 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001415
1416 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001418
1419 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1420 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001421 case TargetLowering::Legal: break;
1422 case TargetLowering::Custom:
1423 Tmp1 = TLI.LowerOperation(Result, DAG);
1424 if (Tmp1.Val) Result = Tmp1;
1425 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001426 case TargetLowering::Expand:
1427 // Expand brcond's setcc into its constituent parts and create a BR_CC
1428 // Node.
1429 if (Tmp2.getOpcode() == ISD::SETCC) {
1430 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1431 Tmp2.getOperand(0), Tmp2.getOperand(1),
1432 Node->getOperand(2));
1433 } else {
1434 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1435 DAG.getCondCode(ISD::SETNE), Tmp2,
1436 DAG.getConstant(0, Tmp2.getValueType()),
1437 Node->getOperand(2));
1438 }
1439 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001440 }
1441 break;
1442 case ISD::BR_CC:
1443 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001444 // Ensure that libcalls are emitted before a branch.
1445 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1446 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001447 Tmp2 = Node->getOperand(2); // LHS
1448 Tmp3 = Node->getOperand(3); // RHS
1449 Tmp4 = Node->getOperand(1); // CC
1450
1451 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001452 LastCALLSEQ_END = DAG.getEntryNode();
1453
Nate Begeman750ac1b2006-02-01 07:19:44 +00001454 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1455 // the LHS is a legal SETCC itself. In this case, we need to compare
1456 // the result against zero to select between true and false values.
1457 if (Tmp3.Val == 0) {
1458 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1459 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001460 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001461
1462 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1463 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001464
Chris Lattner181b7a32005-12-17 23:46:46 +00001465 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1466 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001467 case TargetLowering::Legal: break;
1468 case TargetLowering::Custom:
1469 Tmp4 = TLI.LowerOperation(Result, DAG);
1470 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001471 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001472 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001473 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001474 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001475 LoadSDNode *LD = cast<LoadSDNode>(Node);
1476 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1477 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001478
Evan Cheng466685d2006-10-09 20:57:25 +00001479 ISD::LoadExtType ExtType = LD->getExtensionType();
1480 if (ExtType == ISD::NON_EXTLOAD) {
1481 MVT::ValueType VT = Node->getValueType(0);
1482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1483 Tmp3 = Result.getValue(0);
1484 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001485
Evan Cheng466685d2006-10-09 20:57:25 +00001486 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1487 default: assert(0 && "This action is not supported yet!");
1488 case TargetLowering::Legal: break;
1489 case TargetLowering::Custom:
1490 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1491 if (Tmp1.Val) {
1492 Tmp3 = LegalizeOp(Tmp1);
1493 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001494 }
Evan Cheng466685d2006-10-09 20:57:25 +00001495 break;
1496 case TargetLowering::Promote: {
1497 // Only promote a load of vector type to another.
1498 assert(MVT::isVector(VT) && "Cannot promote this load!");
1499 // Change base type to a different vector type.
1500 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1501
1502 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001503 LD->getSrcValueOffset(),
1504 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001505 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1506 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001507 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001508 }
Evan Cheng466685d2006-10-09 20:57:25 +00001509 }
1510 // Since loads produce two values, make sure to remember that we
1511 // legalized both of them.
1512 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1513 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1514 return Op.ResNo ? Tmp4 : Tmp3;
1515 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001516 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001517 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1518 default: assert(0 && "This action is not supported yet!");
1519 case TargetLowering::Promote:
1520 assert(SrcVT == MVT::i1 &&
1521 "Can only promote extending LOAD from i1 -> i8!");
1522 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1523 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001524 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001525 Tmp1 = Result.getValue(0);
1526 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001527 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001528 case TargetLowering::Custom:
1529 isCustom = true;
1530 // FALLTHROUGH
1531 case TargetLowering::Legal:
1532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1533 Tmp1 = Result.getValue(0);
1534 Tmp2 = Result.getValue(1);
1535
1536 if (isCustom) {
1537 Tmp3 = TLI.LowerOperation(Result, DAG);
1538 if (Tmp3.Val) {
1539 Tmp1 = LegalizeOp(Tmp3);
1540 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1541 }
1542 }
1543 break;
1544 case TargetLowering::Expand:
1545 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1546 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1547 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001548 LD->getSrcValueOffset(),
1549 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001550 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1551 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1552 Tmp2 = LegalizeOp(Load.getValue(1));
1553 break;
1554 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001555 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001556 // Turn the unsupported load into an EXTLOAD followed by an explicit
1557 // zero/sign extend inreg.
1558 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1559 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001560 LD->getSrcValueOffset(), SrcVT,
1561 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001562 SDOperand ValRes;
1563 if (ExtType == ISD::SEXTLOAD)
1564 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1565 Result, DAG.getValueType(SrcVT));
1566 else
1567 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1568 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1569 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1570 break;
1571 }
1572 // Since loads produce two values, make sure to remember that we legalized
1573 // both of them.
1574 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1575 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1576 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001577 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001578 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001579 case ISD::EXTRACT_ELEMENT: {
1580 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1581 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001582 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001583 case Legal:
1584 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1585 // 1 -> Hi
1586 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1587 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1588 TLI.getShiftAmountTy()));
1589 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1590 } else {
1591 // 0 -> Lo
1592 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1593 Node->getOperand(0));
1594 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001595 break;
1596 case Expand:
1597 // Get both the low and high parts.
1598 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1599 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1600 Result = Tmp2; // 1 -> Hi
1601 else
1602 Result = Tmp1; // 0 -> Lo
1603 break;
1604 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001605 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001606 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001607
1608 case ISD::CopyToReg:
1609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001610
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001611 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001612 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001613 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001614 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001615 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001616 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001617 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001618 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001619 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001620 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1622 Tmp3);
1623 } else {
1624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001625 }
1626
1627 // Since this produces two values, make sure to remember that we legalized
1628 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001629 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001630 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001631 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001632 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001633 break;
1634
1635 case ISD::RET:
1636 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001637
1638 // Ensure that libcalls are emitted before a return.
1639 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1640 Tmp1 = LegalizeOp(Tmp1);
1641 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001642
Chris Lattner3e928bb2005-01-07 07:47:09 +00001643 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001644 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001645 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001646 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001647 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001648 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001649 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001650 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001651 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001652 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001653 SDOperand Lo, Hi;
1654 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001655
1656 // Big endian systems want the hi reg first.
1657 if (!TLI.isLittleEndian())
1658 std::swap(Lo, Hi);
1659
Evan Cheng13acce32006-12-11 19:27:14 +00001660 if (Hi.Val)
1661 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1662 else
1663 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001664 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001665 } else {
1666 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001667 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1668 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001669
Dan Gohman7f321562007-06-25 16:23:39 +00001670 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001671 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001672 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001673 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001674 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001675 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001676 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001677 } else if (NumElems == 1) {
1678 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001679 Tmp2 = ScalarizeVectorOp(Tmp2);
1680 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001681 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001682
1683 // FIXME: Returns of gcc generic vectors smaller than a legal type
1684 // should be returned in integer registers!
1685
Chris Lattnerf87324e2006-04-11 01:31:51 +00001686 // The scalarized value type may not be legal, e.g. it might require
1687 // promotion or expansion. Relegalize the return.
1688 Result = LegalizeOp(Result);
1689 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001690 // FIXME: Returns of gcc generic vectors larger than a legal vector
1691 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001692 SDOperand Lo, Hi;
1693 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001694 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001695 Result = LegalizeOp(Result);
1696 }
1697 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001698 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001699 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001700 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001702 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001703 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001704 }
1705 break;
1706 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001707 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001708 break;
1709 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001710 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001711 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001712 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001713 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1714 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001715 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001716 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001717 break;
1718 case Expand: {
1719 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001720 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001721 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001722 ExpandOp(Node->getOperand(i), Lo, Hi);
1723 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001724 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001725 if (Hi.Val) {
1726 NewValues.push_back(Hi);
1727 NewValues.push_back(Node->getOperand(i+1));
1728 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001729 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 }
1731 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001732 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001733 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001734
1735 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001736 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001737 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001738 Result = DAG.getNode(ISD::RET, MVT::Other,
1739 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001740 break;
1741 }
1742 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001743
Chris Lattner6862dbc2006-01-29 21:02:23 +00001744 if (Result.getOpcode() == ISD::RET) {
1745 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1746 default: assert(0 && "This action is not supported yet!");
1747 case TargetLowering::Legal: break;
1748 case TargetLowering::Custom:
1749 Tmp1 = TLI.LowerOperation(Result, DAG);
1750 if (Tmp1.Val) Result = Tmp1;
1751 break;
1752 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001753 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001754 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001755 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001756 StoreSDNode *ST = cast<StoreSDNode>(Node);
1757 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1758 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001759 int SVOffset = ST->getSrcValueOffset();
1760 unsigned Alignment = ST->getAlignment();
1761 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001762
Evan Cheng8b2794a2006-10-13 21:14:26 +00001763 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001764 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1765 // FIXME: We shouldn't do this for TargetConstantFP's.
1766 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1767 // to phase ordering between legalized code and the dag combiner. This
1768 // probably means that we need to integrate dag combiner and legalizer
1769 // together.
1770 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1771 if (CFP->getValueType(0) == MVT::f32) {
1772 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1773 } else {
1774 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1775 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1776 }
1777 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001778 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001779 break;
1780 }
1781
Evan Cheng8b2794a2006-10-13 21:14:26 +00001782 switch (getTypeAction(ST->getStoredVT())) {
1783 case Legal: {
1784 Tmp3 = LegalizeOp(ST->getValue());
1785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1786 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001787
Evan Cheng8b2794a2006-10-13 21:14:26 +00001788 MVT::ValueType VT = Tmp3.getValueType();
1789 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1790 default: assert(0 && "This action is not supported yet!");
1791 case TargetLowering::Legal: break;
1792 case TargetLowering::Custom:
1793 Tmp1 = TLI.LowerOperation(Result, DAG);
1794 if (Tmp1.Val) Result = Tmp1;
1795 break;
1796 case TargetLowering::Promote:
1797 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1798 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1799 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1800 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001801 ST->getSrcValue(), SVOffset, isVolatile,
1802 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001803 break;
1804 }
1805 break;
1806 }
1807 case Promote:
1808 // Truncate the value and store the result.
1809 Tmp3 = PromoteOp(ST->getValue());
1810 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001811 SVOffset, ST->getStoredVT(),
1812 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001813 break;
1814
1815 case Expand:
1816 unsigned IncrementSize = 0;
1817 SDOperand Lo, Hi;
1818
1819 // If this is a vector type, then we have to calculate the increment as
1820 // the product of the element size in bytes, and the number of elements
1821 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001822 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001823 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001824 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1825 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001826
Dan Gohman7f321562007-06-25 16:23:39 +00001827 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001828 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001829 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001830 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001831 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001832 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001833 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001834 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001835 Result = LegalizeOp(Result);
1836 break;
1837 } else if (NumElems == 1) {
1838 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001839 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001840 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001841 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001842 // The scalarized value type may not be legal, e.g. it might require
1843 // promotion or expansion. Relegalize the scalar store.
1844 Result = LegalizeOp(Result);
1845 break;
1846 } else {
1847 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1848 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1849 }
1850 } else {
1851 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001852 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001853
1854 if (!TLI.isLittleEndian())
1855 std::swap(Lo, Hi);
1856 }
1857
1858 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001859 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001860
1861 if (Hi.Val == NULL) {
1862 // Must be int <-> float one-to-one expansion.
1863 Result = Lo;
1864 break;
1865 }
1866
Evan Cheng8b2794a2006-10-13 21:14:26 +00001867 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1868 getIntPtrConstant(IncrementSize));
1869 assert(isTypeLegal(Tmp2.getValueType()) &&
1870 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001871 SVOffset += IncrementSize;
1872 if (Alignment > IncrementSize)
1873 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001874 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001875 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001876 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1877 break;
1878 }
1879 } else {
1880 // Truncating store
1881 assert(isTypeLegal(ST->getValue().getValueType()) &&
1882 "Cannot handle illegal TRUNCSTORE yet!");
1883 Tmp3 = LegalizeOp(ST->getValue());
1884
1885 // The only promote case we handle is TRUNCSTORE:i1 X into
1886 // -> TRUNCSTORE:i8 (and X, 1)
1887 if (ST->getStoredVT() == MVT::i1 &&
1888 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1889 // Promote the bool to a mask then store.
1890 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1891 DAG.getConstant(1, Tmp3.getValueType()));
1892 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001893 SVOffset, MVT::i8,
1894 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001895 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1896 Tmp2 != ST->getBasePtr()) {
1897 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1898 ST->getOffset());
1899 }
1900
1901 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1902 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001903 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001904 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001905 case TargetLowering::Custom:
1906 Tmp1 = TLI.LowerOperation(Result, DAG);
1907 if (Tmp1.Val) Result = Tmp1;
1908 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001909 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001910 }
1911 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001912 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001913 case ISD::PCMARKER:
1914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001916 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001917 case ISD::STACKSAVE:
1918 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001919 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1920 Tmp1 = Result.getValue(0);
1921 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001922
Chris Lattner140d53c2006-01-13 02:50:02 +00001923 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1924 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001925 case TargetLowering::Legal: break;
1926 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001927 Tmp3 = TLI.LowerOperation(Result, DAG);
1928 if (Tmp3.Val) {
1929 Tmp1 = LegalizeOp(Tmp3);
1930 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001931 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001932 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001933 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001934 // Expand to CopyFromReg if the target set
1935 // StackPointerRegisterToSaveRestore.
1936 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001937 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001938 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001939 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001940 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001941 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1942 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001943 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001944 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001945 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001946
1947 // Since stacksave produce two values, make sure to remember that we
1948 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001949 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1950 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1951 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001952
Chris Lattner140d53c2006-01-13 02:50:02 +00001953 case ISD::STACKRESTORE:
1954 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1955 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001957
1958 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1959 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001960 case TargetLowering::Legal: break;
1961 case TargetLowering::Custom:
1962 Tmp1 = TLI.LowerOperation(Result, DAG);
1963 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001964 break;
1965 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001966 // Expand to CopyToReg if the target set
1967 // StackPointerRegisterToSaveRestore.
1968 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1969 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1970 } else {
1971 Result = Tmp1;
1972 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001973 break;
1974 }
1975 break;
1976
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001977 case ISD::READCYCLECOUNTER:
1978 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001979 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001980 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1981 Node->getValueType(0))) {
1982 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001983 case TargetLowering::Legal:
1984 Tmp1 = Result.getValue(0);
1985 Tmp2 = Result.getValue(1);
1986 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001987 case TargetLowering::Custom:
1988 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001989 Tmp1 = LegalizeOp(Result.getValue(0));
1990 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001991 break;
1992 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001993
1994 // Since rdcc produce two values, make sure to remember that we legalized
1995 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001996 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1997 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001998 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001999
Chris Lattner2ee743f2005-01-14 22:08:15 +00002000 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002001 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2002 case Expand: assert(0 && "It's impossible to expand bools");
2003 case Legal:
2004 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2005 break;
2006 case Promote:
2007 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002008 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002009 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002010 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2011 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002012 break;
2013 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002014 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002015 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002016
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002017 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002018
Nate Begemanb942a3d2005-08-23 04:29:48 +00002019 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002020 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002021 case TargetLowering::Legal: break;
2022 case TargetLowering::Custom: {
2023 Tmp1 = TLI.LowerOperation(Result, DAG);
2024 if (Tmp1.Val) Result = Tmp1;
2025 break;
2026 }
Nate Begeman9373a812005-08-10 20:51:12 +00002027 case TargetLowering::Expand:
2028 if (Tmp1.getOpcode() == ISD::SETCC) {
2029 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2030 Tmp2, Tmp3,
2031 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2032 } else {
2033 Result = DAG.getSelectCC(Tmp1,
2034 DAG.getConstant(0, Tmp1.getValueType()),
2035 Tmp2, Tmp3, ISD::SETNE);
2036 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002037 break;
2038 case TargetLowering::Promote: {
2039 MVT::ValueType NVT =
2040 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2041 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002042 if (MVT::isVector(Tmp2.getValueType())) {
2043 ExtOp = ISD::BIT_CONVERT;
2044 TruncOp = ISD::BIT_CONVERT;
2045 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002046 ExtOp = ISD::ANY_EXTEND;
2047 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002048 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002049 ExtOp = ISD::FP_EXTEND;
2050 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002051 }
2052 // Promote each of the values to the new type.
2053 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2054 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2055 // Perform the larger operation, then round down.
2056 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2057 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2058 break;
2059 }
2060 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002061 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002062 case ISD::SELECT_CC: {
2063 Tmp1 = Node->getOperand(0); // LHS
2064 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002065 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2066 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002067 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002068
Nate Begeman750ac1b2006-02-01 07:19:44 +00002069 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2070
2071 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2072 // the LHS is a legal SETCC itself. In this case, we need to compare
2073 // the result against zero to select between true and false values.
2074 if (Tmp2.Val == 0) {
2075 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2076 CC = DAG.getCondCode(ISD::SETNE);
2077 }
2078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2079
2080 // Everything is legal, see if we should expand this op or something.
2081 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2082 default: assert(0 && "This action is not supported yet!");
2083 case TargetLowering::Legal: break;
2084 case TargetLowering::Custom:
2085 Tmp1 = TLI.LowerOperation(Result, DAG);
2086 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002087 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002088 }
2089 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002090 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002091 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002092 Tmp1 = Node->getOperand(0);
2093 Tmp2 = Node->getOperand(1);
2094 Tmp3 = Node->getOperand(2);
2095 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2096
2097 // If we had to Expand the SetCC operands into a SELECT node, then it may
2098 // not always be possible to return a true LHS & RHS. In this case, just
2099 // return the value we legalized, returned in the LHS
2100 if (Tmp2.Val == 0) {
2101 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002102 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002103 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002104
Chris Lattner73e142f2006-01-30 22:43:50 +00002105 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002106 default: assert(0 && "Cannot handle this action for SETCC yet!");
2107 case TargetLowering::Custom:
2108 isCustom = true;
2109 // FALLTHROUGH.
2110 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002112 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002113 Tmp4 = TLI.LowerOperation(Result, DAG);
2114 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002115 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002116 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002117 case TargetLowering::Promote: {
2118 // First step, figure out the appropriate operation to use.
2119 // Allow SETCC to not be supported for all legal data types
2120 // Mostly this targets FP
2121 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002122 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002123
2124 // Scan for the appropriate larger type to use.
2125 while (1) {
2126 NewInTy = (MVT::ValueType)(NewInTy+1);
2127
2128 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2129 "Fell off of the edge of the integer world");
2130 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2131 "Fell off of the edge of the floating point world");
2132
2133 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002134 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002135 break;
2136 }
2137 if (MVT::isInteger(NewInTy))
2138 assert(0 && "Cannot promote Legal Integer SETCC yet");
2139 else {
2140 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2141 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2142 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002143 Tmp1 = LegalizeOp(Tmp1);
2144 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002145 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002146 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002147 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002148 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002149 case TargetLowering::Expand:
2150 // Expand a setcc node into a select_cc of the same condition, lhs, and
2151 // rhs that selects between const 1 (true) and const 0 (false).
2152 MVT::ValueType VT = Node->getValueType(0);
2153 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2154 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002155 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002156 break;
2157 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002158 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002159 case ISD::MEMSET:
2160 case ISD::MEMCPY:
2161 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002163 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2164
2165 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2166 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2167 case Expand: assert(0 && "Cannot expand a byte!");
2168 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002169 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002170 break;
2171 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002172 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002173 break;
2174 }
2175 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002176 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002177 }
Chris Lattner272455b2005-02-02 03:44:41 +00002178
2179 SDOperand Tmp4;
2180 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002181 case Expand: {
2182 // Length is too big, just take the lo-part of the length.
2183 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002184 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002185 break;
2186 }
Chris Lattnere5605212005-01-28 22:29:18 +00002187 case Legal:
2188 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002189 break;
2190 case Promote:
2191 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002192 break;
2193 }
2194
2195 SDOperand Tmp5;
2196 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2197 case Expand: assert(0 && "Cannot expand this yet!");
2198 case Legal:
2199 Tmp5 = LegalizeOp(Node->getOperand(4));
2200 break;
2201 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002202 Tmp5 = PromoteOp(Node->getOperand(4));
2203 break;
2204 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002205
2206 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2207 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002208 case TargetLowering::Custom:
2209 isCustom = true;
2210 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002211 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002213 if (isCustom) {
2214 Tmp1 = TLI.LowerOperation(Result, DAG);
2215 if (Tmp1.Val) Result = Tmp1;
2216 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002217 break;
2218 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002219 // Otherwise, the target does not support this operation. Lower the
2220 // operation to an explicit libcall as appropriate.
2221 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002222 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002223 TargetLowering::ArgListTy Args;
2224 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002225
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002226 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002227 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002228 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002229 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002230 // Extend the (previously legalized) ubyte argument to be an int value
2231 // for the call.
2232 if (Tmp3.getValueType() > MVT::i32)
2233 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2234 else
2235 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002236 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002237 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002238 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002239 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002240
2241 FnName = "memset";
2242 } else if (Node->getOpcode() == ISD::MEMCPY ||
2243 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002244 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002245 Entry.Node = Tmp2; Args.push_back(Entry);
2246 Entry.Node = Tmp3; Args.push_back(Entry);
2247 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002248 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2249 } else {
2250 assert(0 && "Unknown op!");
2251 }
Chris Lattner45982da2005-05-12 16:53:42 +00002252
Chris Lattnere1bd8222005-01-11 05:57:22 +00002253 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002254 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002255 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002256 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002257 break;
2258 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002259 }
2260 break;
2261 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002262
Chris Lattner5b359c62005-04-02 04:00:59 +00002263 case ISD::SHL_PARTS:
2264 case ISD::SRA_PARTS:
2265 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002266 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002267 bool Changed = false;
2268 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2269 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2270 Changed |= Ops.back() != Node->getOperand(i);
2271 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002272 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002273 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002274
Evan Cheng05a2d562006-01-09 18:31:59 +00002275 switch (TLI.getOperationAction(Node->getOpcode(),
2276 Node->getValueType(0))) {
2277 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002278 case TargetLowering::Legal: break;
2279 case TargetLowering::Custom:
2280 Tmp1 = TLI.LowerOperation(Result, DAG);
2281 if (Tmp1.Val) {
2282 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002283 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002284 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002285 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2286 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002287 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002288 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002289 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002290 return RetVal;
2291 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002292 break;
2293 }
2294
Chris Lattner2c8086f2005-04-02 05:00:07 +00002295 // Since these produce multiple values, make sure to remember that we
2296 // legalized all of them.
2297 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2298 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2299 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002300 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002301
2302 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002303 case ISD::ADD:
2304 case ISD::SUB:
2305 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002306 case ISD::MULHS:
2307 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002308 case ISD::UDIV:
2309 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002310 case ISD::AND:
2311 case ISD::OR:
2312 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002313 case ISD::SHL:
2314 case ISD::SRL:
2315 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002316 case ISD::FADD:
2317 case ISD::FSUB:
2318 case ISD::FMUL:
2319 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002320 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002321 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2322 case Expand: assert(0 && "Not possible");
2323 case Legal:
2324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2325 break;
2326 case Promote:
2327 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2328 break;
2329 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002330
2331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002332
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002333 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002334 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002335 case TargetLowering::Legal: break;
2336 case TargetLowering::Custom:
2337 Tmp1 = TLI.LowerOperation(Result, DAG);
2338 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002339 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002340 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002341 if (Node->getValueType(0) == MVT::i32) {
2342 switch (Node->getOpcode()) {
2343 default: assert(0 && "Do not know how to expand this integer BinOp!");
2344 case ISD::UDIV:
2345 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002346 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2347 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002348 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002349 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002350 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002351 };
2352 break;
2353 }
2354
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002355 assert(MVT::isVector(Node->getValueType(0)) &&
2356 "Cannot expand this binary operator!");
2357 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002358 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002359 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002360 MVT::ValueType PtrVT = TLI.getPointerTy();
2361 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2362 i != e; ++i) {
2363 SDOperand Idx = DAG.getConstant(i, PtrVT);
2364 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2365 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2366 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2367 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002368 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2369 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002370 break;
2371 }
Evan Chengcc987612006-04-12 21:20:24 +00002372 case TargetLowering::Promote: {
2373 switch (Node->getOpcode()) {
2374 default: assert(0 && "Do not know how to promote this BinOp!");
2375 case ISD::AND:
2376 case ISD::OR:
2377 case ISD::XOR: {
2378 MVT::ValueType OVT = Node->getValueType(0);
2379 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2380 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2381 // Bit convert each of the values to the new type.
2382 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2383 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2384 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2385 // Bit convert the result back the original type.
2386 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2387 break;
2388 }
2389 }
2390 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002391 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002392 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002393
2394 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2395 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2397 case Expand: assert(0 && "Not possible");
2398 case Legal:
2399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2400 break;
2401 case Promote:
2402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2403 break;
2404 }
2405
2406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2407
2408 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2409 default: assert(0 && "Operation not supported");
2410 case TargetLowering::Custom:
2411 Tmp1 = TLI.LowerOperation(Result, DAG);
2412 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002413 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002414 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002415 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002416 // If this target supports fabs/fneg natively and select is cheap,
2417 // do this efficiently.
2418 if (!TLI.isSelectExpensive() &&
2419 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2420 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002421 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002422 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002423 // Get the sign bit of the RHS.
2424 MVT::ValueType IVT =
2425 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2426 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2427 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2428 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2429 // Get the absolute value of the result.
2430 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2431 // Select between the nabs and abs value based on the sign bit of
2432 // the input.
2433 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2434 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2435 AbsVal),
2436 AbsVal);
2437 Result = LegalizeOp(Result);
2438 break;
2439 }
2440
2441 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002442 MVT::ValueType NVT =
2443 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2444 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2445 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2446 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002447 break;
2448 }
Evan Cheng912095b2007-01-04 21:56:39 +00002449 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002450 break;
2451
Nate Begeman551bf3f2006-02-17 05:43:56 +00002452 case ISD::ADDC:
2453 case ISD::SUBC:
2454 Tmp1 = LegalizeOp(Node->getOperand(0));
2455 Tmp2 = LegalizeOp(Node->getOperand(1));
2456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2457 // Since this produces two values, make sure to remember that we legalized
2458 // both of them.
2459 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2460 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2461 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002462
Nate Begeman551bf3f2006-02-17 05:43:56 +00002463 case ISD::ADDE:
2464 case ISD::SUBE:
2465 Tmp1 = LegalizeOp(Node->getOperand(0));
2466 Tmp2 = LegalizeOp(Node->getOperand(1));
2467 Tmp3 = LegalizeOp(Node->getOperand(2));
2468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2469 // Since this produces two values, make sure to remember that we legalized
2470 // both of them.
2471 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2472 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2473 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002474
Nate Begeman419f8b62005-10-18 00:27:41 +00002475 case ISD::BUILD_PAIR: {
2476 MVT::ValueType PairTy = Node->getValueType(0);
2477 // TODO: handle the case where the Lo and Hi operands are not of legal type
2478 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2479 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2480 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002481 case TargetLowering::Promote:
2482 case TargetLowering::Custom:
2483 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002484 case TargetLowering::Legal:
2485 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2486 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2487 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002488 case TargetLowering::Expand:
2489 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2490 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2491 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2492 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2493 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002494 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002495 break;
2496 }
2497 break;
2498 }
2499
Nate Begemanc105e192005-04-06 00:23:54 +00002500 case ISD::UREM:
2501 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002502 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002503 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2504 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002505
Nate Begemanc105e192005-04-06 00:23:54 +00002506 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002507 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2508 case TargetLowering::Custom:
2509 isCustom = true;
2510 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002511 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002513 if (isCustom) {
2514 Tmp1 = TLI.LowerOperation(Result, DAG);
2515 if (Tmp1.Val) Result = Tmp1;
2516 }
Nate Begemanc105e192005-04-06 00:23:54 +00002517 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002518 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002519 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002520 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002521 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002522 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2523 TargetLowering::Legal) {
2524 // X % Y -> X-X/Y*Y
2525 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002526 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002527 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2528 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2529 } else {
2530 assert(Node->getValueType(0) == MVT::i32 &&
2531 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002532 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2533 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002534 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002535 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002536 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002537 } else {
2538 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002539 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2540 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002541 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002542 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2543 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002544 }
2545 break;
2546 }
2547 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002548 case ISD::VAARG: {
2549 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2550 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2551
Chris Lattner5c62f332006-01-28 07:42:08 +00002552 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002553 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2554 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002555 case TargetLowering::Custom:
2556 isCustom = true;
2557 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002558 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002559 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2560 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002561 Tmp1 = Result.getValue(1);
2562
2563 if (isCustom) {
2564 Tmp2 = TLI.LowerOperation(Result, DAG);
2565 if (Tmp2.Val) {
2566 Result = LegalizeOp(Tmp2);
2567 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2568 }
2569 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002570 break;
2571 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002572 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002573 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002574 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002575 // Increment the pointer, VAList, to the next vaarg
2576 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2577 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2578 TLI.getPointerTy()));
2579 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002580 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2581 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002582 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002583 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002584 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002585 Result = LegalizeOp(Result);
2586 break;
2587 }
2588 }
2589 // Since VAARG produces two values, make sure to remember that we
2590 // legalized both of them.
2591 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002592 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2593 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002594 }
2595
2596 case ISD::VACOPY:
2597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2598 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2599 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2600
2601 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2602 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002603 case TargetLowering::Custom:
2604 isCustom = true;
2605 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002606 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2608 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002609 if (isCustom) {
2610 Tmp1 = TLI.LowerOperation(Result, DAG);
2611 if (Tmp1.Val) Result = Tmp1;
2612 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002613 break;
2614 case TargetLowering::Expand:
2615 // This defaults to loading a pointer from the input and storing it to the
2616 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002617 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002618 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002619 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2620 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002621 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2622 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002623 break;
2624 }
2625 break;
2626
2627 case ISD::VAEND:
2628 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2629 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2630
2631 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2632 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002633 case TargetLowering::Custom:
2634 isCustom = true;
2635 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002636 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002638 if (isCustom) {
2639 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2640 if (Tmp1.Val) Result = Tmp1;
2641 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002642 break;
2643 case TargetLowering::Expand:
2644 Result = Tmp1; // Default to a no-op, return the chain
2645 break;
2646 }
2647 break;
2648
2649 case ISD::VASTART:
2650 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2651 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2652
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002653 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2654
Nate Begemanacc398c2006-01-25 18:21:52 +00002655 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2656 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002657 case TargetLowering::Legal: break;
2658 case TargetLowering::Custom:
2659 Tmp1 = TLI.LowerOperation(Result, DAG);
2660 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002661 break;
2662 }
2663 break;
2664
Nate Begeman35ef9132006-01-11 21:21:00 +00002665 case ISD::ROTL:
2666 case ISD::ROTR:
2667 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2668 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002669 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002670 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2671 default:
2672 assert(0 && "ROTL/ROTR legalize operation not supported");
2673 break;
2674 case TargetLowering::Legal:
2675 break;
2676 case TargetLowering::Custom:
2677 Tmp1 = TLI.LowerOperation(Result, DAG);
2678 if (Tmp1.Val) Result = Tmp1;
2679 break;
2680 case TargetLowering::Promote:
2681 assert(0 && "Do not know how to promote ROTL/ROTR");
2682 break;
2683 case TargetLowering::Expand:
2684 assert(0 && "Do not know how to expand ROTL/ROTR");
2685 break;
2686 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002687 break;
2688
Nate Begemand88fc032006-01-14 03:14:10 +00002689 case ISD::BSWAP:
2690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2691 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002692 case TargetLowering::Custom:
2693 assert(0 && "Cannot custom legalize this yet!");
2694 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002696 break;
2697 case TargetLowering::Promote: {
2698 MVT::ValueType OVT = Tmp1.getValueType();
2699 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002700 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002701
Chris Lattner456a93a2006-01-28 07:39:30 +00002702 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2703 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2704 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2705 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2706 break;
2707 }
2708 case TargetLowering::Expand:
2709 Result = ExpandBSWAP(Tmp1);
2710 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002711 }
2712 break;
2713
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002714 case ISD::CTPOP:
2715 case ISD::CTTZ:
2716 case ISD::CTLZ:
2717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2718 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002719 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002720 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002721 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002722 break;
2723 case TargetLowering::Promote: {
2724 MVT::ValueType OVT = Tmp1.getValueType();
2725 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002726
2727 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002728 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2729 // Perform the larger operation, then subtract if needed.
2730 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002731 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002732 case ISD::CTPOP:
2733 Result = Tmp1;
2734 break;
2735 case ISD::CTTZ:
2736 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002737 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002738 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002739 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002740 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002741 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002742 break;
2743 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002744 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002745 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002746 DAG.getConstant(MVT::getSizeInBits(NVT) -
2747 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002748 break;
2749 }
2750 break;
2751 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002752 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002753 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002754 break;
2755 }
2756 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002757
Chris Lattner2c8086f2005-04-02 05:00:07 +00002758 // Unary operators
2759 case ISD::FABS:
2760 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002761 case ISD::FSQRT:
2762 case ISD::FSIN:
2763 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002764 Tmp1 = LegalizeOp(Node->getOperand(0));
2765 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002766 case TargetLowering::Promote:
2767 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002768 isCustom = true;
2769 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002770 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002771 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002772 if (isCustom) {
2773 Tmp1 = TLI.LowerOperation(Result, DAG);
2774 if (Tmp1.Val) Result = Tmp1;
2775 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002776 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002777 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002778 switch (Node->getOpcode()) {
2779 default: assert(0 && "Unreachable!");
2780 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002781 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2782 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002783 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002784 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002785 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002786 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2787 MVT::ValueType VT = Node->getValueType(0);
2788 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002789 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002790 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2791 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002792 break;
2793 }
2794 case ISD::FSQRT:
2795 case ISD::FSIN:
2796 case ISD::FCOS: {
2797 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002798 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002799 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002800 case ISD::FSQRT:
2801 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2802 break;
2803 case ISD::FSIN:
2804 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2805 break;
2806 case ISD::FCOS:
2807 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2808 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002809 default: assert(0 && "Unreachable!");
2810 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002811 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002812 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2813 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002814 break;
2815 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002816 }
2817 break;
2818 }
2819 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002820 case ISD::FPOWI: {
2821 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002822 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2823 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002824 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002825 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2826 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002827 break;
2828 }
Chris Lattner35481892005-12-23 00:16:34 +00002829 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002830 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002831 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00002832 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2833 // The input has to be a vector type, we have to either scalarize it, pack
2834 // it, or convert it based on whether the input vector type is legal.
2835 SDNode *InVal = Node->getOperand(0).Val;
2836 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2837 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
2838
2839 // Figure out if there is a simple type corresponding to this Vector
2840 // type. If so, convert to the vector type.
2841 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2842 if (TLI.isTypeLegal(TVT)) {
2843 // Turn this into a bit convert of the packed input.
2844 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2845 LegalizeOp(Node->getOperand(0)));
2846 break;
2847 } else if (NumElems == 1) {
2848 // Turn this into a bit convert of the scalar input.
2849 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2850 ScalarizeVectorOp(Node->getOperand(0)));
2851 break;
2852 } else {
2853 // FIXME: UNIMP! Store then reload
2854 assert(0 && "Cast from unsupported vector type not implemented yet!");
2855 }
Chris Lattner67993f72006-01-23 07:30:46 +00002856 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002857 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2858 Node->getOperand(0).getValueType())) {
2859 default: assert(0 && "Unknown operation action!");
2860 case TargetLowering::Expand:
2861 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2862 break;
2863 case TargetLowering::Legal:
2864 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002865 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002866 break;
2867 }
2868 }
2869 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002870
Chris Lattner2c8086f2005-04-02 05:00:07 +00002871 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002872 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002873 case ISD::UINT_TO_FP: {
2874 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002875 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2876 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002877 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002878 Node->getOperand(0).getValueType())) {
2879 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002880 case TargetLowering::Custom:
2881 isCustom = true;
2882 // FALLTHROUGH
2883 case TargetLowering::Legal:
2884 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002885 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002886 if (isCustom) {
2887 Tmp1 = TLI.LowerOperation(Result, DAG);
2888 if (Tmp1.Val) Result = Tmp1;
2889 }
2890 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002891 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002892 Result = ExpandLegalINT_TO_FP(isSigned,
2893 LegalizeOp(Node->getOperand(0)),
2894 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002895 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002896 case TargetLowering::Promote:
2897 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2898 Node->getValueType(0),
2899 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002900 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002901 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002902 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002903 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002904 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2905 Node->getValueType(0), Node->getOperand(0));
2906 break;
2907 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002908 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002909 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002910 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2911 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002912 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002913 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2914 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002915 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002916 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2917 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002918 break;
2919 }
2920 break;
2921 }
2922 case ISD::TRUNCATE:
2923 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2924 case Legal:
2925 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002926 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002927 break;
2928 case Expand:
2929 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2930
2931 // Since the result is legal, we should just be able to truncate the low
2932 // part of the source.
2933 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2934 break;
2935 case Promote:
2936 Result = PromoteOp(Node->getOperand(0));
2937 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2938 break;
2939 }
2940 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002941
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002942 case ISD::FP_TO_SINT:
2943 case ISD::FP_TO_UINT:
2944 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2945 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002946 Tmp1 = LegalizeOp(Node->getOperand(0));
2947
Chris Lattner1618beb2005-07-29 00:11:56 +00002948 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2949 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002950 case TargetLowering::Custom:
2951 isCustom = true;
2952 // FALLTHROUGH
2953 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002954 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002955 if (isCustom) {
2956 Tmp1 = TLI.LowerOperation(Result, DAG);
2957 if (Tmp1.Val) Result = Tmp1;
2958 }
2959 break;
2960 case TargetLowering::Promote:
2961 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2962 Node->getOpcode() == ISD::FP_TO_SINT);
2963 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002964 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002965 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2966 SDOperand True, False;
2967 MVT::ValueType VT = Node->getOperand(0).getValueType();
2968 MVT::ValueType NVT = Node->getValueType(0);
2969 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2970 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2971 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2972 Node->getOperand(0), Tmp2, ISD::SETLT);
2973 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2974 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002975 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002976 Tmp2));
2977 False = DAG.getNode(ISD::XOR, NVT, False,
2978 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002979 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2980 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002981 } else {
2982 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2983 }
2984 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002985 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002986 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002987 case Expand: {
2988 // Convert f32 / f64 to i32 / i64.
2989 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00002990 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00002991 switch (Node->getOpcode()) {
2992 case ISD::FP_TO_SINT:
2993 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002994 LC = (VT == MVT::i32)
2995 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002996 else
Evan Cheng56966222007-01-12 02:11:51 +00002997 LC = (VT == MVT::i32)
2998 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002999 break;
3000 case ISD::FP_TO_UINT:
3001 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003002 LC = (VT == MVT::i32)
3003 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003004 else
Evan Cheng56966222007-01-12 02:11:51 +00003005 LC = (VT == MVT::i32)
3006 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003007 break;
3008 default: assert(0 && "Unreachable!");
3009 }
3010 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003011 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3012 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003013 break;
3014 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003015 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003016 Tmp1 = PromoteOp(Node->getOperand(0));
3017 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3018 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003019 break;
3020 }
3021 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003022
Dale Johannesen849f2142007-07-03 00:53:03 +00003023 case ISD::FP_ROUND:
3024 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3025 TargetLowering::Expand) {
3026 // The only way we can lower this is to turn it into a TRUNCSTORE,
3027 // EXTLOAD pair, targetting a temporary location (a stack slot).
3028
3029 // NOTE: there is a choice here between constantly creating new stack
3030 // slots and always reusing the same one. We currently always create
3031 // new ones, as reuse may inhibit scheduling.
3032 MVT::ValueType VT = Op.getValueType(); // 32
3033 const Type *Ty = MVT::getTypeForValueType(VT);
3034 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3035 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3036 MachineFunction &MF = DAG.getMachineFunction();
3037 int SSFI =
3038 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3039 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3040 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3041 StackSlot, NULL, 0, VT);
3042 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3043 break;
3044 }
3045 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003046 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003047 case ISD::ZERO_EXTEND:
3048 case ISD::SIGN_EXTEND:
3049 case ISD::FP_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003050 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003051 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003052 case Legal:
3053 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003054 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003055 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003056 case Promote:
3057 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003058 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003059 Tmp1 = PromoteOp(Node->getOperand(0));
3060 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003061 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003062 case ISD::ZERO_EXTEND:
3063 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003064 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003065 Result = DAG.getZeroExtendInReg(Result,
3066 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003067 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003068 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003069 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003070 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003071 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003072 Result,
3073 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003074 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003075 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003076 Result = PromoteOp(Node->getOperand(0));
3077 if (Result.getValueType() != Op.getValueType())
3078 // Dynamically dead while we have only 2 FP types.
3079 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3080 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003081 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003082 Result = PromoteOp(Node->getOperand(0));
3083 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3084 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003085 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003086 }
3087 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003088 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003089 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003090 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003091 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003092
3093 // If this operation is not supported, convert it to a shl/shr or load/store
3094 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003095 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3096 default: assert(0 && "This action not supported for this op yet!");
3097 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003099 break;
3100 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003101 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003102 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003103 // NOTE: we could fall back on load/store here too for targets without
3104 // SAR. However, it is doubtful that any exist.
3105 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3106 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003107 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003108 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3109 Node->getOperand(0), ShiftCst);
3110 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3111 Result, ShiftCst);
3112 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003113 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003114 // EXTLOAD pair, targetting a temporary location (a stack slot).
3115
3116 // NOTE: there is a choice here between constantly creating new stack
3117 // slots and always reusing the same one. We currently always create
3118 // new ones, as reuse may inhibit scheduling.
3119 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003120 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003121 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003122 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003123 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003124 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003125 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003126 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3127 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003128 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003129 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003130 } else {
3131 assert(0 && "Unknown op");
3132 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003133 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003134 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003135 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003136 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003137 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003138
Chris Lattner4ddd2832006-04-08 04:13:17 +00003139 assert(Result.getValueType() == Op.getValueType() &&
3140 "Bad legalization!");
3141
Chris Lattner456a93a2006-01-28 07:39:30 +00003142 // Make sure that the generated code is itself legal.
3143 if (Result != Op)
3144 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003145
Chris Lattner45982da2005-05-12 16:53:42 +00003146 // Note that LegalizeOp may be reentered even from single-use nodes, which
3147 // means that we always must cache transformed nodes.
3148 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003149 return Result;
3150}
3151
Chris Lattner8b6fa222005-01-15 22:16:26 +00003152/// PromoteOp - Given an operation that produces a value in an invalid type,
3153/// promote it to compute the value into a larger type. The produced value will
3154/// have the correct bits for the low portion of the register, but no guarantee
3155/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003156SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3157 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003158 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003159 assert(getTypeAction(VT) == Promote &&
3160 "Caller should expand or legalize operands that are not promotable!");
3161 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3162 "Cannot promote to smaller type!");
3163
Chris Lattner03c85462005-01-15 05:21:40 +00003164 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003165 SDOperand Result;
3166 SDNode *Node = Op.Val;
3167
Chris Lattner40030bf2007-02-04 01:17:38 +00003168 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003169 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003170
Chris Lattner03c85462005-01-15 05:21:40 +00003171 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003172 case ISD::CopyFromReg:
3173 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003174 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003175#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003176 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003177#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003178 assert(0 && "Do not know how to promote this operator!");
3179 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003180 case ISD::UNDEF:
3181 Result = DAG.getNode(ISD::UNDEF, NVT);
3182 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003183 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003184 if (VT != MVT::i1)
3185 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3186 else
3187 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003188 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3189 break;
3190 case ISD::ConstantFP:
3191 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3192 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3193 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003194
Chris Lattner82fbfb62005-01-18 02:59:52 +00003195 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003196 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003197 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3198 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003199 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003200
Chris Lattner03c85462005-01-15 05:21:40 +00003201 case ISD::TRUNCATE:
3202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3203 case Legal:
3204 Result = LegalizeOp(Node->getOperand(0));
3205 assert(Result.getValueType() >= NVT &&
3206 "This truncation doesn't make sense!");
3207 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3208 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3209 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003210 case Promote:
3211 // The truncation is not required, because we don't guarantee anything
3212 // about high bits anyway.
3213 Result = PromoteOp(Node->getOperand(0));
3214 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003215 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003216 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3217 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003218 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003219 }
3220 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003221 case ISD::SIGN_EXTEND:
3222 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003223 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003224 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3225 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3226 case Legal:
3227 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003228 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003229 break;
3230 case Promote:
3231 // Promote the reg if it's smaller.
3232 Result = PromoteOp(Node->getOperand(0));
3233 // The high bits are not guaranteed to be anything. Insert an extend.
3234 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003235 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003236 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003237 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003238 Result = DAG.getZeroExtendInReg(Result,
3239 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003240 break;
3241 }
3242 break;
Chris Lattner35481892005-12-23 00:16:34 +00003243 case ISD::BIT_CONVERT:
3244 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3245 Result = PromoteOp(Result);
3246 break;
3247
Chris Lattner8b6fa222005-01-15 22:16:26 +00003248 case ISD::FP_EXTEND:
3249 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3250 case ISD::FP_ROUND:
3251 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3252 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3253 case Promote: assert(0 && "Unreachable with 2 FP types!");
3254 case Legal:
3255 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003256 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003257 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003258 break;
3259 }
3260 break;
3261
3262 case ISD::SINT_TO_FP:
3263 case ISD::UINT_TO_FP:
3264 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3265 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003266 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003267 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003268 break;
3269
3270 case Promote:
3271 Result = PromoteOp(Node->getOperand(0));
3272 if (Node->getOpcode() == ISD::SINT_TO_FP)
3273 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003274 Result,
3275 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003276 else
Chris Lattner23993562005-04-13 02:38:47 +00003277 Result = DAG.getZeroExtendInReg(Result,
3278 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003279 // No extra round required here.
3280 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003281 break;
3282 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003283 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3284 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003285 // Round if we cannot tolerate excess precision.
3286 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003287 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3288 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003289 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003290 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003291 break;
3292
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003293 case ISD::SIGN_EXTEND_INREG:
3294 Result = PromoteOp(Node->getOperand(0));
3295 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3296 Node->getOperand(1));
3297 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003298 case ISD::FP_TO_SINT:
3299 case ISD::FP_TO_UINT:
3300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3301 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003302 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003303 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003304 break;
3305 case Promote:
3306 // The input result is prerounded, so we don't have to do anything
3307 // special.
3308 Tmp1 = PromoteOp(Node->getOperand(0));
3309 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003310 }
Nate Begemand2558e32005-08-14 01:20:53 +00003311 // If we're promoting a UINT to a larger size, check to see if the new node
3312 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3313 // we can use that instead. This allows us to generate better code for
3314 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3315 // legal, such as PowerPC.
3316 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003317 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003318 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3319 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003320 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3321 } else {
3322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3323 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003324 break;
3325
Chris Lattner2c8086f2005-04-02 05:00:07 +00003326 case ISD::FABS:
3327 case ISD::FNEG:
3328 Tmp1 = PromoteOp(Node->getOperand(0));
3329 assert(Tmp1.getValueType() == NVT);
3330 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3331 // NOTE: we do not have to do any extra rounding here for
3332 // NoExcessFPPrecision, because we know the input will have the appropriate
3333 // precision, and these operations don't modify precision at all.
3334 break;
3335
Chris Lattnerda6ba872005-04-28 21:44:33 +00003336 case ISD::FSQRT:
3337 case ISD::FSIN:
3338 case ISD::FCOS:
3339 Tmp1 = PromoteOp(Node->getOperand(0));
3340 assert(Tmp1.getValueType() == NVT);
3341 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003342 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003343 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3344 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003345 break;
3346
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003347 case ISD::FPOWI: {
3348 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3349 // directly as well, which may be better.
3350 Tmp1 = PromoteOp(Node->getOperand(0));
3351 assert(Tmp1.getValueType() == NVT);
3352 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3353 if (NoExcessFPPrecision)
3354 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3355 DAG.getValueType(VT));
3356 break;
3357 }
3358
Chris Lattner03c85462005-01-15 05:21:40 +00003359 case ISD::AND:
3360 case ISD::OR:
3361 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003362 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003363 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003364 case ISD::MUL:
3365 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003366 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003367 // that too is okay if they are integer operations.
3368 Tmp1 = PromoteOp(Node->getOperand(0));
3369 Tmp2 = PromoteOp(Node->getOperand(1));
3370 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3371 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003372 break;
3373 case ISD::FADD:
3374 case ISD::FSUB:
3375 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003376 Tmp1 = PromoteOp(Node->getOperand(0));
3377 Tmp2 = PromoteOp(Node->getOperand(1));
3378 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3379 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3380
3381 // Floating point operations will give excess precision that we may not be
3382 // able to tolerate. If we DO allow excess precision, just leave it,
3383 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003384 // FIXME: Why would we need to round FP ops more than integer ones?
3385 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003386 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003387 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3388 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003389 break;
3390
Chris Lattner8b6fa222005-01-15 22:16:26 +00003391 case ISD::SDIV:
3392 case ISD::SREM:
3393 // These operators require that their input be sign extended.
3394 Tmp1 = PromoteOp(Node->getOperand(0));
3395 Tmp2 = PromoteOp(Node->getOperand(1));
3396 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003397 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3398 DAG.getValueType(VT));
3399 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3400 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003401 }
3402 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3403
3404 // Perform FP_ROUND: this is probably overly pessimistic.
3405 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003406 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3407 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003408 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003409 case ISD::FDIV:
3410 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003411 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003412 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003413 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3414 case Legal:
3415 Tmp1 = LegalizeOp(Node->getOperand(0));
3416 break;
3417 case Promote:
3418 Tmp1 = PromoteOp(Node->getOperand(0));
3419 break;
3420 case Expand:
3421 assert(0 && "not implemented");
3422 }
3423 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3424 case Legal:
3425 Tmp2 = LegalizeOp(Node->getOperand(1));
3426 break;
3427 case Promote:
3428 Tmp2 = PromoteOp(Node->getOperand(1));
3429 break;
3430 case Expand:
3431 assert(0 && "not implemented");
3432 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003433 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3434
3435 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003436 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003437 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3438 DAG.getValueType(VT));
3439 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003440
3441 case ISD::UDIV:
3442 case ISD::UREM:
3443 // These operators require that their input be zero extended.
3444 Tmp1 = PromoteOp(Node->getOperand(0));
3445 Tmp2 = PromoteOp(Node->getOperand(1));
3446 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003447 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3448 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003449 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3450 break;
3451
3452 case ISD::SHL:
3453 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003454 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003455 break;
3456 case ISD::SRA:
3457 // The input value must be properly sign extended.
3458 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003459 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3460 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003461 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003462 break;
3463 case ISD::SRL:
3464 // The input value must be properly zero extended.
3465 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003466 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003467 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003468 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003469
3470 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003471 Tmp1 = Node->getOperand(0); // Get the chain.
3472 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003473 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3474 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3475 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3476 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003477 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003478 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003479 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003480 // Increment the pointer, VAList, to the next vaarg
3481 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3482 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3483 TLI.getPointerTy()));
3484 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003485 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3486 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003487 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003488 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003489 }
3490 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003491 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003492 break;
3493
Evan Cheng466685d2006-10-09 20:57:25 +00003494 case ISD::LOAD: {
3495 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003496 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3497 ? ISD::EXTLOAD : LD->getExtensionType();
3498 Result = DAG.getExtLoad(ExtType, NVT,
3499 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003500 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003501 LD->getLoadedVT(),
3502 LD->isVolatile(),
3503 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003504 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003505 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003506 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003507 }
Chris Lattner03c85462005-01-15 05:21:40 +00003508 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003509 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3510 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003511 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003512 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003513 case ISD::SELECT_CC:
3514 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3515 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3516 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003517 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003518 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003519 case ISD::BSWAP:
3520 Tmp1 = Node->getOperand(0);
3521 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3522 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3523 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003524 DAG.getConstant(MVT::getSizeInBits(NVT) -
3525 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003526 TLI.getShiftAmountTy()));
3527 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003528 case ISD::CTPOP:
3529 case ISD::CTTZ:
3530 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003531 // Zero extend the argument
3532 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003533 // Perform the larger operation, then subtract if needed.
3534 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003535 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003536 case ISD::CTPOP:
3537 Result = Tmp1;
3538 break;
3539 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003540 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003541 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003542 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3543 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003544 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003545 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003546 break;
3547 case ISD::CTLZ:
3548 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003549 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003550 DAG.getConstant(MVT::getSizeInBits(NVT) -
3551 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003552 break;
3553 }
3554 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003555 case ISD::EXTRACT_SUBVECTOR:
3556 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003557 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003558 case ISD::EXTRACT_VECTOR_ELT:
3559 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3560 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003561 }
3562
3563 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003564
3565 // Make sure the result is itself legal.
3566 Result = LegalizeOp(Result);
3567
3568 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003569 AddPromotedOperand(Op, Result);
3570 return Result;
3571}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003572
Dan Gohman7f321562007-06-25 16:23:39 +00003573/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3574/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3575/// based on the vector type. The return type of this matches the element type
3576/// of the vector, which may not be legal for the target.
3577SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003578 // We know that operand #0 is the Vec vector. If the index is a constant
3579 // or if the invec is a supported hardware type, we can use it. Otherwise,
3580 // lower to a store then an indexed load.
3581 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003582 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003583
3584 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003585 MVT::ValueType TVT = InVal->getValueType(0);
3586 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003587
Dan Gohman7f321562007-06-25 16:23:39 +00003588 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3589 default: assert(0 && "This action is not supported yet!");
3590 case TargetLowering::Custom: {
3591 Vec = LegalizeOp(Vec);
3592 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3593 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3594 if (Tmp3.Val)
3595 return Tmp3;
3596 break;
3597 }
3598 case TargetLowering::Legal:
3599 if (isTypeLegal(TVT)) {
3600 Vec = LegalizeOp(Vec);
3601 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3602 Op = LegalizeOp(Op);
3603 }
3604 break;
3605 case TargetLowering::Expand:
3606 break;
3607 }
3608
3609 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003610 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003611 Op = ScalarizeVectorOp(Vec);
3612 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3613 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003614 SDOperand Lo, Hi;
3615 SplitVectorOp(Vec, Lo, Hi);
3616 if (CIdx->getValue() < NumElems/2) {
3617 Vec = Lo;
3618 } else {
3619 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003620 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3621 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003622 }
Dan Gohman7f321562007-06-25 16:23:39 +00003623
Chris Lattner15972212006-03-31 17:55:51 +00003624 // It's now an extract from the appropriate high or low part. Recurse.
3625 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003626 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003627 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003628 // Store the value to a temporary stack slot, then LOAD the scalar
3629 // element back out.
3630 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3631 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3632
3633 // Add the offset to the index.
3634 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3635 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3636 DAG.getConstant(EltSize, Idx.getValueType()));
3637 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3638
3639 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003640 }
Dan Gohman7f321562007-06-25 16:23:39 +00003641 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003642}
3643
Dan Gohman7f321562007-06-25 16:23:39 +00003644/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003645/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003646SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003647 // We know that operand #0 is the Vec vector. For now we assume the index
3648 // is a constant and that the extracted result is a supported hardware type.
3649 SDOperand Vec = Op.getOperand(0);
3650 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3651
Dan Gohman7f321562007-06-25 16:23:39 +00003652 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003653
3654 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3655 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003656 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003657 }
3658
3659 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3660 SDOperand Lo, Hi;
3661 SplitVectorOp(Vec, Lo, Hi);
3662 if (CIdx->getValue() < NumElems/2) {
3663 Vec = Lo;
3664 } else {
3665 Vec = Hi;
3666 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3667 }
3668
3669 // It's now an extract from the appropriate high or low part. Recurse.
3670 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003671 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003672}
3673
Nate Begeman750ac1b2006-02-01 07:19:44 +00003674/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3675/// with condition CC on the current target. This usually involves legalizing
3676/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3677/// there may be no choice but to create a new SetCC node to represent the
3678/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3679/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3680void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3681 SDOperand &RHS,
3682 SDOperand &CC) {
3683 SDOperand Tmp1, Tmp2, Result;
3684
3685 switch (getTypeAction(LHS.getValueType())) {
3686 case Legal:
3687 Tmp1 = LegalizeOp(LHS); // LHS
3688 Tmp2 = LegalizeOp(RHS); // RHS
3689 break;
3690 case Promote:
3691 Tmp1 = PromoteOp(LHS); // LHS
3692 Tmp2 = PromoteOp(RHS); // RHS
3693
3694 // If this is an FP compare, the operands have already been extended.
3695 if (MVT::isInteger(LHS.getValueType())) {
3696 MVT::ValueType VT = LHS.getValueType();
3697 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3698
3699 // Otherwise, we have to insert explicit sign or zero extends. Note
3700 // that we could insert sign extends for ALL conditions, but zero extend
3701 // is cheaper on many machines (an AND instead of two shifts), so prefer
3702 // it.
3703 switch (cast<CondCodeSDNode>(CC)->get()) {
3704 default: assert(0 && "Unknown integer comparison!");
3705 case ISD::SETEQ:
3706 case ISD::SETNE:
3707 case ISD::SETUGE:
3708 case ISD::SETUGT:
3709 case ISD::SETULE:
3710 case ISD::SETULT:
3711 // ALL of these operations will work if we either sign or zero extend
3712 // the operands (including the unsigned comparisons!). Zero extend is
3713 // usually a simpler/cheaper operation, so prefer it.
3714 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3715 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3716 break;
3717 case ISD::SETGE:
3718 case ISD::SETGT:
3719 case ISD::SETLT:
3720 case ISD::SETLE:
3721 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3722 DAG.getValueType(VT));
3723 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3724 DAG.getValueType(VT));
3725 break;
3726 }
3727 }
3728 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003729 case Expand: {
3730 MVT::ValueType VT = LHS.getValueType();
3731 if (VT == MVT::f32 || VT == MVT::f64) {
3732 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003733 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003734 switch (cast<CondCodeSDNode>(CC)->get()) {
3735 case ISD::SETEQ:
3736 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003737 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003738 break;
3739 case ISD::SETNE:
3740 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003741 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003742 break;
3743 case ISD::SETGE:
3744 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003745 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003746 break;
3747 case ISD::SETLT:
3748 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003749 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003750 break;
3751 case ISD::SETLE:
3752 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003753 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003754 break;
3755 case ISD::SETGT:
3756 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003757 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003758 break;
3759 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003760 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3761 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003762 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003763 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003764 break;
3765 default:
Evan Cheng56966222007-01-12 02:11:51 +00003766 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003767 switch (cast<CondCodeSDNode>(CC)->get()) {
3768 case ISD::SETONE:
3769 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003770 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003771 // Fallthrough
3772 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003773 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003774 break;
3775 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003776 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003777 break;
3778 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003779 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003780 break;
3781 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003782 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003783 break;
Evan Cheng56966222007-01-12 02:11:51 +00003784 case ISD::SETUEQ:
3785 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00003786 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003787 default: assert(0 && "Unsupported FP setcc!");
3788 }
3789 }
3790
3791 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003792 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003793 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003794 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003795 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00003796 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00003797 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003798 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003799 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003800 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003801 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003802 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00003803 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00003804 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3805 Tmp2 = SDOperand();
3806 }
3807 LHS = Tmp1;
3808 RHS = Tmp2;
3809 return;
3810 }
3811
Nate Begeman750ac1b2006-02-01 07:19:44 +00003812 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3813 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003814 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003815 switch (cast<CondCodeSDNode>(CC)->get()) {
3816 case ISD::SETEQ:
3817 case ISD::SETNE:
3818 if (RHSLo == RHSHi)
3819 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3820 if (RHSCST->isAllOnesValue()) {
3821 // Comparison to -1.
3822 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3823 Tmp2 = RHSLo;
3824 break;
3825 }
3826
3827 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3828 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3829 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3830 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3831 break;
3832 default:
3833 // If this is a comparison of the sign bit, just look at the top part.
3834 // X > -1, x < 0
3835 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3836 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3837 CST->getValue() == 0) || // X < 0
3838 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3839 CST->isAllOnesValue())) { // X > -1
3840 Tmp1 = LHSHi;
3841 Tmp2 = RHSHi;
3842 break;
3843 }
3844
3845 // FIXME: This generated code sucks.
3846 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00003847 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3848 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003849 default: assert(0 && "Unknown integer setcc!");
3850 case ISD::SETLT:
3851 case ISD::SETULT: LowCC = ISD::SETULT; break;
3852 case ISD::SETGT:
3853 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3854 case ISD::SETLE:
3855 case ISD::SETULE: LowCC = ISD::SETULE; break;
3856 case ISD::SETGE:
3857 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3858 }
3859
3860 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3861 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3862 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3863
3864 // NOTE: on targets without efficient SELECT of bools, we can always use
3865 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00003866 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3867 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3868 false, DagCombineInfo);
3869 if (!Tmp1.Val)
3870 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3871 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3872 CCCode, false, DagCombineInfo);
3873 if (!Tmp2.Val)
3874 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3875
3876 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3877 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3878 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3879 (Tmp2C && Tmp2C->getValue() == 0 &&
3880 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3881 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3882 (Tmp2C && Tmp2C->getValue() == 1 &&
3883 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3884 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3885 // low part is known false, returns high part.
3886 // For LE / GE, if high part is known false, ignore the low part.
3887 // For LT / GT, if high part is known true, ignore the low part.
3888 Tmp1 = Tmp2;
3889 Tmp2 = SDOperand();
3890 } else {
3891 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3892 ISD::SETEQ, false, DagCombineInfo);
3893 if (!Result.Val)
3894 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3895 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3896 Result, Tmp1, Tmp2));
3897 Tmp1 = Result;
3898 Tmp2 = SDOperand();
3899 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003900 }
3901 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003902 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003903 LHS = Tmp1;
3904 RHS = Tmp2;
3905}
3906
Chris Lattner35481892005-12-23 00:16:34 +00003907/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003908/// The resultant code need not be legal. Note that SrcOp is the input operand
3909/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003910SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3911 SDOperand SrcOp) {
3912 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003913 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003914
3915 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003916 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003917 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003918 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003919}
3920
Chris Lattner4352cc92006-04-04 17:23:26 +00003921SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3922 // Create a vector sized/aligned stack slot, store the value to element #0,
3923 // then load the whole vector back out.
3924 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003925 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003926 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003927 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003928}
3929
3930
Chris Lattnerce872152006-03-19 06:31:19 +00003931/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3932/// support the operation, but do support the resultant packed vector type.
3933SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3934
3935 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003936 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003937 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003938 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003939 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003940 std::map<SDOperand, std::vector<unsigned> > Values;
3941 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003942 bool isConstant = true;
3943 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3944 SplatValue.getOpcode() != ISD::UNDEF)
3945 isConstant = false;
3946
Evan Cheng033e6812006-03-24 01:17:21 +00003947 for (unsigned i = 1; i < NumElems; ++i) {
3948 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003949 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003950 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003951 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003952 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003953 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003954
3955 // If this isn't a constant element or an undef, we can't use a constant
3956 // pool load.
3957 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3958 V.getOpcode() != ISD::UNDEF)
3959 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003960 }
3961
3962 if (isOnlyLowElement) {
3963 // If the low element is an undef too, then this whole things is an undef.
3964 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3965 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3966 // Otherwise, turn this into a scalar_to_vector node.
3967 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3968 Node->getOperand(0));
3969 }
3970
Chris Lattner2eb86532006-03-24 07:29:17 +00003971 // If all elements are constants, create a load from the constant pool.
3972 if (isConstant) {
3973 MVT::ValueType VT = Node->getValueType(0);
3974 const Type *OpNTy =
3975 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3976 std::vector<Constant*> CV;
3977 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3978 if (ConstantFPSDNode *V =
3979 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3980 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3981 } else if (ConstantSDNode *V =
3982 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003983 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003984 } else {
3985 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3986 CV.push_back(UndefValue::get(OpNTy));
3987 }
3988 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00003989 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00003990 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003991 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003992 }
3993
Chris Lattner87100e02006-03-20 01:52:29 +00003994 if (SplatValue.Val) { // Splat of one value?
3995 // Build the shuffle constant vector: <0, 0, 0, 0>
3996 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003997 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003998 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003999 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004000 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4001 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004002
4003 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004004 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004005 // Get the splatted value into the low element of a vector register.
4006 SDOperand LowValVec =
4007 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4008
4009 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4010 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4011 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4012 SplatMask);
4013 }
4014 }
4015
Evan Cheng033e6812006-03-24 01:17:21 +00004016 // If there are only two unique elements, we may be able to turn this into a
4017 // vector shuffle.
4018 if (Values.size() == 2) {
4019 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4020 MVT::ValueType MaskVT =
4021 MVT::getIntVectorWithNumElements(NumElems);
4022 std::vector<SDOperand> MaskVec(NumElems);
4023 unsigned i = 0;
4024 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4025 E = Values.end(); I != E; ++I) {
4026 for (std::vector<unsigned>::iterator II = I->second.begin(),
4027 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004028 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004029 i += NumElems;
4030 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004031 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4032 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004033
4034 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004035 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4036 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004037 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004038 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4039 E = Values.end(); I != E; ++I) {
4040 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4041 I->first);
4042 Ops.push_back(Op);
4043 }
4044 Ops.push_back(ShuffleMask);
4045
4046 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004047 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4048 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004049 }
4050 }
Chris Lattnerce872152006-03-19 06:31:19 +00004051
4052 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4053 // aligned object on the stack, store each element into it, then load
4054 // the result as a vector.
4055 MVT::ValueType VT = Node->getValueType(0);
4056 // Create the stack frame object.
4057 SDOperand FIPtr = CreateStackTemporary(VT);
4058
4059 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004060 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004061 unsigned TypeByteSize =
4062 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004063 // Store (in the right endianness) the elements to memory.
4064 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4065 // Ignore undef elements.
4066 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4067
Chris Lattner841c8822006-03-22 01:46:54 +00004068 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004069
4070 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4071 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4072
Evan Cheng786225a2006-10-05 23:01:46 +00004073 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004074 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004075 }
4076
4077 SDOperand StoreChain;
4078 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004079 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4080 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004081 else
4082 StoreChain = DAG.getEntryNode();
4083
4084 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004085 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004086}
4087
4088/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4089/// specified value type.
4090SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4091 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4092 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004093 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004094 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004095 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004096 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4097}
4098
Chris Lattner5b359c62005-04-02 04:00:59 +00004099void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4100 SDOperand Op, SDOperand Amt,
4101 SDOperand &Lo, SDOperand &Hi) {
4102 // Expand the subcomponents.
4103 SDOperand LHSL, LHSH;
4104 ExpandOp(Op, LHSL, LHSH);
4105
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004106 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004107 MVT::ValueType VT = LHSL.getValueType();
4108 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004109 Hi = Lo.getValue(1);
4110}
4111
4112
Chris Lattnere34b3962005-01-19 04:19:40 +00004113/// ExpandShift - Try to find a clever way to expand this shift operation out to
4114/// smaller elements. If we can't find a way that is more efficient than a
4115/// libcall on this target, return false. Otherwise, return true with the
4116/// low-parts expanded into Lo and Hi.
4117bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4118 SDOperand &Lo, SDOperand &Hi) {
4119 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4120 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004121
Chris Lattnere34b3962005-01-19 04:19:40 +00004122 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004123 SDOperand ShAmt = LegalizeOp(Amt);
4124 MVT::ValueType ShTy = ShAmt.getValueType();
4125 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4126 unsigned NVTBits = MVT::getSizeInBits(NVT);
4127
4128 // Handle the case when Amt is an immediate. Other cases are currently broken
4129 // and are disabled.
4130 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4131 unsigned Cst = CN->getValue();
4132 // Expand the incoming operand to be shifted, so that we have its parts
4133 SDOperand InL, InH;
4134 ExpandOp(Op, InL, InH);
4135 switch(Opc) {
4136 case ISD::SHL:
4137 if (Cst > VTBits) {
4138 Lo = DAG.getConstant(0, NVT);
4139 Hi = DAG.getConstant(0, NVT);
4140 } else if (Cst > NVTBits) {
4141 Lo = DAG.getConstant(0, NVT);
4142 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004143 } else if (Cst == NVTBits) {
4144 Lo = DAG.getConstant(0, NVT);
4145 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004146 } else {
4147 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4148 Hi = DAG.getNode(ISD::OR, NVT,
4149 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4150 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4151 }
4152 return true;
4153 case ISD::SRL:
4154 if (Cst > VTBits) {
4155 Lo = DAG.getConstant(0, NVT);
4156 Hi = DAG.getConstant(0, NVT);
4157 } else if (Cst > NVTBits) {
4158 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4159 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004160 } else if (Cst == NVTBits) {
4161 Lo = InH;
4162 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004163 } else {
4164 Lo = DAG.getNode(ISD::OR, NVT,
4165 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4166 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4167 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4168 }
4169 return true;
4170 case ISD::SRA:
4171 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004172 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004173 DAG.getConstant(NVTBits-1, ShTy));
4174 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004175 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004176 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004177 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004178 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004179 } else if (Cst == NVTBits) {
4180 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004181 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004182 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004183 } else {
4184 Lo = DAG.getNode(ISD::OR, NVT,
4185 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4186 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4187 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4188 }
4189 return true;
4190 }
4191 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004192
4193 // Okay, the shift amount isn't constant. However, if we can tell that it is
4194 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4195 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004196 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004197
4198 // If we know that the high bit of the shift amount is one, then we can do
4199 // this as a couple of simple shifts.
4200 if (KnownOne & Mask) {
4201 // Mask out the high bit, which we know is set.
4202 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4203 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4204
4205 // Expand the incoming operand to be shifted, so that we have its parts
4206 SDOperand InL, InH;
4207 ExpandOp(Op, InL, InH);
4208 switch(Opc) {
4209 case ISD::SHL:
4210 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4211 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4212 return true;
4213 case ISD::SRL:
4214 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4215 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4216 return true;
4217 case ISD::SRA:
4218 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4219 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4220 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4221 return true;
4222 }
4223 }
4224
4225 // If we know that the high bit of the shift amount is zero, then we can do
4226 // this as a couple of simple shifts.
4227 if (KnownZero & Mask) {
4228 // Compute 32-amt.
4229 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4230 DAG.getConstant(NVTBits, Amt.getValueType()),
4231 Amt);
4232
4233 // Expand the incoming operand to be shifted, so that we have its parts
4234 SDOperand InL, InH;
4235 ExpandOp(Op, InL, InH);
4236 switch(Opc) {
4237 case ISD::SHL:
4238 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4239 Hi = DAG.getNode(ISD::OR, NVT,
4240 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4241 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4242 return true;
4243 case ISD::SRL:
4244 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4245 Lo = DAG.getNode(ISD::OR, NVT,
4246 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4247 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4248 return true;
4249 case ISD::SRA:
4250 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4251 Lo = DAG.getNode(ISD::OR, NVT,
4252 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4253 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4254 return true;
4255 }
4256 }
4257
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004258 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004259}
Chris Lattner77e77a62005-01-21 06:05:23 +00004260
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004261
Chris Lattner77e77a62005-01-21 06:05:23 +00004262// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4263// does not fit into a register, return the lo part and set the hi part to the
4264// by-reg argument. If it does fit into a single register, return the result
4265// and leave the Hi part unset.
4266SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004267 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004268 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4269 // The input chain to this libcall is the entry node of the function.
4270 // Legalizing the call will automatically add the previous call to the
4271 // dependence.
4272 SDOperand InChain = DAG.getEntryNode();
4273
Chris Lattner77e77a62005-01-21 06:05:23 +00004274 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004275 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004276 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4277 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4278 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004279 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004280 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004281 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004282 }
4283 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004284
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004285 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004286 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004287 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004288 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004289 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004290
Chris Lattner6831a812006-02-13 09:18:02 +00004291 // Legalize the call sequence, starting with the chain. This will advance
4292 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4293 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4294 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004295 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004296 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004297 default: assert(0 && "Unknown thing");
4298 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004299 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004300 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004301 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004302 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004303 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004304 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004305 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004306}
4307
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004308
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004309/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4310///
Chris Lattner77e77a62005-01-21 06:05:23 +00004311SDOperand SelectionDAGLegalize::
4312ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004313 assert(getTypeAction(Source.getValueType()) == Expand &&
4314 "This is not an expansion!");
4315 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4316
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004317 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004318 assert(Source.getValueType() == MVT::i64 &&
4319 "This only works for 64-bit -> FP");
4320 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4321 // incoming integer is set. To handle this, we dynamically test to see if
4322 // it is set, and, if so, add a fudge factor.
4323 SDOperand Lo, Hi;
4324 ExpandOp(Source, Lo, Hi);
4325
Chris Lattner66de05b2005-05-13 04:45:13 +00004326 // If this is unsigned, and not supported, first perform the conversion to
4327 // signed, then adjust the result if the sign bit is set.
4328 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4329 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4330
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004331 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4332 DAG.getConstant(0, Hi.getValueType()),
4333 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004334 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4335 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4336 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004337 uint64_t FF = 0x5f800000ULL;
4338 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004339 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004340
Chris Lattner5839bf22005-08-26 17:15:30 +00004341 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004342 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4343 SDOperand FudgeInReg;
4344 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004345 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004346 else {
4347 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004348 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004349 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004350 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004351 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004352 MVT::ValueType SCVT = SignedConv.getValueType();
4353 if (SCVT != DestTy) {
4354 // Destination type needs to be expanded as well. The FADD now we are
4355 // constructing will be expanded into a libcall.
4356 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4357 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4358 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4359 SignedConv, SignedConv.getValue(1));
4360 }
4361 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4362 }
Chris Lattner473a9902005-09-29 06:44:39 +00004363 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004364 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004365
Chris Lattnera88a2602005-05-14 05:33:54 +00004366 // Check to see if the target has a custom way to lower this. If so, use it.
4367 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4368 default: assert(0 && "This action not implemented for this operation!");
4369 case TargetLowering::Legal:
4370 case TargetLowering::Expand:
4371 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004372 case TargetLowering::Custom: {
4373 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4374 Source), DAG);
4375 if (NV.Val)
4376 return LegalizeOp(NV);
4377 break; // The target decided this was legal after all
4378 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004379 }
4380
Chris Lattner13689e22005-05-12 07:00:44 +00004381 // Expand the source, then glue it back together for the call. We must expand
4382 // the source in case it is shared (this pass of legalize must traverse it).
4383 SDOperand SrcLo, SrcHi;
4384 ExpandOp(Source, SrcLo, SrcHi);
4385 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4386
Evan Cheng56966222007-01-12 02:11:51 +00004387 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004388 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004389 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004390 else {
4391 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004392 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004393 }
Chris Lattner6831a812006-02-13 09:18:02 +00004394
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004395 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004396 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4397 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004398 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4399 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004400}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004401
Chris Lattner22cde6a2006-01-28 08:25:58 +00004402/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4403/// INT_TO_FP operation of the specified operand when the target requests that
4404/// we expand it. At this point, we know that the result and operand types are
4405/// legal for the target.
4406SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4407 SDOperand Op0,
4408 MVT::ValueType DestVT) {
4409 if (Op0.getValueType() == MVT::i32) {
4410 // simple 32-bit [signed|unsigned] integer to float/double expansion
4411
Chris Lattner58092e32007-01-20 22:35:55 +00004412 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004413 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004414 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4415 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004416 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004417 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004418 // get address of 8 byte buffer
4419 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4420 // word offset constant for Hi/Lo address computation
4421 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4422 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004423 SDOperand Hi = StackSlot;
4424 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4425 if (TLI.isLittleEndian())
4426 std::swap(Hi, Lo);
4427
Chris Lattner22cde6a2006-01-28 08:25:58 +00004428 // if signed map to unsigned space
4429 SDOperand Op0Mapped;
4430 if (isSigned) {
4431 // constant used to invert sign bit (signed to unsigned mapping)
4432 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4433 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4434 } else {
4435 Op0Mapped = Op0;
4436 }
4437 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004438 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004439 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004440 // initial hi portion of constructed double
4441 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4442 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004443 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004444 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004445 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004446 // FP constant to bias correct the final result
4447 SDOperand Bias = DAG.getConstantFP(isSigned ?
4448 BitsToDouble(0x4330000080000000ULL)
4449 : BitsToDouble(0x4330000000000000ULL),
4450 MVT::f64);
4451 // subtract the bias
4452 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4453 // final result
4454 SDOperand Result;
4455 // handle final rounding
4456 if (DestVT == MVT::f64) {
4457 // do nothing
4458 Result = Sub;
4459 } else {
4460 // if f32 then cast to f32
4461 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4462 }
4463 return Result;
4464 }
4465 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4466 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4467
4468 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4469 DAG.getConstant(0, Op0.getValueType()),
4470 ISD::SETLT);
4471 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4472 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4473 SignSet, Four, Zero);
4474
4475 // If the sign bit of the integer is set, the large number will be treated
4476 // as a negative number. To counteract this, the dynamic code adds an
4477 // offset depending on the data type.
4478 uint64_t FF;
4479 switch (Op0.getValueType()) {
4480 default: assert(0 && "Unsupported integer type!");
4481 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4482 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4483 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4484 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4485 }
4486 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004487 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004488
4489 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4490 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4491 SDOperand FudgeInReg;
4492 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004493 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004494 else {
4495 assert(DestVT == MVT::f64 && "Unexpected conversion");
4496 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4497 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004498 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004499 }
4500
4501 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4502}
4503
4504/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4505/// *INT_TO_FP operation of the specified operand when the target requests that
4506/// we promote it. At this point, we know that the result and operand types are
4507/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4508/// operation that takes a larger input.
4509SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4510 MVT::ValueType DestVT,
4511 bool isSigned) {
4512 // First step, figure out the appropriate *INT_TO_FP operation to use.
4513 MVT::ValueType NewInTy = LegalOp.getValueType();
4514
4515 unsigned OpToUse = 0;
4516
4517 // Scan for the appropriate larger type to use.
4518 while (1) {
4519 NewInTy = (MVT::ValueType)(NewInTy+1);
4520 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4521
4522 // If the target supports SINT_TO_FP of this type, use it.
4523 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4524 default: break;
4525 case TargetLowering::Legal:
4526 if (!TLI.isTypeLegal(NewInTy))
4527 break; // Can't use this datatype.
4528 // FALL THROUGH.
4529 case TargetLowering::Custom:
4530 OpToUse = ISD::SINT_TO_FP;
4531 break;
4532 }
4533 if (OpToUse) break;
4534 if (isSigned) continue;
4535
4536 // If the target supports UINT_TO_FP of this type, use it.
4537 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4538 default: break;
4539 case TargetLowering::Legal:
4540 if (!TLI.isTypeLegal(NewInTy))
4541 break; // Can't use this datatype.
4542 // FALL THROUGH.
4543 case TargetLowering::Custom:
4544 OpToUse = ISD::UINT_TO_FP;
4545 break;
4546 }
4547 if (OpToUse) break;
4548
4549 // Otherwise, try a larger type.
4550 }
4551
4552 // Okay, we found the operation and type to use. Zero extend our input to the
4553 // desired type then run the operation on it.
4554 return DAG.getNode(OpToUse, DestVT,
4555 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4556 NewInTy, LegalOp));
4557}
4558
4559/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4560/// FP_TO_*INT operation of the specified operand when the target requests that
4561/// we promote it. At this point, we know that the result and operand types are
4562/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4563/// operation that returns a larger result.
4564SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4565 MVT::ValueType DestVT,
4566 bool isSigned) {
4567 // First step, figure out the appropriate FP_TO*INT operation to use.
4568 MVT::ValueType NewOutTy = DestVT;
4569
4570 unsigned OpToUse = 0;
4571
4572 // Scan for the appropriate larger type to use.
4573 while (1) {
4574 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4575 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4576
4577 // If the target supports FP_TO_SINT returning this type, use it.
4578 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4579 default: break;
4580 case TargetLowering::Legal:
4581 if (!TLI.isTypeLegal(NewOutTy))
4582 break; // Can't use this datatype.
4583 // FALL THROUGH.
4584 case TargetLowering::Custom:
4585 OpToUse = ISD::FP_TO_SINT;
4586 break;
4587 }
4588 if (OpToUse) break;
4589
4590 // If the target supports FP_TO_UINT of this type, use it.
4591 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4592 default: break;
4593 case TargetLowering::Legal:
4594 if (!TLI.isTypeLegal(NewOutTy))
4595 break; // Can't use this datatype.
4596 // FALL THROUGH.
4597 case TargetLowering::Custom:
4598 OpToUse = ISD::FP_TO_UINT;
4599 break;
4600 }
4601 if (OpToUse) break;
4602
4603 // Otherwise, try a larger type.
4604 }
4605
4606 // Okay, we found the operation and type to use. Truncate the result of the
4607 // extended FP_TO_*INT operation to the desired size.
4608 return DAG.getNode(ISD::TRUNCATE, DestVT,
4609 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4610}
4611
4612/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4613///
4614SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4615 MVT::ValueType VT = Op.getValueType();
4616 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4617 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4618 switch (VT) {
4619 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4620 case MVT::i16:
4621 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4622 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4623 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4624 case MVT::i32:
4625 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4626 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4627 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4628 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4629 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4630 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4631 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4632 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4633 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4634 case MVT::i64:
4635 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4636 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4637 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4638 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4639 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4640 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4641 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4642 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4643 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4644 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4645 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4646 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4647 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4648 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4649 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4650 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4651 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4652 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4653 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4654 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4655 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4656 }
4657}
4658
4659/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4660///
4661SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4662 switch (Opc) {
4663 default: assert(0 && "Cannot expand this yet!");
4664 case ISD::CTPOP: {
4665 static const uint64_t mask[6] = {
4666 0x5555555555555555ULL, 0x3333333333333333ULL,
4667 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4668 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4669 };
4670 MVT::ValueType VT = Op.getValueType();
4671 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004672 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004673 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4674 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4675 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4676 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4677 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4678 DAG.getNode(ISD::AND, VT,
4679 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4680 }
4681 return Op;
4682 }
4683 case ISD::CTLZ: {
4684 // for now, we do this:
4685 // x = x | (x >> 1);
4686 // x = x | (x >> 2);
4687 // ...
4688 // x = x | (x >>16);
4689 // x = x | (x >>32); // for 64-bit input
4690 // return popcount(~x);
4691 //
4692 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4693 MVT::ValueType VT = Op.getValueType();
4694 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004695 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004696 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4697 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4698 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4699 }
4700 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4701 return DAG.getNode(ISD::CTPOP, VT, Op);
4702 }
4703 case ISD::CTTZ: {
4704 // for now, we use: { return popcount(~x & (x - 1)); }
4705 // unless the target has ctlz but not ctpop, in which case we use:
4706 // { return 32 - nlz(~x & (x-1)); }
4707 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4708 MVT::ValueType VT = Op.getValueType();
4709 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4710 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4711 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4712 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4713 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4714 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4715 TLI.isOperationLegal(ISD::CTLZ, VT))
4716 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004717 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004718 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4719 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4720 }
4721 }
4722}
Chris Lattnere34b3962005-01-19 04:19:40 +00004723
Chris Lattner3e928bb2005-01-07 07:47:09 +00004724/// ExpandOp - Expand the specified SDOperand into its two component pieces
4725/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4726/// LegalizeNodes map is filled in for any results that are not expanded, the
4727/// ExpandedNodes map is filled in for any results that are expanded, and the
4728/// Lo/Hi values are returned.
4729void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4730 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004731 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004732 SDNode *Node = Op.Val;
4733 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004734 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004735 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004736 "Cannot expand to FP value or to larger int value!");
4737
Chris Lattner6fdcb252005-09-02 20:32:45 +00004738 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004739 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004740 = ExpandedNodes.find(Op);
4741 if (I != ExpandedNodes.end()) {
4742 Lo = I->second.first;
4743 Hi = I->second.second;
4744 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004745 }
4746
Chris Lattner3e928bb2005-01-07 07:47:09 +00004747 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004748 case ISD::CopyFromReg:
4749 assert(0 && "CopyFromReg must be legal!");
4750 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004751#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004752 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004753#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004754 assert(0 && "Do not know how to expand this operator!");
4755 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004756 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004757 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004758 Lo = DAG.getNode(ISD::UNDEF, NVT);
4759 Hi = DAG.getNode(ISD::UNDEF, NVT);
4760 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004761 case ISD::Constant: {
4762 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4763 Lo = DAG.getConstant(Cst, NVT);
4764 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4765 break;
4766 }
Evan Cheng00495212006-12-12 21:32:44 +00004767 case ISD::ConstantFP: {
4768 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004769 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004770 if (getTypeAction(Lo.getValueType()) == Expand)
4771 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004772 break;
4773 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004774 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004775 // Return the operands.
4776 Lo = Node->getOperand(0);
4777 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004778 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004779
4780 case ISD::SIGN_EXTEND_INREG:
4781 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004782 // sext_inreg the low part if needed.
4783 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4784
4785 // The high part gets the sign extension from the lo-part. This handles
4786 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004787 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4788 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4789 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004790 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004791
Nate Begemand88fc032006-01-14 03:14:10 +00004792 case ISD::BSWAP: {
4793 ExpandOp(Node->getOperand(0), Lo, Hi);
4794 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4795 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4796 Lo = TempLo;
4797 break;
4798 }
4799
Chris Lattneredb1add2005-05-11 04:51:16 +00004800 case ISD::CTPOP:
4801 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004802 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4803 DAG.getNode(ISD::CTPOP, NVT, Lo),
4804 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004805 Hi = DAG.getConstant(0, NVT);
4806 break;
4807
Chris Lattner39a8f332005-05-12 19:05:01 +00004808 case ISD::CTLZ: {
4809 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004810 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004811 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4812 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004813 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4814 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004815 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4816 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4817
4818 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4819 Hi = DAG.getConstant(0, NVT);
4820 break;
4821 }
4822
4823 case ISD::CTTZ: {
4824 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004825 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004826 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4827 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004828 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4829 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004830 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4831 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4832
4833 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4834 Hi = DAG.getConstant(0, NVT);
4835 break;
4836 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004837
Nate Begemanacc398c2006-01-25 18:21:52 +00004838 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004839 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4840 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004841 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4842 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4843
4844 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004845 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004846 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4847 if (!TLI.isLittleEndian())
4848 std::swap(Lo, Hi);
4849 break;
4850 }
4851
Chris Lattner3e928bb2005-01-07 07:47:09 +00004852 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004853 LoadSDNode *LD = cast<LoadSDNode>(Node);
4854 SDOperand Ch = LD->getChain(); // Legalize the chain.
4855 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4856 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004857 int SVOffset = LD->getSrcValueOffset();
4858 unsigned Alignment = LD->getAlignment();
4859 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004860
Evan Cheng466685d2006-10-09 20:57:25 +00004861 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004862 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4863 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00004864 if (VT == MVT::f32 || VT == MVT::f64) {
4865 // f32->i32 or f64->i64 one to one expansion.
4866 // Remember that we legalized the chain.
4867 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004868 // Recursively expand the new load.
4869 if (getTypeAction(NVT) == Expand)
4870 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004871 break;
4872 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004873
Evan Cheng466685d2006-10-09 20:57:25 +00004874 // Increment the pointer to the other half.
4875 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4876 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4877 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00004878 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004879 if (Alignment > IncrementSize)
4880 Alignment = IncrementSize;
4881 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4882 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004883
Evan Cheng466685d2006-10-09 20:57:25 +00004884 // Build a factor node to remember that this load is independent of the
4885 // other one.
4886 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4887 Hi.getValue(1));
4888
4889 // Remember that we legalized the chain.
4890 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4891 if (!TLI.isLittleEndian())
4892 std::swap(Lo, Hi);
4893 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004894 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004895
4896 if (VT == MVT::f64 && EVT == MVT::f32) {
4897 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4898 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004899 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00004900 // Remember that we legalized the chain.
4901 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4902 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4903 break;
4904 }
Evan Cheng466685d2006-10-09 20:57:25 +00004905
4906 if (EVT == NVT)
4907 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004908 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004909 else
4910 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004911 SVOffset, EVT, isVolatile,
4912 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004913
4914 // Remember that we legalized the chain.
4915 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4916
4917 if (ExtType == ISD::SEXTLOAD) {
4918 // The high part is obtained by SRA'ing all but one of the bits of the
4919 // lo part.
4920 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4921 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4922 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4923 } else if (ExtType == ISD::ZEXTLOAD) {
4924 // The high part is just a zero.
4925 Hi = DAG.getConstant(0, NVT);
4926 } else /* if (ExtType == ISD::EXTLOAD) */ {
4927 // The high part is undefined.
4928 Hi = DAG.getNode(ISD::UNDEF, NVT);
4929 }
4930 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004931 break;
4932 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004933 case ISD::AND:
4934 case ISD::OR:
4935 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4936 SDOperand LL, LH, RL, RH;
4937 ExpandOp(Node->getOperand(0), LL, LH);
4938 ExpandOp(Node->getOperand(1), RL, RH);
4939 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4940 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4941 break;
4942 }
4943 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004944 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004945 ExpandOp(Node->getOperand(1), LL, LH);
4946 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004947 if (getTypeAction(NVT) == Expand)
4948 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004949 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004950 if (VT != MVT::f32)
4951 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004952 break;
4953 }
Nate Begeman9373a812005-08-10 20:51:12 +00004954 case ISD::SELECT_CC: {
4955 SDOperand TL, TH, FL, FH;
4956 ExpandOp(Node->getOperand(2), TL, TH);
4957 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004958 if (getTypeAction(NVT) == Expand)
4959 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004960 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4961 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004962 if (VT != MVT::f32)
4963 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4964 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004965 break;
4966 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004967 case ISD::ANY_EXTEND:
4968 // The low part is any extension of the input (which degenerates to a copy).
4969 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4970 // The high part is undefined.
4971 Hi = DAG.getNode(ISD::UNDEF, NVT);
4972 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004973 case ISD::SIGN_EXTEND: {
4974 // The low part is just a sign extension of the input (which degenerates to
4975 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004976 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004977
Chris Lattner3e928bb2005-01-07 07:47:09 +00004978 // The high part is obtained by SRA'ing all but one of the bits of the lo
4979 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004980 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004981 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4982 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004983 break;
4984 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004985 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004986 // The low part is just a zero extension of the input (which degenerates to
4987 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004988 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004989
Chris Lattner3e928bb2005-01-07 07:47:09 +00004990 // The high part is just a zero.
4991 Hi = DAG.getConstant(0, NVT);
4992 break;
Chris Lattner35481892005-12-23 00:16:34 +00004993
Chris Lattner4c948eb2007-02-13 23:55:16 +00004994 case ISD::TRUNCATE: {
4995 // The input value must be larger than this value. Expand *it*.
4996 SDOperand NewLo;
4997 ExpandOp(Node->getOperand(0), NewLo, Hi);
4998
4999 // The low part is now either the right size, or it is closer. If not the
5000 // right size, make an illegal truncate so we recursively expand it.
5001 if (NewLo.getValueType() != Node->getValueType(0))
5002 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5003 ExpandOp(NewLo, Lo, Hi);
5004 break;
5005 }
5006
Chris Lattner35481892005-12-23 00:16:34 +00005007 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005008 SDOperand Tmp;
5009 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5010 // If the target wants to, allow it to lower this itself.
5011 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5012 case Expand: assert(0 && "cannot expand FP!");
5013 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5014 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5015 }
5016 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5017 }
5018
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005019 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005020 if (VT == MVT::f32 || VT == MVT::f64) {
5021 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005022 if (getTypeAction(NVT) == Expand)
5023 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005024 break;
5025 }
5026
5027 // If source operand will be expanded to the same type as VT, i.e.
5028 // i64 <- f64, i32 <- f32, expand the source operand instead.
5029 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5030 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5031 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005032 break;
5033 }
5034
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005035 // Turn this into a load/store pair by default.
5036 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005037 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005038
Chris Lattner35481892005-12-23 00:16:34 +00005039 ExpandOp(Tmp, Lo, Hi);
5040 break;
5041 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005042
Chris Lattner8137c9e2006-01-28 05:07:51 +00005043 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005044 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5045 TargetLowering::Custom &&
5046 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005047 Lo = TLI.LowerOperation(Op, DAG);
5048 assert(Lo.Val && "Node must be custom expanded!");
5049 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005050 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005051 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005052 break;
5053
Chris Lattner4e6c7462005-01-08 19:27:05 +00005054 // These operators cannot be expanded directly, emit them as calls to
5055 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005056 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005057 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005058 SDOperand Op;
5059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5060 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005061 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5062 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005063 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005064
Chris Lattnerf20d1832005-07-30 01:40:57 +00005065 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5066
Chris Lattner80a3e942005-07-29 00:33:32 +00005067 // Now that the custom expander is done, expand the result, which is still
5068 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005069 if (Op.Val) {
5070 ExpandOp(Op, Lo, Hi);
5071 break;
5072 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005073 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005074
Evan Cheng56966222007-01-12 02:11:51 +00005075 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005076 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005077 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005078 else
Evan Cheng56966222007-01-12 02:11:51 +00005079 LC = RTLIB::FPTOSINT_F64_I64;
5080 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5081 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005082 break;
Evan Cheng56966222007-01-12 02:11:51 +00005083 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005084
Evan Cheng56966222007-01-12 02:11:51 +00005085 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005086 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005087 SDOperand Op;
5088 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5089 case Expand: assert(0 && "cannot expand FP!");
5090 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5091 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5092 }
5093
5094 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5095
5096 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005097 if (Op.Val) {
5098 ExpandOp(Op, Lo, Hi);
5099 break;
5100 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005101 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005102
Evan Cheng56966222007-01-12 02:11:51 +00005103 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005104 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005105 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005106 else
Evan Cheng56966222007-01-12 02:11:51 +00005107 LC = RTLIB::FPTOUINT_F64_I64;
5108 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5109 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005110 break;
Evan Cheng56966222007-01-12 02:11:51 +00005111 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005112
Evan Cheng05a2d562006-01-09 18:31:59 +00005113 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005114 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005115 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005116 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005117 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005118 Op = TLI.LowerOperation(Op, DAG);
5119 if (Op.Val) {
5120 // Now that the custom expander is done, expand the result, which is
5121 // still VT.
5122 ExpandOp(Op, Lo, Hi);
5123 break;
5124 }
5125 }
5126
Chris Lattner79980b02006-09-13 03:50:39 +00005127 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5128 // this X << 1 as X+X.
5129 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5130 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5131 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5132 SDOperand LoOps[2], HiOps[3];
5133 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5134 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5135 LoOps[1] = LoOps[0];
5136 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5137
5138 HiOps[1] = HiOps[0];
5139 HiOps[2] = Lo.getValue(1);
5140 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5141 break;
5142 }
5143 }
5144
Chris Lattnere34b3962005-01-19 04:19:40 +00005145 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005146 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005147 break;
Chris Lattner47599822005-04-02 03:38:53 +00005148
5149 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005150 TargetLowering::LegalizeAction Action =
5151 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5152 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5153 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005154 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005155 break;
5156 }
5157
Chris Lattnere34b3962005-01-19 04:19:40 +00005158 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005159 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5160 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005161 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005162 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005163
Evan Cheng05a2d562006-01-09 18:31:59 +00005164 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005165 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005166 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005167 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005168 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005169 Op = TLI.LowerOperation(Op, DAG);
5170 if (Op.Val) {
5171 // Now that the custom expander is done, expand the result, which is
5172 // still VT.
5173 ExpandOp(Op, Lo, Hi);
5174 break;
5175 }
5176 }
5177
Chris Lattnere34b3962005-01-19 04:19:40 +00005178 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005179 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005180 break;
Chris Lattner47599822005-04-02 03:38:53 +00005181
5182 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005183 TargetLowering::LegalizeAction Action =
5184 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5185 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5186 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005187 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005188 break;
5189 }
5190
Chris Lattnere34b3962005-01-19 04:19:40 +00005191 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005192 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5193 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005194 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005195 }
5196
5197 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005198 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005199 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005200 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005201 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005202 Op = TLI.LowerOperation(Op, DAG);
5203 if (Op.Val) {
5204 // Now that the custom expander is done, expand the result, which is
5205 // still VT.
5206 ExpandOp(Op, Lo, Hi);
5207 break;
5208 }
5209 }
5210
Chris Lattnere34b3962005-01-19 04:19:40 +00005211 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005212 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005213 break;
Chris Lattner47599822005-04-02 03:38:53 +00005214
5215 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005216 TargetLowering::LegalizeAction Action =
5217 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5218 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5219 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005220 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005221 break;
5222 }
5223
Chris Lattnere34b3962005-01-19 04:19:40 +00005224 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005225 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5226 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005227 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005228 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005229
Misha Brukmanedf128a2005-04-21 22:36:52 +00005230 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005231 case ISD::SUB: {
5232 // If the target wants to custom expand this, let them.
5233 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5234 TargetLowering::Custom) {
5235 Op = TLI.LowerOperation(Op, DAG);
5236 if (Op.Val) {
5237 ExpandOp(Op, Lo, Hi);
5238 break;
5239 }
5240 }
5241
5242 // Expand the subcomponents.
5243 SDOperand LHSL, LHSH, RHSL, RHSH;
5244 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5245 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005246 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5247 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005248 LoOps[0] = LHSL;
5249 LoOps[1] = RHSL;
5250 HiOps[0] = LHSH;
5251 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005252 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005253 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005254 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005255 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005256 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005257 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005258 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005259 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005260 }
Chris Lattner84f67882005-01-20 18:52:28 +00005261 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005262 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005263
5264 case ISD::ADDC:
5265 case ISD::SUBC: {
5266 // Expand the subcomponents.
5267 SDOperand LHSL, LHSH, RHSL, RHSH;
5268 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5269 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5270 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5271 SDOperand LoOps[2] = { LHSL, RHSL };
5272 SDOperand HiOps[3] = { LHSH, RHSH };
5273
5274 if (Node->getOpcode() == ISD::ADDC) {
5275 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5276 HiOps[2] = Lo.getValue(1);
5277 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5278 } else {
5279 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5280 HiOps[2] = Lo.getValue(1);
5281 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5282 }
5283 // Remember that we legalized the flag.
5284 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5285 break;
5286 }
5287 case ISD::ADDE:
5288 case ISD::SUBE: {
5289 // Expand the subcomponents.
5290 SDOperand LHSL, LHSH, RHSL, RHSH;
5291 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5292 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5293 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5294 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5295 SDOperand HiOps[3] = { LHSH, RHSH };
5296
5297 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5298 HiOps[2] = Lo.getValue(1);
5299 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5300
5301 // Remember that we legalized the flag.
5302 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5303 break;
5304 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005305 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005306 // If the target wants to custom expand this, let them.
5307 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005308 SDOperand New = TLI.LowerOperation(Op, DAG);
5309 if (New.Val) {
5310 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005311 break;
5312 }
5313 }
5314
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005315 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5316 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005317 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005318 SDOperand LL, LH, RL, RH;
5319 ExpandOp(Node->getOperand(0), LL, LH);
5320 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005321 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005322 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005323 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5324 // extended the sign bit of the low half through the upper half, and if so
5325 // emit a MULHS instead of the alternate sequence that is valid for any
5326 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005327 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005328 // is RH an extension of the sign bit of RL?
5329 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5330 RH.getOperand(1).getOpcode() == ISD::Constant &&
5331 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5332 // is LH an extension of the sign bit of LL?
5333 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5334 LH.getOperand(1).getOpcode() == ISD::Constant &&
5335 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005336 // Low part:
5337 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5338 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005339 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005340 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005341 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005342 // Low part:
5343 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5344
5345 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005346 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5347 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5348 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5349 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5350 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005351 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005352 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005353 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005354
Evan Cheng56966222007-01-12 02:11:51 +00005355 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5356 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005357 break;
5358 }
Evan Cheng56966222007-01-12 02:11:51 +00005359 case ISD::SDIV:
5360 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5361 break;
5362 case ISD::UDIV:
5363 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5364 break;
5365 case ISD::SREM:
5366 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5367 break;
5368 case ISD::UREM:
5369 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5370 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005371
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005372 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005373 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5374 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5375 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005376 break;
5377 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005378 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5379 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5380 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005381 break;
5382 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005383 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5384 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5385 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005386 break;
5387 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005388 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5389 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5390 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005391 break;
5392 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005393 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005394 break;
5395 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005396 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005397 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005398 case ISD::FSQRT:
5399 case ISD::FSIN:
5400 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005401 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005402 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005403 case ISD::FSQRT:
5404 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5405 break;
5406 case ISD::FSIN:
5407 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5408 break;
5409 case ISD::FCOS:
5410 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5411 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005412 default: assert(0 && "Unreachable!");
5413 }
Evan Cheng56966222007-01-12 02:11:51 +00005414 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005415 break;
5416 }
Evan Cheng966bf242006-12-16 00:52:40 +00005417 case ISD::FABS: {
5418 SDOperand Mask = (VT == MVT::f64)
5419 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5420 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5421 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5422 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5423 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5424 if (getTypeAction(NVT) == Expand)
5425 ExpandOp(Lo, Lo, Hi);
5426 break;
5427 }
5428 case ISD::FNEG: {
5429 SDOperand Mask = (VT == MVT::f64)
5430 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5431 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5432 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5433 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5434 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5435 if (getTypeAction(NVT) == Expand)
5436 ExpandOp(Lo, Lo, Hi);
5437 break;
5438 }
Evan Cheng912095b2007-01-04 21:56:39 +00005439 case ISD::FCOPYSIGN: {
5440 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5441 if (getTypeAction(NVT) == Expand)
5442 ExpandOp(Lo, Lo, Hi);
5443 break;
5444 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005445 case ISD::SINT_TO_FP:
5446 case ISD::UINT_TO_FP: {
5447 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5448 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005449 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005450 if (Node->getOperand(0).getValueType() == MVT::i64) {
5451 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005452 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005453 else
Evan Cheng56966222007-01-12 02:11:51 +00005454 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005455 } else {
5456 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005457 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005458 else
Evan Cheng56966222007-01-12 02:11:51 +00005459 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005460 }
5461
5462 // Promote the operand if needed.
5463 if (getTypeAction(SrcVT) == Promote) {
5464 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5465 Tmp = isSigned
5466 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5467 DAG.getValueType(SrcVT))
5468 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5469 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5470 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005471
5472 const char *LibCall = TLI.getLibcallName(LC);
5473 if (LibCall)
5474 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5475 else {
5476 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5477 Node->getOperand(0));
5478 if (getTypeAction(Lo.getValueType()) == Expand)
5479 ExpandOp(Lo, Lo, Hi);
5480 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005481 break;
5482 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005483 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005484
Chris Lattner83397362005-12-21 18:02:52 +00005485 // Make sure the resultant values have been legalized themselves, unless this
5486 // is a type that requires multi-step expansion.
5487 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5488 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005489 if (Hi.Val)
5490 // Don't legalize the high part if it is expanded to a single node.
5491 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005492 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005493
5494 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005495 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005496 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005497}
5498
Dan Gohman7f321562007-06-25 16:23:39 +00005499/// SplitVectorOp - Given an operand of vector type, break it down into
5500/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005501void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5502 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005503 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005504 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005505 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005506 assert(NumElements > 1 && "Cannot split a single element vector!");
5507 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005508 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5509 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005510
5511 // See if we already split it.
5512 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5513 = SplitNodes.find(Op);
5514 if (I != SplitNodes.end()) {
5515 Lo = I->second.first;
5516 Hi = I->second.second;
5517 return;
5518 }
5519
5520 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005521 default:
5522#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005523 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005524#endif
5525 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005526 case ISD::BUILD_PAIR:
5527 Lo = Node->getOperand(0);
5528 Hi = Node->getOperand(1);
5529 break;
5530 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005531 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5532 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005533 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005534
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005535 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005536 Node->op_end());
5537 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005538 break;
5539 }
Dan Gohman7f321562007-06-25 16:23:39 +00005540 case ISD::CONCAT_VECTORS: {
5541 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5542 if (NewNumSubvectors == 1) {
5543 Lo = Node->getOperand(0);
5544 Hi = Node->getOperand(1);
5545 } else {
5546 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5547 Node->op_begin()+NewNumSubvectors);
5548 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005549
Dan Gohman7f321562007-06-25 16:23:39 +00005550 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5551 Node->op_end());
5552 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5553 }
Dan Gohman65956352007-06-13 15:12:02 +00005554 break;
5555 }
Dan Gohman7f321562007-06-25 16:23:39 +00005556 case ISD::ADD:
5557 case ISD::SUB:
5558 case ISD::MUL:
5559 case ISD::FADD:
5560 case ISD::FSUB:
5561 case ISD::FMUL:
5562 case ISD::SDIV:
5563 case ISD::UDIV:
5564 case ISD::FDIV:
5565 case ISD::AND:
5566 case ISD::OR:
5567 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005568 SDOperand LL, LH, RL, RH;
5569 SplitVectorOp(Node->getOperand(0), LL, LH);
5570 SplitVectorOp(Node->getOperand(1), RL, RH);
5571
Dan Gohman7f321562007-06-25 16:23:39 +00005572 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5573 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005574 break;
5575 }
Dan Gohman7f321562007-06-25 16:23:39 +00005576 case ISD::LOAD: {
5577 LoadSDNode *LD = cast<LoadSDNode>(Node);
5578 SDOperand Ch = LD->getChain();
5579 SDOperand Ptr = LD->getBasePtr();
5580 const Value *SV = LD->getSrcValue();
5581 int SVOffset = LD->getSrcValueOffset();
5582 unsigned Alignment = LD->getAlignment();
5583 bool isVolatile = LD->isVolatile();
5584
5585 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5586 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005587 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5588 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005589 SVOffset += IncrementSize;
5590 if (Alignment > IncrementSize)
5591 Alignment = IncrementSize;
5592 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005593
5594 // Build a factor node to remember that this load is independent of the
5595 // other one.
5596 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5597 Hi.getValue(1));
5598
5599 // Remember that we legalized the chain.
5600 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005601 break;
5602 }
Dan Gohman7f321562007-06-25 16:23:39 +00005603 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005604 // We know the result is a vector. The input may be either a vector or a
5605 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005606 SDOperand InOp = Node->getOperand(0);
5607 if (!MVT::isVector(InOp.getValueType()) ||
5608 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5609 // The input is a scalar or single-element vector.
5610 // Lower to a store/load so that it can be split.
5611 // FIXME: this could be improved probably.
5612 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005613
Evan Cheng786225a2006-10-05 23:01:46 +00005614 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005615 InOp, Ptr, NULL, 0);
5616 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005617 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005618 // Split the vector and convert each of the pieces now.
5619 SplitVectorOp(InOp, Lo, Hi);
5620 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5621 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5622 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005623 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005624 }
5625
5626 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005627 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005628 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005629 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005630}
5631
5632
Dan Gohman89b20c02007-06-27 14:06:22 +00005633/// ScalarizeVectorOp - Given an operand of single-element vector type
5634/// (e.g. v1f32), convert it into the equivalent operation that returns a
5635/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005636SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5637 assert(MVT::isVector(Op.getValueType()) &&
5638 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005639 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005640 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5641 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005642
Dan Gohman7f321562007-06-25 16:23:39 +00005643 // See if we already scalarized it.
5644 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5645 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005646
5647 SDOperand Result;
5648 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005649 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005650#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005651 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005652#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005653 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5654 case ISD::ADD:
5655 case ISD::FADD:
5656 case ISD::SUB:
5657 case ISD::FSUB:
5658 case ISD::MUL:
5659 case ISD::FMUL:
5660 case ISD::SDIV:
5661 case ISD::UDIV:
5662 case ISD::FDIV:
5663 case ISD::SREM:
5664 case ISD::UREM:
5665 case ISD::FREM:
5666 case ISD::AND:
5667 case ISD::OR:
5668 case ISD::XOR:
5669 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005670 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005671 ScalarizeVectorOp(Node->getOperand(0)),
5672 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005673 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005674 case ISD::FNEG:
5675 case ISD::FABS:
5676 case ISD::FSQRT:
5677 case ISD::FSIN:
5678 case ISD::FCOS:
5679 Result = DAG.getNode(Node->getOpcode(),
5680 NewVT,
5681 ScalarizeVectorOp(Node->getOperand(0)));
5682 break;
5683 case ISD::LOAD: {
5684 LoadSDNode *LD = cast<LoadSDNode>(Node);
5685 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5686 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005687
Dan Gohman7f321562007-06-25 16:23:39 +00005688 const Value *SV = LD->getSrcValue();
5689 int SVOffset = LD->getSrcValueOffset();
5690 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5691 LD->isVolatile(), LD->getAlignment());
5692
Chris Lattnerc7029802006-03-18 01:44:44 +00005693 // Remember that we legalized the chain.
5694 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5695 break;
5696 }
Dan Gohman7f321562007-06-25 16:23:39 +00005697 case ISD::BUILD_VECTOR:
5698 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005699 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005700 case ISD::INSERT_VECTOR_ELT:
5701 // Returning the inserted scalar element.
5702 Result = Node->getOperand(1);
5703 break;
5704 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005705 assert(Node->getOperand(0).getValueType() == NewVT &&
5706 "Concat of non-legal vectors not yet supported!");
5707 Result = Node->getOperand(0);
5708 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005709 case ISD::VECTOR_SHUFFLE: {
5710 // Figure out if the scalar is the LHS or RHS and return it.
5711 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5712 if (cast<ConstantSDNode>(EltNum)->getValue())
5713 Result = ScalarizeVectorOp(Node->getOperand(1));
5714 else
5715 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005716 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005717 }
5718 case ISD::EXTRACT_SUBVECTOR:
5719 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005720 assert(Result.getValueType() == NewVT);
5721 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005722 case ISD::BIT_CONVERT:
5723 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005724 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005725 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005726 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005727 ScalarizeVectorOp(Op.getOperand(1)),
5728 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005729 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005730 }
5731
5732 if (TLI.isTypeLegal(NewVT))
5733 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005734 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5735 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005736 return Result;
5737}
5738
Chris Lattner3e928bb2005-01-07 07:47:09 +00005739
5740// SelectionDAG::Legalize - This is the entry point for the file.
5741//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005742void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005743 if (ViewLegalizeDAGs) viewGraph();
5744
Chris Lattner3e928bb2005-01-07 07:47:09 +00005745 /// run - This is the main entry point to this class.
5746 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005747 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005748}
5749