blob: 36bf61af13187e1e9000915fa5b36190fed711fa [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000024#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng033e6812006-03-24 01:17:21 +000028#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000029using namespace llvm;
30
Chris Lattner5e46a192006-04-02 03:07:27 +000031#ifndef NDEBUG
32static cl::opt<bool>
33ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
34 cl::desc("Pop up a window to show dags before legalize"));
35#else
36static const bool ViewLegalizeDAGs = 0;
37#endif
38
Chris Lattner3e928bb2005-01-07 07:47:09 +000039//===----------------------------------------------------------------------===//
40/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
41/// hacks on it until the target machine can handle it. This involves
42/// eliminating value sizes the machine cannot handle (promoting small sizes to
43/// large sizes or splitting up large values into small values) as well as
44/// eliminating operations the machine cannot handle.
45///
46/// This code also does a small amount of optimization and recognition of idioms
47/// as part of its processing. For example, if a target does not support a
48/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
49/// will attempt merge setcc and brc instructions into brcc's.
50///
51namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000052class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000053 TargetLowering &TLI;
54 SelectionDAG &DAG;
55
Chris Lattner6831a812006-02-13 09:18:02 +000056 // Libcall insertion helpers.
57
58 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
59 /// legalized. We use this to ensure that calls are properly serialized
60 /// against each other, including inserted libcalls.
61 SDOperand LastCALLSEQ_END;
62
63 /// IsLegalizingCall - This member is used *only* for purposes of providing
64 /// helpful assertions that a libcall isn't created while another call is
65 /// being legalized (which could lead to non-serialized call sequences).
66 bool IsLegalizingCall;
67
Chris Lattner3e928bb2005-01-07 07:47:09 +000068 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000069 Legal, // The target natively supports this operation.
70 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000071 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 };
Chris Lattner6831a812006-02-13 09:18:02 +000073
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 /// ValueTypeActions - This is a bitvector that contains two bits for each
75 /// value type, where the two bits correspond to the LegalizeAction enum.
76 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000077 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
82 std::map<SDOperand, SDOperand> LegalizedNodes;
83
Chris Lattner03c85462005-01-15 05:21:40 +000084 /// PromotedNodes - For nodes that are below legal width, and that have more
85 /// than one use, this map indicates what promoted value to use. This allows
86 /// us to avoid promoting the same thing more than once.
87 std::map<SDOperand, SDOperand> PromotedNodes;
88
Chris Lattnerc7029802006-03-18 01:44:44 +000089 /// ExpandedNodes - For nodes that need to be expanded this map indicates
90 /// which which operands are the expanded version of the input. This allows
91 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000092 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
93
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// SplitNodes - For vector nodes that need to be split, this map indicates
95 /// which which operands are the split version of the input. This allows us
96 /// to avoid splitting the same node more than once.
97 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
98
99 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
100 /// concrete packed types, this contains the mapping of ones we have already
101 /// processed to the result.
102 std::map<SDOperand, SDOperand> PackedNodes;
103
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000105 LegalizedNodes.insert(std::make_pair(From, To));
106 // If someone requests legalization of the new node, return itself.
107 if (From != To)
108 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000109 }
Chris Lattner03c85462005-01-15 05:21:40 +0000110 void AddPromotedOperand(SDOperand From, SDOperand To) {
111 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
112 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000113 // If someone requests legalization of the new node, return itself.
114 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000115 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000116
Chris Lattner3e928bb2005-01-07 07:47:09 +0000117public:
118
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000119 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121 /// getTypeAction - Return how we should legalize values of this type, either
122 /// it is already legal or we need to expand it into multiple registers of
123 /// smaller integer type, or we need to promote it to a larger type.
124 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000125 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126 }
127
128 /// isTypeLegal - Return true if this type is legal on this target.
129 ///
130 bool isTypeLegal(MVT::ValueType VT) const {
131 return getTypeAction(VT) == Legal;
132 }
133
Chris Lattner3e928bb2005-01-07 07:47:09 +0000134 void LegalizeDAG();
135
Chris Lattner456a93a2006-01-28 07:39:30 +0000136private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000137 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
138 /// appropriate for its type.
139 void HandleOp(SDOperand Op);
140
141 /// LegalizeOp - We know that the specified value has a legal type.
142 /// Recursively ensure that the operands have legal types, then return the
143 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000144 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000145
146 /// PromoteOp - Given an operation that produces a value in an invalid type,
147 /// promote it to compute the value into a larger type. The produced value
148 /// will have the correct bits for the low portion of the register, but no
149 /// guarantee is made about the top bits: it may be zero, sign-extended, or
150 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000151 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152
Chris Lattnerc7029802006-03-18 01:44:44 +0000153 /// ExpandOp - Expand the specified SDOperand into its two component pieces
154 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
155 /// the LegalizeNodes map is filled in for any results that are not expanded,
156 /// the ExpandedNodes map is filled in for any results that are expanded, and
157 /// the Lo/Hi values are returned. This applies to integer types and Vector
158 /// types.
159 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
160
161 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
162 /// two smaller values of MVT::Vector type.
163 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
165 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
166 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
167 /// this is called, we know that PackedVT is the right type for the result and
168 /// we know that this type is legal for the target.
169 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
170
Chris Lattner4352cc92006-04-04 17:23:26 +0000171 /// isShuffleLegal - Return true if a vector shuffle is legal with the
172 /// specified mask and type. Targets can specify exactly which masks they
173 /// support and the code generator is tasked with not creating illegal masks.
174 ///
175 /// Note that this will also return true for shuffles that are promoted to a
176 /// different type.
177 ///
178 /// If this is a legal shuffle, this method returns the (possibly promoted)
179 /// build_vector Mask. If it's not a legal shuffle, it returns null.
180 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
181
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000182 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
183 std::set<SDNode*> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000184
Nate Begeman750ac1b2006-02-01 07:19:44 +0000185 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
186
Chris Lattnerce872152006-03-19 06:31:19 +0000187 SDOperand CreateStackTemporary(MVT::ValueType VT);
188
Reid Spencer47857812006-12-31 05:55:36 +0000189 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000190 SDOperand &Hi);
191 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
192 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000193
Chris Lattner35481892005-12-23 00:16:34 +0000194 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000195 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000196 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000197 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
198 SDOperand LegalOp,
199 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000200 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
201 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000202 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000204
Chris Lattner456a93a2006-01-28 07:39:30 +0000205 SDOperand ExpandBSWAP(SDOperand Op);
206 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000207 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
208 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000209 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000211
Chris Lattner15972212006-03-31 17:55:51 +0000212 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000213 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000214
Chris Lattner3e928bb2005-01-07 07:47:09 +0000215 SDOperand getIntPtrConstant(uint64_t Val) {
216 return DAG.getConstant(Val, TLI.getPointerTy());
217 }
218};
219}
220
Chris Lattner4352cc92006-04-04 17:23:26 +0000221/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
222/// specified mask and type. Targets can specify exactly which masks they
223/// support and the code generator is tasked with not creating illegal masks.
224///
225/// Note that this will also return true for shuffles that are promoted to a
226/// different type.
227SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
228 SDOperand Mask) const {
229 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
230 default: return 0;
231 case TargetLowering::Legal:
232 case TargetLowering::Custom:
233 break;
234 case TargetLowering::Promote: {
235 // If this is promoted to a different type, convert the shuffle mask and
236 // ask if it is legal in the promoted type!
237 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
238
239 // If we changed # elements, change the shuffle mask.
240 unsigned NumEltsGrowth =
241 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
242 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
243 if (NumEltsGrowth > 1) {
244 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000245 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000246 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
247 SDOperand InOp = Mask.getOperand(i);
248 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
249 if (InOp.getOpcode() == ISD::UNDEF)
250 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
251 else {
252 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
253 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
254 }
255 }
256 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000257 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000258 }
259 VT = NVT;
260 break;
261 }
262 }
263 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
264}
265
Chris Lattnerc7029802006-03-18 01:44:44 +0000266/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
267/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000268static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000269 switch (VecOp) {
270 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000271 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
272 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
273 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
274 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
275 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
276 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
277 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
278 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000279 }
280}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000281
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000282SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
283 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
284 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000285 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000286 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000287}
288
Chris Lattner32fca002005-10-06 01:20:27 +0000289/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
290/// not been visited yet and if all of its operands have already been visited.
291static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
292 std::map<SDNode*, unsigned> &Visited) {
293 if (++Visited[N] != N->getNumOperands())
294 return; // Haven't visited all operands yet
295
296 Order.push_back(N);
297
298 if (N->hasOneUse()) { // Tail recurse in common case.
299 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
300 return;
301 }
302
303 // Now that we have N in, add anything that uses it if all of their operands
304 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
306 ComputeTopDownOrdering(*UI, Order, Visited);
307}
308
Chris Lattner1618beb2005-07-29 00:11:56 +0000309
Chris Lattner3e928bb2005-01-07 07:47:09 +0000310void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000311 LastCALLSEQ_END = DAG.getEntryNode();
312 IsLegalizingCall = false;
313
Chris Lattnerab510a72005-10-02 17:49:46 +0000314 // The legalize process is inherently a bottom-up recursive process (users
315 // legalize their uses before themselves). Given infinite stack space, we
316 // could just start legalizing on the root and traverse the whole graph. In
317 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000318 // blocks. To avoid this problem, compute an ordering of the nodes where each
319 // node is only legalized after all of its operands are legalized.
320 std::map<SDNode*, unsigned> Visited;
321 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000322
Chris Lattner32fca002005-10-06 01:20:27 +0000323 // Compute ordering from all of the leaves in the graphs, those (like the
324 // entry node) that have no operands.
325 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
326 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000327 if (I->getNumOperands() == 0) {
328 Visited[I] = 0 - 1U;
329 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000330 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000331 }
332
Chris Lattnerde202b32005-11-09 23:47:37 +0000333 assert(Order.size() == Visited.size() &&
334 Order.size() ==
335 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000336 "Error: DAG is cyclic!");
337 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000338
Chris Lattnerc7029802006-03-18 01:44:44 +0000339 for (unsigned i = 0, e = Order.size(); i != e; ++i)
340 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000341
342 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000344 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
345 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346
347 ExpandedNodes.clear();
348 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000349 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000350 SplitNodes.clear();
351 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352
353 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000354 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355}
356
Chris Lattner6831a812006-02-13 09:18:02 +0000357
358/// FindCallEndFromCallStart - Given a chained node that is part of a call
359/// sequence, find the CALLSEQ_END node that terminates the call sequence.
360static SDNode *FindCallEndFromCallStart(SDNode *Node) {
361 if (Node->getOpcode() == ISD::CALLSEQ_END)
362 return Node;
363 if (Node->use_empty())
364 return 0; // No CallSeqEnd
365
366 // The chain is usually at the end.
367 SDOperand TheChain(Node, Node->getNumValues()-1);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Sometimes it's at the beginning.
370 TheChain = SDOperand(Node, 0);
371 if (TheChain.getValueType() != MVT::Other) {
372 // Otherwise, hunt for it.
373 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
374 if (Node->getValueType(i) == MVT::Other) {
375 TheChain = SDOperand(Node, i);
376 break;
377 }
378
379 // Otherwise, we walked into a node without a chain.
380 if (TheChain.getValueType() != MVT::Other)
381 return 0;
382 }
383 }
384
385 for (SDNode::use_iterator UI = Node->use_begin(),
386 E = Node->use_end(); UI != E; ++UI) {
387
388 // Make sure to only follow users of our token chain.
389 SDNode *User = *UI;
390 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
391 if (User->getOperand(i) == TheChain)
392 if (SDNode *Result = FindCallEndFromCallStart(User))
393 return Result;
394 }
395 return 0;
396}
397
398/// FindCallStartFromCallEnd - Given a chained node that is part of a call
399/// sequence, find the CALLSEQ_START node that initiates the call sequence.
400static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
401 assert(Node && "Didn't find callseq_start for a call??");
402 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
403
404 assert(Node->getOperand(0).getValueType() == MVT::Other &&
405 "Node doesn't have a token chain argument!");
406 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
407}
408
409/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
410/// see if any uses can reach Dest. If no dest operands can get to dest,
411/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000412///
413/// Keep track of the nodes we fine that actually do lead to Dest in
414/// NodesLeadingTo. This avoids retraversing them exponential number of times.
415///
416bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
417 std::set<SDNode*> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000418 if (N == Dest) return true; // N certainly leads to Dest :)
419
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000420 // If we've already processed this node and it does lead to Dest, there is no
421 // need to reprocess it.
422 if (NodesLeadingTo.count(N)) return true;
423
Chris Lattner6831a812006-02-13 09:18:02 +0000424 // If the first result of this node has been already legalized, then it cannot
425 // reach N.
426 switch (getTypeAction(N->getValueType(0))) {
427 case Legal:
428 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Promote:
431 if (PromotedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 case Expand:
434 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
435 break;
436 }
437
438 // Okay, this node has not already been legalized. Check and legalize all
439 // operands. If none lead to Dest, then we can legalize this node.
440 bool OperandsLeadToDest = false;
441 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
442 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000443 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000444
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000445 if (OperandsLeadToDest) {
446 NodesLeadingTo.insert(N);
447 return true;
448 }
Chris Lattner6831a812006-02-13 09:18:02 +0000449
450 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000451 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000452 return false;
453}
454
Chris Lattnerc7029802006-03-18 01:44:44 +0000455/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
456/// appropriate for its type.
457void SelectionDAGLegalize::HandleOp(SDOperand Op) {
458 switch (getTypeAction(Op.getValueType())) {
459 default: assert(0 && "Bad type action!");
460 case Legal: LegalizeOp(Op); break;
461 case Promote: PromoteOp(Op); break;
462 case Expand:
463 if (Op.getValueType() != MVT::Vector) {
464 SDOperand X, Y;
465 ExpandOp(Op, X, Y);
466 } else {
467 SDNode *N = Op.Val;
468 unsigned NumOps = N->getNumOperands();
469 unsigned NumElements =
470 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
471 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
472 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
473 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
474 // In the common case, this is a legal vector type, convert it to the
475 // packed operation and type now.
476 PackVectorOp(Op, PackedVT);
477 } else if (NumElements == 1) {
478 // Otherwise, if this is a single element vector, convert it to a
479 // scalar operation.
480 PackVectorOp(Op, EVT);
481 } else {
482 // Otherwise, this is a multiple element vector that isn't supported.
483 // Split it in half and legalize both parts.
484 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000485 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000486 }
487 }
488 break;
489 }
490}
Chris Lattner6831a812006-02-13 09:18:02 +0000491
Evan Cheng9f877882006-12-13 20:57:08 +0000492/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
493/// a load from the constant pool.
494static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000495 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000496 bool Extend = false;
497
498 // If a FP immediate is precise when represented as a float and if the
499 // target can do an extending load from float to double, we put it into
500 // the constant pool as a float, even if it's is statically typed as a
501 // double.
502 MVT::ValueType VT = CFP->getValueType(0);
503 bool isDouble = VT == MVT::f64;
504 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
505 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000506 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000507 double Val = LLVMC->getValue();
508 return isDouble
509 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
510 : DAG.getConstant(FloatToBits(Val), MVT::i32);
511 }
512
Evan Cheng00495212006-12-12 21:32:44 +0000513 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
514 // Only do this if the target has a native EXTLOAD instruction from f32.
515 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
516 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
517 VT = MVT::f32;
518 Extend = true;
519 }
520
521 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
522 if (Extend) {
523 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
524 CPIdx, NULL, 0, MVT::f32);
525 } else {
526 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
527 }
528}
529
Chris Lattner6831a812006-02-13 09:18:02 +0000530
Chris Lattnerc7029802006-03-18 01:44:44 +0000531/// LegalizeOp - We know that the specified value has a legal type.
532/// Recursively ensure that the operands have legal types, then return the
533/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000535 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000536 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000537 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000538
Chris Lattner3e928bb2005-01-07 07:47:09 +0000539 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000540 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000541 if (Node->getNumValues() > 1) {
542 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000543 if (getTypeAction(Node->getValueType(i)) != Legal) {
544 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000545 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000546 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000547 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000548 }
549 }
550
Chris Lattner45982da2005-05-12 16:53:42 +0000551 // Note that LegalizeOp may be reentered even from single-use nodes, which
552 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000553 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
554 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000555
Nate Begeman9373a812005-08-10 20:51:12 +0000556 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000557 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000558 bool isCustom = false;
559
Chris Lattner3e928bb2005-01-07 07:47:09 +0000560 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000561 case ISD::FrameIndex:
562 case ISD::EntryToken:
563 case ISD::Register:
564 case ISD::BasicBlock:
565 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000566 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000567 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000568 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000569 case ISD::TargetConstantPool:
570 case ISD::TargetGlobalAddress:
571 case ISD::TargetExternalSymbol:
572 case ISD::VALUETYPE:
573 case ISD::SRCVALUE:
574 case ISD::STRING:
575 case ISD::CONDCODE:
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +0000576 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner948c1b12006-01-28 08:31:04 +0000577 // Primitives must all be legal.
578 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
579 "This must be legal!");
580 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000581 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000582 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
583 // If this is a target node, legalize it by legalizing the operands then
584 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000585 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000586 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000587 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000588
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000589 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000590
591 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
592 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
593 return Result.getValue(Op.ResNo);
594 }
595 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000596#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +0000597 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000598#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000599 assert(0 && "Do not know how to legalize this operator!");
600 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000601 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000602 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000603 case ISD::ConstantPool:
604 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000605 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
606 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000607 case TargetLowering::Custom:
608 Tmp1 = TLI.LowerOperation(Op, DAG);
609 if (Tmp1.Val) Result = Tmp1;
610 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000611 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000612 break;
613 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000614 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000615 case ISD::AssertSext:
616 case ISD::AssertZext:
617 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000618 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000619 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000620 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000621 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000622 Result = Node->getOperand(Op.ResNo);
623 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000624 case ISD::CopyFromReg:
625 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000626 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000627 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000628 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000629 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000630 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000631 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000632 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000633 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
634 } else {
635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
636 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000637 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
638 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000639 // Since CopyFromReg produces two values, make sure to remember that we
640 // legalized both of them.
641 AddLegalizedOperand(Op.getValue(0), Result);
642 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
643 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000644 case ISD::UNDEF: {
645 MVT::ValueType VT = Op.getValueType();
646 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000647 default: assert(0 && "This action is not supported yet!");
648 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000649 if (MVT::isInteger(VT))
650 Result = DAG.getConstant(0, VT);
651 else if (MVT::isFloatingPoint(VT))
652 Result = DAG.getConstantFP(0, VT);
653 else
654 assert(0 && "Unknown value type!");
655 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000656 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000657 break;
658 }
659 break;
660 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000661
Chris Lattner48b61a72006-03-28 00:40:33 +0000662 case ISD::INTRINSIC_W_CHAIN:
663 case ISD::INTRINSIC_WO_CHAIN:
664 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000665 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000666 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
667 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000668 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000669
670 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000671 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000672 TargetLowering::Custom) {
673 Tmp3 = TLI.LowerOperation(Result, DAG);
674 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000675 }
676
677 if (Result.Val->getNumValues() == 1) break;
678
679 // Must have return value and chain result.
680 assert(Result.Val->getNumValues() == 2 &&
681 "Cannot return more than two values!");
682
683 // Since loads produce two values, make sure to remember that we
684 // legalized both of them.
685 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
686 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
687 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000688 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000689
690 case ISD::LOCATION:
691 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
692 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
693
694 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
695 case TargetLowering::Promote:
696 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000697 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000698 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000699 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
700 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
701
702 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
703 const std::string &FName =
704 cast<StringSDNode>(Node->getOperand(3))->getValue();
705 const std::string &DirName =
706 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000707 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000708
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000709 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000710 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000711 SDOperand LineOp = Node->getOperand(1);
712 SDOperand ColOp = Node->getOperand(2);
713
714 if (useDEBUG_LOC) {
715 Ops.push_back(LineOp); // line #
716 Ops.push_back(ColOp); // col #
717 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000718 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000719 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000720 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
721 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000722 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
723 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000724 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000725 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000726 } else {
727 Result = Tmp1; // chain
728 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000729 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000730 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000731 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000732 if (Tmp1 != Node->getOperand(0) ||
733 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000734 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000735 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000736 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
737 Ops.push_back(Node->getOperand(1)); // line # must be legal.
738 Ops.push_back(Node->getOperand(2)); // col # must be legal.
739 } else {
740 // Otherwise promote them.
741 Ops.push_back(PromoteOp(Node->getOperand(1)));
742 Ops.push_back(PromoteOp(Node->getOperand(2)));
743 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000744 Ops.push_back(Node->getOperand(3)); // filename must be legal.
745 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000746 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000747 }
748 break;
749 }
750 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000751
752 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000753 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000754 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000755 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000756 case TargetLowering::Legal:
757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
758 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
759 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
760 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000761 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000762 break;
763 }
764 break;
765
766 case ISD::DEBUG_LABEL:
767 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
768 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000769 default: assert(0 && "This action is not supported yet!");
770 case TargetLowering::Legal:
771 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
772 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000774 break;
775 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000776 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000777
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 case ISD::Constant:
779 // We know we don't need to expand constants here, constants only have one
780 // value and we check that it is fine above.
781
782 // FIXME: Maybe we should handle things like targets that don't support full
783 // 32-bit immediates?
784 break;
785 case ISD::ConstantFP: {
786 // Spill FP immediates to the constant pool if the target cannot directly
787 // codegen them. Targets often have some immediate values that can be
788 // efficiently generated into an FP register without a load. We explicitly
789 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000790 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
791
792 // Check to see if this FP immediate is already legal.
793 bool isLegal = false;
794 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
795 E = TLI.legal_fpimm_end(); I != E; ++I)
796 if (CFP->isExactlyValue(*I)) {
797 isLegal = true;
798 break;
799 }
800
Chris Lattner3181a772006-01-29 06:26:56 +0000801 // If this is a legal constant, turn it into a TargetConstantFP node.
802 if (isLegal) {
803 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
804 break;
805 }
806
807 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
808 default: assert(0 && "This action is not supported yet!");
809 case TargetLowering::Custom:
810 Tmp3 = TLI.LowerOperation(Result, DAG);
811 if (Tmp3.Val) {
812 Result = Tmp3;
813 break;
814 }
815 // FALLTHROUGH
816 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000817 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818 }
819 break;
820 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000821 case ISD::TokenFactor:
822 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000823 Tmp1 = LegalizeOp(Node->getOperand(0));
824 Tmp2 = LegalizeOp(Node->getOperand(1));
825 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
826 } else if (Node->getNumOperands() == 3) {
827 Tmp1 = LegalizeOp(Node->getOperand(0));
828 Tmp2 = LegalizeOp(Node->getOperand(1));
829 Tmp3 = LegalizeOp(Node->getOperand(2));
830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000831 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000832 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000833 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000834 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
835 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000836 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000837 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000838 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000839
840 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000841 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000842 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000843 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
844 assert(Tmp3.Val && "Target didn't custom lower this node!");
845 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
846 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000847
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000848 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000849 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000850 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
851 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000852 if (Op.ResNo == i)
853 Tmp2 = Tmp1;
854 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
855 }
856 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000857
Chris Lattnerb2827b02006-03-19 00:52:58 +0000858 case ISD::BUILD_VECTOR:
859 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000860 default: assert(0 && "This action is not supported yet!");
861 case TargetLowering::Custom:
862 Tmp3 = TLI.LowerOperation(Result, DAG);
863 if (Tmp3.Val) {
864 Result = Tmp3;
865 break;
866 }
867 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000868 case TargetLowering::Expand:
869 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000870 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000871 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000872 break;
873 case ISD::INSERT_VECTOR_ELT:
874 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
875 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
876 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
878
879 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
880 Node->getValueType(0))) {
881 default: assert(0 && "This action is not supported yet!");
882 case TargetLowering::Legal:
883 break;
884 case TargetLowering::Custom:
885 Tmp3 = TLI.LowerOperation(Result, DAG);
886 if (Tmp3.Val) {
887 Result = Tmp3;
888 break;
889 }
890 // FALLTHROUGH
891 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000892 // If the insert index is a constant, codegen this as a scalar_to_vector,
893 // then a shuffle that inserts it into the right position in the vector.
894 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
895 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
896 Tmp1.getValueType(), Tmp2);
897
898 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
899 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
900 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
901
902 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
903 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
904 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000905 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000906 for (unsigned i = 0; i != NumElts; ++i) {
907 if (i != InsertPos->getValue())
908 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
909 else
910 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
911 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000912 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
913 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +0000914
915 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
916 Tmp1, ScVec, ShufMask);
917 Result = LegalizeOp(Result);
918 break;
919 }
920
Chris Lattner2332b9f2006-03-19 01:17:20 +0000921 // If the target doesn't support this, we have to spill the input vector
922 // to a temporary stack slot, update the element, then reload it. This is
923 // badness. We could also load the value into a vector register (either
924 // with a "move to register" or "extload into register" instruction, then
925 // permute it into place, if the idx is a constant and if the idx is
926 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000927 MVT::ValueType VT = Tmp1.getValueType();
928 MVT::ValueType EltVT = Tmp2.getValueType();
929 MVT::ValueType IdxVT = Tmp3.getValueType();
930 MVT::ValueType PtrVT = TLI.getPointerTy();
931 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000932 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000933 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000934
935 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000936 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
937 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000938 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000939 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
940 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
941 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000942 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000943 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000944 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +0000945 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +0000946 break;
947 }
948 }
949 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000950 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000951 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
952 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
953 break;
954 }
955
Chris Lattnerce872152006-03-19 06:31:19 +0000956 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
957 Result = DAG.UpdateNodeOperands(Result, Tmp1);
958 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
959 Node->getValueType(0))) {
960 default: assert(0 && "This action is not supported yet!");
961 case TargetLowering::Legal:
962 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000963 case TargetLowering::Custom:
964 Tmp3 = TLI.LowerOperation(Result, DAG);
965 if (Tmp3.Val) {
966 Result = Tmp3;
967 break;
968 }
969 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000970 case TargetLowering::Expand:
971 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000972 break;
973 }
Chris Lattnerce872152006-03-19 06:31:19 +0000974 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000975 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
977 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
979
980 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000981 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
982 default: assert(0 && "Unknown operation action!");
983 case TargetLowering::Legal:
984 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
985 "vector shuffle should not be created if not legal!");
986 break;
987 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +0000988 Tmp3 = TLI.LowerOperation(Result, DAG);
989 if (Tmp3.Val) {
990 Result = Tmp3;
991 break;
992 }
993 // FALLTHROUGH
994 case TargetLowering::Expand: {
995 MVT::ValueType VT = Node->getValueType(0);
996 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
997 MVT::ValueType PtrVT = TLI.getPointerTy();
998 SDOperand Mask = Node->getOperand(2);
999 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001000 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001001 for (unsigned i = 0; i != NumElems; ++i) {
1002 SDOperand Arg = Mask.getOperand(i);
1003 if (Arg.getOpcode() == ISD::UNDEF) {
1004 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1005 } else {
1006 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1007 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1008 if (Idx < NumElems)
1009 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1010 DAG.getConstant(Idx, PtrVT)));
1011 else
1012 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1013 DAG.getConstant(Idx - NumElems, PtrVT)));
1014 }
1015 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001016 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001017 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001018 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001019 case TargetLowering::Promote: {
1020 // Change base type to a different vector type.
1021 MVT::ValueType OVT = Node->getValueType(0);
1022 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1023
1024 // Cast the two input vectors.
1025 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1026 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1027
1028 // Convert the shuffle mask to the right # elements.
1029 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1030 assert(Tmp3.Val && "Shuffle not legal?");
1031 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1032 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1033 break;
1034 }
Chris Lattner87100e02006-03-20 01:52:29 +00001035 }
1036 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001037
1038 case ISD::EXTRACT_VECTOR_ELT:
1039 Tmp1 = LegalizeOp(Node->getOperand(0));
1040 Tmp2 = LegalizeOp(Node->getOperand(1));
1041 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001042
1043 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1044 Tmp1.getValueType())) {
1045 default: assert(0 && "This action is not supported yet!");
1046 case TargetLowering::Legal:
1047 break;
1048 case TargetLowering::Custom:
1049 Tmp3 = TLI.LowerOperation(Result, DAG);
1050 if (Tmp3.Val) {
1051 Result = Tmp3;
1052 break;
1053 }
1054 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001055 case TargetLowering::Expand:
1056 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001057 break;
1058 }
Chris Lattner384504c2006-03-21 20:44:12 +00001059 break;
1060
Chris Lattner15972212006-03-31 17:55:51 +00001061 case ISD::VEXTRACT_VECTOR_ELT:
1062 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001063 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001064
Chris Lattner6831a812006-02-13 09:18:02 +00001065 case ISD::CALLSEQ_START: {
1066 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1067
1068 // Recursively Legalize all of the inputs of the call end that do not lead
1069 // to this call start. This ensures that any libcalls that need be inserted
1070 // are inserted *before* the CALLSEQ_START.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001071 {std::set<SDNode*> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001072 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001073 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1074 NodesLeadingTo);
1075 }
Chris Lattner6831a812006-02-13 09:18:02 +00001076
1077 // Now that we legalized all of the inputs (which may have inserted
1078 // libcalls) create the new CALLSEQ_START node.
1079 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1080
1081 // Merge in the last call, to ensure that this call start after the last
1082 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001083 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001084 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1085 Tmp1 = LegalizeOp(Tmp1);
1086 }
Chris Lattner6831a812006-02-13 09:18:02 +00001087
1088 // Do not try to legalize the target-specific arguments (#1+).
1089 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001090 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001091 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001092 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001093 }
1094
1095 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001096 AddLegalizedOperand(Op.getValue(0), Result);
1097 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1098 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1099
Chris Lattner6831a812006-02-13 09:18:02 +00001100 // Now that the callseq_start and all of the non-call nodes above this call
1101 // sequence have been legalized, legalize the call itself. During this
1102 // process, no libcalls can/will be inserted, guaranteeing that no calls
1103 // can overlap.
1104 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1105 SDOperand InCallSEQ = LastCALLSEQ_END;
1106 // Note that we are selecting this call!
1107 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1108 IsLegalizingCall = true;
1109
1110 // Legalize the call, starting from the CALLSEQ_END.
1111 LegalizeOp(LastCALLSEQ_END);
1112 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1113 return Result;
1114 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001115 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001116 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1117 // will cause this node to be legalized as well as handling libcalls right.
1118 if (LastCALLSEQ_END.Val != Node) {
1119 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1120 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1121 assert(I != LegalizedNodes.end() &&
1122 "Legalizing the call start should have legalized this node!");
1123 return I->second;
1124 }
1125
1126 // Otherwise, the call start has been legalized and everything is going
1127 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001129 // Do not try to legalize the target-specific arguments (#1+), except for
1130 // an optional flag input.
1131 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1132 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001133 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001134 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001135 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001136 }
1137 } else {
1138 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1139 if (Tmp1 != Node->getOperand(0) ||
1140 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001141 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001142 Ops[0] = Tmp1;
1143 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001144 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001145 }
Chris Lattner6a542892006-01-24 05:48:21 +00001146 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001147 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001148 // This finishes up call legalization.
1149 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001150
1151 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1152 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1153 if (Node->getNumValues() == 2)
1154 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1155 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001156 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1158 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1159 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001161
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001162 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001163 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001164 switch (TLI.getOperationAction(Node->getOpcode(),
1165 Node->getValueType(0))) {
1166 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001167 case TargetLowering::Expand: {
1168 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1169 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1170 " not tell us which reg is the stack pointer!");
1171 SDOperand Chain = Tmp1.getOperand(0);
1172 SDOperand Size = Tmp2.getOperand(1);
1173 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1174 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1175 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001176 Tmp1 = LegalizeOp(Tmp1);
1177 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001178 break;
1179 }
1180 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001181 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1182 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001183 Tmp1 = LegalizeOp(Tmp3);
1184 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001185 }
Chris Lattner903d2782006-01-15 08:54:32 +00001186 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001187 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001188 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001189 }
Chris Lattner903d2782006-01-15 08:54:32 +00001190 // Since this op produce two values, make sure to remember that we
1191 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001192 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1193 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001194 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001195 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001196 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001197 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001198 bool Changed = false;
1199 // Legalize all of the operands of the inline asm, in case they are nodes
1200 // that need to be expanded or something. Note we skip the asm string and
1201 // all of the TargetConstant flags.
1202 SDOperand Op = LegalizeOp(Ops[0]);
1203 Changed = Op != Ops[0];
1204 Ops[0] = Op;
1205
1206 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1207 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1208 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1209 for (++i; NumVals; ++i, --NumVals) {
1210 SDOperand Op = LegalizeOp(Ops[i]);
1211 if (Op != Ops[i]) {
1212 Changed = true;
1213 Ops[i] = Op;
1214 }
1215 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001216 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001217
1218 if (HasInFlag) {
1219 Op = LegalizeOp(Ops.back());
1220 Changed |= Op != Ops.back();
1221 Ops.back() = Op;
1222 }
1223
1224 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001225 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001226
1227 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001228 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001229 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1230 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001231 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001232 case ISD::BR:
1233 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001234 // Ensure that libcalls are emitted before a branch.
1235 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1236 Tmp1 = LegalizeOp(Tmp1);
1237 LastCALLSEQ_END = DAG.getEntryNode();
1238
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001240 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001241 case ISD::BRIND:
1242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1243 // Ensure that libcalls are emitted before a branch.
1244 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1245 Tmp1 = LegalizeOp(Tmp1);
1246 LastCALLSEQ_END = DAG.getEntryNode();
1247
1248 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1249 default: assert(0 && "Indirect target must be legal type (pointer)!");
1250 case Legal:
1251 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1252 break;
1253 }
1254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1255 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001256 case ISD::BR_JT:
1257 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1258 // Ensure that libcalls are emitted before a branch.
1259 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1260 Tmp1 = LegalizeOp(Tmp1);
1261 LastCALLSEQ_END = DAG.getEntryNode();
1262
1263 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1264 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1265
1266 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Legal: break;
1269 case TargetLowering::Custom:
1270 Tmp1 = TLI.LowerOperation(Result, DAG);
1271 if (Tmp1.Val) Result = Tmp1;
1272 break;
1273 case TargetLowering::Expand: {
1274 SDOperand Chain = Result.getOperand(0);
1275 SDOperand Table = Result.getOperand(1);
1276 SDOperand Index = Result.getOperand(2);
1277
1278 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001279 MachineFunction &MF = DAG.getMachineFunction();
1280 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001281 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1282 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001283
1284 SDOperand LD;
1285 switch (EntrySize) {
1286 default: assert(0 && "Size of jump table not supported yet."); break;
1287 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1288 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1289 }
1290
1291 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001292 // For PIC, the sequence is:
1293 // BRIND(load(Jumptable + index) + RelocBase)
1294 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1295 SDOperand Reloc;
1296 if (TLI.usesGlobalOffsetTable())
1297 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1298 else
1299 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001300 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001301 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1302 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1303 } else {
1304 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1305 }
1306 }
1307 }
1308 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001309 case ISD::BRCOND:
1310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001311 // Ensure that libcalls are emitted before a return.
1312 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1313 Tmp1 = LegalizeOp(Tmp1);
1314 LastCALLSEQ_END = DAG.getEntryNode();
1315
Chris Lattner47e92232005-01-18 19:27:06 +00001316 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1317 case Expand: assert(0 && "It's impossible to expand bools");
1318 case Legal:
1319 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1320 break;
1321 case Promote:
1322 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001323
1324 // The top bits of the promoted condition are not necessarily zero, ensure
1325 // that the value is properly zero extended.
1326 if (!TLI.MaskedValueIsZero(Tmp2,
1327 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1328 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001329 break;
1330 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001331
1332 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001334
1335 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1336 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001337 case TargetLowering::Legal: break;
1338 case TargetLowering::Custom:
1339 Tmp1 = TLI.LowerOperation(Result, DAG);
1340 if (Tmp1.Val) Result = Tmp1;
1341 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001342 case TargetLowering::Expand:
1343 // Expand brcond's setcc into its constituent parts and create a BR_CC
1344 // Node.
1345 if (Tmp2.getOpcode() == ISD::SETCC) {
1346 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1347 Tmp2.getOperand(0), Tmp2.getOperand(1),
1348 Node->getOperand(2));
1349 } else {
1350 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1351 DAG.getCondCode(ISD::SETNE), Tmp2,
1352 DAG.getConstant(0, Tmp2.getValueType()),
1353 Node->getOperand(2));
1354 }
1355 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001356 }
1357 break;
1358 case ISD::BR_CC:
1359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001360 // Ensure that libcalls are emitted before a branch.
1361 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1362 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001363 Tmp2 = Node->getOperand(2); // LHS
1364 Tmp3 = Node->getOperand(3); // RHS
1365 Tmp4 = Node->getOperand(1); // CC
1366
1367 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001368 LastCALLSEQ_END = DAG.getEntryNode();
1369
Nate Begeman750ac1b2006-02-01 07:19:44 +00001370 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1371 // the LHS is a legal SETCC itself. In this case, we need to compare
1372 // the result against zero to select between true and false values.
1373 if (Tmp3.Val == 0) {
1374 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1375 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001376 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001377
1378 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1379 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001380
Chris Lattner181b7a32005-12-17 23:46:46 +00001381 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1382 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001383 case TargetLowering::Legal: break;
1384 case TargetLowering::Custom:
1385 Tmp4 = TLI.LowerOperation(Result, DAG);
1386 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001387 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001388 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001389 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001390 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001391 LoadSDNode *LD = cast<LoadSDNode>(Node);
1392 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1393 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001394
Evan Cheng466685d2006-10-09 20:57:25 +00001395 ISD::LoadExtType ExtType = LD->getExtensionType();
1396 if (ExtType == ISD::NON_EXTLOAD) {
1397 MVT::ValueType VT = Node->getValueType(0);
1398 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1399 Tmp3 = Result.getValue(0);
1400 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001401
Evan Cheng466685d2006-10-09 20:57:25 +00001402 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1403 default: assert(0 && "This action is not supported yet!");
1404 case TargetLowering::Legal: break;
1405 case TargetLowering::Custom:
1406 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1407 if (Tmp1.Val) {
1408 Tmp3 = LegalizeOp(Tmp1);
1409 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001410 }
Evan Cheng466685d2006-10-09 20:57:25 +00001411 break;
1412 case TargetLowering::Promote: {
1413 // Only promote a load of vector type to another.
1414 assert(MVT::isVector(VT) && "Cannot promote this load!");
1415 // Change base type to a different vector type.
1416 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1417
1418 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1419 LD->getSrcValueOffset());
1420 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1421 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001422 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001423 }
Evan Cheng466685d2006-10-09 20:57:25 +00001424 }
1425 // Since loads produce two values, make sure to remember that we
1426 // legalized both of them.
1427 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1428 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1429 return Op.ResNo ? Tmp4 : Tmp3;
1430 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001431 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001432 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1433 default: assert(0 && "This action is not supported yet!");
1434 case TargetLowering::Promote:
1435 assert(SrcVT == MVT::i1 &&
1436 "Can only promote extending LOAD from i1 -> i8!");
1437 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1438 LD->getSrcValue(), LD->getSrcValueOffset(),
1439 MVT::i8);
1440 Tmp1 = Result.getValue(0);
1441 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001443 case TargetLowering::Custom:
1444 isCustom = true;
1445 // FALLTHROUGH
1446 case TargetLowering::Legal:
1447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1448 Tmp1 = Result.getValue(0);
1449 Tmp2 = Result.getValue(1);
1450
1451 if (isCustom) {
1452 Tmp3 = TLI.LowerOperation(Result, DAG);
1453 if (Tmp3.Val) {
1454 Tmp1 = LegalizeOp(Tmp3);
1455 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1456 }
1457 }
1458 break;
1459 case TargetLowering::Expand:
1460 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1461 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1462 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1463 LD->getSrcValueOffset());
1464 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1465 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1466 Tmp2 = LegalizeOp(Load.getValue(1));
1467 break;
1468 }
1469 assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!");
1470 // Turn the unsupported load into an EXTLOAD followed by an explicit
1471 // zero/sign extend inreg.
1472 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1473 Tmp1, Tmp2, LD->getSrcValue(),
1474 LD->getSrcValueOffset(), SrcVT);
1475 SDOperand ValRes;
1476 if (ExtType == ISD::SEXTLOAD)
1477 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1478 Result, DAG.getValueType(SrcVT));
1479 else
1480 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1481 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1482 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1483 break;
1484 }
1485 // Since loads produce two values, make sure to remember that we legalized
1486 // both of them.
1487 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1488 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1489 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001490 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001491 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001492 case ISD::EXTRACT_ELEMENT: {
1493 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1494 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001495 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001496 case Legal:
1497 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1498 // 1 -> Hi
1499 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1500 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1501 TLI.getShiftAmountTy()));
1502 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1503 } else {
1504 // 0 -> Lo
1505 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1506 Node->getOperand(0));
1507 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001508 break;
1509 case Expand:
1510 // Get both the low and high parts.
1511 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1512 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1513 Result = Tmp2; // 1 -> Hi
1514 else
1515 Result = Tmp1; // 0 -> Lo
1516 break;
1517 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001518 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001520
1521 case ISD::CopyToReg:
1522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001523
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001524 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001525 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001526 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001527 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001528 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001530 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001531 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001532 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001533 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1535 Tmp3);
1536 } else {
1537 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001538 }
1539
1540 // Since this produces two values, make sure to remember that we legalized
1541 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001542 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001543 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001544 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001545 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001546 break;
1547
1548 case ISD::RET:
1549 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001550
1551 // Ensure that libcalls are emitted before a return.
1552 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1553 Tmp1 = LegalizeOp(Tmp1);
1554 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001555
Chris Lattner3e928bb2005-01-07 07:47:09 +00001556 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001557 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001558 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001559 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001560 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001561 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001562 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001563 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001564 case Expand:
1565 if (Tmp2.getValueType() != MVT::Vector) {
1566 SDOperand Lo, Hi;
1567 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng13acce32006-12-11 19:27:14 +00001568 if (Hi.Val)
1569 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1570 else
1571 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001572 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001573 } else {
1574 SDNode *InVal = Tmp2.Val;
1575 unsigned NumElems =
1576 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1577 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1578
1579 // Figure out if there is a Packed type corresponding to this Vector
1580 // type. If so, convert to the packed type.
1581 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1582 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1583 // Turn this into a return of the packed type.
1584 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001585 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001586 } else if (NumElems == 1) {
1587 // Turn this into a return of the scalar type.
1588 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001589 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001590
1591 // FIXME: Returns of gcc generic vectors smaller than a legal type
1592 // should be returned in integer registers!
1593
Chris Lattnerf87324e2006-04-11 01:31:51 +00001594 // The scalarized value type may not be legal, e.g. it might require
1595 // promotion or expansion. Relegalize the return.
1596 Result = LegalizeOp(Result);
1597 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001598 // FIXME: Returns of gcc generic vectors larger than a legal vector
1599 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001600 SDOperand Lo, Hi;
1601 SplitVectorOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001602 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001603 Result = LegalizeOp(Result);
1604 }
1605 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001606 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001607 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001608 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001609 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001610 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001611 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001612 }
1613 break;
1614 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001615 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001616 break;
1617 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001618 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001619 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001620 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001621 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1622 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001623 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001624 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001625 break;
1626 case Expand: {
1627 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001628 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1629 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001630 ExpandOp(Node->getOperand(i), Lo, Hi);
1631 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001632 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001633 if (Hi.Val) {
1634 NewValues.push_back(Hi);
1635 NewValues.push_back(Node->getOperand(i+1));
1636 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001637 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001638 }
1639 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001640 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001641 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001642
1643 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001644 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001645 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001646 Result = DAG.getNode(ISD::RET, MVT::Other,
1647 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001648 break;
1649 }
1650 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001651
Chris Lattner6862dbc2006-01-29 21:02:23 +00001652 if (Result.getOpcode() == ISD::RET) {
1653 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1654 default: assert(0 && "This action is not supported yet!");
1655 case TargetLowering::Legal: break;
1656 case TargetLowering::Custom:
1657 Tmp1 = TLI.LowerOperation(Result, DAG);
1658 if (Tmp1.Val) Result = Tmp1;
1659 break;
1660 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001661 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001662 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001663 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001664 StoreSDNode *ST = cast<StoreSDNode>(Node);
1665 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1666 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001667
Evan Cheng8b2794a2006-10-13 21:14:26 +00001668 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001669 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1670 // FIXME: We shouldn't do this for TargetConstantFP's.
1671 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1672 // to phase ordering between legalized code and the dag combiner. This
1673 // probably means that we need to integrate dag combiner and legalizer
1674 // together.
1675 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1676 if (CFP->getValueType(0) == MVT::f32) {
1677 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1678 } else {
1679 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1680 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1681 }
1682 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1683 ST->getSrcValueOffset());
1684 break;
1685 }
1686
Evan Cheng8b2794a2006-10-13 21:14:26 +00001687 switch (getTypeAction(ST->getStoredVT())) {
1688 case Legal: {
1689 Tmp3 = LegalizeOp(ST->getValue());
1690 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1691 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001692
Evan Cheng8b2794a2006-10-13 21:14:26 +00001693 MVT::ValueType VT = Tmp3.getValueType();
1694 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1695 default: assert(0 && "This action is not supported yet!");
1696 case TargetLowering::Legal: break;
1697 case TargetLowering::Custom:
1698 Tmp1 = TLI.LowerOperation(Result, DAG);
1699 if (Tmp1.Val) Result = Tmp1;
1700 break;
1701 case TargetLowering::Promote:
1702 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1703 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1704 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1705 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1706 ST->getSrcValue(), ST->getSrcValueOffset());
1707 break;
1708 }
1709 break;
1710 }
1711 case Promote:
1712 // Truncate the value and store the result.
1713 Tmp3 = PromoteOp(ST->getValue());
1714 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1715 ST->getSrcValueOffset(), ST->getStoredVT());
1716 break;
1717
1718 case Expand:
1719 unsigned IncrementSize = 0;
1720 SDOperand Lo, Hi;
1721
1722 // If this is a vector type, then we have to calculate the increment as
1723 // the product of the element size in bytes, and the number of elements
1724 // in the high half of the vector.
1725 if (ST->getValue().getValueType() == MVT::Vector) {
1726 SDNode *InVal = ST->getValue().Val;
1727 unsigned NumElems =
1728 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1729 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1730
1731 // Figure out if there is a Packed type corresponding to this Vector
1732 // type. If so, convert to the packed type.
1733 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1734 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1735 // Turn this into a normal store of the packed type.
1736 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1737 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1738 ST->getSrcValueOffset());
1739 Result = LegalizeOp(Result);
1740 break;
1741 } else if (NumElems == 1) {
1742 // Turn this into a normal store of the scalar type.
1743 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1744 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1745 ST->getSrcValueOffset());
1746 // The scalarized value type may not be legal, e.g. it might require
1747 // promotion or expansion. Relegalize the scalar store.
1748 Result = LegalizeOp(Result);
1749 break;
1750 } else {
1751 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1752 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1753 }
1754 } else {
1755 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001756 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001757
1758 if (!TLI.isLittleEndian())
1759 std::swap(Lo, Hi);
1760 }
1761
1762 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1763 ST->getSrcValueOffset());
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001764
1765 if (Hi.Val == NULL) {
1766 // Must be int <-> float one-to-one expansion.
1767 Result = Lo;
1768 break;
1769 }
1770
Evan Cheng8b2794a2006-10-13 21:14:26 +00001771 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1772 getIntPtrConstant(IncrementSize));
1773 assert(isTypeLegal(Tmp2.getValueType()) &&
1774 "Pointers must be legal!");
1775 // FIXME: This sets the srcvalue of both halves to be the same, which is
1776 // wrong.
1777 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1778 ST->getSrcValueOffset());
1779 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1780 break;
1781 }
1782 } else {
1783 // Truncating store
1784 assert(isTypeLegal(ST->getValue().getValueType()) &&
1785 "Cannot handle illegal TRUNCSTORE yet!");
1786 Tmp3 = LegalizeOp(ST->getValue());
1787
1788 // The only promote case we handle is TRUNCSTORE:i1 X into
1789 // -> TRUNCSTORE:i8 (and X, 1)
1790 if (ST->getStoredVT() == MVT::i1 &&
1791 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1792 // Promote the bool to a mask then store.
1793 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1794 DAG.getConstant(1, Tmp3.getValueType()));
1795 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1796 ST->getSrcValueOffset(), MVT::i8);
1797 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1798 Tmp2 != ST->getBasePtr()) {
1799 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1800 ST->getOffset());
1801 }
1802
1803 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1804 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001805 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001806 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001807 case TargetLowering::Custom:
1808 Tmp1 = TLI.LowerOperation(Result, DAG);
1809 if (Tmp1.Val) Result = Tmp1;
1810 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001811 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001812 }
1813 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001814 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001815 case ISD::PCMARKER:
1816 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001817 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001818 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001819 case ISD::STACKSAVE:
1820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001821 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1822 Tmp1 = Result.getValue(0);
1823 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001824
Chris Lattner140d53c2006-01-13 02:50:02 +00001825 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1826 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001827 case TargetLowering::Legal: break;
1828 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001829 Tmp3 = TLI.LowerOperation(Result, DAG);
1830 if (Tmp3.Val) {
1831 Tmp1 = LegalizeOp(Tmp3);
1832 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001833 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001834 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001835 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001836 // Expand to CopyFromReg if the target set
1837 // StackPointerRegisterToSaveRestore.
1838 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001839 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001840 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001841 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001842 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001843 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1844 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001845 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001846 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001847 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001848
1849 // Since stacksave produce two values, make sure to remember that we
1850 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001851 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1852 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1853 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001854
Chris Lattner140d53c2006-01-13 02:50:02 +00001855 case ISD::STACKRESTORE:
1856 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1857 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001859
1860 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1861 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001862 case TargetLowering::Legal: break;
1863 case TargetLowering::Custom:
1864 Tmp1 = TLI.LowerOperation(Result, DAG);
1865 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001866 break;
1867 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001868 // Expand to CopyToReg if the target set
1869 // StackPointerRegisterToSaveRestore.
1870 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1871 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1872 } else {
1873 Result = Tmp1;
1874 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001875 break;
1876 }
1877 break;
1878
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001879 case ISD::READCYCLECOUNTER:
1880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001881 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001882 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1883 Node->getValueType(0))) {
1884 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001885 case TargetLowering::Legal:
1886 Tmp1 = Result.getValue(0);
1887 Tmp2 = Result.getValue(1);
1888 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001889 case TargetLowering::Custom:
1890 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001891 Tmp1 = LegalizeOp(Result.getValue(0));
1892 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001893 break;
1894 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001895
1896 // Since rdcc produce two values, make sure to remember that we legalized
1897 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001898 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1899 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001900 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001901
Chris Lattner2ee743f2005-01-14 22:08:15 +00001902 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001903 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1904 case Expand: assert(0 && "It's impossible to expand bools");
1905 case Legal:
1906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1907 break;
1908 case Promote:
1909 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001910 // Make sure the condition is either zero or one.
1911 if (!TLI.MaskedValueIsZero(Tmp1,
1912 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1913 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001914 break;
1915 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001916 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001917 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001918
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001920
Nate Begemanb942a3d2005-08-23 04:29:48 +00001921 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001922 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001923 case TargetLowering::Legal: break;
1924 case TargetLowering::Custom: {
1925 Tmp1 = TLI.LowerOperation(Result, DAG);
1926 if (Tmp1.Val) Result = Tmp1;
1927 break;
1928 }
Nate Begeman9373a812005-08-10 20:51:12 +00001929 case TargetLowering::Expand:
1930 if (Tmp1.getOpcode() == ISD::SETCC) {
1931 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1932 Tmp2, Tmp3,
1933 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1934 } else {
1935 Result = DAG.getSelectCC(Tmp1,
1936 DAG.getConstant(0, Tmp1.getValueType()),
1937 Tmp2, Tmp3, ISD::SETNE);
1938 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001939 break;
1940 case TargetLowering::Promote: {
1941 MVT::ValueType NVT =
1942 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1943 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001944 if (MVT::isVector(Tmp2.getValueType())) {
1945 ExtOp = ISD::BIT_CONVERT;
1946 TruncOp = ISD::BIT_CONVERT;
1947 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001948 ExtOp = ISD::ANY_EXTEND;
1949 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001950 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001951 ExtOp = ISD::FP_EXTEND;
1952 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001953 }
1954 // Promote each of the values to the new type.
1955 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1956 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1957 // Perform the larger operation, then round down.
1958 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1959 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1960 break;
1961 }
1962 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001963 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001964 case ISD::SELECT_CC: {
1965 Tmp1 = Node->getOperand(0); // LHS
1966 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001967 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1968 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001969 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001970
Nate Begeman750ac1b2006-02-01 07:19:44 +00001971 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1972
1973 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1974 // the LHS is a legal SETCC itself. In this case, we need to compare
1975 // the result against zero to select between true and false values.
1976 if (Tmp2.Val == 0) {
1977 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1978 CC = DAG.getCondCode(ISD::SETNE);
1979 }
1980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1981
1982 // Everything is legal, see if we should expand this op or something.
1983 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1984 default: assert(0 && "This action is not supported yet!");
1985 case TargetLowering::Legal: break;
1986 case TargetLowering::Custom:
1987 Tmp1 = TLI.LowerOperation(Result, DAG);
1988 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001989 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001990 }
1991 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001992 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001993 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001994 Tmp1 = Node->getOperand(0);
1995 Tmp2 = Node->getOperand(1);
1996 Tmp3 = Node->getOperand(2);
1997 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1998
1999 // If we had to Expand the SetCC operands into a SELECT node, then it may
2000 // not always be possible to return a true LHS & RHS. In this case, just
2001 // return the value we legalized, returned in the LHS
2002 if (Tmp2.Val == 0) {
2003 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002004 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002005 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002006
Chris Lattner73e142f2006-01-30 22:43:50 +00002007 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002008 default: assert(0 && "Cannot handle this action for SETCC yet!");
2009 case TargetLowering::Custom:
2010 isCustom = true;
2011 // FALLTHROUGH.
2012 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002014 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002015 Tmp4 = TLI.LowerOperation(Result, DAG);
2016 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002017 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002018 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002019 case TargetLowering::Promote: {
2020 // First step, figure out the appropriate operation to use.
2021 // Allow SETCC to not be supported for all legal data types
2022 // Mostly this targets FP
2023 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2024 MVT::ValueType OldVT = NewInTy;
2025
2026 // Scan for the appropriate larger type to use.
2027 while (1) {
2028 NewInTy = (MVT::ValueType)(NewInTy+1);
2029
2030 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2031 "Fell off of the edge of the integer world");
2032 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2033 "Fell off of the edge of the floating point world");
2034
2035 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002036 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002037 break;
2038 }
2039 if (MVT::isInteger(NewInTy))
2040 assert(0 && "Cannot promote Legal Integer SETCC yet");
2041 else {
2042 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2043 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2044 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002045 Tmp1 = LegalizeOp(Tmp1);
2046 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002048 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002049 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002050 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002051 case TargetLowering::Expand:
2052 // Expand a setcc node into a select_cc of the same condition, lhs, and
2053 // rhs that selects between const 1 (true) and const 0 (false).
2054 MVT::ValueType VT = Node->getValueType(0);
2055 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2056 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002057 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002058 break;
2059 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002060 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002061 case ISD::MEMSET:
2062 case ISD::MEMCPY:
2063 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002064 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002065 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2066
2067 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2068 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2069 case Expand: assert(0 && "Cannot expand a byte!");
2070 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002071 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002072 break;
2073 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002074 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002075 break;
2076 }
2077 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002078 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002079 }
Chris Lattner272455b2005-02-02 03:44:41 +00002080
2081 SDOperand Tmp4;
2082 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002083 case Expand: {
2084 // Length is too big, just take the lo-part of the length.
2085 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002086 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002087 break;
2088 }
Chris Lattnere5605212005-01-28 22:29:18 +00002089 case Legal:
2090 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002091 break;
2092 case Promote:
2093 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002094 break;
2095 }
2096
2097 SDOperand Tmp5;
2098 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2099 case Expand: assert(0 && "Cannot expand this yet!");
2100 case Legal:
2101 Tmp5 = LegalizeOp(Node->getOperand(4));
2102 break;
2103 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002104 Tmp5 = PromoteOp(Node->getOperand(4));
2105 break;
2106 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002107
2108 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2109 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002110 case TargetLowering::Custom:
2111 isCustom = true;
2112 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002113 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002115 if (isCustom) {
2116 Tmp1 = TLI.LowerOperation(Result, DAG);
2117 if (Tmp1.Val) Result = Tmp1;
2118 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002119 break;
2120 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002121 // Otherwise, the target does not support this operation. Lower the
2122 // operation to an explicit libcall as appropriate.
2123 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002124 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002125 TargetLowering::ArgListTy Args;
2126 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002127
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002128 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002129 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002130 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002131 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002132 // Extend the (previously legalized) ubyte argument to be an int value
2133 // for the call.
2134 if (Tmp3.getValueType() > MVT::i32)
2135 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2136 else
2137 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencer47857812006-12-31 05:55:36 +00002138 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
2139 Args.push_back(Entry);
2140 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2141 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002142
2143 FnName = "memset";
2144 } else if (Node->getOpcode() == ISD::MEMCPY ||
2145 Node->getOpcode() == ISD::MEMMOVE) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002146 Entry.Ty = IntPtrTy; Entry.isSigned = false;
2147 Entry.Node = Tmp2; Args.push_back(Entry);
2148 Entry.Node = Tmp3; Args.push_back(Entry);
2149 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002150 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2151 } else {
2152 assert(0 && "Unknown op!");
2153 }
Chris Lattner45982da2005-05-12 16:53:42 +00002154
Chris Lattnere1bd8222005-01-11 05:57:22 +00002155 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002156 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002157 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002158 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002159 break;
2160 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002161 }
2162 break;
2163 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002164
Chris Lattner5b359c62005-04-02 04:00:59 +00002165 case ISD::SHL_PARTS:
2166 case ISD::SRA_PARTS:
2167 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002168 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002169 bool Changed = false;
2170 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2171 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2172 Changed |= Ops.back() != Node->getOperand(i);
2173 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002174 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002175 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002176
Evan Cheng05a2d562006-01-09 18:31:59 +00002177 switch (TLI.getOperationAction(Node->getOpcode(),
2178 Node->getValueType(0))) {
2179 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002180 case TargetLowering::Legal: break;
2181 case TargetLowering::Custom:
2182 Tmp1 = TLI.LowerOperation(Result, DAG);
2183 if (Tmp1.Val) {
2184 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002185 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002186 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002187 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2188 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002189 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002190 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002191 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002192 return RetVal;
2193 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002194 break;
2195 }
2196
Chris Lattner2c8086f2005-04-02 05:00:07 +00002197 // Since these produce multiple values, make sure to remember that we
2198 // legalized all of them.
2199 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2200 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2201 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002202 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002203
2204 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002205 case ISD::ADD:
2206 case ISD::SUB:
2207 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002208 case ISD::MULHS:
2209 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002210 case ISD::UDIV:
2211 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002212 case ISD::AND:
2213 case ISD::OR:
2214 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002215 case ISD::SHL:
2216 case ISD::SRL:
2217 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002218 case ISD::FADD:
2219 case ISD::FSUB:
2220 case ISD::FMUL:
2221 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002222 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002223 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2224 case Expand: assert(0 && "Not possible");
2225 case Legal:
2226 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2227 break;
2228 case Promote:
2229 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2230 break;
2231 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002232
2233 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002234
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002235 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002236 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002237 case TargetLowering::Legal: break;
2238 case TargetLowering::Custom:
2239 Tmp1 = TLI.LowerOperation(Result, DAG);
2240 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002241 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002242 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002243 if (Node->getValueType(0) == MVT::i32) {
2244 switch (Node->getOpcode()) {
2245 default: assert(0 && "Do not know how to expand this integer BinOp!");
2246 case ISD::UDIV:
2247 case ISD::SDIV:
2248 const char *FnName = Node->getOpcode() == ISD::UDIV
2249 ? "__udivsi3" : "__divsi3";
2250 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002251 bool isSigned = Node->getOpcode() == ISD::SDIV;
2252 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002253 };
2254 break;
2255 }
2256
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002257 assert(MVT::isVector(Node->getValueType(0)) &&
2258 "Cannot expand this binary operator!");
2259 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002260 SmallVector<SDOperand, 8> Ops;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002261 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2262 MVT::ValueType PtrVT = TLI.getPointerTy();
2263 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2264 i != e; ++i) {
2265 SDOperand Idx = DAG.getConstant(i, PtrVT);
2266 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2267 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2268 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2269 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002270 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2271 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002272 break;
2273 }
Evan Chengcc987612006-04-12 21:20:24 +00002274 case TargetLowering::Promote: {
2275 switch (Node->getOpcode()) {
2276 default: assert(0 && "Do not know how to promote this BinOp!");
2277 case ISD::AND:
2278 case ISD::OR:
2279 case ISD::XOR: {
2280 MVT::ValueType OVT = Node->getValueType(0);
2281 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2282 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2283 // Bit convert each of the values to the new type.
2284 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2285 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2286 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2287 // Bit convert the result back the original type.
2288 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2289 break;
2290 }
2291 }
2292 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002293 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002295
2296 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2297 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2298 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2299 case Expand: assert(0 && "Not possible");
2300 case Legal:
2301 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2302 break;
2303 case Promote:
2304 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2305 break;
2306 }
2307
2308 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2309
2310 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2311 default: assert(0 && "Operation not supported");
2312 case TargetLowering::Custom:
2313 Tmp1 = TLI.LowerOperation(Result, DAG);
2314 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002315 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002316 case TargetLowering::Legal: break;
2317 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002318 // If this target supports fabs/fneg natively, do this efficiently.
2319 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2320 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2321 // Get the sign bit of the RHS.
2322 MVT::ValueType IVT =
2323 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2324 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2325 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2326 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2327 // Get the absolute value of the result.
2328 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2329 // Select between the nabs and abs value based on the sign bit of
2330 // the input.
2331 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2332 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2333 AbsVal),
2334 AbsVal);
2335 Result = LegalizeOp(Result);
2336 break;
2337 }
2338
2339 // Otherwise, do bitwise ops!
2340
2341 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002342 const char *FnName;
2343 if (Node->getValueType(0) == MVT::f32) {
2344 FnName = "copysignf";
2345 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2346 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2347 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2348 } else {
2349 FnName = "copysign";
2350 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2351 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2352 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2353 }
2354 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002355 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnera09f8482006-03-05 05:09:38 +00002356 break;
2357 }
2358 break;
2359
Nate Begeman551bf3f2006-02-17 05:43:56 +00002360 case ISD::ADDC:
2361 case ISD::SUBC:
2362 Tmp1 = LegalizeOp(Node->getOperand(0));
2363 Tmp2 = LegalizeOp(Node->getOperand(1));
2364 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2365 // Since this produces two values, make sure to remember that we legalized
2366 // both of them.
2367 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2368 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2369 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002370
Nate Begeman551bf3f2006-02-17 05:43:56 +00002371 case ISD::ADDE:
2372 case ISD::SUBE:
2373 Tmp1 = LegalizeOp(Node->getOperand(0));
2374 Tmp2 = LegalizeOp(Node->getOperand(1));
2375 Tmp3 = LegalizeOp(Node->getOperand(2));
2376 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2377 // Since this produces two values, make sure to remember that we legalized
2378 // both of them.
2379 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2380 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2381 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002382
Nate Begeman419f8b62005-10-18 00:27:41 +00002383 case ISD::BUILD_PAIR: {
2384 MVT::ValueType PairTy = Node->getValueType(0);
2385 // TODO: handle the case where the Lo and Hi operands are not of legal type
2386 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2387 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2388 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002389 case TargetLowering::Promote:
2390 case TargetLowering::Custom:
2391 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002392 case TargetLowering::Legal:
2393 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2394 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2395 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002396 case TargetLowering::Expand:
2397 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2398 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2399 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2400 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2401 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002402 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002403 break;
2404 }
2405 break;
2406 }
2407
Nate Begemanc105e192005-04-06 00:23:54 +00002408 case ISD::UREM:
2409 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002410 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002411 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2412 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002413
Nate Begemanc105e192005-04-06 00:23:54 +00002414 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002415 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2416 case TargetLowering::Custom:
2417 isCustom = true;
2418 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002419 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002420 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002421 if (isCustom) {
2422 Tmp1 = TLI.LowerOperation(Result, DAG);
2423 if (Tmp1.Val) Result = Tmp1;
2424 }
Nate Begemanc105e192005-04-06 00:23:54 +00002425 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002426 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002427 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002428 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002429 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002430 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2431 TargetLowering::Legal) {
2432 // X % Y -> X-X/Y*Y
2433 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002434 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002435 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2436 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2437 } else {
2438 assert(Node->getValueType(0) == MVT::i32 &&
2439 "Cannot expand this binary operator!");
2440 const char *FnName = Node->getOpcode() == ISD::UREM
2441 ? "__umodsi3" : "__modsi3";
2442 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002443 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002444 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002445 } else {
2446 // Floating point mod -> fmod libcall.
2447 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2448 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002449 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002450 }
2451 break;
2452 }
2453 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002454 case ISD::VAARG: {
2455 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2456 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2457
Chris Lattner5c62f332006-01-28 07:42:08 +00002458 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002459 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2460 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002461 case TargetLowering::Custom:
2462 isCustom = true;
2463 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002464 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2466 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002467 Tmp1 = Result.getValue(1);
2468
2469 if (isCustom) {
2470 Tmp2 = TLI.LowerOperation(Result, DAG);
2471 if (Tmp2.Val) {
2472 Result = LegalizeOp(Tmp2);
2473 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2474 }
2475 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002476 break;
2477 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002478 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002479 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002480 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002481 // Increment the pointer, VAList, to the next vaarg
2482 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2483 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2484 TLI.getPointerTy()));
2485 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002486 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2487 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002488 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002489 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002490 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002491 Result = LegalizeOp(Result);
2492 break;
2493 }
2494 }
2495 // Since VAARG produces two values, make sure to remember that we
2496 // legalized both of them.
2497 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002498 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2499 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002500 }
2501
2502 case ISD::VACOPY:
2503 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2504 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2505 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2506
2507 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2508 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002509 case TargetLowering::Custom:
2510 isCustom = true;
2511 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002512 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002513 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2514 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002515 if (isCustom) {
2516 Tmp1 = TLI.LowerOperation(Result, DAG);
2517 if (Tmp1.Val) Result = Tmp1;
2518 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002519 break;
2520 case TargetLowering::Expand:
2521 // This defaults to loading a pointer from the input and storing it to the
2522 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002523 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002524 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002525 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2526 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002527 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2528 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002529 break;
2530 }
2531 break;
2532
2533 case ISD::VAEND:
2534 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2535 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2536
2537 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2538 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002539 case TargetLowering::Custom:
2540 isCustom = true;
2541 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002542 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002544 if (isCustom) {
2545 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2546 if (Tmp1.Val) Result = Tmp1;
2547 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002548 break;
2549 case TargetLowering::Expand:
2550 Result = Tmp1; // Default to a no-op, return the chain
2551 break;
2552 }
2553 break;
2554
2555 case ISD::VASTART:
2556 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2557 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2558
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002559 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2560
Nate Begemanacc398c2006-01-25 18:21:52 +00002561 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2562 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002563 case TargetLowering::Legal: break;
2564 case TargetLowering::Custom:
2565 Tmp1 = TLI.LowerOperation(Result, DAG);
2566 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002567 break;
2568 }
2569 break;
2570
Nate Begeman35ef9132006-01-11 21:21:00 +00002571 case ISD::ROTL:
2572 case ISD::ROTR:
2573 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2574 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002575
2576 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2577 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002579 break;
2580
Nate Begemand88fc032006-01-14 03:14:10 +00002581 case ISD::BSWAP:
2582 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2583 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002584 case TargetLowering::Custom:
2585 assert(0 && "Cannot custom legalize this yet!");
2586 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002587 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002588 break;
2589 case TargetLowering::Promote: {
2590 MVT::ValueType OVT = Tmp1.getValueType();
2591 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2592 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002593
Chris Lattner456a93a2006-01-28 07:39:30 +00002594 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2595 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2596 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2597 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2598 break;
2599 }
2600 case TargetLowering::Expand:
2601 Result = ExpandBSWAP(Tmp1);
2602 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002603 }
2604 break;
2605
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002606 case ISD::CTPOP:
2607 case ISD::CTTZ:
2608 case ISD::CTLZ:
2609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2610 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002611 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002612 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002613 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002614 break;
2615 case TargetLowering::Promote: {
2616 MVT::ValueType OVT = Tmp1.getValueType();
2617 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002618
2619 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002620 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2621 // Perform the larger operation, then subtract if needed.
2622 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002623 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002624 case ISD::CTPOP:
2625 Result = Tmp1;
2626 break;
2627 case ISD::CTTZ:
2628 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002629 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2630 DAG.getConstant(getSizeInBits(NVT), NVT),
2631 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002632 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002633 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2634 break;
2635 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002636 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002637 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2638 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002639 getSizeInBits(OVT), NVT));
2640 break;
2641 }
2642 break;
2643 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002644 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002645 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002646 break;
2647 }
2648 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002649
Chris Lattner2c8086f2005-04-02 05:00:07 +00002650 // Unary operators
2651 case ISD::FABS:
2652 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002653 case ISD::FSQRT:
2654 case ISD::FSIN:
2655 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002656 Tmp1 = LegalizeOp(Node->getOperand(0));
2657 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002658 case TargetLowering::Promote:
2659 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002660 isCustom = true;
2661 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002662 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002663 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002664 if (isCustom) {
2665 Tmp1 = TLI.LowerOperation(Result, DAG);
2666 if (Tmp1.Val) Result = Tmp1;
2667 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002668 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002669 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002670 switch (Node->getOpcode()) {
2671 default: assert(0 && "Unreachable!");
2672 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002673 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2674 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002675 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002676 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002677 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002678 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2679 MVT::ValueType VT = Node->getValueType(0);
2680 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002681 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002682 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2683 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002684 break;
2685 }
2686 case ISD::FSQRT:
2687 case ISD::FSIN:
2688 case ISD::FCOS: {
2689 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002690 const char *FnName = 0;
2691 switch(Node->getOpcode()) {
2692 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2693 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2694 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2695 default: assert(0 && "Unreachable!");
2696 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002697 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002698 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002699 break;
2700 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002701 }
2702 break;
2703 }
2704 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002705 case ISD::FPOWI: {
2706 // We always lower FPOWI into a libcall. No target support it yet.
2707 const char *FnName = Node->getValueType(0) == MVT::f32
2708 ? "__powisf2" : "__powidf2";
2709 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002710 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002711 break;
2712 }
Chris Lattner35481892005-12-23 00:16:34 +00002713 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002714 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002715 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002716 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002717 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2718 Node->getOperand(0).getValueType())) {
2719 default: assert(0 && "Unknown operation action!");
2720 case TargetLowering::Expand:
2721 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2722 break;
2723 case TargetLowering::Legal:
2724 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002725 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002726 break;
2727 }
2728 }
2729 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002730 case ISD::VBIT_CONVERT: {
2731 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2732 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2733
2734 // The input has to be a vector type, we have to either scalarize it, pack
2735 // it, or convert it based on whether the input vector type is legal.
2736 SDNode *InVal = Node->getOperand(0).Val;
2737 unsigned NumElems =
2738 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2739 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2740
2741 // Figure out if there is a Packed type corresponding to this Vector
2742 // type. If so, convert to the packed type.
2743 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2744 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2745 // Turn this into a bit convert of the packed input.
2746 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2747 PackVectorOp(Node->getOperand(0), TVT));
2748 break;
2749 } else if (NumElems == 1) {
2750 // Turn this into a bit convert of the scalar input.
2751 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2752 PackVectorOp(Node->getOperand(0), EVT));
2753 break;
2754 } else {
2755 // FIXME: UNIMP! Store then reload
2756 assert(0 && "Cast from unsupported vector type not implemented yet!");
2757 }
2758 }
2759
Chris Lattner2c8086f2005-04-02 05:00:07 +00002760 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002761 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002762 case ISD::UINT_TO_FP: {
2763 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002764 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2765 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002766 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002767 Node->getOperand(0).getValueType())) {
2768 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002769 case TargetLowering::Custom:
2770 isCustom = true;
2771 // FALLTHROUGH
2772 case TargetLowering::Legal:
2773 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002774 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002775 if (isCustom) {
2776 Tmp1 = TLI.LowerOperation(Result, DAG);
2777 if (Tmp1.Val) Result = Tmp1;
2778 }
2779 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002780 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002781 Result = ExpandLegalINT_TO_FP(isSigned,
2782 LegalizeOp(Node->getOperand(0)),
2783 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002784 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002785 case TargetLowering::Promote:
2786 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2787 Node->getValueType(0),
2788 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002789 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002790 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002791 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002792 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002793 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2794 Node->getValueType(0), Node->getOperand(0));
2795 break;
2796 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002797 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002798 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002799 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2800 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002801 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002802 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2803 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002804 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002805 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2806 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002807 break;
2808 }
2809 break;
2810 }
2811 case ISD::TRUNCATE:
2812 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2813 case Legal:
2814 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002815 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002816 break;
2817 case Expand:
2818 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2819
2820 // Since the result is legal, we should just be able to truncate the low
2821 // part of the source.
2822 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2823 break;
2824 case Promote:
2825 Result = PromoteOp(Node->getOperand(0));
2826 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2827 break;
2828 }
2829 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002830
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002831 case ISD::FP_TO_SINT:
2832 case ISD::FP_TO_UINT:
2833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2834 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002835 Tmp1 = LegalizeOp(Node->getOperand(0));
2836
Chris Lattner1618beb2005-07-29 00:11:56 +00002837 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2838 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002839 case TargetLowering::Custom:
2840 isCustom = true;
2841 // FALLTHROUGH
2842 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002843 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002844 if (isCustom) {
2845 Tmp1 = TLI.LowerOperation(Result, DAG);
2846 if (Tmp1.Val) Result = Tmp1;
2847 }
2848 break;
2849 case TargetLowering::Promote:
2850 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2851 Node->getOpcode() == ISD::FP_TO_SINT);
2852 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002853 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002854 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2855 SDOperand True, False;
2856 MVT::ValueType VT = Node->getOperand(0).getValueType();
2857 MVT::ValueType NVT = Node->getValueType(0);
2858 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2859 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2860 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2861 Node->getOperand(0), Tmp2, ISD::SETLT);
2862 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2863 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002864 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002865 Tmp2));
2866 False = DAG.getNode(ISD::XOR, NVT, False,
2867 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002868 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2869 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002870 } else {
2871 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2872 }
2873 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002874 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002875 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002876 case Expand: {
2877 // Convert f32 / f64 to i32 / i64.
2878 MVT::ValueType VT = Op.getValueType();
2879 const char *FnName = 0;
2880 switch (Node->getOpcode()) {
2881 case ISD::FP_TO_SINT:
2882 if (Node->getOperand(0).getValueType() == MVT::f32)
2883 FnName = (VT == MVT::i32) ? "__fixsfsi" : "__fixsfdi";
2884 else
2885 FnName = (VT == MVT::i32) ? "__fixdfsi" : "__fixdfdi";
2886 break;
2887 case ISD::FP_TO_UINT:
2888 if (Node->getOperand(0).getValueType() == MVT::f32)
2889 FnName = (VT == MVT::i32) ? "__fixunssfsi" : "__fixunssfdi";
2890 else
2891 FnName = (VT == MVT::i32) ? "__fixunsdfsi" : "__fixunsdfdi";
2892 break;
2893 default: assert(0 && "Unreachable!");
2894 }
2895 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002896 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00002897 break;
2898 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002899 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002900 Tmp1 = PromoteOp(Node->getOperand(0));
2901 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2902 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002903 break;
2904 }
2905 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002906
Chris Lattner13c78e22005-09-02 00:18:10 +00002907 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002908 case ISD::ZERO_EXTEND:
2909 case ISD::SIGN_EXTEND:
2910 case ISD::FP_EXTEND:
2911 case ISD::FP_ROUND:
2912 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002913 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002914 case Legal:
2915 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002916 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002917 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002918 case Promote:
2919 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002920 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002921 Tmp1 = PromoteOp(Node->getOperand(0));
2922 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002923 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002924 case ISD::ZERO_EXTEND:
2925 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002926 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002927 Result = DAG.getZeroExtendInReg(Result,
2928 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002929 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002930 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002931 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002932 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002933 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002934 Result,
2935 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002936 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002937 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002938 Result = PromoteOp(Node->getOperand(0));
2939 if (Result.getValueType() != Op.getValueType())
2940 // Dynamically dead while we have only 2 FP types.
2941 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2942 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002943 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002944 Result = PromoteOp(Node->getOperand(0));
2945 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2946 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002947 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002948 }
2949 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002950 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002951 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002952 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002953 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002954
2955 // If this operation is not supported, convert it to a shl/shr or load/store
2956 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002957 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2958 default: assert(0 && "This action not supported for this op yet!");
2959 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002960 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002961 break;
2962 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002963 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002964 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002965 // NOTE: we could fall back on load/store here too for targets without
2966 // SAR. However, it is doubtful that any exist.
2967 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2968 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002969 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002970 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2971 Node->getOperand(0), ShiftCst);
2972 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2973 Result, ShiftCst);
2974 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00002975 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00002976 // EXTLOAD pair, targetting a temporary location (a stack slot).
2977
2978 // NOTE: there is a choice here between constantly creating new stack
2979 // slots and always reusing the same one. We currently always create
2980 // new ones, as reuse may inhibit scheduling.
2981 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00002982 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2983 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002984 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002985 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002986 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2987 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002988 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
2989 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00002990 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00002991 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002992 } else {
2993 assert(0 && "Unknown op");
2994 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002995 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002996 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002997 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002998 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002999 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003000
Chris Lattner4ddd2832006-04-08 04:13:17 +00003001 assert(Result.getValueType() == Op.getValueType() &&
3002 "Bad legalization!");
3003
Chris Lattner456a93a2006-01-28 07:39:30 +00003004 // Make sure that the generated code is itself legal.
3005 if (Result != Op)
3006 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003007
Chris Lattner45982da2005-05-12 16:53:42 +00003008 // Note that LegalizeOp may be reentered even from single-use nodes, which
3009 // means that we always must cache transformed nodes.
3010 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003011 return Result;
3012}
3013
Chris Lattner8b6fa222005-01-15 22:16:26 +00003014/// PromoteOp - Given an operation that produces a value in an invalid type,
3015/// promote it to compute the value into a larger type. The produced value will
3016/// have the correct bits for the low portion of the register, but no guarantee
3017/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003018SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3019 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003020 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003021 assert(getTypeAction(VT) == Promote &&
3022 "Caller should expand or legalize operands that are not promotable!");
3023 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3024 "Cannot promote to smaller type!");
3025
Chris Lattner03c85462005-01-15 05:21:40 +00003026 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003027 SDOperand Result;
3028 SDNode *Node = Op.Val;
3029
Chris Lattner6fdcb252005-09-02 20:32:45 +00003030 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3031 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003032
Chris Lattner03c85462005-01-15 05:21:40 +00003033 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003034 case ISD::CopyFromReg:
3035 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003036 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003037#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00003038 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003039#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003040 assert(0 && "Do not know how to promote this operator!");
3041 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003042 case ISD::UNDEF:
3043 Result = DAG.getNode(ISD::UNDEF, NVT);
3044 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003045 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003046 if (VT != MVT::i1)
3047 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3048 else
3049 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003050 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3051 break;
3052 case ISD::ConstantFP:
3053 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3054 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3055 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003056
Chris Lattner82fbfb62005-01-18 02:59:52 +00003057 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003058 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003059 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3060 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003061 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003062
Chris Lattner03c85462005-01-15 05:21:40 +00003063 case ISD::TRUNCATE:
3064 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3065 case Legal:
3066 Result = LegalizeOp(Node->getOperand(0));
3067 assert(Result.getValueType() >= NVT &&
3068 "This truncation doesn't make sense!");
3069 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3070 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3071 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003072 case Promote:
3073 // The truncation is not required, because we don't guarantee anything
3074 // about high bits anyway.
3075 Result = PromoteOp(Node->getOperand(0));
3076 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003077 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003078 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3079 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003080 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003081 }
3082 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003083 case ISD::SIGN_EXTEND:
3084 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003085 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003086 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3087 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3088 case Legal:
3089 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003090 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003091 break;
3092 case Promote:
3093 // Promote the reg if it's smaller.
3094 Result = PromoteOp(Node->getOperand(0));
3095 // The high bits are not guaranteed to be anything. Insert an extend.
3096 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003097 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003098 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003099 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003100 Result = DAG.getZeroExtendInReg(Result,
3101 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003102 break;
3103 }
3104 break;
Chris Lattner35481892005-12-23 00:16:34 +00003105 case ISD::BIT_CONVERT:
3106 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3107 Result = PromoteOp(Result);
3108 break;
3109
Chris Lattner8b6fa222005-01-15 22:16:26 +00003110 case ISD::FP_EXTEND:
3111 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3112 case ISD::FP_ROUND:
3113 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3114 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3115 case Promote: assert(0 && "Unreachable with 2 FP types!");
3116 case Legal:
3117 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003118 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003119 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003120 break;
3121 }
3122 break;
3123
3124 case ISD::SINT_TO_FP:
3125 case ISD::UINT_TO_FP:
3126 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3127 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003128 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003129 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003130 break;
3131
3132 case Promote:
3133 Result = PromoteOp(Node->getOperand(0));
3134 if (Node->getOpcode() == ISD::SINT_TO_FP)
3135 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003136 Result,
3137 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003138 else
Chris Lattner23993562005-04-13 02:38:47 +00003139 Result = DAG.getZeroExtendInReg(Result,
3140 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003141 // No extra round required here.
3142 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003143 break;
3144 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003145 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3146 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003147 // Round if we cannot tolerate excess precision.
3148 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003149 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3150 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003151 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003152 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003153 break;
3154
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003155 case ISD::SIGN_EXTEND_INREG:
3156 Result = PromoteOp(Node->getOperand(0));
3157 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3158 Node->getOperand(1));
3159 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003160 case ISD::FP_TO_SINT:
3161 case ISD::FP_TO_UINT:
3162 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3163 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003164 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003165 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003166 break;
3167 case Promote:
3168 // The input result is prerounded, so we don't have to do anything
3169 // special.
3170 Tmp1 = PromoteOp(Node->getOperand(0));
3171 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003172 }
Nate Begemand2558e32005-08-14 01:20:53 +00003173 // If we're promoting a UINT to a larger size, check to see if the new node
3174 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3175 // we can use that instead. This allows us to generate better code for
3176 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3177 // legal, such as PowerPC.
3178 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003179 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003180 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3181 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003182 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3183 } else {
3184 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3185 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003186 break;
3187
Chris Lattner2c8086f2005-04-02 05:00:07 +00003188 case ISD::FABS:
3189 case ISD::FNEG:
3190 Tmp1 = PromoteOp(Node->getOperand(0));
3191 assert(Tmp1.getValueType() == NVT);
3192 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3193 // NOTE: we do not have to do any extra rounding here for
3194 // NoExcessFPPrecision, because we know the input will have the appropriate
3195 // precision, and these operations don't modify precision at all.
3196 break;
3197
Chris Lattnerda6ba872005-04-28 21:44:33 +00003198 case ISD::FSQRT:
3199 case ISD::FSIN:
3200 case ISD::FCOS:
3201 Tmp1 = PromoteOp(Node->getOperand(0));
3202 assert(Tmp1.getValueType() == NVT);
3203 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003204 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003205 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3206 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003207 break;
3208
Chris Lattner03c85462005-01-15 05:21:40 +00003209 case ISD::AND:
3210 case ISD::OR:
3211 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003212 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003213 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003214 case ISD::MUL:
3215 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003216 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003217 // that too is okay if they are integer operations.
3218 Tmp1 = PromoteOp(Node->getOperand(0));
3219 Tmp2 = PromoteOp(Node->getOperand(1));
3220 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3221 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003222 break;
3223 case ISD::FADD:
3224 case ISD::FSUB:
3225 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003226 Tmp1 = PromoteOp(Node->getOperand(0));
3227 Tmp2 = PromoteOp(Node->getOperand(1));
3228 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3229 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3230
3231 // Floating point operations will give excess precision that we may not be
3232 // able to tolerate. If we DO allow excess precision, just leave it,
3233 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003234 // FIXME: Why would we need to round FP ops more than integer ones?
3235 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003236 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003237 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3238 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003239 break;
3240
Chris Lattner8b6fa222005-01-15 22:16:26 +00003241 case ISD::SDIV:
3242 case ISD::SREM:
3243 // These operators require that their input be sign extended.
3244 Tmp1 = PromoteOp(Node->getOperand(0));
3245 Tmp2 = PromoteOp(Node->getOperand(1));
3246 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003247 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3248 DAG.getValueType(VT));
3249 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3250 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003251 }
3252 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3253
3254 // Perform FP_ROUND: this is probably overly pessimistic.
3255 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003256 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3257 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003258 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003259 case ISD::FDIV:
3260 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003261 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003262 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003263 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3264 case Legal:
3265 Tmp1 = LegalizeOp(Node->getOperand(0));
3266 break;
3267 case Promote:
3268 Tmp1 = PromoteOp(Node->getOperand(0));
3269 break;
3270 case Expand:
3271 assert(0 && "not implemented");
3272 }
3273 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3274 case Legal:
3275 Tmp2 = LegalizeOp(Node->getOperand(1));
3276 break;
3277 case Promote:
3278 Tmp2 = PromoteOp(Node->getOperand(1));
3279 break;
3280 case Expand:
3281 assert(0 && "not implemented");
3282 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003283 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3284
3285 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003286 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003287 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3288 DAG.getValueType(VT));
3289 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003290
3291 case ISD::UDIV:
3292 case ISD::UREM:
3293 // These operators require that their input be zero extended.
3294 Tmp1 = PromoteOp(Node->getOperand(0));
3295 Tmp2 = PromoteOp(Node->getOperand(1));
3296 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003297 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3298 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003299 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3300 break;
3301
3302 case ISD::SHL:
3303 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003304 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003305 break;
3306 case ISD::SRA:
3307 // The input value must be properly sign extended.
3308 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003309 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3310 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003311 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003312 break;
3313 case ISD::SRL:
3314 // The input value must be properly zero extended.
3315 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003316 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003317 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003318 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003319
3320 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003321 Tmp1 = Node->getOperand(0); // Get the chain.
3322 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003323 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3324 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3325 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3326 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003327 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003328 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003329 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003330 // Increment the pointer, VAList, to the next vaarg
3331 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3332 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3333 TLI.getPointerTy()));
3334 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003335 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3336 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003337 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003338 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003339 }
3340 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003341 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003342 break;
3343
Evan Cheng466685d2006-10-09 20:57:25 +00003344 case ISD::LOAD: {
3345 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003346 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3347 ? ISD::EXTLOAD : LD->getExtensionType();
3348 Result = DAG.getExtLoad(ExtType, NVT,
3349 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003350 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003351 LD->getLoadedVT());
Chris Lattner03c85462005-01-15 05:21:40 +00003352 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003353 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003354 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003355 }
Chris Lattner03c85462005-01-15 05:21:40 +00003356 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003357 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3358 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003359 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003360 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003361 case ISD::SELECT_CC:
3362 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3363 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3364 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003365 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003366 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003367 case ISD::BSWAP:
3368 Tmp1 = Node->getOperand(0);
3369 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3370 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3371 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3372 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3373 TLI.getShiftAmountTy()));
3374 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003375 case ISD::CTPOP:
3376 case ISD::CTTZ:
3377 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003378 // Zero extend the argument
3379 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003380 // Perform the larger operation, then subtract if needed.
3381 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003382 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003383 case ISD::CTPOP:
3384 Result = Tmp1;
3385 break;
3386 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003387 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003388 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003389 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003390 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003391 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003392 break;
3393 case ISD::CTLZ:
3394 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003395 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3396 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003397 getSizeInBits(VT), NVT));
3398 break;
3399 }
3400 break;
Chris Lattner15972212006-03-31 17:55:51 +00003401 case ISD::VEXTRACT_VECTOR_ELT:
3402 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3403 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003404 case ISD::EXTRACT_VECTOR_ELT:
3405 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3406 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003407 }
3408
3409 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003410
3411 // Make sure the result is itself legal.
3412 Result = LegalizeOp(Result);
3413
3414 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003415 AddPromotedOperand(Op, Result);
3416 return Result;
3417}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003418
Chris Lattner15972212006-03-31 17:55:51 +00003419/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3420/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3421/// on the vector type. The return type of this matches the element type of the
3422/// vector, which may not be legal for the target.
3423SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3424 // We know that operand #0 is the Vec vector. If the index is a constant
3425 // or if the invec is a supported hardware type, we can use it. Otherwise,
3426 // lower to a store then an indexed load.
3427 SDOperand Vec = Op.getOperand(0);
3428 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3429
3430 SDNode *InVal = Vec.Val;
3431 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3432 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3433
3434 // Figure out if there is a Packed type corresponding to this Vector
3435 // type. If so, convert to the packed type.
3436 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3437 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3438 // Turn this into a packed extract_vector_elt operation.
3439 Vec = PackVectorOp(Vec, TVT);
3440 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3441 } else if (NumElems == 1) {
3442 // This must be an access of the only element. Return it.
3443 return PackVectorOp(Vec, EVT);
3444 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3445 SDOperand Lo, Hi;
3446 SplitVectorOp(Vec, Lo, Hi);
3447 if (CIdx->getValue() < NumElems/2) {
3448 Vec = Lo;
3449 } else {
3450 Vec = Hi;
3451 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3452 }
3453
3454 // It's now an extract from the appropriate high or low part. Recurse.
3455 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3456 return LowerVEXTRACT_VECTOR_ELT(Op);
3457 } else {
3458 // Variable index case for extract element.
3459 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3460 assert(0 && "unimp!");
3461 return SDOperand();
3462 }
3463}
3464
Chris Lattner4aab2f42006-04-02 05:06:04 +00003465/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3466/// memory traffic.
3467SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3468 SDOperand Vector = Op.getOperand(0);
3469 SDOperand Idx = Op.getOperand(1);
3470
3471 // If the target doesn't support this, store the value to a temporary
3472 // stack slot, then LOAD the scalar element back out.
3473 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003474 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003475
3476 // Add the offset to the index.
3477 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3478 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3479 DAG.getConstant(EltSize, Idx.getValueType()));
3480 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3481
Evan Cheng466685d2006-10-09 20:57:25 +00003482 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003483}
3484
Chris Lattner15972212006-03-31 17:55:51 +00003485
Nate Begeman750ac1b2006-02-01 07:19:44 +00003486/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3487/// with condition CC on the current target. This usually involves legalizing
3488/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3489/// there may be no choice but to create a new SetCC node to represent the
3490/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3491/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3492void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3493 SDOperand &RHS,
3494 SDOperand &CC) {
3495 SDOperand Tmp1, Tmp2, Result;
3496
3497 switch (getTypeAction(LHS.getValueType())) {
3498 case Legal:
3499 Tmp1 = LegalizeOp(LHS); // LHS
3500 Tmp2 = LegalizeOp(RHS); // RHS
3501 break;
3502 case Promote:
3503 Tmp1 = PromoteOp(LHS); // LHS
3504 Tmp2 = PromoteOp(RHS); // RHS
3505
3506 // If this is an FP compare, the operands have already been extended.
3507 if (MVT::isInteger(LHS.getValueType())) {
3508 MVT::ValueType VT = LHS.getValueType();
3509 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3510
3511 // Otherwise, we have to insert explicit sign or zero extends. Note
3512 // that we could insert sign extends for ALL conditions, but zero extend
3513 // is cheaper on many machines (an AND instead of two shifts), so prefer
3514 // it.
3515 switch (cast<CondCodeSDNode>(CC)->get()) {
3516 default: assert(0 && "Unknown integer comparison!");
3517 case ISD::SETEQ:
3518 case ISD::SETNE:
3519 case ISD::SETUGE:
3520 case ISD::SETUGT:
3521 case ISD::SETULE:
3522 case ISD::SETULT:
3523 // ALL of these operations will work if we either sign or zero extend
3524 // the operands (including the unsigned comparisons!). Zero extend is
3525 // usually a simpler/cheaper operation, so prefer it.
3526 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3527 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3528 break;
3529 case ISD::SETGE:
3530 case ISD::SETGT:
3531 case ISD::SETLT:
3532 case ISD::SETLE:
3533 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3534 DAG.getValueType(VT));
3535 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3536 DAG.getValueType(VT));
3537 break;
3538 }
3539 }
3540 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003541 case Expand: {
3542 MVT::ValueType VT = LHS.getValueType();
3543 if (VT == MVT::f32 || VT == MVT::f64) {
3544 // Expand into one or more soft-fp libcall(s).
3545 const char *FnName1 = NULL, *FnName2 = NULL;
Chris Lattner2d53a322006-12-15 07:36:19 +00003546 ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID;
Evan Cheng2b49c502006-12-15 02:59:56 +00003547 switch (cast<CondCodeSDNode>(CC)->get()) {
3548 case ISD::SETEQ:
3549 case ISD::SETOEQ:
3550 FnName1 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3551 CC1 = ISD::SETEQ;
3552 break;
3553 case ISD::SETNE:
3554 case ISD::SETUNE:
3555 FnName1 = (VT == MVT::f32) ? "__nesf2" : "__nedf2";
3556 CC1 = ISD::SETNE;
3557 break;
3558 case ISD::SETGE:
3559 case ISD::SETOGE:
3560 FnName1 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3561 CC1 = ISD::SETGE;
3562 break;
3563 case ISD::SETLT:
3564 case ISD::SETOLT:
3565 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3566 CC1 = ISD::SETLT;
3567 break;
3568 case ISD::SETLE:
3569 case ISD::SETOLE:
3570 FnName1 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3571 CC1 = ISD::SETLE;
3572 break;
3573 case ISD::SETGT:
3574 case ISD::SETOGT:
3575 FnName1 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3576 CC1 = ISD::SETGT;
3577 break;
3578 case ISD::SETUO:
3579 case ISD::SETO:
3580 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3581 CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO
3582 ? ISD::SETEQ : ISD::SETNE;
3583 break;
3584 default:
3585 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3586 CC1 = ISD::SETNE;
3587 switch (cast<CondCodeSDNode>(CC)->get()) {
3588 case ISD::SETONE:
3589 // SETONE = SETOLT | SETOGT
3590 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3591 CC1 = ISD::SETLT;
3592 // Fallthrough
3593 case ISD::SETUGT:
3594 FnName2 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3595 CC2 = ISD::SETGT;
3596 break;
3597 case ISD::SETUGE:
3598 FnName2 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3599 CC2 = ISD::SETGE;
3600 break;
3601 case ISD::SETULT:
3602 FnName2 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3603 CC2 = ISD::SETLT;
3604 break;
3605 case ISD::SETULE:
3606 FnName2 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3607 CC2 = ISD::SETLE;
3608 break;
3609 case ISD::SETUEQ:
3610 FnName2 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3611 CC2 = ISD::SETEQ;
3612 break;
3613 default: assert(0 && "Unsupported FP setcc!");
3614 }
3615 }
3616
3617 SDOperand Dummy;
3618 Tmp1 = ExpandLibCall(FnName1,
Reid Spencer47857812006-12-31 05:55:36 +00003619 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003620 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003621 Tmp2 = DAG.getConstant(0, MVT::i32);
3622 CC = DAG.getCondCode(CC1);
3623 if (FnName2) {
3624 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
3625 LHS = ExpandLibCall(FnName2,
Reid Spencer47857812006-12-31 05:55:36 +00003626 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003627 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003628 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
3629 DAG.getCondCode(CC2));
3630 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3631 Tmp2 = SDOperand();
3632 }
3633 LHS = Tmp1;
3634 RHS = Tmp2;
3635 return;
3636 }
3637
Nate Begeman750ac1b2006-02-01 07:19:44 +00003638 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3639 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003640 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003641 switch (cast<CondCodeSDNode>(CC)->get()) {
3642 case ISD::SETEQ:
3643 case ISD::SETNE:
3644 if (RHSLo == RHSHi)
3645 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3646 if (RHSCST->isAllOnesValue()) {
3647 // Comparison to -1.
3648 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3649 Tmp2 = RHSLo;
3650 break;
3651 }
3652
3653 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3654 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3655 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3656 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3657 break;
3658 default:
3659 // If this is a comparison of the sign bit, just look at the top part.
3660 // X > -1, x < 0
3661 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3662 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3663 CST->getValue() == 0) || // X < 0
3664 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3665 CST->isAllOnesValue())) { // X > -1
3666 Tmp1 = LHSHi;
3667 Tmp2 = RHSHi;
3668 break;
3669 }
3670
3671 // FIXME: This generated code sucks.
3672 ISD::CondCode LowCC;
3673 switch (cast<CondCodeSDNode>(CC)->get()) {
3674 default: assert(0 && "Unknown integer setcc!");
3675 case ISD::SETLT:
3676 case ISD::SETULT: LowCC = ISD::SETULT; break;
3677 case ISD::SETGT:
3678 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3679 case ISD::SETLE:
3680 case ISD::SETULE: LowCC = ISD::SETULE; break;
3681 case ISD::SETGE:
3682 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3683 }
3684
3685 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3686 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3687 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3688
3689 // NOTE: on targets without efficient SELECT of bools, we can always use
3690 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3691 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3692 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3693 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3694 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3695 Result, Tmp1, Tmp2));
3696 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003697 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003698 }
3699 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003700 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003701 LHS = Tmp1;
3702 RHS = Tmp2;
3703}
3704
Chris Lattner35481892005-12-23 00:16:34 +00003705/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003706/// The resultant code need not be legal. Note that SrcOp is the input operand
3707/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003708SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3709 SDOperand SrcOp) {
3710 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003711 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003712
3713 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003714 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003715 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003716 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003717}
3718
Chris Lattner4352cc92006-04-04 17:23:26 +00003719SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3720 // Create a vector sized/aligned stack slot, store the value to element #0,
3721 // then load the whole vector back out.
3722 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003723 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003724 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003725 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003726}
3727
3728
Chris Lattnerce872152006-03-19 06:31:19 +00003729/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3730/// support the operation, but do support the resultant packed vector type.
3731SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3732
3733 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003734 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003735 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003736 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003737 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003738 std::map<SDOperand, std::vector<unsigned> > Values;
3739 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003740 bool isConstant = true;
3741 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3742 SplatValue.getOpcode() != ISD::UNDEF)
3743 isConstant = false;
3744
Evan Cheng033e6812006-03-24 01:17:21 +00003745 for (unsigned i = 1; i < NumElems; ++i) {
3746 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003747 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003748 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003749 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003750 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003751 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003752
3753 // If this isn't a constant element or an undef, we can't use a constant
3754 // pool load.
3755 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3756 V.getOpcode() != ISD::UNDEF)
3757 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003758 }
3759
3760 if (isOnlyLowElement) {
3761 // If the low element is an undef too, then this whole things is an undef.
3762 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3763 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3764 // Otherwise, turn this into a scalar_to_vector node.
3765 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3766 Node->getOperand(0));
3767 }
3768
Chris Lattner2eb86532006-03-24 07:29:17 +00003769 // If all elements are constants, create a load from the constant pool.
3770 if (isConstant) {
3771 MVT::ValueType VT = Node->getValueType(0);
3772 const Type *OpNTy =
3773 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3774 std::vector<Constant*> CV;
3775 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3776 if (ConstantFPSDNode *V =
3777 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3778 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3779 } else if (ConstantSDNode *V =
3780 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003781 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003782 } else {
3783 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3784 CV.push_back(UndefValue::get(OpNTy));
3785 }
3786 }
3787 Constant *CP = ConstantPacked::get(CV);
3788 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003789 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003790 }
3791
Chris Lattner87100e02006-03-20 01:52:29 +00003792 if (SplatValue.Val) { // Splat of one value?
3793 // Build the shuffle constant vector: <0, 0, 0, 0>
3794 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003795 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003796 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003797 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003798 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003800
3801 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003802 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003803 // Get the splatted value into the low element of a vector register.
3804 SDOperand LowValVec =
3805 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3806
3807 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3808 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3809 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3810 SplatMask);
3811 }
3812 }
3813
Evan Cheng033e6812006-03-24 01:17:21 +00003814 // If there are only two unique elements, we may be able to turn this into a
3815 // vector shuffle.
3816 if (Values.size() == 2) {
3817 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3818 MVT::ValueType MaskVT =
3819 MVT::getIntVectorWithNumElements(NumElems);
3820 std::vector<SDOperand> MaskVec(NumElems);
3821 unsigned i = 0;
3822 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3823 E = Values.end(); I != E; ++I) {
3824 for (std::vector<unsigned>::iterator II = I->second.begin(),
3825 EE = I->second.end(); II != EE; ++II)
3826 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3827 i += NumElems;
3828 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003829 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3830 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003831
3832 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003833 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3834 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003835 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00003836 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3837 E = Values.end(); I != E; ++I) {
3838 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3839 I->first);
3840 Ops.push_back(Op);
3841 }
3842 Ops.push_back(ShuffleMask);
3843
3844 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003845 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3846 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003847 }
3848 }
Chris Lattnerce872152006-03-19 06:31:19 +00003849
3850 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3851 // aligned object on the stack, store each element into it, then load
3852 // the result as a vector.
3853 MVT::ValueType VT = Node->getValueType(0);
3854 // Create the stack frame object.
3855 SDOperand FIPtr = CreateStackTemporary(VT);
3856
3857 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003858 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003859 unsigned TypeByteSize =
3860 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00003861 // Store (in the right endianness) the elements to memory.
3862 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3863 // Ignore undef elements.
3864 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3865
Chris Lattner841c8822006-03-22 01:46:54 +00003866 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003867
3868 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3869 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3870
Evan Cheng786225a2006-10-05 23:01:46 +00003871 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003872 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00003873 }
3874
3875 SDOperand StoreChain;
3876 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003877 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3878 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003879 else
3880 StoreChain = DAG.getEntryNode();
3881
3882 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003883 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003884}
3885
3886/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3887/// specified value type.
3888SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3889 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3890 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3891 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3892 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3893}
3894
Chris Lattner5b359c62005-04-02 04:00:59 +00003895void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3896 SDOperand Op, SDOperand Amt,
3897 SDOperand &Lo, SDOperand &Hi) {
3898 // Expand the subcomponents.
3899 SDOperand LHSL, LHSH;
3900 ExpandOp(Op, LHSL, LHSH);
3901
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003902 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003903 MVT::ValueType VT = LHSL.getValueType();
3904 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00003905 Hi = Lo.getValue(1);
3906}
3907
3908
Chris Lattnere34b3962005-01-19 04:19:40 +00003909/// ExpandShift - Try to find a clever way to expand this shift operation out to
3910/// smaller elements. If we can't find a way that is more efficient than a
3911/// libcall on this target, return false. Otherwise, return true with the
3912/// low-parts expanded into Lo and Hi.
3913bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3914 SDOperand &Lo, SDOperand &Hi) {
3915 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3916 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003917
Chris Lattnere34b3962005-01-19 04:19:40 +00003918 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003919 SDOperand ShAmt = LegalizeOp(Amt);
3920 MVT::ValueType ShTy = ShAmt.getValueType();
3921 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3922 unsigned NVTBits = MVT::getSizeInBits(NVT);
3923
3924 // Handle the case when Amt is an immediate. Other cases are currently broken
3925 // and are disabled.
3926 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3927 unsigned Cst = CN->getValue();
3928 // Expand the incoming operand to be shifted, so that we have its parts
3929 SDOperand InL, InH;
3930 ExpandOp(Op, InL, InH);
3931 switch(Opc) {
3932 case ISD::SHL:
3933 if (Cst > VTBits) {
3934 Lo = DAG.getConstant(0, NVT);
3935 Hi = DAG.getConstant(0, NVT);
3936 } else if (Cst > NVTBits) {
3937 Lo = DAG.getConstant(0, NVT);
3938 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003939 } else if (Cst == NVTBits) {
3940 Lo = DAG.getConstant(0, NVT);
3941 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003942 } else {
3943 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3944 Hi = DAG.getNode(ISD::OR, NVT,
3945 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3946 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3947 }
3948 return true;
3949 case ISD::SRL:
3950 if (Cst > VTBits) {
3951 Lo = DAG.getConstant(0, NVT);
3952 Hi = DAG.getConstant(0, NVT);
3953 } else if (Cst > NVTBits) {
3954 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3955 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003956 } else if (Cst == NVTBits) {
3957 Lo = InH;
3958 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003959 } else {
3960 Lo = DAG.getNode(ISD::OR, NVT,
3961 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3962 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3963 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3964 }
3965 return true;
3966 case ISD::SRA:
3967 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003968 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003969 DAG.getConstant(NVTBits-1, ShTy));
3970 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003971 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003972 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003973 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003974 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003975 } else if (Cst == NVTBits) {
3976 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003977 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003978 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003979 } else {
3980 Lo = DAG.getNode(ISD::OR, NVT,
3981 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3982 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3983 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3984 }
3985 return true;
3986 }
3987 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00003988
3989 // Okay, the shift amount isn't constant. However, if we can tell that it is
3990 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
3991 uint64_t Mask = NVTBits, KnownZero, KnownOne;
3992 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
3993
3994 // If we know that the high bit of the shift amount is one, then we can do
3995 // this as a couple of simple shifts.
3996 if (KnownOne & Mask) {
3997 // Mask out the high bit, which we know is set.
3998 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
3999 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4000
4001 // Expand the incoming operand to be shifted, so that we have its parts
4002 SDOperand InL, InH;
4003 ExpandOp(Op, InL, InH);
4004 switch(Opc) {
4005 case ISD::SHL:
4006 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4007 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4008 return true;
4009 case ISD::SRL:
4010 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4011 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4012 return true;
4013 case ISD::SRA:
4014 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4015 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4016 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4017 return true;
4018 }
4019 }
4020
4021 // If we know that the high bit of the shift amount is zero, then we can do
4022 // this as a couple of simple shifts.
4023 if (KnownZero & Mask) {
4024 // Compute 32-amt.
4025 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4026 DAG.getConstant(NVTBits, Amt.getValueType()),
4027 Amt);
4028
4029 // Expand the incoming operand to be shifted, so that we have its parts
4030 SDOperand InL, InH;
4031 ExpandOp(Op, InL, InH);
4032 switch(Opc) {
4033 case ISD::SHL:
4034 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4035 Hi = DAG.getNode(ISD::OR, NVT,
4036 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4037 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4038 return true;
4039 case ISD::SRL:
4040 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4041 Lo = DAG.getNode(ISD::OR, NVT,
4042 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4043 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4044 return true;
4045 case ISD::SRA:
4046 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4047 Lo = DAG.getNode(ISD::OR, NVT,
4048 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4049 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4050 return true;
4051 }
4052 }
4053
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004054 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004055}
Chris Lattner77e77a62005-01-21 06:05:23 +00004056
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004057
Chris Lattner77e77a62005-01-21 06:05:23 +00004058// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4059// does not fit into a register, return the lo part and set the hi part to the
4060// by-reg argument. If it does fit into a single register, return the result
4061// and leave the Hi part unset.
4062SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004063 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004064 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4065 // The input chain to this libcall is the entry node of the function.
4066 // Legalizing the call will automatically add the previous call to the
4067 // dependence.
4068 SDOperand InChain = DAG.getEntryNode();
4069
Chris Lattner77e77a62005-01-21 06:05:23 +00004070 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004071 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004072 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4073 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4074 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004075 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4076 Entry.isSigned = isSigned;
4077 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004078 }
4079 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004080
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004081 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004082 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004083 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004084 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004085 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004086
Chris Lattner6831a812006-02-13 09:18:02 +00004087 // Legalize the call sequence, starting with the chain. This will advance
4088 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4089 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4090 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004091 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004092 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004093 default: assert(0 && "Unknown thing");
4094 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004095 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004096 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004097 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004098 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004099 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004100 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004101 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004102}
4103
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004104
Chris Lattner77e77a62005-01-21 06:05:23 +00004105/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4106/// destination type is legal.
4107SDOperand SelectionDAGLegalize::
4108ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004109 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00004110 assert(getTypeAction(Source.getValueType()) == Expand &&
4111 "This is not an expansion!");
4112 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4113
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004114 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004115 assert(Source.getValueType() == MVT::i64 &&
4116 "This only works for 64-bit -> FP");
4117 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4118 // incoming integer is set. To handle this, we dynamically test to see if
4119 // it is set, and, if so, add a fudge factor.
4120 SDOperand Lo, Hi;
4121 ExpandOp(Source, Lo, Hi);
4122
Chris Lattner66de05b2005-05-13 04:45:13 +00004123 // If this is unsigned, and not supported, first perform the conversion to
4124 // signed, then adjust the result if the sign bit is set.
4125 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4126 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4127
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004128 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4129 DAG.getConstant(0, Hi.getValueType()),
4130 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004131 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4132 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4133 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004134 uint64_t FF = 0x5f800000ULL;
4135 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004136 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004137
Chris Lattner5839bf22005-08-26 17:15:30 +00004138 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004139 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4140 SDOperand FudgeInReg;
4141 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004142 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004143 else {
4144 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00004145 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004146 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004147 }
Chris Lattner473a9902005-09-29 06:44:39 +00004148 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004149 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004150
Chris Lattnera88a2602005-05-14 05:33:54 +00004151 // Check to see if the target has a custom way to lower this. If so, use it.
4152 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4153 default: assert(0 && "This action not implemented for this operation!");
4154 case TargetLowering::Legal:
4155 case TargetLowering::Expand:
4156 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004157 case TargetLowering::Custom: {
4158 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4159 Source), DAG);
4160 if (NV.Val)
4161 return LegalizeOp(NV);
4162 break; // The target decided this was legal after all
4163 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004164 }
4165
Chris Lattner13689e22005-05-12 07:00:44 +00004166 // Expand the source, then glue it back together for the call. We must expand
4167 // the source in case it is shared (this pass of legalize must traverse it).
4168 SDOperand SrcLo, SrcHi;
4169 ExpandOp(Source, SrcLo, SrcHi);
4170 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4171
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004172 const char *FnName = 0;
4173 if (DestTy == MVT::f32)
4174 FnName = "__floatdisf";
4175 else {
4176 assert(DestTy == MVT::f64 && "Unknown fp value type!");
4177 FnName = "__floatdidf";
4178 }
Chris Lattner6831a812006-02-13 09:18:02 +00004179
4180 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4181 SDOperand UnusedHiPart;
Reid Spencer47857812006-12-31 05:55:36 +00004182 return ExpandLibCall(FnName, Source.Val, isSigned, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004183}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004184
Chris Lattner22cde6a2006-01-28 08:25:58 +00004185/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4186/// INT_TO_FP operation of the specified operand when the target requests that
4187/// we expand it. At this point, we know that the result and operand types are
4188/// legal for the target.
4189SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4190 SDOperand Op0,
4191 MVT::ValueType DestVT) {
4192 if (Op0.getValueType() == MVT::i32) {
4193 // simple 32-bit [signed|unsigned] integer to float/double expansion
4194
4195 // get the stack frame index of a 8 byte buffer
4196 MachineFunction &MF = DAG.getMachineFunction();
4197 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4198 // get address of 8 byte buffer
4199 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4200 // word offset constant for Hi/Lo address computation
4201 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4202 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004203 SDOperand Hi = StackSlot;
4204 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4205 if (TLI.isLittleEndian())
4206 std::swap(Hi, Lo);
4207
Chris Lattner22cde6a2006-01-28 08:25:58 +00004208 // if signed map to unsigned space
4209 SDOperand Op0Mapped;
4210 if (isSigned) {
4211 // constant used to invert sign bit (signed to unsigned mapping)
4212 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4213 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4214 } else {
4215 Op0Mapped = Op0;
4216 }
4217 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004218 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004219 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004220 // initial hi portion of constructed double
4221 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4222 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004223 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004224 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004225 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004226 // FP constant to bias correct the final result
4227 SDOperand Bias = DAG.getConstantFP(isSigned ?
4228 BitsToDouble(0x4330000080000000ULL)
4229 : BitsToDouble(0x4330000000000000ULL),
4230 MVT::f64);
4231 // subtract the bias
4232 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4233 // final result
4234 SDOperand Result;
4235 // handle final rounding
4236 if (DestVT == MVT::f64) {
4237 // do nothing
4238 Result = Sub;
4239 } else {
4240 // if f32 then cast to f32
4241 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4242 }
4243 return Result;
4244 }
4245 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4246 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4247
4248 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4249 DAG.getConstant(0, Op0.getValueType()),
4250 ISD::SETLT);
4251 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4252 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4253 SignSet, Four, Zero);
4254
4255 // If the sign bit of the integer is set, the large number will be treated
4256 // as a negative number. To counteract this, the dynamic code adds an
4257 // offset depending on the data type.
4258 uint64_t FF;
4259 switch (Op0.getValueType()) {
4260 default: assert(0 && "Unsupported integer type!");
4261 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4262 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4263 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4264 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4265 }
4266 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004267 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004268
4269 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4270 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4271 SDOperand FudgeInReg;
4272 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004273 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004274 else {
4275 assert(DestVT == MVT::f64 && "Unexpected conversion");
4276 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4277 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004278 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004279 }
4280
4281 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4282}
4283
4284/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4285/// *INT_TO_FP operation of the specified operand when the target requests that
4286/// we promote it. At this point, we know that the result and operand types are
4287/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4288/// operation that takes a larger input.
4289SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4290 MVT::ValueType DestVT,
4291 bool isSigned) {
4292 // First step, figure out the appropriate *INT_TO_FP operation to use.
4293 MVT::ValueType NewInTy = LegalOp.getValueType();
4294
4295 unsigned OpToUse = 0;
4296
4297 // Scan for the appropriate larger type to use.
4298 while (1) {
4299 NewInTy = (MVT::ValueType)(NewInTy+1);
4300 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4301
4302 // If the target supports SINT_TO_FP of this type, use it.
4303 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4304 default: break;
4305 case TargetLowering::Legal:
4306 if (!TLI.isTypeLegal(NewInTy))
4307 break; // Can't use this datatype.
4308 // FALL THROUGH.
4309 case TargetLowering::Custom:
4310 OpToUse = ISD::SINT_TO_FP;
4311 break;
4312 }
4313 if (OpToUse) break;
4314 if (isSigned) continue;
4315
4316 // If the target supports UINT_TO_FP of this type, use it.
4317 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4318 default: break;
4319 case TargetLowering::Legal:
4320 if (!TLI.isTypeLegal(NewInTy))
4321 break; // Can't use this datatype.
4322 // FALL THROUGH.
4323 case TargetLowering::Custom:
4324 OpToUse = ISD::UINT_TO_FP;
4325 break;
4326 }
4327 if (OpToUse) break;
4328
4329 // Otherwise, try a larger type.
4330 }
4331
4332 // Okay, we found the operation and type to use. Zero extend our input to the
4333 // desired type then run the operation on it.
4334 return DAG.getNode(OpToUse, DestVT,
4335 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4336 NewInTy, LegalOp));
4337}
4338
4339/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4340/// FP_TO_*INT operation of the specified operand when the target requests that
4341/// we promote it. At this point, we know that the result and operand types are
4342/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4343/// operation that returns a larger result.
4344SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4345 MVT::ValueType DestVT,
4346 bool isSigned) {
4347 // First step, figure out the appropriate FP_TO*INT operation to use.
4348 MVT::ValueType NewOutTy = DestVT;
4349
4350 unsigned OpToUse = 0;
4351
4352 // Scan for the appropriate larger type to use.
4353 while (1) {
4354 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4355 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4356
4357 // If the target supports FP_TO_SINT returning this type, use it.
4358 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4359 default: break;
4360 case TargetLowering::Legal:
4361 if (!TLI.isTypeLegal(NewOutTy))
4362 break; // Can't use this datatype.
4363 // FALL THROUGH.
4364 case TargetLowering::Custom:
4365 OpToUse = ISD::FP_TO_SINT;
4366 break;
4367 }
4368 if (OpToUse) break;
4369
4370 // If the target supports FP_TO_UINT of this type, use it.
4371 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4372 default: break;
4373 case TargetLowering::Legal:
4374 if (!TLI.isTypeLegal(NewOutTy))
4375 break; // Can't use this datatype.
4376 // FALL THROUGH.
4377 case TargetLowering::Custom:
4378 OpToUse = ISD::FP_TO_UINT;
4379 break;
4380 }
4381 if (OpToUse) break;
4382
4383 // Otherwise, try a larger type.
4384 }
4385
4386 // Okay, we found the operation and type to use. Truncate the result of the
4387 // extended FP_TO_*INT operation to the desired size.
4388 return DAG.getNode(ISD::TRUNCATE, DestVT,
4389 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4390}
4391
4392/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4393///
4394SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4395 MVT::ValueType VT = Op.getValueType();
4396 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4397 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4398 switch (VT) {
4399 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4400 case MVT::i16:
4401 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4402 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4403 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4404 case MVT::i32:
4405 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4406 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4407 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4408 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4409 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4410 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4411 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4412 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4413 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4414 case MVT::i64:
4415 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4416 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4417 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4418 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4419 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4420 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4421 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4422 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4423 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4424 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4425 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4426 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4427 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4428 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4429 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4430 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4431 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4432 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4433 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4434 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4435 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4436 }
4437}
4438
4439/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4440///
4441SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4442 switch (Opc) {
4443 default: assert(0 && "Cannot expand this yet!");
4444 case ISD::CTPOP: {
4445 static const uint64_t mask[6] = {
4446 0x5555555555555555ULL, 0x3333333333333333ULL,
4447 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4448 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4449 };
4450 MVT::ValueType VT = Op.getValueType();
4451 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4452 unsigned len = getSizeInBits(VT);
4453 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4454 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4455 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4456 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4457 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4458 DAG.getNode(ISD::AND, VT,
4459 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4460 }
4461 return Op;
4462 }
4463 case ISD::CTLZ: {
4464 // for now, we do this:
4465 // x = x | (x >> 1);
4466 // x = x | (x >> 2);
4467 // ...
4468 // x = x | (x >>16);
4469 // x = x | (x >>32); // for 64-bit input
4470 // return popcount(~x);
4471 //
4472 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4473 MVT::ValueType VT = Op.getValueType();
4474 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4475 unsigned len = getSizeInBits(VT);
4476 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4477 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4478 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4479 }
4480 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4481 return DAG.getNode(ISD::CTPOP, VT, Op);
4482 }
4483 case ISD::CTTZ: {
4484 // for now, we use: { return popcount(~x & (x - 1)); }
4485 // unless the target has ctlz but not ctpop, in which case we use:
4486 // { return 32 - nlz(~x & (x-1)); }
4487 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4488 MVT::ValueType VT = Op.getValueType();
4489 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4490 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4491 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4492 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4493 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4494 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4495 TLI.isOperationLegal(ISD::CTLZ, VT))
4496 return DAG.getNode(ISD::SUB, VT,
4497 DAG.getConstant(getSizeInBits(VT), VT),
4498 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4499 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4500 }
4501 }
4502}
Chris Lattnere34b3962005-01-19 04:19:40 +00004503
Chris Lattner3e928bb2005-01-07 07:47:09 +00004504/// ExpandOp - Expand the specified SDOperand into its two component pieces
4505/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4506/// LegalizeNodes map is filled in for any results that are not expanded, the
4507/// ExpandedNodes map is filled in for any results that are expanded, and the
4508/// Lo/Hi values are returned.
4509void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4510 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004511 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004512 SDNode *Node = Op.Val;
4513 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004514 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4515 VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004516 "Cannot expand to FP value or to larger int value!");
4517
Chris Lattner6fdcb252005-09-02 20:32:45 +00004518 // See if we already expanded it.
4519 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4520 = ExpandedNodes.find(Op);
4521 if (I != ExpandedNodes.end()) {
4522 Lo = I->second.first;
4523 Hi = I->second.second;
4524 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004525 }
4526
Chris Lattner3e928bb2005-01-07 07:47:09 +00004527 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004528 case ISD::CopyFromReg:
4529 assert(0 && "CopyFromReg must be legal!");
4530 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004531#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00004532 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004533#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004534 assert(0 && "Do not know how to expand this operator!");
4535 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004536 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004537 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004538 Lo = DAG.getNode(ISD::UNDEF, NVT);
4539 Hi = DAG.getNode(ISD::UNDEF, NVT);
4540 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004541 case ISD::Constant: {
4542 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4543 Lo = DAG.getConstant(Cst, NVT);
4544 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4545 break;
4546 }
Evan Cheng00495212006-12-12 21:32:44 +00004547 case ISD::ConstantFP: {
4548 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004549 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004550 if (getTypeAction(Lo.getValueType()) == Expand)
4551 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004552 break;
4553 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004554 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004555 // Return the operands.
4556 Lo = Node->getOperand(0);
4557 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004558 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004559
4560 case ISD::SIGN_EXTEND_INREG:
4561 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004562 // sext_inreg the low part if needed.
4563 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4564
4565 // The high part gets the sign extension from the lo-part. This handles
4566 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004567 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4568 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4569 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004570 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004571
Nate Begemand88fc032006-01-14 03:14:10 +00004572 case ISD::BSWAP: {
4573 ExpandOp(Node->getOperand(0), Lo, Hi);
4574 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4575 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4576 Lo = TempLo;
4577 break;
4578 }
4579
Chris Lattneredb1add2005-05-11 04:51:16 +00004580 case ISD::CTPOP:
4581 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004582 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4583 DAG.getNode(ISD::CTPOP, NVT, Lo),
4584 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004585 Hi = DAG.getConstant(0, NVT);
4586 break;
4587
Chris Lattner39a8f332005-05-12 19:05:01 +00004588 case ISD::CTLZ: {
4589 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004590 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004591 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4592 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004593 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4594 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004595 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4596 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4597
4598 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4599 Hi = DAG.getConstant(0, NVT);
4600 break;
4601 }
4602
4603 case ISD::CTTZ: {
4604 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004605 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004606 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4607 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004608 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4609 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004610 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4611 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4612
4613 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4614 Hi = DAG.getConstant(0, NVT);
4615 break;
4616 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004617
Nate Begemanacc398c2006-01-25 18:21:52 +00004618 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004619 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4620 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004621 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4622 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4623
4624 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004625 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004626 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4627 if (!TLI.isLittleEndian())
4628 std::swap(Lo, Hi);
4629 break;
4630 }
4631
Chris Lattner3e928bb2005-01-07 07:47:09 +00004632 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004633 LoadSDNode *LD = cast<LoadSDNode>(Node);
4634 SDOperand Ch = LD->getChain(); // Legalize the chain.
4635 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4636 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004637
Evan Cheng466685d2006-10-09 20:57:25 +00004638 if (ExtType == ISD::NON_EXTLOAD) {
4639 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng00495212006-12-12 21:32:44 +00004640 if (VT == MVT::f32 || VT == MVT::f64) {
4641 // f32->i32 or f64->i64 one to one expansion.
4642 // Remember that we legalized the chain.
4643 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004644 // Recursively expand the new load.
4645 if (getTypeAction(NVT) == Expand)
4646 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004647 break;
4648 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004649
Evan Cheng466685d2006-10-09 20:57:25 +00004650 // Increment the pointer to the other half.
4651 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4652 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4653 getIntPtrConstant(IncrementSize));
4654 // FIXME: This creates a bogus srcvalue!
4655 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004656
Evan Cheng466685d2006-10-09 20:57:25 +00004657 // Build a factor node to remember that this load is independent of the
4658 // other one.
4659 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4660 Hi.getValue(1));
4661
4662 // Remember that we legalized the chain.
4663 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4664 if (!TLI.isLittleEndian())
4665 std::swap(Lo, Hi);
4666 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004667 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004668
4669 if (VT == MVT::f64 && EVT == MVT::f32) {
4670 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4671 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4672 LD->getSrcValueOffset());
4673 // Remember that we legalized the chain.
4674 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4675 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4676 break;
4677 }
Evan Cheng466685d2006-10-09 20:57:25 +00004678
4679 if (EVT == NVT)
4680 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4681 LD->getSrcValueOffset());
4682 else
4683 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4684 LD->getSrcValueOffset(), EVT);
4685
4686 // Remember that we legalized the chain.
4687 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4688
4689 if (ExtType == ISD::SEXTLOAD) {
4690 // The high part is obtained by SRA'ing all but one of the bits of the
4691 // lo part.
4692 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4693 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4694 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4695 } else if (ExtType == ISD::ZEXTLOAD) {
4696 // The high part is just a zero.
4697 Hi = DAG.getConstant(0, NVT);
4698 } else /* if (ExtType == ISD::EXTLOAD) */ {
4699 // The high part is undefined.
4700 Hi = DAG.getNode(ISD::UNDEF, NVT);
4701 }
4702 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004703 break;
4704 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004705 case ISD::AND:
4706 case ISD::OR:
4707 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4708 SDOperand LL, LH, RL, RH;
4709 ExpandOp(Node->getOperand(0), LL, LH);
4710 ExpandOp(Node->getOperand(1), RL, RH);
4711 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4712 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4713 break;
4714 }
4715 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004716 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004717 ExpandOp(Node->getOperand(1), LL, LH);
4718 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004719 if (getTypeAction(NVT) == Expand)
4720 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004721 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004722 if (VT != MVT::f32)
4723 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004724 break;
4725 }
Nate Begeman9373a812005-08-10 20:51:12 +00004726 case ISD::SELECT_CC: {
4727 SDOperand TL, TH, FL, FH;
4728 ExpandOp(Node->getOperand(2), TL, TH);
4729 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004730 if (getTypeAction(NVT) == Expand)
4731 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004732 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4733 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004734 if (VT != MVT::f32)
4735 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4736 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004737 break;
4738 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004739 case ISD::ANY_EXTEND:
4740 // The low part is any extension of the input (which degenerates to a copy).
4741 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4742 // The high part is undefined.
4743 Hi = DAG.getNode(ISD::UNDEF, NVT);
4744 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004745 case ISD::SIGN_EXTEND: {
4746 // The low part is just a sign extension of the input (which degenerates to
4747 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004748 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004749
Chris Lattner3e928bb2005-01-07 07:47:09 +00004750 // The high part is obtained by SRA'ing all but one of the bits of the lo
4751 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004752 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004753 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4754 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004755 break;
4756 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004757 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004758 // The low part is just a zero extension of the input (which degenerates to
4759 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004760 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004761
Chris Lattner3e928bb2005-01-07 07:47:09 +00004762 // The high part is just a zero.
4763 Hi = DAG.getConstant(0, NVT);
4764 break;
Chris Lattner35481892005-12-23 00:16:34 +00004765
4766 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004767 SDOperand Tmp;
4768 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4769 // If the target wants to, allow it to lower this itself.
4770 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4771 case Expand: assert(0 && "cannot expand FP!");
4772 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4773 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4774 }
4775 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4776 }
4777
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004778 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00004779 if (VT == MVT::f32 || VT == MVT::f64) {
4780 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00004781 if (getTypeAction(NVT) == Expand)
4782 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00004783 break;
4784 }
4785
4786 // If source operand will be expanded to the same type as VT, i.e.
4787 // i64 <- f64, i32 <- f32, expand the source operand instead.
4788 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4789 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4790 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004791 break;
4792 }
4793
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004794 // Turn this into a load/store pair by default.
4795 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00004796 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004797
Chris Lattner35481892005-12-23 00:16:34 +00004798 ExpandOp(Tmp, Lo, Hi);
4799 break;
4800 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004801
Chris Lattner8137c9e2006-01-28 05:07:51 +00004802 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004803 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4804 TargetLowering::Custom &&
4805 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004806 Lo = TLI.LowerOperation(Op, DAG);
4807 assert(Lo.Val && "Node must be custom expanded!");
4808 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004809 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004810 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004811 break;
4812
Chris Lattner4e6c7462005-01-08 19:27:05 +00004813 // These operators cannot be expanded directly, emit them as calls to
4814 // library functions.
4815 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004816 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004817 SDOperand Op;
4818 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4819 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004820 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4821 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004822 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004823
Chris Lattnerf20d1832005-07-30 01:40:57 +00004824 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4825
Chris Lattner80a3e942005-07-29 00:33:32 +00004826 // Now that the custom expander is done, expand the result, which is still
4827 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004828 if (Op.Val) {
4829 ExpandOp(Op, Lo, Hi);
4830 break;
4831 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004832 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004833
Chris Lattner4e6c7462005-01-08 19:27:05 +00004834 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencerb47b25c2007-01-03 04:22:32 +00004835 Lo = ExpandLibCall("__fixsfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004836 else
Reid Spencerb47b25c2007-01-03 04:22:32 +00004837 Lo = ExpandLibCall("__fixdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004838 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004839
Chris Lattner4e6c7462005-01-08 19:27:05 +00004840 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004841 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004842 SDOperand Op;
4843 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4844 case Expand: assert(0 && "cannot expand FP!");
4845 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4846 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4847 }
4848
4849 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4850
4851 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004852 if (Op.Val) {
4853 ExpandOp(Op, Lo, Hi);
4854 break;
4855 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004856 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004857
Chris Lattner4e6c7462005-01-08 19:27:05 +00004858 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencerb47b25c2007-01-03 04:22:32 +00004859 Lo = ExpandLibCall("__fixunssfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004860 else
Reid Spencerb47b25c2007-01-03 04:22:32 +00004861 Lo = ExpandLibCall("__fixunsdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004862 break;
4863
Evan Cheng05a2d562006-01-09 18:31:59 +00004864 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004865 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004866 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004867 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004868 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004869 Op = TLI.LowerOperation(Op, DAG);
4870 if (Op.Val) {
4871 // Now that the custom expander is done, expand the result, which is
4872 // still VT.
4873 ExpandOp(Op, Lo, Hi);
4874 break;
4875 }
4876 }
4877
Chris Lattner79980b02006-09-13 03:50:39 +00004878 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4879 // this X << 1 as X+X.
4880 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4881 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4882 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4883 SDOperand LoOps[2], HiOps[3];
4884 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4885 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4886 LoOps[1] = LoOps[0];
4887 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4888
4889 HiOps[1] = HiOps[0];
4890 HiOps[2] = Lo.getValue(1);
4891 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4892 break;
4893 }
4894 }
4895
Chris Lattnere34b3962005-01-19 04:19:40 +00004896 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004897 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004898 break;
Chris Lattner47599822005-04-02 03:38:53 +00004899
4900 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004901 TargetLowering::LegalizeAction Action =
4902 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4903 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4904 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004905 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004906 break;
4907 }
4908
Chris Lattnere34b3962005-01-19 04:19:40 +00004909 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004910 Lo = ExpandLibCall("__ashldi3", Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004911 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004912 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004913
Evan Cheng05a2d562006-01-09 18:31:59 +00004914 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004915 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004916 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004917 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004918 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004919 Op = TLI.LowerOperation(Op, DAG);
4920 if (Op.Val) {
4921 // Now that the custom expander is done, expand the result, which is
4922 // still VT.
4923 ExpandOp(Op, Lo, Hi);
4924 break;
4925 }
4926 }
4927
Chris Lattnere34b3962005-01-19 04:19:40 +00004928 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004929 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004930 break;
Chris Lattner47599822005-04-02 03:38:53 +00004931
4932 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004933 TargetLowering::LegalizeAction Action =
4934 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4935 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4936 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004937 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004938 break;
4939 }
4940
Chris Lattnere34b3962005-01-19 04:19:40 +00004941 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004942 Lo = ExpandLibCall("__ashrdi3", Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004943 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004944 }
4945
4946 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004947 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004948 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004949 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004950 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004951 Op = TLI.LowerOperation(Op, DAG);
4952 if (Op.Val) {
4953 // Now that the custom expander is done, expand the result, which is
4954 // still VT.
4955 ExpandOp(Op, Lo, Hi);
4956 break;
4957 }
4958 }
4959
Chris Lattnere34b3962005-01-19 04:19:40 +00004960 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004961 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004962 break;
Chris Lattner47599822005-04-02 03:38:53 +00004963
4964 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004965 TargetLowering::LegalizeAction Action =
4966 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4967 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4968 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004969 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004970 break;
4971 }
4972
Chris Lattnere34b3962005-01-19 04:19:40 +00004973 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004974 Lo = ExpandLibCall("__lshrdi3", Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004975 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004976 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004977
Misha Brukmanedf128a2005-04-21 22:36:52 +00004978 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004979 case ISD::SUB: {
4980 // If the target wants to custom expand this, let them.
4981 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4982 TargetLowering::Custom) {
4983 Op = TLI.LowerOperation(Op, DAG);
4984 if (Op.Val) {
4985 ExpandOp(Op, Lo, Hi);
4986 break;
4987 }
4988 }
4989
4990 // Expand the subcomponents.
4991 SDOperand LHSL, LHSH, RHSL, RHSH;
4992 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4993 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00004994 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
4995 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004996 LoOps[0] = LHSL;
4997 LoOps[1] = RHSL;
4998 HiOps[0] = LHSH;
4999 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005000 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005001 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005002 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005003 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005004 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005005 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005006 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005007 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005008 }
Chris Lattner84f67882005-01-20 18:52:28 +00005009 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005010 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005011 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005012 // If the target wants to custom expand this, let them.
5013 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005014 SDOperand New = TLI.LowerOperation(Op, DAG);
5015 if (New.Val) {
5016 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005017 break;
5018 }
5019 }
5020
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005021 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5022 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005023 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005024 SDOperand LL, LH, RL, RH;
5025 ExpandOp(Node->getOperand(0), LL, LH);
5026 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005027 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005028 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005029 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5030 // extended the sign bit of the low half through the upper half, and if so
5031 // emit a MULHS instead of the alternate sequence that is valid for any
5032 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005033 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005034 // is RH an extension of the sign bit of RL?
5035 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5036 RH.getOperand(1).getOpcode() == ISD::Constant &&
5037 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5038 // is LH an extension of the sign bit of LL?
5039 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5040 LH.getOperand(1).getOpcode() == ISD::Constant &&
5041 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005042 // Low part:
5043 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5044 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005045 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005046 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005047 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005048 // Low part:
5049 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5050
5051 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005052 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5053 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5054 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5055 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5056 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005057 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005058 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005059 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005060
Reid Spencerb47b25c2007-01-03 04:22:32 +00005061 Lo = ExpandLibCall("__muldi3" , Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005062 break;
5063 }
Reid Spencer47857812006-12-31 05:55:36 +00005064 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, true, Hi); break;
5065 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, false, Hi); break;
5066 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, true, Hi); break;
5067 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, false, Hi); break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005068
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005069 case ISD::FADD:
Reid Spencer47857812006-12-31 05:55:36 +00005070 Lo = ExpandLibCall(((VT == MVT::f32) ? "__addsf3" : "__adddf3"), Node,
5071 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005072 break;
5073 case ISD::FSUB:
Reid Spencer47857812006-12-31 05:55:36 +00005074 Lo = ExpandLibCall(((VT == MVT::f32) ? "__subsf3" : "__subdf3"), Node,
5075 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005076 break;
5077 case ISD::FMUL:
Reid Spencer47857812006-12-31 05:55:36 +00005078 Lo = ExpandLibCall(((VT == MVT::f32) ? "__mulsf3" : "__muldf3"), Node,
5079 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005080 break;
5081 case ISD::FDIV:
Reid Spencer47857812006-12-31 05:55:36 +00005082 Lo = ExpandLibCall(((VT == MVT::f32) ? "__divsf3" : "__divdf3"), Node,
5083 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005084 break;
5085 case ISD::FP_EXTEND:
Reid Spencer47857812006-12-31 05:55:36 +00005086 Lo = ExpandLibCall("__extendsfdf2", Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005087 break;
5088 case ISD::FP_ROUND:
Reid Spencer47857812006-12-31 05:55:36 +00005089 Lo = ExpandLibCall("__truncdfsf2", Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005090 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005091 case ISD::FSQRT:
5092 case ISD::FSIN:
5093 case ISD::FCOS: {
5094 const char *FnName = 0;
5095 switch(Node->getOpcode()) {
5096 case ISD::FSQRT: FnName = (VT == MVT::f32) ? "sqrtf" : "sqrt"; break;
5097 case ISD::FSIN: FnName = (VT == MVT::f32) ? "sinf" : "sin"; break;
5098 case ISD::FCOS: FnName = (VT == MVT::f32) ? "cosf" : "cos"; break;
5099 default: assert(0 && "Unreachable!");
5100 }
Reid Spencer47857812006-12-31 05:55:36 +00005101 Lo = ExpandLibCall(FnName, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005102 break;
5103 }
Evan Cheng966bf242006-12-16 00:52:40 +00005104 case ISD::FABS: {
5105 SDOperand Mask = (VT == MVT::f64)
5106 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5107 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5108 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5109 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5110 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5111 if (getTypeAction(NVT) == Expand)
5112 ExpandOp(Lo, Lo, Hi);
5113 break;
5114 }
5115 case ISD::FNEG: {
5116 SDOperand Mask = (VT == MVT::f64)
5117 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5118 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5119 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5120 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5121 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5122 if (getTypeAction(NVT) == Expand)
5123 ExpandOp(Lo, Lo, Hi);
5124 break;
5125 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005126 case ISD::SINT_TO_FP:
5127 case ISD::UINT_TO_FP: {
5128 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5129 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
5130 const char *FnName = 0;
5131 if (Node->getOperand(0).getValueType() == MVT::i64) {
5132 if (VT == MVT::f32)
5133 FnName = isSigned ? "__floatdisf" : "__floatundisf";
5134 else
5135 FnName = isSigned ? "__floatdidf" : "__floatundidf";
5136 } else {
5137 if (VT == MVT::f32)
5138 FnName = isSigned ? "__floatsisf" : "__floatunsisf";
5139 else
5140 FnName = isSigned ? "__floatsidf" : "__floatunsidf";
5141 }
5142
5143 // Promote the operand if needed.
5144 if (getTypeAction(SrcVT) == Promote) {
5145 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5146 Tmp = isSigned
5147 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5148 DAG.getValueType(SrcVT))
5149 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5150 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5151 }
Reid Spencer47857812006-12-31 05:55:36 +00005152 Lo = ExpandLibCall(FnName, Node, isSigned, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00005153 break;
5154 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005155 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005156
Chris Lattner83397362005-12-21 18:02:52 +00005157 // Make sure the resultant values have been legalized themselves, unless this
5158 // is a type that requires multi-step expansion.
5159 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5160 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005161 if (Hi.Val)
5162 // Don't legalize the high part if it is expanded to a single node.
5163 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005164 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005165
5166 // Remember in a map if the values will be reused later.
5167 bool isNew =
5168 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5169 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005170}
5171
Chris Lattnerc7029802006-03-18 01:44:44 +00005172/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5173/// two smaller values of MVT::Vector type.
5174void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5175 SDOperand &Hi) {
5176 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5177 SDNode *Node = Op.Val;
5178 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5179 assert(NumElements > 1 && "Cannot split a single element vector!");
5180 unsigned NewNumElts = NumElements/2;
5181 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5182 SDOperand TypeNode = *(Node->op_end()-1);
5183
5184 // See if we already split it.
5185 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5186 = SplitNodes.find(Op);
5187 if (I != SplitNodes.end()) {
5188 Lo = I->second.first;
5189 Hi = I->second.second;
5190 return;
5191 }
5192
5193 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005194 default:
5195#ifndef NDEBUG
5196 Node->dump();
5197#endif
5198 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00005199 case ISD::VBUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005200 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5201 Node->op_begin()+NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005202 LoOps.push_back(NewNumEltsNode);
5203 LoOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005204 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005205
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005206 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5207 Node->op_end()-2);
Chris Lattnerc7029802006-03-18 01:44:44 +00005208 HiOps.push_back(NewNumEltsNode);
5209 HiOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005210 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005211 break;
5212 }
5213 case ISD::VADD:
5214 case ISD::VSUB:
5215 case ISD::VMUL:
5216 case ISD::VSDIV:
5217 case ISD::VUDIV:
5218 case ISD::VAND:
5219 case ISD::VOR:
5220 case ISD::VXOR: {
5221 SDOperand LL, LH, RL, RH;
5222 SplitVectorOp(Node->getOperand(0), LL, LH);
5223 SplitVectorOp(Node->getOperand(1), RL, RH);
5224
5225 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5226 NewNumEltsNode, TypeNode);
5227 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5228 NewNumEltsNode, TypeNode);
5229 break;
5230 }
5231 case ISD::VLOAD: {
5232 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5233 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5234 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5235
5236 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5237 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5238 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5239 getIntPtrConstant(IncrementSize));
5240 // FIXME: This creates a bogus srcvalue!
5241 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5242
5243 // Build a factor node to remember that this load is independent of the
5244 // other one.
5245 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5246 Hi.getValue(1));
5247
5248 // Remember that we legalized the chain.
5249 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005250 break;
5251 }
Chris Lattner7692eb42006-03-23 21:16:34 +00005252 case ISD::VBIT_CONVERT: {
5253 // We know the result is a vector. The input may be either a vector or a
5254 // scalar value.
5255 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5256 // Lower to a store/load. FIXME: this could be improved probably.
5257 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5258
Evan Cheng786225a2006-10-05 23:01:46 +00005259 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005260 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005261 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5262 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5263 SplitVectorOp(St, Lo, Hi);
5264 } else {
5265 // If the input is a vector type, we have to either scalarize it, pack it
5266 // or convert it based on whether the input vector type is legal.
5267 SDNode *InVal = Node->getOperand(0).Val;
5268 unsigned NumElems =
5269 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5270 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5271
5272 // If the input is from a single element vector, scalarize the vector,
5273 // then treat like a scalar.
5274 if (NumElems == 1) {
5275 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5276 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5277 Op.getOperand(1), Op.getOperand(2));
5278 SplitVectorOp(Scalar, Lo, Hi);
5279 } else {
5280 // Split the input vector.
5281 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5282
5283 // Convert each of the pieces now.
5284 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5285 NewNumEltsNode, TypeNode);
5286 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5287 NewNumEltsNode, TypeNode);
5288 }
5289 break;
5290 }
5291 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005292 }
5293
5294 // Remember in a map if the values will be reused later.
5295 bool isNew =
5296 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5297 assert(isNew && "Value already expanded?!?");
5298}
5299
5300
5301/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5302/// equivalent operation that returns a scalar (e.g. F32) or packed value
5303/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5304/// type for the result.
5305SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5306 MVT::ValueType NewVT) {
5307 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5308 SDNode *Node = Op.Val;
5309
5310 // See if we already packed it.
5311 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5312 if (I != PackedNodes.end()) return I->second;
5313
5314 SDOperand Result;
5315 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005316 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005317#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00005318 Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005319#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00005320 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005321 case ISD::VADD:
5322 case ISD::VSUB:
5323 case ISD::VMUL:
5324 case ISD::VSDIV:
5325 case ISD::VUDIV:
5326 case ISD::VAND:
5327 case ISD::VOR:
5328 case ISD::VXOR:
5329 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5330 NewVT,
5331 PackVectorOp(Node->getOperand(0), NewVT),
5332 PackVectorOp(Node->getOperand(1), NewVT));
5333 break;
5334 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00005335 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5336 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005337
Evan Cheng466685d2006-10-09 20:57:25 +00005338 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5339 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattnerc7029802006-03-18 01:44:44 +00005340
5341 // Remember that we legalized the chain.
5342 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5343 break;
5344 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00005345 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00005346 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005347 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00005348 Result = Node->getOperand(0);
5349 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005350 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00005351
5352 // If all elements of the build_vector are undefs, return an undef.
5353 bool AllUndef = true;
5354 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5355 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5356 AllUndef = false;
5357 break;
5358 }
5359 if (AllUndef) {
5360 Result = DAG.getNode(ISD::UNDEF, NewVT);
5361 } else {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005362 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5363 Node->getNumOperands()-2);
Chris Lattner17614ea2006-04-08 05:34:25 +00005364 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005365 }
5366 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00005367 case ISD::VINSERT_VECTOR_ELT:
5368 if (!MVT::isVector(NewVT)) {
5369 // Returning a scalar? Must be the inserted element.
5370 Result = Node->getOperand(1);
5371 } else {
5372 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5373 PackVectorOp(Node->getOperand(0), NewVT),
5374 Node->getOperand(1), Node->getOperand(2));
5375 }
5376 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00005377 case ISD::VVECTOR_SHUFFLE:
5378 if (!MVT::isVector(NewVT)) {
5379 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5380 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5381 if (cast<ConstantSDNode>(EltNum)->getValue())
5382 Result = PackVectorOp(Node->getOperand(1), NewVT);
5383 else
5384 Result = PackVectorOp(Node->getOperand(0), NewVT);
5385 } else {
5386 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5387 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5388 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5389 Node->getOperand(2).Val->op_end()-2);
5390 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005391 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5392 Node->getOperand(2).Val->op_begin(),
5393 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattner5b2316e2006-03-28 20:24:43 +00005394
5395 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5396 PackVectorOp(Node->getOperand(0), NewVT),
5397 PackVectorOp(Node->getOperand(1), NewVT), BV);
5398 }
5399 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00005400 case ISD::VBIT_CONVERT:
5401 if (Op.getOperand(0).getValueType() != MVT::Vector)
5402 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5403 else {
5404 // If the input is a vector type, we have to either scalarize it, pack it
5405 // or convert it based on whether the input vector type is legal.
5406 SDNode *InVal = Node->getOperand(0).Val;
5407 unsigned NumElems =
5408 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5409 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5410
5411 // Figure out if there is a Packed type corresponding to this Vector
5412 // type. If so, convert to the packed type.
5413 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5414 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5415 // Turn this into a bit convert of the packed input.
5416 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5417 PackVectorOp(Node->getOperand(0), TVT));
5418 break;
5419 } else if (NumElems == 1) {
5420 // Turn this into a bit convert of the scalar input.
5421 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5422 PackVectorOp(Node->getOperand(0), EVT));
5423 break;
5424 } else {
5425 // FIXME: UNIMP!
5426 assert(0 && "Cast from unsupported vector type not implemented yet!");
5427 }
5428 }
Evan Chengdb3c6262006-04-10 18:54:36 +00005429 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005430 case ISD::VSELECT:
5431 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5432 PackVectorOp(Op.getOperand(1), NewVT),
5433 PackVectorOp(Op.getOperand(2), NewVT));
5434 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005435 }
5436
5437 if (TLI.isTypeLegal(NewVT))
5438 Result = LegalizeOp(Result);
5439 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5440 assert(isNew && "Value already packed?");
5441 return Result;
5442}
5443
Chris Lattner3e928bb2005-01-07 07:47:09 +00005444
5445// SelectionDAG::Legalize - This is the entry point for the file.
5446//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005447void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005448 if (ViewLegalizeDAGs) viewGraph();
5449
Chris Lattner3e928bb2005-01-07 07:47:09 +00005450 /// run - This is the main entry point to this class.
5451 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005452 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005453}
5454