blob: a5146bd45290b7d8af4534cee3b6f2509578e57e [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 Lattner3e928bb2005-01-07 07:47:09 +000042//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
58
Chris Lattner6831a812006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
64 SDOperand LastCALLSEQ_END;
65
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 };
Chris Lattner6831a812006-02-13 09:18:02 +000076
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000081
Chris Lattner3e928bb2005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000085 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000086
Chris Lattner03c85462005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000090 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000091
Chris Lattnerc7029802006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
93 /// which which operands are the expanded version of the input. This allows
94 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000095 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000096
Chris Lattnerc7029802006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
98 /// which which operands are the split version of the input. This allows us
99 /// to avoid splitting the same node more than once.
100 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
101
Dan Gohman7f321562007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000105 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000106
Chris Lattner8afc48e2005-01-07 22:28:47 +0000107 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000108 LegalizedNodes.insert(std::make_pair(From, To));
109 // If someone requests legalization of the new node, return itself.
110 if (From != To)
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000112 }
Chris Lattner03c85462005-01-15 05:21:40 +0000113 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000114 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000115 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000116 // If someone requests legalization of the new node, return itself.
117 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000118 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000119
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120public:
121
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000122 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124 /// getTypeAction - Return how we should legalize values of this type, either
125 /// it is already legal or we need to expand it into multiple registers of
126 /// smaller integer type, or we need to promote it to a larger type.
127 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000128 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129 }
130
131 /// isTypeLegal - Return true if this type is legal on this target.
132 ///
133 bool isTypeLegal(MVT::ValueType VT) const {
134 return getTypeAction(VT) == Legal;
135 }
136
Chris Lattner3e928bb2005-01-07 07:47:09 +0000137 void LegalizeDAG();
138
Chris Lattner456a93a2006-01-28 07:39:30 +0000139private:
Dan Gohman7f321562007-06-25 16:23:39 +0000140 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000141 /// appropriate for its type.
142 void HandleOp(SDOperand Op);
143
144 /// LegalizeOp - We know that the specified value has a legal type.
145 /// Recursively ensure that the operands have legal types, then return the
146 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000148
149 /// PromoteOp - Given an operation that produces a value in an invalid type,
150 /// promote it to compute the value into a larger type. The produced value
151 /// will have the correct bits for the low portion of the register, but no
152 /// guarantee is made about the top bits: it may be zero, sign-extended, or
153 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000154 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
Chris Lattnerc7029802006-03-18 01:44:44 +0000156 /// ExpandOp - Expand the specified SDOperand into its two component pieces
157 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
158 /// the LegalizeNodes map is filled in for any results that are not expanded,
159 /// the ExpandedNodes map is filled in for any results that are expanded, and
160 /// the Lo/Hi values are returned. This applies to integer types and Vector
161 /// types.
162 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
163
Dan Gohman7f321562007-06-25 16:23:39 +0000164 /// SplitVectorOp - Given an operand of vector type, break it down into
165 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000166 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
167
Dan Gohman89b20c02007-06-27 14:06:22 +0000168 /// ScalarizeVectorOp - Given an operand of single-element vector type
169 /// (e.g. v1f32), convert it into the equivalent operation that returns a
170 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000171 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000172
Chris Lattner4352cc92006-04-04 17:23:26 +0000173 /// isShuffleLegal - Return true if a vector shuffle is legal with the
174 /// specified mask and type. Targets can specify exactly which masks they
175 /// support and the code generator is tasked with not creating illegal masks.
176 ///
177 /// Note that this will also return true for shuffles that are promoted to a
178 /// different type.
179 ///
180 /// If this is a legal shuffle, this method returns the (possibly promoted)
181 /// build_vector Mask. If it's not a legal shuffle, it returns null.
182 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
183
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000184 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000185 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000186
Nate Begeman750ac1b2006-02-01 07:19:44 +0000187 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
188
Chris Lattnerce872152006-03-19 06:31:19 +0000189 SDOperand CreateStackTemporary(MVT::ValueType VT);
190
Reid Spencer47857812006-12-31 05:55:36 +0000191 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000192 SDOperand &Hi);
193 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
194 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000195
Chris Lattner35481892005-12-23 00:16:34 +0000196 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000197 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000198 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000199 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
200 SDOperand LegalOp,
201 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000202 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000204 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
205 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000206
Chris Lattner456a93a2006-01-28 07:39:30 +0000207 SDOperand ExpandBSWAP(SDOperand Op);
208 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000209 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000211 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
212 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000213
Dan Gohman7f321562007-06-25 16:23:39 +0000214 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000215 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000216
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217 SDOperand getIntPtrConstant(uint64_t Val) {
218 return DAG.getConstant(Val, TLI.getPointerTy());
219 }
220};
221}
222
Chris Lattner4352cc92006-04-04 17:23:26 +0000223/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
224/// specified mask and type. Targets can specify exactly which masks they
225/// support and the code generator is tasked with not creating illegal masks.
226///
227/// Note that this will also return true for shuffles that are promoted to a
228/// different type.
229SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
230 SDOperand Mask) const {
231 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
232 default: return 0;
233 case TargetLowering::Legal:
234 case TargetLowering::Custom:
235 break;
236 case TargetLowering::Promote: {
237 // If this is promoted to a different type, convert the shuffle mask and
238 // ask if it is legal in the promoted type!
239 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
240
241 // If we changed # elements, change the shuffle mask.
242 unsigned NumEltsGrowth =
243 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
244 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
245 if (NumEltsGrowth > 1) {
246 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000247 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000248 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
249 SDOperand InOp = Mask.getOperand(i);
250 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
251 if (InOp.getOpcode() == ISD::UNDEF)
252 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
253 else {
254 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
255 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
256 }
257 }
258 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000259 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000260 }
261 VT = NVT;
262 break;
263 }
264 }
265 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
266}
267
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000268SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
269 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
270 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000271 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000272 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273}
274
Chris Lattnere10e6f72007-06-18 21:28:10 +0000275/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
276/// contains all of a nodes operands before it contains the node.
277static void ComputeTopDownOrdering(SelectionDAG &DAG,
278 SmallVector<SDNode*, 64> &Order) {
279
280 DenseMap<SDNode*, unsigned> Visited;
281 std::vector<SDNode*> Worklist;
282 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000283
Chris Lattnere10e6f72007-06-18 21:28:10 +0000284 // Compute ordering from all of the leaves in the graphs, those (like the
285 // entry node) that have no operands.
286 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
287 E = DAG.allnodes_end(); I != E; ++I) {
288 if (I->getNumOperands() == 0) {
289 Visited[I] = 0 - 1U;
290 Worklist.push_back(I);
291 }
Chris Lattner32fca002005-10-06 01:20:27 +0000292 }
293
Chris Lattnere10e6f72007-06-18 21:28:10 +0000294 while (!Worklist.empty()) {
295 SDNode *N = Worklist.back();
296 Worklist.pop_back();
297
298 if (++Visited[N] != N->getNumOperands())
299 continue; // Haven't visited all operands yet
300
301 Order.push_back(N);
302
303 // Now that we have N in, add anything that uses it if all of their operands
304 // are now done.
305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
306 UI != E; ++UI)
307 Worklist.push_back(*UI);
308 }
309
310 assert(Order.size() == Visited.size() &&
311 Order.size() ==
312 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
313 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000314}
315
Chris Lattner1618beb2005-07-29 00:11:56 +0000316
Chris Lattner3e928bb2005-01-07 07:47:09 +0000317void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000318 LastCALLSEQ_END = DAG.getEntryNode();
319 IsLegalizingCall = false;
320
Chris Lattnerab510a72005-10-02 17:49:46 +0000321 // The legalize process is inherently a bottom-up recursive process (users
322 // legalize their uses before themselves). Given infinite stack space, we
323 // could just start legalizing on the root and traverse the whole graph. In
324 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000325 // blocks. To avoid this problem, compute an ordering of the nodes where each
326 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000327 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000328 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000329
Chris Lattnerc7029802006-03-18 01:44:44 +0000330 for (unsigned i = 0, e = Order.size(); i != e; ++i)
331 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000332
333 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000334 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000335 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
336 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000337
338 ExpandedNodes.clear();
339 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000340 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000341 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000342 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343
344 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000345 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346}
347
Chris Lattner6831a812006-02-13 09:18:02 +0000348
349/// FindCallEndFromCallStart - Given a chained node that is part of a call
350/// sequence, find the CALLSEQ_END node that terminates the call sequence.
351static SDNode *FindCallEndFromCallStart(SDNode *Node) {
352 if (Node->getOpcode() == ISD::CALLSEQ_END)
353 return Node;
354 if (Node->use_empty())
355 return 0; // No CallSeqEnd
356
357 // The chain is usually at the end.
358 SDOperand TheChain(Node, Node->getNumValues()-1);
359 if (TheChain.getValueType() != MVT::Other) {
360 // Sometimes it's at the beginning.
361 TheChain = SDOperand(Node, 0);
362 if (TheChain.getValueType() != MVT::Other) {
363 // Otherwise, hunt for it.
364 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
365 if (Node->getValueType(i) == MVT::Other) {
366 TheChain = SDOperand(Node, i);
367 break;
368 }
369
370 // Otherwise, we walked into a node without a chain.
371 if (TheChain.getValueType() != MVT::Other)
372 return 0;
373 }
374 }
375
376 for (SDNode::use_iterator UI = Node->use_begin(),
377 E = Node->use_end(); UI != E; ++UI) {
378
379 // Make sure to only follow users of our token chain.
380 SDNode *User = *UI;
381 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
382 if (User->getOperand(i) == TheChain)
383 if (SDNode *Result = FindCallEndFromCallStart(User))
384 return Result;
385 }
386 return 0;
387}
388
389/// FindCallStartFromCallEnd - Given a chained node that is part of a call
390/// sequence, find the CALLSEQ_START node that initiates the call sequence.
391static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
392 assert(Node && "Didn't find callseq_start for a call??");
393 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
394
395 assert(Node->getOperand(0).getValueType() == MVT::Other &&
396 "Node doesn't have a token chain argument!");
397 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
398}
399
400/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
401/// see if any uses can reach Dest. If no dest operands can get to dest,
402/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000403///
404/// Keep track of the nodes we fine that actually do lead to Dest in
405/// NodesLeadingTo. This avoids retraversing them exponential number of times.
406///
407bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000408 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000409 if (N == Dest) return true; // N certainly leads to Dest :)
410
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000411 // If we've already processed this node and it does lead to Dest, there is no
412 // need to reprocess it.
413 if (NodesLeadingTo.count(N)) return true;
414
Chris Lattner6831a812006-02-13 09:18:02 +0000415 // If the first result of this node has been already legalized, then it cannot
416 // reach N.
417 switch (getTypeAction(N->getValueType(0))) {
418 case Legal:
419 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Promote:
422 if (PromotedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 case Expand:
425 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 }
428
429 // Okay, this node has not already been legalized. Check and legalize all
430 // operands. If none lead to Dest, then we can legalize this node.
431 bool OperandsLeadToDest = false;
432 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
433 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000434 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000435
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000436 if (OperandsLeadToDest) {
437 NodesLeadingTo.insert(N);
438 return true;
439 }
Chris Lattner6831a812006-02-13 09:18:02 +0000440
441 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000442 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000443 return false;
444}
445
Dan Gohman7f321562007-06-25 16:23:39 +0000446/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000447/// appropriate for its type.
448void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000449 MVT::ValueType VT = Op.getValueType();
450 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000451 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000452 case Legal: (void)LegalizeOp(Op); break;
453 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000454 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000455 if (!MVT::isVector(VT)) {
456 // If this is an illegal scalar, expand it into its two component
457 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 SDOperand X, Y;
459 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000460 } else if (MVT::getVectorNumElements(VT) == 1) {
461 // If this is an illegal single element vector, convert it to a
462 // scalar operation.
463 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000464 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000465 // Otherwise, this is an illegal multiple element vector.
466 // Split it in half and legalize both parts.
467 SDOperand X, Y;
468 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000469 }
470 break;
471 }
472}
Chris Lattner6831a812006-02-13 09:18:02 +0000473
Evan Cheng9f877882006-12-13 20:57:08 +0000474/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
475/// a load from the constant pool.
476static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000477 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000478 bool Extend = false;
479
480 // If a FP immediate is precise when represented as a float and if the
481 // target can do an extending load from float to double, we put it into
482 // the constant pool as a float, even if it's is statically typed as a
483 // double.
484 MVT::ValueType VT = CFP->getValueType(0);
485 bool isDouble = VT == MVT::f64;
486 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
487 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000488 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000489 double Val = LLVMC->getValue();
490 return isDouble
491 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
492 : DAG.getConstant(FloatToBits(Val), MVT::i32);
493 }
494
Evan Cheng00495212006-12-12 21:32:44 +0000495 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
496 // Only do this if the target has a native EXTLOAD instruction from f32.
497 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
498 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
499 VT = MVT::f32;
500 Extend = true;
501 }
502
503 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
504 if (Extend) {
505 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
506 CPIdx, NULL, 0, MVT::f32);
507 } else {
508 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
509 }
510}
511
Chris Lattner6831a812006-02-13 09:18:02 +0000512
Evan Cheng912095b2007-01-04 21:56:39 +0000513/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
514/// operations.
515static
516SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
517 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000518 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000519 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000520 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
521 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000522 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000523
Evan Cheng912095b2007-01-04 21:56:39 +0000524 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000525 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000526 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
527 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000529 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000530 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000531 // Shift right or sign-extend it if the two operands have different types.
532 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
533 if (SizeDiff > 0) {
534 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
535 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
536 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
537 } else if (SizeDiff < 0)
538 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000539
540 // Clear the sign bit of first operand.
541 SDOperand Mask2 = (VT == MVT::f64)
542 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
543 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
544 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000545 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000546 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
547
548 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000549 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
550 return Result;
551}
552
553
Dan Gohmana3466152007-07-13 20:14:11 +0000554/// LegalizeOp - We know that the specified value has a legal type, and
555/// that its operands are legal. Now ensure that the operation itself
556/// is legal, recursively ensuring that the operands' operations remain
557/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000558SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000559 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000560 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000561 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000562
Chris Lattner3e928bb2005-01-07 07:47:09 +0000563 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000564 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000565 if (Node->getNumValues() > 1) {
566 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000567 if (getTypeAction(Node->getValueType(i)) != Legal) {
568 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000570 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000571 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000572 }
573 }
574
Chris Lattner45982da2005-05-12 16:53:42 +0000575 // Note that LegalizeOp may be reentered even from single-use nodes, which
576 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000577 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000578 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579
Nate Begeman9373a812005-08-10 20:51:12 +0000580 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000581 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000582 bool isCustom = false;
583
Chris Lattner3e928bb2005-01-07 07:47:09 +0000584 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000585 case ISD::FrameIndex:
586 case ISD::EntryToken:
587 case ISD::Register:
588 case ISD::BasicBlock:
589 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000590 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000591 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000592 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000593 case ISD::TargetConstantPool:
594 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000595 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000596 case ISD::TargetExternalSymbol:
597 case ISD::VALUETYPE:
598 case ISD::SRCVALUE:
599 case ISD::STRING:
600 case ISD::CONDCODE:
601 // Primitives must all be legal.
602 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
603 "This must be legal!");
604 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000605 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000606 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
607 // If this is a target node, legalize it by legalizing the operands then
608 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000609 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000611 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000612
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000613 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000614
615 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
616 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
617 return Result.getValue(Op.ResNo);
618 }
619 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000620#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000621 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000622#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 assert(0 && "Do not know how to legalize this operator!");
624 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000625 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000626 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000627 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000628 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000629 case ISD::ConstantPool:
630 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000631 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
632 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000633 case TargetLowering::Custom:
634 Tmp1 = TLI.LowerOperation(Op, DAG);
635 if (Tmp1.Val) Result = Tmp1;
636 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000637 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000638 break;
639 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000640 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000641 case ISD::FRAMEADDR:
642 case ISD::RETURNADDR:
643 // The only option for these nodes is to custom lower them. If the target
644 // does not custom lower them, then return zero.
645 Tmp1 = TLI.LowerOperation(Op, DAG);
646 if (Tmp1.Val)
647 Result = Tmp1;
648 else
649 Result = DAG.getConstant(0, TLI.getPointerTy());
650 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000651 case ISD::EXCEPTIONADDR: {
652 Tmp1 = LegalizeOp(Node->getOperand(0));
653 MVT::ValueType VT = Node->getValueType(0);
654 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
655 default: assert(0 && "This action is not supported yet!");
656 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000657 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000658 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
659 }
660 break;
661 case TargetLowering::Custom:
662 Result = TLI.LowerOperation(Op, DAG);
663 if (Result.Val) break;
664 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000665 case TargetLowering::Legal: {
666 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
667 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
668 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000669 break;
670 }
671 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000672 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000673 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000674 case ISD::EHSELECTION: {
675 Tmp1 = LegalizeOp(Node->getOperand(0));
676 Tmp2 = LegalizeOp(Node->getOperand(1));
677 MVT::ValueType VT = Node->getValueType(0);
678 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
679 default: assert(0 && "This action is not supported yet!");
680 case TargetLowering::Expand: {
681 unsigned Reg = TLI.getExceptionSelectorRegister();
682 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
683 }
684 break;
685 case TargetLowering::Custom:
686 Result = TLI.LowerOperation(Op, DAG);
687 if (Result.Val) break;
688 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000689 case TargetLowering::Legal: {
690 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
691 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
692 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000693 break;
694 }
695 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000696 }
Jim Laskey8782d482007-02-28 20:43:58 +0000697 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000698 case ISD::AssertSext:
699 case ISD::AssertZext:
700 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000702 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000703 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000704 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000705 Result = Node->getOperand(Op.ResNo);
706 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000707 case ISD::CopyFromReg:
708 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000709 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000710 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000712 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000713 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000714 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000715 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000716 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
717 } else {
718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
719 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000720 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
721 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000722 // Since CopyFromReg produces two values, make sure to remember that we
723 // legalized both of them.
724 AddLegalizedOperand(Op.getValue(0), Result);
725 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
726 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000727 case ISD::UNDEF: {
728 MVT::ValueType VT = Op.getValueType();
729 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000730 default: assert(0 && "This action is not supported yet!");
731 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000732 if (MVT::isInteger(VT))
733 Result = DAG.getConstant(0, VT);
734 else if (MVT::isFloatingPoint(VT))
735 Result = DAG.getConstantFP(0, VT);
736 else
737 assert(0 && "Unknown value type!");
738 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000739 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000740 break;
741 }
742 break;
743 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000744
Chris Lattner48b61a72006-03-28 00:40:33 +0000745 case ISD::INTRINSIC_W_CHAIN:
746 case ISD::INTRINSIC_WO_CHAIN:
747 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000748 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000749 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
750 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000751 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000752
753 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000754 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000755 TargetLowering::Custom) {
756 Tmp3 = TLI.LowerOperation(Result, DAG);
757 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000758 }
759
760 if (Result.Val->getNumValues() == 1) break;
761
762 // Must have return value and chain result.
763 assert(Result.Val->getNumValues() == 2 &&
764 "Cannot return more than two values!");
765
766 // Since loads produce two values, make sure to remember that we
767 // legalized both of them.
768 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
769 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
770 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000771 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000772
773 case ISD::LOCATION:
774 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
776
777 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
778 case TargetLowering::Promote:
779 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000780 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000781 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000782 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000783 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000784
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000785 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000786 const std::string &FName =
787 cast<StringSDNode>(Node->getOperand(3))->getValue();
788 const std::string &DirName =
789 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000790 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000791
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000792 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000793 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000794 SDOperand LineOp = Node->getOperand(1);
795 SDOperand ColOp = Node->getOperand(2);
796
797 if (useDEBUG_LOC) {
798 Ops.push_back(LineOp); // line #
799 Ops.push_back(ColOp); // col #
800 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000801 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000802 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000803 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
804 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000805 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000806 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000807 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000808 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000809 } else {
810 Result = Tmp1; // chain
811 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000812 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000813 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000814 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000815 if (Tmp1 != Node->getOperand(0) ||
816 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000817 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000818 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000819 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
820 Ops.push_back(Node->getOperand(1)); // line # must be legal.
821 Ops.push_back(Node->getOperand(2)); // col # must be legal.
822 } else {
823 // Otherwise promote them.
824 Ops.push_back(PromoteOp(Node->getOperand(1)));
825 Ops.push_back(PromoteOp(Node->getOperand(2)));
826 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000827 Ops.push_back(Node->getOperand(3)); // filename must be legal.
828 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000829 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000830 }
831 break;
832 }
833 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000834
835 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000836 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000837 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000838 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000839 case TargetLowering::Legal:
840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
841 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
842 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
843 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000844 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000845 break;
846 }
847 break;
848
Jim Laskey1ee29252007-01-26 14:34:52 +0000849 case ISD::LABEL:
850 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
851 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000852 default: assert(0 && "This action is not supported yet!");
853 case TargetLowering::Legal:
854 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
855 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000856 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000857 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000858 case TargetLowering::Expand:
859 Result = LegalizeOp(Node->getOperand(0));
860 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000861 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000862 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000863
Chris Lattner3e928bb2005-01-07 07:47:09 +0000864 case ISD::Constant:
865 // We know we don't need to expand constants here, constants only have one
866 // value and we check that it is fine above.
867
868 // FIXME: Maybe we should handle things like targets that don't support full
869 // 32-bit immediates?
870 break;
871 case ISD::ConstantFP: {
872 // Spill FP immediates to the constant pool if the target cannot directly
873 // codegen them. Targets often have some immediate values that can be
874 // efficiently generated into an FP register without a load. We explicitly
875 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000876 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
877
878 // Check to see if this FP immediate is already legal.
879 bool isLegal = false;
880 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
881 E = TLI.legal_fpimm_end(); I != E; ++I)
882 if (CFP->isExactlyValue(*I)) {
883 isLegal = true;
884 break;
885 }
886
Chris Lattner3181a772006-01-29 06:26:56 +0000887 // If this is a legal constant, turn it into a TargetConstantFP node.
888 if (isLegal) {
889 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
890 break;
891 }
892
893 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
894 default: assert(0 && "This action is not supported yet!");
895 case TargetLowering::Custom:
896 Tmp3 = TLI.LowerOperation(Result, DAG);
897 if (Tmp3.Val) {
898 Result = Tmp3;
899 break;
900 }
901 // FALLTHROUGH
902 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000903 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000904 }
905 break;
906 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000907 case ISD::TokenFactor:
908 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000909 Tmp1 = LegalizeOp(Node->getOperand(0));
910 Tmp2 = LegalizeOp(Node->getOperand(1));
911 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
912 } else if (Node->getNumOperands() == 3) {
913 Tmp1 = LegalizeOp(Node->getOperand(0));
914 Tmp2 = LegalizeOp(Node->getOperand(1));
915 Tmp3 = LegalizeOp(Node->getOperand(2));
916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000917 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000918 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000919 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000920 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
921 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000922 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000923 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000924 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000925
926 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000927 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000928 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000929 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
930 assert(Tmp3.Val && "Target didn't custom lower this node!");
931 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
932 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000933
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000934 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000935 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000936 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
937 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000938 if (Op.ResNo == i)
939 Tmp2 = Tmp1;
940 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
941 }
942 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000943
Chris Lattnerb2827b02006-03-19 00:52:58 +0000944 case ISD::BUILD_VECTOR:
945 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000946 default: assert(0 && "This action is not supported yet!");
947 case TargetLowering::Custom:
948 Tmp3 = TLI.LowerOperation(Result, DAG);
949 if (Tmp3.Val) {
950 Result = Tmp3;
951 break;
952 }
953 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000954 case TargetLowering::Expand:
955 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000956 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000957 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000958 break;
959 case ISD::INSERT_VECTOR_ELT:
960 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
961 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
962 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
964
965 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
966 Node->getValueType(0))) {
967 default: assert(0 && "This action is not supported yet!");
968 case TargetLowering::Legal:
969 break;
970 case TargetLowering::Custom:
971 Tmp3 = TLI.LowerOperation(Result, DAG);
972 if (Tmp3.Val) {
973 Result = Tmp3;
974 break;
975 }
976 // FALLTHROUGH
977 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000978 // If the insert index is a constant, codegen this as a scalar_to_vector,
979 // then a shuffle that inserts it into the right position in the vector.
980 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
981 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
982 Tmp1.getValueType(), Tmp2);
983
984 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
985 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +0000986 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +0000987
988 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
989 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
990 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000991 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000992 for (unsigned i = 0; i != NumElts; ++i) {
993 if (i != InsertPos->getValue())
994 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
995 else
996 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
997 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000998 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
999 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001000
1001 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1002 Tmp1, ScVec, ShufMask);
1003 Result = LegalizeOp(Result);
1004 break;
1005 }
1006
Chris Lattner2332b9f2006-03-19 01:17:20 +00001007 // If the target doesn't support this, we have to spill the input vector
1008 // to a temporary stack slot, update the element, then reload it. This is
1009 // badness. We could also load the value into a vector register (either
1010 // with a "move to register" or "extload into register" instruction, then
1011 // permute it into place, if the idx is a constant and if the idx is
1012 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001013 MVT::ValueType VT = Tmp1.getValueType();
1014 MVT::ValueType EltVT = Tmp2.getValueType();
1015 MVT::ValueType IdxVT = Tmp3.getValueType();
1016 MVT::ValueType PtrVT = TLI.getPointerTy();
1017 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001018 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001019 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001020
1021 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001022 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1023 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001024 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001025 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1026 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1027 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001028 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001029 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001030 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001031 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001032 break;
1033 }
1034 }
1035 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001036 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001037 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1038 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1039 break;
1040 }
1041
Chris Lattnerce872152006-03-19 06:31:19 +00001042 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1043 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1044 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1045 Node->getValueType(0))) {
1046 default: assert(0 && "This action is not supported yet!");
1047 case TargetLowering::Legal:
1048 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001049 case TargetLowering::Custom:
1050 Tmp3 = TLI.LowerOperation(Result, DAG);
1051 if (Tmp3.Val) {
1052 Result = Tmp3;
1053 break;
1054 }
1055 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001056 case TargetLowering::Expand:
1057 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001058 break;
1059 }
Chris Lattnerce872152006-03-19 06:31:19 +00001060 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001061 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1063 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1064 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1065
1066 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001067 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1068 default: assert(0 && "Unknown operation action!");
1069 case TargetLowering::Legal:
1070 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1071 "vector shuffle should not be created if not legal!");
1072 break;
1073 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001074 Tmp3 = TLI.LowerOperation(Result, DAG);
1075 if (Tmp3.Val) {
1076 Result = Tmp3;
1077 break;
1078 }
1079 // FALLTHROUGH
1080 case TargetLowering::Expand: {
1081 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001082 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001083 MVT::ValueType PtrVT = TLI.getPointerTy();
1084 SDOperand Mask = Node->getOperand(2);
1085 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001086 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001087 for (unsigned i = 0; i != NumElems; ++i) {
1088 SDOperand Arg = Mask.getOperand(i);
1089 if (Arg.getOpcode() == ISD::UNDEF) {
1090 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1091 } else {
1092 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1093 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1094 if (Idx < NumElems)
1095 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1096 DAG.getConstant(Idx, PtrVT)));
1097 else
1098 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1099 DAG.getConstant(Idx - NumElems, PtrVT)));
1100 }
1101 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001102 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001103 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001104 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001105 case TargetLowering::Promote: {
1106 // Change base type to a different vector type.
1107 MVT::ValueType OVT = Node->getValueType(0);
1108 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1109
1110 // Cast the two input vectors.
1111 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1112 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1113
1114 // Convert the shuffle mask to the right # elements.
1115 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1116 assert(Tmp3.Val && "Shuffle not legal?");
1117 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1118 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1119 break;
1120 }
Chris Lattner87100e02006-03-20 01:52:29 +00001121 }
1122 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001123
1124 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001125 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001126 Tmp2 = LegalizeOp(Node->getOperand(1));
1127 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001128 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001129 break;
1130
Dan Gohman7f321562007-06-25 16:23:39 +00001131 case ISD::EXTRACT_SUBVECTOR:
1132 Tmp1 = Node->getOperand(0);
1133 Tmp2 = LegalizeOp(Node->getOperand(1));
1134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1135 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001136 break;
1137
Chris Lattner6831a812006-02-13 09:18:02 +00001138 case ISD::CALLSEQ_START: {
1139 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1140
1141 // Recursively Legalize all of the inputs of the call end that do not lead
1142 // to this call start. This ensures that any libcalls that need be inserted
1143 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001144 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001145 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001146 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1147 NodesLeadingTo);
1148 }
Chris Lattner6831a812006-02-13 09:18:02 +00001149
1150 // Now that we legalized all of the inputs (which may have inserted
1151 // libcalls) create the new CALLSEQ_START node.
1152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1153
1154 // Merge in the last call, to ensure that this call start after the last
1155 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001156 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001157 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1158 Tmp1 = LegalizeOp(Tmp1);
1159 }
Chris Lattner6831a812006-02-13 09:18:02 +00001160
1161 // Do not try to legalize the target-specific arguments (#1+).
1162 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001163 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001164 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001165 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001166 }
1167
1168 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001169 AddLegalizedOperand(Op.getValue(0), Result);
1170 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1171 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1172
Chris Lattner6831a812006-02-13 09:18:02 +00001173 // Now that the callseq_start and all of the non-call nodes above this call
1174 // sequence have been legalized, legalize the call itself. During this
1175 // process, no libcalls can/will be inserted, guaranteeing that no calls
1176 // can overlap.
1177 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1178 SDOperand InCallSEQ = LastCALLSEQ_END;
1179 // Note that we are selecting this call!
1180 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1181 IsLegalizingCall = true;
1182
1183 // Legalize the call, starting from the CALLSEQ_END.
1184 LegalizeOp(LastCALLSEQ_END);
1185 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1186 return Result;
1187 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001188 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001189 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1190 // will cause this node to be legalized as well as handling libcalls right.
1191 if (LastCALLSEQ_END.Val != Node) {
1192 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001193 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001194 assert(I != LegalizedNodes.end() &&
1195 "Legalizing the call start should have legalized this node!");
1196 return I->second;
1197 }
1198
1199 // Otherwise, the call start has been legalized and everything is going
1200 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001201 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001202 // Do not try to legalize the target-specific arguments (#1+), except for
1203 // an optional flag input.
1204 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1205 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001206 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001207 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001208 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001209 }
1210 } else {
1211 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1212 if (Tmp1 != Node->getOperand(0) ||
1213 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001214 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001215 Ops[0] = Tmp1;
1216 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001217 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001218 }
Chris Lattner6a542892006-01-24 05:48:21 +00001219 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001220 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001221 // This finishes up call legalization.
1222 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001223
1224 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1225 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1226 if (Node->getNumValues() == 2)
1227 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1228 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001229 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001230 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1231 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1232 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001233 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001234
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001236 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001237 switch (TLI.getOperationAction(Node->getOpcode(),
1238 Node->getValueType(0))) {
1239 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001240 case TargetLowering::Expand: {
1241 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1242 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1243 " not tell us which reg is the stack pointer!");
1244 SDOperand Chain = Tmp1.getOperand(0);
1245 SDOperand Size = Tmp2.getOperand(1);
1246 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1247 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1248 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001249 Tmp1 = LegalizeOp(Tmp1);
1250 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001251 break;
1252 }
1253 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001254 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1255 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001256 Tmp1 = LegalizeOp(Tmp3);
1257 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001258 }
Chris Lattner903d2782006-01-15 08:54:32 +00001259 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001260 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001261 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001262 }
Chris Lattner903d2782006-01-15 08:54:32 +00001263 // Since this op produce two values, make sure to remember that we
1264 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001265 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1266 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001267 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001268 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001269 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001270 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001271 bool Changed = false;
1272 // Legalize all of the operands of the inline asm, in case they are nodes
1273 // that need to be expanded or something. Note we skip the asm string and
1274 // all of the TargetConstant flags.
1275 SDOperand Op = LegalizeOp(Ops[0]);
1276 Changed = Op != Ops[0];
1277 Ops[0] = Op;
1278
1279 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1280 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1281 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1282 for (++i; NumVals; ++i, --NumVals) {
1283 SDOperand Op = LegalizeOp(Ops[i]);
1284 if (Op != Ops[i]) {
1285 Changed = true;
1286 Ops[i] = Op;
1287 }
1288 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001289 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001290
1291 if (HasInFlag) {
1292 Op = LegalizeOp(Ops.back());
1293 Changed |= Op != Ops.back();
1294 Ops.back() = Op;
1295 }
1296
1297 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001298 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001299
1300 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001301 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001302 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1303 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001304 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001305 case ISD::BR:
1306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001307 // Ensure that libcalls are emitted before a branch.
1308 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1309 Tmp1 = LegalizeOp(Tmp1);
1310 LastCALLSEQ_END = DAG.getEntryNode();
1311
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001313 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001314 case ISD::BRIND:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1316 // Ensure that libcalls are emitted before a branch.
1317 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1318 Tmp1 = LegalizeOp(Tmp1);
1319 LastCALLSEQ_END = DAG.getEntryNode();
1320
1321 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1322 default: assert(0 && "Indirect target must be legal type (pointer)!");
1323 case Legal:
1324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1325 break;
1326 }
1327 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1328 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001329 case ISD::BR_JT:
1330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1331 // Ensure that libcalls are emitted before a branch.
1332 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1333 Tmp1 = LegalizeOp(Tmp1);
1334 LastCALLSEQ_END = DAG.getEntryNode();
1335
1336 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1338
1339 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1340 default: assert(0 && "This action is not supported yet!");
1341 case TargetLowering::Legal: break;
1342 case TargetLowering::Custom:
1343 Tmp1 = TLI.LowerOperation(Result, DAG);
1344 if (Tmp1.Val) Result = Tmp1;
1345 break;
1346 case TargetLowering::Expand: {
1347 SDOperand Chain = Result.getOperand(0);
1348 SDOperand Table = Result.getOperand(1);
1349 SDOperand Index = Result.getOperand(2);
1350
1351 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001352 MachineFunction &MF = DAG.getMachineFunction();
1353 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001354 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1355 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001356
1357 SDOperand LD;
1358 switch (EntrySize) {
1359 default: assert(0 && "Size of jump table not supported yet."); break;
1360 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1361 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1362 }
1363
1364 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001365 // For PIC, the sequence is:
1366 // BRIND(load(Jumptable + index) + RelocBase)
1367 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1368 SDOperand Reloc;
1369 if (TLI.usesGlobalOffsetTable())
1370 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1371 else
1372 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001373 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001374 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1375 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1376 } else {
1377 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1378 }
1379 }
1380 }
1381 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001382 case ISD::BRCOND:
1383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001384 // Ensure that libcalls are emitted before a return.
1385 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1386 Tmp1 = LegalizeOp(Tmp1);
1387 LastCALLSEQ_END = DAG.getEntryNode();
1388
Chris Lattner47e92232005-01-18 19:27:06 +00001389 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1390 case Expand: assert(0 && "It's impossible to expand bools");
1391 case Legal:
1392 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1393 break;
1394 case Promote:
1395 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001396
1397 // The top bits of the promoted condition are not necessarily zero, ensure
1398 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001399 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001400 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1401 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001402 break;
1403 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001404
1405 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001407
1408 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1409 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001410 case TargetLowering::Legal: break;
1411 case TargetLowering::Custom:
1412 Tmp1 = TLI.LowerOperation(Result, DAG);
1413 if (Tmp1.Val) Result = Tmp1;
1414 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001415 case TargetLowering::Expand:
1416 // Expand brcond's setcc into its constituent parts and create a BR_CC
1417 // Node.
1418 if (Tmp2.getOpcode() == ISD::SETCC) {
1419 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1420 Tmp2.getOperand(0), Tmp2.getOperand(1),
1421 Node->getOperand(2));
1422 } else {
1423 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1424 DAG.getCondCode(ISD::SETNE), Tmp2,
1425 DAG.getConstant(0, Tmp2.getValueType()),
1426 Node->getOperand(2));
1427 }
1428 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001429 }
1430 break;
1431 case ISD::BR_CC:
1432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001433 // Ensure that libcalls are emitted before a branch.
1434 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1435 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001436 Tmp2 = Node->getOperand(2); // LHS
1437 Tmp3 = Node->getOperand(3); // RHS
1438 Tmp4 = Node->getOperand(1); // CC
1439
1440 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001441 LastCALLSEQ_END = DAG.getEntryNode();
1442
Nate Begeman750ac1b2006-02-01 07:19:44 +00001443 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1444 // the LHS is a legal SETCC itself. In this case, we need to compare
1445 // the result against zero to select between true and false values.
1446 if (Tmp3.Val == 0) {
1447 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1448 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001449 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001450
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1452 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001453
Chris Lattner181b7a32005-12-17 23:46:46 +00001454 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1455 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001456 case TargetLowering::Legal: break;
1457 case TargetLowering::Custom:
1458 Tmp4 = TLI.LowerOperation(Result, DAG);
1459 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001460 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001461 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001462 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001463 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001464 LoadSDNode *LD = cast<LoadSDNode>(Node);
1465 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1466 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001467
Evan Cheng466685d2006-10-09 20:57:25 +00001468 ISD::LoadExtType ExtType = LD->getExtensionType();
1469 if (ExtType == ISD::NON_EXTLOAD) {
1470 MVT::ValueType VT = Node->getValueType(0);
1471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1472 Tmp3 = Result.getValue(0);
1473 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001474
Evan Cheng466685d2006-10-09 20:57:25 +00001475 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1476 default: assert(0 && "This action is not supported yet!");
1477 case TargetLowering::Legal: break;
1478 case TargetLowering::Custom:
1479 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1480 if (Tmp1.Val) {
1481 Tmp3 = LegalizeOp(Tmp1);
1482 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001483 }
Evan Cheng466685d2006-10-09 20:57:25 +00001484 break;
1485 case TargetLowering::Promote: {
1486 // Only promote a load of vector type to another.
1487 assert(MVT::isVector(VT) && "Cannot promote this load!");
1488 // Change base type to a different vector type.
1489 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1490
1491 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001492 LD->getSrcValueOffset(),
1493 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001494 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1495 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001496 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001497 }
Evan Cheng466685d2006-10-09 20:57:25 +00001498 }
1499 // Since loads produce two values, make sure to remember that we
1500 // legalized both of them.
1501 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1502 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1503 return Op.ResNo ? Tmp4 : Tmp3;
1504 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001505 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001506 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1507 default: assert(0 && "This action is not supported yet!");
1508 case TargetLowering::Promote:
1509 assert(SrcVT == MVT::i1 &&
1510 "Can only promote extending LOAD from i1 -> i8!");
1511 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1512 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001513 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001514 Tmp1 = Result.getValue(0);
1515 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001516 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001517 case TargetLowering::Custom:
1518 isCustom = true;
1519 // FALLTHROUGH
1520 case TargetLowering::Legal:
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1522 Tmp1 = Result.getValue(0);
1523 Tmp2 = Result.getValue(1);
1524
1525 if (isCustom) {
1526 Tmp3 = TLI.LowerOperation(Result, DAG);
1527 if (Tmp3.Val) {
1528 Tmp1 = LegalizeOp(Tmp3);
1529 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1530 }
1531 }
1532 break;
1533 case TargetLowering::Expand:
1534 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1535 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1536 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001537 LD->getSrcValueOffset(),
1538 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001539 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1540 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1541 Tmp2 = LegalizeOp(Load.getValue(1));
1542 break;
1543 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001544 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001545 // Turn the unsupported load into an EXTLOAD followed by an explicit
1546 // zero/sign extend inreg.
1547 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1548 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001549 LD->getSrcValueOffset(), SrcVT,
1550 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001551 SDOperand ValRes;
1552 if (ExtType == ISD::SEXTLOAD)
1553 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1554 Result, DAG.getValueType(SrcVT));
1555 else
1556 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1557 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1558 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1559 break;
1560 }
1561 // Since loads produce two values, make sure to remember that we legalized
1562 // both of them.
1563 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1564 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1565 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001566 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001567 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001568 case ISD::EXTRACT_ELEMENT: {
1569 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1570 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001571 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001572 case Legal:
1573 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1574 // 1 -> Hi
1575 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1576 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1577 TLI.getShiftAmountTy()));
1578 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1579 } else {
1580 // 0 -> Lo
1581 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1582 Node->getOperand(0));
1583 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001584 break;
1585 case Expand:
1586 // Get both the low and high parts.
1587 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1588 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1589 Result = Tmp2; // 1 -> Hi
1590 else
1591 Result = Tmp1; // 0 -> Lo
1592 break;
1593 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001594 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001595 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001596
1597 case ISD::CopyToReg:
1598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001599
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001600 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001601 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001602 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001603 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001604 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001606 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001607 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001608 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001609 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1611 Tmp3);
1612 } else {
1613 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001614 }
1615
1616 // Since this produces two values, make sure to remember that we legalized
1617 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001618 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001619 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001620 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001621 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001622 break;
1623
1624 case ISD::RET:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001626
1627 // Ensure that libcalls are emitted before a return.
1628 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1629 Tmp1 = LegalizeOp(Tmp1);
1630 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001631
Chris Lattner3e928bb2005-01-07 07:47:09 +00001632 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001633 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001634 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001635 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001636 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001637 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001638 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001639 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001640 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001641 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001642 SDOperand Lo, Hi;
1643 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001644
1645 // Big endian systems want the hi reg first.
1646 if (!TLI.isLittleEndian())
1647 std::swap(Lo, Hi);
1648
Evan Cheng13acce32006-12-11 19:27:14 +00001649 if (Hi.Val)
1650 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1651 else
1652 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001653 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001654 } else {
1655 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001656 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1657 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001658
Dan Gohman7f321562007-06-25 16:23:39 +00001659 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001660 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001661 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001662 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001663 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001664 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001666 } else if (NumElems == 1) {
1667 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001668 Tmp2 = ScalarizeVectorOp(Tmp2);
1669 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001671
1672 // FIXME: Returns of gcc generic vectors smaller than a legal type
1673 // should be returned in integer registers!
1674
Chris Lattnerf87324e2006-04-11 01:31:51 +00001675 // The scalarized value type may not be legal, e.g. it might require
1676 // promotion or expansion. Relegalize the return.
1677 Result = LegalizeOp(Result);
1678 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001679 // FIXME: Returns of gcc generic vectors larger than a legal vector
1680 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001681 SDOperand Lo, Hi;
1682 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001683 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001684 Result = LegalizeOp(Result);
1685 }
1686 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001687 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001688 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001689 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001690 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001691 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001692 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001693 }
1694 break;
1695 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001696 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001697 break;
1698 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001699 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001700 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001701 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001702 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1703 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001704 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001705 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001706 break;
1707 case Expand: {
1708 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001709 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001710 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001711 ExpandOp(Node->getOperand(i), Lo, Hi);
1712 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001713 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001714 if (Hi.Val) {
1715 NewValues.push_back(Hi);
1716 NewValues.push_back(Node->getOperand(i+1));
1717 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001718 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001719 }
1720 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001721 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001722 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001723
1724 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001725 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001726 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001727 Result = DAG.getNode(ISD::RET, MVT::Other,
1728 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001729 break;
1730 }
1731 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001732
Chris Lattner6862dbc2006-01-29 21:02:23 +00001733 if (Result.getOpcode() == ISD::RET) {
1734 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1735 default: assert(0 && "This action is not supported yet!");
1736 case TargetLowering::Legal: break;
1737 case TargetLowering::Custom:
1738 Tmp1 = TLI.LowerOperation(Result, DAG);
1739 if (Tmp1.Val) Result = Tmp1;
1740 break;
1741 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001742 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001743 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001744 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001745 StoreSDNode *ST = cast<StoreSDNode>(Node);
1746 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1747 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001748 int SVOffset = ST->getSrcValueOffset();
1749 unsigned Alignment = ST->getAlignment();
1750 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001751
Evan Cheng8b2794a2006-10-13 21:14:26 +00001752 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001753 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1754 // FIXME: We shouldn't do this for TargetConstantFP's.
1755 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1756 // to phase ordering between legalized code and the dag combiner. This
1757 // probably means that we need to integrate dag combiner and legalizer
1758 // together.
1759 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1760 if (CFP->getValueType(0) == MVT::f32) {
1761 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1762 } else {
1763 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1764 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1765 }
1766 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001767 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001768 break;
1769 }
1770
Evan Cheng8b2794a2006-10-13 21:14:26 +00001771 switch (getTypeAction(ST->getStoredVT())) {
1772 case Legal: {
1773 Tmp3 = LegalizeOp(ST->getValue());
1774 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1775 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001776
Evan Cheng8b2794a2006-10-13 21:14:26 +00001777 MVT::ValueType VT = Tmp3.getValueType();
1778 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1779 default: assert(0 && "This action is not supported yet!");
1780 case TargetLowering::Legal: break;
1781 case TargetLowering::Custom:
1782 Tmp1 = TLI.LowerOperation(Result, DAG);
1783 if (Tmp1.Val) Result = Tmp1;
1784 break;
1785 case TargetLowering::Promote:
1786 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1787 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1788 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1789 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001790 ST->getSrcValue(), SVOffset, isVolatile,
1791 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001792 break;
1793 }
1794 break;
1795 }
1796 case Promote:
1797 // Truncate the value and store the result.
1798 Tmp3 = PromoteOp(ST->getValue());
1799 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001800 SVOffset, ST->getStoredVT(),
1801 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001802 break;
1803
1804 case Expand:
1805 unsigned IncrementSize = 0;
1806 SDOperand Lo, Hi;
1807
1808 // If this is a vector type, then we have to calculate the increment as
1809 // the product of the element size in bytes, and the number of elements
1810 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001811 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001812 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001813 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1814 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001815
Dan Gohman7f321562007-06-25 16:23:39 +00001816 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001817 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001818 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001819 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001820 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001821 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001822 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001823 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001824 Result = LegalizeOp(Result);
1825 break;
1826 } else if (NumElems == 1) {
1827 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001828 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001829 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001830 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001831 // The scalarized value type may not be legal, e.g. it might require
1832 // promotion or expansion. Relegalize the scalar store.
1833 Result = LegalizeOp(Result);
1834 break;
1835 } else {
1836 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1837 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1838 }
1839 } else {
1840 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001841 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001842
1843 if (!TLI.isLittleEndian())
1844 std::swap(Lo, Hi);
1845 }
1846
1847 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001848 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001849
1850 if (Hi.Val == NULL) {
1851 // Must be int <-> float one-to-one expansion.
1852 Result = Lo;
1853 break;
1854 }
1855
Evan Cheng8b2794a2006-10-13 21:14:26 +00001856 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1857 getIntPtrConstant(IncrementSize));
1858 assert(isTypeLegal(Tmp2.getValueType()) &&
1859 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001860 SVOffset += IncrementSize;
1861 if (Alignment > IncrementSize)
1862 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001863 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001864 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001865 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1866 break;
1867 }
1868 } else {
1869 // Truncating store
1870 assert(isTypeLegal(ST->getValue().getValueType()) &&
1871 "Cannot handle illegal TRUNCSTORE yet!");
1872 Tmp3 = LegalizeOp(ST->getValue());
1873
1874 // The only promote case we handle is TRUNCSTORE:i1 X into
1875 // -> TRUNCSTORE:i8 (and X, 1)
1876 if (ST->getStoredVT() == MVT::i1 &&
1877 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1878 // Promote the bool to a mask then store.
1879 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1880 DAG.getConstant(1, Tmp3.getValueType()));
1881 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001882 SVOffset, MVT::i8,
1883 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001884 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1885 Tmp2 != ST->getBasePtr()) {
1886 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1887 ST->getOffset());
1888 }
1889
1890 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1891 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001892 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001893 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001894 case TargetLowering::Custom:
1895 Tmp1 = TLI.LowerOperation(Result, DAG);
1896 if (Tmp1.Val) Result = Tmp1;
1897 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001898 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001899 }
1900 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001901 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001902 case ISD::PCMARKER:
1903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001905 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001906 case ISD::STACKSAVE:
1907 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001908 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1909 Tmp1 = Result.getValue(0);
1910 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001911
Chris Lattner140d53c2006-01-13 02:50:02 +00001912 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1913 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001916 Tmp3 = TLI.LowerOperation(Result, DAG);
1917 if (Tmp3.Val) {
1918 Tmp1 = LegalizeOp(Tmp3);
1919 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001920 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001921 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001922 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001923 // Expand to CopyFromReg if the target set
1924 // StackPointerRegisterToSaveRestore.
1925 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001926 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001927 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001928 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001929 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001930 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1931 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001932 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001933 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001934 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001935
1936 // Since stacksave produce two values, make sure to remember that we
1937 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001938 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1939 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1940 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001941
Chris Lattner140d53c2006-01-13 02:50:02 +00001942 case ISD::STACKRESTORE:
1943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1944 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001946
1947 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1948 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001949 case TargetLowering::Legal: break;
1950 case TargetLowering::Custom:
1951 Tmp1 = TLI.LowerOperation(Result, DAG);
1952 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001953 break;
1954 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001955 // Expand to CopyToReg if the target set
1956 // StackPointerRegisterToSaveRestore.
1957 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1958 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1959 } else {
1960 Result = Tmp1;
1961 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001962 break;
1963 }
1964 break;
1965
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001966 case ISD::READCYCLECOUNTER:
1967 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001968 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001969 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1970 Node->getValueType(0))) {
1971 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001972 case TargetLowering::Legal:
1973 Tmp1 = Result.getValue(0);
1974 Tmp2 = Result.getValue(1);
1975 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001976 case TargetLowering::Custom:
1977 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001978 Tmp1 = LegalizeOp(Result.getValue(0));
1979 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001980 break;
1981 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001982
1983 // Since rdcc produce two values, make sure to remember that we legalized
1984 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001985 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1986 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001987 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001988
Chris Lattner2ee743f2005-01-14 22:08:15 +00001989 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001990 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1991 case Expand: assert(0 && "It's impossible to expand bools");
1992 case Legal:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1994 break;
1995 case Promote:
1996 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001997 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00001998 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00001999 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2000 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002001 break;
2002 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002003 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002004 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002005
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002007
Nate Begemanb942a3d2005-08-23 04:29:48 +00002008 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002009 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002010 case TargetLowering::Legal: break;
2011 case TargetLowering::Custom: {
2012 Tmp1 = TLI.LowerOperation(Result, DAG);
2013 if (Tmp1.Val) Result = Tmp1;
2014 break;
2015 }
Nate Begeman9373a812005-08-10 20:51:12 +00002016 case TargetLowering::Expand:
2017 if (Tmp1.getOpcode() == ISD::SETCC) {
2018 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2019 Tmp2, Tmp3,
2020 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2021 } else {
2022 Result = DAG.getSelectCC(Tmp1,
2023 DAG.getConstant(0, Tmp1.getValueType()),
2024 Tmp2, Tmp3, ISD::SETNE);
2025 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002026 break;
2027 case TargetLowering::Promote: {
2028 MVT::ValueType NVT =
2029 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2030 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002031 if (MVT::isVector(Tmp2.getValueType())) {
2032 ExtOp = ISD::BIT_CONVERT;
2033 TruncOp = ISD::BIT_CONVERT;
2034 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002035 ExtOp = ISD::ANY_EXTEND;
2036 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002037 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002038 ExtOp = ISD::FP_EXTEND;
2039 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002040 }
2041 // Promote each of the values to the new type.
2042 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2043 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2044 // Perform the larger operation, then round down.
2045 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2046 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2047 break;
2048 }
2049 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002050 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002051 case ISD::SELECT_CC: {
2052 Tmp1 = Node->getOperand(0); // LHS
2053 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002054 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2055 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002056 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002057
Nate Begeman750ac1b2006-02-01 07:19:44 +00002058 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2059
2060 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2061 // the LHS is a legal SETCC itself. In this case, we need to compare
2062 // the result against zero to select between true and false values.
2063 if (Tmp2.Val == 0) {
2064 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2065 CC = DAG.getCondCode(ISD::SETNE);
2066 }
2067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2068
2069 // Everything is legal, see if we should expand this op or something.
2070 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2071 default: assert(0 && "This action is not supported yet!");
2072 case TargetLowering::Legal: break;
2073 case TargetLowering::Custom:
2074 Tmp1 = TLI.LowerOperation(Result, DAG);
2075 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002076 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002077 }
2078 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002079 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002080 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002081 Tmp1 = Node->getOperand(0);
2082 Tmp2 = Node->getOperand(1);
2083 Tmp3 = Node->getOperand(2);
2084 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2085
2086 // If we had to Expand the SetCC operands into a SELECT node, then it may
2087 // not always be possible to return a true LHS & RHS. In this case, just
2088 // return the value we legalized, returned in the LHS
2089 if (Tmp2.Val == 0) {
2090 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002091 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002092 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002093
Chris Lattner73e142f2006-01-30 22:43:50 +00002094 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002095 default: assert(0 && "Cannot handle this action for SETCC yet!");
2096 case TargetLowering::Custom:
2097 isCustom = true;
2098 // FALLTHROUGH.
2099 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002101 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002102 Tmp4 = TLI.LowerOperation(Result, DAG);
2103 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002104 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002105 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002106 case TargetLowering::Promote: {
2107 // First step, figure out the appropriate operation to use.
2108 // Allow SETCC to not be supported for all legal data types
2109 // Mostly this targets FP
2110 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002111 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002112
2113 // Scan for the appropriate larger type to use.
2114 while (1) {
2115 NewInTy = (MVT::ValueType)(NewInTy+1);
2116
2117 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2118 "Fell off of the edge of the integer world");
2119 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2120 "Fell off of the edge of the floating point world");
2121
2122 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002123 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002124 break;
2125 }
2126 if (MVT::isInteger(NewInTy))
2127 assert(0 && "Cannot promote Legal Integer SETCC yet");
2128 else {
2129 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2130 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2131 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002132 Tmp1 = LegalizeOp(Tmp1);
2133 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002135 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002136 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002137 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002138 case TargetLowering::Expand:
2139 // Expand a setcc node into a select_cc of the same condition, lhs, and
2140 // rhs that selects between const 1 (true) and const 0 (false).
2141 MVT::ValueType VT = Node->getValueType(0);
2142 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2143 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002144 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002145 break;
2146 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002147 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002148 case ISD::MEMSET:
2149 case ISD::MEMCPY:
2150 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2153
2154 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2155 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2156 case Expand: assert(0 && "Cannot expand a byte!");
2157 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002158 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002159 break;
2160 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002161 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002162 break;
2163 }
2164 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002165 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002166 }
Chris Lattner272455b2005-02-02 03:44:41 +00002167
2168 SDOperand Tmp4;
2169 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002170 case Expand: {
2171 // Length is too big, just take the lo-part of the length.
2172 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002173 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002174 break;
2175 }
Chris Lattnere5605212005-01-28 22:29:18 +00002176 case Legal:
2177 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002178 break;
2179 case Promote:
2180 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002181 break;
2182 }
2183
2184 SDOperand Tmp5;
2185 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2186 case Expand: assert(0 && "Cannot expand this yet!");
2187 case Legal:
2188 Tmp5 = LegalizeOp(Node->getOperand(4));
2189 break;
2190 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002191 Tmp5 = PromoteOp(Node->getOperand(4));
2192 break;
2193 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002194
2195 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2196 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002197 case TargetLowering::Custom:
2198 isCustom = true;
2199 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002200 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002202 if (isCustom) {
2203 Tmp1 = TLI.LowerOperation(Result, DAG);
2204 if (Tmp1.Val) Result = Tmp1;
2205 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002206 break;
2207 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002208 // Otherwise, the target does not support this operation. Lower the
2209 // operation to an explicit libcall as appropriate.
2210 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002211 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002212 TargetLowering::ArgListTy Args;
2213 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002214
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002215 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002216 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002217 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002218 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002219 // Extend the (previously legalized) ubyte argument to be an int value
2220 // for the call.
2221 if (Tmp3.getValueType() > MVT::i32)
2222 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2223 else
2224 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002225 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002226 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002227 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002228 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002229
2230 FnName = "memset";
2231 } else if (Node->getOpcode() == ISD::MEMCPY ||
2232 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002233 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002234 Entry.Node = Tmp2; Args.push_back(Entry);
2235 Entry.Node = Tmp3; Args.push_back(Entry);
2236 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002237 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2238 } else {
2239 assert(0 && "Unknown op!");
2240 }
Chris Lattner45982da2005-05-12 16:53:42 +00002241
Chris Lattnere1bd8222005-01-11 05:57:22 +00002242 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002243 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002244 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002245 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002246 break;
2247 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002248 }
2249 break;
2250 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002251
Chris Lattner5b359c62005-04-02 04:00:59 +00002252 case ISD::SHL_PARTS:
2253 case ISD::SRA_PARTS:
2254 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002255 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002256 bool Changed = false;
2257 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2258 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2259 Changed |= Ops.back() != Node->getOperand(i);
2260 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002261 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002262 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002263
Evan Cheng05a2d562006-01-09 18:31:59 +00002264 switch (TLI.getOperationAction(Node->getOpcode(),
2265 Node->getValueType(0))) {
2266 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002267 case TargetLowering::Legal: break;
2268 case TargetLowering::Custom:
2269 Tmp1 = TLI.LowerOperation(Result, DAG);
2270 if (Tmp1.Val) {
2271 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002272 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002273 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002274 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2275 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002276 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002277 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002278 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002279 return RetVal;
2280 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002281 break;
2282 }
2283
Chris Lattner2c8086f2005-04-02 05:00:07 +00002284 // Since these produce multiple values, make sure to remember that we
2285 // legalized all of them.
2286 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2287 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2288 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002289 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002290
2291 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002292 case ISD::ADD:
2293 case ISD::SUB:
2294 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002295 case ISD::MULHS:
2296 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002297 case ISD::UDIV:
2298 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002299 case ISD::AND:
2300 case ISD::OR:
2301 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002302 case ISD::SHL:
2303 case ISD::SRL:
2304 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002305 case ISD::FADD:
2306 case ISD::FSUB:
2307 case ISD::FMUL:
2308 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002309 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002310 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2311 case Expand: assert(0 && "Not possible");
2312 case Legal:
2313 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2314 break;
2315 case Promote:
2316 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2317 break;
2318 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002319
2320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002321
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002322 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002323 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002324 case TargetLowering::Legal: break;
2325 case TargetLowering::Custom:
2326 Tmp1 = TLI.LowerOperation(Result, DAG);
2327 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002328 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002329 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002330 if (Node->getValueType(0) == MVT::i32) {
2331 switch (Node->getOpcode()) {
2332 default: assert(0 && "Do not know how to expand this integer BinOp!");
2333 case ISD::UDIV:
2334 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002335 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2336 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002337 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002338 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002339 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002340 };
2341 break;
2342 }
2343
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002344 assert(MVT::isVector(Node->getValueType(0)) &&
2345 "Cannot expand this binary operator!");
2346 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002347 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002348 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002349 MVT::ValueType PtrVT = TLI.getPointerTy();
2350 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2351 i != e; ++i) {
2352 SDOperand Idx = DAG.getConstant(i, PtrVT);
2353 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2354 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2355 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2356 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002357 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2358 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002359 break;
2360 }
Evan Chengcc987612006-04-12 21:20:24 +00002361 case TargetLowering::Promote: {
2362 switch (Node->getOpcode()) {
2363 default: assert(0 && "Do not know how to promote this BinOp!");
2364 case ISD::AND:
2365 case ISD::OR:
2366 case ISD::XOR: {
2367 MVT::ValueType OVT = Node->getValueType(0);
2368 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2369 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2370 // Bit convert each of the values to the new type.
2371 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2372 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2373 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2374 // Bit convert the result back the original type.
2375 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2376 break;
2377 }
2378 }
2379 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002380 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002381 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002382
2383 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2384 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2385 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2386 case Expand: assert(0 && "Not possible");
2387 case Legal:
2388 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2389 break;
2390 case Promote:
2391 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2392 break;
2393 }
2394
2395 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2396
2397 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2398 default: assert(0 && "Operation not supported");
2399 case TargetLowering::Custom:
2400 Tmp1 = TLI.LowerOperation(Result, DAG);
2401 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002402 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002403 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002404 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002405 // If this target supports fabs/fneg natively and select is cheap,
2406 // do this efficiently.
2407 if (!TLI.isSelectExpensive() &&
2408 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2409 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002410 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002411 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002412 // Get the sign bit of the RHS.
2413 MVT::ValueType IVT =
2414 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2415 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2416 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2417 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2418 // Get the absolute value of the result.
2419 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2420 // Select between the nabs and abs value based on the sign bit of
2421 // the input.
2422 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2423 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2424 AbsVal),
2425 AbsVal);
2426 Result = LegalizeOp(Result);
2427 break;
2428 }
2429
2430 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002431 MVT::ValueType NVT =
2432 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2433 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2434 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2435 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002436 break;
2437 }
Evan Cheng912095b2007-01-04 21:56:39 +00002438 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002439 break;
2440
Nate Begeman551bf3f2006-02-17 05:43:56 +00002441 case ISD::ADDC:
2442 case ISD::SUBC:
2443 Tmp1 = LegalizeOp(Node->getOperand(0));
2444 Tmp2 = LegalizeOp(Node->getOperand(1));
2445 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2446 // Since this produces two values, make sure to remember that we legalized
2447 // both of them.
2448 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2449 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2450 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002451
Nate Begeman551bf3f2006-02-17 05:43:56 +00002452 case ISD::ADDE:
2453 case ISD::SUBE:
2454 Tmp1 = LegalizeOp(Node->getOperand(0));
2455 Tmp2 = LegalizeOp(Node->getOperand(1));
2456 Tmp3 = LegalizeOp(Node->getOperand(2));
2457 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2458 // Since this produces two values, make sure to remember that we legalized
2459 // both of them.
2460 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2461 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2462 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002463
Nate Begeman419f8b62005-10-18 00:27:41 +00002464 case ISD::BUILD_PAIR: {
2465 MVT::ValueType PairTy = Node->getValueType(0);
2466 // TODO: handle the case where the Lo and Hi operands are not of legal type
2467 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2468 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2469 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002470 case TargetLowering::Promote:
2471 case TargetLowering::Custom:
2472 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002473 case TargetLowering::Legal:
2474 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2475 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2476 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002477 case TargetLowering::Expand:
2478 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2479 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2480 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2481 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2482 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002483 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002484 break;
2485 }
2486 break;
2487 }
2488
Nate Begemanc105e192005-04-06 00:23:54 +00002489 case ISD::UREM:
2490 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002491 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002492 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2493 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002494
Nate Begemanc105e192005-04-06 00:23:54 +00002495 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002496 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2497 case TargetLowering::Custom:
2498 isCustom = true;
2499 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002500 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002501 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002502 if (isCustom) {
2503 Tmp1 = TLI.LowerOperation(Result, DAG);
2504 if (Tmp1.Val) Result = Tmp1;
2505 }
Nate Begemanc105e192005-04-06 00:23:54 +00002506 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002507 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002508 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002509 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002510 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002511 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2512 TargetLowering::Legal) {
2513 // X % Y -> X-X/Y*Y
2514 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002515 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002516 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2517 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2518 } else {
2519 assert(Node->getValueType(0) == MVT::i32 &&
2520 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002521 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2522 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002523 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002524 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002525 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002526 } else {
2527 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002528 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2529 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002530 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002531 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2532 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002533 }
2534 break;
2535 }
2536 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002537 case ISD::VAARG: {
2538 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2539 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2540
Chris Lattner5c62f332006-01-28 07:42:08 +00002541 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002542 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2543 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002544 case TargetLowering::Custom:
2545 isCustom = true;
2546 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002547 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002548 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2549 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002550 Tmp1 = Result.getValue(1);
2551
2552 if (isCustom) {
2553 Tmp2 = TLI.LowerOperation(Result, DAG);
2554 if (Tmp2.Val) {
2555 Result = LegalizeOp(Tmp2);
2556 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2557 }
2558 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002559 break;
2560 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002561 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002562 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002563 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002564 // Increment the pointer, VAList, to the next vaarg
2565 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2566 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2567 TLI.getPointerTy()));
2568 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002569 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2570 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002571 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002572 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002573 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002574 Result = LegalizeOp(Result);
2575 break;
2576 }
2577 }
2578 // Since VAARG produces two values, make sure to remember that we
2579 // legalized both of them.
2580 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002581 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2582 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002583 }
2584
2585 case ISD::VACOPY:
2586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2587 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2588 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2589
2590 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2591 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002592 case TargetLowering::Custom:
2593 isCustom = true;
2594 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002595 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2597 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002598 if (isCustom) {
2599 Tmp1 = TLI.LowerOperation(Result, DAG);
2600 if (Tmp1.Val) Result = Tmp1;
2601 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002602 break;
2603 case TargetLowering::Expand:
2604 // This defaults to loading a pointer from the input and storing it to the
2605 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002606 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002607 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002608 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2609 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002610 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2611 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002612 break;
2613 }
2614 break;
2615
2616 case ISD::VAEND:
2617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2618 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2619
2620 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2621 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002622 case TargetLowering::Custom:
2623 isCustom = true;
2624 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002625 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002626 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002627 if (isCustom) {
2628 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2629 if (Tmp1.Val) Result = Tmp1;
2630 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002631 break;
2632 case TargetLowering::Expand:
2633 Result = Tmp1; // Default to a no-op, return the chain
2634 break;
2635 }
2636 break;
2637
2638 case ISD::VASTART:
2639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2640 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2641
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2643
Nate Begemanacc398c2006-01-25 18:21:52 +00002644 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2645 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002646 case TargetLowering::Legal: break;
2647 case TargetLowering::Custom:
2648 Tmp1 = TLI.LowerOperation(Result, DAG);
2649 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002650 break;
2651 }
2652 break;
2653
Nate Begeman35ef9132006-01-11 21:21:00 +00002654 case ISD::ROTL:
2655 case ISD::ROTR:
2656 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2657 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002659 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2660 default:
2661 assert(0 && "ROTL/ROTR legalize operation not supported");
2662 break;
2663 case TargetLowering::Legal:
2664 break;
2665 case TargetLowering::Custom:
2666 Tmp1 = TLI.LowerOperation(Result, DAG);
2667 if (Tmp1.Val) Result = Tmp1;
2668 break;
2669 case TargetLowering::Promote:
2670 assert(0 && "Do not know how to promote ROTL/ROTR");
2671 break;
2672 case TargetLowering::Expand:
2673 assert(0 && "Do not know how to expand ROTL/ROTR");
2674 break;
2675 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002676 break;
2677
Nate Begemand88fc032006-01-14 03:14:10 +00002678 case ISD::BSWAP:
2679 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2680 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002681 case TargetLowering::Custom:
2682 assert(0 && "Cannot custom legalize this yet!");
2683 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002684 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002685 break;
2686 case TargetLowering::Promote: {
2687 MVT::ValueType OVT = Tmp1.getValueType();
2688 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002689 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002690
Chris Lattner456a93a2006-01-28 07:39:30 +00002691 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2692 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2693 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2694 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2695 break;
2696 }
2697 case TargetLowering::Expand:
2698 Result = ExpandBSWAP(Tmp1);
2699 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002700 }
2701 break;
2702
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002703 case ISD::CTPOP:
2704 case ISD::CTTZ:
2705 case ISD::CTLZ:
2706 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2707 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002708 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002709 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002710 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002711 break;
2712 case TargetLowering::Promote: {
2713 MVT::ValueType OVT = Tmp1.getValueType();
2714 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002715
2716 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002717 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2718 // Perform the larger operation, then subtract if needed.
2719 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002721 case ISD::CTPOP:
2722 Result = Tmp1;
2723 break;
2724 case ISD::CTTZ:
2725 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002726 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002727 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002728 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002729 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002730 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002731 break;
2732 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002733 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002734 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002735 DAG.getConstant(MVT::getSizeInBits(NVT) -
2736 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002737 break;
2738 }
2739 break;
2740 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002741 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002742 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002743 break;
2744 }
2745 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002746
Chris Lattner2c8086f2005-04-02 05:00:07 +00002747 // Unary operators
2748 case ISD::FABS:
2749 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002750 case ISD::FSQRT:
2751 case ISD::FSIN:
2752 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002753 Tmp1 = LegalizeOp(Node->getOperand(0));
2754 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002755 case TargetLowering::Promote:
2756 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002757 isCustom = true;
2758 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002759 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002760 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002761 if (isCustom) {
2762 Tmp1 = TLI.LowerOperation(Result, DAG);
2763 if (Tmp1.Val) Result = Tmp1;
2764 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002765 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002766 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002767 switch (Node->getOpcode()) {
2768 default: assert(0 && "Unreachable!");
2769 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002770 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2771 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002772 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002773 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002774 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002775 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2776 MVT::ValueType VT = Node->getValueType(0);
2777 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002778 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002779 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2780 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002781 break;
2782 }
2783 case ISD::FSQRT:
2784 case ISD::FSIN:
2785 case ISD::FCOS: {
2786 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002787 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002788 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002789 case ISD::FSQRT:
2790 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2791 break;
2792 case ISD::FSIN:
2793 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2794 break;
2795 case ISD::FCOS:
2796 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2797 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002798 default: assert(0 && "Unreachable!");
2799 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002800 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002801 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2802 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002803 break;
2804 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002805 }
2806 break;
2807 }
2808 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002809 case ISD::FPOWI: {
2810 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002811 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2812 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002813 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002814 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2815 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002816 break;
2817 }
Chris Lattner35481892005-12-23 00:16:34 +00002818 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002819 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002820 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00002821 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2822 // The input has to be a vector type, we have to either scalarize it, pack
2823 // it, or convert it based on whether the input vector type is legal.
2824 SDNode *InVal = Node->getOperand(0).Val;
2825 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2826 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
2827
2828 // Figure out if there is a simple type corresponding to this Vector
2829 // type. If so, convert to the vector type.
2830 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2831 if (TLI.isTypeLegal(TVT)) {
2832 // Turn this into a bit convert of the packed input.
2833 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2834 LegalizeOp(Node->getOperand(0)));
2835 break;
2836 } else if (NumElems == 1) {
2837 // Turn this into a bit convert of the scalar input.
2838 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2839 ScalarizeVectorOp(Node->getOperand(0)));
2840 break;
2841 } else {
2842 // FIXME: UNIMP! Store then reload
2843 assert(0 && "Cast from unsupported vector type not implemented yet!");
2844 }
Chris Lattner67993f72006-01-23 07:30:46 +00002845 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002846 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2847 Node->getOperand(0).getValueType())) {
2848 default: assert(0 && "Unknown operation action!");
2849 case TargetLowering::Expand:
2850 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2851 break;
2852 case TargetLowering::Legal:
2853 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002854 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002855 break;
2856 }
2857 }
2858 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002859
Chris Lattner2c8086f2005-04-02 05:00:07 +00002860 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002861 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002862 case ISD::UINT_TO_FP: {
2863 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002864 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2865 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002866 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002867 Node->getOperand(0).getValueType())) {
2868 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002869 case TargetLowering::Custom:
2870 isCustom = true;
2871 // FALLTHROUGH
2872 case TargetLowering::Legal:
2873 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002875 if (isCustom) {
2876 Tmp1 = TLI.LowerOperation(Result, DAG);
2877 if (Tmp1.Val) Result = Tmp1;
2878 }
2879 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002880 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002881 Result = ExpandLegalINT_TO_FP(isSigned,
2882 LegalizeOp(Node->getOperand(0)),
2883 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002884 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002885 case TargetLowering::Promote:
2886 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2887 Node->getValueType(0),
2888 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002889 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002890 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002891 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002892 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002893 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2894 Node->getValueType(0), Node->getOperand(0));
2895 break;
2896 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002897 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002898 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002899 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2900 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002901 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002902 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2903 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002904 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002905 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2906 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002907 break;
2908 }
2909 break;
2910 }
2911 case ISD::TRUNCATE:
2912 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2913 case Legal:
2914 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002915 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002916 break;
2917 case Expand:
2918 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2919
2920 // Since the result is legal, we should just be able to truncate the low
2921 // part of the source.
2922 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2923 break;
2924 case Promote:
2925 Result = PromoteOp(Node->getOperand(0));
2926 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2927 break;
2928 }
2929 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002930
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002931 case ISD::FP_TO_SINT:
2932 case ISD::FP_TO_UINT:
2933 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2934 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002935 Tmp1 = LegalizeOp(Node->getOperand(0));
2936
Chris Lattner1618beb2005-07-29 00:11:56 +00002937 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2938 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002939 case TargetLowering::Custom:
2940 isCustom = true;
2941 // FALLTHROUGH
2942 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002943 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002944 if (isCustom) {
2945 Tmp1 = TLI.LowerOperation(Result, DAG);
2946 if (Tmp1.Val) Result = Tmp1;
2947 }
2948 break;
2949 case TargetLowering::Promote:
2950 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2951 Node->getOpcode() == ISD::FP_TO_SINT);
2952 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002953 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002954 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2955 SDOperand True, False;
2956 MVT::ValueType VT = Node->getOperand(0).getValueType();
2957 MVT::ValueType NVT = Node->getValueType(0);
2958 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2959 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2960 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2961 Node->getOperand(0), Tmp2, ISD::SETLT);
2962 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2963 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002964 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002965 Tmp2));
2966 False = DAG.getNode(ISD::XOR, NVT, False,
2967 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002968 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2969 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002970 } else {
2971 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2972 }
2973 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002974 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002975 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002976 case Expand: {
2977 // Convert f32 / f64 to i32 / i64.
2978 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00002979 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00002980 switch (Node->getOpcode()) {
2981 case ISD::FP_TO_SINT:
2982 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002983 LC = (VT == MVT::i32)
2984 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002985 else
Evan Cheng56966222007-01-12 02:11:51 +00002986 LC = (VT == MVT::i32)
2987 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002988 break;
2989 case ISD::FP_TO_UINT:
2990 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002991 LC = (VT == MVT::i32)
2992 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002993 else
Evan Cheng56966222007-01-12 02:11:51 +00002994 LC = (VT == MVT::i32)
2995 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002996 break;
2997 default: assert(0 && "Unreachable!");
2998 }
2999 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003000 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3001 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003002 break;
3003 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003004 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003005 Tmp1 = PromoteOp(Node->getOperand(0));
3006 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3007 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003008 break;
3009 }
3010 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003011
Dale Johannesen849f2142007-07-03 00:53:03 +00003012 case ISD::FP_ROUND:
3013 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3014 TargetLowering::Expand) {
3015 // The only way we can lower this is to turn it into a TRUNCSTORE,
3016 // EXTLOAD pair, targetting a temporary location (a stack slot).
3017
3018 // NOTE: there is a choice here between constantly creating new stack
3019 // slots and always reusing the same one. We currently always create
3020 // new ones, as reuse may inhibit scheduling.
3021 MVT::ValueType VT = Op.getValueType(); // 32
3022 const Type *Ty = MVT::getTypeForValueType(VT);
3023 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3024 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3025 MachineFunction &MF = DAG.getMachineFunction();
3026 int SSFI =
3027 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3028 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3029 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3030 StackSlot, NULL, 0, VT);
3031 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3032 break;
3033 }
3034 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003035 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003036 case ISD::ZERO_EXTEND:
3037 case ISD::SIGN_EXTEND:
3038 case ISD::FP_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003039 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003040 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003041 case Legal:
3042 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003043 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003044 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003045 case Promote:
3046 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003047 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003048 Tmp1 = PromoteOp(Node->getOperand(0));
3049 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003050 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003051 case ISD::ZERO_EXTEND:
3052 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003053 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003054 Result = DAG.getZeroExtendInReg(Result,
3055 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003056 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003057 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003058 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003059 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003060 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003061 Result,
3062 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003063 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003064 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003065 Result = PromoteOp(Node->getOperand(0));
3066 if (Result.getValueType() != Op.getValueType())
3067 // Dynamically dead while we have only 2 FP types.
3068 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3069 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003070 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003071 Result = PromoteOp(Node->getOperand(0));
3072 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3073 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003074 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 }
3076 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003077 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003078 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003079 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003080 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003081
3082 // If this operation is not supported, convert it to a shl/shr or load/store
3083 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003084 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3085 default: assert(0 && "This action not supported for this op yet!");
3086 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003088 break;
3089 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003090 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003091 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003092 // NOTE: we could fall back on load/store here too for targets without
3093 // SAR. However, it is doubtful that any exist.
3094 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3095 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003096 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003097 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3098 Node->getOperand(0), ShiftCst);
3099 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3100 Result, ShiftCst);
3101 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003102 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003103 // EXTLOAD pair, targetting a temporary location (a stack slot).
3104
3105 // NOTE: there is a choice here between constantly creating new stack
3106 // slots and always reusing the same one. We currently always create
3107 // new ones, as reuse may inhibit scheduling.
3108 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003109 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003110 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003111 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003112 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003113 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003114 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003115 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3116 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003117 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003118 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003119 } else {
3120 assert(0 && "Unknown op");
3121 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003122 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003123 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003124 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003125 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003126 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003127
Chris Lattner4ddd2832006-04-08 04:13:17 +00003128 assert(Result.getValueType() == Op.getValueType() &&
3129 "Bad legalization!");
3130
Chris Lattner456a93a2006-01-28 07:39:30 +00003131 // Make sure that the generated code is itself legal.
3132 if (Result != Op)
3133 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003134
Chris Lattner45982da2005-05-12 16:53:42 +00003135 // Note that LegalizeOp may be reentered even from single-use nodes, which
3136 // means that we always must cache transformed nodes.
3137 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003138 return Result;
3139}
3140
Chris Lattner8b6fa222005-01-15 22:16:26 +00003141/// PromoteOp - Given an operation that produces a value in an invalid type,
3142/// promote it to compute the value into a larger type. The produced value will
3143/// have the correct bits for the low portion of the register, but no guarantee
3144/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003145SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3146 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003147 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003148 assert(getTypeAction(VT) == Promote &&
3149 "Caller should expand or legalize operands that are not promotable!");
3150 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3151 "Cannot promote to smaller type!");
3152
Chris Lattner03c85462005-01-15 05:21:40 +00003153 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003154 SDOperand Result;
3155 SDNode *Node = Op.Val;
3156
Chris Lattner40030bf2007-02-04 01:17:38 +00003157 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003158 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003159
Chris Lattner03c85462005-01-15 05:21:40 +00003160 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003161 case ISD::CopyFromReg:
3162 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003163 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003164#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003165 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003166#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003167 assert(0 && "Do not know how to promote this operator!");
3168 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003169 case ISD::UNDEF:
3170 Result = DAG.getNode(ISD::UNDEF, NVT);
3171 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003172 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003173 if (VT != MVT::i1)
3174 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3175 else
3176 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003177 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3178 break;
3179 case ISD::ConstantFP:
3180 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3181 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3182 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003183
Chris Lattner82fbfb62005-01-18 02:59:52 +00003184 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003185 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003186 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3187 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003188 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003189
Chris Lattner03c85462005-01-15 05:21:40 +00003190 case ISD::TRUNCATE:
3191 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3192 case Legal:
3193 Result = LegalizeOp(Node->getOperand(0));
3194 assert(Result.getValueType() >= NVT &&
3195 "This truncation doesn't make sense!");
3196 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3197 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3198 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003199 case Promote:
3200 // The truncation is not required, because we don't guarantee anything
3201 // about high bits anyway.
3202 Result = PromoteOp(Node->getOperand(0));
3203 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003204 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003205 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3206 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003207 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003208 }
3209 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003210 case ISD::SIGN_EXTEND:
3211 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003212 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003213 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3214 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3215 case Legal:
3216 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003217 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003218 break;
3219 case Promote:
3220 // Promote the reg if it's smaller.
3221 Result = PromoteOp(Node->getOperand(0));
3222 // The high bits are not guaranteed to be anything. Insert an extend.
3223 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003224 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003225 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003226 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003227 Result = DAG.getZeroExtendInReg(Result,
3228 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003229 break;
3230 }
3231 break;
Chris Lattner35481892005-12-23 00:16:34 +00003232 case ISD::BIT_CONVERT:
3233 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3234 Result = PromoteOp(Result);
3235 break;
3236
Chris Lattner8b6fa222005-01-15 22:16:26 +00003237 case ISD::FP_EXTEND:
3238 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3239 case ISD::FP_ROUND:
3240 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3241 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3242 case Promote: assert(0 && "Unreachable with 2 FP types!");
3243 case Legal:
3244 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003245 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003246 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003247 break;
3248 }
3249 break;
3250
3251 case ISD::SINT_TO_FP:
3252 case ISD::UINT_TO_FP:
3253 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3254 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003255 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003256 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003257 break;
3258
3259 case Promote:
3260 Result = PromoteOp(Node->getOperand(0));
3261 if (Node->getOpcode() == ISD::SINT_TO_FP)
3262 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003263 Result,
3264 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003265 else
Chris Lattner23993562005-04-13 02:38:47 +00003266 Result = DAG.getZeroExtendInReg(Result,
3267 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003268 // No extra round required here.
3269 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003270 break;
3271 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003272 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3273 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003274 // Round if we cannot tolerate excess precision.
3275 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003276 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3277 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003278 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003279 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003280 break;
3281
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003282 case ISD::SIGN_EXTEND_INREG:
3283 Result = PromoteOp(Node->getOperand(0));
3284 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3285 Node->getOperand(1));
3286 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003287 case ISD::FP_TO_SINT:
3288 case ISD::FP_TO_UINT:
3289 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3290 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003291 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003292 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003293 break;
3294 case Promote:
3295 // The input result is prerounded, so we don't have to do anything
3296 // special.
3297 Tmp1 = PromoteOp(Node->getOperand(0));
3298 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003299 }
Nate Begemand2558e32005-08-14 01:20:53 +00003300 // If we're promoting a UINT to a larger size, check to see if the new node
3301 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3302 // we can use that instead. This allows us to generate better code for
3303 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3304 // legal, such as PowerPC.
3305 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003306 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003307 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3308 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003309 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3310 } else {
3311 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3312 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003313 break;
3314
Chris Lattner2c8086f2005-04-02 05:00:07 +00003315 case ISD::FABS:
3316 case ISD::FNEG:
3317 Tmp1 = PromoteOp(Node->getOperand(0));
3318 assert(Tmp1.getValueType() == NVT);
3319 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3320 // NOTE: we do not have to do any extra rounding here for
3321 // NoExcessFPPrecision, because we know the input will have the appropriate
3322 // precision, and these operations don't modify precision at all.
3323 break;
3324
Chris Lattnerda6ba872005-04-28 21:44:33 +00003325 case ISD::FSQRT:
3326 case ISD::FSIN:
3327 case ISD::FCOS:
3328 Tmp1 = PromoteOp(Node->getOperand(0));
3329 assert(Tmp1.getValueType() == NVT);
3330 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003331 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003332 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3333 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003334 break;
3335
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003336 case ISD::FPOWI: {
3337 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3338 // directly as well, which may be better.
3339 Tmp1 = PromoteOp(Node->getOperand(0));
3340 assert(Tmp1.getValueType() == NVT);
3341 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3342 if (NoExcessFPPrecision)
3343 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3344 DAG.getValueType(VT));
3345 break;
3346 }
3347
Chris Lattner03c85462005-01-15 05:21:40 +00003348 case ISD::AND:
3349 case ISD::OR:
3350 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003351 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003352 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003353 case ISD::MUL:
3354 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003355 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003356 // that too is okay if they are integer operations.
3357 Tmp1 = PromoteOp(Node->getOperand(0));
3358 Tmp2 = PromoteOp(Node->getOperand(1));
3359 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3360 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003361 break;
3362 case ISD::FADD:
3363 case ISD::FSUB:
3364 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003365 Tmp1 = PromoteOp(Node->getOperand(0));
3366 Tmp2 = PromoteOp(Node->getOperand(1));
3367 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3368 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3369
3370 // Floating point operations will give excess precision that we may not be
3371 // able to tolerate. If we DO allow excess precision, just leave it,
3372 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003373 // FIXME: Why would we need to round FP ops more than integer ones?
3374 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003375 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003376 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3377 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003378 break;
3379
Chris Lattner8b6fa222005-01-15 22:16:26 +00003380 case ISD::SDIV:
3381 case ISD::SREM:
3382 // These operators require that their input be sign extended.
3383 Tmp1 = PromoteOp(Node->getOperand(0));
3384 Tmp2 = PromoteOp(Node->getOperand(1));
3385 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003386 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3387 DAG.getValueType(VT));
3388 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3389 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003390 }
3391 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3392
3393 // Perform FP_ROUND: this is probably overly pessimistic.
3394 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003395 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3396 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003397 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003398 case ISD::FDIV:
3399 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003400 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003401 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003402 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3403 case Legal:
3404 Tmp1 = LegalizeOp(Node->getOperand(0));
3405 break;
3406 case Promote:
3407 Tmp1 = PromoteOp(Node->getOperand(0));
3408 break;
3409 case Expand:
3410 assert(0 && "not implemented");
3411 }
3412 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3413 case Legal:
3414 Tmp2 = LegalizeOp(Node->getOperand(1));
3415 break;
3416 case Promote:
3417 Tmp2 = PromoteOp(Node->getOperand(1));
3418 break;
3419 case Expand:
3420 assert(0 && "not implemented");
3421 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003422 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3423
3424 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003425 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003426 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3427 DAG.getValueType(VT));
3428 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003429
3430 case ISD::UDIV:
3431 case ISD::UREM:
3432 // These operators require that their input be zero extended.
3433 Tmp1 = PromoteOp(Node->getOperand(0));
3434 Tmp2 = PromoteOp(Node->getOperand(1));
3435 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003436 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3437 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003438 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3439 break;
3440
3441 case ISD::SHL:
3442 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003443 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003444 break;
3445 case ISD::SRA:
3446 // The input value must be properly sign extended.
3447 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003448 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3449 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003450 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003451 break;
3452 case ISD::SRL:
3453 // The input value must be properly zero extended.
3454 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003455 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003456 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003457 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003458
3459 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003460 Tmp1 = Node->getOperand(0); // Get the chain.
3461 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003462 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3463 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3464 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3465 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003466 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003467 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003468 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003469 // Increment the pointer, VAList, to the next vaarg
3470 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3471 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3472 TLI.getPointerTy()));
3473 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003474 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3475 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003476 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003477 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003478 }
3479 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003480 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003481 break;
3482
Evan Cheng466685d2006-10-09 20:57:25 +00003483 case ISD::LOAD: {
3484 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003485 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3486 ? ISD::EXTLOAD : LD->getExtensionType();
3487 Result = DAG.getExtLoad(ExtType, NVT,
3488 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003489 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003490 LD->getLoadedVT(),
3491 LD->isVolatile(),
3492 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003493 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003494 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003495 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003496 }
Chris Lattner03c85462005-01-15 05:21:40 +00003497 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003498 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3499 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003500 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003501 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003502 case ISD::SELECT_CC:
3503 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3504 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3505 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003506 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003507 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003508 case ISD::BSWAP:
3509 Tmp1 = Node->getOperand(0);
3510 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3511 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3512 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003513 DAG.getConstant(MVT::getSizeInBits(NVT) -
3514 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003515 TLI.getShiftAmountTy()));
3516 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003517 case ISD::CTPOP:
3518 case ISD::CTTZ:
3519 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003520 // Zero extend the argument
3521 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003522 // Perform the larger operation, then subtract if needed.
3523 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003524 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003525 case ISD::CTPOP:
3526 Result = Tmp1;
3527 break;
3528 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003529 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003530 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003531 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3532 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003533 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003534 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003535 break;
3536 case ISD::CTLZ:
3537 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003538 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003539 DAG.getConstant(MVT::getSizeInBits(NVT) -
3540 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003541 break;
3542 }
3543 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003544 case ISD::EXTRACT_SUBVECTOR:
3545 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003546 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003547 case ISD::EXTRACT_VECTOR_ELT:
3548 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3549 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003550 }
3551
3552 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003553
3554 // Make sure the result is itself legal.
3555 Result = LegalizeOp(Result);
3556
3557 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003558 AddPromotedOperand(Op, Result);
3559 return Result;
3560}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003561
Dan Gohman7f321562007-06-25 16:23:39 +00003562/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3563/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3564/// based on the vector type. The return type of this matches the element type
3565/// of the vector, which may not be legal for the target.
3566SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003567 // We know that operand #0 is the Vec vector. If the index is a constant
3568 // or if the invec is a supported hardware type, we can use it. Otherwise,
3569 // lower to a store then an indexed load.
3570 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003571 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003572
3573 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003574 MVT::ValueType TVT = InVal->getValueType(0);
3575 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003576
Dan Gohman7f321562007-06-25 16:23:39 +00003577 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3578 default: assert(0 && "This action is not supported yet!");
3579 case TargetLowering::Custom: {
3580 Vec = LegalizeOp(Vec);
3581 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3582 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3583 if (Tmp3.Val)
3584 return Tmp3;
3585 break;
3586 }
3587 case TargetLowering::Legal:
3588 if (isTypeLegal(TVT)) {
3589 Vec = LegalizeOp(Vec);
3590 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3591 Op = LegalizeOp(Op);
3592 }
3593 break;
3594 case TargetLowering::Expand:
3595 break;
3596 }
3597
3598 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003599 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003600 Op = ScalarizeVectorOp(Vec);
3601 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3602 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003603 SDOperand Lo, Hi;
3604 SplitVectorOp(Vec, Lo, Hi);
3605 if (CIdx->getValue() < NumElems/2) {
3606 Vec = Lo;
3607 } else {
3608 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003609 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3610 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003611 }
Dan Gohman7f321562007-06-25 16:23:39 +00003612
Chris Lattner15972212006-03-31 17:55:51 +00003613 // It's now an extract from the appropriate high or low part. Recurse.
3614 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003615 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003616 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003617 // Store the value to a temporary stack slot, then LOAD the scalar
3618 // element back out.
3619 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3620 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3621
3622 // Add the offset to the index.
3623 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3624 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3625 DAG.getConstant(EltSize, Idx.getValueType()));
3626 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3627
3628 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003629 }
Dan Gohman7f321562007-06-25 16:23:39 +00003630 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003631}
3632
Dan Gohman7f321562007-06-25 16:23:39 +00003633/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003634/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003635SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003636 // We know that operand #0 is the Vec vector. For now we assume the index
3637 // is a constant and that the extracted result is a supported hardware type.
3638 SDOperand Vec = Op.getOperand(0);
3639 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3640
Dan Gohman7f321562007-06-25 16:23:39 +00003641 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003642
3643 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3644 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003645 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003646 }
3647
3648 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3649 SDOperand Lo, Hi;
3650 SplitVectorOp(Vec, Lo, Hi);
3651 if (CIdx->getValue() < NumElems/2) {
3652 Vec = Lo;
3653 } else {
3654 Vec = Hi;
3655 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3656 }
3657
3658 // It's now an extract from the appropriate high or low part. Recurse.
3659 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003660 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003661}
3662
Nate Begeman750ac1b2006-02-01 07:19:44 +00003663/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3664/// with condition CC on the current target. This usually involves legalizing
3665/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3666/// there may be no choice but to create a new SetCC node to represent the
3667/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3668/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3669void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3670 SDOperand &RHS,
3671 SDOperand &CC) {
3672 SDOperand Tmp1, Tmp2, Result;
3673
3674 switch (getTypeAction(LHS.getValueType())) {
3675 case Legal:
3676 Tmp1 = LegalizeOp(LHS); // LHS
3677 Tmp2 = LegalizeOp(RHS); // RHS
3678 break;
3679 case Promote:
3680 Tmp1 = PromoteOp(LHS); // LHS
3681 Tmp2 = PromoteOp(RHS); // RHS
3682
3683 // If this is an FP compare, the operands have already been extended.
3684 if (MVT::isInteger(LHS.getValueType())) {
3685 MVT::ValueType VT = LHS.getValueType();
3686 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3687
3688 // Otherwise, we have to insert explicit sign or zero extends. Note
3689 // that we could insert sign extends for ALL conditions, but zero extend
3690 // is cheaper on many machines (an AND instead of two shifts), so prefer
3691 // it.
3692 switch (cast<CondCodeSDNode>(CC)->get()) {
3693 default: assert(0 && "Unknown integer comparison!");
3694 case ISD::SETEQ:
3695 case ISD::SETNE:
3696 case ISD::SETUGE:
3697 case ISD::SETUGT:
3698 case ISD::SETULE:
3699 case ISD::SETULT:
3700 // ALL of these operations will work if we either sign or zero extend
3701 // the operands (including the unsigned comparisons!). Zero extend is
3702 // usually a simpler/cheaper operation, so prefer it.
3703 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3704 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3705 break;
3706 case ISD::SETGE:
3707 case ISD::SETGT:
3708 case ISD::SETLT:
3709 case ISD::SETLE:
3710 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3711 DAG.getValueType(VT));
3712 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3713 DAG.getValueType(VT));
3714 break;
3715 }
3716 }
3717 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003718 case Expand: {
3719 MVT::ValueType VT = LHS.getValueType();
3720 if (VT == MVT::f32 || VT == MVT::f64) {
3721 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003722 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003723 switch (cast<CondCodeSDNode>(CC)->get()) {
3724 case ISD::SETEQ:
3725 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003726 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003727 break;
3728 case ISD::SETNE:
3729 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003730 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003731 break;
3732 case ISD::SETGE:
3733 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003734 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003735 break;
3736 case ISD::SETLT:
3737 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003738 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003739 break;
3740 case ISD::SETLE:
3741 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003742 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003743 break;
3744 case ISD::SETGT:
3745 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003746 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003747 break;
3748 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003749 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3750 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003751 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003752 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003753 break;
3754 default:
Evan Cheng56966222007-01-12 02:11:51 +00003755 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003756 switch (cast<CondCodeSDNode>(CC)->get()) {
3757 case ISD::SETONE:
3758 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003759 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003760 // Fallthrough
3761 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003762 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003763 break;
3764 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003765 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003766 break;
3767 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003768 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003769 break;
3770 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003771 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003772 break;
Evan Cheng56966222007-01-12 02:11:51 +00003773 case ISD::SETUEQ:
3774 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00003775 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003776 default: assert(0 && "Unsupported FP setcc!");
3777 }
3778 }
3779
3780 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003781 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003782 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003783 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003784 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00003785 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00003786 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003787 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003788 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003789 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003790 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003791 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00003792 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00003793 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3794 Tmp2 = SDOperand();
3795 }
3796 LHS = Tmp1;
3797 RHS = Tmp2;
3798 return;
3799 }
3800
Nate Begeman750ac1b2006-02-01 07:19:44 +00003801 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3802 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003803 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003804 switch (cast<CondCodeSDNode>(CC)->get()) {
3805 case ISD::SETEQ:
3806 case ISD::SETNE:
3807 if (RHSLo == RHSHi)
3808 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3809 if (RHSCST->isAllOnesValue()) {
3810 // Comparison to -1.
3811 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3812 Tmp2 = RHSLo;
3813 break;
3814 }
3815
3816 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3817 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3818 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3819 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3820 break;
3821 default:
3822 // If this is a comparison of the sign bit, just look at the top part.
3823 // X > -1, x < 0
3824 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3825 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3826 CST->getValue() == 0) || // X < 0
3827 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3828 CST->isAllOnesValue())) { // X > -1
3829 Tmp1 = LHSHi;
3830 Tmp2 = RHSHi;
3831 break;
3832 }
3833
3834 // FIXME: This generated code sucks.
3835 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00003836 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3837 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003838 default: assert(0 && "Unknown integer setcc!");
3839 case ISD::SETLT:
3840 case ISD::SETULT: LowCC = ISD::SETULT; break;
3841 case ISD::SETGT:
3842 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3843 case ISD::SETLE:
3844 case ISD::SETULE: LowCC = ISD::SETULE; break;
3845 case ISD::SETGE:
3846 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3847 }
3848
3849 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3850 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3851 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3852
3853 // NOTE: on targets without efficient SELECT of bools, we can always use
3854 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00003855 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3856 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3857 false, DagCombineInfo);
3858 if (!Tmp1.Val)
3859 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3860 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3861 CCCode, false, DagCombineInfo);
3862 if (!Tmp2.Val)
3863 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3864
3865 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3866 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3867 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3868 (Tmp2C && Tmp2C->getValue() == 0 &&
3869 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3870 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3871 (Tmp2C && Tmp2C->getValue() == 1 &&
3872 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3873 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3874 // low part is known false, returns high part.
3875 // For LE / GE, if high part is known false, ignore the low part.
3876 // For LT / GT, if high part is known true, ignore the low part.
3877 Tmp1 = Tmp2;
3878 Tmp2 = SDOperand();
3879 } else {
3880 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3881 ISD::SETEQ, false, DagCombineInfo);
3882 if (!Result.Val)
3883 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3884 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3885 Result, Tmp1, Tmp2));
3886 Tmp1 = Result;
3887 Tmp2 = SDOperand();
3888 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003889 }
3890 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003891 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003892 LHS = Tmp1;
3893 RHS = Tmp2;
3894}
3895
Chris Lattner35481892005-12-23 00:16:34 +00003896/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003897/// The resultant code need not be legal. Note that SrcOp is the input operand
3898/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003899SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3900 SDOperand SrcOp) {
3901 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003902 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003903
3904 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003905 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003906 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003907 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003908}
3909
Chris Lattner4352cc92006-04-04 17:23:26 +00003910SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3911 // Create a vector sized/aligned stack slot, store the value to element #0,
3912 // then load the whole vector back out.
3913 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003914 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003915 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003916 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003917}
3918
3919
Chris Lattnerce872152006-03-19 06:31:19 +00003920/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3921/// support the operation, but do support the resultant packed vector type.
3922SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3923
3924 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003925 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003926 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003927 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003928 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003929 std::map<SDOperand, std::vector<unsigned> > Values;
3930 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003931 bool isConstant = true;
3932 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3933 SplatValue.getOpcode() != ISD::UNDEF)
3934 isConstant = false;
3935
Evan Cheng033e6812006-03-24 01:17:21 +00003936 for (unsigned i = 1; i < NumElems; ++i) {
3937 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003938 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003939 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003940 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003941 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003942 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003943
3944 // If this isn't a constant element or an undef, we can't use a constant
3945 // pool load.
3946 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3947 V.getOpcode() != ISD::UNDEF)
3948 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003949 }
3950
3951 if (isOnlyLowElement) {
3952 // If the low element is an undef too, then this whole things is an undef.
3953 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3954 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3955 // Otherwise, turn this into a scalar_to_vector node.
3956 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3957 Node->getOperand(0));
3958 }
3959
Chris Lattner2eb86532006-03-24 07:29:17 +00003960 // If all elements are constants, create a load from the constant pool.
3961 if (isConstant) {
3962 MVT::ValueType VT = Node->getValueType(0);
3963 const Type *OpNTy =
3964 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3965 std::vector<Constant*> CV;
3966 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3967 if (ConstantFPSDNode *V =
3968 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3969 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3970 } else if (ConstantSDNode *V =
3971 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003972 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003973 } else {
3974 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3975 CV.push_back(UndefValue::get(OpNTy));
3976 }
3977 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00003978 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00003979 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003980 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003981 }
3982
Chris Lattner87100e02006-03-20 01:52:29 +00003983 if (SplatValue.Val) { // Splat of one value?
3984 // Build the shuffle constant vector: <0, 0, 0, 0>
3985 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003986 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003987 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003988 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003989 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3990 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003991
3992 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003993 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003994 // Get the splatted value into the low element of a vector register.
3995 SDOperand LowValVec =
3996 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3997
3998 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3999 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4000 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4001 SplatMask);
4002 }
4003 }
4004
Evan Cheng033e6812006-03-24 01:17:21 +00004005 // If there are only two unique elements, we may be able to turn this into a
4006 // vector shuffle.
4007 if (Values.size() == 2) {
4008 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4009 MVT::ValueType MaskVT =
4010 MVT::getIntVectorWithNumElements(NumElems);
4011 std::vector<SDOperand> MaskVec(NumElems);
4012 unsigned i = 0;
4013 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4014 E = Values.end(); I != E; ++I) {
4015 for (std::vector<unsigned>::iterator II = I->second.begin(),
4016 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004017 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004018 i += NumElems;
4019 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004020 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4021 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004022
4023 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004024 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4025 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004026 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004027 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4028 E = Values.end(); I != E; ++I) {
4029 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4030 I->first);
4031 Ops.push_back(Op);
4032 }
4033 Ops.push_back(ShuffleMask);
4034
4035 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004036 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4037 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004038 }
4039 }
Chris Lattnerce872152006-03-19 06:31:19 +00004040
4041 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4042 // aligned object on the stack, store each element into it, then load
4043 // the result as a vector.
4044 MVT::ValueType VT = Node->getValueType(0);
4045 // Create the stack frame object.
4046 SDOperand FIPtr = CreateStackTemporary(VT);
4047
4048 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004049 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004050 unsigned TypeByteSize =
4051 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004052 // Store (in the right endianness) the elements to memory.
4053 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4054 // Ignore undef elements.
4055 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4056
Chris Lattner841c8822006-03-22 01:46:54 +00004057 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004058
4059 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4060 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4061
Evan Cheng786225a2006-10-05 23:01:46 +00004062 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004063 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004064 }
4065
4066 SDOperand StoreChain;
4067 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004068 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4069 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004070 else
4071 StoreChain = DAG.getEntryNode();
4072
4073 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004074 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004075}
4076
4077/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4078/// specified value type.
4079SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4080 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4081 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004082 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004083 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004084 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004085 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4086}
4087
Chris Lattner5b359c62005-04-02 04:00:59 +00004088void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4089 SDOperand Op, SDOperand Amt,
4090 SDOperand &Lo, SDOperand &Hi) {
4091 // Expand the subcomponents.
4092 SDOperand LHSL, LHSH;
4093 ExpandOp(Op, LHSL, LHSH);
4094
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004095 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004096 MVT::ValueType VT = LHSL.getValueType();
4097 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004098 Hi = Lo.getValue(1);
4099}
4100
4101
Chris Lattnere34b3962005-01-19 04:19:40 +00004102/// ExpandShift - Try to find a clever way to expand this shift operation out to
4103/// smaller elements. If we can't find a way that is more efficient than a
4104/// libcall on this target, return false. Otherwise, return true with the
4105/// low-parts expanded into Lo and Hi.
4106bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4107 SDOperand &Lo, SDOperand &Hi) {
4108 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4109 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004110
Chris Lattnere34b3962005-01-19 04:19:40 +00004111 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004112 SDOperand ShAmt = LegalizeOp(Amt);
4113 MVT::ValueType ShTy = ShAmt.getValueType();
4114 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4115 unsigned NVTBits = MVT::getSizeInBits(NVT);
4116
4117 // Handle the case when Amt is an immediate. Other cases are currently broken
4118 // and are disabled.
4119 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4120 unsigned Cst = CN->getValue();
4121 // Expand the incoming operand to be shifted, so that we have its parts
4122 SDOperand InL, InH;
4123 ExpandOp(Op, InL, InH);
4124 switch(Opc) {
4125 case ISD::SHL:
4126 if (Cst > VTBits) {
4127 Lo = DAG.getConstant(0, NVT);
4128 Hi = DAG.getConstant(0, NVT);
4129 } else if (Cst > NVTBits) {
4130 Lo = DAG.getConstant(0, NVT);
4131 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004132 } else if (Cst == NVTBits) {
4133 Lo = DAG.getConstant(0, NVT);
4134 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004135 } else {
4136 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4137 Hi = DAG.getNode(ISD::OR, NVT,
4138 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4139 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4140 }
4141 return true;
4142 case ISD::SRL:
4143 if (Cst > VTBits) {
4144 Lo = DAG.getConstant(0, NVT);
4145 Hi = DAG.getConstant(0, NVT);
4146 } else if (Cst > NVTBits) {
4147 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4148 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004149 } else if (Cst == NVTBits) {
4150 Lo = InH;
4151 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004152 } else {
4153 Lo = DAG.getNode(ISD::OR, NVT,
4154 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4155 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4156 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4157 }
4158 return true;
4159 case ISD::SRA:
4160 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004161 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004162 DAG.getConstant(NVTBits-1, ShTy));
4163 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004164 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004165 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004166 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004167 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004168 } else if (Cst == NVTBits) {
4169 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004170 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004171 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004172 } else {
4173 Lo = DAG.getNode(ISD::OR, NVT,
4174 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4175 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4176 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4177 }
4178 return true;
4179 }
4180 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004181
4182 // Okay, the shift amount isn't constant. However, if we can tell that it is
4183 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4184 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004185 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004186
4187 // If we know that the high bit of the shift amount is one, then we can do
4188 // this as a couple of simple shifts.
4189 if (KnownOne & Mask) {
4190 // Mask out the high bit, which we know is set.
4191 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4192 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4193
4194 // Expand the incoming operand to be shifted, so that we have its parts
4195 SDOperand InL, InH;
4196 ExpandOp(Op, InL, InH);
4197 switch(Opc) {
4198 case ISD::SHL:
4199 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4200 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4201 return true;
4202 case ISD::SRL:
4203 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4204 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4205 return true;
4206 case ISD::SRA:
4207 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4208 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4209 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4210 return true;
4211 }
4212 }
4213
4214 // If we know that the high bit of the shift amount is zero, then we can do
4215 // this as a couple of simple shifts.
4216 if (KnownZero & Mask) {
4217 // Compute 32-amt.
4218 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4219 DAG.getConstant(NVTBits, Amt.getValueType()),
4220 Amt);
4221
4222 // Expand the incoming operand to be shifted, so that we have its parts
4223 SDOperand InL, InH;
4224 ExpandOp(Op, InL, InH);
4225 switch(Opc) {
4226 case ISD::SHL:
4227 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4228 Hi = DAG.getNode(ISD::OR, NVT,
4229 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4230 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4231 return true;
4232 case ISD::SRL:
4233 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4234 Lo = DAG.getNode(ISD::OR, NVT,
4235 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4236 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4237 return true;
4238 case ISD::SRA:
4239 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4240 Lo = DAG.getNode(ISD::OR, NVT,
4241 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4242 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4243 return true;
4244 }
4245 }
4246
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004247 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004248}
Chris Lattner77e77a62005-01-21 06:05:23 +00004249
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004250
Chris Lattner77e77a62005-01-21 06:05:23 +00004251// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4252// does not fit into a register, return the lo part and set the hi part to the
4253// by-reg argument. If it does fit into a single register, return the result
4254// and leave the Hi part unset.
4255SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004256 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004257 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4258 // The input chain to this libcall is the entry node of the function.
4259 // Legalizing the call will automatically add the previous call to the
4260 // dependence.
4261 SDOperand InChain = DAG.getEntryNode();
4262
Chris Lattner77e77a62005-01-21 06:05:23 +00004263 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004264 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004265 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4266 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4267 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004268 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004269 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004270 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004271 }
4272 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004273
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004274 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004275 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004276 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004277 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004278 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004279
Chris Lattner6831a812006-02-13 09:18:02 +00004280 // Legalize the call sequence, starting with the chain. This will advance
4281 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4282 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4283 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004284 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004285 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004286 default: assert(0 && "Unknown thing");
4287 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004288 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004289 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004290 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004291 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004292 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004293 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004294 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004295}
4296
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004297
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004298/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4299///
Chris Lattner77e77a62005-01-21 06:05:23 +00004300SDOperand SelectionDAGLegalize::
4301ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004302 assert(getTypeAction(Source.getValueType()) == Expand &&
4303 "This is not an expansion!");
4304 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4305
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004306 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004307 assert(Source.getValueType() == MVT::i64 &&
4308 "This only works for 64-bit -> FP");
4309 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4310 // incoming integer is set. To handle this, we dynamically test to see if
4311 // it is set, and, if so, add a fudge factor.
4312 SDOperand Lo, Hi;
4313 ExpandOp(Source, Lo, Hi);
4314
Chris Lattner66de05b2005-05-13 04:45:13 +00004315 // If this is unsigned, and not supported, first perform the conversion to
4316 // signed, then adjust the result if the sign bit is set.
4317 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4318 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4319
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004320 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4321 DAG.getConstant(0, Hi.getValueType()),
4322 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004323 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4324 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4325 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004326 uint64_t FF = 0x5f800000ULL;
4327 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004328 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004329
Chris Lattner5839bf22005-08-26 17:15:30 +00004330 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004331 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4332 SDOperand FudgeInReg;
4333 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004334 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004335 else {
4336 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004337 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004338 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004339 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004340 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004341 MVT::ValueType SCVT = SignedConv.getValueType();
4342 if (SCVT != DestTy) {
4343 // Destination type needs to be expanded as well. The FADD now we are
4344 // constructing will be expanded into a libcall.
4345 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4346 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4347 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4348 SignedConv, SignedConv.getValue(1));
4349 }
4350 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4351 }
Chris Lattner473a9902005-09-29 06:44:39 +00004352 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004353 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004354
Chris Lattnera88a2602005-05-14 05:33:54 +00004355 // Check to see if the target has a custom way to lower this. If so, use it.
4356 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4357 default: assert(0 && "This action not implemented for this operation!");
4358 case TargetLowering::Legal:
4359 case TargetLowering::Expand:
4360 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004361 case TargetLowering::Custom: {
4362 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4363 Source), DAG);
4364 if (NV.Val)
4365 return LegalizeOp(NV);
4366 break; // The target decided this was legal after all
4367 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004368 }
4369
Chris Lattner13689e22005-05-12 07:00:44 +00004370 // Expand the source, then glue it back together for the call. We must expand
4371 // the source in case it is shared (this pass of legalize must traverse it).
4372 SDOperand SrcLo, SrcHi;
4373 ExpandOp(Source, SrcLo, SrcHi);
4374 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4375
Evan Cheng56966222007-01-12 02:11:51 +00004376 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004377 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004378 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004379 else {
4380 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004381 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004382 }
Chris Lattner6831a812006-02-13 09:18:02 +00004383
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004384 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004385 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4386 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004387 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4388 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004389}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004390
Chris Lattner22cde6a2006-01-28 08:25:58 +00004391/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4392/// INT_TO_FP operation of the specified operand when the target requests that
4393/// we expand it. At this point, we know that the result and operand types are
4394/// legal for the target.
4395SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4396 SDOperand Op0,
4397 MVT::ValueType DestVT) {
4398 if (Op0.getValueType() == MVT::i32) {
4399 // simple 32-bit [signed|unsigned] integer to float/double expansion
4400
Chris Lattner58092e32007-01-20 22:35:55 +00004401 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004402 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004403 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4404 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004405 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004406 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004407 // get address of 8 byte buffer
4408 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4409 // word offset constant for Hi/Lo address computation
4410 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4411 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004412 SDOperand Hi = StackSlot;
4413 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4414 if (TLI.isLittleEndian())
4415 std::swap(Hi, Lo);
4416
Chris Lattner22cde6a2006-01-28 08:25:58 +00004417 // if signed map to unsigned space
4418 SDOperand Op0Mapped;
4419 if (isSigned) {
4420 // constant used to invert sign bit (signed to unsigned mapping)
4421 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4422 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4423 } else {
4424 Op0Mapped = Op0;
4425 }
4426 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004427 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004428 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004429 // initial hi portion of constructed double
4430 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4431 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004432 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004433 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004434 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004435 // FP constant to bias correct the final result
4436 SDOperand Bias = DAG.getConstantFP(isSigned ?
4437 BitsToDouble(0x4330000080000000ULL)
4438 : BitsToDouble(0x4330000000000000ULL),
4439 MVT::f64);
4440 // subtract the bias
4441 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4442 // final result
4443 SDOperand Result;
4444 // handle final rounding
4445 if (DestVT == MVT::f64) {
4446 // do nothing
4447 Result = Sub;
4448 } else {
4449 // if f32 then cast to f32
4450 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4451 }
4452 return Result;
4453 }
4454 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4455 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4456
4457 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4458 DAG.getConstant(0, Op0.getValueType()),
4459 ISD::SETLT);
4460 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4461 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4462 SignSet, Four, Zero);
4463
4464 // If the sign bit of the integer is set, the large number will be treated
4465 // as a negative number. To counteract this, the dynamic code adds an
4466 // offset depending on the data type.
4467 uint64_t FF;
4468 switch (Op0.getValueType()) {
4469 default: assert(0 && "Unsupported integer type!");
4470 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4471 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4472 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4473 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4474 }
4475 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004476 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004477
4478 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4479 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4480 SDOperand FudgeInReg;
4481 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004482 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004483 else {
4484 assert(DestVT == MVT::f64 && "Unexpected conversion");
4485 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4486 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004487 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004488 }
4489
4490 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4491}
4492
4493/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4494/// *INT_TO_FP operation of the specified operand when the target requests that
4495/// we promote it. At this point, we know that the result and operand types are
4496/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4497/// operation that takes a larger input.
4498SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4499 MVT::ValueType DestVT,
4500 bool isSigned) {
4501 // First step, figure out the appropriate *INT_TO_FP operation to use.
4502 MVT::ValueType NewInTy = LegalOp.getValueType();
4503
4504 unsigned OpToUse = 0;
4505
4506 // Scan for the appropriate larger type to use.
4507 while (1) {
4508 NewInTy = (MVT::ValueType)(NewInTy+1);
4509 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4510
4511 // If the target supports SINT_TO_FP of this type, use it.
4512 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4513 default: break;
4514 case TargetLowering::Legal:
4515 if (!TLI.isTypeLegal(NewInTy))
4516 break; // Can't use this datatype.
4517 // FALL THROUGH.
4518 case TargetLowering::Custom:
4519 OpToUse = ISD::SINT_TO_FP;
4520 break;
4521 }
4522 if (OpToUse) break;
4523 if (isSigned) continue;
4524
4525 // If the target supports UINT_TO_FP of this type, use it.
4526 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4527 default: break;
4528 case TargetLowering::Legal:
4529 if (!TLI.isTypeLegal(NewInTy))
4530 break; // Can't use this datatype.
4531 // FALL THROUGH.
4532 case TargetLowering::Custom:
4533 OpToUse = ISD::UINT_TO_FP;
4534 break;
4535 }
4536 if (OpToUse) break;
4537
4538 // Otherwise, try a larger type.
4539 }
4540
4541 // Okay, we found the operation and type to use. Zero extend our input to the
4542 // desired type then run the operation on it.
4543 return DAG.getNode(OpToUse, DestVT,
4544 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4545 NewInTy, LegalOp));
4546}
4547
4548/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4549/// FP_TO_*INT operation of the specified operand when the target requests that
4550/// we promote it. At this point, we know that the result and operand types are
4551/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4552/// operation that returns a larger result.
4553SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4554 MVT::ValueType DestVT,
4555 bool isSigned) {
4556 // First step, figure out the appropriate FP_TO*INT operation to use.
4557 MVT::ValueType NewOutTy = DestVT;
4558
4559 unsigned OpToUse = 0;
4560
4561 // Scan for the appropriate larger type to use.
4562 while (1) {
4563 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4564 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4565
4566 // If the target supports FP_TO_SINT returning this type, use it.
4567 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4568 default: break;
4569 case TargetLowering::Legal:
4570 if (!TLI.isTypeLegal(NewOutTy))
4571 break; // Can't use this datatype.
4572 // FALL THROUGH.
4573 case TargetLowering::Custom:
4574 OpToUse = ISD::FP_TO_SINT;
4575 break;
4576 }
4577 if (OpToUse) break;
4578
4579 // If the target supports FP_TO_UINT of this type, use it.
4580 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4581 default: break;
4582 case TargetLowering::Legal:
4583 if (!TLI.isTypeLegal(NewOutTy))
4584 break; // Can't use this datatype.
4585 // FALL THROUGH.
4586 case TargetLowering::Custom:
4587 OpToUse = ISD::FP_TO_UINT;
4588 break;
4589 }
4590 if (OpToUse) break;
4591
4592 // Otherwise, try a larger type.
4593 }
4594
4595 // Okay, we found the operation and type to use. Truncate the result of the
4596 // extended FP_TO_*INT operation to the desired size.
4597 return DAG.getNode(ISD::TRUNCATE, DestVT,
4598 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4599}
4600
4601/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4602///
4603SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4604 MVT::ValueType VT = Op.getValueType();
4605 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4606 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4607 switch (VT) {
4608 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4609 case MVT::i16:
4610 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4611 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4612 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4613 case MVT::i32:
4614 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4615 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4616 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4617 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4618 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4619 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4620 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4621 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4622 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4623 case MVT::i64:
4624 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4625 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4626 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4627 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4628 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4629 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4630 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4631 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4632 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4633 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4634 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4635 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4636 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4637 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4638 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4639 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4640 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4641 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4642 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4643 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4644 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4645 }
4646}
4647
4648/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4649///
4650SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4651 switch (Opc) {
4652 default: assert(0 && "Cannot expand this yet!");
4653 case ISD::CTPOP: {
4654 static const uint64_t mask[6] = {
4655 0x5555555555555555ULL, 0x3333333333333333ULL,
4656 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4657 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4658 };
4659 MVT::ValueType VT = Op.getValueType();
4660 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004661 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004662 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4663 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4664 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4665 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4666 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4667 DAG.getNode(ISD::AND, VT,
4668 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4669 }
4670 return Op;
4671 }
4672 case ISD::CTLZ: {
4673 // for now, we do this:
4674 // x = x | (x >> 1);
4675 // x = x | (x >> 2);
4676 // ...
4677 // x = x | (x >>16);
4678 // x = x | (x >>32); // for 64-bit input
4679 // return popcount(~x);
4680 //
4681 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4682 MVT::ValueType VT = Op.getValueType();
4683 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004684 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004685 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4686 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4687 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4688 }
4689 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4690 return DAG.getNode(ISD::CTPOP, VT, Op);
4691 }
4692 case ISD::CTTZ: {
4693 // for now, we use: { return popcount(~x & (x - 1)); }
4694 // unless the target has ctlz but not ctpop, in which case we use:
4695 // { return 32 - nlz(~x & (x-1)); }
4696 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4697 MVT::ValueType VT = Op.getValueType();
4698 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4699 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4700 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4701 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4702 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4703 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4704 TLI.isOperationLegal(ISD::CTLZ, VT))
4705 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004706 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004707 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4708 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4709 }
4710 }
4711}
Chris Lattnere34b3962005-01-19 04:19:40 +00004712
Chris Lattner3e928bb2005-01-07 07:47:09 +00004713/// ExpandOp - Expand the specified SDOperand into its two component pieces
4714/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4715/// LegalizeNodes map is filled in for any results that are not expanded, the
4716/// ExpandedNodes map is filled in for any results that are expanded, and the
4717/// Lo/Hi values are returned.
4718void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4719 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004720 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004721 SDNode *Node = Op.Val;
4722 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004723 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004724 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004725 "Cannot expand to FP value or to larger int value!");
4726
Chris Lattner6fdcb252005-09-02 20:32:45 +00004727 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004728 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004729 = ExpandedNodes.find(Op);
4730 if (I != ExpandedNodes.end()) {
4731 Lo = I->second.first;
4732 Hi = I->second.second;
4733 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004734 }
4735
Chris Lattner3e928bb2005-01-07 07:47:09 +00004736 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004737 case ISD::CopyFromReg:
4738 assert(0 && "CopyFromReg must be legal!");
4739 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004740#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004741 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004742#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004743 assert(0 && "Do not know how to expand this operator!");
4744 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004745 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004746 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004747 Lo = DAG.getNode(ISD::UNDEF, NVT);
4748 Hi = DAG.getNode(ISD::UNDEF, NVT);
4749 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004750 case ISD::Constant: {
4751 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4752 Lo = DAG.getConstant(Cst, NVT);
4753 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4754 break;
4755 }
Evan Cheng00495212006-12-12 21:32:44 +00004756 case ISD::ConstantFP: {
4757 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004758 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004759 if (getTypeAction(Lo.getValueType()) == Expand)
4760 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004761 break;
4762 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004763 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004764 // Return the operands.
4765 Lo = Node->getOperand(0);
4766 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004767 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004768
4769 case ISD::SIGN_EXTEND_INREG:
4770 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004771 // sext_inreg the low part if needed.
4772 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4773
4774 // The high part gets the sign extension from the lo-part. This handles
4775 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004776 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4777 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4778 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004779 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004780
Nate Begemand88fc032006-01-14 03:14:10 +00004781 case ISD::BSWAP: {
4782 ExpandOp(Node->getOperand(0), Lo, Hi);
4783 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4784 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4785 Lo = TempLo;
4786 break;
4787 }
4788
Chris Lattneredb1add2005-05-11 04:51:16 +00004789 case ISD::CTPOP:
4790 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004791 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4792 DAG.getNode(ISD::CTPOP, NVT, Lo),
4793 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004794 Hi = DAG.getConstant(0, NVT);
4795 break;
4796
Chris Lattner39a8f332005-05-12 19:05:01 +00004797 case ISD::CTLZ: {
4798 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004799 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004800 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4801 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004802 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4803 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004804 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4805 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4806
4807 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4808 Hi = DAG.getConstant(0, NVT);
4809 break;
4810 }
4811
4812 case ISD::CTTZ: {
4813 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004814 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004815 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4816 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004817 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4818 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004819 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4820 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4821
4822 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4823 Hi = DAG.getConstant(0, NVT);
4824 break;
4825 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004826
Nate Begemanacc398c2006-01-25 18:21:52 +00004827 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004828 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4829 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004830 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4831 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4832
4833 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004834 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004835 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4836 if (!TLI.isLittleEndian())
4837 std::swap(Lo, Hi);
4838 break;
4839 }
4840
Chris Lattner3e928bb2005-01-07 07:47:09 +00004841 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004842 LoadSDNode *LD = cast<LoadSDNode>(Node);
4843 SDOperand Ch = LD->getChain(); // Legalize the chain.
4844 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4845 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004846 int SVOffset = LD->getSrcValueOffset();
4847 unsigned Alignment = LD->getAlignment();
4848 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004849
Evan Cheng466685d2006-10-09 20:57:25 +00004850 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004851 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4852 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00004853 if (VT == MVT::f32 || VT == MVT::f64) {
4854 // f32->i32 or f64->i64 one to one expansion.
4855 // Remember that we legalized the chain.
4856 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004857 // Recursively expand the new load.
4858 if (getTypeAction(NVT) == Expand)
4859 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004860 break;
4861 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004862
Evan Cheng466685d2006-10-09 20:57:25 +00004863 // Increment the pointer to the other half.
4864 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4865 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4866 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00004867 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004868 if (Alignment > IncrementSize)
4869 Alignment = IncrementSize;
4870 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4871 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004872
Evan Cheng466685d2006-10-09 20:57:25 +00004873 // Build a factor node to remember that this load is independent of the
4874 // other one.
4875 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4876 Hi.getValue(1));
4877
4878 // Remember that we legalized the chain.
4879 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4880 if (!TLI.isLittleEndian())
4881 std::swap(Lo, Hi);
4882 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004883 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004884
4885 if (VT == MVT::f64 && EVT == MVT::f32) {
4886 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4887 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004888 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00004889 // Remember that we legalized the chain.
4890 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4891 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4892 break;
4893 }
Evan Cheng466685d2006-10-09 20:57:25 +00004894
4895 if (EVT == NVT)
4896 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004897 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004898 else
4899 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004900 SVOffset, EVT, isVolatile,
4901 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004902
4903 // Remember that we legalized the chain.
4904 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4905
4906 if (ExtType == ISD::SEXTLOAD) {
4907 // The high part is obtained by SRA'ing all but one of the bits of the
4908 // lo part.
4909 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4910 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4911 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4912 } else if (ExtType == ISD::ZEXTLOAD) {
4913 // The high part is just a zero.
4914 Hi = DAG.getConstant(0, NVT);
4915 } else /* if (ExtType == ISD::EXTLOAD) */ {
4916 // The high part is undefined.
4917 Hi = DAG.getNode(ISD::UNDEF, NVT);
4918 }
4919 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004920 break;
4921 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004922 case ISD::AND:
4923 case ISD::OR:
4924 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4925 SDOperand LL, LH, RL, RH;
4926 ExpandOp(Node->getOperand(0), LL, LH);
4927 ExpandOp(Node->getOperand(1), RL, RH);
4928 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4929 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4930 break;
4931 }
4932 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004933 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004934 ExpandOp(Node->getOperand(1), LL, LH);
4935 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004936 if (getTypeAction(NVT) == Expand)
4937 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004938 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004939 if (VT != MVT::f32)
4940 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004941 break;
4942 }
Nate Begeman9373a812005-08-10 20:51:12 +00004943 case ISD::SELECT_CC: {
4944 SDOperand TL, TH, FL, FH;
4945 ExpandOp(Node->getOperand(2), TL, TH);
4946 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004947 if (getTypeAction(NVT) == Expand)
4948 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004949 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4950 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004951 if (VT != MVT::f32)
4952 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4953 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004954 break;
4955 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004956 case ISD::ANY_EXTEND:
4957 // The low part is any extension of the input (which degenerates to a copy).
4958 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4959 // The high part is undefined.
4960 Hi = DAG.getNode(ISD::UNDEF, NVT);
4961 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004962 case ISD::SIGN_EXTEND: {
4963 // The low part is just a sign extension of the input (which degenerates to
4964 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004965 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004966
Chris Lattner3e928bb2005-01-07 07:47:09 +00004967 // The high part is obtained by SRA'ing all but one of the bits of the lo
4968 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004969 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004970 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4971 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004972 break;
4973 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004974 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004975 // The low part is just a zero extension of the input (which degenerates to
4976 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004977 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004978
Chris Lattner3e928bb2005-01-07 07:47:09 +00004979 // The high part is just a zero.
4980 Hi = DAG.getConstant(0, NVT);
4981 break;
Chris Lattner35481892005-12-23 00:16:34 +00004982
Chris Lattner4c948eb2007-02-13 23:55:16 +00004983 case ISD::TRUNCATE: {
4984 // The input value must be larger than this value. Expand *it*.
4985 SDOperand NewLo;
4986 ExpandOp(Node->getOperand(0), NewLo, Hi);
4987
4988 // The low part is now either the right size, or it is closer. If not the
4989 // right size, make an illegal truncate so we recursively expand it.
4990 if (NewLo.getValueType() != Node->getValueType(0))
4991 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
4992 ExpandOp(NewLo, Lo, Hi);
4993 break;
4994 }
4995
Chris Lattner35481892005-12-23 00:16:34 +00004996 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004997 SDOperand Tmp;
4998 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4999 // If the target wants to, allow it to lower this itself.
5000 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5001 case Expand: assert(0 && "cannot expand FP!");
5002 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5003 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5004 }
5005 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5006 }
5007
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005008 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005009 if (VT == MVT::f32 || VT == MVT::f64) {
5010 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005011 if (getTypeAction(NVT) == Expand)
5012 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005013 break;
5014 }
5015
5016 // If source operand will be expanded to the same type as VT, i.e.
5017 // i64 <- f64, i32 <- f32, expand the source operand instead.
5018 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5019 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5020 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005021 break;
5022 }
5023
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005024 // Turn this into a load/store pair by default.
5025 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005026 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005027
Chris Lattner35481892005-12-23 00:16:34 +00005028 ExpandOp(Tmp, Lo, Hi);
5029 break;
5030 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005031
Chris Lattner8137c9e2006-01-28 05:07:51 +00005032 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005033 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5034 TargetLowering::Custom &&
5035 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005036 Lo = TLI.LowerOperation(Op, DAG);
5037 assert(Lo.Val && "Node must be custom expanded!");
5038 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005039 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005040 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005041 break;
5042
Chris Lattner4e6c7462005-01-08 19:27:05 +00005043 // These operators cannot be expanded directly, emit them as calls to
5044 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005045 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005046 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005047 SDOperand Op;
5048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5049 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005050 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5051 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005052 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005053
Chris Lattnerf20d1832005-07-30 01:40:57 +00005054 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5055
Chris Lattner80a3e942005-07-29 00:33:32 +00005056 // Now that the custom expander is done, expand the result, which is still
5057 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005058 if (Op.Val) {
5059 ExpandOp(Op, Lo, Hi);
5060 break;
5061 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005062 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005063
Evan Cheng56966222007-01-12 02:11:51 +00005064 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005065 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005066 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005067 else
Evan Cheng56966222007-01-12 02:11:51 +00005068 LC = RTLIB::FPTOSINT_F64_I64;
5069 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5070 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005071 break;
Evan Cheng56966222007-01-12 02:11:51 +00005072 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005073
Evan Cheng56966222007-01-12 02:11:51 +00005074 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005075 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005076 SDOperand Op;
5077 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5078 case Expand: assert(0 && "cannot expand FP!");
5079 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5080 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5081 }
5082
5083 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5084
5085 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005086 if (Op.Val) {
5087 ExpandOp(Op, Lo, Hi);
5088 break;
5089 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005090 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005091
Evan Cheng56966222007-01-12 02:11:51 +00005092 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005093 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005094 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005095 else
Evan Cheng56966222007-01-12 02:11:51 +00005096 LC = RTLIB::FPTOUINT_F64_I64;
5097 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5098 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005099 break;
Evan Cheng56966222007-01-12 02:11:51 +00005100 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005101
Evan Cheng05a2d562006-01-09 18:31:59 +00005102 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005103 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005104 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005105 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005106 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005107 Op = TLI.LowerOperation(Op, DAG);
5108 if (Op.Val) {
5109 // Now that the custom expander is done, expand the result, which is
5110 // still VT.
5111 ExpandOp(Op, Lo, Hi);
5112 break;
5113 }
5114 }
5115
Chris Lattner79980b02006-09-13 03:50:39 +00005116 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5117 // this X << 1 as X+X.
5118 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5119 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5120 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5121 SDOperand LoOps[2], HiOps[3];
5122 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5123 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5124 LoOps[1] = LoOps[0];
5125 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5126
5127 HiOps[1] = HiOps[0];
5128 HiOps[2] = Lo.getValue(1);
5129 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5130 break;
5131 }
5132 }
5133
Chris Lattnere34b3962005-01-19 04:19:40 +00005134 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005135 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005136 break;
Chris Lattner47599822005-04-02 03:38:53 +00005137
5138 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005139 TargetLowering::LegalizeAction Action =
5140 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5141 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5142 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005143 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005144 break;
5145 }
5146
Chris Lattnere34b3962005-01-19 04:19:40 +00005147 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005148 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5149 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005150 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005151 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005152
Evan Cheng05a2d562006-01-09 18:31:59 +00005153 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005154 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005155 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005156 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005157 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005158 Op = TLI.LowerOperation(Op, DAG);
5159 if (Op.Val) {
5160 // Now that the custom expander is done, expand the result, which is
5161 // still VT.
5162 ExpandOp(Op, Lo, Hi);
5163 break;
5164 }
5165 }
5166
Chris Lattnere34b3962005-01-19 04:19:40 +00005167 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005168 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005169 break;
Chris Lattner47599822005-04-02 03:38:53 +00005170
5171 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005172 TargetLowering::LegalizeAction Action =
5173 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5174 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5175 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005176 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005177 break;
5178 }
5179
Chris Lattnere34b3962005-01-19 04:19:40 +00005180 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005181 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5182 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005183 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005184 }
5185
5186 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005187 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005188 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005189 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005190 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005191 Op = TLI.LowerOperation(Op, DAG);
5192 if (Op.Val) {
5193 // Now that the custom expander is done, expand the result, which is
5194 // still VT.
5195 ExpandOp(Op, Lo, Hi);
5196 break;
5197 }
5198 }
5199
Chris Lattnere34b3962005-01-19 04:19:40 +00005200 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005201 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005202 break;
Chris Lattner47599822005-04-02 03:38:53 +00005203
5204 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005205 TargetLowering::LegalizeAction Action =
5206 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5207 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5208 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005209 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005210 break;
5211 }
5212
Chris Lattnere34b3962005-01-19 04:19:40 +00005213 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005214 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5215 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005216 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005217 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005218
Misha Brukmanedf128a2005-04-21 22:36:52 +00005219 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005220 case ISD::SUB: {
5221 // If the target wants to custom expand this, let them.
5222 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5223 TargetLowering::Custom) {
5224 Op = TLI.LowerOperation(Op, DAG);
5225 if (Op.Val) {
5226 ExpandOp(Op, Lo, Hi);
5227 break;
5228 }
5229 }
5230
5231 // Expand the subcomponents.
5232 SDOperand LHSL, LHSH, RHSL, RHSH;
5233 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5234 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005235 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5236 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005237 LoOps[0] = LHSL;
5238 LoOps[1] = RHSL;
5239 HiOps[0] = LHSH;
5240 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005241 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005242 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005243 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005244 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005245 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005246 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005247 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005248 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005249 }
Chris Lattner84f67882005-01-20 18:52:28 +00005250 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005251 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005252
5253 case ISD::ADDC:
5254 case ISD::SUBC: {
5255 // Expand the subcomponents.
5256 SDOperand LHSL, LHSH, RHSL, RHSH;
5257 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5258 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5259 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5260 SDOperand LoOps[2] = { LHSL, RHSL };
5261 SDOperand HiOps[3] = { LHSH, RHSH };
5262
5263 if (Node->getOpcode() == ISD::ADDC) {
5264 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5265 HiOps[2] = Lo.getValue(1);
5266 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5267 } else {
5268 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5269 HiOps[2] = Lo.getValue(1);
5270 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5271 }
5272 // Remember that we legalized the flag.
5273 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5274 break;
5275 }
5276 case ISD::ADDE:
5277 case ISD::SUBE: {
5278 // Expand the subcomponents.
5279 SDOperand LHSL, LHSH, RHSL, RHSH;
5280 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5281 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5282 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5283 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5284 SDOperand HiOps[3] = { LHSH, RHSH };
5285
5286 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5287 HiOps[2] = Lo.getValue(1);
5288 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5289
5290 // Remember that we legalized the flag.
5291 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5292 break;
5293 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005294 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005295 // If the target wants to custom expand this, let them.
5296 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005297 SDOperand New = TLI.LowerOperation(Op, DAG);
5298 if (New.Val) {
5299 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005300 break;
5301 }
5302 }
5303
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005304 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5305 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005306 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005307 SDOperand LL, LH, RL, RH;
5308 ExpandOp(Node->getOperand(0), LL, LH);
5309 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005310 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005311 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005312 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5313 // extended the sign bit of the low half through the upper half, and if so
5314 // emit a MULHS instead of the alternate sequence that is valid for any
5315 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005316 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005317 // is RH an extension of the sign bit of RL?
5318 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5319 RH.getOperand(1).getOpcode() == ISD::Constant &&
5320 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5321 // is LH an extension of the sign bit of LL?
5322 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5323 LH.getOperand(1).getOpcode() == ISD::Constant &&
5324 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005325 // Low part:
5326 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5327 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005328 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005329 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005330 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005331 // Low part:
5332 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5333
5334 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005335 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5336 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5337 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5338 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5339 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005340 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005341 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005342 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005343
Evan Cheng56966222007-01-12 02:11:51 +00005344 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5345 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005346 break;
5347 }
Evan Cheng56966222007-01-12 02:11:51 +00005348 case ISD::SDIV:
5349 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5350 break;
5351 case ISD::UDIV:
5352 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5353 break;
5354 case ISD::SREM:
5355 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5356 break;
5357 case ISD::UREM:
5358 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5359 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005360
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005361 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005362 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5363 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5364 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005365 break;
5366 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005367 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5368 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5369 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005370 break;
5371 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005372 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5373 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5374 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005375 break;
5376 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005377 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5378 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5379 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005380 break;
5381 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005382 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005383 break;
5384 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005385 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005386 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005387 case ISD::FSQRT:
5388 case ISD::FSIN:
5389 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005390 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005391 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005392 case ISD::FSQRT:
5393 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5394 break;
5395 case ISD::FSIN:
5396 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5397 break;
5398 case ISD::FCOS:
5399 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5400 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005401 default: assert(0 && "Unreachable!");
5402 }
Evan Cheng56966222007-01-12 02:11:51 +00005403 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005404 break;
5405 }
Evan Cheng966bf242006-12-16 00:52:40 +00005406 case ISD::FABS: {
5407 SDOperand Mask = (VT == MVT::f64)
5408 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5409 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5410 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5411 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5412 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5413 if (getTypeAction(NVT) == Expand)
5414 ExpandOp(Lo, Lo, Hi);
5415 break;
5416 }
5417 case ISD::FNEG: {
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::XOR, NVT, Lo, Mask);
5424 if (getTypeAction(NVT) == Expand)
5425 ExpandOp(Lo, Lo, Hi);
5426 break;
5427 }
Evan Cheng912095b2007-01-04 21:56:39 +00005428 case ISD::FCOPYSIGN: {
5429 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5430 if (getTypeAction(NVT) == Expand)
5431 ExpandOp(Lo, Lo, Hi);
5432 break;
5433 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005434 case ISD::SINT_TO_FP:
5435 case ISD::UINT_TO_FP: {
5436 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5437 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005438 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005439 if (Node->getOperand(0).getValueType() == MVT::i64) {
5440 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005441 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005442 else
Evan Cheng56966222007-01-12 02:11:51 +00005443 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005444 } else {
5445 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005446 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005447 else
Evan Cheng56966222007-01-12 02:11:51 +00005448 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005449 }
5450
5451 // Promote the operand if needed.
5452 if (getTypeAction(SrcVT) == Promote) {
5453 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5454 Tmp = isSigned
5455 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5456 DAG.getValueType(SrcVT))
5457 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5458 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5459 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005460
5461 const char *LibCall = TLI.getLibcallName(LC);
5462 if (LibCall)
5463 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5464 else {
5465 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5466 Node->getOperand(0));
5467 if (getTypeAction(Lo.getValueType()) == Expand)
5468 ExpandOp(Lo, Lo, Hi);
5469 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005470 break;
5471 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005472 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005473
Chris Lattner83397362005-12-21 18:02:52 +00005474 // Make sure the resultant values have been legalized themselves, unless this
5475 // is a type that requires multi-step expansion.
5476 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5477 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005478 if (Hi.Val)
5479 // Don't legalize the high part if it is expanded to a single node.
5480 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005481 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005482
5483 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005484 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005485 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005486}
5487
Dan Gohman7f321562007-06-25 16:23:39 +00005488/// SplitVectorOp - Given an operand of vector type, break it down into
5489/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005490void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5491 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005492 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005493 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005494 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005495 assert(NumElements > 1 && "Cannot split a single element vector!");
5496 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005497 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5498 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005499
5500 // See if we already split it.
5501 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5502 = SplitNodes.find(Op);
5503 if (I != SplitNodes.end()) {
5504 Lo = I->second.first;
5505 Hi = I->second.second;
5506 return;
5507 }
5508
5509 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005510 default:
5511#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005512 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005513#endif
5514 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005515 case ISD::BUILD_PAIR:
5516 Lo = Node->getOperand(0);
5517 Hi = Node->getOperand(1);
5518 break;
5519 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005520 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5521 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005522 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005523
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005524 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005525 Node->op_end());
5526 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005527 break;
5528 }
Dan Gohman7f321562007-06-25 16:23:39 +00005529 case ISD::CONCAT_VECTORS: {
5530 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5531 if (NewNumSubvectors == 1) {
5532 Lo = Node->getOperand(0);
5533 Hi = Node->getOperand(1);
5534 } else {
5535 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5536 Node->op_begin()+NewNumSubvectors);
5537 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005538
Dan Gohman7f321562007-06-25 16:23:39 +00005539 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5540 Node->op_end());
5541 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5542 }
Dan Gohman65956352007-06-13 15:12:02 +00005543 break;
5544 }
Dan Gohman7f321562007-06-25 16:23:39 +00005545 case ISD::ADD:
5546 case ISD::SUB:
5547 case ISD::MUL:
5548 case ISD::FADD:
5549 case ISD::FSUB:
5550 case ISD::FMUL:
5551 case ISD::SDIV:
5552 case ISD::UDIV:
5553 case ISD::FDIV:
5554 case ISD::AND:
5555 case ISD::OR:
5556 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005557 SDOperand LL, LH, RL, RH;
5558 SplitVectorOp(Node->getOperand(0), LL, LH);
5559 SplitVectorOp(Node->getOperand(1), RL, RH);
5560
Dan Gohman7f321562007-06-25 16:23:39 +00005561 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5562 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005563 break;
5564 }
Dan Gohman7f321562007-06-25 16:23:39 +00005565 case ISD::LOAD: {
5566 LoadSDNode *LD = cast<LoadSDNode>(Node);
5567 SDOperand Ch = LD->getChain();
5568 SDOperand Ptr = LD->getBasePtr();
5569 const Value *SV = LD->getSrcValue();
5570 int SVOffset = LD->getSrcValueOffset();
5571 unsigned Alignment = LD->getAlignment();
5572 bool isVolatile = LD->isVolatile();
5573
5574 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5575 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005576 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5577 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005578 SVOffset += IncrementSize;
5579 if (Alignment > IncrementSize)
5580 Alignment = IncrementSize;
5581 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005582
5583 // Build a factor node to remember that this load is independent of the
5584 // other one.
5585 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5586 Hi.getValue(1));
5587
5588 // Remember that we legalized the chain.
5589 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005590 break;
5591 }
Dan Gohman7f321562007-06-25 16:23:39 +00005592 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005593 // We know the result is a vector. The input may be either a vector or a
5594 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005595 SDOperand InOp = Node->getOperand(0);
5596 if (!MVT::isVector(InOp.getValueType()) ||
5597 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5598 // The input is a scalar or single-element vector.
5599 // Lower to a store/load so that it can be split.
5600 // FIXME: this could be improved probably.
5601 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005602
Evan Cheng786225a2006-10-05 23:01:46 +00005603 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005604 InOp, Ptr, NULL, 0);
5605 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005606 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005607 // Split the vector and convert each of the pieces now.
5608 SplitVectorOp(InOp, Lo, Hi);
5609 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5610 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5611 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005612 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005613 }
5614
5615 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005616 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005617 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005618 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005619}
5620
5621
Dan Gohman89b20c02007-06-27 14:06:22 +00005622/// ScalarizeVectorOp - Given an operand of single-element vector type
5623/// (e.g. v1f32), convert it into the equivalent operation that returns a
5624/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005625SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5626 assert(MVT::isVector(Op.getValueType()) &&
5627 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005628 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005629 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5630 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005631
Dan Gohman7f321562007-06-25 16:23:39 +00005632 // See if we already scalarized it.
5633 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5634 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005635
5636 SDOperand Result;
5637 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005638 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005639#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005640 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005641#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005642 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5643 case ISD::ADD:
5644 case ISD::FADD:
5645 case ISD::SUB:
5646 case ISD::FSUB:
5647 case ISD::MUL:
5648 case ISD::FMUL:
5649 case ISD::SDIV:
5650 case ISD::UDIV:
5651 case ISD::FDIV:
5652 case ISD::SREM:
5653 case ISD::UREM:
5654 case ISD::FREM:
5655 case ISD::AND:
5656 case ISD::OR:
5657 case ISD::XOR:
5658 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005659 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005660 ScalarizeVectorOp(Node->getOperand(0)),
5661 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005662 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005663 case ISD::FNEG:
5664 case ISD::FABS:
5665 case ISD::FSQRT:
5666 case ISD::FSIN:
5667 case ISD::FCOS:
5668 Result = DAG.getNode(Node->getOpcode(),
5669 NewVT,
5670 ScalarizeVectorOp(Node->getOperand(0)));
5671 break;
5672 case ISD::LOAD: {
5673 LoadSDNode *LD = cast<LoadSDNode>(Node);
5674 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5675 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005676
Dan Gohman7f321562007-06-25 16:23:39 +00005677 const Value *SV = LD->getSrcValue();
5678 int SVOffset = LD->getSrcValueOffset();
5679 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5680 LD->isVolatile(), LD->getAlignment());
5681
Chris Lattnerc7029802006-03-18 01:44:44 +00005682 // Remember that we legalized the chain.
5683 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5684 break;
5685 }
Dan Gohman7f321562007-06-25 16:23:39 +00005686 case ISD::BUILD_VECTOR:
5687 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005688 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005689 case ISD::INSERT_VECTOR_ELT:
5690 // Returning the inserted scalar element.
5691 Result = Node->getOperand(1);
5692 break;
5693 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005694 assert(Node->getOperand(0).getValueType() == NewVT &&
5695 "Concat of non-legal vectors not yet supported!");
5696 Result = Node->getOperand(0);
5697 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005698 case ISD::VECTOR_SHUFFLE: {
5699 // Figure out if the scalar is the LHS or RHS and return it.
5700 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5701 if (cast<ConstantSDNode>(EltNum)->getValue())
5702 Result = ScalarizeVectorOp(Node->getOperand(1));
5703 else
5704 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005705 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005706 }
5707 case ISD::EXTRACT_SUBVECTOR:
5708 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005709 assert(Result.getValueType() == NewVT);
5710 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005711 case ISD::BIT_CONVERT:
5712 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005713 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005714 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005715 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005716 ScalarizeVectorOp(Op.getOperand(1)),
5717 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005718 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005719 }
5720
5721 if (TLI.isTypeLegal(NewVT))
5722 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005723 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5724 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005725 return Result;
5726}
5727
Chris Lattner3e928bb2005-01-07 07:47:09 +00005728
5729// SelectionDAG::Legalize - This is the entry point for the file.
5730//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005731void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005732 if (ViewLegalizeDAGs) viewGraph();
5733
Chris Lattner3e928bb2005-01-07 07:47:09 +00005734 /// run - This is the main entry point to this class.
5735 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005736 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005737}
5738