blob: b25670c33bf2e9f41485b45dfbfdb46c0e778081 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000031#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000032using namespace llvm;
33
Chris Lattner5e46a192006-04-02 03:07:27 +000034#ifndef NDEBUG
35static cl::opt<bool>
36ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
37 cl::desc("Pop up a window to show dags before legalize"));
38#else
39static const bool ViewLegalizeDAGs = 0;
40#endif
41
Chris Lattner3e928bb2005-01-07 07:47:09 +000042//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
58
Chris Lattner6831a812006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
64 SDOperand LastCALLSEQ_END;
65
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 };
Chris Lattner6831a812006-02-13 09:18:02 +000076
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000081
Chris Lattner3e928bb2005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000085 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000086
Chris Lattner03c85462005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000090 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000091
Chris Lattnerc7029802006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
93 /// which which operands are the expanded version of the input. This allows
94 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000095 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000096
Chris Lattnerc7029802006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
98 /// which which operands are the split version of the input. This allows us
99 /// to avoid splitting the same node more than once.
100 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
101
Dan Gohman7f321562007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000105 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000106
Chris Lattner8afc48e2005-01-07 22:28:47 +0000107 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000108 LegalizedNodes.insert(std::make_pair(From, To));
109 // If someone requests legalization of the new node, return itself.
110 if (From != To)
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000112 }
Chris Lattner03c85462005-01-15 05:21:40 +0000113 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000114 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000115 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000116 // If someone requests legalization of the new node, return itself.
117 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000118 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000119
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120public:
121
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000122 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124 /// getTypeAction - Return how we should legalize values of this type, either
125 /// it is already legal or we need to expand it into multiple registers of
126 /// smaller integer type, or we need to promote it to a larger type.
127 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000128 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129 }
130
131 /// isTypeLegal - Return true if this type is legal on this target.
132 ///
133 bool isTypeLegal(MVT::ValueType VT) const {
134 return getTypeAction(VT) == Legal;
135 }
136
Chris Lattner3e928bb2005-01-07 07:47:09 +0000137 void LegalizeDAG();
138
Chris Lattner456a93a2006-01-28 07:39:30 +0000139private:
Dan Gohman7f321562007-06-25 16:23:39 +0000140 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000141 /// appropriate for its type.
142 void HandleOp(SDOperand Op);
143
144 /// LegalizeOp - We know that the specified value has a legal type.
145 /// Recursively ensure that the operands have legal types, then return the
146 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000148
149 /// PromoteOp - Given an operation that produces a value in an invalid type,
150 /// promote it to compute the value into a larger type. The produced value
151 /// will have the correct bits for the low portion of the register, but no
152 /// guarantee is made about the top bits: it may be zero, sign-extended, or
153 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000154 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
Chris Lattnerc7029802006-03-18 01:44:44 +0000156 /// ExpandOp - Expand the specified SDOperand into its two component pieces
157 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
158 /// the LegalizeNodes map is filled in for any results that are not expanded,
159 /// the ExpandedNodes map is filled in for any results that are expanded, and
160 /// the Lo/Hi values are returned. This applies to integer types and Vector
161 /// types.
162 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
163
Dan Gohman7f321562007-06-25 16:23:39 +0000164 /// SplitVectorOp - Given an operand of vector type, break it down into
165 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000166 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
167
Dan Gohman89b20c02007-06-27 14:06:22 +0000168 /// ScalarizeVectorOp - Given an operand of single-element vector type
169 /// (e.g. v1f32), convert it into the equivalent operation that returns a
170 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000171 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000172
Chris Lattner4352cc92006-04-04 17:23:26 +0000173 /// isShuffleLegal - Return true if a vector shuffle is legal with the
174 /// specified mask and type. Targets can specify exactly which masks they
175 /// support and the code generator is tasked with not creating illegal masks.
176 ///
177 /// Note that this will also return true for shuffles that are promoted to a
178 /// different type.
179 ///
180 /// If this is a legal shuffle, this method returns the (possibly promoted)
181 /// build_vector Mask. If it's not a legal shuffle, it returns null.
182 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
183
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000184 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000185 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000186
Nate Begeman750ac1b2006-02-01 07:19:44 +0000187 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
188
Chris Lattnerce872152006-03-19 06:31:19 +0000189 SDOperand CreateStackTemporary(MVT::ValueType VT);
190
Reid Spencer47857812006-12-31 05:55:36 +0000191 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000192 SDOperand &Hi);
193 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
194 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000195
Chris Lattner35481892005-12-23 00:16:34 +0000196 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000197 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000198 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000199 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
200 SDOperand LegalOp,
201 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000202 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000204 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
205 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000206
Chris Lattner456a93a2006-01-28 07:39:30 +0000207 SDOperand ExpandBSWAP(SDOperand Op);
208 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000209 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000211 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
212 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000213
Dan Gohman7f321562007-06-25 16:23:39 +0000214 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000215 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000216
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217 SDOperand getIntPtrConstant(uint64_t Val) {
218 return DAG.getConstant(Val, TLI.getPointerTy());
219 }
220};
221}
222
Chris Lattner4352cc92006-04-04 17:23:26 +0000223/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
224/// specified mask and type. Targets can specify exactly which masks they
225/// support and the code generator is tasked with not creating illegal masks.
226///
227/// Note that this will also return true for shuffles that are promoted to a
228/// different type.
229SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
230 SDOperand Mask) const {
231 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
232 default: return 0;
233 case TargetLowering::Legal:
234 case TargetLowering::Custom:
235 break;
236 case TargetLowering::Promote: {
237 // If this is promoted to a different type, convert the shuffle mask and
238 // ask if it is legal in the promoted type!
239 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
240
241 // If we changed # elements, change the shuffle mask.
242 unsigned NumEltsGrowth =
243 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
244 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
245 if (NumEltsGrowth > 1) {
246 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000247 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000248 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
249 SDOperand InOp = Mask.getOperand(i);
250 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
251 if (InOp.getOpcode() == ISD::UNDEF)
252 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
253 else {
254 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
255 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
256 }
257 }
258 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000259 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000260 }
261 VT = NVT;
262 break;
263 }
264 }
265 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
266}
267
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000268SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
269 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
270 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000271 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000272 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273}
274
Chris Lattnere10e6f72007-06-18 21:28:10 +0000275/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
276/// contains all of a nodes operands before it contains the node.
277static void ComputeTopDownOrdering(SelectionDAG &DAG,
278 SmallVector<SDNode*, 64> &Order) {
279
280 DenseMap<SDNode*, unsigned> Visited;
281 std::vector<SDNode*> Worklist;
282 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000283
Chris Lattnere10e6f72007-06-18 21:28:10 +0000284 // Compute ordering from all of the leaves in the graphs, those (like the
285 // entry node) that have no operands.
286 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
287 E = DAG.allnodes_end(); I != E; ++I) {
288 if (I->getNumOperands() == 0) {
289 Visited[I] = 0 - 1U;
290 Worklist.push_back(I);
291 }
Chris Lattner32fca002005-10-06 01:20:27 +0000292 }
293
Chris Lattnere10e6f72007-06-18 21:28:10 +0000294 while (!Worklist.empty()) {
295 SDNode *N = Worklist.back();
296 Worklist.pop_back();
297
298 if (++Visited[N] != N->getNumOperands())
299 continue; // Haven't visited all operands yet
300
301 Order.push_back(N);
302
303 // Now that we have N in, add anything that uses it if all of their operands
304 // are now done.
305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
306 UI != E; ++UI)
307 Worklist.push_back(*UI);
308 }
309
310 assert(Order.size() == Visited.size() &&
311 Order.size() ==
312 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
313 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000314}
315
Chris Lattner1618beb2005-07-29 00:11:56 +0000316
Chris Lattner3e928bb2005-01-07 07:47:09 +0000317void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000318 LastCALLSEQ_END = DAG.getEntryNode();
319 IsLegalizingCall = false;
320
Chris Lattnerab510a72005-10-02 17:49:46 +0000321 // The legalize process is inherently a bottom-up recursive process (users
322 // legalize their uses before themselves). Given infinite stack space, we
323 // could just start legalizing on the root and traverse the whole graph. In
324 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000325 // blocks. To avoid this problem, compute an ordering of the nodes where each
326 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000327 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000328 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000329
Chris Lattnerc7029802006-03-18 01:44:44 +0000330 for (unsigned i = 0, e = Order.size(); i != e; ++i)
331 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000332
333 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000334 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000335 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
336 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000337
338 ExpandedNodes.clear();
339 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000340 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000341 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000342 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343
344 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000345 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346}
347
Chris Lattner6831a812006-02-13 09:18:02 +0000348
349/// FindCallEndFromCallStart - Given a chained node that is part of a call
350/// sequence, find the CALLSEQ_END node that terminates the call sequence.
351static SDNode *FindCallEndFromCallStart(SDNode *Node) {
352 if (Node->getOpcode() == ISD::CALLSEQ_END)
353 return Node;
354 if (Node->use_empty())
355 return 0; // No CallSeqEnd
356
357 // The chain is usually at the end.
358 SDOperand TheChain(Node, Node->getNumValues()-1);
359 if (TheChain.getValueType() != MVT::Other) {
360 // Sometimes it's at the beginning.
361 TheChain = SDOperand(Node, 0);
362 if (TheChain.getValueType() != MVT::Other) {
363 // Otherwise, hunt for it.
364 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
365 if (Node->getValueType(i) == MVT::Other) {
366 TheChain = SDOperand(Node, i);
367 break;
368 }
369
370 // Otherwise, we walked into a node without a chain.
371 if (TheChain.getValueType() != MVT::Other)
372 return 0;
373 }
374 }
375
376 for (SDNode::use_iterator UI = Node->use_begin(),
377 E = Node->use_end(); UI != E; ++UI) {
378
379 // Make sure to only follow users of our token chain.
380 SDNode *User = *UI;
381 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
382 if (User->getOperand(i) == TheChain)
383 if (SDNode *Result = FindCallEndFromCallStart(User))
384 return Result;
385 }
386 return 0;
387}
388
389/// FindCallStartFromCallEnd - Given a chained node that is part of a call
390/// sequence, find the CALLSEQ_START node that initiates the call sequence.
391static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
392 assert(Node && "Didn't find callseq_start for a call??");
393 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
394
395 assert(Node->getOperand(0).getValueType() == MVT::Other &&
396 "Node doesn't have a token chain argument!");
397 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
398}
399
400/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
401/// see if any uses can reach Dest. If no dest operands can get to dest,
402/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000403///
404/// Keep track of the nodes we fine that actually do lead to Dest in
405/// NodesLeadingTo. This avoids retraversing them exponential number of times.
406///
407bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000408 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000409 if (N == Dest) return true; // N certainly leads to Dest :)
410
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000411 // If we've already processed this node and it does lead to Dest, there is no
412 // need to reprocess it.
413 if (NodesLeadingTo.count(N)) return true;
414
Chris Lattner6831a812006-02-13 09:18:02 +0000415 // If the first result of this node has been already legalized, then it cannot
416 // reach N.
417 switch (getTypeAction(N->getValueType(0))) {
418 case Legal:
419 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Promote:
422 if (PromotedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 case Expand:
425 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 }
428
429 // Okay, this node has not already been legalized. Check and legalize all
430 // operands. If none lead to Dest, then we can legalize this node.
431 bool OperandsLeadToDest = false;
432 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
433 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000434 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000435
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000436 if (OperandsLeadToDest) {
437 NodesLeadingTo.insert(N);
438 return true;
439 }
Chris Lattner6831a812006-02-13 09:18:02 +0000440
441 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000442 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000443 return false;
444}
445
Dan Gohman7f321562007-06-25 16:23:39 +0000446/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000447/// appropriate for its type.
448void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000449 MVT::ValueType VT = Op.getValueType();
450 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000451 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000452 case Legal: (void)LegalizeOp(Op); break;
453 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000454 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000455 if (!MVT::isVector(VT)) {
456 // If this is an illegal scalar, expand it into its two component
457 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 SDOperand X, Y;
459 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000460 } else if (MVT::getVectorNumElements(VT) == 1) {
461 // If this is an illegal single element vector, convert it to a
462 // scalar operation.
463 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000464 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000465 // Otherwise, this is an illegal multiple element vector.
466 // Split it in half and legalize both parts.
467 SDOperand X, Y;
468 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000469 }
470 break;
471 }
472}
Chris Lattner6831a812006-02-13 09:18:02 +0000473
Evan Cheng9f877882006-12-13 20:57:08 +0000474/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
475/// a load from the constant pool.
476static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000477 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000478 bool Extend = false;
479
480 // If a FP immediate is precise when represented as a float and if the
481 // target can do an extending load from float to double, we put it into
482 // the constant pool as a float, even if it's is statically typed as a
483 // double.
484 MVT::ValueType VT = CFP->getValueType(0);
485 bool isDouble = VT == MVT::f64;
486 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
487 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000488 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000489 double Val = LLVMC->getValue();
490 return isDouble
491 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
492 : DAG.getConstant(FloatToBits(Val), MVT::i32);
493 }
494
Evan Cheng00495212006-12-12 21:32:44 +0000495 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
496 // Only do this if the target has a native EXTLOAD instruction from f32.
497 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
498 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
499 VT = MVT::f32;
500 Extend = true;
501 }
502
503 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
504 if (Extend) {
505 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
506 CPIdx, NULL, 0, MVT::f32);
507 } else {
508 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
509 }
510}
511
Chris Lattner6831a812006-02-13 09:18:02 +0000512
Evan Cheng912095b2007-01-04 21:56:39 +0000513/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
514/// operations.
515static
516SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
517 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000518 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000519 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000520 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
521 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000522 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000523
Evan Cheng912095b2007-01-04 21:56:39 +0000524 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000525 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000526 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
527 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000529 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000530 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000531 // Shift right or sign-extend it if the two operands have different types.
532 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
533 if (SizeDiff > 0) {
534 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
535 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
536 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
537 } else if (SizeDiff < 0)
538 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000539
540 // Clear the sign bit of first operand.
541 SDOperand Mask2 = (VT == MVT::f64)
542 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
543 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
544 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000545 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000546 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
547
548 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000549 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
550 return Result;
551}
552
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000553/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
554static
555SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
556 TargetLowering &TLI) {
557 assert(MVT::isInteger(ST->getStoredVT()) &&
558 "Non integer unaligned stores not implemented.");
559 int SVOffset = ST->getSrcValueOffset();
560 SDOperand Chain = ST->getChain();
561 SDOperand Ptr = ST->getBasePtr();
562 SDOperand Val = ST->getValue();
563 MVT::ValueType VT = Val.getValueType();
564 // Get the half-size VT
565 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
566 int NumBits = MVT::getSizeInBits(NewStoredVT);
567 int Alignment = ST->getAlignment();
568 int IncrementSize = NumBits / 8;
569
570 // Divide the stored value in two parts.
571 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
572 SDOperand Lo = Val;
573 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
574
575 // Store the two parts
576 SDOperand Store1, Store2;
577 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
578 ST->getSrcValue(), SVOffset, NewStoredVT,
579 ST->isVolatile(), Alignment);
580 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
581 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
582 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
583 ST->getSrcValue(), SVOffset + IncrementSize,
584 NewStoredVT, ST->isVolatile(), Alignment);
585
586 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
587}
588
589/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
590static
591SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
592 TargetLowering &TLI) {
593 assert(MVT::isInteger(LD->getLoadedVT()) &&
594 "Non integer unaligned loads not implemented.");
595 int SVOffset = LD->getSrcValueOffset();
596 SDOperand Chain = LD->getChain();
597 SDOperand Ptr = LD->getBasePtr();
598 MVT::ValueType VT = LD->getValueType(0);
599 MVT::ValueType NewLoadedVT = LD->getLoadedVT() - 1;
600 int NumBits = MVT::getSizeInBits(NewLoadedVT);
601 int Alignment = LD->getAlignment();
602 int IncrementSize = NumBits / 8;
603 ISD::LoadExtType HiExtType = LD->getExtensionType();
604
605 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
606 if (HiExtType == ISD::NON_EXTLOAD)
607 HiExtType = ISD::ZEXTLOAD;
608
609 // Load the value in two parts
610 SDOperand Lo, Hi;
611 if (TLI.isLittleEndian()) {
612 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
613 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
616 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
617 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
618 Alignment);
619 } else {
620 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
621 NewLoadedVT,LD->isVolatile(), Alignment);
622 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
623 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
624 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
625 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
626 Alignment);
627 }
628
629 // aggregate the two parts
630 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
631 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
632 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
633
634 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
635 Hi.getValue(1));
636
637 SDOperand Ops[] = { Result, TF };
638 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
639}
Evan Cheng912095b2007-01-04 21:56:39 +0000640
Dan Gohmana3466152007-07-13 20:14:11 +0000641/// LegalizeOp - We know that the specified value has a legal type, and
642/// that its operands are legal. Now ensure that the operation itself
643/// is legal, recursively ensuring that the operands' operations remain
644/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000645SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000646 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000647 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000648 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000649
Chris Lattner3e928bb2005-01-07 07:47:09 +0000650 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000651 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000652 if (Node->getNumValues() > 1) {
653 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000654 if (getTypeAction(Node->getValueType(i)) != Legal) {
655 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000657 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000658 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000659 }
660 }
661
Chris Lattner45982da2005-05-12 16:53:42 +0000662 // Note that LegalizeOp may be reentered even from single-use nodes, which
663 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000664 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000665 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000666
Nate Begeman9373a812005-08-10 20:51:12 +0000667 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000668 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000669 bool isCustom = false;
670
Chris Lattner3e928bb2005-01-07 07:47:09 +0000671 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000672 case ISD::FrameIndex:
673 case ISD::EntryToken:
674 case ISD::Register:
675 case ISD::BasicBlock:
676 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000677 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000678 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000679 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000680 case ISD::TargetConstantPool:
681 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000682 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000683 case ISD::TargetExternalSymbol:
684 case ISD::VALUETYPE:
685 case ISD::SRCVALUE:
686 case ISD::STRING:
687 case ISD::CONDCODE:
688 // Primitives must all be legal.
689 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
690 "This must be legal!");
691 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000692 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000693 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
694 // If this is a target node, legalize it by legalizing the operands then
695 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000696 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000697 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000698 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000699
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000700 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000701
702 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
703 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
704 return Result.getValue(Op.ResNo);
705 }
706 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000707#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000708 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000709#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000710 assert(0 && "Do not know how to legalize this operator!");
711 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000712 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000713 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000714 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000715 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000716 case ISD::ConstantPool:
717 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000718 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
719 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000720 case TargetLowering::Custom:
721 Tmp1 = TLI.LowerOperation(Op, DAG);
722 if (Tmp1.Val) Result = Tmp1;
723 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000724 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000725 break;
726 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000727 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000728 case ISD::FRAMEADDR:
729 case ISD::RETURNADDR:
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000730 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemanbcc5f362007-01-29 22:58:52 +0000731 // The only option for these nodes is to custom lower them. If the target
732 // does not custom lower them, then return zero.
733 Tmp1 = TLI.LowerOperation(Op, DAG);
734 if (Tmp1.Val)
735 Result = Tmp1;
736 else
737 Result = DAG.getConstant(0, TLI.getPointerTy());
738 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000739 case ISD::EXCEPTIONADDR: {
740 Tmp1 = LegalizeOp(Node->getOperand(0));
741 MVT::ValueType VT = Node->getValueType(0);
742 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000745 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000746 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
747 }
748 break;
749 case TargetLowering::Custom:
750 Result = TLI.LowerOperation(Op, DAG);
751 if (Result.Val) break;
752 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000753 case TargetLowering::Legal: {
754 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
755 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
756 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000757 break;
758 }
759 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000760 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000761 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000762 case ISD::EHSELECTION: {
763 Tmp1 = LegalizeOp(Node->getOperand(0));
764 Tmp2 = LegalizeOp(Node->getOperand(1));
765 MVT::ValueType VT = Node->getValueType(0);
766 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
767 default: assert(0 && "This action is not supported yet!");
768 case TargetLowering::Expand: {
769 unsigned Reg = TLI.getExceptionSelectorRegister();
770 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
771 }
772 break;
773 case TargetLowering::Custom:
774 Result = TLI.LowerOperation(Op, DAG);
775 if (Result.Val) break;
776 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000777 case TargetLowering::Legal: {
778 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
779 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
780 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000781 break;
782 }
783 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000784 }
Jim Laskey8782d482007-02-28 20:43:58 +0000785 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000786 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000787 MVT::ValueType VT = Node->getValueType(0);
788 // The only "good" option for this node is to custom lower it.
789 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
790 default: assert(0 && "This action is not supported at all!");
791 case TargetLowering::Custom:
792 Result = TLI.LowerOperation(Op, DAG);
793 if (Result.Val) break;
794 // Fall Thru
795 case TargetLowering::Legal:
796 // Target does not know, how to lower this, lower to noop
797 Result = LegalizeOp(Node->getOperand(0));
798 break;
799 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000800 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000801 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000802 case ISD::AssertSext:
803 case ISD::AssertZext:
804 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000806 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000807 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000808 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000809 Result = Node->getOperand(Op.ResNo);
810 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000811 case ISD::CopyFromReg:
812 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000813 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000814 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000816 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000817 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000818 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000819 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
821 } else {
822 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
823 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000824 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
825 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000826 // Since CopyFromReg produces two values, make sure to remember that we
827 // legalized both of them.
828 AddLegalizedOperand(Op.getValue(0), Result);
829 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
830 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000831 case ISD::UNDEF: {
832 MVT::ValueType VT = Op.getValueType();
833 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000836 if (MVT::isInteger(VT))
837 Result = DAG.getConstant(0, VT);
838 else if (MVT::isFloatingPoint(VT))
839 Result = DAG.getConstantFP(0, VT);
840 else
841 assert(0 && "Unknown value type!");
842 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000843 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000844 break;
845 }
846 break;
847 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000848
Chris Lattner48b61a72006-03-28 00:40:33 +0000849 case ISD::INTRINSIC_W_CHAIN:
850 case ISD::INTRINSIC_WO_CHAIN:
851 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000852 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000853 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
854 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000855 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000856
857 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000858 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000859 TargetLowering::Custom) {
860 Tmp3 = TLI.LowerOperation(Result, DAG);
861 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000862 }
863
864 if (Result.Val->getNumValues() == 1) break;
865
866 // Must have return value and chain result.
867 assert(Result.Val->getNumValues() == 2 &&
868 "Cannot return more than two values!");
869
870 // Since loads produce two values, make sure to remember that we
871 // legalized both of them.
872 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
873 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
874 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000875 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000876
877 case ISD::LOCATION:
878 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
880
881 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
882 case TargetLowering::Promote:
883 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000884 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000885 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000886 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000887 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000888
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000889 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000890 const std::string &FName =
891 cast<StringSDNode>(Node->getOperand(3))->getValue();
892 const std::string &DirName =
893 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000894 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000895
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000896 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000897 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000898 SDOperand LineOp = Node->getOperand(1);
899 SDOperand ColOp = Node->getOperand(2);
900
901 if (useDEBUG_LOC) {
902 Ops.push_back(LineOp); // line #
903 Ops.push_back(ColOp); // col #
904 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000905 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000906 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000907 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
908 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000909 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000910 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000911 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000912 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000913 } else {
914 Result = Tmp1; // chain
915 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000916 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000917 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000918 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000919 if (Tmp1 != Node->getOperand(0) ||
920 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000921 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000922 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000923 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
924 Ops.push_back(Node->getOperand(1)); // line # must be legal.
925 Ops.push_back(Node->getOperand(2)); // col # must be legal.
926 } else {
927 // Otherwise promote them.
928 Ops.push_back(PromoteOp(Node->getOperand(1)));
929 Ops.push_back(PromoteOp(Node->getOperand(2)));
930 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000931 Ops.push_back(Node->getOperand(3)); // filename must be legal.
932 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000933 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000934 }
935 break;
936 }
937 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000938
939 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000940 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000941 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000942 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000943 case TargetLowering::Legal:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
945 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
946 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
947 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000949 break;
950 }
951 break;
952
Jim Laskey1ee29252007-01-26 14:34:52 +0000953 case ISD::LABEL:
954 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
955 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000956 default: assert(0 && "This action is not supported yet!");
957 case TargetLowering::Legal:
958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000960 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000961 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000962 case TargetLowering::Expand:
963 Result = LegalizeOp(Node->getOperand(0));
964 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000965 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000966 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000967
Chris Lattner3e928bb2005-01-07 07:47:09 +0000968 case ISD::Constant:
969 // We know we don't need to expand constants here, constants only have one
970 // value and we check that it is fine above.
971
972 // FIXME: Maybe we should handle things like targets that don't support full
973 // 32-bit immediates?
974 break;
975 case ISD::ConstantFP: {
976 // Spill FP immediates to the constant pool if the target cannot directly
977 // codegen them. Targets often have some immediate values that can be
978 // efficiently generated into an FP register without a load. We explicitly
979 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000980 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
981
982 // Check to see if this FP immediate is already legal.
983 bool isLegal = false;
984 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
985 E = TLI.legal_fpimm_end(); I != E; ++I)
986 if (CFP->isExactlyValue(*I)) {
987 isLegal = true;
988 break;
989 }
990
Chris Lattner3181a772006-01-29 06:26:56 +0000991 // If this is a legal constant, turn it into a TargetConstantFP node.
992 if (isLegal) {
993 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
994 break;
995 }
996
997 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
998 default: assert(0 && "This action is not supported yet!");
999 case TargetLowering::Custom:
1000 Tmp3 = TLI.LowerOperation(Result, DAG);
1001 if (Tmp3.Val) {
1002 Result = Tmp3;
1003 break;
1004 }
1005 // FALLTHROUGH
1006 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001007 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001008 }
1009 break;
1010 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001011 case ISD::TokenFactor:
1012 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001013 Tmp1 = LegalizeOp(Node->getOperand(0));
1014 Tmp2 = LegalizeOp(Node->getOperand(1));
1015 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1016 } else if (Node->getNumOperands() == 3) {
1017 Tmp1 = LegalizeOp(Node->getOperand(0));
1018 Tmp2 = LegalizeOp(Node->getOperand(1));
1019 Tmp3 = LegalizeOp(Node->getOperand(2));
1020 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001021 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001022 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001023 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001024 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1025 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001026 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001027 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001028 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001029
1030 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001031 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001032 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001033 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1034 assert(Tmp3.Val && "Target didn't custom lower this node!");
1035 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1036 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001037
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001038 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001039 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001040 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1041 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001042 if (Op.ResNo == i)
1043 Tmp2 = Tmp1;
1044 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1045 }
1046 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001047 case ISD::EXTRACT_SUBREG: {
1048 Tmp1 = LegalizeOp(Node->getOperand(0));
1049 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1050 assert(idx && "Operand must be a constant");
1051 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1053 }
1054 break;
1055 case ISD::INSERT_SUBREG: {
1056 Tmp1 = LegalizeOp(Node->getOperand(0));
1057 Tmp2 = LegalizeOp(Node->getOperand(1));
1058 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1059 assert(idx && "Operand must be a constant");
1060 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1062 }
1063 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001064 case ISD::BUILD_VECTOR:
1065 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001066 default: assert(0 && "This action is not supported yet!");
1067 case TargetLowering::Custom:
1068 Tmp3 = TLI.LowerOperation(Result, DAG);
1069 if (Tmp3.Val) {
1070 Result = Tmp3;
1071 break;
1072 }
1073 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001074 case TargetLowering::Expand:
1075 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001076 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001077 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001078 break;
1079 case ISD::INSERT_VECTOR_ELT:
1080 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1081 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1082 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1083 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1084
1085 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1086 Node->getValueType(0))) {
1087 default: assert(0 && "This action is not supported yet!");
1088 case TargetLowering::Legal:
1089 break;
1090 case TargetLowering::Custom:
1091 Tmp3 = TLI.LowerOperation(Result, DAG);
1092 if (Tmp3.Val) {
1093 Result = Tmp3;
1094 break;
1095 }
1096 // FALLTHROUGH
1097 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001098 // If the insert index is a constant, codegen this as a scalar_to_vector,
1099 // then a shuffle that inserts it into the right position in the vector.
1100 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1101 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1102 Tmp1.getValueType(), Tmp2);
1103
1104 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1105 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001106 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001107
1108 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1109 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1110 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001111 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001112 for (unsigned i = 0; i != NumElts; ++i) {
1113 if (i != InsertPos->getValue())
1114 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1115 else
1116 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1117 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001118 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1119 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001120
1121 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1122 Tmp1, ScVec, ShufMask);
1123 Result = LegalizeOp(Result);
1124 break;
1125 }
1126
Chris Lattner2332b9f2006-03-19 01:17:20 +00001127 // If the target doesn't support this, we have to spill the input vector
1128 // to a temporary stack slot, update the element, then reload it. This is
1129 // badness. We could also load the value into a vector register (either
1130 // with a "move to register" or "extload into register" instruction, then
1131 // permute it into place, if the idx is a constant and if the idx is
1132 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001133 MVT::ValueType VT = Tmp1.getValueType();
1134 MVT::ValueType EltVT = Tmp2.getValueType();
1135 MVT::ValueType IdxVT = Tmp3.getValueType();
1136 MVT::ValueType PtrVT = TLI.getPointerTy();
1137 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001138 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001139 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001140
1141 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001142 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1143 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001144 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001145 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1146 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1147 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001148 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001149 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001150 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001151 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001152 break;
1153 }
1154 }
1155 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001156 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001157 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1158 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1159 break;
1160 }
1161
Chris Lattnerce872152006-03-19 06:31:19 +00001162 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1163 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1164 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1165 Node->getValueType(0))) {
1166 default: assert(0 && "This action is not supported yet!");
1167 case TargetLowering::Legal:
1168 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001169 case TargetLowering::Custom:
1170 Tmp3 = TLI.LowerOperation(Result, DAG);
1171 if (Tmp3.Val) {
1172 Result = Tmp3;
1173 break;
1174 }
1175 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001176 case TargetLowering::Expand:
1177 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001178 break;
1179 }
Chris Lattnerce872152006-03-19 06:31:19 +00001180 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001181 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001182 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1183 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1185
1186 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001187 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1188 default: assert(0 && "Unknown operation action!");
1189 case TargetLowering::Legal:
1190 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1191 "vector shuffle should not be created if not legal!");
1192 break;
1193 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001194 Tmp3 = TLI.LowerOperation(Result, DAG);
1195 if (Tmp3.Val) {
1196 Result = Tmp3;
1197 break;
1198 }
1199 // FALLTHROUGH
1200 case TargetLowering::Expand: {
1201 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001202 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001203 MVT::ValueType PtrVT = TLI.getPointerTy();
1204 SDOperand Mask = Node->getOperand(2);
1205 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001206 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001207 for (unsigned i = 0; i != NumElems; ++i) {
1208 SDOperand Arg = Mask.getOperand(i);
1209 if (Arg.getOpcode() == ISD::UNDEF) {
1210 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1211 } else {
1212 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1213 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1214 if (Idx < NumElems)
1215 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1216 DAG.getConstant(Idx, PtrVT)));
1217 else
1218 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1219 DAG.getConstant(Idx - NumElems, PtrVT)));
1220 }
1221 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001222 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001223 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001224 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001225 case TargetLowering::Promote: {
1226 // Change base type to a different vector type.
1227 MVT::ValueType OVT = Node->getValueType(0);
1228 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1229
1230 // Cast the two input vectors.
1231 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1232 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1233
1234 // Convert the shuffle mask to the right # elements.
1235 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1236 assert(Tmp3.Val && "Shuffle not legal?");
1237 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1238 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1239 break;
1240 }
Chris Lattner87100e02006-03-20 01:52:29 +00001241 }
1242 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001243
1244 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001245 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001246 Tmp2 = LegalizeOp(Node->getOperand(1));
1247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001248 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001249 break;
1250
Dan Gohman7f321562007-06-25 16:23:39 +00001251 case ISD::EXTRACT_SUBVECTOR:
1252 Tmp1 = Node->getOperand(0);
1253 Tmp2 = LegalizeOp(Node->getOperand(1));
1254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1255 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001256 break;
1257
Chris Lattner6831a812006-02-13 09:18:02 +00001258 case ISD::CALLSEQ_START: {
1259 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1260
1261 // Recursively Legalize all of the inputs of the call end that do not lead
1262 // to this call start. This ensures that any libcalls that need be inserted
1263 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001264 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001265 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001266 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1267 NodesLeadingTo);
1268 }
Chris Lattner6831a812006-02-13 09:18:02 +00001269
1270 // Now that we legalized all of the inputs (which may have inserted
1271 // libcalls) create the new CALLSEQ_START node.
1272 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1273
1274 // Merge in the last call, to ensure that this call start after the last
1275 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001276 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001277 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1278 Tmp1 = LegalizeOp(Tmp1);
1279 }
Chris Lattner6831a812006-02-13 09:18:02 +00001280
1281 // Do not try to legalize the target-specific arguments (#1+).
1282 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001283 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001284 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001285 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001286 }
1287
1288 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001289 AddLegalizedOperand(Op.getValue(0), Result);
1290 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1291 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1292
Chris Lattner6831a812006-02-13 09:18:02 +00001293 // Now that the callseq_start and all of the non-call nodes above this call
1294 // sequence have been legalized, legalize the call itself. During this
1295 // process, no libcalls can/will be inserted, guaranteeing that no calls
1296 // can overlap.
1297 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1298 SDOperand InCallSEQ = LastCALLSEQ_END;
1299 // Note that we are selecting this call!
1300 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1301 IsLegalizingCall = true;
1302
1303 // Legalize the call, starting from the CALLSEQ_END.
1304 LegalizeOp(LastCALLSEQ_END);
1305 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1306 return Result;
1307 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001308 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001309 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1310 // will cause this node to be legalized as well as handling libcalls right.
1311 if (LastCALLSEQ_END.Val != Node) {
1312 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001313 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001314 assert(I != LegalizedNodes.end() &&
1315 "Legalizing the call start should have legalized this node!");
1316 return I->second;
1317 }
1318
1319 // Otherwise, the call start has been legalized and everything is going
1320 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001322 // Do not try to legalize the target-specific arguments (#1+), except for
1323 // an optional flag input.
1324 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1325 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001326 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001327 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001328 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001329 }
1330 } else {
1331 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1332 if (Tmp1 != Node->getOperand(0) ||
1333 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001334 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001335 Ops[0] = Tmp1;
1336 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001337 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001338 }
Chris Lattner6a542892006-01-24 05:48:21 +00001339 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001340 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001341 // This finishes up call legalization.
1342 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001343
1344 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1345 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1346 if (Node->getNumValues() == 2)
1347 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1348 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001349 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001350 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1352 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001354
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001355 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001356 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001357 switch (TLI.getOperationAction(Node->getOpcode(),
1358 Node->getValueType(0))) {
1359 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001360 case TargetLowering::Expand: {
1361 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1362 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1363 " not tell us which reg is the stack pointer!");
1364 SDOperand Chain = Tmp1.getOperand(0);
1365 SDOperand Size = Tmp2.getOperand(1);
1366 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1367 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1368 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001369 Tmp1 = LegalizeOp(Tmp1);
1370 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001371 break;
1372 }
1373 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001374 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1375 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001376 Tmp1 = LegalizeOp(Tmp3);
1377 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001378 }
Chris Lattner903d2782006-01-15 08:54:32 +00001379 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001380 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001381 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001382 }
Chris Lattner903d2782006-01-15 08:54:32 +00001383 // Since this op produce two values, make sure to remember that we
1384 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1386 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001387 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001388 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001389 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001390 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001391 bool Changed = false;
1392 // Legalize all of the operands of the inline asm, in case they are nodes
1393 // that need to be expanded or something. Note we skip the asm string and
1394 // all of the TargetConstant flags.
1395 SDOperand Op = LegalizeOp(Ops[0]);
1396 Changed = Op != Ops[0];
1397 Ops[0] = Op;
1398
1399 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1400 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1401 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1402 for (++i; NumVals; ++i, --NumVals) {
1403 SDOperand Op = LegalizeOp(Ops[i]);
1404 if (Op != Ops[i]) {
1405 Changed = true;
1406 Ops[i] = Op;
1407 }
1408 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001409 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001410
1411 if (HasInFlag) {
1412 Op = LegalizeOp(Ops.back());
1413 Changed |= Op != Ops.back();
1414 Ops.back() = Op;
1415 }
1416
1417 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001418 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001419
1420 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001421 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001422 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1423 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001424 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001425 case ISD::BR:
1426 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001427 // Ensure that libcalls are emitted before a branch.
1428 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1429 Tmp1 = LegalizeOp(Tmp1);
1430 LastCALLSEQ_END = DAG.getEntryNode();
1431
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001432 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001433 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001434 case ISD::BRIND:
1435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1436 // Ensure that libcalls are emitted before a branch.
1437 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1438 Tmp1 = LegalizeOp(Tmp1);
1439 LastCALLSEQ_END = DAG.getEntryNode();
1440
1441 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1442 default: assert(0 && "Indirect target must be legal type (pointer)!");
1443 case Legal:
1444 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1445 break;
1446 }
1447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1448 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001449 case ISD::BR_JT:
1450 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1451 // Ensure that libcalls are emitted before a branch.
1452 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1453 Tmp1 = LegalizeOp(Tmp1);
1454 LastCALLSEQ_END = DAG.getEntryNode();
1455
1456 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1457 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1458
1459 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1460 default: assert(0 && "This action is not supported yet!");
1461 case TargetLowering::Legal: break;
1462 case TargetLowering::Custom:
1463 Tmp1 = TLI.LowerOperation(Result, DAG);
1464 if (Tmp1.Val) Result = Tmp1;
1465 break;
1466 case TargetLowering::Expand: {
1467 SDOperand Chain = Result.getOperand(0);
1468 SDOperand Table = Result.getOperand(1);
1469 SDOperand Index = Result.getOperand(2);
1470
1471 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001472 MachineFunction &MF = DAG.getMachineFunction();
1473 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001474 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1475 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001476
1477 SDOperand LD;
1478 switch (EntrySize) {
1479 default: assert(0 && "Size of jump table not supported yet."); break;
1480 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1481 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1482 }
1483
1484 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001485 // For PIC, the sequence is:
1486 // BRIND(load(Jumptable + index) + RelocBase)
1487 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1488 SDOperand Reloc;
1489 if (TLI.usesGlobalOffsetTable())
1490 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1491 else
1492 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001493 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001494 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1495 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1496 } else {
1497 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1498 }
1499 }
1500 }
1501 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001502 case ISD::BRCOND:
1503 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001504 // Ensure that libcalls are emitted before a return.
1505 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1506 Tmp1 = LegalizeOp(Tmp1);
1507 LastCALLSEQ_END = DAG.getEntryNode();
1508
Chris Lattner47e92232005-01-18 19:27:06 +00001509 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1510 case Expand: assert(0 && "It's impossible to expand bools");
1511 case Legal:
1512 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1513 break;
1514 case Promote:
1515 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001516
1517 // The top bits of the promoted condition are not necessarily zero, ensure
1518 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001519 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001520 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1521 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001522 break;
1523 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001524
1525 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001527
1528 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1529 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001530 case TargetLowering::Legal: break;
1531 case TargetLowering::Custom:
1532 Tmp1 = TLI.LowerOperation(Result, DAG);
1533 if (Tmp1.Val) Result = Tmp1;
1534 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001535 case TargetLowering::Expand:
1536 // Expand brcond's setcc into its constituent parts and create a BR_CC
1537 // Node.
1538 if (Tmp2.getOpcode() == ISD::SETCC) {
1539 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1540 Tmp2.getOperand(0), Tmp2.getOperand(1),
1541 Node->getOperand(2));
1542 } else {
1543 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1544 DAG.getCondCode(ISD::SETNE), Tmp2,
1545 DAG.getConstant(0, Tmp2.getValueType()),
1546 Node->getOperand(2));
1547 }
1548 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001549 }
1550 break;
1551 case ISD::BR_CC:
1552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001553 // Ensure that libcalls are emitted before a branch.
1554 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1555 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001556 Tmp2 = Node->getOperand(2); // LHS
1557 Tmp3 = Node->getOperand(3); // RHS
1558 Tmp4 = Node->getOperand(1); // CC
1559
1560 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001561 LastCALLSEQ_END = DAG.getEntryNode();
1562
Nate Begeman750ac1b2006-02-01 07:19:44 +00001563 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1564 // the LHS is a legal SETCC itself. In this case, we need to compare
1565 // the result against zero to select between true and false values.
1566 if (Tmp3.Val == 0) {
1567 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1568 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001569 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001570
1571 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1572 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001573
Chris Lattner181b7a32005-12-17 23:46:46 +00001574 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1575 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001576 case TargetLowering::Legal: break;
1577 case TargetLowering::Custom:
1578 Tmp4 = TLI.LowerOperation(Result, DAG);
1579 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001580 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001581 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001582 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001583 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001584 LoadSDNode *LD = cast<LoadSDNode>(Node);
1585 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1586 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001587
Evan Cheng466685d2006-10-09 20:57:25 +00001588 ISD::LoadExtType ExtType = LD->getExtensionType();
1589 if (ExtType == ISD::NON_EXTLOAD) {
1590 MVT::ValueType VT = Node->getValueType(0);
1591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1592 Tmp3 = Result.getValue(0);
1593 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001594
Evan Cheng466685d2006-10-09 20:57:25 +00001595 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1596 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001597 case TargetLowering::Legal:
1598 // If this is an unaligned load and the target doesn't support it,
1599 // expand it.
1600 if (!TLI.allowsUnalignedMemoryAccesses()) {
1601 unsigned ABIAlignment = TLI.getTargetData()->
1602 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1603 if (LD->getAlignment() < ABIAlignment){
1604 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1605 TLI);
1606 Tmp3 = Result.getOperand(0);
1607 Tmp4 = Result.getOperand(1);
1608 LegalizeOp(Tmp3);
1609 LegalizeOp(Tmp4);
1610 }
1611 }
1612 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001613 case TargetLowering::Custom:
1614 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1615 if (Tmp1.Val) {
1616 Tmp3 = LegalizeOp(Tmp1);
1617 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001618 }
Evan Cheng466685d2006-10-09 20:57:25 +00001619 break;
1620 case TargetLowering::Promote: {
1621 // Only promote a load of vector type to another.
1622 assert(MVT::isVector(VT) && "Cannot promote this load!");
1623 // Change base type to a different vector type.
1624 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1625
1626 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001627 LD->getSrcValueOffset(),
1628 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001629 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1630 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001631 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001632 }
Evan Cheng466685d2006-10-09 20:57:25 +00001633 }
1634 // Since loads produce two values, make sure to remember that we
1635 // legalized both of them.
1636 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1637 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1638 return Op.ResNo ? Tmp4 : Tmp3;
1639 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001640 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001641 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1642 default: assert(0 && "This action is not supported yet!");
1643 case TargetLowering::Promote:
1644 assert(SrcVT == MVT::i1 &&
1645 "Can only promote extending LOAD from i1 -> i8!");
1646 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1647 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001648 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001649 Tmp1 = Result.getValue(0);
1650 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001651 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001652 case TargetLowering::Custom:
1653 isCustom = true;
1654 // FALLTHROUGH
1655 case TargetLowering::Legal:
1656 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1657 Tmp1 = Result.getValue(0);
1658 Tmp2 = Result.getValue(1);
1659
1660 if (isCustom) {
1661 Tmp3 = TLI.LowerOperation(Result, DAG);
1662 if (Tmp3.Val) {
1663 Tmp1 = LegalizeOp(Tmp3);
1664 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1665 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001666 } else {
1667 // If this is an unaligned load and the target doesn't support it,
1668 // expand it.
1669 if (!TLI.allowsUnalignedMemoryAccesses()) {
1670 unsigned ABIAlignment = TLI.getTargetData()->
1671 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1672 if (LD->getAlignment() < ABIAlignment){
1673 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1674 TLI);
1675 Tmp1 = Result.getOperand(0);
1676 Tmp2 = Result.getOperand(1);
1677 LegalizeOp(Tmp1);
1678 LegalizeOp(Tmp2);
1679 }
1680 }
Evan Cheng466685d2006-10-09 20:57:25 +00001681 }
1682 break;
1683 case TargetLowering::Expand:
1684 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1685 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1686 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001687 LD->getSrcValueOffset(),
1688 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001689 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1690 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1691 Tmp2 = LegalizeOp(Load.getValue(1));
1692 break;
1693 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001694 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001695 // Turn the unsupported load into an EXTLOAD followed by an explicit
1696 // zero/sign extend inreg.
1697 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1698 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001699 LD->getSrcValueOffset(), SrcVT,
1700 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001701 SDOperand ValRes;
1702 if (ExtType == ISD::SEXTLOAD)
1703 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1704 Result, DAG.getValueType(SrcVT));
1705 else
1706 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1707 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1708 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1709 break;
1710 }
1711 // Since loads produce two values, make sure to remember that we legalized
1712 // both of them.
1713 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1714 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1715 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001716 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001717 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001718 case ISD::EXTRACT_ELEMENT: {
1719 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1720 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001721 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001722 case Legal:
1723 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1724 // 1 -> Hi
1725 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1726 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1727 TLI.getShiftAmountTy()));
1728 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1729 } else {
1730 // 0 -> Lo
1731 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1732 Node->getOperand(0));
1733 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001734 break;
1735 case Expand:
1736 // Get both the low and high parts.
1737 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1738 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1739 Result = Tmp2; // 1 -> Hi
1740 else
1741 Result = Tmp1; // 0 -> Lo
1742 break;
1743 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001744 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001745 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001746
1747 case ISD::CopyToReg:
1748 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001749
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001750 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001751 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001752 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001753 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001754 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001756 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001757 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001758 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001759 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1761 Tmp3);
1762 } else {
1763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001764 }
1765
1766 // Since this produces two values, make sure to remember that we legalized
1767 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001768 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001769 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001770 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001771 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001772 break;
1773
1774 case ISD::RET:
1775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001776
1777 // Ensure that libcalls are emitted before a return.
1778 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1779 Tmp1 = LegalizeOp(Tmp1);
1780 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001781
Chris Lattner3e928bb2005-01-07 07:47:09 +00001782 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001783 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001784 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001785 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001786 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001787 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001788 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001789 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001790 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001791 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001792 SDOperand Lo, Hi;
1793 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001794
1795 // Big endian systems want the hi reg first.
1796 if (!TLI.isLittleEndian())
1797 std::swap(Lo, Hi);
1798
Evan Cheng13acce32006-12-11 19:27:14 +00001799 if (Hi.Val)
1800 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1801 else
1802 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001803 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001804 } else {
1805 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001806 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1807 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001808
Dan Gohman7f321562007-06-25 16:23:39 +00001809 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001810 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001811 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001812 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001813 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001814 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001816 } else if (NumElems == 1) {
1817 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001818 Tmp2 = ScalarizeVectorOp(Tmp2);
1819 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001821
1822 // FIXME: Returns of gcc generic vectors smaller than a legal type
1823 // should be returned in integer registers!
1824
Chris Lattnerf87324e2006-04-11 01:31:51 +00001825 // The scalarized value type may not be legal, e.g. it might require
1826 // promotion or expansion. Relegalize the return.
1827 Result = LegalizeOp(Result);
1828 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001829 // FIXME: Returns of gcc generic vectors larger than a legal vector
1830 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001831 SDOperand Lo, Hi;
1832 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001833 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001834 Result = LegalizeOp(Result);
1835 }
1836 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001837 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001838 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001839 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001840 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001841 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001842 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001843 }
1844 break;
1845 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001846 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001847 break;
1848 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001849 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001850 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001851 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001852 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1853 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001854 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001855 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001856 break;
1857 case Expand: {
1858 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001859 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001860 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001861 ExpandOp(Node->getOperand(i), Lo, Hi);
1862 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001863 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001864 if (Hi.Val) {
1865 NewValues.push_back(Hi);
1866 NewValues.push_back(Node->getOperand(i+1));
1867 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001868 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001869 }
1870 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001871 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001873
1874 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001875 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001876 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001877 Result = DAG.getNode(ISD::RET, MVT::Other,
1878 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001879 break;
1880 }
1881 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001882
Chris Lattner6862dbc2006-01-29 21:02:23 +00001883 if (Result.getOpcode() == ISD::RET) {
1884 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1885 default: assert(0 && "This action is not supported yet!");
1886 case TargetLowering::Legal: break;
1887 case TargetLowering::Custom:
1888 Tmp1 = TLI.LowerOperation(Result, DAG);
1889 if (Tmp1.Val) Result = Tmp1;
1890 break;
1891 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001892 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001893 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001894 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001895 StoreSDNode *ST = cast<StoreSDNode>(Node);
1896 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1897 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001898 int SVOffset = ST->getSrcValueOffset();
1899 unsigned Alignment = ST->getAlignment();
1900 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001901
Evan Cheng8b2794a2006-10-13 21:14:26 +00001902 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001903 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1904 // FIXME: We shouldn't do this for TargetConstantFP's.
1905 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1906 // to phase ordering between legalized code and the dag combiner. This
1907 // probably means that we need to integrate dag combiner and legalizer
1908 // together.
1909 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1910 if (CFP->getValueType(0) == MVT::f32) {
1911 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1912 } else {
1913 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1914 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1915 }
1916 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001917 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001918 break;
1919 }
1920
Evan Cheng8b2794a2006-10-13 21:14:26 +00001921 switch (getTypeAction(ST->getStoredVT())) {
1922 case Legal: {
1923 Tmp3 = LegalizeOp(ST->getValue());
1924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1925 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001926
Evan Cheng8b2794a2006-10-13 21:14:26 +00001927 MVT::ValueType VT = Tmp3.getValueType();
1928 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1929 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001930 case TargetLowering::Legal:
1931 // If this is an unaligned store and the target doesn't support it,
1932 // expand it.
1933 if (!TLI.allowsUnalignedMemoryAccesses()) {
1934 unsigned ABIAlignment = TLI.getTargetData()->
1935 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
1936 if (ST->getAlignment() < ABIAlignment)
1937 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
1938 TLI);
1939 }
1940 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001941 case TargetLowering::Custom:
1942 Tmp1 = TLI.LowerOperation(Result, DAG);
1943 if (Tmp1.Val) Result = Tmp1;
1944 break;
1945 case TargetLowering::Promote:
1946 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1947 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1948 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1949 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001950 ST->getSrcValue(), SVOffset, isVolatile,
1951 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001952 break;
1953 }
1954 break;
1955 }
1956 case Promote:
1957 // Truncate the value and store the result.
1958 Tmp3 = PromoteOp(ST->getValue());
1959 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001960 SVOffset, ST->getStoredVT(),
1961 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001962 break;
1963
1964 case Expand:
1965 unsigned IncrementSize = 0;
1966 SDOperand Lo, Hi;
1967
1968 // If this is a vector type, then we have to calculate the increment as
1969 // the product of the element size in bytes, and the number of elements
1970 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001971 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001972 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001973 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1974 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001975
Dan Gohman7f321562007-06-25 16:23:39 +00001976 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001977 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001978 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001979 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001980 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001981 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001982 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001983 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001984 Result = LegalizeOp(Result);
1985 break;
1986 } else if (NumElems == 1) {
1987 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001988 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001989 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001990 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001991 // The scalarized value type may not be legal, e.g. it might require
1992 // promotion or expansion. Relegalize the scalar store.
1993 Result = LegalizeOp(Result);
1994 break;
1995 } else {
1996 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1997 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1998 }
1999 } else {
2000 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002001 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002002
2003 if (!TLI.isLittleEndian())
2004 std::swap(Lo, Hi);
2005 }
2006
2007 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002008 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002009
2010 if (Hi.Val == NULL) {
2011 // Must be int <-> float one-to-one expansion.
2012 Result = Lo;
2013 break;
2014 }
2015
Evan Cheng8b2794a2006-10-13 21:14:26 +00002016 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2017 getIntPtrConstant(IncrementSize));
2018 assert(isTypeLegal(Tmp2.getValueType()) &&
2019 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002020 SVOffset += IncrementSize;
2021 if (Alignment > IncrementSize)
2022 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002023 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002024 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002025 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2026 break;
2027 }
2028 } else {
2029 // Truncating store
2030 assert(isTypeLegal(ST->getValue().getValueType()) &&
2031 "Cannot handle illegal TRUNCSTORE yet!");
2032 Tmp3 = LegalizeOp(ST->getValue());
2033
2034 // The only promote case we handle is TRUNCSTORE:i1 X into
2035 // -> TRUNCSTORE:i8 (and X, 1)
2036 if (ST->getStoredVT() == MVT::i1 &&
2037 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2038 // Promote the bool to a mask then store.
2039 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2040 DAG.getConstant(1, Tmp3.getValueType()));
2041 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002042 SVOffset, MVT::i8,
2043 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002044 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2045 Tmp2 != ST->getBasePtr()) {
2046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2047 ST->getOffset());
2048 }
2049
2050 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2051 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002052 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002053 case TargetLowering::Legal:
2054 // If this is an unaligned store and the target doesn't support it,
2055 // expand it.
2056 if (!TLI.allowsUnalignedMemoryAccesses()) {
2057 unsigned ABIAlignment = TLI.getTargetData()->
2058 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2059 if (ST->getAlignment() < ABIAlignment)
2060 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2061 TLI);
2062 }
2063 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002064 case TargetLowering::Custom:
2065 Tmp1 = TLI.LowerOperation(Result, DAG);
2066 if (Tmp1.Val) Result = Tmp1;
2067 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002068 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002069 }
2070 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002071 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002072 case ISD::PCMARKER:
2073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002075 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002076 case ISD::STACKSAVE:
2077 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002078 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2079 Tmp1 = Result.getValue(0);
2080 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002081
Chris Lattner140d53c2006-01-13 02:50:02 +00002082 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2083 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002084 case TargetLowering::Legal: break;
2085 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002086 Tmp3 = TLI.LowerOperation(Result, DAG);
2087 if (Tmp3.Val) {
2088 Tmp1 = LegalizeOp(Tmp3);
2089 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002090 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002091 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002092 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002093 // Expand to CopyFromReg if the target set
2094 // StackPointerRegisterToSaveRestore.
2095 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002096 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002097 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002098 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002099 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002100 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2101 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002102 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002103 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002104 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002105
2106 // Since stacksave produce two values, make sure to remember that we
2107 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002108 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2109 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2110 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002111
Chris Lattner140d53c2006-01-13 02:50:02 +00002112 case ISD::STACKRESTORE:
2113 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2114 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002116
2117 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2118 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002119 case TargetLowering::Legal: break;
2120 case TargetLowering::Custom:
2121 Tmp1 = TLI.LowerOperation(Result, DAG);
2122 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002123 break;
2124 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002125 // Expand to CopyToReg if the target set
2126 // StackPointerRegisterToSaveRestore.
2127 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2128 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2129 } else {
2130 Result = Tmp1;
2131 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002132 break;
2133 }
2134 break;
2135
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002136 case ISD::READCYCLECOUNTER:
2137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002138 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002139 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2140 Node->getValueType(0))) {
2141 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002142 case TargetLowering::Legal:
2143 Tmp1 = Result.getValue(0);
2144 Tmp2 = Result.getValue(1);
2145 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002146 case TargetLowering::Custom:
2147 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002148 Tmp1 = LegalizeOp(Result.getValue(0));
2149 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002150 break;
2151 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002152
2153 // Since rdcc produce two values, make sure to remember that we legalized
2154 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002155 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2156 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002157 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002158
Chris Lattner2ee743f2005-01-14 22:08:15 +00002159 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002160 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2161 case Expand: assert(0 && "It's impossible to expand bools");
2162 case Legal:
2163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2164 break;
2165 case Promote:
2166 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002167 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002168 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002169 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2170 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002171 break;
2172 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002173 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002174 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002175
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002177
Nate Begemanb942a3d2005-08-23 04:29:48 +00002178 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002179 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002180 case TargetLowering::Legal: break;
2181 case TargetLowering::Custom: {
2182 Tmp1 = TLI.LowerOperation(Result, DAG);
2183 if (Tmp1.Val) Result = Tmp1;
2184 break;
2185 }
Nate Begeman9373a812005-08-10 20:51:12 +00002186 case TargetLowering::Expand:
2187 if (Tmp1.getOpcode() == ISD::SETCC) {
2188 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2189 Tmp2, Tmp3,
2190 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2191 } else {
2192 Result = DAG.getSelectCC(Tmp1,
2193 DAG.getConstant(0, Tmp1.getValueType()),
2194 Tmp2, Tmp3, ISD::SETNE);
2195 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002196 break;
2197 case TargetLowering::Promote: {
2198 MVT::ValueType NVT =
2199 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2200 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002201 if (MVT::isVector(Tmp2.getValueType())) {
2202 ExtOp = ISD::BIT_CONVERT;
2203 TruncOp = ISD::BIT_CONVERT;
2204 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002205 ExtOp = ISD::ANY_EXTEND;
2206 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002207 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002208 ExtOp = ISD::FP_EXTEND;
2209 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002210 }
2211 // Promote each of the values to the new type.
2212 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2213 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2214 // Perform the larger operation, then round down.
2215 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2216 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2217 break;
2218 }
2219 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002220 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002221 case ISD::SELECT_CC: {
2222 Tmp1 = Node->getOperand(0); // LHS
2223 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002224 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2225 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002226 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002227
Nate Begeman750ac1b2006-02-01 07:19:44 +00002228 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2229
2230 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2231 // the LHS is a legal SETCC itself. In this case, we need to compare
2232 // the result against zero to select between true and false values.
2233 if (Tmp2.Val == 0) {
2234 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2235 CC = DAG.getCondCode(ISD::SETNE);
2236 }
2237 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2238
2239 // Everything is legal, see if we should expand this op or something.
2240 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2241 default: assert(0 && "This action is not supported yet!");
2242 case TargetLowering::Legal: break;
2243 case TargetLowering::Custom:
2244 Tmp1 = TLI.LowerOperation(Result, DAG);
2245 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002246 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002247 }
2248 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002249 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002250 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002251 Tmp1 = Node->getOperand(0);
2252 Tmp2 = Node->getOperand(1);
2253 Tmp3 = Node->getOperand(2);
2254 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2255
2256 // If we had to Expand the SetCC operands into a SELECT node, then it may
2257 // not always be possible to return a true LHS & RHS. In this case, just
2258 // return the value we legalized, returned in the LHS
2259 if (Tmp2.Val == 0) {
2260 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002261 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002262 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002263
Chris Lattner73e142f2006-01-30 22:43:50 +00002264 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002265 default: assert(0 && "Cannot handle this action for SETCC yet!");
2266 case TargetLowering::Custom:
2267 isCustom = true;
2268 // FALLTHROUGH.
2269 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002270 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002271 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002272 Tmp4 = TLI.LowerOperation(Result, DAG);
2273 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002274 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002275 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002276 case TargetLowering::Promote: {
2277 // First step, figure out the appropriate operation to use.
2278 // Allow SETCC to not be supported for all legal data types
2279 // Mostly this targets FP
2280 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002281 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002282
2283 // Scan for the appropriate larger type to use.
2284 while (1) {
2285 NewInTy = (MVT::ValueType)(NewInTy+1);
2286
2287 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2288 "Fell off of the edge of the integer world");
2289 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2290 "Fell off of the edge of the floating point world");
2291
2292 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002293 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002294 break;
2295 }
2296 if (MVT::isInteger(NewInTy))
2297 assert(0 && "Cannot promote Legal Integer SETCC yet");
2298 else {
2299 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2300 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2301 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002302 Tmp1 = LegalizeOp(Tmp1);
2303 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002305 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002306 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002307 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002308 case TargetLowering::Expand:
2309 // Expand a setcc node into a select_cc of the same condition, lhs, and
2310 // rhs that selects between const 1 (true) and const 0 (false).
2311 MVT::ValueType VT = Node->getValueType(0);
2312 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2313 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002314 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002315 break;
2316 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002317 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002318 case ISD::MEMSET:
2319 case ISD::MEMCPY:
2320 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2323
2324 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2325 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2326 case Expand: assert(0 && "Cannot expand a byte!");
2327 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002328 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002329 break;
2330 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002331 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002332 break;
2333 }
2334 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002335 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002336 }
Chris Lattner272455b2005-02-02 03:44:41 +00002337
2338 SDOperand Tmp4;
2339 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002340 case Expand: {
2341 // Length is too big, just take the lo-part of the length.
2342 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002343 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002344 break;
2345 }
Chris Lattnere5605212005-01-28 22:29:18 +00002346 case Legal:
2347 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002348 break;
2349 case Promote:
2350 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002351 break;
2352 }
2353
2354 SDOperand Tmp5;
2355 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2356 case Expand: assert(0 && "Cannot expand this yet!");
2357 case Legal:
2358 Tmp5 = LegalizeOp(Node->getOperand(4));
2359 break;
2360 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002361 Tmp5 = PromoteOp(Node->getOperand(4));
2362 break;
2363 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002364
2365 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2366 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002367 case TargetLowering::Custom:
2368 isCustom = true;
2369 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002370 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002372 if (isCustom) {
2373 Tmp1 = TLI.LowerOperation(Result, DAG);
2374 if (Tmp1.Val) Result = Tmp1;
2375 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002376 break;
2377 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002378 // Otherwise, the target does not support this operation. Lower the
2379 // operation to an explicit libcall as appropriate.
2380 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002381 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002382 TargetLowering::ArgListTy Args;
2383 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002384
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002385 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002386 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002387 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002388 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002389 // Extend the (previously legalized) ubyte argument to be an int value
2390 // for the call.
2391 if (Tmp3.getValueType() > MVT::i32)
2392 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2393 else
2394 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002395 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002396 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002397 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002398 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002399
2400 FnName = "memset";
2401 } else if (Node->getOpcode() == ISD::MEMCPY ||
2402 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002403 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002404 Entry.Node = Tmp2; Args.push_back(Entry);
2405 Entry.Node = Tmp3; Args.push_back(Entry);
2406 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002407 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2408 } else {
2409 assert(0 && "Unknown op!");
2410 }
Chris Lattner45982da2005-05-12 16:53:42 +00002411
Chris Lattnere1bd8222005-01-11 05:57:22 +00002412 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002413 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002414 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002415 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002416 break;
2417 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002418 }
2419 break;
2420 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002421
Chris Lattner5b359c62005-04-02 04:00:59 +00002422 case ISD::SHL_PARTS:
2423 case ISD::SRA_PARTS:
2424 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002425 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002426 bool Changed = false;
2427 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2428 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2429 Changed |= Ops.back() != Node->getOperand(i);
2430 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002431 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002432 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002433
Evan Cheng05a2d562006-01-09 18:31:59 +00002434 switch (TLI.getOperationAction(Node->getOpcode(),
2435 Node->getValueType(0))) {
2436 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002437 case TargetLowering::Legal: break;
2438 case TargetLowering::Custom:
2439 Tmp1 = TLI.LowerOperation(Result, DAG);
2440 if (Tmp1.Val) {
2441 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002442 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002443 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002444 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2445 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002446 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002447 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002448 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002449 return RetVal;
2450 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002451 break;
2452 }
2453
Chris Lattner2c8086f2005-04-02 05:00:07 +00002454 // Since these produce multiple values, make sure to remember that we
2455 // legalized all of them.
2456 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2457 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2458 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002459 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002460
2461 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002462 case ISD::ADD:
2463 case ISD::SUB:
2464 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002465 case ISD::MULHS:
2466 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002467 case ISD::UDIV:
2468 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002469 case ISD::AND:
2470 case ISD::OR:
2471 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002472 case ISD::SHL:
2473 case ISD::SRL:
2474 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002475 case ISD::FADD:
2476 case ISD::FSUB:
2477 case ISD::FMUL:
2478 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002479 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002480 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2481 case Expand: assert(0 && "Not possible");
2482 case Legal:
2483 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2484 break;
2485 case Promote:
2486 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2487 break;
2488 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002489
2490 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002491
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002492 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002493 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002494 case TargetLowering::Legal: break;
2495 case TargetLowering::Custom:
2496 Tmp1 = TLI.LowerOperation(Result, DAG);
2497 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002498 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002499 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002500 if (Node->getValueType(0) == MVT::i32) {
2501 switch (Node->getOpcode()) {
2502 default: assert(0 && "Do not know how to expand this integer BinOp!");
2503 case ISD::UDIV:
2504 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002505 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2506 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002507 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002508 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002509 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002510 };
2511 break;
2512 }
2513
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002514 assert(MVT::isVector(Node->getValueType(0)) &&
2515 "Cannot expand this binary operator!");
2516 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002517 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002518 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002519 MVT::ValueType PtrVT = TLI.getPointerTy();
2520 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2521 i != e; ++i) {
2522 SDOperand Idx = DAG.getConstant(i, PtrVT);
2523 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2524 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2525 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2526 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002527 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2528 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002529 break;
2530 }
Evan Chengcc987612006-04-12 21:20:24 +00002531 case TargetLowering::Promote: {
2532 switch (Node->getOpcode()) {
2533 default: assert(0 && "Do not know how to promote this BinOp!");
2534 case ISD::AND:
2535 case ISD::OR:
2536 case ISD::XOR: {
2537 MVT::ValueType OVT = Node->getValueType(0);
2538 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2539 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2540 // Bit convert each of the values to the new type.
2541 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2542 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2543 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2544 // Bit convert the result back the original type.
2545 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2546 break;
2547 }
2548 }
2549 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002550 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002551 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002552
2553 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2554 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2555 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2556 case Expand: assert(0 && "Not possible");
2557 case Legal:
2558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2559 break;
2560 case Promote:
2561 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2562 break;
2563 }
2564
2565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2566
2567 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2568 default: assert(0 && "Operation not supported");
2569 case TargetLowering::Custom:
2570 Tmp1 = TLI.LowerOperation(Result, DAG);
2571 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002572 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002573 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002574 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002575 // If this target supports fabs/fneg natively and select is cheap,
2576 // do this efficiently.
2577 if (!TLI.isSelectExpensive() &&
2578 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2579 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002580 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002581 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002582 // Get the sign bit of the RHS.
2583 MVT::ValueType IVT =
2584 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2585 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2586 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2587 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2588 // Get the absolute value of the result.
2589 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2590 // Select between the nabs and abs value based on the sign bit of
2591 // the input.
2592 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2593 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2594 AbsVal),
2595 AbsVal);
2596 Result = LegalizeOp(Result);
2597 break;
2598 }
2599
2600 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002601 MVT::ValueType NVT =
2602 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2603 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2604 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2605 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002606 break;
2607 }
Evan Cheng912095b2007-01-04 21:56:39 +00002608 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002609 break;
2610
Nate Begeman551bf3f2006-02-17 05:43:56 +00002611 case ISD::ADDC:
2612 case ISD::SUBC:
2613 Tmp1 = LegalizeOp(Node->getOperand(0));
2614 Tmp2 = LegalizeOp(Node->getOperand(1));
2615 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2616 // Since this produces two values, make sure to remember that we legalized
2617 // both of them.
2618 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2619 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2620 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002621
Nate Begeman551bf3f2006-02-17 05:43:56 +00002622 case ISD::ADDE:
2623 case ISD::SUBE:
2624 Tmp1 = LegalizeOp(Node->getOperand(0));
2625 Tmp2 = LegalizeOp(Node->getOperand(1));
2626 Tmp3 = LegalizeOp(Node->getOperand(2));
2627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2628 // Since this produces two values, make sure to remember that we legalized
2629 // both of them.
2630 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2631 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2632 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002633
Nate Begeman419f8b62005-10-18 00:27:41 +00002634 case ISD::BUILD_PAIR: {
2635 MVT::ValueType PairTy = Node->getValueType(0);
2636 // TODO: handle the case where the Lo and Hi operands are not of legal type
2637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2638 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2639 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002640 case TargetLowering::Promote:
2641 case TargetLowering::Custom:
2642 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002643 case TargetLowering::Legal:
2644 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2645 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2646 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002647 case TargetLowering::Expand:
2648 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2649 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2650 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2651 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2652 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002653 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002654 break;
2655 }
2656 break;
2657 }
2658
Nate Begemanc105e192005-04-06 00:23:54 +00002659 case ISD::UREM:
2660 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002661 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002662 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2663 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002664
Nate Begemanc105e192005-04-06 00:23:54 +00002665 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002666 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2667 case TargetLowering::Custom:
2668 isCustom = true;
2669 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002670 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002671 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002672 if (isCustom) {
2673 Tmp1 = TLI.LowerOperation(Result, DAG);
2674 if (Tmp1.Val) Result = Tmp1;
2675 }
Nate Begemanc105e192005-04-06 00:23:54 +00002676 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002677 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002678 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002679 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002680 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002681 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2682 TargetLowering::Legal) {
2683 // X % Y -> X-X/Y*Y
2684 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002685 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002686 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2687 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2688 } else {
2689 assert(Node->getValueType(0) == MVT::i32 &&
2690 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002691 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2692 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002693 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002694 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002695 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002696 } else {
2697 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002698 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2699 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002700 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002701 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2702 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002703 }
2704 break;
2705 }
2706 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002707 case ISD::VAARG: {
2708 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2709 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2710
Chris Lattner5c62f332006-01-28 07:42:08 +00002711 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002712 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2713 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002714 case TargetLowering::Custom:
2715 isCustom = true;
2716 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002717 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2719 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 Tmp1 = Result.getValue(1);
2721
2722 if (isCustom) {
2723 Tmp2 = TLI.LowerOperation(Result, DAG);
2724 if (Tmp2.Val) {
2725 Result = LegalizeOp(Tmp2);
2726 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2727 }
2728 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002729 break;
2730 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002731 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002732 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002733 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002734 // Increment the pointer, VAList, to the next vaarg
2735 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2736 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2737 TLI.getPointerTy()));
2738 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002739 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2740 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002741 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002742 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002743 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002744 Result = LegalizeOp(Result);
2745 break;
2746 }
2747 }
2748 // Since VAARG produces two values, make sure to remember that we
2749 // legalized both of them.
2750 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002751 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2752 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002753 }
2754
2755 case ISD::VACOPY:
2756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2757 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2758 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2759
2760 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2761 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002762 case TargetLowering::Custom:
2763 isCustom = true;
2764 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002765 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2767 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002768 if (isCustom) {
2769 Tmp1 = TLI.LowerOperation(Result, DAG);
2770 if (Tmp1.Val) Result = Tmp1;
2771 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002772 break;
2773 case TargetLowering::Expand:
2774 // This defaults to loading a pointer from the input and storing it to the
2775 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002776 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002777 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002778 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2779 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002780 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2781 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002782 break;
2783 }
2784 break;
2785
2786 case ISD::VAEND:
2787 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2788 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2789
2790 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2791 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002792 case TargetLowering::Custom:
2793 isCustom = true;
2794 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002795 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002796 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002797 if (isCustom) {
2798 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2799 if (Tmp1.Val) Result = Tmp1;
2800 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002801 break;
2802 case TargetLowering::Expand:
2803 Result = Tmp1; // Default to a no-op, return the chain
2804 break;
2805 }
2806 break;
2807
2808 case ISD::VASTART:
2809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2810 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2811
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2813
Nate Begemanacc398c2006-01-25 18:21:52 +00002814 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2815 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002816 case TargetLowering::Legal: break;
2817 case TargetLowering::Custom:
2818 Tmp1 = TLI.LowerOperation(Result, DAG);
2819 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002820 break;
2821 }
2822 break;
2823
Nate Begeman35ef9132006-01-11 21:21:00 +00002824 case ISD::ROTL:
2825 case ISD::ROTR:
2826 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2827 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002829 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2830 default:
2831 assert(0 && "ROTL/ROTR legalize operation not supported");
2832 break;
2833 case TargetLowering::Legal:
2834 break;
2835 case TargetLowering::Custom:
2836 Tmp1 = TLI.LowerOperation(Result, DAG);
2837 if (Tmp1.Val) Result = Tmp1;
2838 break;
2839 case TargetLowering::Promote:
2840 assert(0 && "Do not know how to promote ROTL/ROTR");
2841 break;
2842 case TargetLowering::Expand:
2843 assert(0 && "Do not know how to expand ROTL/ROTR");
2844 break;
2845 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002846 break;
2847
Nate Begemand88fc032006-01-14 03:14:10 +00002848 case ISD::BSWAP:
2849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2850 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002851 case TargetLowering::Custom:
2852 assert(0 && "Cannot custom legalize this yet!");
2853 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002854 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002855 break;
2856 case TargetLowering::Promote: {
2857 MVT::ValueType OVT = Tmp1.getValueType();
2858 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002859 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002860
Chris Lattner456a93a2006-01-28 07:39:30 +00002861 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2862 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2863 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2864 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2865 break;
2866 }
2867 case TargetLowering::Expand:
2868 Result = ExpandBSWAP(Tmp1);
2869 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002870 }
2871 break;
2872
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002873 case ISD::CTPOP:
2874 case ISD::CTTZ:
2875 case ISD::CTLZ:
2876 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2877 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00002878 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002879 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002880 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00002881 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
2882 TargetLowering::Custom) {
2883 Tmp1 = TLI.LowerOperation(Result, DAG);
2884 if (Tmp1.Val) {
2885 Result = Tmp1;
2886 }
2887 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002888 break;
2889 case TargetLowering::Promote: {
2890 MVT::ValueType OVT = Tmp1.getValueType();
2891 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002892
2893 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002894 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2895 // Perform the larger operation, then subtract if needed.
2896 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002897 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002898 case ISD::CTPOP:
2899 Result = Tmp1;
2900 break;
2901 case ISD::CTTZ:
2902 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002903 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002904 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002905 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002906 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00002907 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002908 break;
2909 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002910 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002911 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002912 DAG.getConstant(MVT::getSizeInBits(NVT) -
2913 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002914 break;
2915 }
2916 break;
2917 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002918 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002919 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002920 break;
2921 }
2922 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002923
Chris Lattner2c8086f2005-04-02 05:00:07 +00002924 // Unary operators
2925 case ISD::FABS:
2926 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002927 case ISD::FSQRT:
2928 case ISD::FSIN:
2929 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002930 Tmp1 = LegalizeOp(Node->getOperand(0));
2931 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002932 case TargetLowering::Promote:
2933 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002934 isCustom = true;
2935 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002936 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002937 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002938 if (isCustom) {
2939 Tmp1 = TLI.LowerOperation(Result, DAG);
2940 if (Tmp1.Val) Result = Tmp1;
2941 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002942 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002943 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002944 switch (Node->getOpcode()) {
2945 default: assert(0 && "Unreachable!");
2946 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002947 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2948 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002949 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002950 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002951 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002952 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2953 MVT::ValueType VT = Node->getValueType(0);
2954 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002955 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002956 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2957 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002958 break;
2959 }
2960 case ISD::FSQRT:
2961 case ISD::FSIN:
2962 case ISD::FCOS: {
2963 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002964 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002965 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002966 case ISD::FSQRT:
2967 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2968 break;
2969 case ISD::FSIN:
2970 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2971 break;
2972 case ISD::FCOS:
2973 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2974 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002975 default: assert(0 && "Unreachable!");
2976 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002977 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002978 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2979 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002980 break;
2981 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002982 }
2983 break;
2984 }
2985 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002986 case ISD::FPOWI: {
2987 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002988 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2989 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002990 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002991 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2992 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002993 break;
2994 }
Chris Lattner35481892005-12-23 00:16:34 +00002995 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002996 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002997 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00002998 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2999 // The input has to be a vector type, we have to either scalarize it, pack
3000 // it, or convert it based on whether the input vector type is legal.
3001 SDNode *InVal = Node->getOperand(0).Val;
3002 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3003 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3004
3005 // Figure out if there is a simple type corresponding to this Vector
3006 // type. If so, convert to the vector type.
3007 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3008 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003009 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003010 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3011 LegalizeOp(Node->getOperand(0)));
3012 break;
3013 } else if (NumElems == 1) {
3014 // Turn this into a bit convert of the scalar input.
3015 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3016 ScalarizeVectorOp(Node->getOperand(0)));
3017 break;
3018 } else {
3019 // FIXME: UNIMP! Store then reload
3020 assert(0 && "Cast from unsupported vector type not implemented yet!");
3021 }
Chris Lattner67993f72006-01-23 07:30:46 +00003022 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003023 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3024 Node->getOperand(0).getValueType())) {
3025 default: assert(0 && "Unknown operation action!");
3026 case TargetLowering::Expand:
3027 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3028 break;
3029 case TargetLowering::Legal:
3030 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003031 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003032 break;
3033 }
3034 }
3035 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003036
Chris Lattner2c8086f2005-04-02 05:00:07 +00003037 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003038 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003039 case ISD::UINT_TO_FP: {
3040 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003041 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3042 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003043 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003044 Node->getOperand(0).getValueType())) {
3045 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003046 case TargetLowering::Custom:
3047 isCustom = true;
3048 // FALLTHROUGH
3049 case TargetLowering::Legal:
3050 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003051 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003052 if (isCustom) {
3053 Tmp1 = TLI.LowerOperation(Result, DAG);
3054 if (Tmp1.Val) Result = Tmp1;
3055 }
3056 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003057 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003058 Result = ExpandLegalINT_TO_FP(isSigned,
3059 LegalizeOp(Node->getOperand(0)),
3060 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003061 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003062 case TargetLowering::Promote:
3063 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3064 Node->getValueType(0),
3065 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003066 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003067 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003069 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003070 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3071 Node->getValueType(0), Node->getOperand(0));
3072 break;
3073 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003074 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003075 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003076 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3077 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003078 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003079 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3080 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003081 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003082 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3083 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003084 break;
3085 }
3086 break;
3087 }
3088 case ISD::TRUNCATE:
3089 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3090 case Legal:
3091 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003092 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003093 break;
3094 case Expand:
3095 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3096
3097 // Since the result is legal, we should just be able to truncate the low
3098 // part of the source.
3099 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3100 break;
3101 case Promote:
3102 Result = PromoteOp(Node->getOperand(0));
3103 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3104 break;
3105 }
3106 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003107
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003108 case ISD::FP_TO_SINT:
3109 case ISD::FP_TO_UINT:
3110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3111 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003112 Tmp1 = LegalizeOp(Node->getOperand(0));
3113
Chris Lattner1618beb2005-07-29 00:11:56 +00003114 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3115 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003116 case TargetLowering::Custom:
3117 isCustom = true;
3118 // FALLTHROUGH
3119 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003120 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003121 if (isCustom) {
3122 Tmp1 = TLI.LowerOperation(Result, DAG);
3123 if (Tmp1.Val) Result = Tmp1;
3124 }
3125 break;
3126 case TargetLowering::Promote:
3127 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3128 Node->getOpcode() == ISD::FP_TO_SINT);
3129 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003130 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003131 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3132 SDOperand True, False;
3133 MVT::ValueType VT = Node->getOperand(0).getValueType();
3134 MVT::ValueType NVT = Node->getValueType(0);
3135 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3136 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3137 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3138 Node->getOperand(0), Tmp2, ISD::SETLT);
3139 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3140 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003141 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003142 Tmp2));
3143 False = DAG.getNode(ISD::XOR, NVT, False,
3144 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003145 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3146 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003147 } else {
3148 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3149 }
3150 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003151 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003152 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003153 case Expand: {
3154 // Convert f32 / f64 to i32 / i64.
3155 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003156 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003157 switch (Node->getOpcode()) {
3158 case ISD::FP_TO_SINT:
3159 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003160 LC = (VT == MVT::i32)
3161 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003162 else
Evan Cheng56966222007-01-12 02:11:51 +00003163 LC = (VT == MVT::i32)
3164 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003165 break;
3166 case ISD::FP_TO_UINT:
3167 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003168 LC = (VT == MVT::i32)
3169 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003170 else
Evan Cheng56966222007-01-12 02:11:51 +00003171 LC = (VT == MVT::i32)
3172 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003173 break;
3174 default: assert(0 && "Unreachable!");
3175 }
3176 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003177 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3178 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003179 break;
3180 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003181 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003182 Tmp1 = PromoteOp(Node->getOperand(0));
3183 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3184 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003185 break;
3186 }
3187 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003188
Dale Johannesen849f2142007-07-03 00:53:03 +00003189 case ISD::FP_ROUND:
3190 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3191 TargetLowering::Expand) {
3192 // The only way we can lower this is to turn it into a TRUNCSTORE,
3193 // EXTLOAD pair, targetting a temporary location (a stack slot).
3194
3195 // NOTE: there is a choice here between constantly creating new stack
3196 // slots and always reusing the same one. We currently always create
3197 // new ones, as reuse may inhibit scheduling.
3198 MVT::ValueType VT = Op.getValueType(); // 32
3199 const Type *Ty = MVT::getTypeForValueType(VT);
3200 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3201 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3202 MachineFunction &MF = DAG.getMachineFunction();
3203 int SSFI =
3204 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3205 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3206 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3207 StackSlot, NULL, 0, VT);
3208 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3209 break;
3210 }
3211 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003212 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003213 case ISD::ZERO_EXTEND:
3214 case ISD::SIGN_EXTEND:
3215 case ISD::FP_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003216 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003217 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003218 case Legal:
3219 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003220 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003221 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003222 case Promote:
3223 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003224 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003225 Tmp1 = PromoteOp(Node->getOperand(0));
3226 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003227 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003228 case ISD::ZERO_EXTEND:
3229 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003230 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003231 Result = DAG.getZeroExtendInReg(Result,
3232 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003233 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003234 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003235 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003236 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003237 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003238 Result,
3239 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003240 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003241 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003242 Result = PromoteOp(Node->getOperand(0));
3243 if (Result.getValueType() != Op.getValueType())
3244 // Dynamically dead while we have only 2 FP types.
3245 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3246 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003247 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003248 Result = PromoteOp(Node->getOperand(0));
3249 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3250 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003251 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003252 }
3253 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003254 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003255 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003256 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003257 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003258
3259 // If this operation is not supported, convert it to a shl/shr or load/store
3260 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003261 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3262 default: assert(0 && "This action not supported for this op yet!");
3263 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003264 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003265 break;
3266 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003267 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003268 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003269 // NOTE: we could fall back on load/store here too for targets without
3270 // SAR. However, it is doubtful that any exist.
3271 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3272 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003273 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003274 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3275 Node->getOperand(0), ShiftCst);
3276 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3277 Result, ShiftCst);
3278 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003279 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003280 // EXTLOAD pair, targetting a temporary location (a stack slot).
3281
3282 // NOTE: there is a choice here between constantly creating new stack
3283 // slots and always reusing the same one. We currently always create
3284 // new ones, as reuse may inhibit scheduling.
3285 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003286 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003287 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003288 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003289 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003290 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003291 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003292 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3293 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003294 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003295 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003296 } else {
3297 assert(0 && "Unknown op");
3298 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003299 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003300 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003301 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003302 }
Duncan Sands36397f52007-07-27 12:58:54 +00003303 case ISD::ADJUST_TRAMP: {
3304 Tmp1 = LegalizeOp(Node->getOperand(0));
3305 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3306 default: assert(0 && "This action is not supported yet!");
3307 case TargetLowering::Custom:
3308 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3309 Result = TLI.LowerOperation(Result, DAG);
3310 if (Result.Val) break;
3311 // FALL THROUGH
3312 case TargetLowering::Expand:
3313 Result = Tmp1;
3314 break;
3315 }
3316 break;
3317 }
3318 case ISD::TRAMPOLINE: {
3319 SDOperand Ops[6];
3320 for (unsigned i = 0; i != 6; ++i)
3321 Ops[i] = LegalizeOp(Node->getOperand(i));
3322 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3323 // The only option for this node is to custom lower it.
3324 Result = TLI.LowerOperation(Result, DAG);
3325 assert(Result.Val && "Should always custom lower!");
3326 break;
3327 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003328 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003329
Chris Lattner4ddd2832006-04-08 04:13:17 +00003330 assert(Result.getValueType() == Op.getValueType() &&
3331 "Bad legalization!");
3332
Chris Lattner456a93a2006-01-28 07:39:30 +00003333 // Make sure that the generated code is itself legal.
3334 if (Result != Op)
3335 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003336
Chris Lattner45982da2005-05-12 16:53:42 +00003337 // Note that LegalizeOp may be reentered even from single-use nodes, which
3338 // means that we always must cache transformed nodes.
3339 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003340 return Result;
3341}
3342
Chris Lattner8b6fa222005-01-15 22:16:26 +00003343/// PromoteOp - Given an operation that produces a value in an invalid type,
3344/// promote it to compute the value into a larger type. The produced value will
3345/// have the correct bits for the low portion of the register, but no guarantee
3346/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003347SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3348 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003349 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003350 assert(getTypeAction(VT) == Promote &&
3351 "Caller should expand or legalize operands that are not promotable!");
3352 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3353 "Cannot promote to smaller type!");
3354
Chris Lattner03c85462005-01-15 05:21:40 +00003355 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003356 SDOperand Result;
3357 SDNode *Node = Op.Val;
3358
Chris Lattner40030bf2007-02-04 01:17:38 +00003359 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003360 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003361
Chris Lattner03c85462005-01-15 05:21:40 +00003362 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003363 case ISD::CopyFromReg:
3364 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003365 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003366#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003367 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003368#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003369 assert(0 && "Do not know how to promote this operator!");
3370 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003371 case ISD::UNDEF:
3372 Result = DAG.getNode(ISD::UNDEF, NVT);
3373 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003374 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003375 if (VT != MVT::i1)
3376 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3377 else
3378 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003379 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3380 break;
3381 case ISD::ConstantFP:
3382 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3383 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3384 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003385
Chris Lattner82fbfb62005-01-18 02:59:52 +00003386 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003387 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003388 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3389 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003390 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003391
Chris Lattner03c85462005-01-15 05:21:40 +00003392 case ISD::TRUNCATE:
3393 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3394 case Legal:
3395 Result = LegalizeOp(Node->getOperand(0));
3396 assert(Result.getValueType() >= NVT &&
3397 "This truncation doesn't make sense!");
3398 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3399 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3400 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003401 case Promote:
3402 // The truncation is not required, because we don't guarantee anything
3403 // about high bits anyway.
3404 Result = PromoteOp(Node->getOperand(0));
3405 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003406 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003407 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3408 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003409 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003410 }
3411 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003412 case ISD::SIGN_EXTEND:
3413 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003414 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003415 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3416 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3417 case Legal:
3418 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003419 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003420 break;
3421 case Promote:
3422 // Promote the reg if it's smaller.
3423 Result = PromoteOp(Node->getOperand(0));
3424 // The high bits are not guaranteed to be anything. Insert an extend.
3425 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003426 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003427 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003428 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003429 Result = DAG.getZeroExtendInReg(Result,
3430 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003431 break;
3432 }
3433 break;
Chris Lattner35481892005-12-23 00:16:34 +00003434 case ISD::BIT_CONVERT:
3435 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3436 Result = PromoteOp(Result);
3437 break;
3438
Chris Lattner8b6fa222005-01-15 22:16:26 +00003439 case ISD::FP_EXTEND:
3440 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3441 case ISD::FP_ROUND:
3442 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3443 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3444 case Promote: assert(0 && "Unreachable with 2 FP types!");
3445 case Legal:
3446 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003447 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003448 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003449 break;
3450 }
3451 break;
3452
3453 case ISD::SINT_TO_FP:
3454 case ISD::UINT_TO_FP:
3455 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3456 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003457 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003458 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003459 break;
3460
3461 case Promote:
3462 Result = PromoteOp(Node->getOperand(0));
3463 if (Node->getOpcode() == ISD::SINT_TO_FP)
3464 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003465 Result,
3466 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003467 else
Chris Lattner23993562005-04-13 02:38:47 +00003468 Result = DAG.getZeroExtendInReg(Result,
3469 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003470 // No extra round required here.
3471 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003472 break;
3473 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003474 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3475 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003476 // Round if we cannot tolerate excess precision.
3477 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003478 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3479 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003480 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003481 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003482 break;
3483
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003484 case ISD::SIGN_EXTEND_INREG:
3485 Result = PromoteOp(Node->getOperand(0));
3486 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3487 Node->getOperand(1));
3488 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003489 case ISD::FP_TO_SINT:
3490 case ISD::FP_TO_UINT:
3491 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3492 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003493 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003494 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003495 break;
3496 case Promote:
3497 // The input result is prerounded, so we don't have to do anything
3498 // special.
3499 Tmp1 = PromoteOp(Node->getOperand(0));
3500 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003501 }
Nate Begemand2558e32005-08-14 01:20:53 +00003502 // If we're promoting a UINT to a larger size, check to see if the new node
3503 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3504 // we can use that instead. This allows us to generate better code for
3505 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3506 // legal, such as PowerPC.
3507 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003508 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003509 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3510 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003511 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3512 } else {
3513 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3514 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003515 break;
3516
Chris Lattner2c8086f2005-04-02 05:00:07 +00003517 case ISD::FABS:
3518 case ISD::FNEG:
3519 Tmp1 = PromoteOp(Node->getOperand(0));
3520 assert(Tmp1.getValueType() == NVT);
3521 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3522 // NOTE: we do not have to do any extra rounding here for
3523 // NoExcessFPPrecision, because we know the input will have the appropriate
3524 // precision, and these operations don't modify precision at all.
3525 break;
3526
Chris Lattnerda6ba872005-04-28 21:44:33 +00003527 case ISD::FSQRT:
3528 case ISD::FSIN:
3529 case ISD::FCOS:
3530 Tmp1 = PromoteOp(Node->getOperand(0));
3531 assert(Tmp1.getValueType() == NVT);
3532 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003533 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003534 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3535 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003536 break;
3537
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003538 case ISD::FPOWI: {
3539 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3540 // directly as well, which may be better.
3541 Tmp1 = PromoteOp(Node->getOperand(0));
3542 assert(Tmp1.getValueType() == NVT);
3543 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3544 if (NoExcessFPPrecision)
3545 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3546 DAG.getValueType(VT));
3547 break;
3548 }
3549
Chris Lattner03c85462005-01-15 05:21:40 +00003550 case ISD::AND:
3551 case ISD::OR:
3552 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003553 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003554 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003555 case ISD::MUL:
3556 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003557 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003558 // that too is okay if they are integer operations.
3559 Tmp1 = PromoteOp(Node->getOperand(0));
3560 Tmp2 = PromoteOp(Node->getOperand(1));
3561 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3562 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003563 break;
3564 case ISD::FADD:
3565 case ISD::FSUB:
3566 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003567 Tmp1 = PromoteOp(Node->getOperand(0));
3568 Tmp2 = PromoteOp(Node->getOperand(1));
3569 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3570 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3571
3572 // Floating point operations will give excess precision that we may not be
3573 // able to tolerate. If we DO allow excess precision, just leave it,
3574 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003575 // FIXME: Why would we need to round FP ops more than integer ones?
3576 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003577 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003578 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3579 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003580 break;
3581
Chris Lattner8b6fa222005-01-15 22:16:26 +00003582 case ISD::SDIV:
3583 case ISD::SREM:
3584 // These operators require that their input be sign extended.
3585 Tmp1 = PromoteOp(Node->getOperand(0));
3586 Tmp2 = PromoteOp(Node->getOperand(1));
3587 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003588 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3589 DAG.getValueType(VT));
3590 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3591 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003592 }
3593 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3594
3595 // Perform FP_ROUND: this is probably overly pessimistic.
3596 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003597 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3598 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003599 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003600 case ISD::FDIV:
3601 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003602 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003603 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003604 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3605 case Legal:
3606 Tmp1 = LegalizeOp(Node->getOperand(0));
3607 break;
3608 case Promote:
3609 Tmp1 = PromoteOp(Node->getOperand(0));
3610 break;
3611 case Expand:
3612 assert(0 && "not implemented");
3613 }
3614 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3615 case Legal:
3616 Tmp2 = LegalizeOp(Node->getOperand(1));
3617 break;
3618 case Promote:
3619 Tmp2 = PromoteOp(Node->getOperand(1));
3620 break;
3621 case Expand:
3622 assert(0 && "not implemented");
3623 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003624 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3625
3626 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003627 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003628 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3629 DAG.getValueType(VT));
3630 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003631
3632 case ISD::UDIV:
3633 case ISD::UREM:
3634 // These operators require that their input be zero extended.
3635 Tmp1 = PromoteOp(Node->getOperand(0));
3636 Tmp2 = PromoteOp(Node->getOperand(1));
3637 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003638 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3639 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003640 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3641 break;
3642
3643 case ISD::SHL:
3644 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003645 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003646 break;
3647 case ISD::SRA:
3648 // The input value must be properly sign extended.
3649 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003650 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3651 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003652 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003653 break;
3654 case ISD::SRL:
3655 // The input value must be properly zero extended.
3656 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003657 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003658 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003659 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003660
3661 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003662 Tmp1 = Node->getOperand(0); // Get the chain.
3663 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003664 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3665 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3666 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3667 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003668 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003669 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003670 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003671 // Increment the pointer, VAList, to the next vaarg
3672 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3673 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3674 TLI.getPointerTy()));
3675 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003676 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3677 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003678 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003679 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003680 }
3681 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003682 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003683 break;
3684
Evan Cheng466685d2006-10-09 20:57:25 +00003685 case ISD::LOAD: {
3686 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003687 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3688 ? ISD::EXTLOAD : LD->getExtensionType();
3689 Result = DAG.getExtLoad(ExtType, NVT,
3690 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003691 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003692 LD->getLoadedVT(),
3693 LD->isVolatile(),
3694 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003695 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003696 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003697 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003698 }
Chris Lattner03c85462005-01-15 05:21:40 +00003699 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003700 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3701 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003702 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003703 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003704 case ISD::SELECT_CC:
3705 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3706 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3707 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003708 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003709 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003710 case ISD::BSWAP:
3711 Tmp1 = Node->getOperand(0);
3712 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3713 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3714 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003715 DAG.getConstant(MVT::getSizeInBits(NVT) -
3716 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003717 TLI.getShiftAmountTy()));
3718 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003719 case ISD::CTPOP:
3720 case ISD::CTTZ:
3721 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003722 // Zero extend the argument
3723 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003724 // Perform the larger operation, then subtract if needed.
3725 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003726 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003727 case ISD::CTPOP:
3728 Result = Tmp1;
3729 break;
3730 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003731 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003732 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003733 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3734 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003735 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003736 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003737 break;
3738 case ISD::CTLZ:
3739 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003740 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003741 DAG.getConstant(MVT::getSizeInBits(NVT) -
3742 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003743 break;
3744 }
3745 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003746 case ISD::EXTRACT_SUBVECTOR:
3747 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003748 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003749 case ISD::EXTRACT_VECTOR_ELT:
3750 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3751 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003752 }
3753
3754 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003755
3756 // Make sure the result is itself legal.
3757 Result = LegalizeOp(Result);
3758
3759 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003760 AddPromotedOperand(Op, Result);
3761 return Result;
3762}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003763
Dan Gohman7f321562007-06-25 16:23:39 +00003764/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3765/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3766/// based on the vector type. The return type of this matches the element type
3767/// of the vector, which may not be legal for the target.
3768SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003769 // We know that operand #0 is the Vec vector. If the index is a constant
3770 // or if the invec is a supported hardware type, we can use it. Otherwise,
3771 // lower to a store then an indexed load.
3772 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003773 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003774
3775 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003776 MVT::ValueType TVT = InVal->getValueType(0);
3777 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003778
Dan Gohman7f321562007-06-25 16:23:39 +00003779 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3780 default: assert(0 && "This action is not supported yet!");
3781 case TargetLowering::Custom: {
3782 Vec = LegalizeOp(Vec);
3783 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3784 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3785 if (Tmp3.Val)
3786 return Tmp3;
3787 break;
3788 }
3789 case TargetLowering::Legal:
3790 if (isTypeLegal(TVT)) {
3791 Vec = LegalizeOp(Vec);
3792 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003793 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003794 }
3795 break;
3796 case TargetLowering::Expand:
3797 break;
3798 }
3799
3800 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003801 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003802 Op = ScalarizeVectorOp(Vec);
3803 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3804 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003805 SDOperand Lo, Hi;
3806 SplitVectorOp(Vec, Lo, Hi);
3807 if (CIdx->getValue() < NumElems/2) {
3808 Vec = Lo;
3809 } else {
3810 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003811 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3812 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003813 }
Dan Gohman7f321562007-06-25 16:23:39 +00003814
Chris Lattner15972212006-03-31 17:55:51 +00003815 // It's now an extract from the appropriate high or low part. Recurse.
3816 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003817 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003818 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003819 // Store the value to a temporary stack slot, then LOAD the scalar
3820 // element back out.
3821 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3822 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3823
3824 // Add the offset to the index.
3825 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3826 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3827 DAG.getConstant(EltSize, Idx.getValueType()));
3828 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3829
3830 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003831 }
Dan Gohman7f321562007-06-25 16:23:39 +00003832 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003833}
3834
Dan Gohman7f321562007-06-25 16:23:39 +00003835/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003836/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003837SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003838 // We know that operand #0 is the Vec vector. For now we assume the index
3839 // is a constant and that the extracted result is a supported hardware type.
3840 SDOperand Vec = Op.getOperand(0);
3841 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3842
Dan Gohman7f321562007-06-25 16:23:39 +00003843 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003844
3845 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3846 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003847 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003848 }
3849
3850 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3851 SDOperand Lo, Hi;
3852 SplitVectorOp(Vec, Lo, Hi);
3853 if (CIdx->getValue() < NumElems/2) {
3854 Vec = Lo;
3855 } else {
3856 Vec = Hi;
3857 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3858 }
3859
3860 // It's now an extract from the appropriate high or low part. Recurse.
3861 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003862 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003863}
3864
Nate Begeman750ac1b2006-02-01 07:19:44 +00003865/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3866/// with condition CC on the current target. This usually involves legalizing
3867/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3868/// there may be no choice but to create a new SetCC node to represent the
3869/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3870/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3871void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3872 SDOperand &RHS,
3873 SDOperand &CC) {
3874 SDOperand Tmp1, Tmp2, Result;
3875
3876 switch (getTypeAction(LHS.getValueType())) {
3877 case Legal:
3878 Tmp1 = LegalizeOp(LHS); // LHS
3879 Tmp2 = LegalizeOp(RHS); // RHS
3880 break;
3881 case Promote:
3882 Tmp1 = PromoteOp(LHS); // LHS
3883 Tmp2 = PromoteOp(RHS); // RHS
3884
3885 // If this is an FP compare, the operands have already been extended.
3886 if (MVT::isInteger(LHS.getValueType())) {
3887 MVT::ValueType VT = LHS.getValueType();
3888 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3889
3890 // Otherwise, we have to insert explicit sign or zero extends. Note
3891 // that we could insert sign extends for ALL conditions, but zero extend
3892 // is cheaper on many machines (an AND instead of two shifts), so prefer
3893 // it.
3894 switch (cast<CondCodeSDNode>(CC)->get()) {
3895 default: assert(0 && "Unknown integer comparison!");
3896 case ISD::SETEQ:
3897 case ISD::SETNE:
3898 case ISD::SETUGE:
3899 case ISD::SETUGT:
3900 case ISD::SETULE:
3901 case ISD::SETULT:
3902 // ALL of these operations will work if we either sign or zero extend
3903 // the operands (including the unsigned comparisons!). Zero extend is
3904 // usually a simpler/cheaper operation, so prefer it.
3905 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3906 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3907 break;
3908 case ISD::SETGE:
3909 case ISD::SETGT:
3910 case ISD::SETLT:
3911 case ISD::SETLE:
3912 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3913 DAG.getValueType(VT));
3914 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3915 DAG.getValueType(VT));
3916 break;
3917 }
3918 }
3919 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003920 case Expand: {
3921 MVT::ValueType VT = LHS.getValueType();
3922 if (VT == MVT::f32 || VT == MVT::f64) {
3923 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003924 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003925 switch (cast<CondCodeSDNode>(CC)->get()) {
3926 case ISD::SETEQ:
3927 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003928 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003929 break;
3930 case ISD::SETNE:
3931 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003932 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003933 break;
3934 case ISD::SETGE:
3935 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003936 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003937 break;
3938 case ISD::SETLT:
3939 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003940 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003941 break;
3942 case ISD::SETLE:
3943 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003944 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003945 break;
3946 case ISD::SETGT:
3947 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003948 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003949 break;
3950 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003951 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3952 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003953 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003954 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003955 break;
3956 default:
Evan Cheng56966222007-01-12 02:11:51 +00003957 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003958 switch (cast<CondCodeSDNode>(CC)->get()) {
3959 case ISD::SETONE:
3960 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003961 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003962 // Fallthrough
3963 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003964 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003965 break;
3966 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003967 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003968 break;
3969 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003970 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003971 break;
3972 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003973 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003974 break;
Evan Cheng56966222007-01-12 02:11:51 +00003975 case ISD::SETUEQ:
3976 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00003977 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003978 default: assert(0 && "Unsupported FP setcc!");
3979 }
3980 }
3981
3982 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003983 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003984 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003985 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003986 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00003987 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00003988 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003989 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003990 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003991 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003992 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003993 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00003994 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00003995 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3996 Tmp2 = SDOperand();
3997 }
3998 LHS = Tmp1;
3999 RHS = Tmp2;
4000 return;
4001 }
4002
Nate Begeman750ac1b2006-02-01 07:19:44 +00004003 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4004 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00004005 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004006 switch (cast<CondCodeSDNode>(CC)->get()) {
4007 case ISD::SETEQ:
4008 case ISD::SETNE:
4009 if (RHSLo == RHSHi)
4010 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4011 if (RHSCST->isAllOnesValue()) {
4012 // Comparison to -1.
4013 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4014 Tmp2 = RHSLo;
4015 break;
4016 }
4017
4018 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4019 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4020 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4021 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4022 break;
4023 default:
4024 // If this is a comparison of the sign bit, just look at the top part.
4025 // X > -1, x < 0
4026 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4027 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4028 CST->getValue() == 0) || // X < 0
4029 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4030 CST->isAllOnesValue())) { // X > -1
4031 Tmp1 = LHSHi;
4032 Tmp2 = RHSHi;
4033 break;
4034 }
4035
4036 // FIXME: This generated code sucks.
4037 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004038 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4039 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004040 default: assert(0 && "Unknown integer setcc!");
4041 case ISD::SETLT:
4042 case ISD::SETULT: LowCC = ISD::SETULT; break;
4043 case ISD::SETGT:
4044 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4045 case ISD::SETLE:
4046 case ISD::SETULE: LowCC = ISD::SETULE; break;
4047 case ISD::SETGE:
4048 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4049 }
4050
4051 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4052 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4053 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4054
4055 // NOTE: on targets without efficient SELECT of bools, we can always use
4056 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004057 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4058 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4059 false, DagCombineInfo);
4060 if (!Tmp1.Val)
4061 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4062 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4063 CCCode, false, DagCombineInfo);
4064 if (!Tmp2.Val)
4065 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4066
4067 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4068 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4069 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4070 (Tmp2C && Tmp2C->getValue() == 0 &&
4071 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4072 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4073 (Tmp2C && Tmp2C->getValue() == 1 &&
4074 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4075 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4076 // low part is known false, returns high part.
4077 // For LE / GE, if high part is known false, ignore the low part.
4078 // For LT / GT, if high part is known true, ignore the low part.
4079 Tmp1 = Tmp2;
4080 Tmp2 = SDOperand();
4081 } else {
4082 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4083 ISD::SETEQ, false, DagCombineInfo);
4084 if (!Result.Val)
4085 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4086 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4087 Result, Tmp1, Tmp2));
4088 Tmp1 = Result;
4089 Tmp2 = SDOperand();
4090 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004091 }
4092 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004093 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004094 LHS = Tmp1;
4095 RHS = Tmp2;
4096}
4097
Chris Lattner35481892005-12-23 00:16:34 +00004098/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004099/// The resultant code need not be legal. Note that SrcOp is the input operand
4100/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004101SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4102 SDOperand SrcOp) {
4103 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004104 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004105
4106 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004107 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004108 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004109 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004110}
4111
Chris Lattner4352cc92006-04-04 17:23:26 +00004112SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4113 // Create a vector sized/aligned stack slot, store the value to element #0,
4114 // then load the whole vector back out.
4115 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004116 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004117 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004118 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004119}
4120
4121
Chris Lattnerce872152006-03-19 06:31:19 +00004122/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004123/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004124SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4125
4126 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004127 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004128 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004129 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004130 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004131 std::map<SDOperand, std::vector<unsigned> > Values;
4132 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004133 bool isConstant = true;
4134 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4135 SplatValue.getOpcode() != ISD::UNDEF)
4136 isConstant = false;
4137
Evan Cheng033e6812006-03-24 01:17:21 +00004138 for (unsigned i = 1; i < NumElems; ++i) {
4139 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004140 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004141 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004142 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004143 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004144 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004145
4146 // If this isn't a constant element or an undef, we can't use a constant
4147 // pool load.
4148 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4149 V.getOpcode() != ISD::UNDEF)
4150 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004151 }
4152
4153 if (isOnlyLowElement) {
4154 // If the low element is an undef too, then this whole things is an undef.
4155 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4156 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4157 // Otherwise, turn this into a scalar_to_vector node.
4158 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4159 Node->getOperand(0));
4160 }
4161
Chris Lattner2eb86532006-03-24 07:29:17 +00004162 // If all elements are constants, create a load from the constant pool.
4163 if (isConstant) {
4164 MVT::ValueType VT = Node->getValueType(0);
4165 const Type *OpNTy =
4166 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4167 std::vector<Constant*> CV;
4168 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4169 if (ConstantFPSDNode *V =
4170 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4171 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4172 } else if (ConstantSDNode *V =
4173 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004174 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004175 } else {
4176 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4177 CV.push_back(UndefValue::get(OpNTy));
4178 }
4179 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004180 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004181 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004182 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004183 }
4184
Chris Lattner87100e02006-03-20 01:52:29 +00004185 if (SplatValue.Val) { // Splat of one value?
4186 // Build the shuffle constant vector: <0, 0, 0, 0>
4187 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004188 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004189 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004190 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004191 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4192 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004193
4194 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004195 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004196 // Get the splatted value into the low element of a vector register.
4197 SDOperand LowValVec =
4198 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4199
4200 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4201 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4202 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4203 SplatMask);
4204 }
4205 }
4206
Evan Cheng033e6812006-03-24 01:17:21 +00004207 // If there are only two unique elements, we may be able to turn this into a
4208 // vector shuffle.
4209 if (Values.size() == 2) {
4210 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4211 MVT::ValueType MaskVT =
4212 MVT::getIntVectorWithNumElements(NumElems);
4213 std::vector<SDOperand> MaskVec(NumElems);
4214 unsigned i = 0;
4215 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4216 E = Values.end(); I != E; ++I) {
4217 for (std::vector<unsigned>::iterator II = I->second.begin(),
4218 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004219 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004220 i += NumElems;
4221 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004222 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4223 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004224
4225 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004226 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4227 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004228 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004229 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4230 E = Values.end(); I != E; ++I) {
4231 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4232 I->first);
4233 Ops.push_back(Op);
4234 }
4235 Ops.push_back(ShuffleMask);
4236
4237 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004238 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4239 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004240 }
4241 }
Chris Lattnerce872152006-03-19 06:31:19 +00004242
4243 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4244 // aligned object on the stack, store each element into it, then load
4245 // the result as a vector.
4246 MVT::ValueType VT = Node->getValueType(0);
4247 // Create the stack frame object.
4248 SDOperand FIPtr = CreateStackTemporary(VT);
4249
4250 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004251 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004252 unsigned TypeByteSize =
4253 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004254 // Store (in the right endianness) the elements to memory.
4255 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4256 // Ignore undef elements.
4257 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4258
Chris Lattner841c8822006-03-22 01:46:54 +00004259 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004260
4261 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4262 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4263
Evan Cheng786225a2006-10-05 23:01:46 +00004264 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004265 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004266 }
4267
4268 SDOperand StoreChain;
4269 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004270 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4271 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004272 else
4273 StoreChain = DAG.getEntryNode();
4274
4275 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004276 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004277}
4278
4279/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4280/// specified value type.
4281SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4282 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4283 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004284 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004285 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004286 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004287 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4288}
4289
Chris Lattner5b359c62005-04-02 04:00:59 +00004290void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4291 SDOperand Op, SDOperand Amt,
4292 SDOperand &Lo, SDOperand &Hi) {
4293 // Expand the subcomponents.
4294 SDOperand LHSL, LHSH;
4295 ExpandOp(Op, LHSL, LHSH);
4296
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004297 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004298 MVT::ValueType VT = LHSL.getValueType();
4299 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004300 Hi = Lo.getValue(1);
4301}
4302
4303
Chris Lattnere34b3962005-01-19 04:19:40 +00004304/// ExpandShift - Try to find a clever way to expand this shift operation out to
4305/// smaller elements. If we can't find a way that is more efficient than a
4306/// libcall on this target, return false. Otherwise, return true with the
4307/// low-parts expanded into Lo and Hi.
4308bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4309 SDOperand &Lo, SDOperand &Hi) {
4310 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4311 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004312
Chris Lattnere34b3962005-01-19 04:19:40 +00004313 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004314 SDOperand ShAmt = LegalizeOp(Amt);
4315 MVT::ValueType ShTy = ShAmt.getValueType();
4316 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4317 unsigned NVTBits = MVT::getSizeInBits(NVT);
4318
4319 // Handle the case when Amt is an immediate. Other cases are currently broken
4320 // and are disabled.
4321 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4322 unsigned Cst = CN->getValue();
4323 // Expand the incoming operand to be shifted, so that we have its parts
4324 SDOperand InL, InH;
4325 ExpandOp(Op, InL, InH);
4326 switch(Opc) {
4327 case ISD::SHL:
4328 if (Cst > VTBits) {
4329 Lo = DAG.getConstant(0, NVT);
4330 Hi = DAG.getConstant(0, NVT);
4331 } else if (Cst > NVTBits) {
4332 Lo = DAG.getConstant(0, NVT);
4333 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004334 } else if (Cst == NVTBits) {
4335 Lo = DAG.getConstant(0, NVT);
4336 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004337 } else {
4338 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4339 Hi = DAG.getNode(ISD::OR, NVT,
4340 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4341 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4342 }
4343 return true;
4344 case ISD::SRL:
4345 if (Cst > VTBits) {
4346 Lo = DAG.getConstant(0, NVT);
4347 Hi = DAG.getConstant(0, NVT);
4348 } else if (Cst > NVTBits) {
4349 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4350 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004351 } else if (Cst == NVTBits) {
4352 Lo = InH;
4353 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004354 } else {
4355 Lo = DAG.getNode(ISD::OR, NVT,
4356 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4357 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4358 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4359 }
4360 return true;
4361 case ISD::SRA:
4362 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004363 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004364 DAG.getConstant(NVTBits-1, ShTy));
4365 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004366 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004367 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004368 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004369 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004370 } else if (Cst == NVTBits) {
4371 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004372 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004373 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004374 } else {
4375 Lo = DAG.getNode(ISD::OR, NVT,
4376 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4377 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4378 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4379 }
4380 return true;
4381 }
4382 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004383
4384 // Okay, the shift amount isn't constant. However, if we can tell that it is
4385 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4386 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004387 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004388
4389 // If we know that the high bit of the shift amount is one, then we can do
4390 // this as a couple of simple shifts.
4391 if (KnownOne & Mask) {
4392 // Mask out the high bit, which we know is set.
4393 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4394 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4395
4396 // Expand the incoming operand to be shifted, so that we have its parts
4397 SDOperand InL, InH;
4398 ExpandOp(Op, InL, InH);
4399 switch(Opc) {
4400 case ISD::SHL:
4401 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4402 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4403 return true;
4404 case ISD::SRL:
4405 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4406 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4407 return true;
4408 case ISD::SRA:
4409 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4410 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4411 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4412 return true;
4413 }
4414 }
4415
4416 // If we know that the high bit of the shift amount is zero, then we can do
4417 // this as a couple of simple shifts.
4418 if (KnownZero & Mask) {
4419 // Compute 32-amt.
4420 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4421 DAG.getConstant(NVTBits, Amt.getValueType()),
4422 Amt);
4423
4424 // Expand the incoming operand to be shifted, so that we have its parts
4425 SDOperand InL, InH;
4426 ExpandOp(Op, InL, InH);
4427 switch(Opc) {
4428 case ISD::SHL:
4429 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4430 Hi = DAG.getNode(ISD::OR, NVT,
4431 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4432 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4433 return true;
4434 case ISD::SRL:
4435 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4436 Lo = DAG.getNode(ISD::OR, NVT,
4437 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4438 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4439 return true;
4440 case ISD::SRA:
4441 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4442 Lo = DAG.getNode(ISD::OR, NVT,
4443 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4444 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4445 return true;
4446 }
4447 }
4448
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004449 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004450}
Chris Lattner77e77a62005-01-21 06:05:23 +00004451
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004452
Chris Lattner77e77a62005-01-21 06:05:23 +00004453// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4454// does not fit into a register, return the lo part and set the hi part to the
4455// by-reg argument. If it does fit into a single register, return the result
4456// and leave the Hi part unset.
4457SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004458 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004459 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4460 // The input chain to this libcall is the entry node of the function.
4461 // Legalizing the call will automatically add the previous call to the
4462 // dependence.
4463 SDOperand InChain = DAG.getEntryNode();
4464
Chris Lattner77e77a62005-01-21 06:05:23 +00004465 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004466 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004467 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4468 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4469 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004470 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004471 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004472 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004473 }
4474 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004475
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004476 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004477 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004478 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004479 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004480 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004481
Chris Lattner6831a812006-02-13 09:18:02 +00004482 // Legalize the call sequence, starting with the chain. This will advance
4483 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4484 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4485 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004486 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004487 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004488 default: assert(0 && "Unknown thing");
4489 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004490 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004491 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004492 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004493 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004494 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004495 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004496 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004497}
4498
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004499
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004500/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4501///
Chris Lattner77e77a62005-01-21 06:05:23 +00004502SDOperand SelectionDAGLegalize::
4503ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004504 assert(getTypeAction(Source.getValueType()) == Expand &&
4505 "This is not an expansion!");
4506 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4507
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004508 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004509 assert(Source.getValueType() == MVT::i64 &&
4510 "This only works for 64-bit -> FP");
4511 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4512 // incoming integer is set. To handle this, we dynamically test to see if
4513 // it is set, and, if so, add a fudge factor.
4514 SDOperand Lo, Hi;
4515 ExpandOp(Source, Lo, Hi);
4516
Chris Lattner66de05b2005-05-13 04:45:13 +00004517 // If this is unsigned, and not supported, first perform the conversion to
4518 // signed, then adjust the result if the sign bit is set.
4519 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4520 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4521
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004522 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4523 DAG.getConstant(0, Hi.getValueType()),
4524 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004525 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4526 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4527 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004528 uint64_t FF = 0x5f800000ULL;
4529 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004530 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004531
Chris Lattner5839bf22005-08-26 17:15:30 +00004532 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004533 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4534 SDOperand FudgeInReg;
4535 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004536 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004537 else {
4538 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004539 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004540 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004541 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004542 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004543 MVT::ValueType SCVT = SignedConv.getValueType();
4544 if (SCVT != DestTy) {
4545 // Destination type needs to be expanded as well. The FADD now we are
4546 // constructing will be expanded into a libcall.
4547 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4548 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4549 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4550 SignedConv, SignedConv.getValue(1));
4551 }
4552 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4553 }
Chris Lattner473a9902005-09-29 06:44:39 +00004554 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004555 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004556
Chris Lattnera88a2602005-05-14 05:33:54 +00004557 // Check to see if the target has a custom way to lower this. If so, use it.
4558 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4559 default: assert(0 && "This action not implemented for this operation!");
4560 case TargetLowering::Legal:
4561 case TargetLowering::Expand:
4562 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004563 case TargetLowering::Custom: {
4564 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4565 Source), DAG);
4566 if (NV.Val)
4567 return LegalizeOp(NV);
4568 break; // The target decided this was legal after all
4569 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004570 }
4571
Chris Lattner13689e22005-05-12 07:00:44 +00004572 // Expand the source, then glue it back together for the call. We must expand
4573 // the source in case it is shared (this pass of legalize must traverse it).
4574 SDOperand SrcLo, SrcHi;
4575 ExpandOp(Source, SrcLo, SrcHi);
4576 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4577
Evan Cheng56966222007-01-12 02:11:51 +00004578 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004579 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004580 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004581 else {
4582 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004583 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004584 }
Chris Lattner6831a812006-02-13 09:18:02 +00004585
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004586 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004587 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4588 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004589 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4590 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004591}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004592
Chris Lattner22cde6a2006-01-28 08:25:58 +00004593/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4594/// INT_TO_FP operation of the specified operand when the target requests that
4595/// we expand it. At this point, we know that the result and operand types are
4596/// legal for the target.
4597SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4598 SDOperand Op0,
4599 MVT::ValueType DestVT) {
4600 if (Op0.getValueType() == MVT::i32) {
4601 // simple 32-bit [signed|unsigned] integer to float/double expansion
4602
Chris Lattner58092e32007-01-20 22:35:55 +00004603 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004604 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004605 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4606 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004607 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004608 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004609 // get address of 8 byte buffer
4610 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4611 // word offset constant for Hi/Lo address computation
4612 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4613 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004614 SDOperand Hi = StackSlot;
4615 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4616 if (TLI.isLittleEndian())
4617 std::swap(Hi, Lo);
4618
Chris Lattner22cde6a2006-01-28 08:25:58 +00004619 // if signed map to unsigned space
4620 SDOperand Op0Mapped;
4621 if (isSigned) {
4622 // constant used to invert sign bit (signed to unsigned mapping)
4623 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4624 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4625 } else {
4626 Op0Mapped = Op0;
4627 }
4628 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004629 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004630 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004631 // initial hi portion of constructed double
4632 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4633 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004634 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004635 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004636 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004637 // FP constant to bias correct the final result
4638 SDOperand Bias = DAG.getConstantFP(isSigned ?
4639 BitsToDouble(0x4330000080000000ULL)
4640 : BitsToDouble(0x4330000000000000ULL),
4641 MVT::f64);
4642 // subtract the bias
4643 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4644 // final result
4645 SDOperand Result;
4646 // handle final rounding
4647 if (DestVT == MVT::f64) {
4648 // do nothing
4649 Result = Sub;
4650 } else {
4651 // if f32 then cast to f32
4652 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4653 }
4654 return Result;
4655 }
4656 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4657 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4658
4659 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4660 DAG.getConstant(0, Op0.getValueType()),
4661 ISD::SETLT);
4662 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4663 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4664 SignSet, Four, Zero);
4665
4666 // If the sign bit of the integer is set, the large number will be treated
4667 // as a negative number. To counteract this, the dynamic code adds an
4668 // offset depending on the data type.
4669 uint64_t FF;
4670 switch (Op0.getValueType()) {
4671 default: assert(0 && "Unsupported integer type!");
4672 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4673 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4674 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4675 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4676 }
4677 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004678 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004679
4680 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4681 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4682 SDOperand FudgeInReg;
4683 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004684 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004685 else {
4686 assert(DestVT == MVT::f64 && "Unexpected conversion");
4687 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4688 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004689 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004690 }
4691
4692 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4693}
4694
4695/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4696/// *INT_TO_FP operation of the specified operand when the target requests that
4697/// we promote it. At this point, we know that the result and operand types are
4698/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4699/// operation that takes a larger input.
4700SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4701 MVT::ValueType DestVT,
4702 bool isSigned) {
4703 // First step, figure out the appropriate *INT_TO_FP operation to use.
4704 MVT::ValueType NewInTy = LegalOp.getValueType();
4705
4706 unsigned OpToUse = 0;
4707
4708 // Scan for the appropriate larger type to use.
4709 while (1) {
4710 NewInTy = (MVT::ValueType)(NewInTy+1);
4711 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4712
4713 // If the target supports SINT_TO_FP of this type, use it.
4714 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4715 default: break;
4716 case TargetLowering::Legal:
4717 if (!TLI.isTypeLegal(NewInTy))
4718 break; // Can't use this datatype.
4719 // FALL THROUGH.
4720 case TargetLowering::Custom:
4721 OpToUse = ISD::SINT_TO_FP;
4722 break;
4723 }
4724 if (OpToUse) break;
4725 if (isSigned) continue;
4726
4727 // If the target supports UINT_TO_FP of this type, use it.
4728 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4729 default: break;
4730 case TargetLowering::Legal:
4731 if (!TLI.isTypeLegal(NewInTy))
4732 break; // Can't use this datatype.
4733 // FALL THROUGH.
4734 case TargetLowering::Custom:
4735 OpToUse = ISD::UINT_TO_FP;
4736 break;
4737 }
4738 if (OpToUse) break;
4739
4740 // Otherwise, try a larger type.
4741 }
4742
4743 // Okay, we found the operation and type to use. Zero extend our input to the
4744 // desired type then run the operation on it.
4745 return DAG.getNode(OpToUse, DestVT,
4746 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4747 NewInTy, LegalOp));
4748}
4749
4750/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4751/// FP_TO_*INT operation of the specified operand when the target requests that
4752/// we promote it. At this point, we know that the result and operand types are
4753/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4754/// operation that returns a larger result.
4755SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4756 MVT::ValueType DestVT,
4757 bool isSigned) {
4758 // First step, figure out the appropriate FP_TO*INT operation to use.
4759 MVT::ValueType NewOutTy = DestVT;
4760
4761 unsigned OpToUse = 0;
4762
4763 // Scan for the appropriate larger type to use.
4764 while (1) {
4765 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4766 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4767
4768 // If the target supports FP_TO_SINT returning this type, use it.
4769 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4770 default: break;
4771 case TargetLowering::Legal:
4772 if (!TLI.isTypeLegal(NewOutTy))
4773 break; // Can't use this datatype.
4774 // FALL THROUGH.
4775 case TargetLowering::Custom:
4776 OpToUse = ISD::FP_TO_SINT;
4777 break;
4778 }
4779 if (OpToUse) break;
4780
4781 // If the target supports FP_TO_UINT of this type, use it.
4782 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4783 default: break;
4784 case TargetLowering::Legal:
4785 if (!TLI.isTypeLegal(NewOutTy))
4786 break; // Can't use this datatype.
4787 // FALL THROUGH.
4788 case TargetLowering::Custom:
4789 OpToUse = ISD::FP_TO_UINT;
4790 break;
4791 }
4792 if (OpToUse) break;
4793
4794 // Otherwise, try a larger type.
4795 }
4796
4797 // Okay, we found the operation and type to use. Truncate the result of the
4798 // extended FP_TO_*INT operation to the desired size.
4799 return DAG.getNode(ISD::TRUNCATE, DestVT,
4800 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4801}
4802
4803/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4804///
4805SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4806 MVT::ValueType VT = Op.getValueType();
4807 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4808 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4809 switch (VT) {
4810 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4811 case MVT::i16:
4812 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4813 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4814 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4815 case MVT::i32:
4816 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4817 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4818 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4819 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4820 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4821 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4822 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4823 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4824 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4825 case MVT::i64:
4826 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4827 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4828 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4829 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4830 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4831 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4832 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4833 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4834 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4835 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4836 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4837 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4838 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4839 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4840 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4841 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4842 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4843 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4844 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4845 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4846 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4847 }
4848}
4849
4850/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4851///
4852SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4853 switch (Opc) {
4854 default: assert(0 && "Cannot expand this yet!");
4855 case ISD::CTPOP: {
4856 static const uint64_t mask[6] = {
4857 0x5555555555555555ULL, 0x3333333333333333ULL,
4858 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4859 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4860 };
4861 MVT::ValueType VT = Op.getValueType();
4862 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004863 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004864 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4865 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4866 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4867 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4868 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4869 DAG.getNode(ISD::AND, VT,
4870 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4871 }
4872 return Op;
4873 }
4874 case ISD::CTLZ: {
4875 // for now, we do this:
4876 // x = x | (x >> 1);
4877 // x = x | (x >> 2);
4878 // ...
4879 // x = x | (x >>16);
4880 // x = x | (x >>32); // for 64-bit input
4881 // return popcount(~x);
4882 //
4883 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4884 MVT::ValueType VT = Op.getValueType();
4885 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004886 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004887 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4888 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4889 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4890 }
4891 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4892 return DAG.getNode(ISD::CTPOP, VT, Op);
4893 }
4894 case ISD::CTTZ: {
4895 // for now, we use: { return popcount(~x & (x - 1)); }
4896 // unless the target has ctlz but not ctpop, in which case we use:
4897 // { return 32 - nlz(~x & (x-1)); }
4898 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4899 MVT::ValueType VT = Op.getValueType();
4900 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4901 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4902 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4903 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4904 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4905 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4906 TLI.isOperationLegal(ISD::CTLZ, VT))
4907 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004908 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004909 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4910 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4911 }
4912 }
4913}
Chris Lattnere34b3962005-01-19 04:19:40 +00004914
Chris Lattner3e928bb2005-01-07 07:47:09 +00004915/// ExpandOp - Expand the specified SDOperand into its two component pieces
4916/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4917/// LegalizeNodes map is filled in for any results that are not expanded, the
4918/// ExpandedNodes map is filled in for any results that are expanded, and the
4919/// Lo/Hi values are returned.
4920void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4921 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004922 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004923 SDNode *Node = Op.Val;
4924 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004925 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004926 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004927 "Cannot expand to FP value or to larger int value!");
4928
Chris Lattner6fdcb252005-09-02 20:32:45 +00004929 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004930 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004931 = ExpandedNodes.find(Op);
4932 if (I != ExpandedNodes.end()) {
4933 Lo = I->second.first;
4934 Hi = I->second.second;
4935 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004936 }
4937
Chris Lattner3e928bb2005-01-07 07:47:09 +00004938 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004939 case ISD::CopyFromReg:
4940 assert(0 && "CopyFromReg must be legal!");
4941 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004942#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004943 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004944#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004945 assert(0 && "Do not know how to expand this operator!");
4946 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004947 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004948 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004949 Lo = DAG.getNode(ISD::UNDEF, NVT);
4950 Hi = DAG.getNode(ISD::UNDEF, NVT);
4951 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004952 case ISD::Constant: {
4953 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4954 Lo = DAG.getConstant(Cst, NVT);
4955 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4956 break;
4957 }
Evan Cheng00495212006-12-12 21:32:44 +00004958 case ISD::ConstantFP: {
4959 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004960 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004961 if (getTypeAction(Lo.getValueType()) == Expand)
4962 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004963 break;
4964 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004965 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004966 // Return the operands.
4967 Lo = Node->getOperand(0);
4968 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004969 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004970
4971 case ISD::SIGN_EXTEND_INREG:
4972 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004973 // sext_inreg the low part if needed.
4974 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4975
4976 // The high part gets the sign extension from the lo-part. This handles
4977 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004978 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4979 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4980 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004981 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004982
Nate Begemand88fc032006-01-14 03:14:10 +00004983 case ISD::BSWAP: {
4984 ExpandOp(Node->getOperand(0), Lo, Hi);
4985 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4986 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4987 Lo = TempLo;
4988 break;
4989 }
4990
Chris Lattneredb1add2005-05-11 04:51:16 +00004991 case ISD::CTPOP:
4992 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004993 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4994 DAG.getNode(ISD::CTPOP, NVT, Lo),
4995 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004996 Hi = DAG.getConstant(0, NVT);
4997 break;
4998
Chris Lattner39a8f332005-05-12 19:05:01 +00004999 case ISD::CTLZ: {
5000 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005001 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005002 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5003 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005004 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5005 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005006 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5007 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5008
5009 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5010 Hi = DAG.getConstant(0, NVT);
5011 break;
5012 }
5013
5014 case ISD::CTTZ: {
5015 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005016 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005017 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5018 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005019 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5020 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005021 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5022 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5023
5024 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5025 Hi = DAG.getConstant(0, NVT);
5026 break;
5027 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005028
Nate Begemanacc398c2006-01-25 18:21:52 +00005029 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005030 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5031 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005032 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5033 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5034
5035 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005036 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005037 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5038 if (!TLI.isLittleEndian())
5039 std::swap(Lo, Hi);
5040 break;
5041 }
5042
Chris Lattner3e928bb2005-01-07 07:47:09 +00005043 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005044 LoadSDNode *LD = cast<LoadSDNode>(Node);
5045 SDOperand Ch = LD->getChain(); // Legalize the chain.
5046 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5047 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005048 int SVOffset = LD->getSrcValueOffset();
5049 unsigned Alignment = LD->getAlignment();
5050 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005051
Evan Cheng466685d2006-10-09 20:57:25 +00005052 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005053 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5054 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005055 if (VT == MVT::f32 || VT == MVT::f64) {
5056 // f32->i32 or f64->i64 one to one expansion.
5057 // Remember that we legalized the chain.
5058 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005059 // Recursively expand the new load.
5060 if (getTypeAction(NVT) == Expand)
5061 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005062 break;
5063 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005064
Evan Cheng466685d2006-10-09 20:57:25 +00005065 // Increment the pointer to the other half.
5066 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5067 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5068 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005069 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005070 if (Alignment > IncrementSize)
5071 Alignment = IncrementSize;
5072 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5073 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005074
Evan Cheng466685d2006-10-09 20:57:25 +00005075 // Build a factor node to remember that this load is independent of the
5076 // other one.
5077 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5078 Hi.getValue(1));
5079
5080 // Remember that we legalized the chain.
5081 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5082 if (!TLI.isLittleEndian())
5083 std::swap(Lo, Hi);
5084 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005085 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005086
5087 if (VT == MVT::f64 && EVT == MVT::f32) {
5088 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5089 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005090 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005091 // Remember that we legalized the chain.
5092 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5093 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5094 break;
5095 }
Evan Cheng466685d2006-10-09 20:57:25 +00005096
5097 if (EVT == NVT)
5098 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005099 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005100 else
5101 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005102 SVOffset, EVT, isVolatile,
5103 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005104
5105 // Remember that we legalized the chain.
5106 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5107
5108 if (ExtType == ISD::SEXTLOAD) {
5109 // The high part is obtained by SRA'ing all but one of the bits of the
5110 // lo part.
5111 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5112 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5113 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5114 } else if (ExtType == ISD::ZEXTLOAD) {
5115 // The high part is just a zero.
5116 Hi = DAG.getConstant(0, NVT);
5117 } else /* if (ExtType == ISD::EXTLOAD) */ {
5118 // The high part is undefined.
5119 Hi = DAG.getNode(ISD::UNDEF, NVT);
5120 }
5121 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005122 break;
5123 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005124 case ISD::AND:
5125 case ISD::OR:
5126 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5127 SDOperand LL, LH, RL, RH;
5128 ExpandOp(Node->getOperand(0), LL, LH);
5129 ExpandOp(Node->getOperand(1), RL, RH);
5130 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5131 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5132 break;
5133 }
5134 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005135 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005136 ExpandOp(Node->getOperand(1), LL, LH);
5137 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005138 if (getTypeAction(NVT) == Expand)
5139 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005140 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005141 if (VT != MVT::f32)
5142 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005143 break;
5144 }
Nate Begeman9373a812005-08-10 20:51:12 +00005145 case ISD::SELECT_CC: {
5146 SDOperand TL, TH, FL, FH;
5147 ExpandOp(Node->getOperand(2), TL, TH);
5148 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005149 if (getTypeAction(NVT) == Expand)
5150 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005151 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5152 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005153 if (VT != MVT::f32)
5154 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5155 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005156 break;
5157 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005158 case ISD::ANY_EXTEND:
5159 // The low part is any extension of the input (which degenerates to a copy).
5160 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5161 // The high part is undefined.
5162 Hi = DAG.getNode(ISD::UNDEF, NVT);
5163 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005164 case ISD::SIGN_EXTEND: {
5165 // The low part is just a sign extension of the input (which degenerates to
5166 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005167 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005168
Chris Lattner3e928bb2005-01-07 07:47:09 +00005169 // The high part is obtained by SRA'ing all but one of the bits of the lo
5170 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005171 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005172 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5173 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005174 break;
5175 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005176 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005177 // The low part is just a zero extension of the input (which degenerates to
5178 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005179 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005180
Chris Lattner3e928bb2005-01-07 07:47:09 +00005181 // The high part is just a zero.
5182 Hi = DAG.getConstant(0, NVT);
5183 break;
Chris Lattner35481892005-12-23 00:16:34 +00005184
Chris Lattner4c948eb2007-02-13 23:55:16 +00005185 case ISD::TRUNCATE: {
5186 // The input value must be larger than this value. Expand *it*.
5187 SDOperand NewLo;
5188 ExpandOp(Node->getOperand(0), NewLo, Hi);
5189
5190 // The low part is now either the right size, or it is closer. If not the
5191 // right size, make an illegal truncate so we recursively expand it.
5192 if (NewLo.getValueType() != Node->getValueType(0))
5193 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5194 ExpandOp(NewLo, Lo, Hi);
5195 break;
5196 }
5197
Chris Lattner35481892005-12-23 00:16:34 +00005198 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005199 SDOperand Tmp;
5200 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5201 // If the target wants to, allow it to lower this itself.
5202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5203 case Expand: assert(0 && "cannot expand FP!");
5204 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5205 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5206 }
5207 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5208 }
5209
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005210 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005211 if (VT == MVT::f32 || VT == MVT::f64) {
5212 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005213 if (getTypeAction(NVT) == Expand)
5214 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005215 break;
5216 }
5217
5218 // If source operand will be expanded to the same type as VT, i.e.
5219 // i64 <- f64, i32 <- f32, expand the source operand instead.
5220 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5221 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5222 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005223 break;
5224 }
5225
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005226 // Turn this into a load/store pair by default.
5227 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005228 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005229
Chris Lattner35481892005-12-23 00:16:34 +00005230 ExpandOp(Tmp, Lo, Hi);
5231 break;
5232 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005233
Chris Lattner8137c9e2006-01-28 05:07:51 +00005234 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005235 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5236 TargetLowering::Custom &&
5237 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005238 Lo = TLI.LowerOperation(Op, DAG);
5239 assert(Lo.Val && "Node must be custom expanded!");
5240 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005241 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005242 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005243 break;
5244
Chris Lattner4e6c7462005-01-08 19:27:05 +00005245 // These operators cannot be expanded directly, emit them as calls to
5246 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005247 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005248 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005249 SDOperand Op;
5250 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5251 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005252 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5253 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005254 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005255
Chris Lattnerf20d1832005-07-30 01:40:57 +00005256 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5257
Chris Lattner80a3e942005-07-29 00:33:32 +00005258 // Now that the custom expander is done, expand the result, which is still
5259 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005260 if (Op.Val) {
5261 ExpandOp(Op, Lo, Hi);
5262 break;
5263 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005264 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005265
Evan Cheng56966222007-01-12 02:11:51 +00005266 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005267 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005268 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005269 else
Evan Cheng56966222007-01-12 02:11:51 +00005270 LC = RTLIB::FPTOSINT_F64_I64;
5271 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5272 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005273 break;
Evan Cheng56966222007-01-12 02:11:51 +00005274 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005275
Evan Cheng56966222007-01-12 02:11:51 +00005276 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005277 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005278 SDOperand Op;
5279 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5280 case Expand: assert(0 && "cannot expand FP!");
5281 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5282 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5283 }
5284
5285 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5286
5287 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005288 if (Op.Val) {
5289 ExpandOp(Op, Lo, Hi);
5290 break;
5291 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005292 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005293
Evan Cheng56966222007-01-12 02:11:51 +00005294 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005295 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005296 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005297 else
Evan Cheng56966222007-01-12 02:11:51 +00005298 LC = RTLIB::FPTOUINT_F64_I64;
5299 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5300 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005301 break;
Evan Cheng56966222007-01-12 02:11:51 +00005302 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005303
Evan Cheng05a2d562006-01-09 18:31:59 +00005304 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005305 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005306 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005307 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005308 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005309 Op = TLI.LowerOperation(Op, DAG);
5310 if (Op.Val) {
5311 // Now that the custom expander is done, expand the result, which is
5312 // still VT.
5313 ExpandOp(Op, Lo, Hi);
5314 break;
5315 }
5316 }
5317
Chris Lattner79980b02006-09-13 03:50:39 +00005318 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5319 // this X << 1 as X+X.
5320 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5321 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5322 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5323 SDOperand LoOps[2], HiOps[3];
5324 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5325 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5326 LoOps[1] = LoOps[0];
5327 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5328
5329 HiOps[1] = HiOps[0];
5330 HiOps[2] = Lo.getValue(1);
5331 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5332 break;
5333 }
5334 }
5335
Chris Lattnere34b3962005-01-19 04:19:40 +00005336 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005337 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005338 break;
Chris Lattner47599822005-04-02 03:38:53 +00005339
5340 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005341 TargetLowering::LegalizeAction Action =
5342 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5343 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5344 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005345 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005346 break;
5347 }
5348
Chris Lattnere34b3962005-01-19 04:19:40 +00005349 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005350 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5351 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005352 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005353 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005354
Evan Cheng05a2d562006-01-09 18:31:59 +00005355 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005356 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005357 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005358 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005359 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005360 Op = TLI.LowerOperation(Op, DAG);
5361 if (Op.Val) {
5362 // Now that the custom expander is done, expand the result, which is
5363 // still VT.
5364 ExpandOp(Op, Lo, Hi);
5365 break;
5366 }
5367 }
5368
Chris Lattnere34b3962005-01-19 04:19:40 +00005369 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005370 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005371 break;
Chris Lattner47599822005-04-02 03:38:53 +00005372
5373 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005374 TargetLowering::LegalizeAction Action =
5375 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5376 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5377 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005378 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005379 break;
5380 }
5381
Chris Lattnere34b3962005-01-19 04:19:40 +00005382 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005383 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5384 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005385 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005386 }
5387
5388 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005389 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005390 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005391 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005392 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005393 Op = TLI.LowerOperation(Op, DAG);
5394 if (Op.Val) {
5395 // Now that the custom expander is done, expand the result, which is
5396 // still VT.
5397 ExpandOp(Op, Lo, Hi);
5398 break;
5399 }
5400 }
5401
Chris Lattnere34b3962005-01-19 04:19:40 +00005402 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005403 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005404 break;
Chris Lattner47599822005-04-02 03:38:53 +00005405
5406 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005407 TargetLowering::LegalizeAction Action =
5408 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5409 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5410 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005411 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005412 break;
5413 }
5414
Chris Lattnere34b3962005-01-19 04:19:40 +00005415 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005416 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5417 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005418 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005419 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005420
Misha Brukmanedf128a2005-04-21 22:36:52 +00005421 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005422 case ISD::SUB: {
5423 // If the target wants to custom expand this, let them.
5424 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5425 TargetLowering::Custom) {
5426 Op = TLI.LowerOperation(Op, DAG);
5427 if (Op.Val) {
5428 ExpandOp(Op, Lo, Hi);
5429 break;
5430 }
5431 }
5432
5433 // Expand the subcomponents.
5434 SDOperand LHSL, LHSH, RHSL, RHSH;
5435 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5436 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005437 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5438 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005439 LoOps[0] = LHSL;
5440 LoOps[1] = RHSL;
5441 HiOps[0] = LHSH;
5442 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005443 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005444 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005445 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005446 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005447 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005448 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005449 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005450 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005451 }
Chris Lattner84f67882005-01-20 18:52:28 +00005452 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005453 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005454
5455 case ISD::ADDC:
5456 case ISD::SUBC: {
5457 // Expand the subcomponents.
5458 SDOperand LHSL, LHSH, RHSL, RHSH;
5459 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5460 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5461 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5462 SDOperand LoOps[2] = { LHSL, RHSL };
5463 SDOperand HiOps[3] = { LHSH, RHSH };
5464
5465 if (Node->getOpcode() == ISD::ADDC) {
5466 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5467 HiOps[2] = Lo.getValue(1);
5468 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5469 } else {
5470 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5471 HiOps[2] = Lo.getValue(1);
5472 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5473 }
5474 // Remember that we legalized the flag.
5475 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5476 break;
5477 }
5478 case ISD::ADDE:
5479 case ISD::SUBE: {
5480 // Expand the subcomponents.
5481 SDOperand LHSL, LHSH, RHSL, RHSH;
5482 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5483 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5484 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5485 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5486 SDOperand HiOps[3] = { LHSH, RHSH };
5487
5488 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5489 HiOps[2] = Lo.getValue(1);
5490 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5491
5492 // Remember that we legalized the flag.
5493 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5494 break;
5495 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005496 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005497 // If the target wants to custom expand this, let them.
5498 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005499 SDOperand New = TLI.LowerOperation(Op, DAG);
5500 if (New.Val) {
5501 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005502 break;
5503 }
5504 }
5505
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005506 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5507 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005508 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005509 SDOperand LL, LH, RL, RH;
5510 ExpandOp(Node->getOperand(0), LL, LH);
5511 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005512 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005513 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005514 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5515 // extended the sign bit of the low half through the upper half, and if so
5516 // emit a MULHS instead of the alternate sequence that is valid for any
5517 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005518 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005519 // is RH an extension of the sign bit of RL?
5520 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5521 RH.getOperand(1).getOpcode() == ISD::Constant &&
5522 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5523 // is LH an extension of the sign bit of LL?
5524 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5525 LH.getOperand(1).getOpcode() == ISD::Constant &&
5526 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005527 // Low part:
5528 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5529 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005530 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005531 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005532 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005533 // Low part:
5534 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5535
5536 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005537 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5538 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5539 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5540 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5541 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005542 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005543 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005544 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005545
Evan Cheng56966222007-01-12 02:11:51 +00005546 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5547 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005548 break;
5549 }
Evan Cheng56966222007-01-12 02:11:51 +00005550 case ISD::SDIV:
5551 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5552 break;
5553 case ISD::UDIV:
5554 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5555 break;
5556 case ISD::SREM:
5557 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5558 break;
5559 case ISD::UREM:
5560 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5561 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005562
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005563 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005564 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5565 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5566 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005567 break;
5568 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005569 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5570 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5571 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005572 break;
5573 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005574 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5575 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5576 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005577 break;
5578 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005579 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5580 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5581 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005582 break;
5583 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005584 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005585 break;
5586 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005587 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005588 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005589 case ISD::FSQRT:
5590 case ISD::FSIN:
5591 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005592 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005593 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005594 case ISD::FSQRT:
5595 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5596 break;
5597 case ISD::FSIN:
5598 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5599 break;
5600 case ISD::FCOS:
5601 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5602 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005603 default: assert(0 && "Unreachable!");
5604 }
Evan Cheng56966222007-01-12 02:11:51 +00005605 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005606 break;
5607 }
Evan Cheng966bf242006-12-16 00:52:40 +00005608 case ISD::FABS: {
5609 SDOperand Mask = (VT == MVT::f64)
5610 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5611 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5612 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5613 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5614 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5615 if (getTypeAction(NVT) == Expand)
5616 ExpandOp(Lo, Lo, Hi);
5617 break;
5618 }
5619 case ISD::FNEG: {
5620 SDOperand Mask = (VT == MVT::f64)
5621 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5622 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5623 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5624 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5625 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5626 if (getTypeAction(NVT) == Expand)
5627 ExpandOp(Lo, Lo, Hi);
5628 break;
5629 }
Evan Cheng912095b2007-01-04 21:56:39 +00005630 case ISD::FCOPYSIGN: {
5631 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5632 if (getTypeAction(NVT) == Expand)
5633 ExpandOp(Lo, Lo, Hi);
5634 break;
5635 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005636 case ISD::SINT_TO_FP:
5637 case ISD::UINT_TO_FP: {
5638 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5639 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005640 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005641 if (Node->getOperand(0).getValueType() == MVT::i64) {
5642 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005643 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005644 else
Evan Cheng56966222007-01-12 02:11:51 +00005645 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005646 } else {
5647 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005648 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005649 else
Evan Cheng56966222007-01-12 02:11:51 +00005650 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005651 }
5652
5653 // Promote the operand if needed.
5654 if (getTypeAction(SrcVT) == Promote) {
5655 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5656 Tmp = isSigned
5657 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5658 DAG.getValueType(SrcVT))
5659 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5660 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5661 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005662
5663 const char *LibCall = TLI.getLibcallName(LC);
5664 if (LibCall)
5665 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5666 else {
5667 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5668 Node->getOperand(0));
5669 if (getTypeAction(Lo.getValueType()) == Expand)
5670 ExpandOp(Lo, Lo, Hi);
5671 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005672 break;
5673 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005674 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005675
Chris Lattner83397362005-12-21 18:02:52 +00005676 // Make sure the resultant values have been legalized themselves, unless this
5677 // is a type that requires multi-step expansion.
5678 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5679 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005680 if (Hi.Val)
5681 // Don't legalize the high part if it is expanded to a single node.
5682 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005683 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005684
5685 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005686 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005687 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005688}
5689
Dan Gohman7f321562007-06-25 16:23:39 +00005690/// SplitVectorOp - Given an operand of vector type, break it down into
5691/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005692void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5693 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005694 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005695 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005696 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005697 assert(NumElements > 1 && "Cannot split a single element vector!");
5698 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005699 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5700 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005701
5702 // See if we already split it.
5703 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5704 = SplitNodes.find(Op);
5705 if (I != SplitNodes.end()) {
5706 Lo = I->second.first;
5707 Hi = I->second.second;
5708 return;
5709 }
5710
5711 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005712 default:
5713#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005714 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005715#endif
5716 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005717 case ISD::BUILD_PAIR:
5718 Lo = Node->getOperand(0);
5719 Hi = Node->getOperand(1);
5720 break;
5721 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005722 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5723 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005724 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005725
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005726 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005727 Node->op_end());
5728 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005729 break;
5730 }
Dan Gohman7f321562007-06-25 16:23:39 +00005731 case ISD::CONCAT_VECTORS: {
5732 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5733 if (NewNumSubvectors == 1) {
5734 Lo = Node->getOperand(0);
5735 Hi = Node->getOperand(1);
5736 } else {
5737 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5738 Node->op_begin()+NewNumSubvectors);
5739 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005740
Dan Gohman7f321562007-06-25 16:23:39 +00005741 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5742 Node->op_end());
5743 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5744 }
Dan Gohman65956352007-06-13 15:12:02 +00005745 break;
5746 }
Dan Gohman7f321562007-06-25 16:23:39 +00005747 case ISD::ADD:
5748 case ISD::SUB:
5749 case ISD::MUL:
5750 case ISD::FADD:
5751 case ISD::FSUB:
5752 case ISD::FMUL:
5753 case ISD::SDIV:
5754 case ISD::UDIV:
5755 case ISD::FDIV:
5756 case ISD::AND:
5757 case ISD::OR:
5758 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005759 SDOperand LL, LH, RL, RH;
5760 SplitVectorOp(Node->getOperand(0), LL, LH);
5761 SplitVectorOp(Node->getOperand(1), RL, RH);
5762
Dan Gohman7f321562007-06-25 16:23:39 +00005763 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5764 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005765 break;
5766 }
Dan Gohman7f321562007-06-25 16:23:39 +00005767 case ISD::LOAD: {
5768 LoadSDNode *LD = cast<LoadSDNode>(Node);
5769 SDOperand Ch = LD->getChain();
5770 SDOperand Ptr = LD->getBasePtr();
5771 const Value *SV = LD->getSrcValue();
5772 int SVOffset = LD->getSrcValueOffset();
5773 unsigned Alignment = LD->getAlignment();
5774 bool isVolatile = LD->isVolatile();
5775
5776 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5777 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005778 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5779 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005780 SVOffset += IncrementSize;
5781 if (Alignment > IncrementSize)
5782 Alignment = IncrementSize;
5783 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005784
5785 // Build a factor node to remember that this load is independent of the
5786 // other one.
5787 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5788 Hi.getValue(1));
5789
5790 // Remember that we legalized the chain.
5791 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005792 break;
5793 }
Dan Gohman7f321562007-06-25 16:23:39 +00005794 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005795 // We know the result is a vector. The input may be either a vector or a
5796 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005797 SDOperand InOp = Node->getOperand(0);
5798 if (!MVT::isVector(InOp.getValueType()) ||
5799 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5800 // The input is a scalar or single-element vector.
5801 // Lower to a store/load so that it can be split.
5802 // FIXME: this could be improved probably.
5803 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005804
Evan Cheng786225a2006-10-05 23:01:46 +00005805 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005806 InOp, Ptr, NULL, 0);
5807 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005808 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005809 // Split the vector and convert each of the pieces now.
5810 SplitVectorOp(InOp, Lo, Hi);
5811 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5812 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5813 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005814 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005815 }
5816
5817 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005818 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005819 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005820 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005821}
5822
5823
Dan Gohman89b20c02007-06-27 14:06:22 +00005824/// ScalarizeVectorOp - Given an operand of single-element vector type
5825/// (e.g. v1f32), convert it into the equivalent operation that returns a
5826/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005827SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5828 assert(MVT::isVector(Op.getValueType()) &&
5829 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005830 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005831 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5832 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005833
Dan Gohman7f321562007-06-25 16:23:39 +00005834 // See if we already scalarized it.
5835 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5836 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005837
5838 SDOperand Result;
5839 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005840 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005841#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005842 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005843#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005844 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5845 case ISD::ADD:
5846 case ISD::FADD:
5847 case ISD::SUB:
5848 case ISD::FSUB:
5849 case ISD::MUL:
5850 case ISD::FMUL:
5851 case ISD::SDIV:
5852 case ISD::UDIV:
5853 case ISD::FDIV:
5854 case ISD::SREM:
5855 case ISD::UREM:
5856 case ISD::FREM:
5857 case ISD::AND:
5858 case ISD::OR:
5859 case ISD::XOR:
5860 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005861 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005862 ScalarizeVectorOp(Node->getOperand(0)),
5863 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005864 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005865 case ISD::FNEG:
5866 case ISD::FABS:
5867 case ISD::FSQRT:
5868 case ISD::FSIN:
5869 case ISD::FCOS:
5870 Result = DAG.getNode(Node->getOpcode(),
5871 NewVT,
5872 ScalarizeVectorOp(Node->getOperand(0)));
5873 break;
5874 case ISD::LOAD: {
5875 LoadSDNode *LD = cast<LoadSDNode>(Node);
5876 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5877 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005878
Dan Gohman7f321562007-06-25 16:23:39 +00005879 const Value *SV = LD->getSrcValue();
5880 int SVOffset = LD->getSrcValueOffset();
5881 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5882 LD->isVolatile(), LD->getAlignment());
5883
Chris Lattnerc7029802006-03-18 01:44:44 +00005884 // Remember that we legalized the chain.
5885 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5886 break;
5887 }
Dan Gohman7f321562007-06-25 16:23:39 +00005888 case ISD::BUILD_VECTOR:
5889 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005890 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005891 case ISD::INSERT_VECTOR_ELT:
5892 // Returning the inserted scalar element.
5893 Result = Node->getOperand(1);
5894 break;
5895 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005896 assert(Node->getOperand(0).getValueType() == NewVT &&
5897 "Concat of non-legal vectors not yet supported!");
5898 Result = Node->getOperand(0);
5899 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005900 case ISD::VECTOR_SHUFFLE: {
5901 // Figure out if the scalar is the LHS or RHS and return it.
5902 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5903 if (cast<ConstantSDNode>(EltNum)->getValue())
5904 Result = ScalarizeVectorOp(Node->getOperand(1));
5905 else
5906 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005907 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005908 }
5909 case ISD::EXTRACT_SUBVECTOR:
5910 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005911 assert(Result.getValueType() == NewVT);
5912 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005913 case ISD::BIT_CONVERT:
5914 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005915 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005916 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005917 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005918 ScalarizeVectorOp(Op.getOperand(1)),
5919 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005920 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005921 }
5922
5923 if (TLI.isTypeLegal(NewVT))
5924 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005925 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5926 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005927 return Result;
5928}
5929
Chris Lattner3e928bb2005-01-07 07:47:09 +00005930
5931// SelectionDAG::Legalize - This is the entry point for the file.
5932//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005933void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005934 if (ViewLegalizeDAGs) viewGraph();
5935
Chris Lattner3e928bb2005-01-07 07:47:09 +00005936 /// run - This is the main entry point to this class.
5937 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005938 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005939}
5940