blob: 3fe984ebcc636ca8dc426b4308b994c5aefc9276 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Evan Cheng1d2e9952006-03-24 01:17:21 +000024#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner462505f2006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattnerdc750592005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000060 };
Chris Lattner462505f2006-02-13 09:18:02 +000061
Chris Lattnerdc750592005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000066
Chris Lattnerdc750592005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner1f2c9d82005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattner32206f52006-03-18 01:44:44 +000077 /// ExpandedNodes - For nodes that need to be expanded this map indicates
78 /// which which operands are the expanded version of the input. This allows
79 /// us to avoid expanding the same node more than once.
Chris Lattnerdc750592005-01-07 07:47:09 +000080 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
81
Chris Lattner32206f52006-03-18 01:44:44 +000082 /// SplitNodes - For vector nodes that need to be split, this map indicates
83 /// which which operands are the split version of the input. This allows us
84 /// to avoid splitting the same node more than once.
85 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
86
87 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
88 /// concrete packed types, this contains the mapping of ones we have already
89 /// processed to the result.
90 std::map<SDOperand, SDOperand> PackedNodes;
91
Chris Lattnerea4ca942005-01-07 22:28:47 +000092 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +000093 LegalizedNodes.insert(std::make_pair(From, To));
94 // If someone requests legalization of the new node, return itself.
95 if (From != To)
96 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +000097 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000098 void AddPromotedOperand(SDOperand From, SDOperand To) {
99 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
100 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000101 // If someone requests legalization of the new node, return itself.
102 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000103 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000104
Chris Lattnerdc750592005-01-07 07:47:09 +0000105public:
106
Chris Lattner4add7e32005-01-23 04:42:50 +0000107 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000108
Chris Lattnerdc750592005-01-07 07:47:09 +0000109 /// getTypeAction - Return how we should legalize values of this type, either
110 /// it is already legal or we need to expand it into multiple registers of
111 /// smaller integer type, or we need to promote it to a larger type.
112 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000114 }
115
116 /// isTypeLegal - Return true if this type is legal on this target.
117 ///
118 bool isTypeLegal(MVT::ValueType VT) const {
119 return getTypeAction(VT) == Legal;
120 }
121
Chris Lattnerdc750592005-01-07 07:47:09 +0000122 void LegalizeDAG();
123
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000124private:
Chris Lattner32206f52006-03-18 01:44:44 +0000125 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
126 /// appropriate for its type.
127 void HandleOp(SDOperand Op);
128
129 /// LegalizeOp - We know that the specified value has a legal type.
130 /// Recursively ensure that the operands have legal types, then return the
131 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000132 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000133
134 /// PromoteOp - Given an operation that produces a value in an invalid type,
135 /// promote it to compute the value into a larger type. The produced value
136 /// will have the correct bits for the low portion of the register, but no
137 /// guarantee is made about the top bits: it may be zero, sign-extended, or
138 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000139 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000140
Chris Lattner32206f52006-03-18 01:44:44 +0000141 /// ExpandOp - Expand the specified SDOperand into its two component pieces
142 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
143 /// the LegalizeNodes map is filled in for any results that are not expanded,
144 /// the ExpandedNodes map is filled in for any results that are expanded, and
145 /// the Lo/Hi values are returned. This applies to integer types and Vector
146 /// types.
147 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
148
149 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
150 /// two smaller values of MVT::Vector type.
151 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
152
153 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
154 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
155 /// this is called, we know that PackedVT is the right type for the result and
156 /// we know that this type is legal for the target.
157 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
158
Chris Lattner462505f2006-02-13 09:18:02 +0000159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
160
Nate Begeman7e7f4392006-02-01 07:19:44 +0000161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
162
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000163 SDOperand CreateStackTemporary(MVT::ValueType VT);
164
Chris Lattneraac464e2005-01-21 06:05:23 +0000165 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
166 SDOperand &Hi);
167 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
168 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000169
Chris Lattner36e663d2005-12-23 00:16:34 +0000170 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000171 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand LegalOp,
174 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000175 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
176 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000177 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
178 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000179
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000180 SDOperand ExpandBSWAP(SDOperand Op);
181 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000182 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
183 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000184 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
185 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000186
Chris Lattnerdc750592005-01-07 07:47:09 +0000187 SDOperand getIntPtrConstant(uint64_t Val) {
188 return DAG.getConstant(Val, TLI.getPointerTy());
189 }
190};
191}
192
Chris Lattner32206f52006-03-18 01:44:44 +0000193/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
194/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000195static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000196 switch (VecOp) {
197 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-03-03 07:01:07 +0000198 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
199 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
200 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
201 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
202 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
203 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
204 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
205 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begemanb2e089c2005-11-19 00:36:38 +0000206 }
207}
Chris Lattnerdc750592005-01-07 07:47:09 +0000208
Chris Lattner4add7e32005-01-23 04:42:50 +0000209SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
210 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
211 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000212 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000213 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000214}
215
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000216/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
217/// not been visited yet and if all of its operands have already been visited.
218static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
219 std::map<SDNode*, unsigned> &Visited) {
220 if (++Visited[N] != N->getNumOperands())
221 return; // Haven't visited all operands yet
222
223 Order.push_back(N);
224
225 if (N->hasOneUse()) { // Tail recurse in common case.
226 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
227 return;
228 }
229
230 // Now that we have N in, add anything that uses it if all of their operands
231 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000232 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
233 ComputeTopDownOrdering(*UI, Order, Visited);
234}
235
Chris Lattner44fe26f2005-07-29 00:11:56 +0000236
Chris Lattnerdc750592005-01-07 07:47:09 +0000237void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000238 LastCALLSEQ_END = DAG.getEntryNode();
239 IsLegalizingCall = false;
240
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000241 // The legalize process is inherently a bottom-up recursive process (users
242 // legalize their uses before themselves). Given infinite stack space, we
243 // could just start legalizing on the root and traverse the whole graph. In
244 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000245 // blocks. To avoid this problem, compute an ordering of the nodes where each
246 // node is only legalized after all of its operands are legalized.
247 std::map<SDNode*, unsigned> Visited;
248 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000249
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000250 // Compute ordering from all of the leaves in the graphs, those (like the
251 // entry node) that have no operands.
252 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
253 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000254 if (I->getNumOperands() == 0) {
255 Visited[I] = 0 - 1U;
256 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000257 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000258 }
259
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000260 assert(Order.size() == Visited.size() &&
261 Order.size() ==
262 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000263 "Error: DAG is cyclic!");
264 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000265
Chris Lattner32206f52006-03-18 01:44:44 +0000266 for (unsigned i = 0, e = Order.size(); i != e; ++i)
267 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000268
269 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000270 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000271 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
272 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000273
274 ExpandedNodes.clear();
275 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000276 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000277 SplitNodes.clear();
278 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000279
280 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000281 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000282}
283
Chris Lattner462505f2006-02-13 09:18:02 +0000284
285/// FindCallEndFromCallStart - Given a chained node that is part of a call
286/// sequence, find the CALLSEQ_END node that terminates the call sequence.
287static SDNode *FindCallEndFromCallStart(SDNode *Node) {
288 if (Node->getOpcode() == ISD::CALLSEQ_END)
289 return Node;
290 if (Node->use_empty())
291 return 0; // No CallSeqEnd
292
293 // The chain is usually at the end.
294 SDOperand TheChain(Node, Node->getNumValues()-1);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Sometimes it's at the beginning.
297 TheChain = SDOperand(Node, 0);
298 if (TheChain.getValueType() != MVT::Other) {
299 // Otherwise, hunt for it.
300 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
301 if (Node->getValueType(i) == MVT::Other) {
302 TheChain = SDOperand(Node, i);
303 break;
304 }
305
306 // Otherwise, we walked into a node without a chain.
307 if (TheChain.getValueType() != MVT::Other)
308 return 0;
309 }
310 }
311
312 for (SDNode::use_iterator UI = Node->use_begin(),
313 E = Node->use_end(); UI != E; ++UI) {
314
315 // Make sure to only follow users of our token chain.
316 SDNode *User = *UI;
317 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
318 if (User->getOperand(i) == TheChain)
319 if (SDNode *Result = FindCallEndFromCallStart(User))
320 return Result;
321 }
322 return 0;
323}
324
325/// FindCallStartFromCallEnd - Given a chained node that is part of a call
326/// sequence, find the CALLSEQ_START node that initiates the call sequence.
327static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
328 assert(Node && "Didn't find callseq_start for a call??");
329 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
330
331 assert(Node->getOperand(0).getValueType() == MVT::Other &&
332 "Node doesn't have a token chain argument!");
333 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
334}
335
336/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
337/// see if any uses can reach Dest. If no dest operands can get to dest,
338/// legalize them, legalize ourself, and return false, otherwise, return true.
339bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
340 SDNode *Dest) {
341 if (N == Dest) return true; // N certainly leads to Dest :)
342
343 // If the first result of this node has been already legalized, then it cannot
344 // reach N.
345 switch (getTypeAction(N->getValueType(0))) {
346 case Legal:
347 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Promote:
350 if (PromotedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 case Expand:
353 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
354 break;
355 }
356
357 // Okay, this node has not already been legalized. Check and legalize all
358 // operands. If none lead to Dest, then we can legalize this node.
359 bool OperandsLeadToDest = false;
360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
362 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
363
364 if (OperandsLeadToDest) return true;
365
366 // Okay, this node looks safe, legalize it and return false.
367 switch (getTypeAction(N->getValueType(0))) {
368 case Legal:
369 LegalizeOp(SDOperand(N, 0));
370 break;
371 case Promote:
372 PromoteOp(SDOperand(N, 0));
373 break;
374 case Expand: {
375 SDOperand X, Y;
376 ExpandOp(SDOperand(N, 0), X, Y);
377 break;
378 }
379 }
380 return false;
381}
382
Chris Lattner32206f52006-03-18 01:44:44 +0000383/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
384/// appropriate for its type.
385void SelectionDAGLegalize::HandleOp(SDOperand Op) {
386 switch (getTypeAction(Op.getValueType())) {
387 default: assert(0 && "Bad type action!");
388 case Legal: LegalizeOp(Op); break;
389 case Promote: PromoteOp(Op); break;
390 case Expand:
391 if (Op.getValueType() != MVT::Vector) {
392 SDOperand X, Y;
393 ExpandOp(Op, X, Y);
394 } else {
395 SDNode *N = Op.Val;
396 unsigned NumOps = N->getNumOperands();
397 unsigned NumElements =
398 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
399 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
400 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
401 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
402 // In the common case, this is a legal vector type, convert it to the
403 // packed operation and type now.
404 PackVectorOp(Op, PackedVT);
405 } else if (NumElements == 1) {
406 // Otherwise, if this is a single element vector, convert it to a
407 // scalar operation.
408 PackVectorOp(Op, EVT);
409 } else {
410 // Otherwise, this is a multiple element vector that isn't supported.
411 // Split it in half and legalize both parts.
412 SDOperand X, Y;
Chris Lattner93640542006-03-19 00:07:49 +0000413 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000414 }
415 }
416 break;
417 }
418}
Chris Lattner462505f2006-02-13 09:18:02 +0000419
420
Chris Lattner32206f52006-03-18 01:44:44 +0000421/// LegalizeOp - We know that the specified value has a legal type.
422/// Recursively ensure that the operands have legal types, then return the
423/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000424SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000425 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000426 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000427 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000428
Chris Lattnerdc750592005-01-07 07:47:09 +0000429 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000430 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000431 if (Node->getNumValues() > 1) {
432 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000433 if (getTypeAction(Node->getValueType(i)) != Legal) {
434 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000435 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000436 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000437 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000438 }
439 }
440
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000441 // Note that LegalizeOp may be reentered even from single-use nodes, which
442 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000443 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
444 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000445
Nate Begemane5b86d72005-08-10 20:51:12 +0000446 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000447 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000448 bool isCustom = false;
449
Chris Lattnerdc750592005-01-07 07:47:09 +0000450 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000451 case ISD::FrameIndex:
452 case ISD::EntryToken:
453 case ISD::Register:
454 case ISD::BasicBlock:
455 case ISD::TargetFrameIndex:
456 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000457 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000458 case ISD::TargetConstantPool:
459 case ISD::TargetGlobalAddress:
460 case ISD::TargetExternalSymbol:
461 case ISD::VALUETYPE:
462 case ISD::SRCVALUE:
463 case ISD::STRING:
464 case ISD::CONDCODE:
465 // Primitives must all be legal.
466 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
467 "This must be legal!");
468 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000469 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000470 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
471 // If this is a target node, legalize it by legalizing the operands then
472 // passing it through.
473 std::vector<SDOperand> Ops;
474 bool Changed = false;
475 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
476 Ops.push_back(LegalizeOp(Node->getOperand(i)));
477 Changed = Changed || Node->getOperand(i) != Ops.back();
478 }
479 if (Changed)
480 if (Node->getNumValues() == 1)
481 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
482 else {
483 std::vector<MVT::ValueType> VTs(Node->value_begin(),
484 Node->value_end());
485 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
486 }
487
488 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
489 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
490 return Result.getValue(Op.ResNo);
491 }
492 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000493 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
494 assert(0 && "Do not know how to legalize this operator!");
495 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000496 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000497 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000498 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000499 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
500 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000501 case TargetLowering::Custom:
502 Tmp1 = TLI.LowerOperation(Op, DAG);
503 if (Tmp1.Val) Result = Tmp1;
504 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000505 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000506 break;
507 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000508 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000509 case ISD::AssertSext:
510 case ISD::AssertZext:
511 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000513 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000514 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000515 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000516 Result = Node->getOperand(Op.ResNo);
517 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000518 case ISD::CopyFromReg:
519 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000520 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000521 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000523 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000524 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000525 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000526 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
528 } else {
529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
530 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000531 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
532 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000533 // Since CopyFromReg produces two values, make sure to remember that we
534 // legalized both of them.
535 AddLegalizedOperand(Op.getValue(0), Result);
536 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
537 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000538 case ISD::UNDEF: {
539 MVT::ValueType VT = Op.getValueType();
540 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000541 default: assert(0 && "This action is not supported yet!");
542 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000543 if (MVT::isInteger(VT))
544 Result = DAG.getConstant(0, VT);
545 else if (MVT::isFloatingPoint(VT))
546 Result = DAG.getConstantFP(0, VT);
547 else
548 assert(0 && "Unknown value type!");
549 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000550 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000551 break;
552 }
553 break;
554 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000555
Chris Lattnere55d1712006-03-28 00:40:33 +0000556 case ISD::INTRINSIC_W_CHAIN:
557 case ISD::INTRINSIC_WO_CHAIN:
558 case ISD::INTRINSIC_VOID: {
Chris Lattnera4f68052006-03-24 02:26:29 +0000559 std::vector<SDOperand> Ops;
560 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
561 Ops.push_back(LegalizeOp(Node->getOperand(i)));
562 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner30ee7252006-03-26 09:12:51 +0000563
564 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000565 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000566 TargetLowering::Custom) {
567 Tmp3 = TLI.LowerOperation(Result, DAG);
568 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000569 }
570
571 if (Result.Val->getNumValues() == 1) break;
572
573 // Must have return value and chain result.
574 assert(Result.Val->getNumValues() == 2 &&
575 "Cannot return more than two values!");
576
577 // Since loads produce two values, make sure to remember that we
578 // legalized both of them.
579 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
580 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
581 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000582 }
Chris Lattner435b4022005-11-29 06:21:05 +0000583
584 case ISD::LOCATION:
585 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
587
588 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
589 case TargetLowering::Promote:
590 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000591 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000592 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000593 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
594 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
595
596 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
597 const std::string &FName =
598 cast<StringSDNode>(Node->getOperand(3))->getValue();
599 const std::string &DirName =
600 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000601 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000602
Jim Laskey9e296be2005-12-21 20:51:37 +0000603 std::vector<SDOperand> Ops;
604 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000605 SDOperand LineOp = Node->getOperand(1);
606 SDOperand ColOp = Node->getOperand(2);
607
608 if (useDEBUG_LOC) {
609 Ops.push_back(LineOp); // line #
610 Ops.push_back(ColOp); // col #
611 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
612 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
613 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000614 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
615 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000616 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
617 Ops.push_back(DAG.getConstant(ID, MVT::i32));
618 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
619 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000620 } else {
621 Result = Tmp1; // chain
622 }
Chris Lattner435b4022005-11-29 06:21:05 +0000623 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000624 }
Chris Lattner435b4022005-11-29 06:21:05 +0000625 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000626 if (Tmp1 != Node->getOperand(0) ||
627 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000628 std::vector<SDOperand> Ops;
629 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000630 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
631 Ops.push_back(Node->getOperand(1)); // line # must be legal.
632 Ops.push_back(Node->getOperand(2)); // col # must be legal.
633 } else {
634 // Otherwise promote them.
635 Ops.push_back(PromoteOp(Node->getOperand(1)));
636 Ops.push_back(PromoteOp(Node->getOperand(2)));
637 }
Chris Lattner435b4022005-11-29 06:21:05 +0000638 Ops.push_back(Node->getOperand(3)); // filename must be legal.
639 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000640 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000641 }
642 break;
643 }
644 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000645
646 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000647 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000648 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000649 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000650 case TargetLowering::Legal:
651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
652 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
653 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
654 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000655 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000656 break;
657 }
658 break;
659
660 case ISD::DEBUG_LABEL:
661 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
662 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000663 default: assert(0 && "This action is not supported yet!");
664 case TargetLowering::Legal:
665 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000667 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000668 break;
669 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000670 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000671
Chris Lattnerdc750592005-01-07 07:47:09 +0000672 case ISD::Constant:
673 // We know we don't need to expand constants here, constants only have one
674 // value and we check that it is fine above.
675
676 // FIXME: Maybe we should handle things like targets that don't support full
677 // 32-bit immediates?
678 break;
679 case ISD::ConstantFP: {
680 // Spill FP immediates to the constant pool if the target cannot directly
681 // codegen them. Targets often have some immediate values that can be
682 // efficiently generated into an FP register without a load. We explicitly
683 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000684 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
685
686 // Check to see if this FP immediate is already legal.
687 bool isLegal = false;
688 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
689 E = TLI.legal_fpimm_end(); I != E; ++I)
690 if (CFP->isExactlyValue(*I)) {
691 isLegal = true;
692 break;
693 }
694
Chris Lattner758b0ac2006-01-29 06:26:56 +0000695 // If this is a legal constant, turn it into a TargetConstantFP node.
696 if (isLegal) {
697 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
698 break;
699 }
700
701 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
702 default: assert(0 && "This action is not supported yet!");
703 case TargetLowering::Custom:
704 Tmp3 = TLI.LowerOperation(Result, DAG);
705 if (Tmp3.Val) {
706 Result = Tmp3;
707 break;
708 }
709 // FALLTHROUGH
710 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000711 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000712 bool Extend = false;
713
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000714 // If a FP immediate is precise when represented as a float and if the
715 // target can do an extending load from float to double, we put it into
716 // the constant pool as a float, even if it's is statically typed as a
717 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000718 MVT::ValueType VT = CFP->getValueType(0);
719 bool isDouble = VT == MVT::f64;
720 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
721 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000722 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
723 // Only do this if the target has a native EXTLOAD instruction from
724 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000725 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000726 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
727 VT = MVT::f32;
728 Extend = true;
729 }
Misha Brukman835702a2005-04-21 22:36:52 +0000730
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000731 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000732 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000733 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
734 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000735 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000736 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
737 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000738 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000739 }
740 break;
741 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000742 case ISD::TokenFactor:
743 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000744 Tmp1 = LegalizeOp(Node->getOperand(0));
745 Tmp2 = LegalizeOp(Node->getOperand(1));
746 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
747 } else if (Node->getNumOperands() == 3) {
748 Tmp1 = LegalizeOp(Node->getOperand(0));
749 Tmp2 = LegalizeOp(Node->getOperand(1));
750 Tmp3 = LegalizeOp(Node->getOperand(2));
751 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000752 } else {
753 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000754 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000755 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
756 Ops.push_back(LegalizeOp(Node->getOperand(i)));
757 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000758 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000759 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000760
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000761 case ISD::BUILD_VECTOR:
762 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000763 default: assert(0 && "This action is not supported yet!");
764 case TargetLowering::Custom:
765 Tmp3 = TLI.LowerOperation(Result, DAG);
766 if (Tmp3.Val) {
767 Result = Tmp3;
768 break;
769 }
770 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000771 case TargetLowering::Expand:
772 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000773 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000774 }
Chris Lattner29b23012006-03-19 01:17:20 +0000775 break;
776 case ISD::INSERT_VECTOR_ELT:
777 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
778 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
779 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
780 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
781
782 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
783 Node->getValueType(0))) {
784 default: assert(0 && "This action is not supported yet!");
785 case TargetLowering::Legal:
786 break;
787 case TargetLowering::Custom:
788 Tmp3 = TLI.LowerOperation(Result, DAG);
789 if (Tmp3.Val) {
790 Result = Tmp3;
791 break;
792 }
793 // FALLTHROUGH
794 case TargetLowering::Expand: {
795 // If the target doesn't support this, we have to spill the input vector
796 // to a temporary stack slot, update the element, then reload it. This is
797 // badness. We could also load the value into a vector register (either
798 // with a "move to register" or "extload into register" instruction, then
799 // permute it into place, if the idx is a constant and if the idx is
800 // supported by the target.
Evan Cheng168e45b2006-03-31 01:27:51 +0000801 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
802 // Store the vector.
803 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
804 Tmp1, StackPtr, DAG.getSrcValue(NULL));
805
806 // Truncate or zero extend offset to target pointer type.
807 MVT::ValueType IntPtr = TLI.getPointerTy();
808 if (Tmp3.getValueType() > IntPtr)
809 Tmp3 = DAG.getNode(ISD::TRUNCATE, IntPtr, Tmp3);
810 else
811 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Tmp3);
812
813 // Add the offset to the index.
814 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
815 Tmp3 = DAG.getNode(ISD::MUL, Tmp3.getValueType(), Tmp3,
816 DAG.getConstant(EltSize, Tmp3.getValueType()));
817 SDOperand StackPtr2 =
818 DAG.getNode(ISD::ADD, Tmp3.getValueType(), Tmp3, StackPtr);
819 // Store the scalar value.
820 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
821 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
822 // Load the updated vector.
823 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
824 DAG.getSrcValue(NULL));
Chris Lattner29b23012006-03-19 01:17:20 +0000825 break;
826 }
827 }
828 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000829 case ISD::SCALAR_TO_VECTOR:
830 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
831 Result = DAG.UpdateNodeOperands(Result, Tmp1);
832 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
833 Node->getValueType(0))) {
834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Legal:
836 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000837 case TargetLowering::Custom:
838 Tmp3 = TLI.LowerOperation(Result, DAG);
839 if (Tmp3.Val) {
840 Result = Tmp3;
841 break;
842 }
843 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000844 case TargetLowering::Expand: {
845 // If the target doesn't support this, store the value to a temporary
846 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000847 // TODO: If a target doesn't support this, create a stack slot for the
848 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000849 SDOperand StackPtr =
850 CreateStackTemporary(Node->getOperand(0).getValueType());
851 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
852 Node->getOperand(0), StackPtr,
853 DAG.getSrcValue(NULL));
854 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
855 DAG.getSrcValue(NULL),
856 Node->getOperand(0).getValueType());
857 break;
858 }
859 }
860 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000861 case ISD::VECTOR_SHUFFLE:
862 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
863 "vector shuffle should not be created if not legal!");
864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
867
868 // Allow targets to custom lower the SHUFFLEs they support.
869 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
870 == TargetLowering::Custom) {
871 Tmp1 = TLI.LowerOperation(Result, DAG);
872 if (Tmp1.Val) Result = Tmp1;
873 }
874 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000875
876 case ISD::EXTRACT_VECTOR_ELT:
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 Tmp2 = LegalizeOp(Node->getOperand(1));
879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000880
881 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
882 Tmp1.getValueType())) {
883 default: assert(0 && "This action is not supported yet!");
884 case TargetLowering::Legal:
885 break;
886 case TargetLowering::Custom:
887 Tmp3 = TLI.LowerOperation(Result, DAG);
888 if (Tmp3.Val) {
889 Result = Tmp3;
890 break;
891 }
892 // FALLTHROUGH
893 case TargetLowering::Expand: {
894 // If the target doesn't support this, store the value to a temporary
895 // stack slot, then LOAD the scalar element back out.
896 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
897 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
898 Tmp1, StackPtr, DAG.getSrcValue(NULL));
899
900 // Add the offset to the index.
901 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
902 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
903 DAG.getConstant(EltSize, Tmp2.getValueType()));
904 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
905
906 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
907 DAG.getSrcValue(NULL));
908 break;
909 }
910 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000911 break;
912
Chris Lattner5be43522006-03-22 00:12:37 +0000913 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000914 // We know that operand #0 is the Vec vector. If the index is a constant
915 // or if the invec is a supported hardware type, we can use it. Otherwise,
916 // lower to a store then an indexed load.
917 Tmp1 = Node->getOperand(0);
918 Tmp2 = LegalizeOp(Node->getOperand(1));
919
920 SDNode *InVal = Tmp1.Val;
921 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
922 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
923
924 // Figure out if there is a Packed type corresponding to this Vector
925 // type. If so, convert to the packed type.
926 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
927 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
928 // Turn this into a packed extract_vector_elt operation.
929 Tmp1 = PackVectorOp(Tmp1, TVT);
930 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
931 Tmp1, Tmp2);
932 break;
933 } else if (NumElems == 1) {
934 // This must be an access of the only element.
935 Result = PackVectorOp(Tmp1, EVT);
936 break;
937 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
938 SDOperand Lo, Hi;
939 SplitVectorOp(Tmp1, Lo, Hi);
940 if (CIdx->getValue() < NumElems/2) {
941 Tmp1 = Lo;
942 } else {
943 Tmp1 = Hi;
944 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
945 Tmp2.getValueType());
946 }
947
948 // It's now an extract from the appropriate high or low part.
949 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
950 } else {
Chris Lattner5be43522006-03-22 00:12:37 +0000951 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000952 assert(0 && "unimp!");
953 }
954 break;
Chris Lattner5be43522006-03-22 00:12:37 +0000955 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000956
Chris Lattner462505f2006-02-13 09:18:02 +0000957 case ISD::CALLSEQ_START: {
958 SDNode *CallEnd = FindCallEndFromCallStart(Node);
959
960 // Recursively Legalize all of the inputs of the call end that do not lead
961 // to this call start. This ensures that any libcalls that need be inserted
962 // are inserted *before* the CALLSEQ_START.
963 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
964 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
965
966 // Now that we legalized all of the inputs (which may have inserted
967 // libcalls) create the new CALLSEQ_START node.
968 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
969
970 // Merge in the last call, to ensure that this call start after the last
971 // call ended.
972 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
973 Tmp1 = LegalizeOp(Tmp1);
974
975 // Do not try to legalize the target-specific arguments (#1+).
976 if (Tmp1 != Node->getOperand(0)) {
977 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
978 Ops[0] = Tmp1;
979 Result = DAG.UpdateNodeOperands(Result, Ops);
980 }
981
982 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000983 AddLegalizedOperand(Op.getValue(0), Result);
984 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
985 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
986
Chris Lattner462505f2006-02-13 09:18:02 +0000987 // Now that the callseq_start and all of the non-call nodes above this call
988 // sequence have been legalized, legalize the call itself. During this
989 // process, no libcalls can/will be inserted, guaranteeing that no calls
990 // can overlap.
991 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
992 SDOperand InCallSEQ = LastCALLSEQ_END;
993 // Note that we are selecting this call!
994 LastCALLSEQ_END = SDOperand(CallEnd, 0);
995 IsLegalizingCall = true;
996
997 // Legalize the call, starting from the CALLSEQ_END.
998 LegalizeOp(LastCALLSEQ_END);
999 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1000 return Result;
1001 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001002 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001003 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1004 // will cause this node to be legalized as well as handling libcalls right.
1005 if (LastCALLSEQ_END.Val != Node) {
1006 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1007 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1008 assert(I != LegalizedNodes.end() &&
1009 "Legalizing the call start should have legalized this node!");
1010 return I->second;
1011 }
1012
1013 // Otherwise, the call start has been legalized and everything is going
1014 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001016 // Do not try to legalize the target-specific arguments (#1+), except for
1017 // an optional flag input.
1018 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1019 if (Tmp1 != Node->getOperand(0)) {
1020 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1021 Ops[0] = Tmp1;
1022 Result = DAG.UpdateNodeOperands(Result, Ops);
1023 }
1024 } else {
1025 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1026 if (Tmp1 != Node->getOperand(0) ||
1027 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1028 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1029 Ops[0] = Tmp1;
1030 Ops.back() = Tmp2;
1031 Result = DAG.UpdateNodeOperands(Result, Ops);
1032 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001033 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001034 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001035 // This finishes up call legalization.
1036 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001037
1038 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1039 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1040 if (Node->getNumValues() == 2)
1041 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1042 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001043 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001044 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1046 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001048
Chris Lattnerd02b0542006-01-28 10:58:55 +00001049 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001050 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001051 switch (TLI.getOperationAction(Node->getOpcode(),
1052 Node->getValueType(0))) {
1053 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001054 case TargetLowering::Expand: {
1055 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1056 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1057 " not tell us which reg is the stack pointer!");
1058 SDOperand Chain = Tmp1.getOperand(0);
1059 SDOperand Size = Tmp2.getOperand(1);
1060 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1061 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1062 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001063 Tmp1 = LegalizeOp(Tmp1);
1064 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001065 break;
1066 }
1067 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001068 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1069 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001070 Tmp1 = LegalizeOp(Tmp3);
1071 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001072 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001073 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001074 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001075 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001076 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001077 // Since this op produce two values, make sure to remember that we
1078 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001079 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1080 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001081 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001082 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001083 case ISD::INLINEASM:
1084 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1085 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001086 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001087 Tmp2 = Tmp3 = SDOperand(0, 0);
1088 else
1089 Tmp3 = LegalizeOp(Tmp2);
1090
1091 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1092 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1093 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001094 if (Tmp3.Val) Ops.back() = Tmp3;
1095 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001096 }
1097
1098 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001099 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001100 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1101 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001102 case ISD::BR:
1103 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001104 // Ensure that libcalls are emitted before a branch.
1105 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1106 Tmp1 = LegalizeOp(Tmp1);
1107 LastCALLSEQ_END = DAG.getEntryNode();
1108
Chris Lattnerd02b0542006-01-28 10:58:55 +00001109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001110 break;
1111
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001112 case ISD::BRCOND:
1113 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001114 // Ensure that libcalls are emitted before a return.
1115 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1116 Tmp1 = LegalizeOp(Tmp1);
1117 LastCALLSEQ_END = DAG.getEntryNode();
1118
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001119 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1120 case Expand: assert(0 && "It's impossible to expand bools");
1121 case Legal:
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1123 break;
1124 case Promote:
1125 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1126 break;
1127 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001128
1129 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001131
1132 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1133 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001134 case TargetLowering::Legal: break;
1135 case TargetLowering::Custom:
1136 Tmp1 = TLI.LowerOperation(Result, DAG);
1137 if (Tmp1.Val) Result = Tmp1;
1138 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001139 case TargetLowering::Expand:
1140 // Expand brcond's setcc into its constituent parts and create a BR_CC
1141 // Node.
1142 if (Tmp2.getOpcode() == ISD::SETCC) {
1143 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1144 Tmp2.getOperand(0), Tmp2.getOperand(1),
1145 Node->getOperand(2));
1146 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001147 // Make sure the condition is either zero or one. It may have been
1148 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001149 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1150 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1151 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001152
Nate Begeman371e4952005-08-16 19:49:35 +00001153 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1154 DAG.getCondCode(ISD::SETNE), Tmp2,
1155 DAG.getConstant(0, Tmp2.getValueType()),
1156 Node->getOperand(2));
1157 }
1158 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001159 }
1160 break;
1161 case ISD::BR_CC:
1162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001163 // Ensure that libcalls are emitted before a branch.
1164 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1165 Tmp1 = LegalizeOp(Tmp1);
1166 LastCALLSEQ_END = DAG.getEntryNode();
1167
Nate Begeman7e7f4392006-02-01 07:19:44 +00001168 Tmp2 = Node->getOperand(2); // LHS
1169 Tmp3 = Node->getOperand(3); // RHS
1170 Tmp4 = Node->getOperand(1); // CC
1171
1172 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1173
1174 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1175 // the LHS is a legal SETCC itself. In this case, we need to compare
1176 // the result against zero to select between true and false values.
1177 if (Tmp3.Val == 0) {
1178 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1179 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001180 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001181
1182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1183 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001184
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001185 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1186 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001187 case TargetLowering::Legal: break;
1188 case TargetLowering::Custom:
1189 Tmp4 = TLI.LowerOperation(Result, DAG);
1190 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001191 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001192 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001193 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001194 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001195 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1196 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001197
Evan Cheng31d15fa2005-12-23 07:29:34 +00001198 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1200 Tmp2 = Result.getValue(0);
1201 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001202
Evan Cheng31d15fa2005-12-23 07:29:34 +00001203 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1204 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001205 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001206 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001207 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001208 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001209 Tmp2 = LegalizeOp(Tmp1);
1210 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001211 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001212 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001213 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001214 // Since loads produce two values, make sure to remember that we
1215 // legalized both of them.
1216 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1217 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1218 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001219 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001220 case ISD::EXTLOAD:
1221 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001222 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001223 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1224 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001225
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001226 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001227 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001228 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001229 case TargetLowering::Promote:
1230 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001231 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1232 DAG.getValueType(MVT::i8));
1233 Tmp1 = Result.getValue(0);
1234 Tmp2 = Result.getValue(1);
1235 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001236 case TargetLowering::Custom:
1237 isCustom = true;
1238 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001239 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001240 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1241 Node->getOperand(3));
1242 Tmp1 = Result.getValue(0);
1243 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001244
1245 if (isCustom) {
1246 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1247 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001248 Tmp1 = LegalizeOp(Tmp3);
1249 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001250 }
1251 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001252 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001253 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001254 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001255 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1256 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001257 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001258 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1259 Tmp2 = LegalizeOp(Load.getValue(1));
1260 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001261 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001262 assert(Node->getOpcode() != ISD::EXTLOAD &&
1263 "EXTLOAD should always be supported!");
1264 // Turn the unsupported load into an EXTLOAD followed by an explicit
1265 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001266 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1267 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001268 SDOperand ValRes;
1269 if (Node->getOpcode() == ISD::SEXTLOAD)
1270 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001271 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001272 else
1273 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001274 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1275 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1276 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001277 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001278 // Since loads produce two values, make sure to remember that we legalized
1279 // both of them.
1280 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1281 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1282 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001283 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001284 case ISD::EXTRACT_ELEMENT: {
1285 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1286 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001287 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001288 case Legal:
1289 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1290 // 1 -> Hi
1291 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1292 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1293 TLI.getShiftAmountTy()));
1294 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1295 } else {
1296 // 0 -> Lo
1297 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1298 Node->getOperand(0));
1299 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001300 break;
1301 case Expand:
1302 // Get both the low and high parts.
1303 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1304 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1305 Result = Tmp2; // 1 -> Hi
1306 else
1307 Result = Tmp1; // 0 -> Lo
1308 break;
1309 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001310 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001311 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001312
1313 case ISD::CopyToReg:
1314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001315
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001316 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001317 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001318 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001319 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001320 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001322 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001323 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001324 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001325 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1327 Tmp3);
1328 } else {
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001330 }
1331
1332 // Since this produces two values, make sure to remember that we legalized
1333 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001334 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001335 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001336 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001337 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001338 break;
1339
1340 case ISD::RET:
1341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001342
1343 // Ensure that libcalls are emitted before a return.
1344 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1345 Tmp1 = LegalizeOp(Tmp1);
1346 LastCALLSEQ_END = DAG.getEntryNode();
1347
Chris Lattnerdc750592005-01-07 07:47:09 +00001348 switch (Node->getNumOperands()) {
1349 case 2: // ret val
1350 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1351 case Legal:
1352 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001354 break;
1355 case Expand: {
1356 SDOperand Lo, Hi;
1357 ExpandOp(Node->getOperand(1), Lo, Hi);
1358 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001359 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001360 }
1361 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001362 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1364 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001365 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001366 }
1367 break;
1368 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001369 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001370 break;
1371 default: { // ret <values>
1372 std::vector<SDOperand> NewValues;
1373 NewValues.push_back(Tmp1);
1374 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1375 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1376 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001377 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001378 break;
1379 case Expand: {
1380 SDOperand Lo, Hi;
1381 ExpandOp(Node->getOperand(i), Lo, Hi);
1382 NewValues.push_back(Lo);
1383 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001384 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001385 }
1386 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001387 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001388 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001389
1390 if (NewValues.size() == Node->getNumOperands())
1391 Result = DAG.UpdateNodeOperands(Result, NewValues);
1392 else
1393 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001394 break;
1395 }
1396 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001397
Chris Lattner4d1ea712006-01-29 21:02:23 +00001398 if (Result.getOpcode() == ISD::RET) {
1399 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1400 default: assert(0 && "This action is not supported yet!");
1401 case TargetLowering::Legal: break;
1402 case TargetLowering::Custom:
1403 Tmp1 = TLI.LowerOperation(Result, DAG);
1404 if (Tmp1.Val) Result = Tmp1;
1405 break;
1406 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001407 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001408 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001409 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001410 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1411 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1412
Chris Lattnere69daaf2005-01-08 06:25:56 +00001413 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001414 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001415 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001416 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001417 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001418 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001419 } else {
1420 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001421 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001422 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001423 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1424 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001425 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001426 }
1427
Chris Lattnerdc750592005-01-07 07:47:09 +00001428 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1429 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001430 Tmp3 = LegalizeOp(Node->getOperand(1));
1431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1432 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001433
Chris Lattnerd02b0542006-01-28 10:58:55 +00001434 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001435 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1436 default: assert(0 && "This action is not supported yet!");
1437 case TargetLowering::Legal: break;
1438 case TargetLowering::Custom:
1439 Tmp1 = TLI.LowerOperation(Result, DAG);
1440 if (Tmp1.Val) Result = Tmp1;
1441 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001442 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001443 break;
1444 }
1445 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001446 // Truncate the value and store the result.
1447 Tmp3 = PromoteOp(Node->getOperand(1));
1448 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001449 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001450 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001451 break;
1452
Chris Lattnerdc750592005-01-07 07:47:09 +00001453 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001454 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001455 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001456
1457 // If this is a vector type, then we have to calculate the increment as
1458 // the product of the element size in bytes, and the number of elements
1459 // in the high half of the vector.
1460 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1461 SDNode *InVal = Node->getOperand(1).Val;
1462 unsigned NumElems =
1463 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1464 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1465
1466 // Figure out if there is a Packed type corresponding to this Vector
1467 // type. If so, convert to the packed type.
1468 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1469 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1470 // Turn this into a normal store of the packed type.
1471 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1473 Node->getOperand(3));
1474 break;
1475 } else if (NumElems == 1) {
1476 // Turn this into a normal store of the scalar type.
1477 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1478 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1479 Node->getOperand(3));
Chris Lattner8e1fcab2006-03-31 17:37:22 +00001480 // The scalarized value type may not be legal, e.g. it might require
1481 // promotion or expansion. Relegalize the scalar store.
1482 Result = LegalizeOp(Result);
Chris Lattner32206f52006-03-18 01:44:44 +00001483 break;
1484 } else {
1485 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1486 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1487 }
1488 } else {
1489 ExpandOp(Node->getOperand(1), Lo, Hi);
1490 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1491 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001492
1493 if (!TLI.isLittleEndian())
1494 std::swap(Lo, Hi);
1495
Chris Lattner55e9cde2005-05-11 04:51:16 +00001496 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1497 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001498 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1499 getIntPtrConstant(IncrementSize));
1500 assert(isTypeLegal(Tmp2.getValueType()) &&
1501 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001502 // FIXME: This sets the srcvalue of both halves to be the same, which is
1503 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001504 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1505 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001506 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1507 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001508 }
1509 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001510 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001511 case ISD::PCMARKER:
1512 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001513 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001514 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001515 case ISD::STACKSAVE:
1516 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001517 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1518 Tmp1 = Result.getValue(0);
1519 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001520
Chris Lattnerb3266452006-01-13 02:50:02 +00001521 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1522 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001523 case TargetLowering::Legal: break;
1524 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001525 Tmp3 = TLI.LowerOperation(Result, DAG);
1526 if (Tmp3.Val) {
1527 Tmp1 = LegalizeOp(Tmp3);
1528 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001529 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001530 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001531 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001532 // Expand to CopyFromReg if the target set
1533 // StackPointerRegisterToSaveRestore.
1534 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001535 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001536 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001537 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001538 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001539 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1540 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001541 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001542 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001543 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001544
1545 // Since stacksave produce two values, make sure to remember that we
1546 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001547 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1548 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1549 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001550
Chris Lattnerb3266452006-01-13 02:50:02 +00001551 case ISD::STACKRESTORE:
1552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1553 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001554 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001555
1556 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1557 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001558 case TargetLowering::Legal: break;
1559 case TargetLowering::Custom:
1560 Tmp1 = TLI.LowerOperation(Result, DAG);
1561 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001562 break;
1563 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001564 // Expand to CopyToReg if the target set
1565 // StackPointerRegisterToSaveRestore.
1566 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1567 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1568 } else {
1569 Result = Tmp1;
1570 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001571 break;
1572 }
1573 break;
1574
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001575 case ISD::READCYCLECOUNTER:
1576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001577 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001578
1579 // Since rdcc produce two values, make sure to remember that we legalized
1580 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001581 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001582 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001583 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001584
Evan Cheng31d15fa2005-12-23 07:29:34 +00001585 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1587 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1588
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001589 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1590 "Cannot handle illegal TRUNCSTORE yet!");
1591 Tmp2 = LegalizeOp(Node->getOperand(1));
1592
1593 // The only promote case we handle is TRUNCSTORE:i1 X into
1594 // -> TRUNCSTORE:i8 (and X, 1)
1595 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1596 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1597 TargetLowering::Promote) {
1598 // Promote the bool to a mask then store.
1599 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1600 DAG.getConstant(1, Tmp2.getValueType()));
1601 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1602 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001603
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001604 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1605 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001606 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1607 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001608 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001609
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001610 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1611 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1612 default: assert(0 && "This action is not supported yet!");
1613 case TargetLowering::Legal: break;
1614 case TargetLowering::Custom:
1615 Tmp1 = TLI.LowerOperation(Result, DAG);
1616 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001617 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001618 }
1619 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001620 }
Chris Lattner39c67442005-01-14 22:08:15 +00001621 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1623 case Expand: assert(0 && "It's impossible to expand bools");
1624 case Legal:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1626 break;
1627 case Promote:
1628 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1629 break;
1630 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001631 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001632 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001633
Chris Lattnerd02b0542006-01-28 10:58:55 +00001634 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001635
Nate Begeman987121a2005-08-23 04:29:48 +00001636 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001637 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001638 case TargetLowering::Legal: break;
1639 case TargetLowering::Custom: {
1640 Tmp1 = TLI.LowerOperation(Result, DAG);
1641 if (Tmp1.Val) Result = Tmp1;
1642 break;
1643 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001644 case TargetLowering::Expand:
1645 if (Tmp1.getOpcode() == ISD::SETCC) {
1646 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1647 Tmp2, Tmp3,
1648 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1649 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001650 // Make sure the condition is either zero or one. It may have been
1651 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001652 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1653 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1654 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001655 Result = DAG.getSelectCC(Tmp1,
1656 DAG.getConstant(0, Tmp1.getValueType()),
1657 Tmp2, Tmp3, ISD::SETNE);
1658 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001659 break;
1660 case TargetLowering::Promote: {
1661 MVT::ValueType NVT =
1662 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1663 unsigned ExtOp, TruncOp;
1664 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001665 ExtOp = ISD::ANY_EXTEND;
1666 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001667 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001668 ExtOp = ISD::FP_EXTEND;
1669 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001670 }
1671 // Promote each of the values to the new type.
1672 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1673 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1674 // Perform the larger operation, then round down.
1675 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1676 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1677 break;
1678 }
1679 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001680 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001681 case ISD::SELECT_CC: {
1682 Tmp1 = Node->getOperand(0); // LHS
1683 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001684 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1685 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001686 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001687
Nate Begeman7e7f4392006-02-01 07:19:44 +00001688 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1689
1690 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1691 // the LHS is a legal SETCC itself. In this case, we need to compare
1692 // the result against zero to select between true and false values.
1693 if (Tmp2.Val == 0) {
1694 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1695 CC = DAG.getCondCode(ISD::SETNE);
1696 }
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1698
1699 // Everything is legal, see if we should expand this op or something.
1700 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1701 default: assert(0 && "This action is not supported yet!");
1702 case TargetLowering::Legal: break;
1703 case TargetLowering::Custom:
1704 Tmp1 = TLI.LowerOperation(Result, DAG);
1705 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001706 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001707 }
1708 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001709 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001710 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001711 Tmp1 = Node->getOperand(0);
1712 Tmp2 = Node->getOperand(1);
1713 Tmp3 = Node->getOperand(2);
1714 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1715
1716 // If we had to Expand the SetCC operands into a SELECT node, then it may
1717 // not always be possible to return a true LHS & RHS. In this case, just
1718 // return the value we legalized, returned in the LHS
1719 if (Tmp2.Val == 0) {
1720 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001721 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001722 }
Nate Begeman987121a2005-08-23 04:29:48 +00001723
Chris Lattnerf263a232006-01-30 22:43:50 +00001724 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001725 default: assert(0 && "Cannot handle this action for SETCC yet!");
1726 case TargetLowering::Custom:
1727 isCustom = true;
1728 // FALLTHROUGH.
1729 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001731 if (isCustom) {
1732 Tmp3 = TLI.LowerOperation(Result, DAG);
1733 if (Tmp3.Val) Result = Tmp3;
1734 }
Nate Begeman987121a2005-08-23 04:29:48 +00001735 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001736 case TargetLowering::Promote: {
1737 // First step, figure out the appropriate operation to use.
1738 // Allow SETCC to not be supported for all legal data types
1739 // Mostly this targets FP
1740 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1741 MVT::ValueType OldVT = NewInTy;
1742
1743 // Scan for the appropriate larger type to use.
1744 while (1) {
1745 NewInTy = (MVT::ValueType)(NewInTy+1);
1746
1747 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1748 "Fell off of the edge of the integer world");
1749 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1750 "Fell off of the edge of the floating point world");
1751
1752 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001753 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001754 break;
1755 }
1756 if (MVT::isInteger(NewInTy))
1757 assert(0 && "Cannot promote Legal Integer SETCC yet");
1758 else {
1759 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1760 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1761 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001762 Tmp1 = LegalizeOp(Tmp1);
1763 Tmp2 = LegalizeOp(Tmp2);
1764 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001765 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001766 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001767 }
Nate Begeman987121a2005-08-23 04:29:48 +00001768 case TargetLowering::Expand:
1769 // Expand a setcc node into a select_cc of the same condition, lhs, and
1770 // rhs that selects between const 1 (true) and const 0 (false).
1771 MVT::ValueType VT = Node->getValueType(0);
1772 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1773 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1774 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001775 break;
1776 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001777 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001778 case ISD::MEMSET:
1779 case ISD::MEMCPY:
1780 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001782 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1783
1784 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1785 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1786 case Expand: assert(0 && "Cannot expand a byte!");
1787 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001788 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001789 break;
1790 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001791 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001792 break;
1793 }
1794 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001795 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001796 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001797
1798 SDOperand Tmp4;
1799 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001800 case Expand: {
1801 // Length is too big, just take the lo-part of the length.
1802 SDOperand HiPart;
1803 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1804 break;
1805 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001806 case Legal:
1807 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001808 break;
1809 case Promote:
1810 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001811 break;
1812 }
1813
1814 SDOperand Tmp5;
1815 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1816 case Expand: assert(0 && "Cannot expand this yet!");
1817 case Legal:
1818 Tmp5 = LegalizeOp(Node->getOperand(4));
1819 break;
1820 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001821 Tmp5 = PromoteOp(Node->getOperand(4));
1822 break;
1823 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001824
1825 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1826 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001827 case TargetLowering::Custom:
1828 isCustom = true;
1829 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001830 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001832 if (isCustom) {
1833 Tmp1 = TLI.LowerOperation(Result, DAG);
1834 if (Tmp1.Val) Result = Tmp1;
1835 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001836 break;
1837 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001838 // Otherwise, the target does not support this operation. Lower the
1839 // operation to an explicit libcall as appropriate.
1840 MVT::ValueType IntPtr = TLI.getPointerTy();
1841 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1842 std::vector<std::pair<SDOperand, const Type*> > Args;
1843
Reid Spencer6dced922005-01-12 14:53:45 +00001844 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001845 if (Node->getOpcode() == ISD::MEMSET) {
1846 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001847 // Extend the (previously legalized) ubyte argument to be an int value
1848 // for the call.
1849 if (Tmp3.getValueType() > MVT::i32)
1850 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1851 else
1852 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001853 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1854 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1855
1856 FnName = "memset";
1857 } else if (Node->getOpcode() == ISD::MEMCPY ||
1858 Node->getOpcode() == ISD::MEMMOVE) {
1859 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1860 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1861 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1862 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1863 } else {
1864 assert(0 && "Unknown op!");
1865 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001866
Chris Lattner85d70c62005-01-11 05:57:22 +00001867 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001868 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001869 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001870 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001871 break;
1872 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001873 }
1874 break;
1875 }
Chris Lattner5385db52005-05-09 20:23:03 +00001876
Chris Lattner4157c412005-04-02 04:00:59 +00001877 case ISD::SHL_PARTS:
1878 case ISD::SRA_PARTS:
1879 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001880 std::vector<SDOperand> Ops;
1881 bool Changed = false;
1882 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1883 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1884 Changed |= Ops.back() != Node->getOperand(i);
1885 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001886 if (Changed)
1887 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001888
Evan Cheng870e4f82006-01-09 18:31:59 +00001889 switch (TLI.getOperationAction(Node->getOpcode(),
1890 Node->getValueType(0))) {
1891 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001892 case TargetLowering::Legal: break;
1893 case TargetLowering::Custom:
1894 Tmp1 = TLI.LowerOperation(Result, DAG);
1895 if (Tmp1.Val) {
1896 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001897 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001898 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001899 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1900 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001901 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001902 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001903 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001904 return RetVal;
1905 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001906 break;
1907 }
1908
Chris Lattner13fe99c2005-04-02 05:00:07 +00001909 // Since these produce multiple values, make sure to remember that we
1910 // legalized all of them.
1911 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1912 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1913 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001914 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001915
1916 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001917 case ISD::ADD:
1918 case ISD::SUB:
1919 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001920 case ISD::MULHS:
1921 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001922 case ISD::UDIV:
1923 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001924 case ISD::AND:
1925 case ISD::OR:
1926 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001927 case ISD::SHL:
1928 case ISD::SRL:
1929 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001930 case ISD::FADD:
1931 case ISD::FSUB:
1932 case ISD::FMUL:
1933 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001934 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001935 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1936 case Expand: assert(0 && "Not possible");
1937 case Legal:
1938 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1939 break;
1940 case Promote:
1941 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1942 break;
1943 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001944
1945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001946
Andrew Lenharth72594262005-12-24 23:42:32 +00001947 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001948 default: assert(0 && "Operation not supported");
1949 case TargetLowering::Legal: break;
1950 case TargetLowering::Custom:
1951 Tmp1 = TLI.LowerOperation(Result, DAG);
1952 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001953 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001954 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001955 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001956
1957 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1958 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1959 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1960 case Expand: assert(0 && "Not possible");
1961 case Legal:
1962 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1963 break;
1964 case Promote:
1965 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1966 break;
1967 }
1968
1969 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1970
1971 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1972 default: assert(0 && "Operation not supported");
1973 case TargetLowering::Custom:
1974 Tmp1 = TLI.LowerOperation(Result, DAG);
1975 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001976 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001977 case TargetLowering::Legal: break;
1978 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001979 // If this target supports fabs/fneg natively, do this efficiently.
1980 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1981 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1982 // Get the sign bit of the RHS.
1983 MVT::ValueType IVT =
1984 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1985 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1986 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1987 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1988 // Get the absolute value of the result.
1989 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1990 // Select between the nabs and abs value based on the sign bit of
1991 // the input.
1992 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1993 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1994 AbsVal),
1995 AbsVal);
1996 Result = LegalizeOp(Result);
1997 break;
1998 }
1999
2000 // Otherwise, do bitwise ops!
2001
2002 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002003 const char *FnName;
2004 if (Node->getValueType(0) == MVT::f32) {
2005 FnName = "copysignf";
2006 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2007 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2008 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2009 } else {
2010 FnName = "copysign";
2011 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2012 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2013 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2014 }
2015 SDOperand Dummy;
2016 Result = ExpandLibCall(FnName, Node, Dummy);
2017 break;
2018 }
2019 break;
2020
Nate Begeman5965bd12006-02-17 05:43:56 +00002021 case ISD::ADDC:
2022 case ISD::SUBC:
2023 Tmp1 = LegalizeOp(Node->getOperand(0));
2024 Tmp2 = LegalizeOp(Node->getOperand(1));
2025 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2026 // Since this produces two values, make sure to remember that we legalized
2027 // both of them.
2028 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2029 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2030 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002031
Nate Begeman5965bd12006-02-17 05:43:56 +00002032 case ISD::ADDE:
2033 case ISD::SUBE:
2034 Tmp1 = LegalizeOp(Node->getOperand(0));
2035 Tmp2 = LegalizeOp(Node->getOperand(1));
2036 Tmp3 = LegalizeOp(Node->getOperand(2));
2037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2038 // Since this produces two values, make sure to remember that we legalized
2039 // both of them.
2040 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2041 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2042 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002043
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002044 case ISD::BUILD_PAIR: {
2045 MVT::ValueType PairTy = Node->getValueType(0);
2046 // TODO: handle the case where the Lo and Hi operands are not of legal type
2047 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2048 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2049 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002050 case TargetLowering::Promote:
2051 case TargetLowering::Custom:
2052 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002053 case TargetLowering::Legal:
2054 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2055 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2056 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002057 case TargetLowering::Expand:
2058 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2059 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2060 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2061 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2062 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002063 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002064 break;
2065 }
2066 break;
2067 }
2068
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002069 case ISD::UREM:
2070 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002071 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002072 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2073 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002074
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002075 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002076 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2077 case TargetLowering::Custom:
2078 isCustom = true;
2079 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002080 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002082 if (isCustom) {
2083 Tmp1 = TLI.LowerOperation(Result, DAG);
2084 if (Tmp1.Val) Result = Tmp1;
2085 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002086 break;
Chris Lattner81914422005-08-03 20:31:37 +00002087 case TargetLowering::Expand:
2088 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002089 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002090 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002091 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002092 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2093 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2094 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2095 } else {
2096 // Floating point mod -> fmod libcall.
2097 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2098 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002099 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002100 }
2101 break;
2102 }
2103 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002104 case ISD::VAARG: {
2105 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2107
Chris Lattner364b89a2006-01-28 07:42:08 +00002108 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002109 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2110 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002111 case TargetLowering::Custom:
2112 isCustom = true;
2113 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002114 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2116 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002117 Tmp1 = Result.getValue(1);
2118
2119 if (isCustom) {
2120 Tmp2 = TLI.LowerOperation(Result, DAG);
2121 if (Tmp2.Val) {
2122 Result = LegalizeOp(Tmp2);
2123 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2124 }
2125 }
Nate Begemane74795c2006-01-25 18:21:52 +00002126 break;
2127 case TargetLowering::Expand: {
2128 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2129 Node->getOperand(2));
2130 // Increment the pointer, VAList, to the next vaarg
2131 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2132 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2133 TLI.getPointerTy()));
2134 // Store the incremented VAList to the legalized pointer
2135 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2136 Node->getOperand(2));
2137 // Load the actual argument out of the pointer VAList
2138 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002139 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002140 Result = LegalizeOp(Result);
2141 break;
2142 }
2143 }
2144 // Since VAARG produces two values, make sure to remember that we
2145 // legalized both of them.
2146 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002147 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2148 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002149 }
2150
2151 case ISD::VACOPY:
2152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2153 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2154 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2155
2156 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2157 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002158 case TargetLowering::Custom:
2159 isCustom = true;
2160 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002161 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2163 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002164 if (isCustom) {
2165 Tmp1 = TLI.LowerOperation(Result, DAG);
2166 if (Tmp1.Val) Result = Tmp1;
2167 }
Nate Begemane74795c2006-01-25 18:21:52 +00002168 break;
2169 case TargetLowering::Expand:
2170 // This defaults to loading a pointer from the input and storing it to the
2171 // output, returning the chain.
2172 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2173 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2174 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002175 break;
2176 }
2177 break;
2178
2179 case ISD::VAEND:
2180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2182
2183 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2184 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002185 case TargetLowering::Custom:
2186 isCustom = true;
2187 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002188 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002190 if (isCustom) {
2191 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2192 if (Tmp1.Val) Result = Tmp1;
2193 }
Nate Begemane74795c2006-01-25 18:21:52 +00002194 break;
2195 case TargetLowering::Expand:
2196 Result = Tmp1; // Default to a no-op, return the chain
2197 break;
2198 }
2199 break;
2200
2201 case ISD::VASTART:
2202 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2203 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2204
Chris Lattnerd02b0542006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2206
Nate Begemane74795c2006-01-25 18:21:52 +00002207 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2208 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002209 case TargetLowering::Legal: break;
2210 case TargetLowering::Custom:
2211 Tmp1 = TLI.LowerOperation(Result, DAG);
2212 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002213 break;
2214 }
2215 break;
2216
Nate Begeman1b8121b2006-01-11 21:21:00 +00002217 case ISD::ROTL:
2218 case ISD::ROTR:
2219 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2220 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002221
2222 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2223 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002224 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002225 break;
2226
Nate Begeman2fba8a32006-01-14 03:14:10 +00002227 case ISD::BSWAP:
2228 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2229 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002230 case TargetLowering::Custom:
2231 assert(0 && "Cannot custom legalize this yet!");
2232 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002233 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002234 break;
2235 case TargetLowering::Promote: {
2236 MVT::ValueType OVT = Tmp1.getValueType();
2237 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2238 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002239
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002240 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2241 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2242 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2243 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2244 break;
2245 }
2246 case TargetLowering::Expand:
2247 Result = ExpandBSWAP(Tmp1);
2248 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002249 }
2250 break;
2251
Andrew Lenharth5e177822005-05-03 17:19:30 +00002252 case ISD::CTPOP:
2253 case ISD::CTTZ:
2254 case ISD::CTLZ:
2255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2256 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002257 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002258 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002259 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002260 break;
2261 case TargetLowering::Promote: {
2262 MVT::ValueType OVT = Tmp1.getValueType();
2263 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002264
2265 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002266 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2267 // Perform the larger operation, then subtract if needed.
2268 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002269 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002270 case ISD::CTPOP:
2271 Result = Tmp1;
2272 break;
2273 case ISD::CTTZ:
2274 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002275 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2276 DAG.getConstant(getSizeInBits(NVT), NVT),
2277 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002278 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002279 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2280 break;
2281 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002282 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002283 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2284 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002285 getSizeInBits(OVT), NVT));
2286 break;
2287 }
2288 break;
2289 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002290 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002291 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002292 break;
2293 }
2294 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002295
Chris Lattner13fe99c2005-04-02 05:00:07 +00002296 // Unary operators
2297 case ISD::FABS:
2298 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002299 case ISD::FSQRT:
2300 case ISD::FSIN:
2301 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002302 Tmp1 = LegalizeOp(Node->getOperand(0));
2303 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002304 case TargetLowering::Promote:
2305 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002306 isCustom = true;
2307 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002308 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002309 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002310 if (isCustom) {
2311 Tmp1 = TLI.LowerOperation(Result, DAG);
2312 if (Tmp1.Val) Result = Tmp1;
2313 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002314 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002315 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002316 switch (Node->getOpcode()) {
2317 default: assert(0 && "Unreachable!");
2318 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002319 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2320 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002321 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002322 break;
Chris Lattner80026402005-04-30 04:43:14 +00002323 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002324 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2325 MVT::ValueType VT = Node->getValueType(0);
2326 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002327 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002328 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2329 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002330 break;
2331 }
2332 case ISD::FSQRT:
2333 case ISD::FSIN:
2334 case ISD::FCOS: {
2335 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002336 const char *FnName = 0;
2337 switch(Node->getOpcode()) {
2338 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2339 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2340 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2341 default: assert(0 && "Unreachable!");
2342 }
Nate Begeman77558da2005-08-04 21:43:28 +00002343 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002344 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002345 break;
2346 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002347 }
2348 break;
2349 }
2350 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002351
2352 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002353 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002354 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002355 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002356 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2357 Node->getOperand(0).getValueType())) {
2358 default: assert(0 && "Unknown operation action!");
2359 case TargetLowering::Expand:
2360 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2361 break;
2362 case TargetLowering::Legal:
2363 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002364 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002365 break;
2366 }
2367 }
2368 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002369 case ISD::VBIT_CONVERT: {
2370 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2371 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2372
2373 // The input has to be a vector type, we have to either scalarize it, pack
2374 // it, or convert it based on whether the input vector type is legal.
2375 SDNode *InVal = Node->getOperand(0).Val;
2376 unsigned NumElems =
2377 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2378 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2379
2380 // Figure out if there is a Packed type corresponding to this Vector
2381 // type. If so, convert to the packed type.
2382 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2383 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2384 // Turn this into a bit convert of the packed input.
2385 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2386 PackVectorOp(Node->getOperand(0), TVT));
2387 break;
2388 } else if (NumElems == 1) {
2389 // Turn this into a bit convert of the scalar input.
2390 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2391 PackVectorOp(Node->getOperand(0), EVT));
2392 break;
2393 } else {
2394 // FIXME: UNIMP! Store then reload
2395 assert(0 && "Cast from unsupported vector type not implemented yet!");
2396 }
2397 }
2398
Chris Lattner13fe99c2005-04-02 05:00:07 +00002399 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002400 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002401 case ISD::UINT_TO_FP: {
2402 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002403 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2404 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002405 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002406 Node->getOperand(0).getValueType())) {
2407 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002408 case TargetLowering::Custom:
2409 isCustom = true;
2410 // FALLTHROUGH
2411 case TargetLowering::Legal:
2412 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002413 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002414 if (isCustom) {
2415 Tmp1 = TLI.LowerOperation(Result, DAG);
2416 if (Tmp1.Val) Result = Tmp1;
2417 }
2418 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002419 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002420 Result = ExpandLegalINT_TO_FP(isSigned,
2421 LegalizeOp(Node->getOperand(0)),
2422 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002423 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002424 case TargetLowering::Promote:
2425 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2426 Node->getValueType(0),
2427 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002428 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002429 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002430 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002431 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002432 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2433 Node->getValueType(0), Node->getOperand(0));
2434 break;
2435 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002436 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002437 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002438 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2439 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002440 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002441 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2442 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002443 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002444 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2445 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002446 break;
2447 }
2448 break;
2449 }
2450 case ISD::TRUNCATE:
2451 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2452 case Legal:
2453 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002454 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002455 break;
2456 case Expand:
2457 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2458
2459 // Since the result is legal, we should just be able to truncate the low
2460 // part of the source.
2461 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2462 break;
2463 case Promote:
2464 Result = PromoteOp(Node->getOperand(0));
2465 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2466 break;
2467 }
2468 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002469
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002470 case ISD::FP_TO_SINT:
2471 case ISD::FP_TO_UINT:
2472 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2473 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002474 Tmp1 = LegalizeOp(Node->getOperand(0));
2475
Chris Lattner44fe26f2005-07-29 00:11:56 +00002476 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2477 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002478 case TargetLowering::Custom:
2479 isCustom = true;
2480 // FALLTHROUGH
2481 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002482 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002483 if (isCustom) {
2484 Tmp1 = TLI.LowerOperation(Result, DAG);
2485 if (Tmp1.Val) Result = Tmp1;
2486 }
2487 break;
2488 case TargetLowering::Promote:
2489 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2490 Node->getOpcode() == ISD::FP_TO_SINT);
2491 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002492 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002493 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2494 SDOperand True, False;
2495 MVT::ValueType VT = Node->getOperand(0).getValueType();
2496 MVT::ValueType NVT = Node->getValueType(0);
2497 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2498 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2499 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2500 Node->getOperand(0), Tmp2, ISD::SETLT);
2501 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2502 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002503 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002504 Tmp2));
2505 False = DAG.getNode(ISD::XOR, NVT, False,
2506 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002507 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2508 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002509 } else {
2510 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2511 }
2512 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002513 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002514 break;
2515 case Expand:
2516 assert(0 && "Shouldn't need to expand other operators here!");
2517 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002518 Tmp1 = PromoteOp(Node->getOperand(0));
2519 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2520 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002521 break;
2522 }
2523 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002524
Chris Lattner7753f172005-09-02 00:18:10 +00002525 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002526 case ISD::ZERO_EXTEND:
2527 case ISD::SIGN_EXTEND:
2528 case ISD::FP_EXTEND:
2529 case ISD::FP_ROUND:
2530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002531 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002532 case Legal:
2533 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002534 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002535 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002536 case Promote:
2537 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002538 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002539 Tmp1 = PromoteOp(Node->getOperand(0));
2540 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002541 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002542 case ISD::ZERO_EXTEND:
2543 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002544 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002545 Result = DAG.getZeroExtendInReg(Result,
2546 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002547 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002548 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002549 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002550 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002551 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002552 Result,
2553 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002554 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002555 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002556 Result = PromoteOp(Node->getOperand(0));
2557 if (Result.getValueType() != Op.getValueType())
2558 // Dynamically dead while we have only 2 FP types.
2559 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2560 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002561 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002562 Result = PromoteOp(Node->getOperand(0));
2563 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2564 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002565 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002566 }
2567 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002568 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002569 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002570 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002571 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002572
2573 // If this operation is not supported, convert it to a shl/shr or load/store
2574 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002575 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2576 default: assert(0 && "This action not supported for this op yet!");
2577 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002579 break;
2580 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002581 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002582 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002583 // NOTE: we could fall back on load/store here too for targets without
2584 // SAR. However, it is doubtful that any exist.
2585 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2586 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002587 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002588 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2589 Node->getOperand(0), ShiftCst);
2590 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2591 Result, ShiftCst);
2592 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2593 // The only way we can lower this is to turn it into a STORETRUNC,
2594 // EXTLOAD pair, targetting a temporary location (a stack slot).
2595
2596 // NOTE: there is a choice here between constantly creating new stack
2597 // slots and always reusing the same one. We currently always create
2598 // new ones, as reuse may inhibit scheduling.
2599 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2600 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2601 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2602 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002603 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002604 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2605 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2606 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002607 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002608 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002609 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2610 Result, StackSlot, DAG.getSrcValue(NULL),
2611 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002612 } else {
2613 assert(0 && "Unknown op");
2614 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002615 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002616 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002617 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002618 }
Chris Lattner99222f72005-01-15 07:15:18 +00002619 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002620
2621 // Make sure that the generated code is itself legal.
2622 if (Result != Op)
2623 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002624
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002625 // Note that LegalizeOp may be reentered even from single-use nodes, which
2626 // means that we always must cache transformed nodes.
2627 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002628 return Result;
2629}
2630
Chris Lattner4d978642005-01-15 22:16:26 +00002631/// PromoteOp - Given an operation that produces a value in an invalid type,
2632/// promote it to compute the value into a larger type. The produced value will
2633/// have the correct bits for the low portion of the register, but no guarantee
2634/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002635SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2636 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002637 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002638 assert(getTypeAction(VT) == Promote &&
2639 "Caller should expand or legalize operands that are not promotable!");
2640 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2641 "Cannot promote to smaller type!");
2642
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002643 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002644 SDOperand Result;
2645 SDNode *Node = Op.Val;
2646
Chris Lattner1a570f12005-09-02 20:32:45 +00002647 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2648 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002649
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002650 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002651 case ISD::CopyFromReg:
2652 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002653 default:
2654 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2655 assert(0 && "Do not know how to promote this operator!");
2656 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002657 case ISD::UNDEF:
2658 Result = DAG.getNode(ISD::UNDEF, NVT);
2659 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002660 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002661 if (VT != MVT::i1)
2662 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2663 else
2664 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002665 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2666 break;
2667 case ISD::ConstantFP:
2668 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2669 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2670 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002671
Chris Lattner2cb338d2005-01-18 02:59:52 +00002672 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002673 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002674 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2675 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002676 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002677
2678 case ISD::TRUNCATE:
2679 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2680 case Legal:
2681 Result = LegalizeOp(Node->getOperand(0));
2682 assert(Result.getValueType() >= NVT &&
2683 "This truncation doesn't make sense!");
2684 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2685 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2686 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002687 case Promote:
2688 // The truncation is not required, because we don't guarantee anything
2689 // about high bits anyway.
2690 Result = PromoteOp(Node->getOperand(0));
2691 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002692 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002693 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2694 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002695 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002696 }
2697 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002698 case ISD::SIGN_EXTEND:
2699 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002700 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002701 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2702 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2703 case Legal:
2704 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002705 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002706 break;
2707 case Promote:
2708 // Promote the reg if it's smaller.
2709 Result = PromoteOp(Node->getOperand(0));
2710 // The high bits are not guaranteed to be anything. Insert an extend.
2711 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002712 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002713 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002714 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002715 Result = DAG.getZeroExtendInReg(Result,
2716 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002717 break;
2718 }
2719 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002720 case ISD::BIT_CONVERT:
2721 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2722 Result = PromoteOp(Result);
2723 break;
2724
Chris Lattner4d978642005-01-15 22:16:26 +00002725 case ISD::FP_EXTEND:
2726 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2727 case ISD::FP_ROUND:
2728 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2729 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2730 case Promote: assert(0 && "Unreachable with 2 FP types!");
2731 case Legal:
2732 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002733 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002734 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002735 break;
2736 }
2737 break;
2738
2739 case ISD::SINT_TO_FP:
2740 case ISD::UINT_TO_FP:
2741 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2742 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002743 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002744 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002745 break;
2746
2747 case Promote:
2748 Result = PromoteOp(Node->getOperand(0));
2749 if (Node->getOpcode() == ISD::SINT_TO_FP)
2750 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002751 Result,
2752 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002753 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002754 Result = DAG.getZeroExtendInReg(Result,
2755 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002756 // No extra round required here.
2757 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002758 break;
2759 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002760 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2761 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002762 // Round if we cannot tolerate excess precision.
2763 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002764 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2765 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002766 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002767 }
Chris Lattner4d978642005-01-15 22:16:26 +00002768 break;
2769
Chris Lattner268d4572005-12-09 17:32:47 +00002770 case ISD::SIGN_EXTEND_INREG:
2771 Result = PromoteOp(Node->getOperand(0));
2772 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2773 Node->getOperand(1));
2774 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002775 case ISD::FP_TO_SINT:
2776 case ISD::FP_TO_UINT:
2777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2778 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002779 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002780 break;
2781 case Promote:
2782 // The input result is prerounded, so we don't have to do anything
2783 // special.
2784 Tmp1 = PromoteOp(Node->getOperand(0));
2785 break;
2786 case Expand:
2787 assert(0 && "not implemented");
2788 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002789 // If we're promoting a UINT to a larger size, check to see if the new node
2790 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2791 // we can use that instead. This allows us to generate better code for
2792 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2793 // legal, such as PowerPC.
2794 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002795 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002796 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2797 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002798 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2799 } else {
2800 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2801 }
Chris Lattner4d978642005-01-15 22:16:26 +00002802 break;
2803
Chris Lattner13fe99c2005-04-02 05:00:07 +00002804 case ISD::FABS:
2805 case ISD::FNEG:
2806 Tmp1 = PromoteOp(Node->getOperand(0));
2807 assert(Tmp1.getValueType() == NVT);
2808 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2809 // NOTE: we do not have to do any extra rounding here for
2810 // NoExcessFPPrecision, because we know the input will have the appropriate
2811 // precision, and these operations don't modify precision at all.
2812 break;
2813
Chris Lattner9d6fa982005-04-28 21:44:33 +00002814 case ISD::FSQRT:
2815 case ISD::FSIN:
2816 case ISD::FCOS:
2817 Tmp1 = PromoteOp(Node->getOperand(0));
2818 assert(Tmp1.getValueType() == NVT);
2819 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002820 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002821 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2822 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002823 break;
2824
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002825 case ISD::AND:
2826 case ISD::OR:
2827 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002828 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002829 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002830 case ISD::MUL:
2831 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002832 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002833 // that too is okay if they are integer operations.
2834 Tmp1 = PromoteOp(Node->getOperand(0));
2835 Tmp2 = PromoteOp(Node->getOperand(1));
2836 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2837 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002838 break;
2839 case ISD::FADD:
2840 case ISD::FSUB:
2841 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002842 Tmp1 = PromoteOp(Node->getOperand(0));
2843 Tmp2 = PromoteOp(Node->getOperand(1));
2844 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2845 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2846
2847 // Floating point operations will give excess precision that we may not be
2848 // able to tolerate. If we DO allow excess precision, just leave it,
2849 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002850 // FIXME: Why would we need to round FP ops more than integer ones?
2851 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002852 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002853 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2854 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002855 break;
2856
Chris Lattner4d978642005-01-15 22:16:26 +00002857 case ISD::SDIV:
2858 case ISD::SREM:
2859 // These operators require that their input be sign extended.
2860 Tmp1 = PromoteOp(Node->getOperand(0));
2861 Tmp2 = PromoteOp(Node->getOperand(1));
2862 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002863 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2864 DAG.getValueType(VT));
2865 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2866 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002867 }
2868 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2869
2870 // Perform FP_ROUND: this is probably overly pessimistic.
2871 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002872 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2873 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002874 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002875 case ISD::FDIV:
2876 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002877 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002878 // These operators require that their input be fp extended.
2879 Tmp1 = PromoteOp(Node->getOperand(0));
2880 Tmp2 = PromoteOp(Node->getOperand(1));
2881 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2882
2883 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002884 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002885 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2886 DAG.getValueType(VT));
2887 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002888
2889 case ISD::UDIV:
2890 case ISD::UREM:
2891 // These operators require that their input be zero extended.
2892 Tmp1 = PromoteOp(Node->getOperand(0));
2893 Tmp2 = PromoteOp(Node->getOperand(1));
2894 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002895 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2896 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002897 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2898 break;
2899
2900 case ISD::SHL:
2901 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002902 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002903 break;
2904 case ISD::SRA:
2905 // The input value must be properly sign extended.
2906 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002907 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2908 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002909 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002910 break;
2911 case ISD::SRL:
2912 // The input value must be properly zero extended.
2913 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002914 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002915 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002916 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002917
2918 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002919 Tmp1 = Node->getOperand(0); // Get the chain.
2920 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002921 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2922 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2923 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2924 } else {
2925 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2926 Node->getOperand(2));
2927 // Increment the pointer, VAList, to the next vaarg
2928 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2929 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2930 TLI.getPointerTy()));
2931 // Store the incremented VAList to the legalized pointer
2932 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2933 Node->getOperand(2));
2934 // Load the actual argument out of the pointer VAList
2935 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2936 DAG.getSrcValue(0), VT);
2937 }
2938 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002939 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002940 break;
2941
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002942 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002943 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2944 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002945 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002946 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002947 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002948 case ISD::SEXTLOAD:
2949 case ISD::ZEXTLOAD:
2950 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002951 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2952 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002953 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002954 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002955 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002956 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002957 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002958 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2959 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002960 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002961 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002962 case ISD::SELECT_CC:
2963 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2964 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2965 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002966 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002967 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002968 case ISD::BSWAP:
2969 Tmp1 = Node->getOperand(0);
2970 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2971 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2972 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2973 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2974 TLI.getShiftAmountTy()));
2975 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002976 case ISD::CTPOP:
2977 case ISD::CTTZ:
2978 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002979 // Zero extend the argument
2980 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002981 // Perform the larger operation, then subtract if needed.
2982 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002983 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002984 case ISD::CTPOP:
2985 Result = Tmp1;
2986 break;
2987 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002988 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002989 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002990 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002991 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002992 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002993 break;
2994 case ISD::CTLZ:
2995 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002996 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2997 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002998 getSizeInBits(VT), NVT));
2999 break;
3000 }
3001 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003002 }
3003
3004 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003005
3006 // Make sure the result is itself legal.
3007 Result = LegalizeOp(Result);
3008
3009 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003010 AddPromotedOperand(Op, Result);
3011 return Result;
3012}
Chris Lattnerdc750592005-01-07 07:47:09 +00003013
Nate Begeman7e7f4392006-02-01 07:19:44 +00003014/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3015/// with condition CC on the current target. This usually involves legalizing
3016/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3017/// there may be no choice but to create a new SetCC node to represent the
3018/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3019/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3020void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3021 SDOperand &RHS,
3022 SDOperand &CC) {
3023 SDOperand Tmp1, Tmp2, Result;
3024
3025 switch (getTypeAction(LHS.getValueType())) {
3026 case Legal:
3027 Tmp1 = LegalizeOp(LHS); // LHS
3028 Tmp2 = LegalizeOp(RHS); // RHS
3029 break;
3030 case Promote:
3031 Tmp1 = PromoteOp(LHS); // LHS
3032 Tmp2 = PromoteOp(RHS); // RHS
3033
3034 // If this is an FP compare, the operands have already been extended.
3035 if (MVT::isInteger(LHS.getValueType())) {
3036 MVT::ValueType VT = LHS.getValueType();
3037 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3038
3039 // Otherwise, we have to insert explicit sign or zero extends. Note
3040 // that we could insert sign extends for ALL conditions, but zero extend
3041 // is cheaper on many machines (an AND instead of two shifts), so prefer
3042 // it.
3043 switch (cast<CondCodeSDNode>(CC)->get()) {
3044 default: assert(0 && "Unknown integer comparison!");
3045 case ISD::SETEQ:
3046 case ISD::SETNE:
3047 case ISD::SETUGE:
3048 case ISD::SETUGT:
3049 case ISD::SETULE:
3050 case ISD::SETULT:
3051 // ALL of these operations will work if we either sign or zero extend
3052 // the operands (including the unsigned comparisons!). Zero extend is
3053 // usually a simpler/cheaper operation, so prefer it.
3054 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3055 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3056 break;
3057 case ISD::SETGE:
3058 case ISD::SETGT:
3059 case ISD::SETLT:
3060 case ISD::SETLE:
3061 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3062 DAG.getValueType(VT));
3063 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3064 DAG.getValueType(VT));
3065 break;
3066 }
3067 }
3068 break;
3069 case Expand:
3070 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3071 ExpandOp(LHS, LHSLo, LHSHi);
3072 ExpandOp(RHS, RHSLo, RHSHi);
3073 switch (cast<CondCodeSDNode>(CC)->get()) {
3074 case ISD::SETEQ:
3075 case ISD::SETNE:
3076 if (RHSLo == RHSHi)
3077 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3078 if (RHSCST->isAllOnesValue()) {
3079 // Comparison to -1.
3080 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3081 Tmp2 = RHSLo;
3082 break;
3083 }
3084
3085 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3086 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3087 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3088 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3089 break;
3090 default:
3091 // If this is a comparison of the sign bit, just look at the top part.
3092 // X > -1, x < 0
3093 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3094 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3095 CST->getValue() == 0) || // X < 0
3096 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3097 CST->isAllOnesValue())) { // X > -1
3098 Tmp1 = LHSHi;
3099 Tmp2 = RHSHi;
3100 break;
3101 }
3102
3103 // FIXME: This generated code sucks.
3104 ISD::CondCode LowCC;
3105 switch (cast<CondCodeSDNode>(CC)->get()) {
3106 default: assert(0 && "Unknown integer setcc!");
3107 case ISD::SETLT:
3108 case ISD::SETULT: LowCC = ISD::SETULT; break;
3109 case ISD::SETGT:
3110 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3111 case ISD::SETLE:
3112 case ISD::SETULE: LowCC = ISD::SETULE; break;
3113 case ISD::SETGE:
3114 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3115 }
3116
3117 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3118 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3119 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3120
3121 // NOTE: on targets without efficient SELECT of bools, we can always use
3122 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3123 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3124 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3125 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3126 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3127 Result, Tmp1, Tmp2));
3128 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003129 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003130 }
3131 }
3132 LHS = Tmp1;
3133 RHS = Tmp2;
3134}
3135
Chris Lattner36e663d2005-12-23 00:16:34 +00003136/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003137/// The resultant code need not be legal. Note that SrcOp is the input operand
3138/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003139SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3140 SDOperand SrcOp) {
3141 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003142 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003143
3144 // Emit a store to the stack slot.
3145 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003146 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003147 // Result is a load from the stack slot.
3148 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3149}
3150
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003151/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3152/// support the operation, but do support the resultant packed vector type.
3153SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3154
3155 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003156 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003157 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003158 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003159 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003160 std::map<SDOperand, std::vector<unsigned> > Values;
3161 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003162 bool isConstant = true;
3163 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3164 SplatValue.getOpcode() != ISD::UNDEF)
3165 isConstant = false;
3166
Evan Cheng1d2e9952006-03-24 01:17:21 +00003167 for (unsigned i = 1; i < NumElems; ++i) {
3168 SDOperand V = Node->getOperand(i);
3169 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3170 if (I != Values.end())
3171 I->second.push_back(i);
3172 else
3173 Values[V].push_back(i);
3174 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003175 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003176 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003177 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003178
3179 // If this isn't a constant element or an undef, we can't use a constant
3180 // pool load.
3181 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3182 V.getOpcode() != ISD::UNDEF)
3183 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003184 }
3185
3186 if (isOnlyLowElement) {
3187 // If the low element is an undef too, then this whole things is an undef.
3188 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3189 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3190 // Otherwise, turn this into a scalar_to_vector node.
3191 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3192 Node->getOperand(0));
3193 }
3194
Chris Lattner77e271c2006-03-24 07:29:17 +00003195 // If all elements are constants, create a load from the constant pool.
3196 if (isConstant) {
3197 MVT::ValueType VT = Node->getValueType(0);
3198 const Type *OpNTy =
3199 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3200 std::vector<Constant*> CV;
3201 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3202 if (ConstantFPSDNode *V =
3203 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3204 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3205 } else if (ConstantSDNode *V =
3206 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3207 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3208 } else {
3209 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3210 CV.push_back(UndefValue::get(OpNTy));
3211 }
3212 }
3213 Constant *CP = ConstantPacked::get(CV);
3214 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3215 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3216 DAG.getSrcValue(NULL));
3217 }
3218
Chris Lattner21e68c82006-03-20 01:52:29 +00003219 if (SplatValue.Val) { // Splat of one value?
3220 // Build the shuffle constant vector: <0, 0, 0, 0>
3221 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003222 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003223 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003224 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003225 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3226
3227 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3228 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3229 // Get the splatted value into the low element of a vector register.
3230 SDOperand LowValVec =
3231 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3232
3233 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3234 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3235 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3236 SplatMask);
3237 }
3238 }
3239
Evan Cheng1d2e9952006-03-24 01:17:21 +00003240 // If there are only two unique elements, we may be able to turn this into a
3241 // vector shuffle.
3242 if (Values.size() == 2) {
3243 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3244 MVT::ValueType MaskVT =
3245 MVT::getIntVectorWithNumElements(NumElems);
3246 std::vector<SDOperand> MaskVec(NumElems);
3247 unsigned i = 0;
3248 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3249 E = Values.end(); I != E; ++I) {
3250 for (std::vector<unsigned>::iterator II = I->second.begin(),
3251 EE = I->second.end(); II != EE; ++II)
3252 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3253 i += NumElems;
3254 }
3255 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3256
3257 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Evan Cheng68d9bf22006-03-24 18:45:20 +00003258 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask) &&
3259 TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0))) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003260 std::vector<SDOperand> Ops;
3261 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3262 E = Values.end(); I != E; ++I) {
3263 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3264 I->first);
3265 Ops.push_back(Op);
3266 }
3267 Ops.push_back(ShuffleMask);
3268
3269 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3270 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3271 }
3272 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003273
3274 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3275 // aligned object on the stack, store each element into it, then load
3276 // the result as a vector.
3277 MVT::ValueType VT = Node->getValueType(0);
3278 // Create the stack frame object.
3279 SDOperand FIPtr = CreateStackTemporary(VT);
3280
3281 // Emit a store of each element to the stack slot.
3282 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003283 unsigned TypeByteSize =
3284 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3285 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3286 // Store (in the right endianness) the elements to memory.
3287 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3288 // Ignore undef elements.
3289 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3290
Chris Lattner8fa445a2006-03-22 01:46:54 +00003291 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003292
3293 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3294 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3295
3296 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3297 Node->getOperand(i), Idx,
3298 DAG.getSrcValue(NULL)));
3299 }
3300
3301 SDOperand StoreChain;
3302 if (!Stores.empty()) // Not all undef elements?
3303 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3304 else
3305 StoreChain = DAG.getEntryNode();
3306
3307 // Result is a load from the stack slot.
3308 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3309}
3310
3311/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3312/// specified value type.
3313SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3314 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3315 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3316 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3317 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3318}
3319
Chris Lattner4157c412005-04-02 04:00:59 +00003320void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3321 SDOperand Op, SDOperand Amt,
3322 SDOperand &Lo, SDOperand &Hi) {
3323 // Expand the subcomponents.
3324 SDOperand LHSL, LHSH;
3325 ExpandOp(Op, LHSL, LHSH);
3326
3327 std::vector<SDOperand> Ops;
3328 Ops.push_back(LHSL);
3329 Ops.push_back(LHSH);
3330 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003331 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003332 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003333 Hi = Lo.getValue(1);
3334}
3335
3336
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003337/// ExpandShift - Try to find a clever way to expand this shift operation out to
3338/// smaller elements. If we can't find a way that is more efficient than a
3339/// libcall on this target, return false. Otherwise, return true with the
3340/// low-parts expanded into Lo and Hi.
3341bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3342 SDOperand &Lo, SDOperand &Hi) {
3343 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3344 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003345
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003346 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003347 SDOperand ShAmt = LegalizeOp(Amt);
3348 MVT::ValueType ShTy = ShAmt.getValueType();
3349 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3350 unsigned NVTBits = MVT::getSizeInBits(NVT);
3351
3352 // Handle the case when Amt is an immediate. Other cases are currently broken
3353 // and are disabled.
3354 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3355 unsigned Cst = CN->getValue();
3356 // Expand the incoming operand to be shifted, so that we have its parts
3357 SDOperand InL, InH;
3358 ExpandOp(Op, InL, InH);
3359 switch(Opc) {
3360 case ISD::SHL:
3361 if (Cst > VTBits) {
3362 Lo = DAG.getConstant(0, NVT);
3363 Hi = DAG.getConstant(0, NVT);
3364 } else if (Cst > NVTBits) {
3365 Lo = DAG.getConstant(0, NVT);
3366 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003367 } else if (Cst == NVTBits) {
3368 Lo = DAG.getConstant(0, NVT);
3369 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003370 } else {
3371 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3372 Hi = DAG.getNode(ISD::OR, NVT,
3373 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3374 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3375 }
3376 return true;
3377 case ISD::SRL:
3378 if (Cst > VTBits) {
3379 Lo = DAG.getConstant(0, NVT);
3380 Hi = DAG.getConstant(0, NVT);
3381 } else if (Cst > NVTBits) {
3382 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3383 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003384 } else if (Cst == NVTBits) {
3385 Lo = InH;
3386 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003387 } else {
3388 Lo = DAG.getNode(ISD::OR, NVT,
3389 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3390 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3391 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3392 }
3393 return true;
3394 case ISD::SRA:
3395 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003396 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003397 DAG.getConstant(NVTBits-1, ShTy));
3398 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003399 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003400 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003401 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003402 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003403 } else if (Cst == NVTBits) {
3404 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003405 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003406 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003407 } else {
3408 Lo = DAG.getNode(ISD::OR, NVT,
3409 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3410 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3411 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3412 }
3413 return true;
3414 }
3415 }
Nate Begemanb0674922005-04-06 21:13:14 +00003416 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003417}
Chris Lattneraac464e2005-01-21 06:05:23 +00003418
Chris Lattner4add7e32005-01-23 04:42:50 +00003419
Chris Lattneraac464e2005-01-21 06:05:23 +00003420// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3421// does not fit into a register, return the lo part and set the hi part to the
3422// by-reg argument. If it does fit into a single register, return the result
3423// and leave the Hi part unset.
3424SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3425 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003426 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3427 // The input chain to this libcall is the entry node of the function.
3428 // Legalizing the call will automatically add the previous call to the
3429 // dependence.
3430 SDOperand InChain = DAG.getEntryNode();
3431
Chris Lattneraac464e2005-01-21 06:05:23 +00003432 TargetLowering::ArgListTy Args;
3433 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3434 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3435 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3436 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3437 }
3438 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003439
Chris Lattner06bbeb62005-05-11 19:02:11 +00003440 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003441 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003442 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003443 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3444 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003445
Chris Lattner462505f2006-02-13 09:18:02 +00003446 // Legalize the call sequence, starting with the chain. This will advance
3447 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3448 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3449 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003450 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003451 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003452 default: assert(0 && "Unknown thing");
3453 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003454 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003455 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003456 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003457 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003458 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003459 }
Chris Lattner63022662005-09-02 20:26:58 +00003460 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003461}
3462
Chris Lattner4add7e32005-01-23 04:42:50 +00003463
Chris Lattneraac464e2005-01-21 06:05:23 +00003464/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3465/// destination type is legal.
3466SDOperand SelectionDAGLegalize::
3467ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003468 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003469 assert(getTypeAction(Source.getValueType()) == Expand &&
3470 "This is not an expansion!");
3471 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3472
Chris Lattner06bbeb62005-05-11 19:02:11 +00003473 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003474 assert(Source.getValueType() == MVT::i64 &&
3475 "This only works for 64-bit -> FP");
3476 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3477 // incoming integer is set. To handle this, we dynamically test to see if
3478 // it is set, and, if so, add a fudge factor.
3479 SDOperand Lo, Hi;
3480 ExpandOp(Source, Lo, Hi);
3481
Chris Lattner2a4f7312005-05-13 04:45:13 +00003482 // If this is unsigned, and not supported, first perform the conversion to
3483 // signed, then adjust the result if the sign bit is set.
3484 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3485 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3486
Chris Lattnerd47675e2005-08-09 20:20:18 +00003487 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3488 DAG.getConstant(0, Hi.getValueType()),
3489 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003490 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3491 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3492 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003493 uint64_t FF = 0x5f800000ULL;
3494 if (TLI.isLittleEndian()) FF <<= 32;
3495 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003496
Chris Lattnerc30405e2005-08-26 17:15:30 +00003497 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003498 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3499 SDOperand FudgeInReg;
3500 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003501 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3502 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003503 else {
3504 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003505 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3506 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003507 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003508 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003509 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003510
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003511 // Check to see if the target has a custom way to lower this. If so, use it.
3512 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3513 default: assert(0 && "This action not implemented for this operation!");
3514 case TargetLowering::Legal:
3515 case TargetLowering::Expand:
3516 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003517 case TargetLowering::Custom: {
3518 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3519 Source), DAG);
3520 if (NV.Val)
3521 return LegalizeOp(NV);
3522 break; // The target decided this was legal after all
3523 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003524 }
3525
Chris Lattner153587e2005-05-12 07:00:44 +00003526 // Expand the source, then glue it back together for the call. We must expand
3527 // the source in case it is shared (this pass of legalize must traverse it).
3528 SDOperand SrcLo, SrcHi;
3529 ExpandOp(Source, SrcLo, SrcHi);
3530 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3531
Chris Lattner06bbeb62005-05-11 19:02:11 +00003532 const char *FnName = 0;
3533 if (DestTy == MVT::f32)
3534 FnName = "__floatdisf";
3535 else {
3536 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3537 FnName = "__floatdidf";
3538 }
Chris Lattner462505f2006-02-13 09:18:02 +00003539
3540 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3541 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003542 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003543}
Misha Brukman835702a2005-04-21 22:36:52 +00003544
Chris Lattner689bdcc2006-01-28 08:25:58 +00003545/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3546/// INT_TO_FP operation of the specified operand when the target requests that
3547/// we expand it. At this point, we know that the result and operand types are
3548/// legal for the target.
3549SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3550 SDOperand Op0,
3551 MVT::ValueType DestVT) {
3552 if (Op0.getValueType() == MVT::i32) {
3553 // simple 32-bit [signed|unsigned] integer to float/double expansion
3554
3555 // get the stack frame index of a 8 byte buffer
3556 MachineFunction &MF = DAG.getMachineFunction();
3557 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3558 // get address of 8 byte buffer
3559 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3560 // word offset constant for Hi/Lo address computation
3561 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3562 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003563 SDOperand Hi = StackSlot;
3564 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3565 if (TLI.isLittleEndian())
3566 std::swap(Hi, Lo);
3567
Chris Lattner689bdcc2006-01-28 08:25:58 +00003568 // if signed map to unsigned space
3569 SDOperand Op0Mapped;
3570 if (isSigned) {
3571 // constant used to invert sign bit (signed to unsigned mapping)
3572 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3573 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3574 } else {
3575 Op0Mapped = Op0;
3576 }
3577 // store the lo of the constructed double - based on integer input
3578 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3579 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3580 // initial hi portion of constructed double
3581 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3582 // store the hi of the constructed double - biased exponent
3583 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3584 InitialHi, Hi, DAG.getSrcValue(NULL));
3585 // load the constructed double
3586 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3587 DAG.getSrcValue(NULL));
3588 // FP constant to bias correct the final result
3589 SDOperand Bias = DAG.getConstantFP(isSigned ?
3590 BitsToDouble(0x4330000080000000ULL)
3591 : BitsToDouble(0x4330000000000000ULL),
3592 MVT::f64);
3593 // subtract the bias
3594 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3595 // final result
3596 SDOperand Result;
3597 // handle final rounding
3598 if (DestVT == MVT::f64) {
3599 // do nothing
3600 Result = Sub;
3601 } else {
3602 // if f32 then cast to f32
3603 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3604 }
3605 return Result;
3606 }
3607 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3608 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3609
3610 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3611 DAG.getConstant(0, Op0.getValueType()),
3612 ISD::SETLT);
3613 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3614 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3615 SignSet, Four, Zero);
3616
3617 // If the sign bit of the integer is set, the large number will be treated
3618 // as a negative number. To counteract this, the dynamic code adds an
3619 // offset depending on the data type.
3620 uint64_t FF;
3621 switch (Op0.getValueType()) {
3622 default: assert(0 && "Unsupported integer type!");
3623 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3624 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3625 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3626 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3627 }
3628 if (TLI.isLittleEndian()) FF <<= 32;
3629 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3630
3631 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3632 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3633 SDOperand FudgeInReg;
3634 if (DestVT == MVT::f32)
3635 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3636 DAG.getSrcValue(NULL));
3637 else {
3638 assert(DestVT == MVT::f64 && "Unexpected conversion");
3639 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3640 DAG.getEntryNode(), CPIdx,
3641 DAG.getSrcValue(NULL), MVT::f32));
3642 }
3643
3644 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3645}
3646
3647/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3648/// *INT_TO_FP operation of the specified operand when the target requests that
3649/// we promote it. At this point, we know that the result and operand types are
3650/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3651/// operation that takes a larger input.
3652SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3653 MVT::ValueType DestVT,
3654 bool isSigned) {
3655 // First step, figure out the appropriate *INT_TO_FP operation to use.
3656 MVT::ValueType NewInTy = LegalOp.getValueType();
3657
3658 unsigned OpToUse = 0;
3659
3660 // Scan for the appropriate larger type to use.
3661 while (1) {
3662 NewInTy = (MVT::ValueType)(NewInTy+1);
3663 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3664
3665 // If the target supports SINT_TO_FP of this type, use it.
3666 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3667 default: break;
3668 case TargetLowering::Legal:
3669 if (!TLI.isTypeLegal(NewInTy))
3670 break; // Can't use this datatype.
3671 // FALL THROUGH.
3672 case TargetLowering::Custom:
3673 OpToUse = ISD::SINT_TO_FP;
3674 break;
3675 }
3676 if (OpToUse) break;
3677 if (isSigned) continue;
3678
3679 // If the target supports UINT_TO_FP of this type, use it.
3680 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3681 default: break;
3682 case TargetLowering::Legal:
3683 if (!TLI.isTypeLegal(NewInTy))
3684 break; // Can't use this datatype.
3685 // FALL THROUGH.
3686 case TargetLowering::Custom:
3687 OpToUse = ISD::UINT_TO_FP;
3688 break;
3689 }
3690 if (OpToUse) break;
3691
3692 // Otherwise, try a larger type.
3693 }
3694
3695 // Okay, we found the operation and type to use. Zero extend our input to the
3696 // desired type then run the operation on it.
3697 return DAG.getNode(OpToUse, DestVT,
3698 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3699 NewInTy, LegalOp));
3700}
3701
3702/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3703/// FP_TO_*INT operation of the specified operand when the target requests that
3704/// we promote it. At this point, we know that the result and operand types are
3705/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3706/// operation that returns a larger result.
3707SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3708 MVT::ValueType DestVT,
3709 bool isSigned) {
3710 // First step, figure out the appropriate FP_TO*INT operation to use.
3711 MVT::ValueType NewOutTy = DestVT;
3712
3713 unsigned OpToUse = 0;
3714
3715 // Scan for the appropriate larger type to use.
3716 while (1) {
3717 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3718 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3719
3720 // If the target supports FP_TO_SINT returning this type, use it.
3721 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3722 default: break;
3723 case TargetLowering::Legal:
3724 if (!TLI.isTypeLegal(NewOutTy))
3725 break; // Can't use this datatype.
3726 // FALL THROUGH.
3727 case TargetLowering::Custom:
3728 OpToUse = ISD::FP_TO_SINT;
3729 break;
3730 }
3731 if (OpToUse) break;
3732
3733 // If the target supports FP_TO_UINT of this type, use it.
3734 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3735 default: break;
3736 case TargetLowering::Legal:
3737 if (!TLI.isTypeLegal(NewOutTy))
3738 break; // Can't use this datatype.
3739 // FALL THROUGH.
3740 case TargetLowering::Custom:
3741 OpToUse = ISD::FP_TO_UINT;
3742 break;
3743 }
3744 if (OpToUse) break;
3745
3746 // Otherwise, try a larger type.
3747 }
3748
3749 // Okay, we found the operation and type to use. Truncate the result of the
3750 // extended FP_TO_*INT operation to the desired size.
3751 return DAG.getNode(ISD::TRUNCATE, DestVT,
3752 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3753}
3754
3755/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3756///
3757SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3758 MVT::ValueType VT = Op.getValueType();
3759 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3760 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3761 switch (VT) {
3762 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3763 case MVT::i16:
3764 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3765 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3766 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3767 case MVT::i32:
3768 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3769 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3770 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3771 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3772 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3773 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3774 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3775 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3776 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3777 case MVT::i64:
3778 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3779 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3780 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3781 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3782 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3783 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3784 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3785 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3786 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3787 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3788 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3789 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3790 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3791 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3792 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3793 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3794 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3795 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3796 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3797 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3798 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3799 }
3800}
3801
3802/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3803///
3804SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3805 switch (Opc) {
3806 default: assert(0 && "Cannot expand this yet!");
3807 case ISD::CTPOP: {
3808 static const uint64_t mask[6] = {
3809 0x5555555555555555ULL, 0x3333333333333333ULL,
3810 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3811 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3812 };
3813 MVT::ValueType VT = Op.getValueType();
3814 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3815 unsigned len = getSizeInBits(VT);
3816 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3817 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3818 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3819 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3820 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3821 DAG.getNode(ISD::AND, VT,
3822 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3823 }
3824 return Op;
3825 }
3826 case ISD::CTLZ: {
3827 // for now, we do this:
3828 // x = x | (x >> 1);
3829 // x = x | (x >> 2);
3830 // ...
3831 // x = x | (x >>16);
3832 // x = x | (x >>32); // for 64-bit input
3833 // return popcount(~x);
3834 //
3835 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3836 MVT::ValueType VT = Op.getValueType();
3837 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3838 unsigned len = getSizeInBits(VT);
3839 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3840 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3841 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3842 }
3843 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3844 return DAG.getNode(ISD::CTPOP, VT, Op);
3845 }
3846 case ISD::CTTZ: {
3847 // for now, we use: { return popcount(~x & (x - 1)); }
3848 // unless the target has ctlz but not ctpop, in which case we use:
3849 // { return 32 - nlz(~x & (x-1)); }
3850 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3851 MVT::ValueType VT = Op.getValueType();
3852 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3853 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3854 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3855 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3856 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3857 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3858 TLI.isOperationLegal(ISD::CTLZ, VT))
3859 return DAG.getNode(ISD::SUB, VT,
3860 DAG.getConstant(getSizeInBits(VT), VT),
3861 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3862 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3863 }
3864 }
3865}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003866
3867
Chris Lattnerdc750592005-01-07 07:47:09 +00003868/// ExpandOp - Expand the specified SDOperand into its two component pieces
3869/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3870/// LegalizeNodes map is filled in for any results that are not expanded, the
3871/// ExpandedNodes map is filled in for any results that are expanded, and the
3872/// Lo/Hi values are returned.
3873void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3874 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003875 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003876 SDNode *Node = Op.Val;
3877 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003878 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3879 "Cannot expand FP values!");
3880 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003881 "Cannot expand to FP value or to larger int value!");
3882
Chris Lattner1a570f12005-09-02 20:32:45 +00003883 // See if we already expanded it.
3884 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3885 = ExpandedNodes.find(Op);
3886 if (I != ExpandedNodes.end()) {
3887 Lo = I->second.first;
3888 Hi = I->second.second;
3889 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003890 }
3891
Chris Lattnerdc750592005-01-07 07:47:09 +00003892 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003893 case ISD::CopyFromReg:
3894 assert(0 && "CopyFromReg must be legal!");
3895 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003896 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3897 assert(0 && "Do not know how to expand this operator!");
3898 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003899 case ISD::UNDEF:
3900 Lo = DAG.getNode(ISD::UNDEF, NVT);
3901 Hi = DAG.getNode(ISD::UNDEF, NVT);
3902 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003903 case ISD::Constant: {
3904 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3905 Lo = DAG.getConstant(Cst, NVT);
3906 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3907 break;
3908 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003909 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003910 // Return the operands.
3911 Lo = Node->getOperand(0);
3912 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003913 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003914
3915 case ISD::SIGN_EXTEND_INREG:
3916 ExpandOp(Node->getOperand(0), Lo, Hi);
3917 // Sign extend the lo-part.
3918 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3919 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3920 TLI.getShiftAmountTy()));
3921 // sext_inreg the low part if needed.
3922 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3923 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003924
Nate Begeman2fba8a32006-01-14 03:14:10 +00003925 case ISD::BSWAP: {
3926 ExpandOp(Node->getOperand(0), Lo, Hi);
3927 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3928 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3929 Lo = TempLo;
3930 break;
3931 }
3932
Chris Lattner55e9cde2005-05-11 04:51:16 +00003933 case ISD::CTPOP:
3934 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003935 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3936 DAG.getNode(ISD::CTPOP, NVT, Lo),
3937 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003938 Hi = DAG.getConstant(0, NVT);
3939 break;
3940
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003941 case ISD::CTLZ: {
3942 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003943 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003944 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3945 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003946 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3947 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003948 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3949 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3950
3951 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3952 Hi = DAG.getConstant(0, NVT);
3953 break;
3954 }
3955
3956 case ISD::CTTZ: {
3957 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003958 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003959 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3960 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003961 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3962 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003963 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3964 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3965
3966 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3967 Hi = DAG.getConstant(0, NVT);
3968 break;
3969 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003970
Nate Begemane74795c2006-01-25 18:21:52 +00003971 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003972 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3973 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003974 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3975 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3976
3977 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003978 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003979 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3980 if (!TLI.isLittleEndian())
3981 std::swap(Lo, Hi);
3982 break;
3983 }
3984
Chris Lattnerdc750592005-01-07 07:47:09 +00003985 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003986 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3987 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003988 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003989
3990 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003991 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003992 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3993 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003994 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003995 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003996
3997 // Build a factor node to remember that this load is independent of the
3998 // other one.
3999 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4000 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00004001
Chris Lattnerdc750592005-01-07 07:47:09 +00004002 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004003 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00004004 if (!TLI.isLittleEndian())
4005 std::swap(Lo, Hi);
4006 break;
4007 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004008 case ISD::AND:
4009 case ISD::OR:
4010 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4011 SDOperand LL, LH, RL, RH;
4012 ExpandOp(Node->getOperand(0), LL, LH);
4013 ExpandOp(Node->getOperand(1), RL, RH);
4014 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4015 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4016 break;
4017 }
4018 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004019 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004020 ExpandOp(Node->getOperand(1), LL, LH);
4021 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004022 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4023 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004024 break;
4025 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004026 case ISD::SELECT_CC: {
4027 SDOperand TL, TH, FL, FH;
4028 ExpandOp(Node->getOperand(2), TL, TH);
4029 ExpandOp(Node->getOperand(3), FL, FH);
4030 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4031 Node->getOperand(1), TL, FL, Node->getOperand(4));
4032 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4033 Node->getOperand(1), TH, FH, Node->getOperand(4));
4034 break;
4035 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00004036 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004037 SDOperand Chain = Node->getOperand(0);
4038 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004039 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4040
4041 if (EVT == NVT)
4042 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4043 else
4044 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4045 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004046
4047 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004048 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004049
Nate Begemanc3a89c52005-10-13 17:15:37 +00004050 // The high part is obtained by SRA'ing all but one of the bits of the lo
4051 // part.
4052 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4053 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4054 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004055 break;
4056 }
4057 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004058 SDOperand Chain = Node->getOperand(0);
4059 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004060 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4061
4062 if (EVT == NVT)
4063 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4064 else
4065 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4066 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004067
4068 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004069 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004070
Nate Begemanc3a89c52005-10-13 17:15:37 +00004071 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004072 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004073 break;
4074 }
4075 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004076 SDOperand Chain = Node->getOperand(0);
4077 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004078 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4079
4080 if (EVT == NVT)
4081 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4082 else
4083 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4084 EVT);
4085
4086 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004087 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004088
4089 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004090 Hi = DAG.getNode(ISD::UNDEF, NVT);
4091 break;
4092 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004093 case ISD::ANY_EXTEND:
4094 // The low part is any extension of the input (which degenerates to a copy).
4095 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4096 // The high part is undefined.
4097 Hi = DAG.getNode(ISD::UNDEF, NVT);
4098 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004099 case ISD::SIGN_EXTEND: {
4100 // The low part is just a sign extension of the input (which degenerates to
4101 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004102 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004103
Chris Lattnerdc750592005-01-07 07:47:09 +00004104 // The high part is obtained by SRA'ing all but one of the bits of the lo
4105 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004106 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004107 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4108 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004109 break;
4110 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004111 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004112 // The low part is just a zero extension of the input (which degenerates to
4113 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004114 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004115
Chris Lattnerdc750592005-01-07 07:47:09 +00004116 // The high part is just a zero.
4117 Hi = DAG.getConstant(0, NVT);
4118 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004119
4120 case ISD::BIT_CONVERT: {
4121 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4122 Node->getOperand(0));
4123 ExpandOp(Tmp, Lo, Hi);
4124 break;
4125 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004126
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004127 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004128 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4129 TargetLowering::Custom &&
4130 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004131 Lo = TLI.LowerOperation(Op, DAG);
4132 assert(Lo.Val && "Node must be custom expanded!");
4133 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004134 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004135 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004136 break;
4137
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004138 // These operators cannot be expanded directly, emit them as calls to
4139 // library functions.
4140 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004141 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004142 SDOperand Op;
4143 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4144 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004145 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4146 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004147 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004148
Chris Lattner941d84a2005-07-30 01:40:57 +00004149 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4150
Chris Lattnerfe68d752005-07-29 00:33:32 +00004151 // Now that the custom expander is done, expand the result, which is still
4152 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004153 if (Op.Val) {
4154 ExpandOp(Op, Lo, Hi);
4155 break;
4156 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004157 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004158
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004159 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004160 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004161 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004162 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004163 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004164
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004165 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004166 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004167 SDOperand Op;
4168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4169 case Expand: assert(0 && "cannot expand FP!");
4170 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4171 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4172 }
4173
4174 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4175
4176 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004177 if (Op.Val) {
4178 ExpandOp(Op, Lo, Hi);
4179 break;
4180 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004181 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004182
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004183 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004184 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004185 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004186 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004187 break;
4188
Evan Cheng870e4f82006-01-09 18:31:59 +00004189 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004190 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004191 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004192 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004193 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004194 Op = TLI.LowerOperation(Op, DAG);
4195 if (Op.Val) {
4196 // Now that the custom expander is done, expand the result, which is
4197 // still VT.
4198 ExpandOp(Op, Lo, Hi);
4199 break;
4200 }
4201 }
4202
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004203 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004204 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004205 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004206
4207 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004208 TargetLowering::LegalizeAction Action =
4209 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4210 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4211 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004212 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004213 break;
4214 }
4215
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004216 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004217 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004218 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004219 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004220
Evan Cheng870e4f82006-01-09 18:31:59 +00004221 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004222 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004223 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004224 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004225 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004226 Op = TLI.LowerOperation(Op, DAG);
4227 if (Op.Val) {
4228 // Now that the custom expander is done, expand the result, which is
4229 // still VT.
4230 ExpandOp(Op, Lo, Hi);
4231 break;
4232 }
4233 }
4234
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004235 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004236 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004237 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004238
4239 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004240 TargetLowering::LegalizeAction Action =
4241 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4242 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4243 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004244 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004245 break;
4246 }
4247
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004248 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004249 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004250 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004251 }
4252
4253 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004254 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004255 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004256 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004257 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004258 Op = TLI.LowerOperation(Op, DAG);
4259 if (Op.Val) {
4260 // Now that the custom expander is done, expand the result, which is
4261 // still VT.
4262 ExpandOp(Op, Lo, Hi);
4263 break;
4264 }
4265 }
4266
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004267 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004268 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004269 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004270
4271 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004272 TargetLowering::LegalizeAction Action =
4273 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4274 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4275 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004276 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004277 break;
4278 }
4279
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004280 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004281 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004282 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004283 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004284
Misha Brukman835702a2005-04-21 22:36:52 +00004285 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004286 case ISD::SUB: {
4287 // If the target wants to custom expand this, let them.
4288 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4289 TargetLowering::Custom) {
4290 Op = TLI.LowerOperation(Op, DAG);
4291 if (Op.Val) {
4292 ExpandOp(Op, Lo, Hi);
4293 break;
4294 }
4295 }
4296
4297 // Expand the subcomponents.
4298 SDOperand LHSL, LHSH, RHSL, RHSH;
4299 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4300 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004301 std::vector<MVT::ValueType> VTs;
4302 std::vector<SDOperand> LoOps, HiOps;
4303 VTs.push_back(LHSL.getValueType());
4304 VTs.push_back(MVT::Flag);
4305 LoOps.push_back(LHSL);
4306 LoOps.push_back(RHSL);
4307 HiOps.push_back(LHSH);
4308 HiOps.push_back(RHSH);
4309 if (Node->getOpcode() == ISD::ADD) {
4310 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4311 HiOps.push_back(Lo.getValue(1));
4312 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4313 } else {
4314 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4315 HiOps.push_back(Lo.getValue(1));
4316 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4317 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004318 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004319 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004320 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004321 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004322 SDOperand LL, LH, RL, RH;
4323 ExpandOp(Node->getOperand(0), LL, LH);
4324 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004325 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4326 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4327 // extended the sign bit of the low half through the upper half, and if so
4328 // emit a MULHS instead of the alternate sequence that is valid for any
4329 // i64 x i64 multiply.
4330 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4331 // is RH an extension of the sign bit of RL?
4332 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4333 RH.getOperand(1).getOpcode() == ISD::Constant &&
4334 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4335 // is LH an extension of the sign bit of LL?
4336 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4337 LH.getOperand(1).getOpcode() == ISD::Constant &&
4338 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4339 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4340 } else {
4341 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4342 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4343 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4344 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4345 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4346 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004347 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4348 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004349 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004350 }
4351 break;
4352 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004353 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4354 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4355 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4356 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004357 }
4358
Chris Lattnerac12f682005-12-21 18:02:52 +00004359 // Make sure the resultant values have been legalized themselves, unless this
4360 // is a type that requires multi-step expansion.
4361 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4362 Lo = LegalizeOp(Lo);
4363 Hi = LegalizeOp(Hi);
4364 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004365
4366 // Remember in a map if the values will be reused later.
4367 bool isNew =
4368 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4369 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004370}
4371
Chris Lattner32206f52006-03-18 01:44:44 +00004372/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4373/// two smaller values of MVT::Vector type.
4374void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4375 SDOperand &Hi) {
4376 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4377 SDNode *Node = Op.Val;
4378 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4379 assert(NumElements > 1 && "Cannot split a single element vector!");
4380 unsigned NewNumElts = NumElements/2;
4381 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4382 SDOperand TypeNode = *(Node->op_end()-1);
4383
4384 // See if we already split it.
4385 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4386 = SplitNodes.find(Op);
4387 if (I != SplitNodes.end()) {
4388 Lo = I->second.first;
4389 Hi = I->second.second;
4390 return;
4391 }
4392
4393 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004394 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004395 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004396 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4397 LoOps.push_back(NewNumEltsNode);
4398 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004399 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004400
4401 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4402 HiOps.push_back(NewNumEltsNode);
4403 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004404 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004405 break;
4406 }
4407 case ISD::VADD:
4408 case ISD::VSUB:
4409 case ISD::VMUL:
4410 case ISD::VSDIV:
4411 case ISD::VUDIV:
4412 case ISD::VAND:
4413 case ISD::VOR:
4414 case ISD::VXOR: {
4415 SDOperand LL, LH, RL, RH;
4416 SplitVectorOp(Node->getOperand(0), LL, LH);
4417 SplitVectorOp(Node->getOperand(1), RL, RH);
4418
4419 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4420 NewNumEltsNode, TypeNode);
4421 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4422 NewNumEltsNode, TypeNode);
4423 break;
4424 }
4425 case ISD::VLOAD: {
4426 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4427 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4428 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4429
4430 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4431 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4432 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4433 getIntPtrConstant(IncrementSize));
4434 // FIXME: This creates a bogus srcvalue!
4435 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4436
4437 // Build a factor node to remember that this load is independent of the
4438 // other one.
4439 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4440 Hi.getValue(1));
4441
4442 // Remember that we legalized the chain.
4443 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4444 if (!TLI.isLittleEndian())
4445 std::swap(Lo, Hi);
4446 break;
4447 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004448 case ISD::VBIT_CONVERT: {
4449 // We know the result is a vector. The input may be either a vector or a
4450 // scalar value.
4451 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4452 // Lower to a store/load. FIXME: this could be improved probably.
4453 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4454
4455 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4456 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4457 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4458 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4459 SplitVectorOp(St, Lo, Hi);
4460 } else {
4461 // If the input is a vector type, we have to either scalarize it, pack it
4462 // or convert it based on whether the input vector type is legal.
4463 SDNode *InVal = Node->getOperand(0).Val;
4464 unsigned NumElems =
4465 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4466 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4467
4468 // If the input is from a single element vector, scalarize the vector,
4469 // then treat like a scalar.
4470 if (NumElems == 1) {
4471 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4472 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4473 Op.getOperand(1), Op.getOperand(2));
4474 SplitVectorOp(Scalar, Lo, Hi);
4475 } else {
4476 // Split the input vector.
4477 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4478
4479 // Convert each of the pieces now.
4480 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4481 NewNumEltsNode, TypeNode);
4482 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4483 NewNumEltsNode, TypeNode);
4484 }
4485 break;
4486 }
4487 }
Chris Lattner32206f52006-03-18 01:44:44 +00004488 }
4489
4490 // Remember in a map if the values will be reused later.
4491 bool isNew =
4492 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4493 assert(isNew && "Value already expanded?!?");
4494}
4495
4496
4497/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4498/// equivalent operation that returns a scalar (e.g. F32) or packed value
4499/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4500/// type for the result.
4501SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4502 MVT::ValueType NewVT) {
4503 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4504 SDNode *Node = Op.Val;
4505
4506 // See if we already packed it.
4507 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4508 if (I != PackedNodes.end()) return I->second;
4509
4510 SDOperand Result;
4511 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004512 default:
4513 Node->dump(); std::cerr << "\n";
4514 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004515 case ISD::VADD:
4516 case ISD::VSUB:
4517 case ISD::VMUL:
4518 case ISD::VSDIV:
4519 case ISD::VUDIV:
4520 case ISD::VAND:
4521 case ISD::VOR:
4522 case ISD::VXOR:
4523 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4524 NewVT,
4525 PackVectorOp(Node->getOperand(0), NewVT),
4526 PackVectorOp(Node->getOperand(1), NewVT));
4527 break;
4528 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004529 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4530 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004531
Chris Lattner93640542006-03-19 00:07:49 +00004532 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004533
4534 // Remember that we legalized the chain.
4535 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4536 break;
4537 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004538 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00004539 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004540 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004541 Result = Node->getOperand(0);
4542 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004543 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004544 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004545 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004546 }
4547 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004548 case ISD::VINSERT_VECTOR_ELT:
4549 if (!MVT::isVector(NewVT)) {
4550 // Returning a scalar? Must be the inserted element.
4551 Result = Node->getOperand(1);
4552 } else {
4553 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4554 PackVectorOp(Node->getOperand(0), NewVT),
4555 Node->getOperand(1), Node->getOperand(2));
4556 }
4557 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00004558 case ISD::VVECTOR_SHUFFLE:
4559 if (!MVT::isVector(NewVT)) {
4560 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4561 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4562 if (cast<ConstantSDNode>(EltNum)->getValue())
4563 Result = PackVectorOp(Node->getOperand(1), NewVT);
4564 else
4565 Result = PackVectorOp(Node->getOperand(0), NewVT);
4566 } else {
4567 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4568 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4569 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4570 Node->getOperand(2).Val->op_end()-2);
4571 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4572 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4573
4574 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4575 PackVectorOp(Node->getOperand(0), NewVT),
4576 PackVectorOp(Node->getOperand(1), NewVT), BV);
4577 }
4578 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004579 case ISD::VBIT_CONVERT:
4580 if (Op.getOperand(0).getValueType() != MVT::Vector)
4581 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4582 else {
4583 // If the input is a vector type, we have to either scalarize it, pack it
4584 // or convert it based on whether the input vector type is legal.
4585 SDNode *InVal = Node->getOperand(0).Val;
4586 unsigned NumElems =
4587 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4588 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4589
4590 // Figure out if there is a Packed type corresponding to this Vector
4591 // type. If so, convert to the packed type.
4592 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4593 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4594 // Turn this into a bit convert of the packed input.
4595 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4596 PackVectorOp(Node->getOperand(0), TVT));
4597 break;
4598 } else if (NumElems == 1) {
4599 // Turn this into a bit convert of the scalar input.
4600 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4601 PackVectorOp(Node->getOperand(0), EVT));
4602 break;
4603 } else {
4604 // FIXME: UNIMP!
4605 assert(0 && "Cast from unsupported vector type not implemented yet!");
4606 }
4607 }
Chris Lattner32206f52006-03-18 01:44:44 +00004608 }
4609
4610 if (TLI.isTypeLegal(NewVT))
4611 Result = LegalizeOp(Result);
4612 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4613 assert(isNew && "Value already packed?");
4614 return Result;
4615}
4616
Chris Lattnerdc750592005-01-07 07:47:09 +00004617
4618// SelectionDAG::Legalize - This is the entry point for the file.
4619//
Chris Lattner4add7e32005-01-23 04:42:50 +00004620void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004621 /// run - This is the main entry point to this class.
4622 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004623 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004624}
4625