blob: 3c4252a0c468fd87c2ecada076935d8f00d42660 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Chris Lattneref598052006-04-02 03:07:27 +000024#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000028#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000029using namespace llvm;
30
Chris Lattneref598052006-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 Lattnerdc750592005-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 Lattner54a34cd2006-06-28 21:58:30 +000052class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000053 TargetLowering &TLI;
54 SelectionDAG &DAG;
55
Chris Lattner462505f2006-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 Lattnerdc750592005-01-07 07:47:09 +000068 enum LegalizeAction {
Chris Lattner2c748af2006-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 Lattneraa2372562006-05-24 17:04:05 +000071 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000072 };
Chris Lattner462505f2006-02-13 09:18:02 +000073
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +000077 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000078
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +000092 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
93
Chris Lattner32206f52006-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 Lattnerea4ca942005-01-07 22:28:47 +0000104 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +0000109 }
Chris Lattner1f2c9d82005-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 Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000115 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000116
Chris Lattnerdc750592005-01-07 07:47:09 +0000117public:
118
Chris Lattner4add7e32005-01-23 04:42:50 +0000119 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000120
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +0000125 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000134 void LegalizeDAG();
135
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000136private:
Chris Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +0000144 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-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 Lattner1f2c9d82005-01-15 05:21:40 +0000151 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000152
Chris Lattner32206f52006-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 Lattner6be79822006-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 Lattner4488f0c2006-07-26 23:55:56 +0000182 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
183 std::set<SDNode*> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000184
Nate Begeman7e7f4392006-02-01 07:19:44 +0000185 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
186
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000187 SDOperand CreateStackTemporary(MVT::ValueType VT);
188
Reid Spencere63b6512006-12-31 05:55:36 +0000189 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000190 SDOperand &Hi);
191 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
192 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000193
Chris Lattner36e663d2005-12-23 00:16:34 +0000194 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000195 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000196 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000197 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
198 SDOperand LegalOp,
199 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000200 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
201 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000202 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000204
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000205 SDOperand ExpandBSWAP(SDOperand Op);
206 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000207 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
208 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000209 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000211
Chris Lattner6f423252006-03-31 17:55:51 +0000212 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000213 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000214
Chris Lattnerdc750592005-01-07 07:47:09 +0000215 SDOperand getIntPtrConstant(uint64_t Val) {
216 return DAG.getConstant(Val, TLI.getPointerTy());
217 }
218};
219}
220
Chris Lattner6be79822006-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 Lattnerc24a1d32006-08-08 02:23:42 +0000245 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-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 Lattnerc24a1d32006-08-08 02:23:42 +0000257 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-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 Lattner32206f52006-03-18 01:44:44 +0000266/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
267/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000268static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000269 switch (VecOp) {
270 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-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 Begemanb2e089c2005-11-19 00:36:38 +0000279 }
280}
Chris Lattnerdc750592005-01-07 07:47:09 +0000281
Chris Lattner4add7e32005-01-23 04:42:50 +0000282SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
283 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
284 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000285 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000286 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000287}
288
Chris Lattner4bbbb9e2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000309
Chris Lattnerdc750592005-01-07 07:47:09 +0000310void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000311 LastCALLSEQ_END = DAG.getEntryNode();
312 IsLegalizingCall = false;
313
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000322
Chris Lattner4bbbb9e2005-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 Lattnerbf4f23322005-11-09 23:47:37 +0000327 if (I->getNumOperands() == 0) {
328 Visited[I] = 0 - 1U;
329 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000330 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000331 }
332
Chris Lattnerbf4f23322005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000336 "Error: DAG is cyclic!");
337 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000338
Chris Lattner32206f52006-03-18 01:44:44 +0000339 for (unsigned i = 0, e = Order.size(); i != e; ++i)
340 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000341
342 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000343 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000344 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
345 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000346
347 ExpandedNodes.clear();
348 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000349 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000350 SplitNodes.clear();
351 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000352
353 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000354 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000355}
356
Chris Lattner462505f2006-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 Lattner4488f0c2006-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 Lattner462505f2006-02-13 09:18:02 +0000418 if (N == Dest) return true; // N certainly leads to Dest :)
419
Chris Lattner4488f0c2006-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 Lattner462505f2006-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 Lattner4488f0c2006-07-26 23:55:56 +0000443 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000444
Chris Lattner4488f0c2006-07-26 23:55:56 +0000445 if (OperandsLeadToDest) {
446 NodesLeadingTo.insert(N);
447 return true;
448 }
Chris Lattner462505f2006-02-13 09:18:02 +0000449
450 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000451 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000452 return false;
453}
454
Chris Lattner32206f52006-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 Lattner93640542006-03-19 00:07:49 +0000485 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000486 }
487 }
488 break;
489 }
490}
Chris Lattner462505f2006-02-13 09:18:02 +0000491
Evan Cheng22cf8992006-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 Cheng3766fc602006-12-12 22:19:28 +0000495 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-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 Cheng22cf8992006-12-13 20:57:08 +0000506 if (!UseCP) {
Evan Cheng3766fc602006-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 Cheng47833a12006-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 Lattner462505f2006-02-13 09:18:02 +0000530
Evan Cheng003feb02007-01-04 21:56:39 +0000531/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
532/// operations.
533static
534SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
535 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000536 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000537 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
538 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539
Evan Cheng003feb02007-01-04 21:56:39 +0000540 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000541 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000542 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
543 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000544 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000545 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000546 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000547 // Shift right or sign-extend it if the two operands have different types.
548 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
549 if (SizeDiff > 0) {
550 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
551 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
552 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
553 } else if (SizeDiff < 0)
554 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000555
556 // Clear the sign bit of first operand.
557 SDOperand Mask2 = (VT == MVT::f64)
558 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
559 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
560 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000561 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000562 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
563
564 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000565 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
566 return Result;
567}
568
569
Chris Lattner32206f52006-03-18 01:44:44 +0000570/// LegalizeOp - We know that the specified value has a legal type.
571/// Recursively ensure that the operands have legal types, then return the
572/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000573SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000574 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000575 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000576 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000577
Chris Lattnerdc750592005-01-07 07:47:09 +0000578 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000579 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000580 if (Node->getNumValues() > 1) {
581 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000582 if (getTypeAction(Node->getValueType(i)) != Legal) {
583 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000584 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000585 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000586 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000587 }
588 }
589
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000590 // Note that LegalizeOp may be reentered even from single-use nodes, which
591 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000592 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
593 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000594
Nate Begemane5b86d72005-08-10 20:51:12 +0000595 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000596 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000597 bool isCustom = false;
598
Chris Lattnerdc750592005-01-07 07:47:09 +0000599 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000600 case ISD::FrameIndex:
601 case ISD::EntryToken:
602 case ISD::Register:
603 case ISD::BasicBlock:
604 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000605 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000606 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000607 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000608 case ISD::TargetConstantPool:
609 case ISD::TargetGlobalAddress:
610 case ISD::TargetExternalSymbol:
611 case ISD::VALUETYPE:
612 case ISD::SRCVALUE:
613 case ISD::STRING:
614 case ISD::CONDCODE:
Andrew Lenhartha6bbf332006-10-11 04:29:42 +0000615 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnereb637512006-01-28 08:31:04 +0000616 // Primitives must all be legal.
617 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
618 "This must be legal!");
619 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000620 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000621 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
622 // If this is a target node, legalize it by legalizing the operands then
623 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000624 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000625 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000626 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000627
Chris Lattner97af9d52006-08-08 01:09:31 +0000628 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000629
630 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
631 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
632 return Result.getValue(Op.ResNo);
633 }
634 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000635#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +0000636 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000637#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000638 assert(0 && "Do not know how to legalize this operator!");
639 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000640 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000641 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000642 case ISD::ConstantPool:
643 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000644 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
645 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000646 case TargetLowering::Custom:
647 Tmp1 = TLI.LowerOperation(Op, DAG);
648 if (Tmp1.Val) Result = Tmp1;
649 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000650 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000651 break;
652 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000653 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000654 case ISD::AssertSext:
655 case ISD::AssertZext:
656 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000657 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000658 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000659 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000660 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000661 Result = Node->getOperand(Op.ResNo);
662 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000663 case ISD::CopyFromReg:
664 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000665 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000666 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000667 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000668 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000669 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000670 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000671 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
673 } else {
674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
675 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000676 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
677 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000678 // Since CopyFromReg produces two values, make sure to remember that we
679 // legalized both of them.
680 AddLegalizedOperand(Op.getValue(0), Result);
681 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
682 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000683 case ISD::UNDEF: {
684 MVT::ValueType VT = Op.getValueType();
685 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000686 default: assert(0 && "This action is not supported yet!");
687 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000688 if (MVT::isInteger(VT))
689 Result = DAG.getConstant(0, VT);
690 else if (MVT::isFloatingPoint(VT))
691 Result = DAG.getConstantFP(0, VT);
692 else
693 assert(0 && "Unknown value type!");
694 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000695 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000696 break;
697 }
698 break;
699 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000700
Chris Lattnere55d1712006-03-28 00:40:33 +0000701 case ISD::INTRINSIC_W_CHAIN:
702 case ISD::INTRINSIC_WO_CHAIN:
703 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000704 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000705 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
706 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000707 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000708
709 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000710 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000711 TargetLowering::Custom) {
712 Tmp3 = TLI.LowerOperation(Result, DAG);
713 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000714 }
715
716 if (Result.Val->getNumValues() == 1) break;
717
718 // Must have return value and chain result.
719 assert(Result.Val->getNumValues() == 2 &&
720 "Cannot return more than two values!");
721
722 // Since loads produce two values, make sure to remember that we
723 // legalized both of them.
724 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
725 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
726 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000727 }
Chris Lattner435b4022005-11-29 06:21:05 +0000728
729 case ISD::LOCATION:
730 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
731 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
732
733 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
734 case TargetLowering::Promote:
735 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000736 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000737 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000738 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
739 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
740
741 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
742 const std::string &FName =
743 cast<StringSDNode>(Node->getOperand(3))->getValue();
744 const std::string &DirName =
745 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000746 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000747
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000748 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000749 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000750 SDOperand LineOp = Node->getOperand(1);
751 SDOperand ColOp = Node->getOperand(2);
752
753 if (useDEBUG_LOC) {
754 Ops.push_back(LineOp); // line #
755 Ops.push_back(ColOp); // col #
756 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000757 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000758 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000759 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
760 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000761 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
762 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000763 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000764 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000765 } else {
766 Result = Tmp1; // chain
767 }
Chris Lattner435b4022005-11-29 06:21:05 +0000768 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000769 }
Chris Lattner435b4022005-11-29 06:21:05 +0000770 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000771 if (Tmp1 != Node->getOperand(0) ||
772 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000773 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000774 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000775 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
776 Ops.push_back(Node->getOperand(1)); // line # must be legal.
777 Ops.push_back(Node->getOperand(2)); // col # must be legal.
778 } else {
779 // Otherwise promote them.
780 Ops.push_back(PromoteOp(Node->getOperand(1)));
781 Ops.push_back(PromoteOp(Node->getOperand(2)));
782 }
Chris Lattner435b4022005-11-29 06:21:05 +0000783 Ops.push_back(Node->getOperand(3)); // filename must be legal.
784 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000785 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000786 }
787 break;
788 }
789 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000790
791 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000792 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000793 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000794 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000795 case TargetLowering::Legal:
796 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
797 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
798 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
799 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000800 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000801 break;
802 }
803 break;
804
805 case ISD::DEBUG_LABEL:
806 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
807 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000808 default: assert(0 && "This action is not supported yet!");
809 case TargetLowering::Legal:
810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
811 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000813 break;
814 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000815 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000816
Chris Lattnerdc750592005-01-07 07:47:09 +0000817 case ISD::Constant:
818 // We know we don't need to expand constants here, constants only have one
819 // value and we check that it is fine above.
820
821 // FIXME: Maybe we should handle things like targets that don't support full
822 // 32-bit immediates?
823 break;
824 case ISD::ConstantFP: {
825 // Spill FP immediates to the constant pool if the target cannot directly
826 // codegen them. Targets often have some immediate values that can be
827 // efficiently generated into an FP register without a load. We explicitly
828 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000829 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
830
831 // Check to see if this FP immediate is already legal.
832 bool isLegal = false;
833 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
834 E = TLI.legal_fpimm_end(); I != E; ++I)
835 if (CFP->isExactlyValue(*I)) {
836 isLegal = true;
837 break;
838 }
839
Chris Lattner758b0ac2006-01-29 06:26:56 +0000840 // If this is a legal constant, turn it into a TargetConstantFP node.
841 if (isLegal) {
842 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
843 break;
844 }
845
846 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
847 default: assert(0 && "This action is not supported yet!");
848 case TargetLowering::Custom:
849 Tmp3 = TLI.LowerOperation(Result, DAG);
850 if (Tmp3.Val) {
851 Result = Tmp3;
852 break;
853 }
854 // FALLTHROUGH
855 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000856 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000857 }
858 break;
859 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000860 case ISD::TokenFactor:
861 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000862 Tmp1 = LegalizeOp(Node->getOperand(0));
863 Tmp2 = LegalizeOp(Node->getOperand(1));
864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
865 } else if (Node->getNumOperands() == 3) {
866 Tmp1 = LegalizeOp(Node->getOperand(0));
867 Tmp2 = LegalizeOp(Node->getOperand(1));
868 Tmp3 = LegalizeOp(Node->getOperand(2));
869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000870 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000871 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000872 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000873 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
874 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000875 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000876 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000877 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000878
879 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000880 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000881 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000882 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
883 assert(Tmp3.Val && "Target didn't custom lower this node!");
884 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
885 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000886
Chris Lattneraaa23d92006-05-16 22:53:20 +0000887 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000888 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000889 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
890 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000891 if (Op.ResNo == i)
892 Tmp2 = Tmp1;
893 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
894 }
895 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000896
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000897 case ISD::BUILD_VECTOR:
898 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000899 default: assert(0 && "This action is not supported yet!");
900 case TargetLowering::Custom:
901 Tmp3 = TLI.LowerOperation(Result, DAG);
902 if (Tmp3.Val) {
903 Result = Tmp3;
904 break;
905 }
906 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000907 case TargetLowering::Expand:
908 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000909 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000910 }
Chris Lattner29b23012006-03-19 01:17:20 +0000911 break;
912 case ISD::INSERT_VECTOR_ELT:
913 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
914 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
915 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
917
918 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
919 Node->getValueType(0))) {
920 default: assert(0 && "This action is not supported yet!");
921 case TargetLowering::Legal:
922 break;
923 case TargetLowering::Custom:
924 Tmp3 = TLI.LowerOperation(Result, DAG);
925 if (Tmp3.Val) {
926 Result = Tmp3;
927 break;
928 }
929 // FALLTHROUGH
930 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +0000931 // If the insert index is a constant, codegen this as a scalar_to_vector,
932 // then a shuffle that inserts it into the right position in the vector.
933 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
934 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
935 Tmp1.getValueType(), Tmp2);
936
937 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
938 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
939 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
940
941 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
942 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
943 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000944 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +0000945 for (unsigned i = 0; i != NumElts; ++i) {
946 if (i != InsertPos->getValue())
947 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
948 else
949 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
950 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000951 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
952 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +0000953
954 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
955 Tmp1, ScVec, ShufMask);
956 Result = LegalizeOp(Result);
957 break;
958 }
959
Chris Lattner29b23012006-03-19 01:17:20 +0000960 // If the target doesn't support this, we have to spill the input vector
961 // to a temporary stack slot, update the element, then reload it. This is
962 // badness. We could also load the value into a vector register (either
963 // with a "move to register" or "extload into register" instruction, then
964 // permute it into place, if the idx is a constant and if the idx is
965 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +0000966 MVT::ValueType VT = Tmp1.getValueType();
967 MVT::ValueType EltVT = Tmp2.getValueType();
968 MVT::ValueType IdxVT = Tmp3.getValueType();
969 MVT::ValueType PtrVT = TLI.getPointerTy();
970 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +0000971 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +0000972 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +0000973
974 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +0000975 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
976 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +0000977 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +0000978 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
979 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
980 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +0000981 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +0000982 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +0000983 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +0000984 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +0000985 break;
986 }
987 }
988 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000989 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +0000990 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
991 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
992 break;
993 }
994
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000995 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
996 Result = DAG.UpdateNodeOperands(Result, Tmp1);
997 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
998 Node->getValueType(0))) {
999 default: assert(0 && "This action is not supported yet!");
1000 case TargetLowering::Legal:
1001 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001002 case TargetLowering::Custom:
1003 Tmp3 = TLI.LowerOperation(Result, DAG);
1004 if (Tmp3.Val) {
1005 Result = Tmp3;
1006 break;
1007 }
1008 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001009 case TargetLowering::Expand:
1010 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001011 break;
1012 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001013 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001014 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001015 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1016 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1017 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1018
1019 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001020 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1021 default: assert(0 && "Unknown operation action!");
1022 case TargetLowering::Legal:
1023 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1024 "vector shuffle should not be created if not legal!");
1025 break;
1026 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001027 Tmp3 = TLI.LowerOperation(Result, DAG);
1028 if (Tmp3.Val) {
1029 Result = Tmp3;
1030 break;
1031 }
1032 // FALLTHROUGH
1033 case TargetLowering::Expand: {
1034 MVT::ValueType VT = Node->getValueType(0);
1035 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1036 MVT::ValueType PtrVT = TLI.getPointerTy();
1037 SDOperand Mask = Node->getOperand(2);
1038 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001039 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001040 for (unsigned i = 0; i != NumElems; ++i) {
1041 SDOperand Arg = Mask.getOperand(i);
1042 if (Arg.getOpcode() == ISD::UNDEF) {
1043 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1044 } else {
1045 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1046 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1047 if (Idx < NumElems)
1048 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1049 DAG.getConstant(Idx, PtrVT)));
1050 else
1051 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1052 DAG.getConstant(Idx - NumElems, PtrVT)));
1053 }
1054 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001055 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001056 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001057 }
Chris Lattner6be79822006-04-04 17:23:26 +00001058 case TargetLowering::Promote: {
1059 // Change base type to a different vector type.
1060 MVT::ValueType OVT = Node->getValueType(0);
1061 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1062
1063 // Cast the two input vectors.
1064 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1065 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1066
1067 // Convert the shuffle mask to the right # elements.
1068 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1069 assert(Tmp3.Val && "Shuffle not legal?");
1070 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1071 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1072 break;
1073 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001074 }
1075 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001076
1077 case ISD::EXTRACT_VECTOR_ELT:
1078 Tmp1 = LegalizeOp(Node->getOperand(0));
1079 Tmp2 = LegalizeOp(Node->getOperand(1));
1080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +00001081
1082 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1083 Tmp1.getValueType())) {
1084 default: assert(0 && "This action is not supported yet!");
1085 case TargetLowering::Legal:
1086 break;
1087 case TargetLowering::Custom:
1088 Tmp3 = TLI.LowerOperation(Result, DAG);
1089 if (Tmp3.Val) {
1090 Result = Tmp3;
1091 break;
1092 }
1093 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001094 case TargetLowering::Expand:
1095 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001096 break;
1097 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001098 break;
1099
Chris Lattner6f423252006-03-31 17:55:51 +00001100 case ISD::VEXTRACT_VECTOR_ELT:
1101 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001102 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001103
Chris Lattner462505f2006-02-13 09:18:02 +00001104 case ISD::CALLSEQ_START: {
1105 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1106
1107 // Recursively Legalize all of the inputs of the call end that do not lead
1108 // to this call start. This ensures that any libcalls that need be inserted
1109 // are inserted *before* the CALLSEQ_START.
Chris Lattner4488f0c2006-07-26 23:55:56 +00001110 {std::set<SDNode*> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001111 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001112 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1113 NodesLeadingTo);
1114 }
Chris Lattner462505f2006-02-13 09:18:02 +00001115
1116 // Now that we legalized all of the inputs (which may have inserted
1117 // libcalls) create the new CALLSEQ_START node.
1118 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1119
1120 // Merge in the last call, to ensure that this call start after the last
1121 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001122 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001123 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1124 Tmp1 = LegalizeOp(Tmp1);
1125 }
Chris Lattner462505f2006-02-13 09:18:02 +00001126
1127 // Do not try to legalize the target-specific arguments (#1+).
1128 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001129 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001130 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001131 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001132 }
1133
1134 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001135 AddLegalizedOperand(Op.getValue(0), Result);
1136 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1137 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1138
Chris Lattner462505f2006-02-13 09:18:02 +00001139 // Now that the callseq_start and all of the non-call nodes above this call
1140 // sequence have been legalized, legalize the call itself. During this
1141 // process, no libcalls can/will be inserted, guaranteeing that no calls
1142 // can overlap.
1143 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1144 SDOperand InCallSEQ = LastCALLSEQ_END;
1145 // Note that we are selecting this call!
1146 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1147 IsLegalizingCall = true;
1148
1149 // Legalize the call, starting from the CALLSEQ_END.
1150 LegalizeOp(LastCALLSEQ_END);
1151 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1152 return Result;
1153 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001154 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001155 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1156 // will cause this node to be legalized as well as handling libcalls right.
1157 if (LastCALLSEQ_END.Val != Node) {
1158 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1159 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1160 assert(I != LegalizedNodes.end() &&
1161 "Legalizing the call start should have legalized this node!");
1162 return I->second;
1163 }
1164
1165 // Otherwise, the call start has been legalized and everything is going
1166 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001167 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001168 // Do not try to legalize the target-specific arguments (#1+), except for
1169 // an optional flag input.
1170 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1171 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001172 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001173 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001174 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001175 }
1176 } else {
1177 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1178 if (Tmp1 != Node->getOperand(0) ||
1179 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001180 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001181 Ops[0] = Tmp1;
1182 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001183 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001184 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001185 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001186 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001187 // This finishes up call legalization.
1188 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001189
1190 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1191 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1192 if (Node->getNumValues() == 2)
1193 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1194 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001195 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1197 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1198 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001200
Chris Lattnerd02b0542006-01-28 10:58:55 +00001201 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001202 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001203 switch (TLI.getOperationAction(Node->getOpcode(),
1204 Node->getValueType(0))) {
1205 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001206 case TargetLowering::Expand: {
1207 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1208 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1209 " not tell us which reg is the stack pointer!");
1210 SDOperand Chain = Tmp1.getOperand(0);
1211 SDOperand Size = Tmp2.getOperand(1);
1212 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1213 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1214 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001215 Tmp1 = LegalizeOp(Tmp1);
1216 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001217 break;
1218 }
1219 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001220 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1221 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001222 Tmp1 = LegalizeOp(Tmp3);
1223 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001224 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001225 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001226 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001227 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001228 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001229 // Since this op produce two values, make sure to remember that we
1230 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001231 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1232 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001233 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001234 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001235 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001236 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001237 bool Changed = false;
1238 // Legalize all of the operands of the inline asm, in case they are nodes
1239 // that need to be expanded or something. Note we skip the asm string and
1240 // all of the TargetConstant flags.
1241 SDOperand Op = LegalizeOp(Ops[0]);
1242 Changed = Op != Ops[0];
1243 Ops[0] = Op;
1244
1245 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1246 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1247 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1248 for (++i; NumVals; ++i, --NumVals) {
1249 SDOperand Op = LegalizeOp(Ops[i]);
1250 if (Op != Ops[i]) {
1251 Changed = true;
1252 Ops[i] = Op;
1253 }
1254 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001255 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001256
1257 if (HasInFlag) {
1258 Op = LegalizeOp(Ops.back());
1259 Changed |= Op != Ops.back();
1260 Ops.back() = Op;
1261 }
1262
1263 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001264 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001265
1266 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001267 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001268 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1269 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001270 }
Chris Lattner68a12142005-01-07 22:12:08 +00001271 case ISD::BR:
1272 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001273 // Ensure that libcalls are emitted before a branch.
1274 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1275 Tmp1 = LegalizeOp(Tmp1);
1276 LastCALLSEQ_END = DAG.getEntryNode();
1277
Chris Lattnerd02b0542006-01-28 10:58:55 +00001278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001279 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001280 case ISD::BRIND:
1281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282 // Ensure that libcalls are emitted before a branch.
1283 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1284 Tmp1 = LegalizeOp(Tmp1);
1285 LastCALLSEQ_END = DAG.getEntryNode();
1286
1287 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1288 default: assert(0 && "Indirect target must be legal type (pointer)!");
1289 case Legal:
1290 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1291 break;
1292 }
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1294 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001295 case ISD::BR_JT:
1296 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1297 // Ensure that libcalls are emitted before a branch.
1298 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1299 Tmp1 = LegalizeOp(Tmp1);
1300 LastCALLSEQ_END = DAG.getEntryNode();
1301
1302 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1304
1305 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1306 default: assert(0 && "This action is not supported yet!");
1307 case TargetLowering::Legal: break;
1308 case TargetLowering::Custom:
1309 Tmp1 = TLI.LowerOperation(Result, DAG);
1310 if (Tmp1.Val) Result = Tmp1;
1311 break;
1312 case TargetLowering::Expand: {
1313 SDOperand Chain = Result.getOperand(0);
1314 SDOperand Table = Result.getOperand(1);
1315 SDOperand Index = Result.getOperand(2);
1316
1317 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001318 MachineFunction &MF = DAG.getMachineFunction();
1319 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001320 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1321 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001322
1323 SDOperand LD;
1324 switch (EntrySize) {
1325 default: assert(0 && "Size of jump table not supported yet."); break;
1326 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1327 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1328 }
1329
1330 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001331 // For PIC, the sequence is:
1332 // BRIND(load(Jumptable + index) + RelocBase)
1333 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1334 SDOperand Reloc;
1335 if (TLI.usesGlobalOffsetTable())
1336 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1337 else
1338 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001339 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001340 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1341 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1342 } else {
1343 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1344 }
1345 }
1346 }
1347 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001348 case ISD::BRCOND:
1349 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001350 // Ensure that libcalls are emitted before a return.
1351 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1352 Tmp1 = LegalizeOp(Tmp1);
1353 LastCALLSEQ_END = DAG.getEntryNode();
1354
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001355 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1356 case Expand: assert(0 && "It's impossible to expand bools");
1357 case Legal:
1358 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1359 break;
1360 case Promote:
1361 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001362
1363 // The top bits of the promoted condition are not necessarily zero, ensure
1364 // that the value is properly zero extended.
1365 if (!TLI.MaskedValueIsZero(Tmp2,
1366 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1367 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001368 break;
1369 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001370
1371 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001372 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001373
1374 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1375 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001376 case TargetLowering::Legal: break;
1377 case TargetLowering::Custom:
1378 Tmp1 = TLI.LowerOperation(Result, DAG);
1379 if (Tmp1.Val) Result = Tmp1;
1380 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001381 case TargetLowering::Expand:
1382 // Expand brcond's setcc into its constituent parts and create a BR_CC
1383 // Node.
1384 if (Tmp2.getOpcode() == ISD::SETCC) {
1385 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1386 Tmp2.getOperand(0), Tmp2.getOperand(1),
1387 Node->getOperand(2));
1388 } else {
1389 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1390 DAG.getCondCode(ISD::SETNE), Tmp2,
1391 DAG.getConstant(0, Tmp2.getValueType()),
1392 Node->getOperand(2));
1393 }
1394 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001395 }
1396 break;
1397 case ISD::BR_CC:
1398 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001399 // Ensure that libcalls are emitted before a branch.
1400 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1401 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001402 Tmp2 = Node->getOperand(2); // LHS
1403 Tmp3 = Node->getOperand(3); // RHS
1404 Tmp4 = Node->getOperand(1); // CC
1405
1406 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001407 LastCALLSEQ_END = DAG.getEntryNode();
1408
Nate Begeman7e7f4392006-02-01 07:19:44 +00001409 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1410 // the LHS is a legal SETCC itself. In this case, we need to compare
1411 // the result against zero to select between true and false values.
1412 if (Tmp3.Val == 0) {
1413 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1414 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001415 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001416
1417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1418 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001419
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001420 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1421 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001422 case TargetLowering::Legal: break;
1423 case TargetLowering::Custom:
1424 Tmp4 = TLI.LowerOperation(Result, DAG);
1425 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001426 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001427 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001428 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001429 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001430 LoadSDNode *LD = cast<LoadSDNode>(Node);
1431 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1432 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001433
Evan Chenge71fe34d2006-10-09 20:57:25 +00001434 ISD::LoadExtType ExtType = LD->getExtensionType();
1435 if (ExtType == ISD::NON_EXTLOAD) {
1436 MVT::ValueType VT = Node->getValueType(0);
1437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1438 Tmp3 = Result.getValue(0);
1439 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001440
Evan Chenge71fe34d2006-10-09 20:57:25 +00001441 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1442 default: assert(0 && "This action is not supported yet!");
1443 case TargetLowering::Legal: break;
1444 case TargetLowering::Custom:
1445 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1446 if (Tmp1.Val) {
1447 Tmp3 = LegalizeOp(Tmp1);
1448 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001449 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001450 break;
1451 case TargetLowering::Promote: {
1452 // Only promote a load of vector type to another.
1453 assert(MVT::isVector(VT) && "Cannot promote this load!");
1454 // Change base type to a different vector type.
1455 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1456
1457 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1458 LD->getSrcValueOffset());
1459 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1460 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001461 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001462 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001463 }
1464 // Since loads produce two values, make sure to remember that we
1465 // legalized both of them.
1466 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1467 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1468 return Op.ResNo ? Tmp4 : Tmp3;
1469 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001470 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001471 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1472 default: assert(0 && "This action is not supported yet!");
1473 case TargetLowering::Promote:
1474 assert(SrcVT == MVT::i1 &&
1475 "Can only promote extending LOAD from i1 -> i8!");
1476 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1477 LD->getSrcValue(), LD->getSrcValueOffset(),
1478 MVT::i8);
1479 Tmp1 = Result.getValue(0);
1480 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001481 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001482 case TargetLowering::Custom:
1483 isCustom = true;
1484 // FALLTHROUGH
1485 case TargetLowering::Legal:
1486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1487 Tmp1 = Result.getValue(0);
1488 Tmp2 = Result.getValue(1);
1489
1490 if (isCustom) {
1491 Tmp3 = TLI.LowerOperation(Result, DAG);
1492 if (Tmp3.Val) {
1493 Tmp1 = LegalizeOp(Tmp3);
1494 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1495 }
1496 }
1497 break;
1498 case TargetLowering::Expand:
1499 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1500 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1501 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1502 LD->getSrcValueOffset());
1503 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1504 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1505 Tmp2 = LegalizeOp(Load.getValue(1));
1506 break;
1507 }
1508 assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!");
1509 // Turn the unsupported load into an EXTLOAD followed by an explicit
1510 // zero/sign extend inreg.
1511 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1512 Tmp1, Tmp2, LD->getSrcValue(),
1513 LD->getSrcValueOffset(), SrcVT);
1514 SDOperand ValRes;
1515 if (ExtType == ISD::SEXTLOAD)
1516 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1517 Result, DAG.getValueType(SrcVT));
1518 else
1519 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1520 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1521 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1522 break;
1523 }
1524 // Since loads produce two values, make sure to remember that we legalized
1525 // both of them.
1526 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1527 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1528 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001529 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001530 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001531 case ISD::EXTRACT_ELEMENT: {
1532 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1533 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001534 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001535 case Legal:
1536 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1537 // 1 -> Hi
1538 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1539 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1540 TLI.getShiftAmountTy()));
1541 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1542 } else {
1543 // 0 -> Lo
1544 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1545 Node->getOperand(0));
1546 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001547 break;
1548 case Expand:
1549 // Get both the low and high parts.
1550 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1551 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1552 Result = Tmp2; // 1 -> Hi
1553 else
1554 Result = Tmp1; // 0 -> Lo
1555 break;
1556 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001557 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001558 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001559
1560 case ISD::CopyToReg:
1561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001562
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001563 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001564 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001565 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001566 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001567 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001569 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001570 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001571 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001572 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1574 Tmp3);
1575 } else {
1576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001577 }
1578
1579 // Since this produces two values, make sure to remember that we legalized
1580 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001581 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001582 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001583 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001584 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001585 break;
1586
1587 case ISD::RET:
1588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001589
1590 // Ensure that libcalls are emitted before a return.
1591 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1592 Tmp1 = LegalizeOp(Tmp1);
1593 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001594
Chris Lattnerdc750592005-01-07 07:47:09 +00001595 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001596 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001597 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001598 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001599 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001600 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001601 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001602 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001603 case Expand:
1604 if (Tmp2.getValueType() != MVT::Vector) {
1605 SDOperand Lo, Hi;
1606 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng3432ab92006-12-11 19:27:14 +00001607 if (Hi.Val)
1608 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1609 else
1610 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001611 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001612 } else {
1613 SDNode *InVal = Tmp2.Val;
1614 unsigned NumElems =
1615 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1616 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1617
1618 // Figure out if there is a Packed type corresponding to this Vector
1619 // type. If so, convert to the packed type.
1620 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1621 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1622 // Turn this into a return of the packed type.
1623 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001625 } else if (NumElems == 1) {
1626 // Turn this into a return of the scalar type.
1627 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001628 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001629
1630 // FIXME: Returns of gcc generic vectors smaller than a legal type
1631 // should be returned in integer registers!
1632
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001633 // The scalarized value type may not be legal, e.g. it might require
1634 // promotion or expansion. Relegalize the return.
1635 Result = LegalizeOp(Result);
1636 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001637 // FIXME: Returns of gcc generic vectors larger than a legal vector
1638 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001639 SDOperand Lo, Hi;
1640 SplitVectorOp(Tmp2, Lo, Hi);
Evan Chenga2e99532006-05-26 23:09:09 +00001641 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001642 Result = LegalizeOp(Result);
1643 }
1644 }
Misha Brukman835702a2005-04-21 22:36:52 +00001645 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001646 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001647 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001649 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001650 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001651 }
1652 break;
1653 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001654 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001655 break;
1656 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001657 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001658 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001659 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001660 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1661 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001662 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001663 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001664 break;
1665 case Expand: {
1666 SDOperand Lo, Hi;
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001667 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1668 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001669 ExpandOp(Node->getOperand(i), Lo, Hi);
1670 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001671 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001672 if (Hi.Val) {
1673 NewValues.push_back(Hi);
1674 NewValues.push_back(Node->getOperand(i+1));
1675 }
Misha Brukman835702a2005-04-21 22:36:52 +00001676 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001677 }
1678 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001679 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001680 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001681
1682 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001683 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001684 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001685 Result = DAG.getNode(ISD::RET, MVT::Other,
1686 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001687 break;
1688 }
1689 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001690
Chris Lattner4d1ea712006-01-29 21:02:23 +00001691 if (Result.getOpcode() == ISD::RET) {
1692 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1693 default: assert(0 && "This action is not supported yet!");
1694 case TargetLowering::Legal: break;
1695 case TargetLowering::Custom:
1696 Tmp1 = TLI.LowerOperation(Result, DAG);
1697 if (Tmp1.Val) Result = Tmp1;
1698 break;
1699 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001700 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001701 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001702 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001703 StoreSDNode *ST = cast<StoreSDNode>(Node);
1704 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1705 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattnerdc750592005-01-07 07:47:09 +00001706
Evan Chengab51cf22006-10-13 21:14:26 +00001707 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001708 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1709 // FIXME: We shouldn't do this for TargetConstantFP's.
1710 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1711 // to phase ordering between legalized code and the dag combiner. This
1712 // probably means that we need to integrate dag combiner and legalizer
1713 // together.
1714 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1715 if (CFP->getValueType(0) == MVT::f32) {
1716 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1717 } else {
1718 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1719 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1720 }
1721 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1722 ST->getSrcValueOffset());
1723 break;
1724 }
1725
Evan Chengab51cf22006-10-13 21:14:26 +00001726 switch (getTypeAction(ST->getStoredVT())) {
1727 case Legal: {
1728 Tmp3 = LegalizeOp(ST->getValue());
1729 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1730 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001731
Evan Chengab51cf22006-10-13 21:14:26 +00001732 MVT::ValueType VT = Tmp3.getValueType();
1733 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1734 default: assert(0 && "This action is not supported yet!");
1735 case TargetLowering::Legal: break;
1736 case TargetLowering::Custom:
1737 Tmp1 = TLI.LowerOperation(Result, DAG);
1738 if (Tmp1.Val) Result = Tmp1;
1739 break;
1740 case TargetLowering::Promote:
1741 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1742 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1743 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1744 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1745 ST->getSrcValue(), ST->getSrcValueOffset());
1746 break;
1747 }
1748 break;
1749 }
1750 case Promote:
1751 // Truncate the value and store the result.
1752 Tmp3 = PromoteOp(ST->getValue());
1753 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1754 ST->getSrcValueOffset(), ST->getStoredVT());
1755 break;
1756
1757 case Expand:
1758 unsigned IncrementSize = 0;
1759 SDOperand Lo, Hi;
1760
1761 // If this is a vector type, then we have to calculate the increment as
1762 // the product of the element size in bytes, and the number of elements
1763 // in the high half of the vector.
1764 if (ST->getValue().getValueType() == MVT::Vector) {
1765 SDNode *InVal = ST->getValue().Val;
1766 unsigned NumElems =
1767 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1768 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1769
1770 // Figure out if there is a Packed type corresponding to this Vector
1771 // type. If so, convert to the packed type.
1772 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1773 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1774 // Turn this into a normal store of the packed type.
1775 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1776 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1777 ST->getSrcValueOffset());
1778 Result = LegalizeOp(Result);
1779 break;
1780 } else if (NumElems == 1) {
1781 // Turn this into a normal store of the scalar type.
1782 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1783 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1784 ST->getSrcValueOffset());
1785 // The scalarized value type may not be legal, e.g. it might require
1786 // promotion or expansion. Relegalize the scalar store.
1787 Result = LegalizeOp(Result);
1788 break;
1789 } else {
1790 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1791 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1792 }
1793 } else {
1794 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001795 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001796
1797 if (!TLI.isLittleEndian())
1798 std::swap(Lo, Hi);
1799 }
1800
1801 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1802 ST->getSrcValueOffset());
Evan Cheng0076ca02006-12-12 19:53:13 +00001803
1804 if (Hi.Val == NULL) {
1805 // Must be int <-> float one-to-one expansion.
1806 Result = Lo;
1807 break;
1808 }
1809
Evan Chengab51cf22006-10-13 21:14:26 +00001810 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1811 getIntPtrConstant(IncrementSize));
1812 assert(isTypeLegal(Tmp2.getValueType()) &&
1813 "Pointers must be legal!");
1814 // FIXME: This sets the srcvalue of both halves to be the same, which is
1815 // wrong.
1816 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1817 ST->getSrcValueOffset());
1818 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1819 break;
1820 }
1821 } else {
1822 // Truncating store
1823 assert(isTypeLegal(ST->getValue().getValueType()) &&
1824 "Cannot handle illegal TRUNCSTORE yet!");
1825 Tmp3 = LegalizeOp(ST->getValue());
1826
1827 // The only promote case we handle is TRUNCSTORE:i1 X into
1828 // -> TRUNCSTORE:i8 (and X, 1)
1829 if (ST->getStoredVT() == MVT::i1 &&
1830 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1831 // Promote the bool to a mask then store.
1832 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1833 DAG.getConstant(1, Tmp3.getValueType()));
1834 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1835 ST->getSrcValueOffset(), MVT::i8);
1836 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1837 Tmp2 != ST->getBasePtr()) {
1838 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1839 ST->getOffset());
1840 }
1841
1842 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1843 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001844 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001845 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001846 case TargetLowering::Custom:
1847 Tmp1 = TLI.LowerOperation(Result, DAG);
1848 if (Tmp1.Val) Result = Tmp1;
1849 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001850 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001851 }
1852 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001853 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001854 case ISD::PCMARKER:
1855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001856 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001857 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001858 case ISD::STACKSAVE:
1859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1861 Tmp1 = Result.getValue(0);
1862 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001863
Chris Lattnerb3266452006-01-13 02:50:02 +00001864 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1865 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001866 case TargetLowering::Legal: break;
1867 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001868 Tmp3 = TLI.LowerOperation(Result, DAG);
1869 if (Tmp3.Val) {
1870 Tmp1 = LegalizeOp(Tmp3);
1871 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001872 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001873 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001874 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001875 // Expand to CopyFromReg if the target set
1876 // StackPointerRegisterToSaveRestore.
1877 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001878 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001879 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001880 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001881 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001882 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1883 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001884 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001885 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001886 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001887
1888 // Since stacksave produce two values, make sure to remember that we
1889 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001890 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1891 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1892 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001893
Chris Lattnerb3266452006-01-13 02:50:02 +00001894 case ISD::STACKRESTORE:
1895 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1896 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001897 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001898
1899 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1900 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001901 case TargetLowering::Legal: break;
1902 case TargetLowering::Custom:
1903 Tmp1 = TLI.LowerOperation(Result, DAG);
1904 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001905 break;
1906 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001907 // Expand to CopyToReg if the target set
1908 // StackPointerRegisterToSaveRestore.
1909 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1910 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1911 } else {
1912 Result = Tmp1;
1913 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001914 break;
1915 }
1916 break;
1917
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001918 case ISD::READCYCLECOUNTER:
1919 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001921 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1922 Node->getValueType(0))) {
1923 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001924 case TargetLowering::Legal:
1925 Tmp1 = Result.getValue(0);
1926 Tmp2 = Result.getValue(1);
1927 break;
Evan Cheng69739932006-11-29 08:26:18 +00001928 case TargetLowering::Custom:
1929 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00001930 Tmp1 = LegalizeOp(Result.getValue(0));
1931 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00001932 break;
1933 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001934
1935 // Since rdcc produce two values, make sure to remember that we legalized
1936 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00001937 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1938 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001939 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001940
Chris Lattner39c67442005-01-14 22:08:15 +00001941 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1943 case Expand: assert(0 && "It's impossible to expand bools");
1944 case Legal:
1945 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1946 break;
1947 case Promote:
1948 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00001949 // Make sure the condition is either zero or one.
1950 if (!TLI.MaskedValueIsZero(Tmp1,
1951 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1952 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001953 break;
1954 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001955 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001956 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001957
Chris Lattnerd02b0542006-01-28 10:58:55 +00001958 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001959
Nate Begeman987121a2005-08-23 04:29:48 +00001960 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001961 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001962 case TargetLowering::Legal: break;
1963 case TargetLowering::Custom: {
1964 Tmp1 = TLI.LowerOperation(Result, DAG);
1965 if (Tmp1.Val) Result = Tmp1;
1966 break;
1967 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001968 case TargetLowering::Expand:
1969 if (Tmp1.getOpcode() == ISD::SETCC) {
1970 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1971 Tmp2, Tmp3,
1972 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1973 } else {
1974 Result = DAG.getSelectCC(Tmp1,
1975 DAG.getConstant(0, Tmp1.getValueType()),
1976 Tmp2, Tmp3, ISD::SETNE);
1977 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001978 break;
1979 case TargetLowering::Promote: {
1980 MVT::ValueType NVT =
1981 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1982 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00001983 if (MVT::isVector(Tmp2.getValueType())) {
1984 ExtOp = ISD::BIT_CONVERT;
1985 TruncOp = ISD::BIT_CONVERT;
1986 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001987 ExtOp = ISD::ANY_EXTEND;
1988 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001989 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001990 ExtOp = ISD::FP_EXTEND;
1991 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001992 }
1993 // Promote each of the values to the new type.
1994 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1995 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1996 // Perform the larger operation, then round down.
1997 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1998 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1999 break;
2000 }
2001 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002002 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002003 case ISD::SELECT_CC: {
2004 Tmp1 = Node->getOperand(0); // LHS
2005 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002006 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2007 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002008 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002009
Nate Begeman7e7f4392006-02-01 07:19:44 +00002010 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2011
2012 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2013 // the LHS is a legal SETCC itself. In this case, we need to compare
2014 // the result against zero to select between true and false values.
2015 if (Tmp2.Val == 0) {
2016 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2017 CC = DAG.getCondCode(ISD::SETNE);
2018 }
2019 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2020
2021 // Everything is legal, see if we should expand this op or something.
2022 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2023 default: assert(0 && "This action is not supported yet!");
2024 case TargetLowering::Legal: break;
2025 case TargetLowering::Custom:
2026 Tmp1 = TLI.LowerOperation(Result, DAG);
2027 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002028 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002029 }
2030 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002031 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002032 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002033 Tmp1 = Node->getOperand(0);
2034 Tmp2 = Node->getOperand(1);
2035 Tmp3 = Node->getOperand(2);
2036 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2037
2038 // If we had to Expand the SetCC operands into a SELECT node, then it may
2039 // not always be possible to return a true LHS & RHS. In this case, just
2040 // return the value we legalized, returned in the LHS
2041 if (Tmp2.Val == 0) {
2042 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002043 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002044 }
Nate Begeman987121a2005-08-23 04:29:48 +00002045
Chris Lattnerf263a232006-01-30 22:43:50 +00002046 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002047 default: assert(0 && "Cannot handle this action for SETCC yet!");
2048 case TargetLowering::Custom:
2049 isCustom = true;
2050 // FALLTHROUGH.
2051 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002053 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002054 Tmp4 = TLI.LowerOperation(Result, DAG);
2055 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002056 }
Nate Begeman987121a2005-08-23 04:29:48 +00002057 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002058 case TargetLowering::Promote: {
2059 // First step, figure out the appropriate operation to use.
2060 // Allow SETCC to not be supported for all legal data types
2061 // Mostly this targets FP
2062 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2063 MVT::ValueType OldVT = NewInTy;
2064
2065 // Scan for the appropriate larger type to use.
2066 while (1) {
2067 NewInTy = (MVT::ValueType)(NewInTy+1);
2068
2069 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2070 "Fell off of the edge of the integer world");
2071 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2072 "Fell off of the edge of the floating point world");
2073
2074 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002075 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002076 break;
2077 }
2078 if (MVT::isInteger(NewInTy))
2079 assert(0 && "Cannot promote Legal Integer SETCC yet");
2080 else {
2081 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2082 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2083 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002084 Tmp1 = LegalizeOp(Tmp1);
2085 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002086 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002087 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002088 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002089 }
Nate Begeman987121a2005-08-23 04:29:48 +00002090 case TargetLowering::Expand:
2091 // Expand a setcc node into a select_cc of the same condition, lhs, and
2092 // rhs that selects between const 1 (true) and const 0 (false).
2093 MVT::ValueType VT = Node->getValueType(0);
2094 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2095 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002096 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002097 break;
2098 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002099 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002100 case ISD::MEMSET:
2101 case ISD::MEMCPY:
2102 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002103 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002104 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2105
2106 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2107 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2108 case Expand: assert(0 && "Cannot expand a byte!");
2109 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002110 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002111 break;
2112 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002113 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002114 break;
2115 }
2116 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002117 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002118 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002119
2120 SDOperand Tmp4;
2121 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002122 case Expand: {
2123 // Length is too big, just take the lo-part of the length.
2124 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002125 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002126 break;
2127 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002128 case Legal:
2129 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002130 break;
2131 case Promote:
2132 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002133 break;
2134 }
2135
2136 SDOperand Tmp5;
2137 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2138 case Expand: assert(0 && "Cannot expand this yet!");
2139 case Legal:
2140 Tmp5 = LegalizeOp(Node->getOperand(4));
2141 break;
2142 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002143 Tmp5 = PromoteOp(Node->getOperand(4));
2144 break;
2145 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002146
2147 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2148 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002149 case TargetLowering::Custom:
2150 isCustom = true;
2151 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002152 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002153 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002154 if (isCustom) {
2155 Tmp1 = TLI.LowerOperation(Result, DAG);
2156 if (Tmp1.Val) Result = Tmp1;
2157 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002158 break;
2159 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002160 // Otherwise, the target does not support this operation. Lower the
2161 // operation to an explicit libcall as appropriate.
2162 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002163 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002164 TargetLowering::ArgListTy Args;
2165 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002166
Reid Spencer6dced922005-01-12 14:53:45 +00002167 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002168 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencer791864c2007-01-03 04:22:32 +00002169 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002170 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002171 // Extend the (previously legalized) ubyte argument to be an int value
2172 // for the call.
2173 if (Tmp3.getValueType() > MVT::i32)
2174 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2175 else
2176 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencere63b6512006-12-31 05:55:36 +00002177 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
2178 Args.push_back(Entry);
2179 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2180 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002181
2182 FnName = "memset";
2183 } else if (Node->getOpcode() == ISD::MEMCPY ||
2184 Node->getOpcode() == ISD::MEMMOVE) {
Reid Spencer791864c2007-01-03 04:22:32 +00002185 Entry.Ty = IntPtrTy; Entry.isSigned = false;
2186 Entry.Node = Tmp2; Args.push_back(Entry);
2187 Entry.Node = Tmp3; Args.push_back(Entry);
2188 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002189 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2190 } else {
2191 assert(0 && "Unknown op!");
2192 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002193
Chris Lattner85d70c62005-01-11 05:57:22 +00002194 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002195 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002196 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002197 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002198 break;
2199 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002200 }
2201 break;
2202 }
Chris Lattner5385db52005-05-09 20:23:03 +00002203
Chris Lattner4157c412005-04-02 04:00:59 +00002204 case ISD::SHL_PARTS:
2205 case ISD::SRA_PARTS:
2206 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002207 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002208 bool Changed = false;
2209 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2210 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2211 Changed |= Ops.back() != Node->getOperand(i);
2212 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002213 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002214 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002215
Evan Cheng870e4f82006-01-09 18:31:59 +00002216 switch (TLI.getOperationAction(Node->getOpcode(),
2217 Node->getValueType(0))) {
2218 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002219 case TargetLowering::Legal: break;
2220 case TargetLowering::Custom:
2221 Tmp1 = TLI.LowerOperation(Result, DAG);
2222 if (Tmp1.Val) {
2223 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002224 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002225 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002226 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2227 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002228 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002229 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002230 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002231 return RetVal;
2232 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002233 break;
2234 }
2235
Chris Lattner13fe99c2005-04-02 05:00:07 +00002236 // Since these produce multiple values, make sure to remember that we
2237 // legalized all of them.
2238 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2239 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2240 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002241 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002242
2243 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002244 case ISD::ADD:
2245 case ISD::SUB:
2246 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002247 case ISD::MULHS:
2248 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002249 case ISD::UDIV:
2250 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002251 case ISD::AND:
2252 case ISD::OR:
2253 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002254 case ISD::SHL:
2255 case ISD::SRL:
2256 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002257 case ISD::FADD:
2258 case ISD::FSUB:
2259 case ISD::FMUL:
2260 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002261 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002262 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2263 case Expand: assert(0 && "Not possible");
2264 case Legal:
2265 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2266 break;
2267 case Promote:
2268 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2269 break;
2270 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002271
2272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002273
Andrew Lenharth72594262005-12-24 23:42:32 +00002274 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002275 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002276 case TargetLowering::Legal: break;
2277 case TargetLowering::Custom:
2278 Tmp1 = TLI.LowerOperation(Result, DAG);
2279 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002280 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002281 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002282 if (Node->getValueType(0) == MVT::i32) {
2283 switch (Node->getOpcode()) {
2284 default: assert(0 && "Do not know how to expand this integer BinOp!");
2285 case ISD::UDIV:
2286 case ISD::SDIV:
2287 const char *FnName = Node->getOpcode() == ISD::UDIV
2288 ? "__udivsi3" : "__divsi3";
2289 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002290 bool isSigned = Node->getOpcode() == ISD::SDIV;
2291 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002292 };
2293 break;
2294 }
2295
Chris Lattner87f08092006-04-02 03:57:31 +00002296 assert(MVT::isVector(Node->getValueType(0)) &&
2297 "Cannot expand this binary operator!");
2298 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002299 SmallVector<SDOperand, 8> Ops;
Chris Lattner87f08092006-04-02 03:57:31 +00002300 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2301 MVT::ValueType PtrVT = TLI.getPointerTy();
2302 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2303 i != e; ++i) {
2304 SDOperand Idx = DAG.getConstant(i, PtrVT);
2305 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2306 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2307 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2308 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002309 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2310 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002311 break;
2312 }
Evan Cheng119266e2006-04-12 21:20:24 +00002313 case TargetLowering::Promote: {
2314 switch (Node->getOpcode()) {
2315 default: assert(0 && "Do not know how to promote this BinOp!");
2316 case ISD::AND:
2317 case ISD::OR:
2318 case ISD::XOR: {
2319 MVT::ValueType OVT = Node->getValueType(0);
2320 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2321 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2322 // Bit convert each of the values to the new type.
2323 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2324 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2325 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2326 // Bit convert the result back the original type.
2327 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2328 break;
2329 }
2330 }
2331 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002332 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002333 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002334
2335 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2336 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2337 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2338 case Expand: assert(0 && "Not possible");
2339 case Legal:
2340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2341 break;
2342 case Promote:
2343 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2344 break;
2345 }
2346
2347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2348
2349 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2350 default: assert(0 && "Operation not supported");
2351 case TargetLowering::Custom:
2352 Tmp1 = TLI.LowerOperation(Result, DAG);
2353 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002354 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002355 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002356 case TargetLowering::Expand: {
Chris Lattner994d8e62006-03-13 06:08:38 +00002357 // If this target supports fabs/fneg natively, do this efficiently.
Evan Cheng003feb02007-01-04 21:56:39 +00002358 if (TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2359 TargetLowering::Legal &&
2360 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
2361 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002362 // Get the sign bit of the RHS.
2363 MVT::ValueType IVT =
2364 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2365 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2366 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2367 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2368 // Get the absolute value of the result.
2369 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2370 // Select between the nabs and abs value based on the sign bit of
2371 // the input.
2372 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2373 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2374 AbsVal),
2375 AbsVal);
2376 Result = LegalizeOp(Result);
2377 break;
2378 }
2379
2380 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002381 MVT::ValueType NVT =
2382 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2383 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2384 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2385 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002386 break;
2387 }
Evan Cheng003feb02007-01-04 21:56:39 +00002388 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002389 break;
2390
Nate Begeman5965bd12006-02-17 05:43:56 +00002391 case ISD::ADDC:
2392 case ISD::SUBC:
2393 Tmp1 = LegalizeOp(Node->getOperand(0));
2394 Tmp2 = LegalizeOp(Node->getOperand(1));
2395 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2396 // Since this produces two values, make sure to remember that we legalized
2397 // both of them.
2398 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2399 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2400 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002401
Nate Begeman5965bd12006-02-17 05:43:56 +00002402 case ISD::ADDE:
2403 case ISD::SUBE:
2404 Tmp1 = LegalizeOp(Node->getOperand(0));
2405 Tmp2 = LegalizeOp(Node->getOperand(1));
2406 Tmp3 = LegalizeOp(Node->getOperand(2));
2407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2408 // Since this produces two values, make sure to remember that we legalized
2409 // both of them.
2410 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2411 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2412 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002413
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002414 case ISD::BUILD_PAIR: {
2415 MVT::ValueType PairTy = Node->getValueType(0);
2416 // TODO: handle the case where the Lo and Hi operands are not of legal type
2417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2418 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2419 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002420 case TargetLowering::Promote:
2421 case TargetLowering::Custom:
2422 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002423 case TargetLowering::Legal:
2424 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2425 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2426 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002427 case TargetLowering::Expand:
2428 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2429 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2430 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2431 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2432 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002433 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002434 break;
2435 }
2436 break;
2437 }
2438
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002439 case ISD::UREM:
2440 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002441 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002442 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2443 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002444
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002445 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002446 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2447 case TargetLowering::Custom:
2448 isCustom = true;
2449 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002450 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002452 if (isCustom) {
2453 Tmp1 = TLI.LowerOperation(Result, DAG);
2454 if (Tmp1.Val) Result = Tmp1;
2455 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002456 break;
Chris Lattner81914422005-08-03 20:31:37 +00002457 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002458 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002459 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002460 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002461 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2462 TargetLowering::Legal) {
2463 // X % Y -> X-X/Y*Y
2464 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002465 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002466 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2467 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2468 } else {
2469 assert(Node->getValueType(0) == MVT::i32 &&
2470 "Cannot expand this binary operator!");
2471 const char *FnName = Node->getOpcode() == ISD::UREM
2472 ? "__umodsi3" : "__modsi3";
2473 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002474 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002475 }
Chris Lattner81914422005-08-03 20:31:37 +00002476 } else {
2477 // Floating point mod -> fmod libcall.
2478 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2479 SDOperand Dummy;
Reid Spencer791864c2007-01-03 04:22:32 +00002480 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002481 }
2482 break;
2483 }
2484 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002485 case ISD::VAARG: {
2486 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2487 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2488
Chris Lattner364b89a2006-01-28 07:42:08 +00002489 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002490 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2491 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002492 case TargetLowering::Custom:
2493 isCustom = true;
2494 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002495 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002496 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2497 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002498 Tmp1 = Result.getValue(1);
2499
2500 if (isCustom) {
2501 Tmp2 = TLI.LowerOperation(Result, DAG);
2502 if (Tmp2.Val) {
2503 Result = LegalizeOp(Tmp2);
2504 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2505 }
2506 }
Nate Begemane74795c2006-01-25 18:21:52 +00002507 break;
2508 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002509 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002510 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002511 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002512 // Increment the pointer, VAList, to the next vaarg
2513 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2514 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2515 TLI.getPointerTy()));
2516 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002517 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2518 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002519 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002520 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002521 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002522 Result = LegalizeOp(Result);
2523 break;
2524 }
2525 }
2526 // Since VAARG produces two values, make sure to remember that we
2527 // legalized both of them.
2528 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002529 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2530 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002531 }
2532
2533 case ISD::VACOPY:
2534 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2535 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2536 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2537
2538 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2539 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002540 case TargetLowering::Custom:
2541 isCustom = true;
2542 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002543 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002544 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2545 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002546 if (isCustom) {
2547 Tmp1 = TLI.LowerOperation(Result, DAG);
2548 if (Tmp1.Val) Result = Tmp1;
2549 }
Nate Begemane74795c2006-01-25 18:21:52 +00002550 break;
2551 case TargetLowering::Expand:
2552 // This defaults to loading a pointer from the input and storing it to the
2553 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002554 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002555 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002556 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2557 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002558 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2559 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002560 break;
2561 }
2562 break;
2563
2564 case ISD::VAEND:
2565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2567
2568 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2569 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002570 case TargetLowering::Custom:
2571 isCustom = true;
2572 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002573 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002575 if (isCustom) {
2576 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2577 if (Tmp1.Val) Result = Tmp1;
2578 }
Nate Begemane74795c2006-01-25 18:21:52 +00002579 break;
2580 case TargetLowering::Expand:
2581 Result = Tmp1; // Default to a no-op, return the chain
2582 break;
2583 }
2584 break;
2585
2586 case ISD::VASTART:
2587 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2588 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2589
Chris Lattnerd02b0542006-01-28 10:58:55 +00002590 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2591
Nate Begemane74795c2006-01-25 18:21:52 +00002592 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2593 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002594 case TargetLowering::Legal: break;
2595 case TargetLowering::Custom:
2596 Tmp1 = TLI.LowerOperation(Result, DAG);
2597 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002598 break;
2599 }
2600 break;
2601
Nate Begeman1b8121b2006-01-11 21:21:00 +00002602 case ISD::ROTL:
2603 case ISD::ROTR:
2604 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2605 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002606
2607 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2608 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002609 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002610 break;
2611
Nate Begeman2fba8a32006-01-14 03:14:10 +00002612 case ISD::BSWAP:
2613 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2614 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002615 case TargetLowering::Custom:
2616 assert(0 && "Cannot custom legalize this yet!");
2617 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002618 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002619 break;
2620 case TargetLowering::Promote: {
2621 MVT::ValueType OVT = Tmp1.getValueType();
2622 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2623 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002624
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002625 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2626 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2627 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2628 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2629 break;
2630 }
2631 case TargetLowering::Expand:
2632 Result = ExpandBSWAP(Tmp1);
2633 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002634 }
2635 break;
2636
Andrew Lenharth5e177822005-05-03 17:19:30 +00002637 case ISD::CTPOP:
2638 case ISD::CTTZ:
2639 case ISD::CTLZ:
2640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2641 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002642 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002643 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002644 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002645 break;
2646 case TargetLowering::Promote: {
2647 MVT::ValueType OVT = Tmp1.getValueType();
2648 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002649
2650 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002651 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2652 // Perform the larger operation, then subtract if needed.
2653 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002654 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002655 case ISD::CTPOP:
2656 Result = Tmp1;
2657 break;
2658 case ISD::CTTZ:
2659 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002660 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2661 DAG.getConstant(getSizeInBits(NVT), NVT),
2662 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002663 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002664 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2665 break;
2666 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002667 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002668 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2669 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002670 getSizeInBits(OVT), NVT));
2671 break;
2672 }
2673 break;
2674 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002675 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002676 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002677 break;
2678 }
2679 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002680
Chris Lattner13fe99c2005-04-02 05:00:07 +00002681 // Unary operators
2682 case ISD::FABS:
2683 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002684 case ISD::FSQRT:
2685 case ISD::FSIN:
2686 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002687 Tmp1 = LegalizeOp(Node->getOperand(0));
2688 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002689 case TargetLowering::Promote:
2690 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002691 isCustom = true;
2692 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002693 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002694 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002695 if (isCustom) {
2696 Tmp1 = TLI.LowerOperation(Result, DAG);
2697 if (Tmp1.Val) Result = Tmp1;
2698 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002699 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002700 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002701 switch (Node->getOpcode()) {
2702 default: assert(0 && "Unreachable!");
2703 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002704 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2705 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002706 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002707 break;
Chris Lattner80026402005-04-30 04:43:14 +00002708 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002709 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2710 MVT::ValueType VT = Node->getValueType(0);
2711 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002712 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002713 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2714 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002715 break;
2716 }
2717 case ISD::FSQRT:
2718 case ISD::FSIN:
2719 case ISD::FCOS: {
2720 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002721 const char *FnName = 0;
2722 switch(Node->getOpcode()) {
2723 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2724 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2725 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2726 default: assert(0 && "Unreachable!");
2727 }
Nate Begeman77558da2005-08-04 21:43:28 +00002728 SDOperand Dummy;
Reid Spencer791864c2007-01-03 04:22:32 +00002729 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002730 break;
2731 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002732 }
2733 break;
2734 }
2735 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002736 case ISD::FPOWI: {
2737 // We always lower FPOWI into a libcall. No target support it yet.
2738 const char *FnName = Node->getValueType(0) == MVT::f32
2739 ? "__powisf2" : "__powidf2";
2740 SDOperand Dummy;
Reid Spencer791864c2007-01-03 04:22:32 +00002741 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002742 break;
2743 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002744 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002745 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002746 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002747 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002748 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2749 Node->getOperand(0).getValueType())) {
2750 default: assert(0 && "Unknown operation action!");
2751 case TargetLowering::Expand:
2752 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2753 break;
2754 case TargetLowering::Legal:
2755 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002756 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002757 break;
2758 }
2759 }
2760 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002761 case ISD::VBIT_CONVERT: {
2762 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2763 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2764
2765 // The input has to be a vector type, we have to either scalarize it, pack
2766 // it, or convert it based on whether the input vector type is legal.
2767 SDNode *InVal = Node->getOperand(0).Val;
2768 unsigned NumElems =
2769 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2770 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2771
2772 // Figure out if there is a Packed type corresponding to this Vector
2773 // type. If so, convert to the packed type.
2774 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2775 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2776 // Turn this into a bit convert of the packed input.
2777 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2778 PackVectorOp(Node->getOperand(0), TVT));
2779 break;
2780 } else if (NumElems == 1) {
2781 // Turn this into a bit convert of the scalar input.
2782 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2783 PackVectorOp(Node->getOperand(0), EVT));
2784 break;
2785 } else {
2786 // FIXME: UNIMP! Store then reload
2787 assert(0 && "Cast from unsupported vector type not implemented yet!");
2788 }
2789 }
2790
Chris Lattner13fe99c2005-04-02 05:00:07 +00002791 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002792 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002793 case ISD::UINT_TO_FP: {
2794 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002795 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2796 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002797 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002798 Node->getOperand(0).getValueType())) {
2799 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800 case TargetLowering::Custom:
2801 isCustom = true;
2802 // FALLTHROUGH
2803 case TargetLowering::Legal:
2804 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002805 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002806 if (isCustom) {
2807 Tmp1 = TLI.LowerOperation(Result, DAG);
2808 if (Tmp1.Val) Result = Tmp1;
2809 }
2810 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002811 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002812 Result = ExpandLegalINT_TO_FP(isSigned,
2813 LegalizeOp(Node->getOperand(0)),
2814 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002815 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002816 case TargetLowering::Promote:
2817 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2818 Node->getValueType(0),
2819 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002820 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002821 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002822 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002823 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002824 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2825 Node->getValueType(0), Node->getOperand(0));
2826 break;
2827 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002828 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002829 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002830 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2831 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002832 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002833 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2834 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002835 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002836 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2837 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002838 break;
2839 }
2840 break;
2841 }
2842 case ISD::TRUNCATE:
2843 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2844 case Legal:
2845 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002846 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002847 break;
2848 case Expand:
2849 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2850
2851 // Since the result is legal, we should just be able to truncate the low
2852 // part of the source.
2853 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2854 break;
2855 case Promote:
2856 Result = PromoteOp(Node->getOperand(0));
2857 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2858 break;
2859 }
2860 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002861
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002862 case ISD::FP_TO_SINT:
2863 case ISD::FP_TO_UINT:
2864 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2865 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002866 Tmp1 = LegalizeOp(Node->getOperand(0));
2867
Chris Lattner44fe26f2005-07-29 00:11:56 +00002868 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2869 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002870 case TargetLowering::Custom:
2871 isCustom = true;
2872 // FALLTHROUGH
2873 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002875 if (isCustom) {
2876 Tmp1 = TLI.LowerOperation(Result, DAG);
2877 if (Tmp1.Val) Result = Tmp1;
2878 }
2879 break;
2880 case TargetLowering::Promote:
2881 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2882 Node->getOpcode() == ISD::FP_TO_SINT);
2883 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002884 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002885 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2886 SDOperand True, False;
2887 MVT::ValueType VT = Node->getOperand(0).getValueType();
2888 MVT::ValueType NVT = Node->getValueType(0);
2889 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2890 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2891 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2892 Node->getOperand(0), Tmp2, ISD::SETLT);
2893 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2894 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002895 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002896 Tmp2));
2897 False = DAG.getNode(ISD::XOR, NVT, False,
2898 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002899 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2900 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002901 } else {
2902 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2903 }
2904 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002905 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002906 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002907 case Expand: {
2908 // Convert f32 / f64 to i32 / i64.
2909 MVT::ValueType VT = Op.getValueType();
2910 const char *FnName = 0;
2911 switch (Node->getOpcode()) {
2912 case ISD::FP_TO_SINT:
2913 if (Node->getOperand(0).getValueType() == MVT::f32)
2914 FnName = (VT == MVT::i32) ? "__fixsfsi" : "__fixsfdi";
2915 else
2916 FnName = (VT == MVT::i32) ? "__fixdfsi" : "__fixdfdi";
2917 break;
2918 case ISD::FP_TO_UINT:
2919 if (Node->getOperand(0).getValueType() == MVT::f32)
2920 FnName = (VT == MVT::i32) ? "__fixunssfsi" : "__fixunssfdi";
2921 else
2922 FnName = (VT == MVT::i32) ? "__fixunsdfsi" : "__fixunsdfdi";
2923 break;
2924 default: assert(0 && "Unreachable!");
2925 }
2926 SDOperand Dummy;
Reid Spencer791864c2007-01-03 04:22:32 +00002927 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002928 break;
2929 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002930 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002931 Tmp1 = PromoteOp(Node->getOperand(0));
2932 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2933 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002934 break;
2935 }
2936 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002937
Chris Lattner7753f172005-09-02 00:18:10 +00002938 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002939 case ISD::ZERO_EXTEND:
2940 case ISD::SIGN_EXTEND:
2941 case ISD::FP_EXTEND:
2942 case ISD::FP_ROUND:
2943 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002944 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002945 case Legal:
2946 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002947 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002948 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002949 case Promote:
2950 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002951 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002952 Tmp1 = PromoteOp(Node->getOperand(0));
2953 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002954 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002955 case ISD::ZERO_EXTEND:
2956 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002957 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002958 Result = DAG.getZeroExtendInReg(Result,
2959 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002960 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002961 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002962 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002963 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002964 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002965 Result,
2966 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002967 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002968 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002969 Result = PromoteOp(Node->getOperand(0));
2970 if (Result.getValueType() != Op.getValueType())
2971 // Dynamically dead while we have only 2 FP types.
2972 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2973 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002974 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002975 Result = PromoteOp(Node->getOperand(0));
2976 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2977 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002978 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002979 }
2980 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002981 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002982 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002983 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002984 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002985
2986 // If this operation is not supported, convert it to a shl/shr or load/store
2987 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002988 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2989 default: assert(0 && "This action not supported for this op yet!");
2990 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002992 break;
2993 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002994 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002995 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002996 // NOTE: we could fall back on load/store here too for targets without
2997 // SAR. However, it is doubtful that any exist.
2998 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2999 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003000 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003001 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3002 Node->getOperand(0), ShiftCst);
3003 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3004 Result, ShiftCst);
3005 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003006 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003007 // EXTLOAD pair, targetting a temporary location (a stack slot).
3008
3009 // NOTE: there is a choice here between constantly creating new stack
3010 // slots and always reusing the same one. We currently always create
3011 // new ones, as reuse may inhibit scheduling.
3012 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Anderson20a631f2006-05-03 01:29:57 +00003013 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
3014 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003015 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003016 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00003017 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3018 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003019 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3020 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003021 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003022 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003023 } else {
3024 assert(0 && "Unknown op");
3025 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003026 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003027 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003028 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003029 }
Chris Lattner99222f72005-01-15 07:15:18 +00003030 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003031
Chris Lattner101ea662006-04-08 04:13:17 +00003032 assert(Result.getValueType() == Op.getValueType() &&
3033 "Bad legalization!");
3034
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003035 // Make sure that the generated code is itself legal.
3036 if (Result != Op)
3037 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003038
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003039 // Note that LegalizeOp may be reentered even from single-use nodes, which
3040 // means that we always must cache transformed nodes.
3041 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003042 return Result;
3043}
3044
Chris Lattner4d978642005-01-15 22:16:26 +00003045/// PromoteOp - Given an operation that produces a value in an invalid type,
3046/// promote it to compute the value into a larger type. The produced value will
3047/// have the correct bits for the low portion of the register, but no guarantee
3048/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003049SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3050 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003051 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003052 assert(getTypeAction(VT) == Promote &&
3053 "Caller should expand or legalize operands that are not promotable!");
3054 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3055 "Cannot promote to smaller type!");
3056
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003057 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003058 SDOperand Result;
3059 SDNode *Node = Op.Val;
3060
Chris Lattner1a570f12005-09-02 20:32:45 +00003061 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3062 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003063
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003064 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003065 case ISD::CopyFromReg:
3066 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003067 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003068#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00003069 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003070#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003071 assert(0 && "Do not know how to promote this operator!");
3072 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003073 case ISD::UNDEF:
3074 Result = DAG.getNode(ISD::UNDEF, NVT);
3075 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003076 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003077 if (VT != MVT::i1)
3078 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3079 else
3080 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003081 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3082 break;
3083 case ISD::ConstantFP:
3084 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3085 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3086 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003087
Chris Lattner2cb338d2005-01-18 02:59:52 +00003088 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003089 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003090 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3091 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003092 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003093
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003094 case ISD::TRUNCATE:
3095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3096 case Legal:
3097 Result = LegalizeOp(Node->getOperand(0));
3098 assert(Result.getValueType() >= NVT &&
3099 "This truncation doesn't make sense!");
3100 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3101 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3102 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003103 case Promote:
3104 // The truncation is not required, because we don't guarantee anything
3105 // about high bits anyway.
3106 Result = PromoteOp(Node->getOperand(0));
3107 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003108 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003109 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3110 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003111 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003112 }
3113 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003114 case ISD::SIGN_EXTEND:
3115 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003116 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003117 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3118 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3119 case Legal:
3120 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003121 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003122 break;
3123 case Promote:
3124 // Promote the reg if it's smaller.
3125 Result = PromoteOp(Node->getOperand(0));
3126 // The high bits are not guaranteed to be anything. Insert an extend.
3127 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003128 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003129 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003130 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003131 Result = DAG.getZeroExtendInReg(Result,
3132 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003133 break;
3134 }
3135 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003136 case ISD::BIT_CONVERT:
3137 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3138 Result = PromoteOp(Result);
3139 break;
3140
Chris Lattner4d978642005-01-15 22:16:26 +00003141 case ISD::FP_EXTEND:
3142 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3143 case ISD::FP_ROUND:
3144 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3145 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3146 case Promote: assert(0 && "Unreachable with 2 FP types!");
3147 case Legal:
3148 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003149 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003150 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003151 break;
3152 }
3153 break;
3154
3155 case ISD::SINT_TO_FP:
3156 case ISD::UINT_TO_FP:
3157 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3158 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003159 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003160 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003161 break;
3162
3163 case Promote:
3164 Result = PromoteOp(Node->getOperand(0));
3165 if (Node->getOpcode() == ISD::SINT_TO_FP)
3166 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003167 Result,
3168 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003169 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003170 Result = DAG.getZeroExtendInReg(Result,
3171 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003172 // No extra round required here.
3173 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003174 break;
3175 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003176 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3177 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003178 // Round if we cannot tolerate excess precision.
3179 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003180 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3181 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003182 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003183 }
Chris Lattner4d978642005-01-15 22:16:26 +00003184 break;
3185
Chris Lattner268d4572005-12-09 17:32:47 +00003186 case ISD::SIGN_EXTEND_INREG:
3187 Result = PromoteOp(Node->getOperand(0));
3188 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3189 Node->getOperand(1));
3190 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003191 case ISD::FP_TO_SINT:
3192 case ISD::FP_TO_UINT:
3193 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3194 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003195 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003196 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003197 break;
3198 case Promote:
3199 // The input result is prerounded, so we don't have to do anything
3200 // special.
3201 Tmp1 = PromoteOp(Node->getOperand(0));
3202 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003203 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003204 // If we're promoting a UINT to a larger size, check to see if the new node
3205 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3206 // we can use that instead. This allows us to generate better code for
3207 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3208 // legal, such as PowerPC.
3209 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003210 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003211 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3212 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003213 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3214 } else {
3215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3216 }
Chris Lattner4d978642005-01-15 22:16:26 +00003217 break;
3218
Chris Lattner13fe99c2005-04-02 05:00:07 +00003219 case ISD::FABS:
3220 case ISD::FNEG:
3221 Tmp1 = PromoteOp(Node->getOperand(0));
3222 assert(Tmp1.getValueType() == NVT);
3223 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3224 // NOTE: we do not have to do any extra rounding here for
3225 // NoExcessFPPrecision, because we know the input will have the appropriate
3226 // precision, and these operations don't modify precision at all.
3227 break;
3228
Chris Lattner9d6fa982005-04-28 21:44:33 +00003229 case ISD::FSQRT:
3230 case ISD::FSIN:
3231 case ISD::FCOS:
3232 Tmp1 = PromoteOp(Node->getOperand(0));
3233 assert(Tmp1.getValueType() == NVT);
3234 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003235 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003236 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3237 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003238 break;
3239
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003240 case ISD::AND:
3241 case ISD::OR:
3242 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003243 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003244 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003245 case ISD::MUL:
3246 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003247 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003248 // that too is okay if they are integer operations.
3249 Tmp1 = PromoteOp(Node->getOperand(0));
3250 Tmp2 = PromoteOp(Node->getOperand(1));
3251 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3252 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003253 break;
3254 case ISD::FADD:
3255 case ISD::FSUB:
3256 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003257 Tmp1 = PromoteOp(Node->getOperand(0));
3258 Tmp2 = PromoteOp(Node->getOperand(1));
3259 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3261
3262 // Floating point operations will give excess precision that we may not be
3263 // able to tolerate. If we DO allow excess precision, just leave it,
3264 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003265 // FIXME: Why would we need to round FP ops more than integer ones?
3266 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003267 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003268 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3269 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003270 break;
3271
Chris Lattner4d978642005-01-15 22:16:26 +00003272 case ISD::SDIV:
3273 case ISD::SREM:
3274 // These operators require that their input be sign extended.
3275 Tmp1 = PromoteOp(Node->getOperand(0));
3276 Tmp2 = PromoteOp(Node->getOperand(1));
3277 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003278 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3279 DAG.getValueType(VT));
3280 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3281 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003282 }
3283 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3284
3285 // Perform FP_ROUND: this is probably overly pessimistic.
3286 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003287 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3288 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003289 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003290 case ISD::FDIV:
3291 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003292 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003293 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003294 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3295 case Legal:
3296 Tmp1 = LegalizeOp(Node->getOperand(0));
3297 break;
3298 case Promote:
3299 Tmp1 = PromoteOp(Node->getOperand(0));
3300 break;
3301 case Expand:
3302 assert(0 && "not implemented");
3303 }
3304 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3305 case Legal:
3306 Tmp2 = LegalizeOp(Node->getOperand(1));
3307 break;
3308 case Promote:
3309 Tmp2 = PromoteOp(Node->getOperand(1));
3310 break;
3311 case Expand:
3312 assert(0 && "not implemented");
3313 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003314 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3315
3316 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003317 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003318 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3319 DAG.getValueType(VT));
3320 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003321
3322 case ISD::UDIV:
3323 case ISD::UREM:
3324 // These operators require that their input be zero extended.
3325 Tmp1 = PromoteOp(Node->getOperand(0));
3326 Tmp2 = PromoteOp(Node->getOperand(1));
3327 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003328 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3329 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003330 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3331 break;
3332
3333 case ISD::SHL:
3334 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003335 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003336 break;
3337 case ISD::SRA:
3338 // The input value must be properly sign extended.
3339 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003340 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3341 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003342 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003343 break;
3344 case ISD::SRL:
3345 // The input value must be properly zero extended.
3346 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003347 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003348 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003349 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003350
3351 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003352 Tmp1 = Node->getOperand(0); // Get the chain.
3353 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003354 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3355 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3356 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3357 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003358 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003359 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003360 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003361 // Increment the pointer, VAList, to the next vaarg
3362 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3363 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3364 TLI.getPointerTy()));
3365 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003366 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3367 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003368 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003369 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003370 }
3371 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003372 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003373 break;
3374
Evan Chenge71fe34d2006-10-09 20:57:25 +00003375 case ISD::LOAD: {
3376 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003377 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3378 ? ISD::EXTLOAD : LD->getExtensionType();
3379 Result = DAG.getExtLoad(ExtType, NVT,
3380 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003381 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengd35734b2006-10-11 07:10:22 +00003382 LD->getLoadedVT());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003383 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003384 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003385 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003386 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003387 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003388 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3389 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003390 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003391 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003392 case ISD::SELECT_CC:
3393 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3394 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3395 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003396 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003397 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003398 case ISD::BSWAP:
3399 Tmp1 = Node->getOperand(0);
3400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3401 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3402 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3403 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3404 TLI.getShiftAmountTy()));
3405 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003406 case ISD::CTPOP:
3407 case ISD::CTTZ:
3408 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003409 // Zero extend the argument
3410 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003411 // Perform the larger operation, then subtract if needed.
3412 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003413 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003414 case ISD::CTPOP:
3415 Result = Tmp1;
3416 break;
3417 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003418 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003419 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003420 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003421 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003422 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003423 break;
3424 case ISD::CTLZ:
3425 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003426 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3427 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003428 getSizeInBits(VT), NVT));
3429 break;
3430 }
3431 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003432 case ISD::VEXTRACT_VECTOR_ELT:
3433 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3434 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003435 case ISD::EXTRACT_VECTOR_ELT:
3436 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3437 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003438 }
3439
3440 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003441
3442 // Make sure the result is itself legal.
3443 Result = LegalizeOp(Result);
3444
3445 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003446 AddPromotedOperand(Op, Result);
3447 return Result;
3448}
Chris Lattnerdc750592005-01-07 07:47:09 +00003449
Chris Lattner6f423252006-03-31 17:55:51 +00003450/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3451/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3452/// on the vector type. The return type of this matches the element type of the
3453/// vector, which may not be legal for the target.
3454SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3455 // We know that operand #0 is the Vec vector. If the index is a constant
3456 // or if the invec is a supported hardware type, we can use it. Otherwise,
3457 // lower to a store then an indexed load.
3458 SDOperand Vec = Op.getOperand(0);
3459 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3460
3461 SDNode *InVal = Vec.Val;
3462 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3463 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3464
3465 // Figure out if there is a Packed type corresponding to this Vector
3466 // type. If so, convert to the packed type.
3467 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3468 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3469 // Turn this into a packed extract_vector_elt operation.
3470 Vec = PackVectorOp(Vec, TVT);
3471 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3472 } else if (NumElems == 1) {
3473 // This must be an access of the only element. Return it.
3474 return PackVectorOp(Vec, EVT);
3475 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3476 SDOperand Lo, Hi;
3477 SplitVectorOp(Vec, Lo, Hi);
3478 if (CIdx->getValue() < NumElems/2) {
3479 Vec = Lo;
3480 } else {
3481 Vec = Hi;
3482 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3483 }
3484
3485 // It's now an extract from the appropriate high or low part. Recurse.
3486 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3487 return LowerVEXTRACT_VECTOR_ELT(Op);
3488 } else {
3489 // Variable index case for extract element.
3490 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3491 assert(0 && "unimp!");
3492 return SDOperand();
3493 }
3494}
3495
Chris Lattner42a5fca2006-04-02 05:06:04 +00003496/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3497/// memory traffic.
3498SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3499 SDOperand Vector = Op.getOperand(0);
3500 SDOperand Idx = Op.getOperand(1);
3501
3502 // If the target doesn't support this, store the value to a temporary
3503 // stack slot, then LOAD the scalar element back out.
3504 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Chengab51cf22006-10-13 21:14:26 +00003505 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003506
3507 // Add the offset to the index.
3508 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3509 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3510 DAG.getConstant(EltSize, Idx.getValueType()));
3511 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3512
Evan Chenge71fe34d2006-10-09 20:57:25 +00003513 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003514}
3515
Chris Lattner6f423252006-03-31 17:55:51 +00003516
Nate Begeman7e7f4392006-02-01 07:19:44 +00003517/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3518/// with condition CC on the current target. This usually involves legalizing
3519/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3520/// there may be no choice but to create a new SetCC node to represent the
3521/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3522/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3523void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3524 SDOperand &RHS,
3525 SDOperand &CC) {
3526 SDOperand Tmp1, Tmp2, Result;
3527
3528 switch (getTypeAction(LHS.getValueType())) {
3529 case Legal:
3530 Tmp1 = LegalizeOp(LHS); // LHS
3531 Tmp2 = LegalizeOp(RHS); // RHS
3532 break;
3533 case Promote:
3534 Tmp1 = PromoteOp(LHS); // LHS
3535 Tmp2 = PromoteOp(RHS); // RHS
3536
3537 // If this is an FP compare, the operands have already been extended.
3538 if (MVT::isInteger(LHS.getValueType())) {
3539 MVT::ValueType VT = LHS.getValueType();
3540 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3541
3542 // Otherwise, we have to insert explicit sign or zero extends. Note
3543 // that we could insert sign extends for ALL conditions, but zero extend
3544 // is cheaper on many machines (an AND instead of two shifts), so prefer
3545 // it.
3546 switch (cast<CondCodeSDNode>(CC)->get()) {
3547 default: assert(0 && "Unknown integer comparison!");
3548 case ISD::SETEQ:
3549 case ISD::SETNE:
3550 case ISD::SETUGE:
3551 case ISD::SETUGT:
3552 case ISD::SETULE:
3553 case ISD::SETULT:
3554 // ALL of these operations will work if we either sign or zero extend
3555 // the operands (including the unsigned comparisons!). Zero extend is
3556 // usually a simpler/cheaper operation, so prefer it.
3557 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3558 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3559 break;
3560 case ISD::SETGE:
3561 case ISD::SETGT:
3562 case ISD::SETLT:
3563 case ISD::SETLE:
3564 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3565 DAG.getValueType(VT));
3566 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3567 DAG.getValueType(VT));
3568 break;
3569 }
3570 }
3571 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003572 case Expand: {
3573 MVT::ValueType VT = LHS.getValueType();
3574 if (VT == MVT::f32 || VT == MVT::f64) {
3575 // Expand into one or more soft-fp libcall(s).
3576 const char *FnName1 = NULL, *FnName2 = NULL;
Chris Lattnerb1a94922006-12-15 07:36:19 +00003577 ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003578 switch (cast<CondCodeSDNode>(CC)->get()) {
3579 case ISD::SETEQ:
3580 case ISD::SETOEQ:
3581 FnName1 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3582 CC1 = ISD::SETEQ;
3583 break;
3584 case ISD::SETNE:
3585 case ISD::SETUNE:
3586 FnName1 = (VT == MVT::f32) ? "__nesf2" : "__nedf2";
3587 CC1 = ISD::SETNE;
3588 break;
3589 case ISD::SETGE:
3590 case ISD::SETOGE:
3591 FnName1 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3592 CC1 = ISD::SETGE;
3593 break;
3594 case ISD::SETLT:
3595 case ISD::SETOLT:
3596 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3597 CC1 = ISD::SETLT;
3598 break;
3599 case ISD::SETLE:
3600 case ISD::SETOLE:
3601 FnName1 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3602 CC1 = ISD::SETLE;
3603 break;
3604 case ISD::SETGT:
3605 case ISD::SETOGT:
3606 FnName1 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3607 CC1 = ISD::SETGT;
3608 break;
3609 case ISD::SETUO:
3610 case ISD::SETO:
3611 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3612 CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO
3613 ? ISD::SETEQ : ISD::SETNE;
3614 break;
3615 default:
3616 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3617 CC1 = ISD::SETNE;
3618 switch (cast<CondCodeSDNode>(CC)->get()) {
3619 case ISD::SETONE:
3620 // SETONE = SETOLT | SETOGT
3621 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3622 CC1 = ISD::SETLT;
3623 // Fallthrough
3624 case ISD::SETUGT:
3625 FnName2 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3626 CC2 = ISD::SETGT;
3627 break;
3628 case ISD::SETUGE:
3629 FnName2 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3630 CC2 = ISD::SETGE;
3631 break;
3632 case ISD::SETULT:
3633 FnName2 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3634 CC2 = ISD::SETLT;
3635 break;
3636 case ISD::SETULE:
3637 FnName2 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3638 CC2 = ISD::SETLE;
3639 break;
3640 case ISD::SETUEQ:
3641 FnName2 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3642 CC2 = ISD::SETEQ;
3643 break;
3644 default: assert(0 && "Unsupported FP setcc!");
3645 }
3646 }
3647
3648 SDOperand Dummy;
3649 Tmp1 = ExpandLibCall(FnName1,
Reid Spencere63b6512006-12-31 05:55:36 +00003650 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003651 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003652 Tmp2 = DAG.getConstant(0, MVT::i32);
3653 CC = DAG.getCondCode(CC1);
3654 if (FnName2) {
3655 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
3656 LHS = ExpandLibCall(FnName2,
Reid Spencere63b6512006-12-31 05:55:36 +00003657 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003658 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003659 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
3660 DAG.getCondCode(CC2));
3661 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3662 Tmp2 = SDOperand();
3663 }
3664 LHS = Tmp1;
3665 RHS = Tmp2;
3666 return;
3667 }
3668
Nate Begeman7e7f4392006-02-01 07:19:44 +00003669 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3670 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003671 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003672 switch (cast<CondCodeSDNode>(CC)->get()) {
3673 case ISD::SETEQ:
3674 case ISD::SETNE:
3675 if (RHSLo == RHSHi)
3676 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3677 if (RHSCST->isAllOnesValue()) {
3678 // Comparison to -1.
3679 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3680 Tmp2 = RHSLo;
3681 break;
3682 }
3683
3684 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3685 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3686 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3687 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3688 break;
3689 default:
3690 // If this is a comparison of the sign bit, just look at the top part.
3691 // X > -1, x < 0
3692 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3693 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3694 CST->getValue() == 0) || // X < 0
3695 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3696 CST->isAllOnesValue())) { // X > -1
3697 Tmp1 = LHSHi;
3698 Tmp2 = RHSHi;
3699 break;
3700 }
3701
3702 // FIXME: This generated code sucks.
3703 ISD::CondCode LowCC;
3704 switch (cast<CondCodeSDNode>(CC)->get()) {
3705 default: assert(0 && "Unknown integer setcc!");
3706 case ISD::SETLT:
3707 case ISD::SETULT: LowCC = ISD::SETULT; break;
3708 case ISD::SETGT:
3709 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3710 case ISD::SETLE:
3711 case ISD::SETULE: LowCC = ISD::SETULE; break;
3712 case ISD::SETGE:
3713 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3714 }
3715
3716 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3717 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3718 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3719
3720 // NOTE: on targets without efficient SELECT of bools, we can always use
3721 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3722 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3723 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3724 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3725 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3726 Result, Tmp1, Tmp2));
3727 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003728 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003729 }
3730 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003731 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003732 LHS = Tmp1;
3733 RHS = Tmp2;
3734}
3735
Chris Lattner36e663d2005-12-23 00:16:34 +00003736/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003737/// The resultant code need not be legal. Note that SrcOp is the input operand
3738/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003739SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3740 SDOperand SrcOp) {
3741 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003742 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003743
3744 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003745 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003746 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003747 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003748}
3749
Chris Lattner6be79822006-04-04 17:23:26 +00003750SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3751 // Create a vector sized/aligned stack slot, store the value to element #0,
3752 // then load the whole vector back out.
3753 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003754 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003755 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003756 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003757}
3758
3759
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003760/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3761/// support the operation, but do support the resultant packed vector type.
3762SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3763
3764 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003765 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003766 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003767 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003768 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003769 std::map<SDOperand, std::vector<unsigned> > Values;
3770 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003771 bool isConstant = true;
3772 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3773 SplatValue.getOpcode() != ISD::UNDEF)
3774 isConstant = false;
3775
Evan Cheng1d2e9952006-03-24 01:17:21 +00003776 for (unsigned i = 1; i < NumElems; ++i) {
3777 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003778 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003779 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003780 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003781 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003782 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003783
3784 // If this isn't a constant element or an undef, we can't use a constant
3785 // pool load.
3786 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3787 V.getOpcode() != ISD::UNDEF)
3788 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003789 }
3790
3791 if (isOnlyLowElement) {
3792 // If the low element is an undef too, then this whole things is an undef.
3793 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3794 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3795 // Otherwise, turn this into a scalar_to_vector node.
3796 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3797 Node->getOperand(0));
3798 }
3799
Chris Lattner77e271c2006-03-24 07:29:17 +00003800 // If all elements are constants, create a load from the constant pool.
3801 if (isConstant) {
3802 MVT::ValueType VT = Node->getValueType(0);
3803 const Type *OpNTy =
3804 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3805 std::vector<Constant*> CV;
3806 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3807 if (ConstantFPSDNode *V =
3808 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3809 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3810 } else if (ConstantSDNode *V =
3811 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003812 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003813 } else {
3814 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3815 CV.push_back(UndefValue::get(OpNTy));
3816 }
3817 }
3818 Constant *CP = ConstantPacked::get(CV);
3819 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003820 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003821 }
3822
Chris Lattner21e68c82006-03-20 01:52:29 +00003823 if (SplatValue.Val) { // Splat of one value?
3824 // Build the shuffle constant vector: <0, 0, 0, 0>
3825 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003826 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003827 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003828 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003829 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3830 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00003831
3832 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003833 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003834 // Get the splatted value into the low element of a vector register.
3835 SDOperand LowValVec =
3836 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3837
3838 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3839 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3840 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3841 SplatMask);
3842 }
3843 }
3844
Evan Cheng1d2e9952006-03-24 01:17:21 +00003845 // If there are only two unique elements, we may be able to turn this into a
3846 // vector shuffle.
3847 if (Values.size() == 2) {
3848 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3849 MVT::ValueType MaskVT =
3850 MVT::getIntVectorWithNumElements(NumElems);
3851 std::vector<SDOperand> MaskVec(NumElems);
3852 unsigned i = 0;
3853 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3854 E = Values.end(); I != E; ++I) {
3855 for (std::vector<unsigned>::iterator II = I->second.begin(),
3856 EE = I->second.end(); II != EE; ++II)
3857 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3858 i += NumElems;
3859 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003860 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3861 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003862
3863 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003864 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3865 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003866 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003867 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3868 E = Values.end(); I != E; ++I) {
3869 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3870 I->first);
3871 Ops.push_back(Op);
3872 }
3873 Ops.push_back(ShuffleMask);
3874
3875 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003876 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3877 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003878 }
3879 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003880
3881 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3882 // aligned object on the stack, store each element into it, then load
3883 // the result as a vector.
3884 MVT::ValueType VT = Node->getValueType(0);
3885 // Create the stack frame object.
3886 SDOperand FIPtr = CreateStackTemporary(VT);
3887
3888 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003889 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003890 unsigned TypeByteSize =
3891 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003892 // Store (in the right endianness) the elements to memory.
3893 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3894 // Ignore undef elements.
3895 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3896
Chris Lattner8fa445a2006-03-22 01:46:54 +00003897 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003898
3899 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3900 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3901
Evan Chengdf9ac472006-10-05 23:01:46 +00003902 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00003903 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003904 }
3905
3906 SDOperand StoreChain;
3907 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003908 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3909 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003910 else
3911 StoreChain = DAG.getEntryNode();
3912
3913 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003914 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003915}
3916
3917/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3918/// specified value type.
3919SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3920 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3921 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3922 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3923 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3924}
3925
Chris Lattner4157c412005-04-02 04:00:59 +00003926void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3927 SDOperand Op, SDOperand Amt,
3928 SDOperand &Lo, SDOperand &Hi) {
3929 // Expand the subcomponents.
3930 SDOperand LHSL, LHSH;
3931 ExpandOp(Op, LHSL, LHSH);
3932
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003933 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00003934 MVT::ValueType VT = LHSL.getValueType();
3935 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00003936 Hi = Lo.getValue(1);
3937}
3938
3939
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003940/// ExpandShift - Try to find a clever way to expand this shift operation out to
3941/// smaller elements. If we can't find a way that is more efficient than a
3942/// libcall on this target, return false. Otherwise, return true with the
3943/// low-parts expanded into Lo and Hi.
3944bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3945 SDOperand &Lo, SDOperand &Hi) {
3946 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3947 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003948
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003949 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003950 SDOperand ShAmt = LegalizeOp(Amt);
3951 MVT::ValueType ShTy = ShAmt.getValueType();
3952 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3953 unsigned NVTBits = MVT::getSizeInBits(NVT);
3954
3955 // Handle the case when Amt is an immediate. Other cases are currently broken
3956 // and are disabled.
3957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3958 unsigned Cst = CN->getValue();
3959 // Expand the incoming operand to be shifted, so that we have its parts
3960 SDOperand InL, InH;
3961 ExpandOp(Op, InL, InH);
3962 switch(Opc) {
3963 case ISD::SHL:
3964 if (Cst > VTBits) {
3965 Lo = DAG.getConstant(0, NVT);
3966 Hi = DAG.getConstant(0, NVT);
3967 } else if (Cst > NVTBits) {
3968 Lo = DAG.getConstant(0, NVT);
3969 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003970 } else if (Cst == NVTBits) {
3971 Lo = DAG.getConstant(0, NVT);
3972 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003973 } else {
3974 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3975 Hi = DAG.getNode(ISD::OR, NVT,
3976 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3977 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3978 }
3979 return true;
3980 case ISD::SRL:
3981 if (Cst > VTBits) {
3982 Lo = DAG.getConstant(0, NVT);
3983 Hi = DAG.getConstant(0, NVT);
3984 } else if (Cst > NVTBits) {
3985 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3986 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003987 } else if (Cst == NVTBits) {
3988 Lo = InH;
3989 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003990 } else {
3991 Lo = DAG.getNode(ISD::OR, NVT,
3992 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3993 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3994 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3995 }
3996 return true;
3997 case ISD::SRA:
3998 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003999 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004000 DAG.getConstant(NVTBits-1, ShTy));
4001 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004002 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004003 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004004 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004005 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004006 } else if (Cst == NVTBits) {
4007 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004008 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004009 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004010 } else {
4011 Lo = DAG.getNode(ISD::OR, NVT,
4012 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4013 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4014 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4015 }
4016 return true;
4017 }
4018 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004019
4020 // Okay, the shift amount isn't constant. However, if we can tell that it is
4021 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4022 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4023 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4024
4025 // If we know that the high bit of the shift amount is one, then we can do
4026 // this as a couple of simple shifts.
4027 if (KnownOne & Mask) {
4028 // Mask out the high bit, which we know is set.
4029 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4030 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4031
4032 // Expand the incoming operand to be shifted, so that we have its parts
4033 SDOperand InL, InH;
4034 ExpandOp(Op, InL, InH);
4035 switch(Opc) {
4036 case ISD::SHL:
4037 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4038 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4039 return true;
4040 case ISD::SRL:
4041 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4042 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4043 return true;
4044 case ISD::SRA:
4045 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4046 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4047 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4048 return true;
4049 }
4050 }
4051
4052 // If we know that the high bit of the shift amount is zero, then we can do
4053 // this as a couple of simple shifts.
4054 if (KnownZero & Mask) {
4055 // Compute 32-amt.
4056 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4057 DAG.getConstant(NVTBits, Amt.getValueType()),
4058 Amt);
4059
4060 // Expand the incoming operand to be shifted, so that we have its parts
4061 SDOperand InL, InH;
4062 ExpandOp(Op, InL, InH);
4063 switch(Opc) {
4064 case ISD::SHL:
4065 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4066 Hi = DAG.getNode(ISD::OR, NVT,
4067 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4068 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4069 return true;
4070 case ISD::SRL:
4071 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4072 Lo = DAG.getNode(ISD::OR, NVT,
4073 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4074 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4075 return true;
4076 case ISD::SRA:
4077 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4078 Lo = DAG.getNode(ISD::OR, NVT,
4079 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4080 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4081 return true;
4082 }
4083 }
4084
Nate Begemanb0674922005-04-06 21:13:14 +00004085 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004086}
Chris Lattneraac464e2005-01-21 06:05:23 +00004087
Chris Lattner4add7e32005-01-23 04:42:50 +00004088
Chris Lattneraac464e2005-01-21 06:05:23 +00004089// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4090// does not fit into a register, return the lo part and set the hi part to the
4091// by-reg argument. If it does fit into a single register, return the result
4092// and leave the Hi part unset.
4093SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004094 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004095 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4096 // The input chain to this libcall is the entry node of the function.
4097 // Legalizing the call will automatically add the previous call to the
4098 // dependence.
4099 SDOperand InChain = DAG.getEntryNode();
4100
Chris Lattneraac464e2005-01-21 06:05:23 +00004101 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004102 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004103 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4104 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4105 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004106 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4107 Entry.isSigned = isSigned;
4108 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004109 }
4110 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004111
Chris Lattner06bbeb62005-05-11 19:02:11 +00004112 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004113 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004114 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004115 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004116 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004117
Chris Lattner462505f2006-02-13 09:18:02 +00004118 // Legalize the call sequence, starting with the chain. This will advance
4119 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4120 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4121 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004122 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004123 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004124 default: assert(0 && "Unknown thing");
4125 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004126 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004127 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004128 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004129 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004130 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004131 }
Chris Lattner63022662005-09-02 20:26:58 +00004132 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004133}
4134
Chris Lattner4add7e32005-01-23 04:42:50 +00004135
Chris Lattneraac464e2005-01-21 06:05:23 +00004136/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4137/// destination type is legal.
4138SDOperand SelectionDAGLegalize::
4139ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004140 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00004141 assert(getTypeAction(Source.getValueType()) == Expand &&
4142 "This is not an expansion!");
4143 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4144
Chris Lattner06bbeb62005-05-11 19:02:11 +00004145 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004146 assert(Source.getValueType() == MVT::i64 &&
4147 "This only works for 64-bit -> FP");
4148 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4149 // incoming integer is set. To handle this, we dynamically test to see if
4150 // it is set, and, if so, add a fudge factor.
4151 SDOperand Lo, Hi;
4152 ExpandOp(Source, Lo, Hi);
4153
Chris Lattner2a4f7312005-05-13 04:45:13 +00004154 // If this is unsigned, and not supported, first perform the conversion to
4155 // signed, then adjust the result if the sign bit is set.
4156 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4157 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4158
Chris Lattnerd47675e2005-08-09 20:20:18 +00004159 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4160 DAG.getConstant(0, Hi.getValueType()),
4161 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004162 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4163 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4164 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004165 uint64_t FF = 0x5f800000ULL;
4166 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004167 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004168
Chris Lattnerc30405e2005-08-26 17:15:30 +00004169 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004170 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4171 SDOperand FudgeInReg;
4172 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004173 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004174 else {
4175 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004176 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004177 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004178 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004179 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004180 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004181
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004182 // Check to see if the target has a custom way to lower this. If so, use it.
4183 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4184 default: assert(0 && "This action not implemented for this operation!");
4185 case TargetLowering::Legal:
4186 case TargetLowering::Expand:
4187 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004188 case TargetLowering::Custom: {
4189 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4190 Source), DAG);
4191 if (NV.Val)
4192 return LegalizeOp(NV);
4193 break; // The target decided this was legal after all
4194 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004195 }
4196
Chris Lattner153587e2005-05-12 07:00:44 +00004197 // Expand the source, then glue it back together for the call. We must expand
4198 // the source in case it is shared (this pass of legalize must traverse it).
4199 SDOperand SrcLo, SrcHi;
4200 ExpandOp(Source, SrcLo, SrcHi);
4201 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4202
Chris Lattner06bbeb62005-05-11 19:02:11 +00004203 const char *FnName = 0;
4204 if (DestTy == MVT::f32)
4205 FnName = "__floatdisf";
4206 else {
4207 assert(DestTy == MVT::f64 && "Unknown fp value type!");
4208 FnName = "__floatdidf";
4209 }
Chris Lattner462505f2006-02-13 09:18:02 +00004210
4211 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4212 SDOperand UnusedHiPart;
Reid Spencere63b6512006-12-31 05:55:36 +00004213 return ExpandLibCall(FnName, Source.Val, isSigned, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004214}
Misha Brukman835702a2005-04-21 22:36:52 +00004215
Chris Lattner689bdcc2006-01-28 08:25:58 +00004216/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4217/// INT_TO_FP operation of the specified operand when the target requests that
4218/// we expand it. At this point, we know that the result and operand types are
4219/// legal for the target.
4220SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4221 SDOperand Op0,
4222 MVT::ValueType DestVT) {
4223 if (Op0.getValueType() == MVT::i32) {
4224 // simple 32-bit [signed|unsigned] integer to float/double expansion
4225
4226 // get the stack frame index of a 8 byte buffer
4227 MachineFunction &MF = DAG.getMachineFunction();
4228 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4229 // get address of 8 byte buffer
4230 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4231 // word offset constant for Hi/Lo address computation
4232 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4233 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004234 SDOperand Hi = StackSlot;
4235 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4236 if (TLI.isLittleEndian())
4237 std::swap(Hi, Lo);
4238
Chris Lattner689bdcc2006-01-28 08:25:58 +00004239 // if signed map to unsigned space
4240 SDOperand Op0Mapped;
4241 if (isSigned) {
4242 // constant used to invert sign bit (signed to unsigned mapping)
4243 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4244 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4245 } else {
4246 Op0Mapped = Op0;
4247 }
4248 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004249 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004250 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004251 // initial hi portion of constructed double
4252 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4253 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004254 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004255 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004256 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004257 // FP constant to bias correct the final result
4258 SDOperand Bias = DAG.getConstantFP(isSigned ?
4259 BitsToDouble(0x4330000080000000ULL)
4260 : BitsToDouble(0x4330000000000000ULL),
4261 MVT::f64);
4262 // subtract the bias
4263 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4264 // final result
4265 SDOperand Result;
4266 // handle final rounding
4267 if (DestVT == MVT::f64) {
4268 // do nothing
4269 Result = Sub;
4270 } else {
4271 // if f32 then cast to f32
4272 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4273 }
4274 return Result;
4275 }
4276 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4277 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4278
4279 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4280 DAG.getConstant(0, Op0.getValueType()),
4281 ISD::SETLT);
4282 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4283 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4284 SignSet, Four, Zero);
4285
4286 // If the sign bit of the integer is set, the large number will be treated
4287 // as a negative number. To counteract this, the dynamic code adds an
4288 // offset depending on the data type.
4289 uint64_t FF;
4290 switch (Op0.getValueType()) {
4291 default: assert(0 && "Unsupported integer type!");
4292 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4293 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4294 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4295 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4296 }
4297 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004298 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004299
4300 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4301 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4302 SDOperand FudgeInReg;
4303 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004304 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004305 else {
4306 assert(DestVT == MVT::f64 && "Unexpected conversion");
4307 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4308 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004309 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004310 }
4311
4312 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4313}
4314
4315/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4316/// *INT_TO_FP operation of the specified operand when the target requests that
4317/// we promote it. At this point, we know that the result and operand types are
4318/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4319/// operation that takes a larger input.
4320SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4321 MVT::ValueType DestVT,
4322 bool isSigned) {
4323 // First step, figure out the appropriate *INT_TO_FP operation to use.
4324 MVT::ValueType NewInTy = LegalOp.getValueType();
4325
4326 unsigned OpToUse = 0;
4327
4328 // Scan for the appropriate larger type to use.
4329 while (1) {
4330 NewInTy = (MVT::ValueType)(NewInTy+1);
4331 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4332
4333 // If the target supports SINT_TO_FP of this type, use it.
4334 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4335 default: break;
4336 case TargetLowering::Legal:
4337 if (!TLI.isTypeLegal(NewInTy))
4338 break; // Can't use this datatype.
4339 // FALL THROUGH.
4340 case TargetLowering::Custom:
4341 OpToUse = ISD::SINT_TO_FP;
4342 break;
4343 }
4344 if (OpToUse) break;
4345 if (isSigned) continue;
4346
4347 // If the target supports UINT_TO_FP of this type, use it.
4348 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4349 default: break;
4350 case TargetLowering::Legal:
4351 if (!TLI.isTypeLegal(NewInTy))
4352 break; // Can't use this datatype.
4353 // FALL THROUGH.
4354 case TargetLowering::Custom:
4355 OpToUse = ISD::UINT_TO_FP;
4356 break;
4357 }
4358 if (OpToUse) break;
4359
4360 // Otherwise, try a larger type.
4361 }
4362
4363 // Okay, we found the operation and type to use. Zero extend our input to the
4364 // desired type then run the operation on it.
4365 return DAG.getNode(OpToUse, DestVT,
4366 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4367 NewInTy, LegalOp));
4368}
4369
4370/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4371/// FP_TO_*INT operation of the specified operand when the target requests that
4372/// we promote it. At this point, we know that the result and operand types are
4373/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4374/// operation that returns a larger result.
4375SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4376 MVT::ValueType DestVT,
4377 bool isSigned) {
4378 // First step, figure out the appropriate FP_TO*INT operation to use.
4379 MVT::ValueType NewOutTy = DestVT;
4380
4381 unsigned OpToUse = 0;
4382
4383 // Scan for the appropriate larger type to use.
4384 while (1) {
4385 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4386 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4387
4388 // If the target supports FP_TO_SINT returning this type, use it.
4389 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4390 default: break;
4391 case TargetLowering::Legal:
4392 if (!TLI.isTypeLegal(NewOutTy))
4393 break; // Can't use this datatype.
4394 // FALL THROUGH.
4395 case TargetLowering::Custom:
4396 OpToUse = ISD::FP_TO_SINT;
4397 break;
4398 }
4399 if (OpToUse) break;
4400
4401 // If the target supports FP_TO_UINT of this type, use it.
4402 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4403 default: break;
4404 case TargetLowering::Legal:
4405 if (!TLI.isTypeLegal(NewOutTy))
4406 break; // Can't use this datatype.
4407 // FALL THROUGH.
4408 case TargetLowering::Custom:
4409 OpToUse = ISD::FP_TO_UINT;
4410 break;
4411 }
4412 if (OpToUse) break;
4413
4414 // Otherwise, try a larger type.
4415 }
4416
4417 // Okay, we found the operation and type to use. Truncate the result of the
4418 // extended FP_TO_*INT operation to the desired size.
4419 return DAG.getNode(ISD::TRUNCATE, DestVT,
4420 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4421}
4422
4423/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4424///
4425SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4426 MVT::ValueType VT = Op.getValueType();
4427 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4428 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4429 switch (VT) {
4430 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4431 case MVT::i16:
4432 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4433 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4434 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4435 case MVT::i32:
4436 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4437 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4438 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4439 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4440 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4441 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4442 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4443 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4444 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4445 case MVT::i64:
4446 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4447 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4448 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4449 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4450 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4451 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4452 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4453 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4454 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4455 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4456 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4457 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4458 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4459 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4460 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4461 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4462 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4463 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4464 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4465 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4466 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4467 }
4468}
4469
4470/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4471///
4472SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4473 switch (Opc) {
4474 default: assert(0 && "Cannot expand this yet!");
4475 case ISD::CTPOP: {
4476 static const uint64_t mask[6] = {
4477 0x5555555555555555ULL, 0x3333333333333333ULL,
4478 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4479 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4480 };
4481 MVT::ValueType VT = Op.getValueType();
4482 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4483 unsigned len = getSizeInBits(VT);
4484 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4485 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4486 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4487 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4488 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4489 DAG.getNode(ISD::AND, VT,
4490 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4491 }
4492 return Op;
4493 }
4494 case ISD::CTLZ: {
4495 // for now, we do this:
4496 // x = x | (x >> 1);
4497 // x = x | (x >> 2);
4498 // ...
4499 // x = x | (x >>16);
4500 // x = x | (x >>32); // for 64-bit input
4501 // return popcount(~x);
4502 //
4503 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4504 MVT::ValueType VT = Op.getValueType();
4505 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4506 unsigned len = getSizeInBits(VT);
4507 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4508 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4509 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4510 }
4511 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4512 return DAG.getNode(ISD::CTPOP, VT, Op);
4513 }
4514 case ISD::CTTZ: {
4515 // for now, we use: { return popcount(~x & (x - 1)); }
4516 // unless the target has ctlz but not ctpop, in which case we use:
4517 // { return 32 - nlz(~x & (x-1)); }
4518 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4519 MVT::ValueType VT = Op.getValueType();
4520 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4521 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4522 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4523 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4524 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4525 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4526 TLI.isOperationLegal(ISD::CTLZ, VT))
4527 return DAG.getNode(ISD::SUB, VT,
4528 DAG.getConstant(getSizeInBits(VT), VT),
4529 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4530 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4531 }
4532 }
4533}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004534
Chris Lattnerdc750592005-01-07 07:47:09 +00004535/// ExpandOp - Expand the specified SDOperand into its two component pieces
4536/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4537/// LegalizeNodes map is filled in for any results that are not expanded, the
4538/// ExpandedNodes map is filled in for any results that are expanded, and the
4539/// Lo/Hi values are returned.
4540void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4541 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004542 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004543 SDNode *Node = Op.Val;
4544 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004545 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4546 VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004547 "Cannot expand to FP value or to larger int value!");
4548
Chris Lattner1a570f12005-09-02 20:32:45 +00004549 // See if we already expanded it.
4550 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4551 = ExpandedNodes.find(Op);
4552 if (I != ExpandedNodes.end()) {
4553 Lo = I->second.first;
4554 Hi = I->second.second;
4555 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004556 }
4557
Chris Lattnerdc750592005-01-07 07:47:09 +00004558 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004559 case ISD::CopyFromReg:
4560 assert(0 && "CopyFromReg must be legal!");
4561 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004562#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00004563 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004564#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004565 assert(0 && "Do not know how to expand this operator!");
4566 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004567 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004568 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004569 Lo = DAG.getNode(ISD::UNDEF, NVT);
4570 Hi = DAG.getNode(ISD::UNDEF, NVT);
4571 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004572 case ISD::Constant: {
4573 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4574 Lo = DAG.getConstant(Cst, NVT);
4575 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4576 break;
4577 }
Evan Cheng47833a12006-12-12 21:32:44 +00004578 case ISD::ConstantFP: {
4579 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004580 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004581 if (getTypeAction(Lo.getValueType()) == Expand)
4582 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004583 break;
4584 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004585 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004586 // Return the operands.
4587 Lo = Node->getOperand(0);
4588 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004589 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004590
4591 case ISD::SIGN_EXTEND_INREG:
4592 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004593 // sext_inreg the low part if needed.
4594 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4595
4596 // The high part gets the sign extension from the lo-part. This handles
4597 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004598 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4599 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4600 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004601 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004602
Nate Begeman2fba8a32006-01-14 03:14:10 +00004603 case ISD::BSWAP: {
4604 ExpandOp(Node->getOperand(0), Lo, Hi);
4605 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4606 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4607 Lo = TempLo;
4608 break;
4609 }
4610
Chris Lattner55e9cde2005-05-11 04:51:16 +00004611 case ISD::CTPOP:
4612 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004613 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4614 DAG.getNode(ISD::CTPOP, NVT, Lo),
4615 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004616 Hi = DAG.getConstant(0, NVT);
4617 break;
4618
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004619 case ISD::CTLZ: {
4620 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004621 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004622 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4623 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004624 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4625 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004626 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4627 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4628
4629 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4630 Hi = DAG.getConstant(0, NVT);
4631 break;
4632 }
4633
4634 case ISD::CTTZ: {
4635 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004636 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004637 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4638 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004639 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4640 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004641 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4642 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4643
4644 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4645 Hi = DAG.getConstant(0, NVT);
4646 break;
4647 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004648
Nate Begemane74795c2006-01-25 18:21:52 +00004649 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004650 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4651 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004652 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4653 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4654
4655 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004656 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004657 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4658 if (!TLI.isLittleEndian())
4659 std::swap(Lo, Hi);
4660 break;
4661 }
4662
Chris Lattnerdc750592005-01-07 07:47:09 +00004663 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004664 LoadSDNode *LD = cast<LoadSDNode>(Node);
4665 SDOperand Ch = LD->getChain(); // Legalize the chain.
4666 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4667 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattnerdc750592005-01-07 07:47:09 +00004668
Evan Chenge71fe34d2006-10-09 20:57:25 +00004669 if (ExtType == ISD::NON_EXTLOAD) {
4670 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng47833a12006-12-12 21:32:44 +00004671 if (VT == MVT::f32 || VT == MVT::f64) {
4672 // f32->i32 or f64->i64 one to one expansion.
4673 // Remember that we legalized the chain.
4674 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004675 // Recursively expand the new load.
4676 if (getTypeAction(NVT) == Expand)
4677 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004678 break;
4679 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004680
Evan Chenge71fe34d2006-10-09 20:57:25 +00004681 // Increment the pointer to the other half.
4682 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4684 getIntPtrConstant(IncrementSize));
4685 // FIXME: This creates a bogus srcvalue!
4686 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Misha Brukman835702a2005-04-21 22:36:52 +00004687
Evan Chenge71fe34d2006-10-09 20:57:25 +00004688 // Build a factor node to remember that this load is independent of the
4689 // other one.
4690 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4691 Hi.getValue(1));
4692
4693 // Remember that we legalized the chain.
4694 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4695 if (!TLI.isLittleEndian())
4696 std::swap(Lo, Hi);
4697 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004698 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004699
4700 if (VT == MVT::f64 && EVT == MVT::f32) {
4701 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4702 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4703 LD->getSrcValueOffset());
4704 // Remember that we legalized the chain.
4705 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4706 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4707 break;
4708 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004709
4710 if (EVT == NVT)
4711 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4712 LD->getSrcValueOffset());
4713 else
4714 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4715 LD->getSrcValueOffset(), EVT);
4716
4717 // Remember that we legalized the chain.
4718 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4719
4720 if (ExtType == ISD::SEXTLOAD) {
4721 // The high part is obtained by SRA'ing all but one of the bits of the
4722 // lo part.
4723 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4724 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4725 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4726 } else if (ExtType == ISD::ZEXTLOAD) {
4727 // The high part is just a zero.
4728 Hi = DAG.getConstant(0, NVT);
4729 } else /* if (ExtType == ISD::EXTLOAD) */ {
4730 // The high part is undefined.
4731 Hi = DAG.getNode(ISD::UNDEF, NVT);
4732 }
4733 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004734 break;
4735 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004736 case ISD::AND:
4737 case ISD::OR:
4738 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4739 SDOperand LL, LH, RL, RH;
4740 ExpandOp(Node->getOperand(0), LL, LH);
4741 ExpandOp(Node->getOperand(1), RL, RH);
4742 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4743 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4744 break;
4745 }
4746 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004747 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004748 ExpandOp(Node->getOperand(1), LL, LH);
4749 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004750 if (getTypeAction(NVT) == Expand)
4751 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004752 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004753 if (VT != MVT::f32)
4754 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004755 break;
4756 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004757 case ISD::SELECT_CC: {
4758 SDOperand TL, TH, FL, FH;
4759 ExpandOp(Node->getOperand(2), TL, TH);
4760 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004761 if (getTypeAction(NVT) == Expand)
4762 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004763 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4764 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004765 if (VT != MVT::f32)
4766 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4767 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004768 break;
4769 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004770 case ISD::ANY_EXTEND:
4771 // The low part is any extension of the input (which degenerates to a copy).
4772 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4773 // The high part is undefined.
4774 Hi = DAG.getNode(ISD::UNDEF, NVT);
4775 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004776 case ISD::SIGN_EXTEND: {
4777 // The low part is just a sign extension of the input (which degenerates to
4778 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004779 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004780
Chris Lattnerdc750592005-01-07 07:47:09 +00004781 // The high part is obtained by SRA'ing all but one of the bits of the lo
4782 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004783 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004784 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4785 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004786 break;
4787 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004788 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004789 // The low part is just a zero extension of the input (which degenerates to
4790 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004791 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004792
Chris Lattnerdc750592005-01-07 07:47:09 +00004793 // The high part is just a zero.
4794 Hi = DAG.getConstant(0, NVT);
4795 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004796
4797 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004798 SDOperand Tmp;
4799 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4800 // If the target wants to, allow it to lower this itself.
4801 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4802 case Expand: assert(0 && "cannot expand FP!");
4803 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4804 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4805 }
4806 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4807 }
4808
Evan Cheng4eee7242006-12-09 02:42:38 +00004809 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00004810 if (VT == MVT::f32 || VT == MVT::f64) {
4811 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00004812 if (getTypeAction(NVT) == Expand)
4813 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00004814 break;
4815 }
4816
4817 // If source operand will be expanded to the same type as VT, i.e.
4818 // i64 <- f64, i32 <- f32, expand the source operand instead.
4819 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4820 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4821 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00004822 break;
4823 }
4824
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004825 // Turn this into a load/store pair by default.
4826 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00004827 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004828
Chris Lattner36e663d2005-12-23 00:16:34 +00004829 ExpandOp(Tmp, Lo, Hi);
4830 break;
4831 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004832
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004833 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004834 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4835 TargetLowering::Custom &&
4836 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004837 Lo = TLI.LowerOperation(Op, DAG);
4838 assert(Lo.Val && "Node must be custom expanded!");
4839 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004840 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004841 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004842 break;
4843
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004844 // These operators cannot be expanded directly, emit them as calls to
4845 // library functions.
4846 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004847 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004848 SDOperand Op;
4849 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4850 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004851 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4852 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004853 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004854
Chris Lattner941d84a2005-07-30 01:40:57 +00004855 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4856
Chris Lattnerfe68d752005-07-29 00:33:32 +00004857 // Now that the custom expander is done, expand the result, which is still
4858 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004859 if (Op.Val) {
4860 ExpandOp(Op, Lo, Hi);
4861 break;
4862 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004863 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004864
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004865 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencer791864c2007-01-03 04:22:32 +00004866 Lo = ExpandLibCall("__fixsfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004867 else
Reid Spencer791864c2007-01-03 04:22:32 +00004868 Lo = ExpandLibCall("__fixdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004869 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004870
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004871 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004872 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004873 SDOperand Op;
4874 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4875 case Expand: assert(0 && "cannot expand FP!");
4876 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4877 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4878 }
4879
4880 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4881
4882 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004883 if (Op.Val) {
4884 ExpandOp(Op, Lo, Hi);
4885 break;
4886 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004887 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004888
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004889 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencer791864c2007-01-03 04:22:32 +00004890 Lo = ExpandLibCall("__fixunssfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004891 else
Reid Spencer791864c2007-01-03 04:22:32 +00004892 Lo = ExpandLibCall("__fixunsdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004893 break;
4894
Evan Cheng870e4f82006-01-09 18:31:59 +00004895 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004896 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004897 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004898 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004899 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004900 Op = TLI.LowerOperation(Op, DAG);
4901 if (Op.Val) {
4902 // Now that the custom expander is done, expand the result, which is
4903 // still VT.
4904 ExpandOp(Op, Lo, Hi);
4905 break;
4906 }
4907 }
4908
Chris Lattner72b503b2006-09-13 03:50:39 +00004909 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4910 // this X << 1 as X+X.
4911 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4912 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4913 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4914 SDOperand LoOps[2], HiOps[3];
4915 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4916 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4917 LoOps[1] = LoOps[0];
4918 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4919
4920 HiOps[1] = HiOps[0];
4921 HiOps[2] = Lo.getValue(1);
4922 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4923 break;
4924 }
4925 }
4926
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004927 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004928 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004929 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004930
4931 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004932 TargetLowering::LegalizeAction Action =
4933 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4934 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4935 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004936 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004937 break;
4938 }
4939
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004940 // Otherwise, emit a libcall.
Reid Spencer791864c2007-01-03 04:22:32 +00004941 Lo = ExpandLibCall("__ashldi3", Node, false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004942 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004943 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004944
Evan Cheng870e4f82006-01-09 18:31:59 +00004945 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004946 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004947 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004948 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004949 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004950 Op = TLI.LowerOperation(Op, DAG);
4951 if (Op.Val) {
4952 // Now that the custom expander is done, expand the result, which is
4953 // still VT.
4954 ExpandOp(Op, Lo, Hi);
4955 break;
4956 }
4957 }
4958
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004959 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004960 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004961 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004962
4963 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004964 TargetLowering::LegalizeAction Action =
4965 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4966 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4967 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004968 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004969 break;
4970 }
4971
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004972 // Otherwise, emit a libcall.
Reid Spencer791864c2007-01-03 04:22:32 +00004973 Lo = ExpandLibCall("__ashrdi3", Node, true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004974 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004975 }
4976
4977 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004978 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004979 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004980 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004981 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004982 Op = TLI.LowerOperation(Op, DAG);
4983 if (Op.Val) {
4984 // Now that the custom expander is done, expand the result, which is
4985 // still VT.
4986 ExpandOp(Op, Lo, Hi);
4987 break;
4988 }
4989 }
4990
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004991 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004992 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004993 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004994
4995 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004996 TargetLowering::LegalizeAction Action =
4997 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4998 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4999 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005000 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005001 break;
5002 }
5003
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005004 // Otherwise, emit a libcall.
Reid Spencer791864c2007-01-03 04:22:32 +00005005 Lo = ExpandLibCall("__lshrdi3", Node, false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005006 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005007 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005008
Misha Brukman835702a2005-04-21 22:36:52 +00005009 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005010 case ISD::SUB: {
5011 // If the target wants to custom expand this, let them.
5012 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5013 TargetLowering::Custom) {
5014 Op = TLI.LowerOperation(Op, DAG);
5015 if (Op.Val) {
5016 ExpandOp(Op, Lo, Hi);
5017 break;
5018 }
5019 }
5020
5021 // Expand the subcomponents.
5022 SDOperand LHSL, LHSH, RHSL, RHSH;
5023 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5024 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005025 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5026 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005027 LoOps[0] = LHSL;
5028 LoOps[1] = RHSL;
5029 HiOps[0] = LHSH;
5030 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005031 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005032 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005033 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005034 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005035 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005036 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005037 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005038 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005039 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005040 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005041 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005042 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005043 // If the target wants to custom expand this, let them.
5044 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005045 SDOperand New = TLI.LowerOperation(Op, DAG);
5046 if (New.Val) {
5047 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005048 break;
5049 }
5050 }
5051
Evan Chenge93762d2006-09-01 18:17:58 +00005052 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5053 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005054 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005055 SDOperand LL, LH, RL, RH;
5056 ExpandOp(Node->getOperand(0), LL, LH);
5057 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005058 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005059 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005060 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5061 // extended the sign bit of the low half through the upper half, and if so
5062 // emit a MULHS instead of the alternate sequence that is valid for any
5063 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005064 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005065 // is RH an extension of the sign bit of RL?
5066 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5067 RH.getOperand(1).getOpcode() == ISD::Constant &&
5068 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5069 // is LH an extension of the sign bit of LL?
5070 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5071 LH.getOperand(1).getOpcode() == ISD::Constant &&
5072 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005073 // Low part:
5074 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5075 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005076 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005077 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005078 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005079 // Low part:
5080 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5081
5082 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005083 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5084 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5085 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5086 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005088 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005089 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005090 }
Evan Chenge93762d2006-09-01 18:17:58 +00005091
Reid Spencer791864c2007-01-03 04:22:32 +00005092 Lo = ExpandLibCall("__muldi3" , Node, false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005093 break;
5094 }
Reid Spencere63b6512006-12-31 05:55:36 +00005095 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, true, Hi); break;
5096 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, false, Hi); break;
5097 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, true, Hi); break;
5098 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, false, Hi); break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005099
Evan Cheng4eee7242006-12-09 02:42:38 +00005100 case ISD::FADD:
Reid Spencere63b6512006-12-31 05:55:36 +00005101 Lo = ExpandLibCall(((VT == MVT::f32) ? "__addsf3" : "__adddf3"), Node,
5102 false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005103 break;
5104 case ISD::FSUB:
Reid Spencere63b6512006-12-31 05:55:36 +00005105 Lo = ExpandLibCall(((VT == MVT::f32) ? "__subsf3" : "__subdf3"), Node,
5106 false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005107 break;
5108 case ISD::FMUL:
Reid Spencere63b6512006-12-31 05:55:36 +00005109 Lo = ExpandLibCall(((VT == MVT::f32) ? "__mulsf3" : "__muldf3"), Node,
5110 false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005111 break;
5112 case ISD::FDIV:
Reid Spencere63b6512006-12-31 05:55:36 +00005113 Lo = ExpandLibCall(((VT == MVT::f32) ? "__divsf3" : "__divdf3"), Node,
5114 false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005115 break;
5116 case ISD::FP_EXTEND:
Reid Spencere63b6512006-12-31 05:55:36 +00005117 Lo = ExpandLibCall("__extendsfdf2", Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005118 break;
5119 case ISD::FP_ROUND:
Reid Spencere63b6512006-12-31 05:55:36 +00005120 Lo = ExpandLibCall("__truncdfsf2", Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005121 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005122 case ISD::FSQRT:
5123 case ISD::FSIN:
5124 case ISD::FCOS: {
5125 const char *FnName = 0;
5126 switch(Node->getOpcode()) {
5127 case ISD::FSQRT: FnName = (VT == MVT::f32) ? "sqrtf" : "sqrt"; break;
5128 case ISD::FSIN: FnName = (VT == MVT::f32) ? "sinf" : "sin"; break;
5129 case ISD::FCOS: FnName = (VT == MVT::f32) ? "cosf" : "cos"; break;
5130 default: assert(0 && "Unreachable!");
5131 }
Reid Spencere63b6512006-12-31 05:55:36 +00005132 Lo = ExpandLibCall(FnName, Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005133 break;
5134 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005135 case ISD::FABS: {
5136 SDOperand Mask = (VT == MVT::f64)
5137 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5138 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5139 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5140 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5141 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5142 if (getTypeAction(NVT) == Expand)
5143 ExpandOp(Lo, Lo, Hi);
5144 break;
5145 }
5146 case ISD::FNEG: {
5147 SDOperand Mask = (VT == MVT::f64)
5148 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5149 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5150 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5151 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5152 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5153 if (getTypeAction(NVT) == Expand)
5154 ExpandOp(Lo, Lo, Hi);
5155 break;
5156 }
Evan Cheng003feb02007-01-04 21:56:39 +00005157 case ISD::FCOPYSIGN: {
5158 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5159 if (getTypeAction(NVT) == Expand)
5160 ExpandOp(Lo, Lo, Hi);
5161 break;
5162 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005163 case ISD::SINT_TO_FP:
5164 case ISD::UINT_TO_FP: {
5165 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5166 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
5167 const char *FnName = 0;
5168 if (Node->getOperand(0).getValueType() == MVT::i64) {
5169 if (VT == MVT::f32)
5170 FnName = isSigned ? "__floatdisf" : "__floatundisf";
5171 else
5172 FnName = isSigned ? "__floatdidf" : "__floatundidf";
5173 } else {
5174 if (VT == MVT::f32)
5175 FnName = isSigned ? "__floatsisf" : "__floatunsisf";
5176 else
5177 FnName = isSigned ? "__floatsidf" : "__floatunsidf";
5178 }
5179
5180 // Promote the operand if needed.
5181 if (getTypeAction(SrcVT) == Promote) {
5182 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5183 Tmp = isSigned
5184 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5185 DAG.getValueType(SrcVT))
5186 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5187 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5188 }
Reid Spencere63b6512006-12-31 05:55:36 +00005189 Lo = ExpandLibCall(FnName, Node, isSigned, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005190 break;
5191 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005192 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005193
Chris Lattnerac12f682005-12-21 18:02:52 +00005194 // Make sure the resultant values have been legalized themselves, unless this
5195 // is a type that requires multi-step expansion.
5196 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5197 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005198 if (Hi.Val)
5199 // Don't legalize the high part if it is expanded to a single node.
5200 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005201 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005202
5203 // Remember in a map if the values will be reused later.
5204 bool isNew =
5205 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5206 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005207}
5208
Chris Lattner32206f52006-03-18 01:44:44 +00005209/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5210/// two smaller values of MVT::Vector type.
5211void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5212 SDOperand &Hi) {
5213 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5214 SDNode *Node = Op.Val;
5215 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5216 assert(NumElements > 1 && "Cannot split a single element vector!");
5217 unsigned NewNumElts = NumElements/2;
5218 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5219 SDOperand TypeNode = *(Node->op_end()-1);
5220
5221 // See if we already split it.
5222 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5223 = SplitNodes.find(Op);
5224 if (I != SplitNodes.end()) {
5225 Lo = I->second.first;
5226 Hi = I->second.second;
5227 return;
5228 }
5229
5230 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005231 default:
5232#ifndef NDEBUG
5233 Node->dump();
5234#endif
5235 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005236 case ISD::VBUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005237 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5238 Node->op_begin()+NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005239 LoOps.push_back(NewNumEltsNode);
5240 LoOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005241 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005242
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005243 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5244 Node->op_end()-2);
Chris Lattner32206f52006-03-18 01:44:44 +00005245 HiOps.push_back(NewNumEltsNode);
5246 HiOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005247 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005248 break;
5249 }
5250 case ISD::VADD:
5251 case ISD::VSUB:
5252 case ISD::VMUL:
5253 case ISD::VSDIV:
5254 case ISD::VUDIV:
5255 case ISD::VAND:
5256 case ISD::VOR:
5257 case ISD::VXOR: {
5258 SDOperand LL, LH, RL, RH;
5259 SplitVectorOp(Node->getOperand(0), LL, LH);
5260 SplitVectorOp(Node->getOperand(1), RL, RH);
5261
5262 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5263 NewNumEltsNode, TypeNode);
5264 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5265 NewNumEltsNode, TypeNode);
5266 break;
5267 }
5268 case ISD::VLOAD: {
5269 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5270 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5271 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5272
5273 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5274 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5275 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5276 getIntPtrConstant(IncrementSize));
5277 // FIXME: This creates a bogus srcvalue!
5278 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5279
5280 // Build a factor node to remember that this load is independent of the
5281 // other one.
5282 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5283 Hi.getValue(1));
5284
5285 // Remember that we legalized the chain.
5286 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005287 break;
5288 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005289 case ISD::VBIT_CONVERT: {
5290 // We know the result is a vector. The input may be either a vector or a
5291 // scalar value.
5292 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5293 // Lower to a store/load. FIXME: this could be improved probably.
5294 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5295
Evan Chengdf9ac472006-10-05 23:01:46 +00005296 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005297 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005298 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5299 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5300 SplitVectorOp(St, Lo, Hi);
5301 } else {
5302 // If the input is a vector type, we have to either scalarize it, pack it
5303 // or convert it based on whether the input vector type is legal.
5304 SDNode *InVal = Node->getOperand(0).Val;
5305 unsigned NumElems =
5306 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5307 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5308
5309 // If the input is from a single element vector, scalarize the vector,
5310 // then treat like a scalar.
5311 if (NumElems == 1) {
5312 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5313 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5314 Op.getOperand(1), Op.getOperand(2));
5315 SplitVectorOp(Scalar, Lo, Hi);
5316 } else {
5317 // Split the input vector.
5318 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5319
5320 // Convert each of the pieces now.
5321 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5322 NewNumEltsNode, TypeNode);
5323 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5324 NewNumEltsNode, TypeNode);
5325 }
5326 break;
5327 }
5328 }
Chris Lattner32206f52006-03-18 01:44:44 +00005329 }
5330
5331 // Remember in a map if the values will be reused later.
5332 bool isNew =
5333 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5334 assert(isNew && "Value already expanded?!?");
5335}
5336
5337
5338/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5339/// equivalent operation that returns a scalar (e.g. F32) or packed value
5340/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5341/// type for the result.
5342SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5343 MVT::ValueType NewVT) {
5344 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5345 SDNode *Node = Op.Val;
5346
5347 // See if we already packed it.
5348 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5349 if (I != PackedNodes.end()) return I->second;
5350
5351 SDOperand Result;
5352 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005353 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005354#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00005355 Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005356#endif
Chris Lattner29b23012006-03-19 01:17:20 +00005357 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00005358 case ISD::VADD:
5359 case ISD::VSUB:
5360 case ISD::VMUL:
5361 case ISD::VSDIV:
5362 case ISD::VUDIV:
5363 case ISD::VAND:
5364 case ISD::VOR:
5365 case ISD::VXOR:
5366 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5367 NewVT,
5368 PackVectorOp(Node->getOperand(0), NewVT),
5369 PackVectorOp(Node->getOperand(1), NewVT));
5370 break;
5371 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00005372 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5373 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005374
Evan Chenge71fe34d2006-10-09 20:57:25 +00005375 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5376 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattner32206f52006-03-18 01:44:44 +00005377
5378 // Remember that we legalized the chain.
5379 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5380 break;
5381 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005382 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00005383 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005384 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00005385 Result = Node->getOperand(0);
5386 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005387 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00005388
5389 // If all elements of the build_vector are undefs, return an undef.
5390 bool AllUndef = true;
5391 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5392 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5393 AllUndef = false;
5394 break;
5395 }
5396 if (AllUndef) {
5397 Result = DAG.getNode(ISD::UNDEF, NewVT);
5398 } else {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005399 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5400 Node->getNumOperands()-2);
Chris Lattnere1401e32006-04-08 05:34:25 +00005401 }
Chris Lattner32206f52006-03-18 01:44:44 +00005402 }
5403 break;
Chris Lattner29b23012006-03-19 01:17:20 +00005404 case ISD::VINSERT_VECTOR_ELT:
5405 if (!MVT::isVector(NewVT)) {
5406 // Returning a scalar? Must be the inserted element.
5407 Result = Node->getOperand(1);
5408 } else {
5409 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5410 PackVectorOp(Node->getOperand(0), NewVT),
5411 Node->getOperand(1), Node->getOperand(2));
5412 }
5413 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005414 case ISD::VVECTOR_SHUFFLE:
5415 if (!MVT::isVector(NewVT)) {
5416 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5417 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5418 if (cast<ConstantSDNode>(EltNum)->getValue())
5419 Result = PackVectorOp(Node->getOperand(1), NewVT);
5420 else
5421 Result = PackVectorOp(Node->getOperand(0), NewVT);
5422 } else {
5423 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5424 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5425 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5426 Node->getOperand(2).Val->op_end()-2);
5427 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005428 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5429 Node->getOperand(2).Val->op_begin(),
5430 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005431
5432 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5433 PackVectorOp(Node->getOperand(0), NewVT),
5434 PackVectorOp(Node->getOperand(1), NewVT), BV);
5435 }
5436 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00005437 case ISD::VBIT_CONVERT:
5438 if (Op.getOperand(0).getValueType() != MVT::Vector)
5439 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5440 else {
5441 // If the input is a vector type, we have to either scalarize it, pack it
5442 // or convert it based on whether the input vector type is legal.
5443 SDNode *InVal = Node->getOperand(0).Val;
5444 unsigned NumElems =
5445 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5446 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5447
5448 // Figure out if there is a Packed type corresponding to this Vector
5449 // type. If so, convert to the packed type.
5450 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5451 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5452 // Turn this into a bit convert of the packed input.
5453 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5454 PackVectorOp(Node->getOperand(0), TVT));
5455 break;
5456 } else if (NumElems == 1) {
5457 // Turn this into a bit convert of the scalar input.
5458 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5459 PackVectorOp(Node->getOperand(0), EVT));
5460 break;
5461 } else {
5462 // FIXME: UNIMP!
5463 assert(0 && "Cast from unsupported vector type not implemented yet!");
5464 }
5465 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00005466 break;
Chris Lattner02274a52006-04-08 22:22:57 +00005467 case ISD::VSELECT:
5468 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5469 PackVectorOp(Op.getOperand(1), NewVT),
5470 PackVectorOp(Op.getOperand(2), NewVT));
5471 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005472 }
5473
5474 if (TLI.isTypeLegal(NewVT))
5475 Result = LegalizeOp(Result);
5476 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5477 assert(isNew && "Value already packed?");
5478 return Result;
5479}
5480
Chris Lattnerdc750592005-01-07 07:47:09 +00005481
5482// SelectionDAG::Legalize - This is the entry point for the file.
5483//
Chris Lattner4add7e32005-01-23 04:42:50 +00005484void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005485 if (ViewLegalizeDAGs) viewGraph();
5486
Chris Lattnerdc750592005-01-07 07:47:09 +00005487 /// run - This is the main entry point to this class.
5488 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005489 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005490}
5491