blob: 3fbfbf927a3d2146a0b206e56194af136555f2c2 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000017#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000018#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000019#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000020#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/CommandLine.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include <iostream>
Evan Cheng033e6812006-03-24 01:17:21 +000025#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000026using namespace llvm;
27
Chris Lattner5e46a192006-04-02 03:07:27 +000028#ifndef NDEBUG
29static cl::opt<bool>
30ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
31 cl::desc("Pop up a window to show dags before legalize"));
32#else
33static const bool ViewLegalizeDAGs = 0;
34#endif
35
Chris Lattner3e928bb2005-01-07 07:47:09 +000036//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
49class SelectionDAGLegalize {
50 TargetLowering &TLI;
51 SelectionDAG &DAG;
52
Chris Lattner6831a812006-02-13 09:18:02 +000053 // Libcall insertion helpers.
54
55 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
56 /// legalized. We use this to ensure that calls are properly serialized
57 /// against each other, including inserted libcalls.
58 SDOperand LastCALLSEQ_END;
59
60 /// IsLegalizingCall - This member is used *only* for purposes of providing
61 /// helpful assertions that a libcall isn't created while another call is
62 /// being legalized (which could lead to non-serialized call sequences).
63 bool IsLegalizingCall;
64
Chris Lattner3e928bb2005-01-07 07:47:09 +000065 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000066 Legal, // The target natively supports this operation.
67 Promote, // This operation should be executed in a larger type.
68 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000069 };
Chris Lattner6831a812006-02-13 09:18:02 +000070
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ValueTypeActions - This is a bitvector that contains two bits for each
72 /// value type, where the two bits correspond to the LegalizeAction enum.
73 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000074 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000075
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 /// LegalizedNodes - For nodes that are of legal width, and that have more
77 /// than one use, this map indicates what regularized operand to use. This
78 /// allows us to avoid legalizing the same thing more than once.
79 std::map<SDOperand, SDOperand> LegalizedNodes;
80
Chris Lattner03c85462005-01-15 05:21:40 +000081 /// PromotedNodes - For nodes that are below legal width, and that have more
82 /// than one use, this map indicates what promoted value to use. This allows
83 /// us to avoid promoting the same thing more than once.
84 std::map<SDOperand, SDOperand> PromotedNodes;
85
Chris Lattnerc7029802006-03-18 01:44:44 +000086 /// ExpandedNodes - For nodes that need to be expanded this map indicates
87 /// which which operands are the expanded version of the input. This allows
88 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000089 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
90
Chris Lattnerc7029802006-03-18 01:44:44 +000091 /// SplitNodes - For vector nodes that need to be split, this map indicates
92 /// which which operands are the split version of the input. This allows us
93 /// to avoid splitting the same node more than once.
94 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
95
96 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
97 /// concrete packed types, this contains the mapping of ones we have already
98 /// processed to the result.
99 std::map<SDOperand, SDOperand> PackedNodes;
100
Chris Lattner8afc48e2005-01-07 22:28:47 +0000101 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000102 LegalizedNodes.insert(std::make_pair(From, To));
103 // If someone requests legalization of the new node, return itself.
104 if (From != To)
105 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000106 }
Chris Lattner03c85462005-01-15 05:21:40 +0000107 void AddPromotedOperand(SDOperand From, SDOperand To) {
108 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
109 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000110 // If someone requests legalization of the new node, return itself.
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000112 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114public:
115
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000116 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000117
Chris Lattner3e928bb2005-01-07 07:47:09 +0000118 /// getTypeAction - Return how we should legalize values of this type, either
119 /// it is already legal or we need to expand it into multiple registers of
120 /// smaller integer type, or we need to promote it to a larger type.
121 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000122 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123 }
124
125 /// isTypeLegal - Return true if this type is legal on this target.
126 ///
127 bool isTypeLegal(MVT::ValueType VT) const {
128 return getTypeAction(VT) == Legal;
129 }
130
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131 void LegalizeDAG();
132
Chris Lattner456a93a2006-01-28 07:39:30 +0000133private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000134 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
135 /// appropriate for its type.
136 void HandleOp(SDOperand Op);
137
138 /// LegalizeOp - We know that the specified value has a legal type.
139 /// Recursively ensure that the operands have legal types, then return the
140 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000142
143 /// PromoteOp - Given an operation that produces a value in an invalid type,
144 /// promote it to compute the value into a larger type. The produced value
145 /// will have the correct bits for the low portion of the register, but no
146 /// guarantee is made about the top bits: it may be zero, sign-extended, or
147 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000148 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149
Chris Lattnerc7029802006-03-18 01:44:44 +0000150 /// ExpandOp - Expand the specified SDOperand into its two component pieces
151 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
152 /// the LegalizeNodes map is filled in for any results that are not expanded,
153 /// the ExpandedNodes map is filled in for any results that are expanded, and
154 /// the Lo/Hi values are returned. This applies to integer types and Vector
155 /// types.
156 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
157
158 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
159 /// two smaller values of MVT::Vector type.
160 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
161
162 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
163 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
164 /// this is called, we know that PackedVT is the right type for the result and
165 /// we know that this type is legal for the target.
166 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
167
Chris Lattner4352cc92006-04-04 17:23:26 +0000168 /// isShuffleLegal - Return true if a vector shuffle is legal with the
169 /// specified mask and type. Targets can specify exactly which masks they
170 /// support and the code generator is tasked with not creating illegal masks.
171 ///
172 /// Note that this will also return true for shuffles that are promoted to a
173 /// different type.
174 ///
175 /// If this is a legal shuffle, this method returns the (possibly promoted)
176 /// build_vector Mask. If it's not a legal shuffle, it returns null.
177 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
178
Chris Lattner6831a812006-02-13 09:18:02 +0000179 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
180
Nate Begeman750ac1b2006-02-01 07:19:44 +0000181 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
182
Chris Lattnerce872152006-03-19 06:31:19 +0000183 SDOperand CreateStackTemporary(MVT::ValueType VT);
184
Chris Lattner77e77a62005-01-21 06:05:23 +0000185 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
186 SDOperand &Hi);
187 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
188 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000189
Chris Lattner35481892005-12-23 00:16:34 +0000190 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000191 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000192 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000193 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
194 SDOperand LegalOp,
195 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000196 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
197 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000198 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
199 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000200
Chris Lattner456a93a2006-01-28 07:39:30 +0000201 SDOperand ExpandBSWAP(SDOperand Op);
202 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000203 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
204 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000205 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
206 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000207
Chris Lattner15972212006-03-31 17:55:51 +0000208 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000209 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000210
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211 SDOperand getIntPtrConstant(uint64_t Val) {
212 return DAG.getConstant(Val, TLI.getPointerTy());
213 }
214};
215}
216
Chris Lattner4352cc92006-04-04 17:23:26 +0000217/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
218/// specified mask and type. Targets can specify exactly which masks they
219/// support and the code generator is tasked with not creating illegal masks.
220///
221/// Note that this will also return true for shuffles that are promoted to a
222/// different type.
223SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
224 SDOperand Mask) const {
225 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
226 default: return 0;
227 case TargetLowering::Legal:
228 case TargetLowering::Custom:
229 break;
230 case TargetLowering::Promote: {
231 // If this is promoted to a different type, convert the shuffle mask and
232 // ask if it is legal in the promoted type!
233 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
234
235 // If we changed # elements, change the shuffle mask.
236 unsigned NumEltsGrowth =
237 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
238 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
239 if (NumEltsGrowth > 1) {
240 // Renumber the elements.
241 std::vector<SDOperand> Ops;
242 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
243 SDOperand InOp = Mask.getOperand(i);
244 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
245 if (InOp.getOpcode() == ISD::UNDEF)
246 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
247 else {
248 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
249 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
250 }
251 }
252 }
253 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, Ops);
254 }
255 VT = NVT;
256 break;
257 }
258 }
259 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
260}
261
Chris Lattnerc7029802006-03-18 01:44:44 +0000262/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
263/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000264static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000265 switch (VecOp) {
266 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000267 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
268 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
269 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
270 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
271 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
272 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
273 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
274 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000275 }
276}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000278SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
279 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
280 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000281 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000282 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000283}
284
Chris Lattner32fca002005-10-06 01:20:27 +0000285/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
286/// not been visited yet and if all of its operands have already been visited.
287static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
288 std::map<SDNode*, unsigned> &Visited) {
289 if (++Visited[N] != N->getNumOperands())
290 return; // Haven't visited all operands yet
291
292 Order.push_back(N);
293
294 if (N->hasOneUse()) { // Tail recurse in common case.
295 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
296 return;
297 }
298
299 // Now that we have N in, add anything that uses it if all of their operands
300 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000301 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
302 ComputeTopDownOrdering(*UI, Order, Visited);
303}
304
Chris Lattner1618beb2005-07-29 00:11:56 +0000305
Chris Lattner3e928bb2005-01-07 07:47:09 +0000306void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000307 LastCALLSEQ_END = DAG.getEntryNode();
308 IsLegalizingCall = false;
309
Chris Lattnerab510a72005-10-02 17:49:46 +0000310 // The legalize process is inherently a bottom-up recursive process (users
311 // legalize their uses before themselves). Given infinite stack space, we
312 // could just start legalizing on the root and traverse the whole graph. In
313 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000314 // blocks. To avoid this problem, compute an ordering of the nodes where each
315 // node is only legalized after all of its operands are legalized.
316 std::map<SDNode*, unsigned> Visited;
317 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000318
Chris Lattner32fca002005-10-06 01:20:27 +0000319 // Compute ordering from all of the leaves in the graphs, those (like the
320 // entry node) that have no operands.
321 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
322 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000323 if (I->getNumOperands() == 0) {
324 Visited[I] = 0 - 1U;
325 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000326 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000327 }
328
Chris Lattnerde202b32005-11-09 23:47:37 +0000329 assert(Order.size() == Visited.size() &&
330 Order.size() ==
331 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000332 "Error: DAG is cyclic!");
333 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000334
Chris Lattnerc7029802006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000346 SplitNodes.clear();
347 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000350 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351}
352
Chris Lattner6831a812006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
409 SDNode *Dest) {
410 if (N == Dest) return true; // N certainly leads to Dest :)
411
412 // If the first result of this node has been already legalized, then it cannot
413 // reach N.
414 switch (getTypeAction(N->getValueType(0))) {
415 case Legal:
416 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
417 break;
418 case Promote:
419 if (PromotedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Expand:
422 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 }
425
426 // Okay, this node has not already been legalized. Check and legalize all
427 // operands. If none lead to Dest, then we can legalize this node.
428 bool OperandsLeadToDest = false;
429 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
430 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
431 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
432
433 if (OperandsLeadToDest) return true;
434
435 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000436 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000437 return false;
438}
439
Chris Lattnerc7029802006-03-18 01:44:44 +0000440/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
441/// appropriate for its type.
442void SelectionDAGLegalize::HandleOp(SDOperand Op) {
443 switch (getTypeAction(Op.getValueType())) {
444 default: assert(0 && "Bad type action!");
445 case Legal: LegalizeOp(Op); break;
446 case Promote: PromoteOp(Op); break;
447 case Expand:
448 if (Op.getValueType() != MVT::Vector) {
449 SDOperand X, Y;
450 ExpandOp(Op, X, Y);
451 } else {
452 SDNode *N = Op.Val;
453 unsigned NumOps = N->getNumOperands();
454 unsigned NumElements =
455 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
456 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
457 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
458 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
459 // In the common case, this is a legal vector type, convert it to the
460 // packed operation and type now.
461 PackVectorOp(Op, PackedVT);
462 } else if (NumElements == 1) {
463 // Otherwise, if this is a single element vector, convert it to a
464 // scalar operation.
465 PackVectorOp(Op, EVT);
466 } else {
467 // Otherwise, this is a multiple element vector that isn't supported.
468 // Split it in half and legalize both parts.
469 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000470 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000471 }
472 }
473 break;
474 }
475}
Chris Lattner6831a812006-02-13 09:18:02 +0000476
477
Chris Lattnerc7029802006-03-18 01:44:44 +0000478/// LegalizeOp - We know that the specified value has a legal type.
479/// Recursively ensure that the operands have legal types, then return the
480/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000481SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000482 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000483 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000484 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000485
Chris Lattner3e928bb2005-01-07 07:47:09 +0000486 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000487 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000488 if (Node->getNumValues() > 1) {
489 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000490 if (getTypeAction(Node->getValueType(i)) != Legal) {
491 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000493 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000494 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000495 }
496 }
497
Chris Lattner45982da2005-05-12 16:53:42 +0000498 // Note that LegalizeOp may be reentered even from single-use nodes, which
499 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000500 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
501 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000502
Nate Begeman9373a812005-08-10 20:51:12 +0000503 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000504 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000505 bool isCustom = false;
506
Chris Lattner3e928bb2005-01-07 07:47:09 +0000507 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000508 case ISD::FrameIndex:
509 case ISD::EntryToken:
510 case ISD::Register:
511 case ISD::BasicBlock:
512 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000513 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000514 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000515 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000516 case ISD::TargetConstantPool:
517 case ISD::TargetGlobalAddress:
518 case ISD::TargetExternalSymbol:
519 case ISD::VALUETYPE:
520 case ISD::SRCVALUE:
521 case ISD::STRING:
522 case ISD::CONDCODE:
523 // Primitives must all be legal.
524 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
525 "This must be legal!");
526 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000528 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
529 // If this is a target node, legalize it by legalizing the operands then
530 // passing it through.
531 std::vector<SDOperand> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000532 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000533 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000534
535 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops);
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000536
537 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
538 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
539 return Result.getValue(Op.ResNo);
540 }
541 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000542 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
543 assert(0 && "Do not know how to legalize this operator!");
544 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000545 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000546 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000547 case ISD::ConstantPool:
548 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000549 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
550 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000551 case TargetLowering::Custom:
552 Tmp1 = TLI.LowerOperation(Op, DAG);
553 if (Tmp1.Val) Result = Tmp1;
554 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000555 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000556 break;
557 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000558 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000563 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000564 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000565 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000566 Result = Node->getOperand(Op.ResNo);
567 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000571 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000573 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000574 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000575 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000576 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
578 } else {
579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
580 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000581 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
582 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000583 // Since CopyFromReg produces two values, make sure to remember that we
584 // legalized both of them.
585 AddLegalizedOperand(Op.getValue(0), Result);
586 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
587 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000588 case ISD::UNDEF: {
589 MVT::ValueType VT = Op.getValueType();
590 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000591 default: assert(0 && "This action is not supported yet!");
592 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000593 if (MVT::isInteger(VT))
594 Result = DAG.getConstant(0, VT);
595 else if (MVT::isFloatingPoint(VT))
596 Result = DAG.getConstantFP(0, VT);
597 else
598 assert(0 && "Unknown value type!");
599 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000600 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000601 break;
602 }
603 break;
604 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000605
Chris Lattner48b61a72006-03-28 00:40:33 +0000606 case ISD::INTRINSIC_W_CHAIN:
607 case ISD::INTRINSIC_WO_CHAIN:
608 case ISD::INTRINSIC_VOID: {
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000609 std::vector<SDOperand> Ops;
610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
611 Ops.push_back(LegalizeOp(Node->getOperand(i)));
612 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner10d7fa62006-03-26 09:12:51 +0000613
614 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000615 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000616 TargetLowering::Custom) {
617 Tmp3 = TLI.LowerOperation(Result, DAG);
618 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000619 }
620
621 if (Result.Val->getNumValues() == 1) break;
622
623 // Must have return value and chain result.
624 assert(Result.Val->getNumValues() == 2 &&
625 "Cannot return more than two values!");
626
627 // Since loads produce two values, make sure to remember that we
628 // legalized both of them.
629 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
630 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
631 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000632 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000633
634 case ISD::LOCATION:
635 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
636 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
637
638 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
639 case TargetLowering::Promote:
640 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000641 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000642 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000643 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
644 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
645
646 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
647 const std::string &FName =
648 cast<StringSDNode>(Node->getOperand(3))->getValue();
649 const std::string &DirName =
650 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000651 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000652
Jim Laskeye81aecb2005-12-21 20:51:37 +0000653 std::vector<SDOperand> Ops;
654 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000655 SDOperand LineOp = Node->getOperand(1);
656 SDOperand ColOp = Node->getOperand(2);
657
658 if (useDEBUG_LOC) {
659 Ops.push_back(LineOp); // line #
660 Ops.push_back(ColOp); // col #
661 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
662 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
663 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000664 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
665 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000666 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
667 Ops.push_back(DAG.getConstant(ID, MVT::i32));
668 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
669 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000670 } else {
671 Result = Tmp1; // chain
672 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000673 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000674 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000675 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000676 if (Tmp1 != Node->getOperand(0) ||
677 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000678 std::vector<SDOperand> Ops;
679 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000680 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
681 Ops.push_back(Node->getOperand(1)); // line # must be legal.
682 Ops.push_back(Node->getOperand(2)); // col # must be legal.
683 } else {
684 // Otherwise promote them.
685 Ops.push_back(PromoteOp(Node->getOperand(1)));
686 Ops.push_back(PromoteOp(Node->getOperand(2)));
687 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000688 Ops.push_back(Node->getOperand(3)); // filename must be legal.
689 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000690 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000691 }
692 break;
693 }
694 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000695
696 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000697 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000698 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000699 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000700 case TargetLowering::Legal:
701 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
702 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
703 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
704 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000705 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000706 break;
707 }
708 break;
709
710 case ISD::DEBUG_LABEL:
711 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
712 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000713 default: assert(0 && "This action is not supported yet!");
714 case TargetLowering::Legal:
715 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
716 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000717 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000718 break;
719 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000720 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000721
Chris Lattner3e928bb2005-01-07 07:47:09 +0000722 case ISD::Constant:
723 // We know we don't need to expand constants here, constants only have one
724 // value and we check that it is fine above.
725
726 // FIXME: Maybe we should handle things like targets that don't support full
727 // 32-bit immediates?
728 break;
729 case ISD::ConstantFP: {
730 // Spill FP immediates to the constant pool if the target cannot directly
731 // codegen them. Targets often have some immediate values that can be
732 // efficiently generated into an FP register without a load. We explicitly
733 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000734 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
735
736 // Check to see if this FP immediate is already legal.
737 bool isLegal = false;
738 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
739 E = TLI.legal_fpimm_end(); I != E; ++I)
740 if (CFP->isExactlyValue(*I)) {
741 isLegal = true;
742 break;
743 }
744
Chris Lattner3181a772006-01-29 06:26:56 +0000745 // If this is a legal constant, turn it into a TargetConstantFP node.
746 if (isLegal) {
747 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
748 break;
749 }
750
751 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
752 default: assert(0 && "This action is not supported yet!");
753 case TargetLowering::Custom:
754 Tmp3 = TLI.LowerOperation(Result, DAG);
755 if (Tmp3.Val) {
756 Result = Tmp3;
757 break;
758 }
759 // FALLTHROUGH
760 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000761 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000762 bool Extend = false;
763
Chris Lattner456a93a2006-01-28 07:39:30 +0000764 // If a FP immediate is precise when represented as a float and if the
765 // target can do an extending load from float to double, we put it into
766 // the constant pool as a float, even if it's is statically typed as a
767 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000768 MVT::ValueType VT = CFP->getValueType(0);
769 bool isDouble = VT == MVT::f64;
770 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
771 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000772 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
773 // Only do this if the target has a native EXTLOAD instruction from
774 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000775 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000776 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
777 VT = MVT::f32;
778 Extend = true;
779 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000780
Chris Lattner456a93a2006-01-28 07:39:30 +0000781 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000782 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000783 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
784 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000785 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000786 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
787 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000788 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000789 }
790 break;
791 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000792 case ISD::TokenFactor:
793 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000794 Tmp1 = LegalizeOp(Node->getOperand(0));
795 Tmp2 = LegalizeOp(Node->getOperand(1));
796 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
797 } else if (Node->getNumOperands() == 3) {
798 Tmp1 = LegalizeOp(Node->getOperand(0));
799 Tmp2 = LegalizeOp(Node->getOperand(1));
800 Tmp3 = LegalizeOp(Node->getOperand(2));
801 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000802 } else {
803 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000804 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000805 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
806 Ops.push_back(LegalizeOp(Node->getOperand(i)));
807 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000808 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000809 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000810
811 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000812 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000813 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000814 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
815 assert(Tmp3.Val && "Target didn't custom lower this node!");
816 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
817 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000818
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000819 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000820 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000821 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
822 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000823 if (Op.ResNo == i)
824 Tmp2 = Tmp1;
825 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
826 }
827 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000828
Chris Lattnerb2827b02006-03-19 00:52:58 +0000829 case ISD::BUILD_VECTOR:
830 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000831 default: assert(0 && "This action is not supported yet!");
832 case TargetLowering::Custom:
833 Tmp3 = TLI.LowerOperation(Result, DAG);
834 if (Tmp3.Val) {
835 Result = Tmp3;
836 break;
837 }
838 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000839 case TargetLowering::Expand:
840 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000841 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000842 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000843 break;
844 case ISD::INSERT_VECTOR_ELT:
845 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
846 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
847 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
848 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
849
850 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
851 Node->getValueType(0))) {
852 default: assert(0 && "This action is not supported yet!");
853 case TargetLowering::Legal:
854 break;
855 case TargetLowering::Custom:
856 Tmp3 = TLI.LowerOperation(Result, DAG);
857 if (Tmp3.Val) {
858 Result = Tmp3;
859 break;
860 }
861 // FALLTHROUGH
862 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000863 // If the insert index is a constant, codegen this as a scalar_to_vector,
864 // then a shuffle that inserts it into the right position in the vector.
865 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
866 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
867 Tmp1.getValueType(), Tmp2);
868
869 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
870 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
871 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
872
873 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
874 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
875 // the RHS.
876 std::vector<SDOperand> ShufOps;
877 for (unsigned i = 0; i != NumElts; ++i) {
878 if (i != InsertPos->getValue())
879 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
880 else
881 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
882 }
883 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,ShufOps);
884
885 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
886 Tmp1, ScVec, ShufMask);
887 Result = LegalizeOp(Result);
888 break;
889 }
890
Chris Lattner2332b9f2006-03-19 01:17:20 +0000891 // If the target doesn't support this, we have to spill the input vector
892 // to a temporary stack slot, update the element, then reload it. This is
893 // badness. We could also load the value into a vector register (either
894 // with a "move to register" or "extload into register" instruction, then
895 // permute it into place, if the idx is a constant and if the idx is
896 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000897 MVT::ValueType VT = Tmp1.getValueType();
898 MVT::ValueType EltVT = Tmp2.getValueType();
899 MVT::ValueType IdxVT = Tmp3.getValueType();
900 MVT::ValueType PtrVT = TLI.getPointerTy();
901 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000902 // Store the vector.
903 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
904 Tmp1, StackPtr, DAG.getSrcValue(NULL));
905
906 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000907 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
908 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000909 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000910 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
911 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
912 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000913 // Store the scalar value.
914 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
915 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
916 // Load the updated vector.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000917 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner2332b9f2006-03-19 01:17:20 +0000918 break;
919 }
920 }
921 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000922 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000923 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
924 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
925 break;
926 }
927
Chris Lattnerce872152006-03-19 06:31:19 +0000928 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
929 Result = DAG.UpdateNodeOperands(Result, Tmp1);
930 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
931 Node->getValueType(0))) {
932 default: assert(0 && "This action is not supported yet!");
933 case TargetLowering::Legal:
934 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000935 case TargetLowering::Custom:
936 Tmp3 = TLI.LowerOperation(Result, DAG);
937 if (Tmp3.Val) {
938 Result = Tmp3;
939 break;
940 }
941 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000942 case TargetLowering::Expand:
943 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000944 break;
945 }
Chris Lattnerce872152006-03-19 06:31:19 +0000946 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000947 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000948 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
949 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
950 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
951
952 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000953 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
954 default: assert(0 && "Unknown operation action!");
955 case TargetLowering::Legal:
956 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
957 "vector shuffle should not be created if not legal!");
958 break;
959 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +0000960 Tmp3 = TLI.LowerOperation(Result, DAG);
961 if (Tmp3.Val) {
962 Result = Tmp3;
963 break;
964 }
965 // FALLTHROUGH
966 case TargetLowering::Expand: {
967 MVT::ValueType VT = Node->getValueType(0);
968 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
969 MVT::ValueType PtrVT = TLI.getPointerTy();
970 SDOperand Mask = Node->getOperand(2);
971 unsigned NumElems = Mask.getNumOperands();
972 std::vector<SDOperand> Ops;
973 for (unsigned i = 0; i != NumElems; ++i) {
974 SDOperand Arg = Mask.getOperand(i);
975 if (Arg.getOpcode() == ISD::UNDEF) {
976 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
977 } else {
978 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
979 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
980 if (Idx < NumElems)
981 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
982 DAG.getConstant(Idx, PtrVT)));
983 else
984 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
985 DAG.getConstant(Idx - NumElems, PtrVT)));
986 }
987 }
988 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner4352cc92006-04-04 17:23:26 +0000989 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +0000990 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000991 case TargetLowering::Promote: {
992 // Change base type to a different vector type.
993 MVT::ValueType OVT = Node->getValueType(0);
994 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
995
996 // Cast the two input vectors.
997 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
998 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
999
1000 // Convert the shuffle mask to the right # elements.
1001 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1002 assert(Tmp3.Val && "Shuffle not legal?");
1003 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1004 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1005 break;
1006 }
Chris Lattner87100e02006-03-20 01:52:29 +00001007 }
1008 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001009
1010 case ISD::EXTRACT_VECTOR_ELT:
1011 Tmp1 = LegalizeOp(Node->getOperand(0));
1012 Tmp2 = LegalizeOp(Node->getOperand(1));
1013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001014
1015 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1016 Tmp1.getValueType())) {
1017 default: assert(0 && "This action is not supported yet!");
1018 case TargetLowering::Legal:
1019 break;
1020 case TargetLowering::Custom:
1021 Tmp3 = TLI.LowerOperation(Result, DAG);
1022 if (Tmp3.Val) {
1023 Result = Tmp3;
1024 break;
1025 }
1026 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001027 case TargetLowering::Expand:
1028 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001029 break;
1030 }
Chris Lattner384504c2006-03-21 20:44:12 +00001031 break;
1032
Chris Lattner15972212006-03-31 17:55:51 +00001033 case ISD::VEXTRACT_VECTOR_ELT:
1034 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001035 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001036
Chris Lattner6831a812006-02-13 09:18:02 +00001037 case ISD::CALLSEQ_START: {
1038 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1039
1040 // Recursively Legalize all of the inputs of the call end that do not lead
1041 // to this call start. This ensures that any libcalls that need be inserted
1042 // are inserted *before* the CALLSEQ_START.
1043 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1044 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1045
1046 // Now that we legalized all of the inputs (which may have inserted
1047 // libcalls) create the new CALLSEQ_START node.
1048 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1049
1050 // Merge in the last call, to ensure that this call start after the last
1051 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001052 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001053 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1054 Tmp1 = LegalizeOp(Tmp1);
1055 }
Chris Lattner6831a812006-02-13 09:18:02 +00001056
1057 // Do not try to legalize the target-specific arguments (#1+).
1058 if (Tmp1 != Node->getOperand(0)) {
1059 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1060 Ops[0] = Tmp1;
1061 Result = DAG.UpdateNodeOperands(Result, Ops);
1062 }
1063
1064 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001065 AddLegalizedOperand(Op.getValue(0), Result);
1066 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1067 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1068
Chris Lattner6831a812006-02-13 09:18:02 +00001069 // Now that the callseq_start and all of the non-call nodes above this call
1070 // sequence have been legalized, legalize the call itself. During this
1071 // process, no libcalls can/will be inserted, guaranteeing that no calls
1072 // can overlap.
1073 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1074 SDOperand InCallSEQ = LastCALLSEQ_END;
1075 // Note that we are selecting this call!
1076 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1077 IsLegalizingCall = true;
1078
1079 // Legalize the call, starting from the CALLSEQ_END.
1080 LegalizeOp(LastCALLSEQ_END);
1081 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1082 return Result;
1083 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001084 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001085 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1086 // will cause this node to be legalized as well as handling libcalls right.
1087 if (LastCALLSEQ_END.Val != Node) {
1088 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1089 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1090 assert(I != LegalizedNodes.end() &&
1091 "Legalizing the call start should have legalized this node!");
1092 return I->second;
1093 }
1094
1095 // Otherwise, the call start has been legalized and everything is going
1096 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001098 // Do not try to legalize the target-specific arguments (#1+), except for
1099 // an optional flag input.
1100 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1101 if (Tmp1 != Node->getOperand(0)) {
1102 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1103 Ops[0] = Tmp1;
1104 Result = DAG.UpdateNodeOperands(Result, Ops);
1105 }
1106 } else {
1107 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1108 if (Tmp1 != Node->getOperand(0) ||
1109 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1110 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1111 Ops[0] = Tmp1;
1112 Ops.back() = Tmp2;
1113 Result = DAG.UpdateNodeOperands(Result, Ops);
1114 }
Chris Lattner6a542892006-01-24 05:48:21 +00001115 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001116 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001117 // This finishes up call legalization.
1118 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001119
1120 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1121 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1122 if (Node->getNumValues() == 2)
1123 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1124 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001125 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001126 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1127 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1128 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001129 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001130
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001131 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001132 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001133 switch (TLI.getOperationAction(Node->getOpcode(),
1134 Node->getValueType(0))) {
1135 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001136 case TargetLowering::Expand: {
1137 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1138 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1139 " not tell us which reg is the stack pointer!");
1140 SDOperand Chain = Tmp1.getOperand(0);
1141 SDOperand Size = Tmp2.getOperand(1);
1142 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1143 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1144 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001145 Tmp1 = LegalizeOp(Tmp1);
1146 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001147 break;
1148 }
1149 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001150 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1151 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001152 Tmp1 = LegalizeOp(Tmp3);
1153 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001154 }
Chris Lattner903d2782006-01-15 08:54:32 +00001155 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001156 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001157 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001158 }
Chris Lattner903d2782006-01-15 08:54:32 +00001159 // Since this op produce two values, make sure to remember that we
1160 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001161 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1162 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001163 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001164 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001165 case ISD::INLINEASM:
1166 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1167 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001168 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001169 Tmp2 = Tmp3 = SDOperand(0, 0);
1170 else
1171 Tmp3 = LegalizeOp(Tmp2);
1172
1173 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1174 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1175 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001176 if (Tmp3.Val) Ops.back() = Tmp3;
1177 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001178 }
1179
1180 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001182 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1183 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001184 case ISD::BR:
1185 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001186 // Ensure that libcalls are emitted before a branch.
1187 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1188 Tmp1 = LegalizeOp(Tmp1);
1189 LastCALLSEQ_END = DAG.getEntryNode();
1190
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001191 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001192 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001193 case ISD::BRIND:
1194 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1195 // Ensure that libcalls are emitted before a branch.
1196 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1197 Tmp1 = LegalizeOp(Tmp1);
1198 LastCALLSEQ_END = DAG.getEntryNode();
1199
1200 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1201 default: assert(0 && "Indirect target must be legal type (pointer)!");
1202 case Legal:
1203 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1204 break;
1205 }
1206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1207 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001208 case ISD::BRCOND:
1209 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001210 // Ensure that libcalls are emitted before a return.
1211 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1212 Tmp1 = LegalizeOp(Tmp1);
1213 LastCALLSEQ_END = DAG.getEntryNode();
1214
Chris Lattner47e92232005-01-18 19:27:06 +00001215 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1216 case Expand: assert(0 && "It's impossible to expand bools");
1217 case Legal:
1218 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1219 break;
1220 case Promote:
1221 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1222 break;
1223 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001224
1225 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001226 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001227
1228 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1229 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001230 case TargetLowering::Legal: break;
1231 case TargetLowering::Custom:
1232 Tmp1 = TLI.LowerOperation(Result, DAG);
1233 if (Tmp1.Val) Result = Tmp1;
1234 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001235 case TargetLowering::Expand:
1236 // Expand brcond's setcc into its constituent parts and create a BR_CC
1237 // Node.
1238 if (Tmp2.getOpcode() == ISD::SETCC) {
1239 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1240 Tmp2.getOperand(0), Tmp2.getOperand(1),
1241 Node->getOperand(2));
1242 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001243 // Make sure the condition is either zero or one. It may have been
1244 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001245 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1246 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1247 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001248
Nate Begeman7cbd5252005-08-16 19:49:35 +00001249 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1250 DAG.getCondCode(ISD::SETNE), Tmp2,
1251 DAG.getConstant(0, Tmp2.getValueType()),
1252 Node->getOperand(2));
1253 }
1254 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001255 }
1256 break;
1257 case ISD::BR_CC:
1258 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001259 // Ensure that libcalls are emitted before a branch.
1260 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1261 Tmp1 = LegalizeOp(Tmp1);
1262 LastCALLSEQ_END = DAG.getEntryNode();
1263
Nate Begeman750ac1b2006-02-01 07:19:44 +00001264 Tmp2 = Node->getOperand(2); // LHS
1265 Tmp3 = Node->getOperand(3); // RHS
1266 Tmp4 = Node->getOperand(1); // CC
1267
1268 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1269
1270 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1271 // the LHS is a legal SETCC itself. In this case, we need to compare
1272 // the result against zero to select between true and false values.
1273 if (Tmp3.Val == 0) {
1274 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1275 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001276 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001277
1278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1279 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001280
Chris Lattner181b7a32005-12-17 23:46:46 +00001281 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1282 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001283 case TargetLowering::Legal: break;
1284 case TargetLowering::Custom:
1285 Tmp4 = TLI.LowerOperation(Result, DAG);
1286 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001287 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001288 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001289 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001290 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1292 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001293
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001294 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001295 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001296 Tmp3 = Result.getValue(0);
1297 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001298
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001299 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1300 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001301 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001302 case TargetLowering::Custom:
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001303 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001304 if (Tmp1.Val) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001305 Tmp3 = LegalizeOp(Tmp1);
1306 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001307 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001308 break;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001309 case TargetLowering::Promote: {
1310 // Only promote a load of vector type to another.
1311 assert(MVT::isVector(VT) && "Cannot promote this load!");
1312 // Change base type to a different vector type.
1313 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1314
1315 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2));
1316 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1317 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1318 break;
1319 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001320 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001321 // Since loads produce two values, make sure to remember that we
1322 // legalized both of them.
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001323 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1324 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1325 return Op.ResNo ? Tmp4 : Tmp3;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001326 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001327 case ISD::EXTLOAD:
1328 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001329 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001332
Chris Lattner5f056bf2005-07-10 01:55:33 +00001333 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001334 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001335 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001336 case TargetLowering::Promote:
1337 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1339 DAG.getValueType(MVT::i8));
1340 Tmp1 = Result.getValue(0);
1341 Tmp2 = Result.getValue(1);
1342 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001343 case TargetLowering::Custom:
1344 isCustom = true;
1345 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001346 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1348 Node->getOperand(3));
1349 Tmp1 = Result.getValue(0);
1350 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001351
1352 if (isCustom) {
1353 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1354 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001355 Tmp1 = LegalizeOp(Tmp3);
1356 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001357 }
1358 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001359 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001360 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001361 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001362 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1363 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001364 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001365 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1366 Tmp2 = LegalizeOp(Load.getValue(1));
1367 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001368 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001369 assert(Node->getOpcode() != ISD::EXTLOAD &&
1370 "EXTLOAD should always be supported!");
1371 // Turn the unsupported load into an EXTLOAD followed by an explicit
1372 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001373 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1374 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001375 SDOperand ValRes;
1376 if (Node->getOpcode() == ISD::SEXTLOAD)
1377 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001378 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001379 else
1380 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001381 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1382 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1383 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001384 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 // Since loads produce two values, make sure to remember that we legalized
1386 // both of them.
1387 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1388 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1389 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001390 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001391 case ISD::EXTRACT_ELEMENT: {
1392 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1393 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001394 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001395 case Legal:
1396 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1397 // 1 -> Hi
1398 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1399 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1400 TLI.getShiftAmountTy()));
1401 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1402 } else {
1403 // 0 -> Lo
1404 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1405 Node->getOperand(0));
1406 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001407 break;
1408 case Expand:
1409 // Get both the low and high parts.
1410 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1411 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1412 Result = Tmp2; // 1 -> Hi
1413 else
1414 Result = Tmp1; // 0 -> Lo
1415 break;
1416 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001417 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001418 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001419
1420 case ISD::CopyToReg:
1421 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001422
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001423 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001424 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001425 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001426 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001427 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001429 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001430 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001431 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001432 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001433 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1434 Tmp3);
1435 } else {
1436 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001437 }
1438
1439 // Since this produces two values, make sure to remember that we legalized
1440 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001441 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001442 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001443 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001444 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001445 break;
1446
1447 case ISD::RET:
1448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001449
1450 // Ensure that libcalls are emitted before a return.
1451 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1452 Tmp1 = LegalizeOp(Tmp1);
1453 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001454
Chris Lattner3e928bb2005-01-07 07:47:09 +00001455 switch (Node->getNumOperands()) {
1456 case 2: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001457 Tmp2 = Node->getOperand(1);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001458 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001459 case Legal:
Chris Lattnerf87324e2006-04-11 01:31:51 +00001460 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001461 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001462 case Expand:
1463 if (Tmp2.getValueType() != MVT::Vector) {
1464 SDOperand Lo, Hi;
1465 ExpandOp(Tmp2, Lo, Hi);
1466 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1467 } else {
1468 SDNode *InVal = Tmp2.Val;
1469 unsigned NumElems =
1470 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1471 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1472
1473 // Figure out if there is a Packed type corresponding to this Vector
1474 // type. If so, convert to the packed type.
1475 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1476 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1477 // Turn this into a return of the packed type.
1478 Tmp2 = PackVectorOp(Tmp2, TVT);
1479 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1480 } else if (NumElems == 1) {
1481 // Turn this into a return of the scalar type.
1482 Tmp2 = PackVectorOp(Tmp2, EVT);
1483 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001484
1485 // FIXME: Returns of gcc generic vectors smaller than a legal type
1486 // should be returned in integer registers!
1487
Chris Lattnerf87324e2006-04-11 01:31:51 +00001488 // The scalarized value type may not be legal, e.g. it might require
1489 // promotion or expansion. Relegalize the return.
1490 Result = LegalizeOp(Result);
1491 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001492 // FIXME: Returns of gcc generic vectors larger than a legal vector
1493 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001494 SDOperand Lo, Hi;
1495 SplitVectorOp(Tmp2, Lo, Hi);
1496 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1497 Result = LegalizeOp(Result);
1498 }
1499 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001500 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001501 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001502 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001503 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1504 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001505 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001506 }
1507 break;
1508 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001509 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001510 break;
1511 default: { // ret <values>
1512 std::vector<SDOperand> NewValues;
1513 NewValues.push_back(Tmp1);
1514 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1515 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1516 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001517 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001518 break;
1519 case Expand: {
1520 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001521 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1522 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001523 ExpandOp(Node->getOperand(i), Lo, Hi);
1524 NewValues.push_back(Lo);
1525 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001526 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 }
1528 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001529 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001530 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001531
1532 if (NewValues.size() == Node->getNumOperands())
1533 Result = DAG.UpdateNodeOperands(Result, NewValues);
1534 else
1535 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001536 break;
1537 }
1538 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001539
Chris Lattner6862dbc2006-01-29 21:02:23 +00001540 if (Result.getOpcode() == ISD::RET) {
1541 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1542 default: assert(0 && "This action is not supported yet!");
1543 case TargetLowering::Legal: break;
1544 case TargetLowering::Custom:
1545 Tmp1 = TLI.LowerOperation(Result, DAG);
1546 if (Tmp1.Val) Result = Tmp1;
1547 break;
1548 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001549 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001550 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001551 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1553 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1554
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001555 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001556 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001557 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001558 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001559 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001560 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001561 } else {
1562 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001563 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001564 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001565 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1566 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001567 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001568 }
1569
Chris Lattner3e928bb2005-01-07 07:47:09 +00001570 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1571 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001572 Tmp3 = LegalizeOp(Node->getOperand(1));
1573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1574 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001575
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001576 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001577 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1578 default: assert(0 && "This action is not supported yet!");
1579 case TargetLowering::Legal: break;
1580 case TargetLowering::Custom:
1581 Tmp1 = TLI.LowerOperation(Result, DAG);
1582 if (Tmp1.Val) Result = Tmp1;
1583 break;
Chris Lattner2efce0a2006-04-16 01:36:45 +00001584 case TargetLowering::Promote:
1585 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1586 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1587 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1589 Node->getOperand(3));
1590 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001591 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001592 break;
1593 }
1594 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001595 // Truncate the value and store the result.
1596 Tmp3 = PromoteOp(Node->getOperand(1));
1597 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001598 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001599 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001600 break;
1601
Chris Lattner3e928bb2005-01-07 07:47:09 +00001602 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001603 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001604 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001605
1606 // If this is a vector type, then we have to calculate the increment as
1607 // the product of the element size in bytes, and the number of elements
1608 // in the high half of the vector.
1609 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1610 SDNode *InVal = Node->getOperand(1).Val;
1611 unsigned NumElems =
1612 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1613 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1614
1615 // Figure out if there is a Packed type corresponding to this Vector
1616 // type. If so, convert to the packed type.
1617 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1618 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1619 // Turn this into a normal store of the packed type.
1620 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1622 Node->getOperand(3));
Chris Lattner2efce0a2006-04-16 01:36:45 +00001623 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001624 break;
1625 } else if (NumElems == 1) {
1626 // Turn this into a normal store of the scalar type.
1627 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1628 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1629 Node->getOperand(3));
Chris Lattner2ae2e982006-03-31 17:37:22 +00001630 // The scalarized value type may not be legal, e.g. it might require
1631 // promotion or expansion. Relegalize the scalar store.
1632 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001633 break;
1634 } else {
1635 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1636 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1637 }
1638 } else {
1639 ExpandOp(Node->getOperand(1), Lo, Hi);
1640 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001641
Chris Lattnerd9731af2006-03-31 18:20:46 +00001642 if (!TLI.isLittleEndian())
1643 std::swap(Lo, Hi);
1644 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001645
Chris Lattneredb1add2005-05-11 04:51:16 +00001646 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1647 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001648 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1649 getIntPtrConstant(IncrementSize));
1650 assert(isTypeLegal(Tmp2.getValueType()) &&
1651 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001652 // FIXME: This sets the srcvalue of both halves to be the same, which is
1653 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001654 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1655 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001656 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1657 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001658 }
1659 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001660 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001661 case ISD::PCMARKER:
1662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001663 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001664 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001665 case ISD::STACKSAVE:
1666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001667 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1668 Tmp1 = Result.getValue(0);
1669 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001670
Chris Lattner140d53c2006-01-13 02:50:02 +00001671 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1672 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001673 case TargetLowering::Legal: break;
1674 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001675 Tmp3 = TLI.LowerOperation(Result, DAG);
1676 if (Tmp3.Val) {
1677 Tmp1 = LegalizeOp(Tmp3);
1678 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001679 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001680 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001681 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001682 // Expand to CopyFromReg if the target set
1683 // StackPointerRegisterToSaveRestore.
1684 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001685 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001686 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001687 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001688 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001689 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1690 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001691 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001692 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001693 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001694
1695 // Since stacksave produce two values, make sure to remember that we
1696 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001697 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1698 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1699 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001700
Chris Lattner140d53c2006-01-13 02:50:02 +00001701 case ISD::STACKRESTORE:
1702 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1703 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001704 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001705
1706 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1707 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001708 case TargetLowering::Legal: break;
1709 case TargetLowering::Custom:
1710 Tmp1 = TLI.LowerOperation(Result, DAG);
1711 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001712 break;
1713 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001714 // Expand to CopyToReg if the target set
1715 // StackPointerRegisterToSaveRestore.
1716 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1717 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1718 } else {
1719 Result = Tmp1;
1720 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001721 break;
1722 }
1723 break;
1724
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001725 case ISD::READCYCLECOUNTER:
1726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001727 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001728
1729 // Since rdcc produce two values, make sure to remember that we legalized
1730 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001731 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001732 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001733 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001734
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001735 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001736 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1737 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1738
Chris Lattner456a93a2006-01-28 07:39:30 +00001739 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1740 "Cannot handle illegal TRUNCSTORE yet!");
1741 Tmp2 = LegalizeOp(Node->getOperand(1));
1742
1743 // The only promote case we handle is TRUNCSTORE:i1 X into
1744 // -> TRUNCSTORE:i8 (and X, 1)
1745 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1746 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1747 TargetLowering::Promote) {
1748 // Promote the bool to a mask then store.
1749 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1750 DAG.getConstant(1, Tmp2.getValueType()));
1751 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1752 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001753
Chris Lattner456a93a2006-01-28 07:39:30 +00001754 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1755 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1757 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001758 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001759
Chris Lattner456a93a2006-01-28 07:39:30 +00001760 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1761 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1762 default: assert(0 && "This action is not supported yet!");
1763 case TargetLowering::Legal: break;
1764 case TargetLowering::Custom:
1765 Tmp1 = TLI.LowerOperation(Result, DAG);
1766 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001767 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001768 }
1769 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001770 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001771 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001772 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1773 case Expand: assert(0 && "It's impossible to expand bools");
1774 case Legal:
1775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1776 break;
1777 case Promote:
1778 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1779 break;
1780 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001781 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001782 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001783
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001784 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001785
Nate Begemanb942a3d2005-08-23 04:29:48 +00001786 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001787 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001788 case TargetLowering::Legal: break;
1789 case TargetLowering::Custom: {
1790 Tmp1 = TLI.LowerOperation(Result, DAG);
1791 if (Tmp1.Val) Result = Tmp1;
1792 break;
1793 }
Nate Begeman9373a812005-08-10 20:51:12 +00001794 case TargetLowering::Expand:
1795 if (Tmp1.getOpcode() == ISD::SETCC) {
1796 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1797 Tmp2, Tmp3,
1798 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1799 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001800 // Make sure the condition is either zero or one. It may have been
1801 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001802 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1803 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1804 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001805 Result = DAG.getSelectCC(Tmp1,
1806 DAG.getConstant(0, Tmp1.getValueType()),
1807 Tmp2, Tmp3, ISD::SETNE);
1808 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001809 break;
1810 case TargetLowering::Promote: {
1811 MVT::ValueType NVT =
1812 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1813 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001814 if (MVT::isVector(Tmp2.getValueType())) {
1815 ExtOp = ISD::BIT_CONVERT;
1816 TruncOp = ISD::BIT_CONVERT;
1817 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001818 ExtOp = ISD::ANY_EXTEND;
1819 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001820 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001821 ExtOp = ISD::FP_EXTEND;
1822 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001823 }
1824 // Promote each of the values to the new type.
1825 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1826 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1827 // Perform the larger operation, then round down.
1828 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1829 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1830 break;
1831 }
1832 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001833 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001834 case ISD::SELECT_CC: {
1835 Tmp1 = Node->getOperand(0); // LHS
1836 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001837 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1838 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001839 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001840
Nate Begeman750ac1b2006-02-01 07:19:44 +00001841 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1842
1843 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1844 // the LHS is a legal SETCC itself. In this case, we need to compare
1845 // the result against zero to select between true and false values.
1846 if (Tmp2.Val == 0) {
1847 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1848 CC = DAG.getCondCode(ISD::SETNE);
1849 }
1850 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1851
1852 // Everything is legal, see if we should expand this op or something.
1853 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1854 default: assert(0 && "This action is not supported yet!");
1855 case TargetLowering::Legal: break;
1856 case TargetLowering::Custom:
1857 Tmp1 = TLI.LowerOperation(Result, DAG);
1858 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001859 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001860 }
1861 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001862 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001863 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001864 Tmp1 = Node->getOperand(0);
1865 Tmp2 = Node->getOperand(1);
1866 Tmp3 = Node->getOperand(2);
1867 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1868
1869 // If we had to Expand the SetCC operands into a SELECT node, then it may
1870 // not always be possible to return a true LHS & RHS. In this case, just
1871 // return the value we legalized, returned in the LHS
1872 if (Tmp2.Val == 0) {
1873 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001874 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001875 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001876
Chris Lattner73e142f2006-01-30 22:43:50 +00001877 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001878 default: assert(0 && "Cannot handle this action for SETCC yet!");
1879 case TargetLowering::Custom:
1880 isCustom = true;
1881 // FALLTHROUGH.
1882 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001884 if (isCustom) {
1885 Tmp3 = TLI.LowerOperation(Result, DAG);
1886 if (Tmp3.Val) Result = Tmp3;
1887 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001888 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001889 case TargetLowering::Promote: {
1890 // First step, figure out the appropriate operation to use.
1891 // Allow SETCC to not be supported for all legal data types
1892 // Mostly this targets FP
1893 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1894 MVT::ValueType OldVT = NewInTy;
1895
1896 // Scan for the appropriate larger type to use.
1897 while (1) {
1898 NewInTy = (MVT::ValueType)(NewInTy+1);
1899
1900 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1901 "Fell off of the edge of the integer world");
1902 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1903 "Fell off of the edge of the floating point world");
1904
1905 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001906 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001907 break;
1908 }
1909 if (MVT::isInteger(NewInTy))
1910 assert(0 && "Cannot promote Legal Integer SETCC yet");
1911 else {
1912 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1913 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1914 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001915 Tmp1 = LegalizeOp(Tmp1);
1916 Tmp2 = LegalizeOp(Tmp2);
1917 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001918 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001919 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001920 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001921 case TargetLowering::Expand:
1922 // Expand a setcc node into a select_cc of the same condition, lhs, and
1923 // rhs that selects between const 1 (true) and const 0 (false).
1924 MVT::ValueType VT = Node->getValueType(0);
1925 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1926 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1927 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001928 break;
1929 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001931 case ISD::MEMSET:
1932 case ISD::MEMCPY:
1933 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001934 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001935 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1936
1937 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1938 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1939 case Expand: assert(0 && "Cannot expand a byte!");
1940 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001941 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001942 break;
1943 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001944 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001945 break;
1946 }
1947 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001948 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001949 }
Chris Lattner272455b2005-02-02 03:44:41 +00001950
1951 SDOperand Tmp4;
1952 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001953 case Expand: {
1954 // Length is too big, just take the lo-part of the length.
1955 SDOperand HiPart;
1956 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1957 break;
1958 }
Chris Lattnere5605212005-01-28 22:29:18 +00001959 case Legal:
1960 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001961 break;
1962 case Promote:
1963 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001964 break;
1965 }
1966
1967 SDOperand Tmp5;
1968 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1969 case Expand: assert(0 && "Cannot expand this yet!");
1970 case Legal:
1971 Tmp5 = LegalizeOp(Node->getOperand(4));
1972 break;
1973 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001974 Tmp5 = PromoteOp(Node->getOperand(4));
1975 break;
1976 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001977
1978 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1979 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001980 case TargetLowering::Custom:
1981 isCustom = true;
1982 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001983 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001985 if (isCustom) {
1986 Tmp1 = TLI.LowerOperation(Result, DAG);
1987 if (Tmp1.Val) Result = Tmp1;
1988 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001989 break;
1990 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001991 // Otherwise, the target does not support this operation. Lower the
1992 // operation to an explicit libcall as appropriate.
1993 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00001994 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Chris Lattnere1bd8222005-01-11 05:57:22 +00001995 std::vector<std::pair<SDOperand, const Type*> > Args;
1996
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001997 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001998 if (Node->getOpcode() == ISD::MEMSET) {
1999 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002000 // Extend the (previously legalized) ubyte argument to be an int value
2001 // for the call.
2002 if (Tmp3.getValueType() > MVT::i32)
2003 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2004 else
2005 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002006 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
2007 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2008
2009 FnName = "memset";
2010 } else if (Node->getOpcode() == ISD::MEMCPY ||
2011 Node->getOpcode() == ISD::MEMMOVE) {
2012 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
2013 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
2014 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2015 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2016 } else {
2017 assert(0 && "Unknown op!");
2018 }
Chris Lattner45982da2005-05-12 16:53:42 +00002019
Chris Lattnere1bd8222005-01-11 05:57:22 +00002020 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002021 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002022 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002023 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002024 break;
2025 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002026 }
2027 break;
2028 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002029
Chris Lattner5b359c62005-04-02 04:00:59 +00002030 case ISD::SHL_PARTS:
2031 case ISD::SRA_PARTS:
2032 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002033 std::vector<SDOperand> Ops;
2034 bool Changed = false;
2035 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2036 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2037 Changed |= Ops.back() != Node->getOperand(i);
2038 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002039 if (Changed)
2040 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00002041
Evan Cheng05a2d562006-01-09 18:31:59 +00002042 switch (TLI.getOperationAction(Node->getOpcode(),
2043 Node->getValueType(0))) {
2044 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002045 case TargetLowering::Legal: break;
2046 case TargetLowering::Custom:
2047 Tmp1 = TLI.LowerOperation(Result, DAG);
2048 if (Tmp1.Val) {
2049 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002050 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002051 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002052 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2053 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002054 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002055 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002056 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002057 return RetVal;
2058 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002059 break;
2060 }
2061
Chris Lattner2c8086f2005-04-02 05:00:07 +00002062 // Since these produce multiple values, make sure to remember that we
2063 // legalized all of them.
2064 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2065 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2066 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002067 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002068
2069 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002070 case ISD::ADD:
2071 case ISD::SUB:
2072 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002073 case ISD::MULHS:
2074 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002075 case ISD::UDIV:
2076 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002077 case ISD::AND:
2078 case ISD::OR:
2079 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002080 case ISD::SHL:
2081 case ISD::SRL:
2082 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002083 case ISD::FADD:
2084 case ISD::FSUB:
2085 case ISD::FMUL:
2086 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002087 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002088 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2089 case Expand: assert(0 && "Not possible");
2090 case Legal:
2091 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2092 break;
2093 case Promote:
2094 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2095 break;
2096 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002097
2098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002099
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002100 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002101 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002102 case TargetLowering::Legal: break;
2103 case TargetLowering::Custom:
2104 Tmp1 = TLI.LowerOperation(Result, DAG);
2105 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002106 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002107 case TargetLowering::Expand: {
2108 assert(MVT::isVector(Node->getValueType(0)) &&
2109 "Cannot expand this binary operator!");
2110 // Expand the operation into a bunch of nasty scalar code.
2111 std::vector<SDOperand> Ops;
2112 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2113 MVT::ValueType PtrVT = TLI.getPointerTy();
2114 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2115 i != e; ++i) {
2116 SDOperand Idx = DAG.getConstant(i, PtrVT);
2117 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2118 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2119 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2120 }
2121 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2122 break;
2123 }
Evan Chengcc987612006-04-12 21:20:24 +00002124 case TargetLowering::Promote: {
2125 switch (Node->getOpcode()) {
2126 default: assert(0 && "Do not know how to promote this BinOp!");
2127 case ISD::AND:
2128 case ISD::OR:
2129 case ISD::XOR: {
2130 MVT::ValueType OVT = Node->getValueType(0);
2131 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2132 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2133 // Bit convert each of the values to the new type.
2134 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2135 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2136 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2137 // Bit convert the result back the original type.
2138 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2139 break;
2140 }
2141 }
2142 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002143 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002144 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002145
2146 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2147 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2148 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2149 case Expand: assert(0 && "Not possible");
2150 case Legal:
2151 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2152 break;
2153 case Promote:
2154 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2155 break;
2156 }
2157
2158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2159
2160 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2161 default: assert(0 && "Operation not supported");
2162 case TargetLowering::Custom:
2163 Tmp1 = TLI.LowerOperation(Result, DAG);
2164 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002165 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002166 case TargetLowering::Legal: break;
2167 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002168 // If this target supports fabs/fneg natively, do this efficiently.
2169 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2170 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2171 // Get the sign bit of the RHS.
2172 MVT::ValueType IVT =
2173 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2174 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2175 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2176 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2177 // Get the absolute value of the result.
2178 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2179 // Select between the nabs and abs value based on the sign bit of
2180 // the input.
2181 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2182 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2183 AbsVal),
2184 AbsVal);
2185 Result = LegalizeOp(Result);
2186 break;
2187 }
2188
2189 // Otherwise, do bitwise ops!
2190
2191 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002192 const char *FnName;
2193 if (Node->getValueType(0) == MVT::f32) {
2194 FnName = "copysignf";
2195 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2196 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2197 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2198 } else {
2199 FnName = "copysign";
2200 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2201 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2202 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2203 }
2204 SDOperand Dummy;
2205 Result = ExpandLibCall(FnName, Node, Dummy);
2206 break;
2207 }
2208 break;
2209
Nate Begeman551bf3f2006-02-17 05:43:56 +00002210 case ISD::ADDC:
2211 case ISD::SUBC:
2212 Tmp1 = LegalizeOp(Node->getOperand(0));
2213 Tmp2 = LegalizeOp(Node->getOperand(1));
2214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2215 // Since this produces two values, make sure to remember that we legalized
2216 // both of them.
2217 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2218 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2219 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002220
Nate Begeman551bf3f2006-02-17 05:43:56 +00002221 case ISD::ADDE:
2222 case ISD::SUBE:
2223 Tmp1 = LegalizeOp(Node->getOperand(0));
2224 Tmp2 = LegalizeOp(Node->getOperand(1));
2225 Tmp3 = LegalizeOp(Node->getOperand(2));
2226 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2227 // Since this produces two values, make sure to remember that we legalized
2228 // both of them.
2229 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2230 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2231 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002232
Nate Begeman419f8b62005-10-18 00:27:41 +00002233 case ISD::BUILD_PAIR: {
2234 MVT::ValueType PairTy = Node->getValueType(0);
2235 // TODO: handle the case where the Lo and Hi operands are not of legal type
2236 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2237 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2238 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002239 case TargetLowering::Promote:
2240 case TargetLowering::Custom:
2241 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002242 case TargetLowering::Legal:
2243 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2244 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2245 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002246 case TargetLowering::Expand:
2247 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2248 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2249 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2250 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2251 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002252 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002253 break;
2254 }
2255 break;
2256 }
2257
Nate Begemanc105e192005-04-06 00:23:54 +00002258 case ISD::UREM:
2259 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002260 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002261 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2262 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002263
Nate Begemanc105e192005-04-06 00:23:54 +00002264 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002265 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2266 case TargetLowering::Custom:
2267 isCustom = true;
2268 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002269 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002270 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002271 if (isCustom) {
2272 Tmp1 = TLI.LowerOperation(Result, DAG);
2273 if (Tmp1.Val) Result = Tmp1;
2274 }
Nate Begemanc105e192005-04-06 00:23:54 +00002275 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002276 case TargetLowering::Expand:
2277 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002278 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002279 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002280 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002281 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2282 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2283 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2284 } else {
2285 // Floating point mod -> fmod libcall.
2286 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2287 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002288 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002289 }
2290 break;
2291 }
2292 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002293 case ISD::VAARG: {
2294 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2295 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2296
Chris Lattner5c62f332006-01-28 07:42:08 +00002297 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002298 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2299 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002300 case TargetLowering::Custom:
2301 isCustom = true;
2302 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002303 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2305 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002306 Tmp1 = Result.getValue(1);
2307
2308 if (isCustom) {
2309 Tmp2 = TLI.LowerOperation(Result, DAG);
2310 if (Tmp2.Val) {
2311 Result = LegalizeOp(Tmp2);
2312 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2313 }
2314 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002315 break;
2316 case TargetLowering::Expand: {
2317 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2318 Node->getOperand(2));
2319 // Increment the pointer, VAList, to the next vaarg
2320 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2321 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2322 TLI.getPointerTy()));
2323 // Store the incremented VAList to the legalized pointer
2324 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2325 Node->getOperand(2));
2326 // Load the actual argument out of the pointer VAList
2327 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002328 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002329 Result = LegalizeOp(Result);
2330 break;
2331 }
2332 }
2333 // Since VAARG produces two values, make sure to remember that we
2334 // legalized both of them.
2335 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002336 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2337 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002338 }
2339
2340 case ISD::VACOPY:
2341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2342 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2343 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2344
2345 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2346 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002347 case TargetLowering::Custom:
2348 isCustom = true;
2349 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002350 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002351 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2352 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002353 if (isCustom) {
2354 Tmp1 = TLI.LowerOperation(Result, DAG);
2355 if (Tmp1.Val) Result = Tmp1;
2356 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002357 break;
2358 case TargetLowering::Expand:
2359 // This defaults to loading a pointer from the input and storing it to the
2360 // output, returning the chain.
2361 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2362 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2363 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002364 break;
2365 }
2366 break;
2367
2368 case ISD::VAEND:
2369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2370 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2371
2372 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2373 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002374 case TargetLowering::Custom:
2375 isCustom = true;
2376 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002377 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002378 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002379 if (isCustom) {
2380 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2381 if (Tmp1.Val) Result = Tmp1;
2382 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002383 break;
2384 case TargetLowering::Expand:
2385 Result = Tmp1; // Default to a no-op, return the chain
2386 break;
2387 }
2388 break;
2389
2390 case ISD::VASTART:
2391 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2392 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2393
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002394 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2395
Nate Begemanacc398c2006-01-25 18:21:52 +00002396 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2397 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002398 case TargetLowering::Legal: break;
2399 case TargetLowering::Custom:
2400 Tmp1 = TLI.LowerOperation(Result, DAG);
2401 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002402 break;
2403 }
2404 break;
2405
Nate Begeman35ef9132006-01-11 21:21:00 +00002406 case ISD::ROTL:
2407 case ISD::ROTR:
2408 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2409 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002410
2411 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2412 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002414 break;
2415
Nate Begemand88fc032006-01-14 03:14:10 +00002416 case ISD::BSWAP:
2417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2418 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002419 case TargetLowering::Custom:
2420 assert(0 && "Cannot custom legalize this yet!");
2421 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002422 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002423 break;
2424 case TargetLowering::Promote: {
2425 MVT::ValueType OVT = Tmp1.getValueType();
2426 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2427 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002428
Chris Lattner456a93a2006-01-28 07:39:30 +00002429 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2430 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2431 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2432 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2433 break;
2434 }
2435 case TargetLowering::Expand:
2436 Result = ExpandBSWAP(Tmp1);
2437 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002438 }
2439 break;
2440
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002441 case ISD::CTPOP:
2442 case ISD::CTTZ:
2443 case ISD::CTLZ:
2444 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2445 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002446 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002447 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002448 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002449 break;
2450 case TargetLowering::Promote: {
2451 MVT::ValueType OVT = Tmp1.getValueType();
2452 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002453
2454 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002455 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2456 // Perform the larger operation, then subtract if needed.
2457 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002458 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002459 case ISD::CTPOP:
2460 Result = Tmp1;
2461 break;
2462 case ISD::CTTZ:
2463 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002464 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2465 DAG.getConstant(getSizeInBits(NVT), NVT),
2466 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002467 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002468 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2469 break;
2470 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002471 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002472 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2473 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002474 getSizeInBits(OVT), NVT));
2475 break;
2476 }
2477 break;
2478 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002479 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002480 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002481 break;
2482 }
2483 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002484
Chris Lattner2c8086f2005-04-02 05:00:07 +00002485 // Unary operators
2486 case ISD::FABS:
2487 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002488 case ISD::FSQRT:
2489 case ISD::FSIN:
2490 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002491 Tmp1 = LegalizeOp(Node->getOperand(0));
2492 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002493 case TargetLowering::Promote:
2494 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002495 isCustom = true;
2496 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002497 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002498 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002499 if (isCustom) {
2500 Tmp1 = TLI.LowerOperation(Result, DAG);
2501 if (Tmp1.Val) Result = Tmp1;
2502 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002503 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002504 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002505 switch (Node->getOpcode()) {
2506 default: assert(0 && "Unreachable!");
2507 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002508 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2509 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002510 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002511 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002512 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002513 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2514 MVT::ValueType VT = Node->getValueType(0);
2515 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002516 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002517 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2518 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002519 break;
2520 }
2521 case ISD::FSQRT:
2522 case ISD::FSIN:
2523 case ISD::FCOS: {
2524 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002525 const char *FnName = 0;
2526 switch(Node->getOpcode()) {
2527 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2528 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2529 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2530 default: assert(0 && "Unreachable!");
2531 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002532 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002533 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002534 break;
2535 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002536 }
2537 break;
2538 }
2539 break;
Chris Lattner35481892005-12-23 00:16:34 +00002540
2541 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002542 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002543 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002544 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002545 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2546 Node->getOperand(0).getValueType())) {
2547 default: assert(0 && "Unknown operation action!");
2548 case TargetLowering::Expand:
2549 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2550 break;
2551 case TargetLowering::Legal:
2552 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002553 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002554 break;
2555 }
2556 }
2557 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002558 case ISD::VBIT_CONVERT: {
2559 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2560 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2561
2562 // The input has to be a vector type, we have to either scalarize it, pack
2563 // it, or convert it based on whether the input vector type is legal.
2564 SDNode *InVal = Node->getOperand(0).Val;
2565 unsigned NumElems =
2566 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2567 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2568
2569 // Figure out if there is a Packed type corresponding to this Vector
2570 // type. If so, convert to the packed type.
2571 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2572 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2573 // Turn this into a bit convert of the packed input.
2574 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2575 PackVectorOp(Node->getOperand(0), TVT));
2576 break;
2577 } else if (NumElems == 1) {
2578 // Turn this into a bit convert of the scalar input.
2579 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2580 PackVectorOp(Node->getOperand(0), EVT));
2581 break;
2582 } else {
2583 // FIXME: UNIMP! Store then reload
2584 assert(0 && "Cast from unsupported vector type not implemented yet!");
2585 }
2586 }
2587
Chris Lattner2c8086f2005-04-02 05:00:07 +00002588 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002589 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002590 case ISD::UINT_TO_FP: {
2591 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002592 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2593 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002594 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002595 Node->getOperand(0).getValueType())) {
2596 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002597 case TargetLowering::Custom:
2598 isCustom = true;
2599 // FALLTHROUGH
2600 case TargetLowering::Legal:
2601 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002602 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002603 if (isCustom) {
2604 Tmp1 = TLI.LowerOperation(Result, DAG);
2605 if (Tmp1.Val) Result = Tmp1;
2606 }
2607 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002608 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002609 Result = ExpandLegalINT_TO_FP(isSigned,
2610 LegalizeOp(Node->getOperand(0)),
2611 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002612 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002613 case TargetLowering::Promote:
2614 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2615 Node->getValueType(0),
2616 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002617 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002618 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002619 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002620 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002621 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2622 Node->getValueType(0), Node->getOperand(0));
2623 break;
2624 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002625 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002626 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002627 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2628 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002629 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002630 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2631 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002632 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2634 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002635 break;
2636 }
2637 break;
2638 }
2639 case ISD::TRUNCATE:
2640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2641 case Legal:
2642 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002643 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002644 break;
2645 case Expand:
2646 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2647
2648 // Since the result is legal, we should just be able to truncate the low
2649 // part of the source.
2650 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2651 break;
2652 case Promote:
2653 Result = PromoteOp(Node->getOperand(0));
2654 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2655 break;
2656 }
2657 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002658
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002659 case ISD::FP_TO_SINT:
2660 case ISD::FP_TO_UINT:
2661 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2662 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002663 Tmp1 = LegalizeOp(Node->getOperand(0));
2664
Chris Lattner1618beb2005-07-29 00:11:56 +00002665 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2666 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002667 case TargetLowering::Custom:
2668 isCustom = true;
2669 // FALLTHROUGH
2670 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002671 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002672 if (isCustom) {
2673 Tmp1 = TLI.LowerOperation(Result, DAG);
2674 if (Tmp1.Val) Result = Tmp1;
2675 }
2676 break;
2677 case TargetLowering::Promote:
2678 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2679 Node->getOpcode() == ISD::FP_TO_SINT);
2680 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002681 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002682 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2683 SDOperand True, False;
2684 MVT::ValueType VT = Node->getOperand(0).getValueType();
2685 MVT::ValueType NVT = Node->getValueType(0);
2686 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2687 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2688 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2689 Node->getOperand(0), Tmp2, ISD::SETLT);
2690 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2691 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002692 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002693 Tmp2));
2694 False = DAG.getNode(ISD::XOR, NVT, False,
2695 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002696 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2697 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002698 } else {
2699 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2700 }
2701 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002702 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002703 break;
2704 case Expand:
2705 assert(0 && "Shouldn't need to expand other operators here!");
2706 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002707 Tmp1 = PromoteOp(Node->getOperand(0));
2708 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2709 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002710 break;
2711 }
2712 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002713
Chris Lattner13c78e22005-09-02 00:18:10 +00002714 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002715 case ISD::ZERO_EXTEND:
2716 case ISD::SIGN_EXTEND:
2717 case ISD::FP_EXTEND:
2718 case ISD::FP_ROUND:
2719 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002721 case Legal:
2722 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002723 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002724 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002725 case Promote:
2726 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002727 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002728 Tmp1 = PromoteOp(Node->getOperand(0));
2729 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002730 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002731 case ISD::ZERO_EXTEND:
2732 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002733 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002734 Result = DAG.getZeroExtendInReg(Result,
2735 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002736 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002737 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002738 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002739 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002740 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002741 Result,
2742 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002743 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002744 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002745 Result = PromoteOp(Node->getOperand(0));
2746 if (Result.getValueType() != Op.getValueType())
2747 // Dynamically dead while we have only 2 FP types.
2748 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2749 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002750 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002751 Result = PromoteOp(Node->getOperand(0));
2752 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2753 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002754 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002755 }
2756 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002757 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002758 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002759 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002760 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002761
2762 // If this operation is not supported, convert it to a shl/shr or load/store
2763 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002764 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2765 default: assert(0 && "This action not supported for this op yet!");
2766 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002768 break;
2769 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002770 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002771 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002772 // NOTE: we could fall back on load/store here too for targets without
2773 // SAR. However, it is doubtful that any exist.
2774 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2775 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002776 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002777 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2778 Node->getOperand(0), ShiftCst);
2779 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2780 Result, ShiftCst);
2781 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2782 // The only way we can lower this is to turn it into a STORETRUNC,
2783 // EXTLOAD pair, targetting a temporary location (a stack slot).
2784
2785 // NOTE: there is a choice here between constantly creating new stack
2786 // slots and always reusing the same one. We currently always create
2787 // new ones, as reuse may inhibit scheduling.
2788 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00002789 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2790 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002791 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002792 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002793 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2794 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2795 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002796 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002797 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002798 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2799 Result, StackSlot, DAG.getSrcValue(NULL),
2800 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002801 } else {
2802 assert(0 && "Unknown op");
2803 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002804 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002805 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002806 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002807 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002808 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002809
Chris Lattner4ddd2832006-04-08 04:13:17 +00002810 assert(Result.getValueType() == Op.getValueType() &&
2811 "Bad legalization!");
2812
Chris Lattner456a93a2006-01-28 07:39:30 +00002813 // Make sure that the generated code is itself legal.
2814 if (Result != Op)
2815 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002816
Chris Lattner45982da2005-05-12 16:53:42 +00002817 // Note that LegalizeOp may be reentered even from single-use nodes, which
2818 // means that we always must cache transformed nodes.
2819 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002820 return Result;
2821}
2822
Chris Lattner8b6fa222005-01-15 22:16:26 +00002823/// PromoteOp - Given an operation that produces a value in an invalid type,
2824/// promote it to compute the value into a larger type. The produced value will
2825/// have the correct bits for the low portion of the register, but no guarantee
2826/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002827SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2828 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002829 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002830 assert(getTypeAction(VT) == Promote &&
2831 "Caller should expand or legalize operands that are not promotable!");
2832 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2833 "Cannot promote to smaller type!");
2834
Chris Lattner03c85462005-01-15 05:21:40 +00002835 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002836 SDOperand Result;
2837 SDNode *Node = Op.Val;
2838
Chris Lattner6fdcb252005-09-02 20:32:45 +00002839 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2840 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002841
Chris Lattner03c85462005-01-15 05:21:40 +00002842 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002843 case ISD::CopyFromReg:
2844 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002845 default:
2846 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2847 assert(0 && "Do not know how to promote this operator!");
2848 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002849 case ISD::UNDEF:
2850 Result = DAG.getNode(ISD::UNDEF, NVT);
2851 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002852 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002853 if (VT != MVT::i1)
2854 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2855 else
2856 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002857 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2858 break;
2859 case ISD::ConstantFP:
2860 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2861 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2862 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002863
Chris Lattner82fbfb62005-01-18 02:59:52 +00002864 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002865 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002866 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2867 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002868 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00002869
Chris Lattner03c85462005-01-15 05:21:40 +00002870 case ISD::TRUNCATE:
2871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2872 case Legal:
2873 Result = LegalizeOp(Node->getOperand(0));
2874 assert(Result.getValueType() >= NVT &&
2875 "This truncation doesn't make sense!");
2876 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2877 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2878 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002879 case Promote:
2880 // The truncation is not required, because we don't guarantee anything
2881 // about high bits anyway.
2882 Result = PromoteOp(Node->getOperand(0));
2883 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002884 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002885 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2886 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002887 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002888 }
2889 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002890 case ISD::SIGN_EXTEND:
2891 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002892 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002893 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2894 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2895 case Legal:
2896 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002897 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002898 break;
2899 case Promote:
2900 // Promote the reg if it's smaller.
2901 Result = PromoteOp(Node->getOperand(0));
2902 // The high bits are not guaranteed to be anything. Insert an extend.
2903 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002904 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002905 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002906 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002907 Result = DAG.getZeroExtendInReg(Result,
2908 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002909 break;
2910 }
2911 break;
Chris Lattner35481892005-12-23 00:16:34 +00002912 case ISD::BIT_CONVERT:
2913 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2914 Result = PromoteOp(Result);
2915 break;
2916
Chris Lattner8b6fa222005-01-15 22:16:26 +00002917 case ISD::FP_EXTEND:
2918 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2919 case ISD::FP_ROUND:
2920 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2921 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2922 case Promote: assert(0 && "Unreachable with 2 FP types!");
2923 case Legal:
2924 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002925 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002926 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002927 break;
2928 }
2929 break;
2930
2931 case ISD::SINT_TO_FP:
2932 case ISD::UINT_TO_FP:
2933 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2934 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002935 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002936 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002937 break;
2938
2939 case Promote:
2940 Result = PromoteOp(Node->getOperand(0));
2941 if (Node->getOpcode() == ISD::SINT_TO_FP)
2942 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002943 Result,
2944 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002945 else
Chris Lattner23993562005-04-13 02:38:47 +00002946 Result = DAG.getZeroExtendInReg(Result,
2947 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002948 // No extra round required here.
2949 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002950 break;
2951 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002952 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2953 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002954 // Round if we cannot tolerate excess precision.
2955 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002956 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2957 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002958 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002959 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002960 break;
2961
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002962 case ISD::SIGN_EXTEND_INREG:
2963 Result = PromoteOp(Node->getOperand(0));
2964 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2965 Node->getOperand(1));
2966 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002967 case ISD::FP_TO_SINT:
2968 case ISD::FP_TO_UINT:
2969 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2970 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002971 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002972 break;
2973 case Promote:
2974 // The input result is prerounded, so we don't have to do anything
2975 // special.
2976 Tmp1 = PromoteOp(Node->getOperand(0));
2977 break;
2978 case Expand:
2979 assert(0 && "not implemented");
2980 }
Nate Begemand2558e32005-08-14 01:20:53 +00002981 // If we're promoting a UINT to a larger size, check to see if the new node
2982 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2983 // we can use that instead. This allows us to generate better code for
2984 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2985 // legal, such as PowerPC.
2986 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002987 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002988 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2989 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002990 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2991 } else {
2992 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2993 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002994 break;
2995
Chris Lattner2c8086f2005-04-02 05:00:07 +00002996 case ISD::FABS:
2997 case ISD::FNEG:
2998 Tmp1 = PromoteOp(Node->getOperand(0));
2999 assert(Tmp1.getValueType() == NVT);
3000 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3001 // NOTE: we do not have to do any extra rounding here for
3002 // NoExcessFPPrecision, because we know the input will have the appropriate
3003 // precision, and these operations don't modify precision at all.
3004 break;
3005
Chris Lattnerda6ba872005-04-28 21:44:33 +00003006 case ISD::FSQRT:
3007 case ISD::FSIN:
3008 case ISD::FCOS:
3009 Tmp1 = PromoteOp(Node->getOperand(0));
3010 assert(Tmp1.getValueType() == NVT);
3011 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003012 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003013 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3014 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003015 break;
3016
Chris Lattner03c85462005-01-15 05:21:40 +00003017 case ISD::AND:
3018 case ISD::OR:
3019 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003020 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003021 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003022 case ISD::MUL:
3023 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003024 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003025 // that too is okay if they are integer operations.
3026 Tmp1 = PromoteOp(Node->getOperand(0));
3027 Tmp2 = PromoteOp(Node->getOperand(1));
3028 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3029 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003030 break;
3031 case ISD::FADD:
3032 case ISD::FSUB:
3033 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003034 Tmp1 = PromoteOp(Node->getOperand(0));
3035 Tmp2 = PromoteOp(Node->getOperand(1));
3036 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3037 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3038
3039 // Floating point operations will give excess precision that we may not be
3040 // able to tolerate. If we DO allow excess precision, just leave it,
3041 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003042 // FIXME: Why would we need to round FP ops more than integer ones?
3043 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003044 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003045 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3046 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003047 break;
3048
Chris Lattner8b6fa222005-01-15 22:16:26 +00003049 case ISD::SDIV:
3050 case ISD::SREM:
3051 // These operators require that their input be sign extended.
3052 Tmp1 = PromoteOp(Node->getOperand(0));
3053 Tmp2 = PromoteOp(Node->getOperand(1));
3054 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003055 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3056 DAG.getValueType(VT));
3057 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3058 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003059 }
3060 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3061
3062 // Perform FP_ROUND: this is probably overly pessimistic.
3063 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003064 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3065 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003066 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003067 case ISD::FDIV:
3068 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003069 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003070 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003071 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3072 case Legal:
3073 Tmp1 = LegalizeOp(Node->getOperand(0));
3074 break;
3075 case Promote:
3076 Tmp1 = PromoteOp(Node->getOperand(0));
3077 break;
3078 case Expand:
3079 assert(0 && "not implemented");
3080 }
3081 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3082 case Legal:
3083 Tmp2 = LegalizeOp(Node->getOperand(1));
3084 break;
3085 case Promote:
3086 Tmp2 = PromoteOp(Node->getOperand(1));
3087 break;
3088 case Expand:
3089 assert(0 && "not implemented");
3090 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003091 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3092
3093 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003094 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003095 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3096 DAG.getValueType(VT));
3097 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003098
3099 case ISD::UDIV:
3100 case ISD::UREM:
3101 // These operators require that their input be zero extended.
3102 Tmp1 = PromoteOp(Node->getOperand(0));
3103 Tmp2 = PromoteOp(Node->getOperand(1));
3104 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003105 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3106 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003107 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3108 break;
3109
3110 case ISD::SHL:
3111 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003112 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003113 break;
3114 case ISD::SRA:
3115 // The input value must be properly sign extended.
3116 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003117 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3118 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003119 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003120 break;
3121 case ISD::SRL:
3122 // The input value must be properly zero extended.
3123 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003124 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003125 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003126 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003127
3128 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003129 Tmp1 = Node->getOperand(0); // Get the chain.
3130 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003131 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3132 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3133 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3134 } else {
3135 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3136 Node->getOperand(2));
3137 // Increment the pointer, VAList, to the next vaarg
3138 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3139 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3140 TLI.getPointerTy()));
3141 // Store the incremented VAList to the legalized pointer
3142 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3143 Node->getOperand(2));
3144 // Load the actual argument out of the pointer VAList
3145 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3146 DAG.getSrcValue(0), VT);
3147 }
3148 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003149 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003150 break;
3151
Chris Lattner03c85462005-01-15 05:21:40 +00003152 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003153 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3154 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003155 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003156 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003157 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003158 case ISD::SEXTLOAD:
3159 case ISD::ZEXTLOAD:
3160 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003161 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3162 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00003163 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003164 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003165 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003166 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003167 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003168 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3169 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003170 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003171 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003172 case ISD::SELECT_CC:
3173 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3174 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3175 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003176 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003177 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003178 case ISD::BSWAP:
3179 Tmp1 = Node->getOperand(0);
3180 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3181 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3182 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3183 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3184 TLI.getShiftAmountTy()));
3185 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003186 case ISD::CTPOP:
3187 case ISD::CTTZ:
3188 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003189 // Zero extend the argument
3190 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003191 // Perform the larger operation, then subtract if needed.
3192 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003193 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003194 case ISD::CTPOP:
3195 Result = Tmp1;
3196 break;
3197 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003198 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003199 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003200 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003201 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003202 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003203 break;
3204 case ISD::CTLZ:
3205 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003206 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3207 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003208 getSizeInBits(VT), NVT));
3209 break;
3210 }
3211 break;
Chris Lattner15972212006-03-31 17:55:51 +00003212 case ISD::VEXTRACT_VECTOR_ELT:
3213 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3214 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003215 case ISD::EXTRACT_VECTOR_ELT:
3216 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3217 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003218 }
3219
3220 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003221
3222 // Make sure the result is itself legal.
3223 Result = LegalizeOp(Result);
3224
3225 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003226 AddPromotedOperand(Op, Result);
3227 return Result;
3228}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003229
Chris Lattner15972212006-03-31 17:55:51 +00003230/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3231/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3232/// on the vector type. The return type of this matches the element type of the
3233/// vector, which may not be legal for the target.
3234SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3235 // We know that operand #0 is the Vec vector. If the index is a constant
3236 // or if the invec is a supported hardware type, we can use it. Otherwise,
3237 // lower to a store then an indexed load.
3238 SDOperand Vec = Op.getOperand(0);
3239 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3240
3241 SDNode *InVal = Vec.Val;
3242 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3243 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3244
3245 // Figure out if there is a Packed type corresponding to this Vector
3246 // type. If so, convert to the packed type.
3247 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3248 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3249 // Turn this into a packed extract_vector_elt operation.
3250 Vec = PackVectorOp(Vec, TVT);
3251 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3252 } else if (NumElems == 1) {
3253 // This must be an access of the only element. Return it.
3254 return PackVectorOp(Vec, EVT);
3255 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3256 SDOperand Lo, Hi;
3257 SplitVectorOp(Vec, Lo, Hi);
3258 if (CIdx->getValue() < NumElems/2) {
3259 Vec = Lo;
3260 } else {
3261 Vec = Hi;
3262 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3263 }
3264
3265 // It's now an extract from the appropriate high or low part. Recurse.
3266 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3267 return LowerVEXTRACT_VECTOR_ELT(Op);
3268 } else {
3269 // Variable index case for extract element.
3270 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3271 assert(0 && "unimp!");
3272 return SDOperand();
3273 }
3274}
3275
Chris Lattner4aab2f42006-04-02 05:06:04 +00003276/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3277/// memory traffic.
3278SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3279 SDOperand Vector = Op.getOperand(0);
3280 SDOperand Idx = Op.getOperand(1);
3281
3282 // If the target doesn't support this, store the value to a temporary
3283 // stack slot, then LOAD the scalar element back out.
3284 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3285 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3286 Vector, StackPtr, DAG.getSrcValue(NULL));
3287
3288 // Add the offset to the index.
3289 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3290 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3291 DAG.getConstant(EltSize, Idx.getValueType()));
3292 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3293
3294 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3295}
3296
Chris Lattner15972212006-03-31 17:55:51 +00003297
Nate Begeman750ac1b2006-02-01 07:19:44 +00003298/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3299/// with condition CC on the current target. This usually involves legalizing
3300/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3301/// there may be no choice but to create a new SetCC node to represent the
3302/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3303/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3304void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3305 SDOperand &RHS,
3306 SDOperand &CC) {
3307 SDOperand Tmp1, Tmp2, Result;
3308
3309 switch (getTypeAction(LHS.getValueType())) {
3310 case Legal:
3311 Tmp1 = LegalizeOp(LHS); // LHS
3312 Tmp2 = LegalizeOp(RHS); // RHS
3313 break;
3314 case Promote:
3315 Tmp1 = PromoteOp(LHS); // LHS
3316 Tmp2 = PromoteOp(RHS); // RHS
3317
3318 // If this is an FP compare, the operands have already been extended.
3319 if (MVT::isInteger(LHS.getValueType())) {
3320 MVT::ValueType VT = LHS.getValueType();
3321 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3322
3323 // Otherwise, we have to insert explicit sign or zero extends. Note
3324 // that we could insert sign extends for ALL conditions, but zero extend
3325 // is cheaper on many machines (an AND instead of two shifts), so prefer
3326 // it.
3327 switch (cast<CondCodeSDNode>(CC)->get()) {
3328 default: assert(0 && "Unknown integer comparison!");
3329 case ISD::SETEQ:
3330 case ISD::SETNE:
3331 case ISD::SETUGE:
3332 case ISD::SETUGT:
3333 case ISD::SETULE:
3334 case ISD::SETULT:
3335 // ALL of these operations will work if we either sign or zero extend
3336 // the operands (including the unsigned comparisons!). Zero extend is
3337 // usually a simpler/cheaper operation, so prefer it.
3338 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3339 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3340 break;
3341 case ISD::SETGE:
3342 case ISD::SETGT:
3343 case ISD::SETLT:
3344 case ISD::SETLE:
3345 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3346 DAG.getValueType(VT));
3347 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3348 DAG.getValueType(VT));
3349 break;
3350 }
3351 }
3352 break;
3353 case Expand:
3354 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3355 ExpandOp(LHS, LHSLo, LHSHi);
3356 ExpandOp(RHS, RHSLo, RHSHi);
3357 switch (cast<CondCodeSDNode>(CC)->get()) {
3358 case ISD::SETEQ:
3359 case ISD::SETNE:
3360 if (RHSLo == RHSHi)
3361 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3362 if (RHSCST->isAllOnesValue()) {
3363 // Comparison to -1.
3364 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3365 Tmp2 = RHSLo;
3366 break;
3367 }
3368
3369 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3370 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3371 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3372 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3373 break;
3374 default:
3375 // If this is a comparison of the sign bit, just look at the top part.
3376 // X > -1, x < 0
3377 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3378 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3379 CST->getValue() == 0) || // X < 0
3380 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3381 CST->isAllOnesValue())) { // X > -1
3382 Tmp1 = LHSHi;
3383 Tmp2 = RHSHi;
3384 break;
3385 }
3386
3387 // FIXME: This generated code sucks.
3388 ISD::CondCode LowCC;
3389 switch (cast<CondCodeSDNode>(CC)->get()) {
3390 default: assert(0 && "Unknown integer setcc!");
3391 case ISD::SETLT:
3392 case ISD::SETULT: LowCC = ISD::SETULT; break;
3393 case ISD::SETGT:
3394 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3395 case ISD::SETLE:
3396 case ISD::SETULE: LowCC = ISD::SETULE; break;
3397 case ISD::SETGE:
3398 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3399 }
3400
3401 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3402 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3403 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3404
3405 // NOTE: on targets without efficient SELECT of bools, we can always use
3406 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3407 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3408 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3409 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3410 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3411 Result, Tmp1, Tmp2));
3412 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003413 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003414 }
3415 }
3416 LHS = Tmp1;
3417 RHS = Tmp2;
3418}
3419
Chris Lattner35481892005-12-23 00:16:34 +00003420/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003421/// The resultant code need not be legal. Note that SrcOp is the input operand
3422/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003423SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3424 SDOperand SrcOp) {
3425 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003426 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003427
3428 // Emit a store to the stack slot.
3429 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003430 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003431 // Result is a load from the stack slot.
3432 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3433}
3434
Chris Lattner4352cc92006-04-04 17:23:26 +00003435SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3436 // Create a vector sized/aligned stack slot, store the value to element #0,
3437 // then load the whole vector back out.
3438 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3439 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3440 Node->getOperand(0), StackPtr,
3441 DAG.getSrcValue(NULL));
3442 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3443}
3444
3445
Chris Lattnerce872152006-03-19 06:31:19 +00003446/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3447/// support the operation, but do support the resultant packed vector type.
3448SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3449
3450 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003451 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003452 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003453 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003454 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003455 std::map<SDOperand, std::vector<unsigned> > Values;
3456 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003457 bool isConstant = true;
3458 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3459 SplatValue.getOpcode() != ISD::UNDEF)
3460 isConstant = false;
3461
Evan Cheng033e6812006-03-24 01:17:21 +00003462 for (unsigned i = 1; i < NumElems; ++i) {
3463 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003464 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003465 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003466 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003467 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003468 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003469
3470 // If this isn't a constant element or an undef, we can't use a constant
3471 // pool load.
3472 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3473 V.getOpcode() != ISD::UNDEF)
3474 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003475 }
3476
3477 if (isOnlyLowElement) {
3478 // If the low element is an undef too, then this whole things is an undef.
3479 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3480 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3481 // Otherwise, turn this into a scalar_to_vector node.
3482 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3483 Node->getOperand(0));
3484 }
3485
Chris Lattner2eb86532006-03-24 07:29:17 +00003486 // If all elements are constants, create a load from the constant pool.
3487 if (isConstant) {
3488 MVT::ValueType VT = Node->getValueType(0);
3489 const Type *OpNTy =
3490 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3491 std::vector<Constant*> CV;
3492 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3493 if (ConstantFPSDNode *V =
3494 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3495 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3496 } else if (ConstantSDNode *V =
3497 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3498 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3499 } else {
3500 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3501 CV.push_back(UndefValue::get(OpNTy));
3502 }
3503 }
3504 Constant *CP = ConstantPacked::get(CV);
3505 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3506 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3507 DAG.getSrcValue(NULL));
3508 }
3509
Chris Lattner87100e02006-03-20 01:52:29 +00003510 if (SplatValue.Val) { // Splat of one value?
3511 // Build the shuffle constant vector: <0, 0, 0, 0>
3512 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003513 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003514 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003515 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003516 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3517
3518 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003519 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003520 // Get the splatted value into the low element of a vector register.
3521 SDOperand LowValVec =
3522 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3523
3524 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3525 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3526 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3527 SplatMask);
3528 }
3529 }
3530
Evan Cheng033e6812006-03-24 01:17:21 +00003531 // If there are only two unique elements, we may be able to turn this into a
3532 // vector shuffle.
3533 if (Values.size() == 2) {
3534 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3535 MVT::ValueType MaskVT =
3536 MVT::getIntVectorWithNumElements(NumElems);
3537 std::vector<SDOperand> MaskVec(NumElems);
3538 unsigned i = 0;
3539 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3540 E = Values.end(); I != E; ++I) {
3541 for (std::vector<unsigned>::iterator II = I->second.begin(),
3542 EE = I->second.end(); II != EE; ++II)
3543 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3544 i += NumElems;
3545 }
3546 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3547
3548 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003549 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3550 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng033e6812006-03-24 01:17:21 +00003551 std::vector<SDOperand> Ops;
3552 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3553 E = Values.end(); I != E; ++I) {
3554 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3555 I->first);
3556 Ops.push_back(Op);
3557 }
3558 Ops.push_back(ShuffleMask);
3559
3560 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3561 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3562 }
3563 }
Chris Lattnerce872152006-03-19 06:31:19 +00003564
3565 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3566 // aligned object on the stack, store each element into it, then load
3567 // the result as a vector.
3568 MVT::ValueType VT = Node->getValueType(0);
3569 // Create the stack frame object.
3570 SDOperand FIPtr = CreateStackTemporary(VT);
3571
3572 // Emit a store of each element to the stack slot.
3573 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003574 unsigned TypeByteSize =
3575 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3576 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3577 // Store (in the right endianness) the elements to memory.
3578 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3579 // Ignore undef elements.
3580 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3581
Chris Lattner841c8822006-03-22 01:46:54 +00003582 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003583
3584 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3585 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3586
3587 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3588 Node->getOperand(i), Idx,
3589 DAG.getSrcValue(NULL)));
3590 }
3591
3592 SDOperand StoreChain;
3593 if (!Stores.empty()) // Not all undef elements?
3594 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3595 else
3596 StoreChain = DAG.getEntryNode();
3597
3598 // Result is a load from the stack slot.
3599 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3600}
3601
3602/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3603/// specified value type.
3604SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3605 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3606 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3607 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3608 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3609}
3610
Chris Lattner5b359c62005-04-02 04:00:59 +00003611void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3612 SDOperand Op, SDOperand Amt,
3613 SDOperand &Lo, SDOperand &Hi) {
3614 // Expand the subcomponents.
3615 SDOperand LHSL, LHSH;
3616 ExpandOp(Op, LHSL, LHSH);
3617
3618 std::vector<SDOperand> Ops;
3619 Ops.push_back(LHSL);
3620 Ops.push_back(LHSH);
3621 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003622 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003623 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003624 Hi = Lo.getValue(1);
3625}
3626
3627
Chris Lattnere34b3962005-01-19 04:19:40 +00003628/// ExpandShift - Try to find a clever way to expand this shift operation out to
3629/// smaller elements. If we can't find a way that is more efficient than a
3630/// libcall on this target, return false. Otherwise, return true with the
3631/// low-parts expanded into Lo and Hi.
3632bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3633 SDOperand &Lo, SDOperand &Hi) {
3634 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3635 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003636
Chris Lattnere34b3962005-01-19 04:19:40 +00003637 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003638 SDOperand ShAmt = LegalizeOp(Amt);
3639 MVT::ValueType ShTy = ShAmt.getValueType();
3640 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3641 unsigned NVTBits = MVT::getSizeInBits(NVT);
3642
3643 // Handle the case when Amt is an immediate. Other cases are currently broken
3644 // and are disabled.
3645 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3646 unsigned Cst = CN->getValue();
3647 // Expand the incoming operand to be shifted, so that we have its parts
3648 SDOperand InL, InH;
3649 ExpandOp(Op, InL, InH);
3650 switch(Opc) {
3651 case ISD::SHL:
3652 if (Cst > VTBits) {
3653 Lo = DAG.getConstant(0, NVT);
3654 Hi = DAG.getConstant(0, NVT);
3655 } else if (Cst > NVTBits) {
3656 Lo = DAG.getConstant(0, NVT);
3657 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003658 } else if (Cst == NVTBits) {
3659 Lo = DAG.getConstant(0, NVT);
3660 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003661 } else {
3662 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3663 Hi = DAG.getNode(ISD::OR, NVT,
3664 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3665 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3666 }
3667 return true;
3668 case ISD::SRL:
3669 if (Cst > VTBits) {
3670 Lo = DAG.getConstant(0, NVT);
3671 Hi = DAG.getConstant(0, NVT);
3672 } else if (Cst > NVTBits) {
3673 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3674 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003675 } else if (Cst == NVTBits) {
3676 Lo = InH;
3677 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003678 } else {
3679 Lo = DAG.getNode(ISD::OR, NVT,
3680 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3681 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3682 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3683 }
3684 return true;
3685 case ISD::SRA:
3686 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003687 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003688 DAG.getConstant(NVTBits-1, ShTy));
3689 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003690 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003691 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003692 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003693 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003694 } else if (Cst == NVTBits) {
3695 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003696 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003697 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003698 } else {
3699 Lo = DAG.getNode(ISD::OR, NVT,
3700 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3701 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3702 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3703 }
3704 return true;
3705 }
3706 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003707 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003708}
Chris Lattner77e77a62005-01-21 06:05:23 +00003709
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003710
Chris Lattner77e77a62005-01-21 06:05:23 +00003711// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3712// does not fit into a register, return the lo part and set the hi part to the
3713// by-reg argument. If it does fit into a single register, return the result
3714// and leave the Hi part unset.
3715SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3716 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003717 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3718 // The input chain to this libcall is the entry node of the function.
3719 // Legalizing the call will automatically add the previous call to the
3720 // dependence.
3721 SDOperand InChain = DAG.getEntryNode();
3722
Chris Lattner77e77a62005-01-21 06:05:23 +00003723 TargetLowering::ArgListTy Args;
3724 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3725 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3726 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3727 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3728 }
3729 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003730
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003731 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003732 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003733 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003734 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3735 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003736
Chris Lattner6831a812006-02-13 09:18:02 +00003737 // Legalize the call sequence, starting with the chain. This will advance
3738 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3739 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3740 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003741 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003742 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003743 default: assert(0 && "Unknown thing");
3744 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003745 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003746 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003747 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003748 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003749 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003750 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003751 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003752}
3753
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003754
Chris Lattner77e77a62005-01-21 06:05:23 +00003755/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3756/// destination type is legal.
3757SDOperand SelectionDAGLegalize::
3758ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003759 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003760 assert(getTypeAction(Source.getValueType()) == Expand &&
3761 "This is not an expansion!");
3762 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3763
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003764 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003765 assert(Source.getValueType() == MVT::i64 &&
3766 "This only works for 64-bit -> FP");
3767 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3768 // incoming integer is set. To handle this, we dynamically test to see if
3769 // it is set, and, if so, add a fudge factor.
3770 SDOperand Lo, Hi;
3771 ExpandOp(Source, Lo, Hi);
3772
Chris Lattner66de05b2005-05-13 04:45:13 +00003773 // If this is unsigned, and not supported, first perform the conversion to
3774 // signed, then adjust the result if the sign bit is set.
3775 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3776 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3777
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003778 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3779 DAG.getConstant(0, Hi.getValueType()),
3780 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003781 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3782 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3783 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003784 uint64_t FF = 0x5f800000ULL;
3785 if (TLI.isLittleEndian()) FF <<= 32;
3786 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003787
Chris Lattner5839bf22005-08-26 17:15:30 +00003788 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003789 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3790 SDOperand FudgeInReg;
3791 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003792 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3793 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003794 else {
3795 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003796 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3797 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003798 }
Chris Lattner473a9902005-09-29 06:44:39 +00003799 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003800 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003801
Chris Lattnera88a2602005-05-14 05:33:54 +00003802 // Check to see if the target has a custom way to lower this. If so, use it.
3803 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3804 default: assert(0 && "This action not implemented for this operation!");
3805 case TargetLowering::Legal:
3806 case TargetLowering::Expand:
3807 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003808 case TargetLowering::Custom: {
3809 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3810 Source), DAG);
3811 if (NV.Val)
3812 return LegalizeOp(NV);
3813 break; // The target decided this was legal after all
3814 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003815 }
3816
Chris Lattner13689e22005-05-12 07:00:44 +00003817 // Expand the source, then glue it back together for the call. We must expand
3818 // the source in case it is shared (this pass of legalize must traverse it).
3819 SDOperand SrcLo, SrcHi;
3820 ExpandOp(Source, SrcLo, SrcHi);
3821 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3822
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003823 const char *FnName = 0;
3824 if (DestTy == MVT::f32)
3825 FnName = "__floatdisf";
3826 else {
3827 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3828 FnName = "__floatdidf";
3829 }
Chris Lattner6831a812006-02-13 09:18:02 +00003830
3831 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3832 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003833 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003834}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003835
Chris Lattner22cde6a2006-01-28 08:25:58 +00003836/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3837/// INT_TO_FP operation of the specified operand when the target requests that
3838/// we expand it. At this point, we know that the result and operand types are
3839/// legal for the target.
3840SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3841 SDOperand Op0,
3842 MVT::ValueType DestVT) {
3843 if (Op0.getValueType() == MVT::i32) {
3844 // simple 32-bit [signed|unsigned] integer to float/double expansion
3845
3846 // get the stack frame index of a 8 byte buffer
3847 MachineFunction &MF = DAG.getMachineFunction();
3848 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3849 // get address of 8 byte buffer
3850 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3851 // word offset constant for Hi/Lo address computation
3852 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3853 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003854 SDOperand Hi = StackSlot;
3855 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3856 if (TLI.isLittleEndian())
3857 std::swap(Hi, Lo);
3858
Chris Lattner22cde6a2006-01-28 08:25:58 +00003859 // if signed map to unsigned space
3860 SDOperand Op0Mapped;
3861 if (isSigned) {
3862 // constant used to invert sign bit (signed to unsigned mapping)
3863 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3864 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3865 } else {
3866 Op0Mapped = Op0;
3867 }
3868 // store the lo of the constructed double - based on integer input
3869 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3870 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3871 // initial hi portion of constructed double
3872 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3873 // store the hi of the constructed double - biased exponent
3874 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3875 InitialHi, Hi, DAG.getSrcValue(NULL));
3876 // load the constructed double
3877 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3878 DAG.getSrcValue(NULL));
3879 // FP constant to bias correct the final result
3880 SDOperand Bias = DAG.getConstantFP(isSigned ?
3881 BitsToDouble(0x4330000080000000ULL)
3882 : BitsToDouble(0x4330000000000000ULL),
3883 MVT::f64);
3884 // subtract the bias
3885 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3886 // final result
3887 SDOperand Result;
3888 // handle final rounding
3889 if (DestVT == MVT::f64) {
3890 // do nothing
3891 Result = Sub;
3892 } else {
3893 // if f32 then cast to f32
3894 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3895 }
3896 return Result;
3897 }
3898 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3899 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3900
3901 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3902 DAG.getConstant(0, Op0.getValueType()),
3903 ISD::SETLT);
3904 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3905 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3906 SignSet, Four, Zero);
3907
3908 // If the sign bit of the integer is set, the large number will be treated
3909 // as a negative number. To counteract this, the dynamic code adds an
3910 // offset depending on the data type.
3911 uint64_t FF;
3912 switch (Op0.getValueType()) {
3913 default: assert(0 && "Unsupported integer type!");
3914 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3915 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3916 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3917 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3918 }
3919 if (TLI.isLittleEndian()) FF <<= 32;
3920 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3921
3922 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3923 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3924 SDOperand FudgeInReg;
3925 if (DestVT == MVT::f32)
3926 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3927 DAG.getSrcValue(NULL));
3928 else {
3929 assert(DestVT == MVT::f64 && "Unexpected conversion");
3930 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3931 DAG.getEntryNode(), CPIdx,
3932 DAG.getSrcValue(NULL), MVT::f32));
3933 }
3934
3935 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3936}
3937
3938/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3939/// *INT_TO_FP operation of the specified operand when the target requests that
3940/// we promote it. At this point, we know that the result and operand types are
3941/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3942/// operation that takes a larger input.
3943SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3944 MVT::ValueType DestVT,
3945 bool isSigned) {
3946 // First step, figure out the appropriate *INT_TO_FP operation to use.
3947 MVT::ValueType NewInTy = LegalOp.getValueType();
3948
3949 unsigned OpToUse = 0;
3950
3951 // Scan for the appropriate larger type to use.
3952 while (1) {
3953 NewInTy = (MVT::ValueType)(NewInTy+1);
3954 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3955
3956 // If the target supports SINT_TO_FP of this type, use it.
3957 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3958 default: break;
3959 case TargetLowering::Legal:
3960 if (!TLI.isTypeLegal(NewInTy))
3961 break; // Can't use this datatype.
3962 // FALL THROUGH.
3963 case TargetLowering::Custom:
3964 OpToUse = ISD::SINT_TO_FP;
3965 break;
3966 }
3967 if (OpToUse) break;
3968 if (isSigned) continue;
3969
3970 // If the target supports UINT_TO_FP of this type, use it.
3971 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3972 default: break;
3973 case TargetLowering::Legal:
3974 if (!TLI.isTypeLegal(NewInTy))
3975 break; // Can't use this datatype.
3976 // FALL THROUGH.
3977 case TargetLowering::Custom:
3978 OpToUse = ISD::UINT_TO_FP;
3979 break;
3980 }
3981 if (OpToUse) break;
3982
3983 // Otherwise, try a larger type.
3984 }
3985
3986 // Okay, we found the operation and type to use. Zero extend our input to the
3987 // desired type then run the operation on it.
3988 return DAG.getNode(OpToUse, DestVT,
3989 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3990 NewInTy, LegalOp));
3991}
3992
3993/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3994/// FP_TO_*INT operation of the specified operand when the target requests that
3995/// we promote it. At this point, we know that the result and operand types are
3996/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3997/// operation that returns a larger result.
3998SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3999 MVT::ValueType DestVT,
4000 bool isSigned) {
4001 // First step, figure out the appropriate FP_TO*INT operation to use.
4002 MVT::ValueType NewOutTy = DestVT;
4003
4004 unsigned OpToUse = 0;
4005
4006 // Scan for the appropriate larger type to use.
4007 while (1) {
4008 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4009 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4010
4011 // If the target supports FP_TO_SINT returning this type, use it.
4012 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4013 default: break;
4014 case TargetLowering::Legal:
4015 if (!TLI.isTypeLegal(NewOutTy))
4016 break; // Can't use this datatype.
4017 // FALL THROUGH.
4018 case TargetLowering::Custom:
4019 OpToUse = ISD::FP_TO_SINT;
4020 break;
4021 }
4022 if (OpToUse) break;
4023
4024 // If the target supports FP_TO_UINT of this type, use it.
4025 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4026 default: break;
4027 case TargetLowering::Legal:
4028 if (!TLI.isTypeLegal(NewOutTy))
4029 break; // Can't use this datatype.
4030 // FALL THROUGH.
4031 case TargetLowering::Custom:
4032 OpToUse = ISD::FP_TO_UINT;
4033 break;
4034 }
4035 if (OpToUse) break;
4036
4037 // Otherwise, try a larger type.
4038 }
4039
4040 // Okay, we found the operation and type to use. Truncate the result of the
4041 // extended FP_TO_*INT operation to the desired size.
4042 return DAG.getNode(ISD::TRUNCATE, DestVT,
4043 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4044}
4045
4046/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4047///
4048SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4049 MVT::ValueType VT = Op.getValueType();
4050 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4051 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4052 switch (VT) {
4053 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4054 case MVT::i16:
4055 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4056 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4057 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4058 case MVT::i32:
4059 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4060 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4061 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4062 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4063 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4064 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4065 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4066 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4067 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4068 case MVT::i64:
4069 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4070 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4071 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4072 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4073 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4074 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4075 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4076 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4077 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4078 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4079 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4080 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4081 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4082 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4083 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4084 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4085 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4086 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4087 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4088 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4089 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4090 }
4091}
4092
4093/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4094///
4095SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4096 switch (Opc) {
4097 default: assert(0 && "Cannot expand this yet!");
4098 case ISD::CTPOP: {
4099 static const uint64_t mask[6] = {
4100 0x5555555555555555ULL, 0x3333333333333333ULL,
4101 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4102 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4103 };
4104 MVT::ValueType VT = Op.getValueType();
4105 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4106 unsigned len = getSizeInBits(VT);
4107 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4108 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4109 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4110 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4111 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4112 DAG.getNode(ISD::AND, VT,
4113 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4114 }
4115 return Op;
4116 }
4117 case ISD::CTLZ: {
4118 // for now, we do this:
4119 // x = x | (x >> 1);
4120 // x = x | (x >> 2);
4121 // ...
4122 // x = x | (x >>16);
4123 // x = x | (x >>32); // for 64-bit input
4124 // return popcount(~x);
4125 //
4126 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4127 MVT::ValueType VT = Op.getValueType();
4128 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4129 unsigned len = getSizeInBits(VT);
4130 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4131 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4132 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4133 }
4134 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4135 return DAG.getNode(ISD::CTPOP, VT, Op);
4136 }
4137 case ISD::CTTZ: {
4138 // for now, we use: { return popcount(~x & (x - 1)); }
4139 // unless the target has ctlz but not ctpop, in which case we use:
4140 // { return 32 - nlz(~x & (x-1)); }
4141 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4142 MVT::ValueType VT = Op.getValueType();
4143 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4144 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4145 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4146 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4147 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4148 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4149 TLI.isOperationLegal(ISD::CTLZ, VT))
4150 return DAG.getNode(ISD::SUB, VT,
4151 DAG.getConstant(getSizeInBits(VT), VT),
4152 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4153 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4154 }
4155 }
4156}
Chris Lattnere34b3962005-01-19 04:19:40 +00004157
Chris Lattner3e928bb2005-01-07 07:47:09 +00004158/// ExpandOp - Expand the specified SDOperand into its two component pieces
4159/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4160/// LegalizeNodes map is filled in for any results that are not expanded, the
4161/// ExpandedNodes map is filled in for any results that are expanded, and the
4162/// Lo/Hi values are returned.
4163void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4164 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004165 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004166 SDNode *Node = Op.Val;
4167 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00004168 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4169 "Cannot expand FP values!");
4170 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004171 "Cannot expand to FP value or to larger int value!");
4172
Chris Lattner6fdcb252005-09-02 20:32:45 +00004173 // See if we already expanded it.
4174 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4175 = ExpandedNodes.find(Op);
4176 if (I != ExpandedNodes.end()) {
4177 Lo = I->second.first;
4178 Hi = I->second.second;
4179 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004180 }
4181
Chris Lattner3e928bb2005-01-07 07:47:09 +00004182 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004183 case ISD::CopyFromReg:
4184 assert(0 && "CopyFromReg must be legal!");
4185 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004186 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4187 assert(0 && "Do not know how to expand this operator!");
4188 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004189 case ISD::UNDEF:
4190 Lo = DAG.getNode(ISD::UNDEF, NVT);
4191 Hi = DAG.getNode(ISD::UNDEF, NVT);
4192 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004193 case ISD::Constant: {
4194 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4195 Lo = DAG.getConstant(Cst, NVT);
4196 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4197 break;
4198 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004199 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004200 // Return the operands.
4201 Lo = Node->getOperand(0);
4202 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004203 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004204
4205 case ISD::SIGN_EXTEND_INREG:
4206 ExpandOp(Node->getOperand(0), Lo, Hi);
4207 // Sign extend the lo-part.
4208 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4209 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4210 TLI.getShiftAmountTy()));
4211 // sext_inreg the low part if needed.
4212 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4213 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004214
Nate Begemand88fc032006-01-14 03:14:10 +00004215 case ISD::BSWAP: {
4216 ExpandOp(Node->getOperand(0), Lo, Hi);
4217 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4218 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4219 Lo = TempLo;
4220 break;
4221 }
4222
Chris Lattneredb1add2005-05-11 04:51:16 +00004223 case ISD::CTPOP:
4224 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004225 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4226 DAG.getNode(ISD::CTPOP, NVT, Lo),
4227 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004228 Hi = DAG.getConstant(0, NVT);
4229 break;
4230
Chris Lattner39a8f332005-05-12 19:05:01 +00004231 case ISD::CTLZ: {
4232 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004233 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004234 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4235 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004236 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4237 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004238 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4239 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4240
4241 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4242 Hi = DAG.getConstant(0, NVT);
4243 break;
4244 }
4245
4246 case ISD::CTTZ: {
4247 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004248 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004249 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4250 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004251 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4252 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004253 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4254 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4255
4256 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4257 Hi = DAG.getConstant(0, NVT);
4258 break;
4259 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004260
Nate Begemanacc398c2006-01-25 18:21:52 +00004261 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004262 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4263 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004264 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4265 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4266
4267 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004268 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004269 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4270 if (!TLI.isLittleEndian())
4271 std::swap(Lo, Hi);
4272 break;
4273 }
4274
Chris Lattner3e928bb2005-01-07 07:47:09 +00004275 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004276 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4277 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004278 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004279
4280 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00004281 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004282 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4283 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00004284 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004285 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004286
4287 // Build a factor node to remember that this load is independent of the
4288 // other one.
4289 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4290 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004291
Chris Lattner3e928bb2005-01-07 07:47:09 +00004292 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004293 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004294 if (!TLI.isLittleEndian())
4295 std::swap(Lo, Hi);
4296 break;
4297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004298 case ISD::AND:
4299 case ISD::OR:
4300 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4301 SDOperand LL, LH, RL, RH;
4302 ExpandOp(Node->getOperand(0), LL, LH);
4303 ExpandOp(Node->getOperand(1), RL, RH);
4304 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4305 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4306 break;
4307 }
4308 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004309 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004310 ExpandOp(Node->getOperand(1), LL, LH);
4311 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00004312 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4313 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004314 break;
4315 }
Nate Begeman9373a812005-08-10 20:51:12 +00004316 case ISD::SELECT_CC: {
4317 SDOperand TL, TH, FL, FH;
4318 ExpandOp(Node->getOperand(2), TL, TH);
4319 ExpandOp(Node->getOperand(3), FL, FH);
4320 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4321 Node->getOperand(1), TL, FL, Node->getOperand(4));
4322 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4323 Node->getOperand(1), TH, FH, Node->getOperand(4));
4324 break;
4325 }
Nate Begeman144ff662005-10-13 17:15:37 +00004326 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004327 SDOperand Chain = Node->getOperand(0);
4328 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004329 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4330
4331 if (EVT == NVT)
4332 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4333 else
4334 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4335 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004336
4337 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004338 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004339
Nate Begeman144ff662005-10-13 17:15:37 +00004340 // The high part is obtained by SRA'ing all but one of the bits of the lo
4341 // part.
4342 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4343 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4344 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004345 break;
4346 }
4347 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004348 SDOperand Chain = Node->getOperand(0);
4349 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004350 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4351
4352 if (EVT == NVT)
4353 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4354 else
4355 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4356 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004357
4358 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004359 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004360
Nate Begeman144ff662005-10-13 17:15:37 +00004361 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004362 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004363 break;
4364 }
4365 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004366 SDOperand Chain = Node->getOperand(0);
4367 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004368 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4369
4370 if (EVT == NVT)
4371 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4372 else
4373 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4374 EVT);
4375
4376 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004377 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004378
4379 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004380 Hi = DAG.getNode(ISD::UNDEF, NVT);
4381 break;
4382 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004383 case ISD::ANY_EXTEND:
4384 // The low part is any extension of the input (which degenerates to a copy).
4385 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4386 // The high part is undefined.
4387 Hi = DAG.getNode(ISD::UNDEF, NVT);
4388 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004389 case ISD::SIGN_EXTEND: {
4390 // The low part is just a sign extension of the input (which degenerates to
4391 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004392 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004393
Chris Lattner3e928bb2005-01-07 07:47:09 +00004394 // The high part is obtained by SRA'ing all but one of the bits of the lo
4395 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004396 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004397 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4398 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004399 break;
4400 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004401 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004402 // The low part is just a zero extension of the input (which degenerates to
4403 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004404 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004405
Chris Lattner3e928bb2005-01-07 07:47:09 +00004406 // The high part is just a zero.
4407 Hi = DAG.getConstant(0, NVT);
4408 break;
Chris Lattner35481892005-12-23 00:16:34 +00004409
4410 case ISD::BIT_CONVERT: {
4411 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4412 Node->getOperand(0));
4413 ExpandOp(Tmp, Lo, Hi);
4414 break;
4415 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004416
Chris Lattner8137c9e2006-01-28 05:07:51 +00004417 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004418 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4419 TargetLowering::Custom &&
4420 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004421 Lo = TLI.LowerOperation(Op, DAG);
4422 assert(Lo.Val && "Node must be custom expanded!");
4423 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004424 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004425 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004426 break;
4427
Chris Lattner4e6c7462005-01-08 19:27:05 +00004428 // These operators cannot be expanded directly, emit them as calls to
4429 // library functions.
4430 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004431 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004432 SDOperand Op;
4433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4434 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004435 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4436 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004437 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004438
Chris Lattnerf20d1832005-07-30 01:40:57 +00004439 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4440
Chris Lattner80a3e942005-07-29 00:33:32 +00004441 // Now that the custom expander is done, expand the result, which is still
4442 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004443 if (Op.Val) {
4444 ExpandOp(Op, Lo, Hi);
4445 break;
4446 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004447 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004448
Chris Lattner4e6c7462005-01-08 19:27:05 +00004449 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004450 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004451 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004452 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004453 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004454
Chris Lattner4e6c7462005-01-08 19:27:05 +00004455 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004456 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004457 SDOperand Op;
4458 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4459 case Expand: assert(0 && "cannot expand FP!");
4460 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4461 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4462 }
4463
4464 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4465
4466 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004467 if (Op.Val) {
4468 ExpandOp(Op, Lo, Hi);
4469 break;
4470 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004471 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004472
Chris Lattner4e6c7462005-01-08 19:27:05 +00004473 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004474 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004475 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004476 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004477 break;
4478
Evan Cheng05a2d562006-01-09 18:31:59 +00004479 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004480 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004481 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004482 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004483 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004484 Op = TLI.LowerOperation(Op, DAG);
4485 if (Op.Val) {
4486 // Now that the custom expander is done, expand the result, which is
4487 // still VT.
4488 ExpandOp(Op, Lo, Hi);
4489 break;
4490 }
4491 }
4492
Chris Lattnere34b3962005-01-19 04:19:40 +00004493 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004494 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004495 break;
Chris Lattner47599822005-04-02 03:38:53 +00004496
4497 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004498 TargetLowering::LegalizeAction Action =
4499 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4500 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4501 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004502 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004503 break;
4504 }
4505
Chris Lattnere34b3962005-01-19 04:19:40 +00004506 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004507 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004508 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004509 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004510
Evan Cheng05a2d562006-01-09 18:31:59 +00004511 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004512 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004513 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004514 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004515 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004516 Op = TLI.LowerOperation(Op, DAG);
4517 if (Op.Val) {
4518 // Now that the custom expander is done, expand the result, which is
4519 // still VT.
4520 ExpandOp(Op, Lo, Hi);
4521 break;
4522 }
4523 }
4524
Chris Lattnere34b3962005-01-19 04:19:40 +00004525 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004526 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004527 break;
Chris Lattner47599822005-04-02 03:38:53 +00004528
4529 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004530 TargetLowering::LegalizeAction Action =
4531 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4532 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4533 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004534 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004535 break;
4536 }
4537
Chris Lattnere34b3962005-01-19 04:19:40 +00004538 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004539 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004540 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004541 }
4542
4543 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004544 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004545 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004546 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004547 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004548 Op = TLI.LowerOperation(Op, DAG);
4549 if (Op.Val) {
4550 // Now that the custom expander is done, expand the result, which is
4551 // still VT.
4552 ExpandOp(Op, Lo, Hi);
4553 break;
4554 }
4555 }
4556
Chris Lattnere34b3962005-01-19 04:19:40 +00004557 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004558 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004559 break;
Chris Lattner47599822005-04-02 03:38:53 +00004560
4561 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004562 TargetLowering::LegalizeAction Action =
4563 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4564 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4565 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004566 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004567 break;
4568 }
4569
Chris Lattnere34b3962005-01-19 04:19:40 +00004570 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004571 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004572 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004573 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004574
Misha Brukmanedf128a2005-04-21 22:36:52 +00004575 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004576 case ISD::SUB: {
4577 // If the target wants to custom expand this, let them.
4578 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4579 TargetLowering::Custom) {
4580 Op = TLI.LowerOperation(Op, DAG);
4581 if (Op.Val) {
4582 ExpandOp(Op, Lo, Hi);
4583 break;
4584 }
4585 }
4586
4587 // Expand the subcomponents.
4588 SDOperand LHSL, LHSH, RHSL, RHSH;
4589 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4590 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004591 std::vector<MVT::ValueType> VTs;
4592 std::vector<SDOperand> LoOps, HiOps;
4593 VTs.push_back(LHSL.getValueType());
4594 VTs.push_back(MVT::Flag);
4595 LoOps.push_back(LHSL);
4596 LoOps.push_back(RHSL);
4597 HiOps.push_back(LHSH);
4598 HiOps.push_back(RHSH);
4599 if (Node->getOpcode() == ISD::ADD) {
4600 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4601 HiOps.push_back(Lo.getValue(1));
4602 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4603 } else {
4604 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4605 HiOps.push_back(Lo.getValue(1));
4606 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4607 }
Chris Lattner84f67882005-01-20 18:52:28 +00004608 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004609 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004610 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004611 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004612 SDOperand LL, LH, RL, RH;
4613 ExpandOp(Node->getOperand(0), LL, LH);
4614 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004615 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4616 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4617 // extended the sign bit of the low half through the upper half, and if so
4618 // emit a MULHS instead of the alternate sequence that is valid for any
4619 // i64 x i64 multiply.
4620 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4621 // is RH an extension of the sign bit of RL?
4622 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4623 RH.getOperand(1).getOpcode() == ISD::Constant &&
4624 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4625 // is LH an extension of the sign bit of LL?
4626 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4627 LH.getOperand(1).getOpcode() == ISD::Constant &&
4628 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4629 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4630 } else {
4631 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4632 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4633 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4634 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4635 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4636 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004637 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4638 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004639 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004640 }
4641 break;
4642 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004643 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4644 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4645 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4646 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004647 }
4648
Chris Lattner83397362005-12-21 18:02:52 +00004649 // Make sure the resultant values have been legalized themselves, unless this
4650 // is a type that requires multi-step expansion.
4651 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4652 Lo = LegalizeOp(Lo);
4653 Hi = LegalizeOp(Hi);
4654 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004655
4656 // Remember in a map if the values will be reused later.
4657 bool isNew =
4658 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4659 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004660}
4661
Chris Lattnerc7029802006-03-18 01:44:44 +00004662/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4663/// two smaller values of MVT::Vector type.
4664void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4665 SDOperand &Hi) {
4666 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4667 SDNode *Node = Op.Val;
4668 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4669 assert(NumElements > 1 && "Cannot split a single element vector!");
4670 unsigned NewNumElts = NumElements/2;
4671 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4672 SDOperand TypeNode = *(Node->op_end()-1);
4673
4674 // See if we already split it.
4675 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4676 = SplitNodes.find(Op);
4677 if (I != SplitNodes.end()) {
4678 Lo = I->second.first;
4679 Hi = I->second.second;
4680 return;
4681 }
4682
4683 switch (Node->getOpcode()) {
Chris Lattner3824e502006-04-14 06:08:35 +00004684 default: Node->dump(); assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004685 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004686 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4687 LoOps.push_back(NewNumEltsNode);
4688 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004689 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004690
4691 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4692 HiOps.push_back(NewNumEltsNode);
4693 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004694 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004695 break;
4696 }
4697 case ISD::VADD:
4698 case ISD::VSUB:
4699 case ISD::VMUL:
4700 case ISD::VSDIV:
4701 case ISD::VUDIV:
4702 case ISD::VAND:
4703 case ISD::VOR:
4704 case ISD::VXOR: {
4705 SDOperand LL, LH, RL, RH;
4706 SplitVectorOp(Node->getOperand(0), LL, LH);
4707 SplitVectorOp(Node->getOperand(1), RL, RH);
4708
4709 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4710 NewNumEltsNode, TypeNode);
4711 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4712 NewNumEltsNode, TypeNode);
4713 break;
4714 }
4715 case ISD::VLOAD: {
4716 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4717 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4718 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4719
4720 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4721 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4722 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4723 getIntPtrConstant(IncrementSize));
4724 // FIXME: This creates a bogus srcvalue!
4725 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4726
4727 // Build a factor node to remember that this load is independent of the
4728 // other one.
4729 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4730 Hi.getValue(1));
4731
4732 // Remember that we legalized the chain.
4733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00004734 break;
4735 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004736 case ISD::VBIT_CONVERT: {
4737 // We know the result is a vector. The input may be either a vector or a
4738 // scalar value.
4739 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4740 // Lower to a store/load. FIXME: this could be improved probably.
4741 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4742
4743 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4744 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4745 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4746 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4747 SplitVectorOp(St, Lo, Hi);
4748 } else {
4749 // If the input is a vector type, we have to either scalarize it, pack it
4750 // or convert it based on whether the input vector type is legal.
4751 SDNode *InVal = Node->getOperand(0).Val;
4752 unsigned NumElems =
4753 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4754 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4755
4756 // If the input is from a single element vector, scalarize the vector,
4757 // then treat like a scalar.
4758 if (NumElems == 1) {
4759 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4760 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4761 Op.getOperand(1), Op.getOperand(2));
4762 SplitVectorOp(Scalar, Lo, Hi);
4763 } else {
4764 // Split the input vector.
4765 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4766
4767 // Convert each of the pieces now.
4768 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4769 NewNumEltsNode, TypeNode);
4770 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4771 NewNumEltsNode, TypeNode);
4772 }
4773 break;
4774 }
4775 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004776 }
4777
4778 // Remember in a map if the values will be reused later.
4779 bool isNew =
4780 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4781 assert(isNew && "Value already expanded?!?");
4782}
4783
4784
4785/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4786/// equivalent operation that returns a scalar (e.g. F32) or packed value
4787/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4788/// type for the result.
4789SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4790 MVT::ValueType NewVT) {
4791 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4792 SDNode *Node = Op.Val;
4793
4794 // See if we already packed it.
4795 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4796 if (I != PackedNodes.end()) return I->second;
4797
4798 SDOperand Result;
4799 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004800 default:
4801 Node->dump(); std::cerr << "\n";
4802 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004803 case ISD::VADD:
4804 case ISD::VSUB:
4805 case ISD::VMUL:
4806 case ISD::VSDIV:
4807 case ISD::VUDIV:
4808 case ISD::VAND:
4809 case ISD::VOR:
4810 case ISD::VXOR:
4811 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4812 NewVT,
4813 PackVectorOp(Node->getOperand(0), NewVT),
4814 PackVectorOp(Node->getOperand(1), NewVT));
4815 break;
4816 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004817 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4818 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004819
Chris Lattner4794a6b2006-03-19 00:07:49 +00004820 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004821
4822 // Remember that we legalized the chain.
4823 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4824 break;
4825 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004826 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00004827 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004828 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004829 Result = Node->getOperand(0);
4830 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004831 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00004832
4833 // If all elements of the build_vector are undefs, return an undef.
4834 bool AllUndef = true;
4835 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4836 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4837 AllUndef = false;
4838 break;
4839 }
4840 if (AllUndef) {
4841 Result = DAG.getNode(ISD::UNDEF, NewVT);
4842 } else {
4843 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4844 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4845 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004846 }
4847 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004848 case ISD::VINSERT_VECTOR_ELT:
4849 if (!MVT::isVector(NewVT)) {
4850 // Returning a scalar? Must be the inserted element.
4851 Result = Node->getOperand(1);
4852 } else {
4853 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4854 PackVectorOp(Node->getOperand(0), NewVT),
4855 Node->getOperand(1), Node->getOperand(2));
4856 }
4857 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00004858 case ISD::VVECTOR_SHUFFLE:
4859 if (!MVT::isVector(NewVT)) {
4860 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4861 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4862 if (cast<ConstantSDNode>(EltNum)->getValue())
4863 Result = PackVectorOp(Node->getOperand(1), NewVT);
4864 else
4865 Result = PackVectorOp(Node->getOperand(0), NewVT);
4866 } else {
4867 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4868 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4869 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4870 Node->getOperand(2).Val->op_end()-2);
4871 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4872 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4873
4874 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4875 PackVectorOp(Node->getOperand(0), NewVT),
4876 PackVectorOp(Node->getOperand(1), NewVT), BV);
4877 }
4878 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004879 case ISD::VBIT_CONVERT:
4880 if (Op.getOperand(0).getValueType() != MVT::Vector)
4881 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4882 else {
4883 // If the input is a vector type, we have to either scalarize it, pack it
4884 // or convert it based on whether the input vector type is legal.
4885 SDNode *InVal = Node->getOperand(0).Val;
4886 unsigned NumElems =
4887 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4888 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4889
4890 // Figure out if there is a Packed type corresponding to this Vector
4891 // type. If so, convert to the packed type.
4892 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4893 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4894 // Turn this into a bit convert of the packed input.
4895 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4896 PackVectorOp(Node->getOperand(0), TVT));
4897 break;
4898 } else if (NumElems == 1) {
4899 // Turn this into a bit convert of the scalar input.
4900 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4901 PackVectorOp(Node->getOperand(0), EVT));
4902 break;
4903 } else {
4904 // FIXME: UNIMP!
4905 assert(0 && "Cast from unsupported vector type not implemented yet!");
4906 }
4907 }
Evan Chengdb3c6262006-04-10 18:54:36 +00004908 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004909 case ISD::VSELECT:
4910 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4911 PackVectorOp(Op.getOperand(1), NewVT),
4912 PackVectorOp(Op.getOperand(2), NewVT));
4913 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004914 }
4915
4916 if (TLI.isTypeLegal(NewVT))
4917 Result = LegalizeOp(Result);
4918 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4919 assert(isNew && "Value already packed?");
4920 return Result;
4921}
4922
Chris Lattner3e928bb2005-01-07 07:47:09 +00004923
4924// SelectionDAG::Legalize - This is the entry point for the file.
4925//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004926void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00004927 if (ViewLegalizeDAGs) viewGraph();
4928
Chris Lattner3e928bb2005-01-07 07:47:09 +00004929 /// run - This is the main entry point to this class.
4930 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004931 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004932}
4933