blob: 4131ed9ecf75628ebfbc1a4c7b02fa9c554dcade [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
553
Dan Gohmana3466152007-07-13 20:14:11 +0000554/// LegalizeOp - We know that the specified value has a legal type, and
555/// that its operands are legal. Now ensure that the operation itself
556/// is legal, recursively ensuring that the operands' operations remain
557/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000558SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000559 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000560 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000561 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000562
Chris Lattner3e928bb2005-01-07 07:47:09 +0000563 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000564 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000565 if (Node->getNumValues() > 1) {
566 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000567 if (getTypeAction(Node->getValueType(i)) != Legal) {
568 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000570 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000571 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000572 }
573 }
574
Chris Lattner45982da2005-05-12 16:53:42 +0000575 // Note that LegalizeOp may be reentered even from single-use nodes, which
576 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000577 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000578 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579
Nate Begeman9373a812005-08-10 20:51:12 +0000580 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000581 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000582 bool isCustom = false;
583
Chris Lattner3e928bb2005-01-07 07:47:09 +0000584 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000585 case ISD::FrameIndex:
586 case ISD::EntryToken:
587 case ISD::Register:
588 case ISD::BasicBlock:
589 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000590 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000591 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000592 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000593 case ISD::TargetConstantPool:
594 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000595 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000596 case ISD::TargetExternalSymbol:
597 case ISD::VALUETYPE:
598 case ISD::SRCVALUE:
599 case ISD::STRING:
600 case ISD::CONDCODE:
601 // Primitives must all be legal.
602 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
603 "This must be legal!");
604 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000605 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000606 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
607 // If this is a target node, legalize it by legalizing the operands then
608 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000609 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000611 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000612
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000613 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000614
615 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
616 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
617 return Result.getValue(Op.ResNo);
618 }
619 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000620#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000621 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000622#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 assert(0 && "Do not know how to legalize this operator!");
624 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000625 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000626 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000627 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000628 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000629 case ISD::ConstantPool:
630 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000631 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
632 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000633 case TargetLowering::Custom:
634 Tmp1 = TLI.LowerOperation(Op, DAG);
635 if (Tmp1.Val) Result = Tmp1;
636 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000637 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000638 break;
639 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000640 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000641 case ISD::FRAMEADDR:
642 case ISD::RETURNADDR:
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000643 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemanbcc5f362007-01-29 22:58:52 +0000644 // The only option for these nodes is to custom lower them. If the target
645 // does not custom lower them, then return zero.
646 Tmp1 = TLI.LowerOperation(Op, DAG);
647 if (Tmp1.Val)
648 Result = Tmp1;
649 else
650 Result = DAG.getConstant(0, TLI.getPointerTy());
651 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000652 case ISD::EXCEPTIONADDR: {
653 Tmp1 = LegalizeOp(Node->getOperand(0));
654 MVT::ValueType VT = Node->getValueType(0);
655 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
656 default: assert(0 && "This action is not supported yet!");
657 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000658 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000659 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
660 }
661 break;
662 case TargetLowering::Custom:
663 Result = TLI.LowerOperation(Op, DAG);
664 if (Result.Val) break;
665 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000666 case TargetLowering::Legal: {
667 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
668 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
669 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000670 break;
671 }
672 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000673 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000674 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000675 case ISD::EHSELECTION: {
676 Tmp1 = LegalizeOp(Node->getOperand(0));
677 Tmp2 = LegalizeOp(Node->getOperand(1));
678 MVT::ValueType VT = Node->getValueType(0);
679 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
680 default: assert(0 && "This action is not supported yet!");
681 case TargetLowering::Expand: {
682 unsigned Reg = TLI.getExceptionSelectorRegister();
683 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
684 }
685 break;
686 case TargetLowering::Custom:
687 Result = TLI.LowerOperation(Op, DAG);
688 if (Result.Val) break;
689 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000690 case TargetLowering::Legal: {
691 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
692 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
693 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000694 break;
695 }
696 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000697 }
Jim Laskey8782d482007-02-28 20:43:58 +0000698 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000699 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000700 MVT::ValueType VT = Node->getValueType(0);
701 // The only "good" option for this node is to custom lower it.
702 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
703 default: assert(0 && "This action is not supported at all!");
704 case TargetLowering::Custom:
705 Result = TLI.LowerOperation(Op, DAG);
706 if (Result.Val) break;
707 // Fall Thru
708 case TargetLowering::Legal:
709 // Target does not know, how to lower this, lower to noop
710 Result = LegalizeOp(Node->getOperand(0));
711 break;
712 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000713 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000714 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000715 case ISD::AssertSext:
716 case ISD::AssertZext:
717 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000719 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000720 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000721 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000722 Result = Node->getOperand(Op.ResNo);
723 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000724 case ISD::CopyFromReg:
725 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000726 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000727 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000729 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000730 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000731 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000732 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000733 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
734 } else {
735 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
736 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000737 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
738 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000739 // Since CopyFromReg produces two values, make sure to remember that we
740 // legalized both of them.
741 AddLegalizedOperand(Op.getValue(0), Result);
742 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
743 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000744 case ISD::UNDEF: {
745 MVT::ValueType VT = Op.getValueType();
746 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000747 default: assert(0 && "This action is not supported yet!");
748 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000749 if (MVT::isInteger(VT))
750 Result = DAG.getConstant(0, VT);
751 else if (MVT::isFloatingPoint(VT))
752 Result = DAG.getConstantFP(0, VT);
753 else
754 assert(0 && "Unknown value type!");
755 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000756 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000757 break;
758 }
759 break;
760 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000761
Chris Lattner48b61a72006-03-28 00:40:33 +0000762 case ISD::INTRINSIC_W_CHAIN:
763 case ISD::INTRINSIC_WO_CHAIN:
764 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000765 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000766 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
767 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000768 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000769
770 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000771 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000772 TargetLowering::Custom) {
773 Tmp3 = TLI.LowerOperation(Result, DAG);
774 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000775 }
776
777 if (Result.Val->getNumValues() == 1) break;
778
779 // Must have return value and chain result.
780 assert(Result.Val->getNumValues() == 2 &&
781 "Cannot return more than two values!");
782
783 // Since loads produce two values, make sure to remember that we
784 // legalized both of them.
785 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
786 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
787 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000788 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000789
790 case ISD::LOCATION:
791 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
792 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
793
794 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
795 case TargetLowering::Promote:
796 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000797 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000798 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000799 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000800 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000801
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000802 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000803 const std::string &FName =
804 cast<StringSDNode>(Node->getOperand(3))->getValue();
805 const std::string &DirName =
806 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000807 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000808
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000809 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000810 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000811 SDOperand LineOp = Node->getOperand(1);
812 SDOperand ColOp = Node->getOperand(2);
813
814 if (useDEBUG_LOC) {
815 Ops.push_back(LineOp); // line #
816 Ops.push_back(ColOp); // col #
817 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000818 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000819 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000820 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
821 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000822 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000823 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000824 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000825 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000826 } else {
827 Result = Tmp1; // chain
828 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000829 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000830 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000831 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000832 if (Tmp1 != Node->getOperand(0) ||
833 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000834 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000835 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000836 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
837 Ops.push_back(Node->getOperand(1)); // line # must be legal.
838 Ops.push_back(Node->getOperand(2)); // col # must be legal.
839 } else {
840 // Otherwise promote them.
841 Ops.push_back(PromoteOp(Node->getOperand(1)));
842 Ops.push_back(PromoteOp(Node->getOperand(2)));
843 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000844 Ops.push_back(Node->getOperand(3)); // filename must be legal.
845 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000846 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000847 }
848 break;
849 }
850 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000851
852 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000853 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000854 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000855 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000856 case TargetLowering::Legal:
857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
859 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
860 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000862 break;
863 }
864 break;
865
Jim Laskey1ee29252007-01-26 14:34:52 +0000866 case ISD::LABEL:
867 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
868 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000869 default: assert(0 && "This action is not supported yet!");
870 case TargetLowering::Legal:
871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
872 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000874 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000875 case TargetLowering::Expand:
876 Result = LegalizeOp(Node->getOperand(0));
877 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000878 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000879 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000880
Chris Lattner3e928bb2005-01-07 07:47:09 +0000881 case ISD::Constant:
882 // We know we don't need to expand constants here, constants only have one
883 // value and we check that it is fine above.
884
885 // FIXME: Maybe we should handle things like targets that don't support full
886 // 32-bit immediates?
887 break;
888 case ISD::ConstantFP: {
889 // Spill FP immediates to the constant pool if the target cannot directly
890 // codegen them. Targets often have some immediate values that can be
891 // efficiently generated into an FP register without a load. We explicitly
892 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000893 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
894
895 // Check to see if this FP immediate is already legal.
896 bool isLegal = false;
897 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
898 E = TLI.legal_fpimm_end(); I != E; ++I)
899 if (CFP->isExactlyValue(*I)) {
900 isLegal = true;
901 break;
902 }
903
Chris Lattner3181a772006-01-29 06:26:56 +0000904 // If this is a legal constant, turn it into a TargetConstantFP node.
905 if (isLegal) {
906 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
907 break;
908 }
909
910 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
911 default: assert(0 && "This action is not supported yet!");
912 case TargetLowering::Custom:
913 Tmp3 = TLI.LowerOperation(Result, DAG);
914 if (Tmp3.Val) {
915 Result = Tmp3;
916 break;
917 }
918 // FALLTHROUGH
919 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000920 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000921 }
922 break;
923 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000924 case ISD::TokenFactor:
925 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000926 Tmp1 = LegalizeOp(Node->getOperand(0));
927 Tmp2 = LegalizeOp(Node->getOperand(1));
928 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
929 } else if (Node->getNumOperands() == 3) {
930 Tmp1 = LegalizeOp(Node->getOperand(0));
931 Tmp2 = LegalizeOp(Node->getOperand(1));
932 Tmp3 = LegalizeOp(Node->getOperand(2));
933 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000934 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000935 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000936 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000937 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
938 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000939 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000940 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000941 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000942
943 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000944 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000945 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000946 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
947 assert(Tmp3.Val && "Target didn't custom lower this node!");
948 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
949 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000950
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000951 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000952 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000953 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
954 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000955 if (Op.ResNo == i)
956 Tmp2 = Tmp1;
957 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
958 }
959 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +0000960 case ISD::EXTRACT_SUBREG: {
961 Tmp1 = LegalizeOp(Node->getOperand(0));
962 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
963 assert(idx && "Operand must be a constant");
964 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
966 }
967 break;
968 case ISD::INSERT_SUBREG: {
969 Tmp1 = LegalizeOp(Node->getOperand(0));
970 Tmp2 = LegalizeOp(Node->getOperand(1));
971 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
972 assert(idx && "Operand must be a constant");
973 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
975 }
976 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000977 case ISD::BUILD_VECTOR:
978 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000979 default: assert(0 && "This action is not supported yet!");
980 case TargetLowering::Custom:
981 Tmp3 = TLI.LowerOperation(Result, DAG);
982 if (Tmp3.Val) {
983 Result = Tmp3;
984 break;
985 }
986 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000987 case TargetLowering::Expand:
988 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000989 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000990 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000991 break;
992 case ISD::INSERT_VECTOR_ELT:
993 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
994 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
995 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
997
998 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
999 Node->getValueType(0))) {
1000 default: assert(0 && "This action is not supported yet!");
1001 case TargetLowering::Legal:
1002 break;
1003 case TargetLowering::Custom:
1004 Tmp3 = TLI.LowerOperation(Result, DAG);
1005 if (Tmp3.Val) {
1006 Result = Tmp3;
1007 break;
1008 }
1009 // FALLTHROUGH
1010 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001011 // If the insert index is a constant, codegen this as a scalar_to_vector,
1012 // then a shuffle that inserts it into the right position in the vector.
1013 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1014 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1015 Tmp1.getValueType(), Tmp2);
1016
1017 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1018 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001019 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001020
1021 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1022 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1023 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001024 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001025 for (unsigned i = 0; i != NumElts; ++i) {
1026 if (i != InsertPos->getValue())
1027 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1028 else
1029 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1030 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001031 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1032 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001033
1034 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1035 Tmp1, ScVec, ShufMask);
1036 Result = LegalizeOp(Result);
1037 break;
1038 }
1039
Chris Lattner2332b9f2006-03-19 01:17:20 +00001040 // If the target doesn't support this, we have to spill the input vector
1041 // to a temporary stack slot, update the element, then reload it. This is
1042 // badness. We could also load the value into a vector register (either
1043 // with a "move to register" or "extload into register" instruction, then
1044 // permute it into place, if the idx is a constant and if the idx is
1045 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001046 MVT::ValueType VT = Tmp1.getValueType();
1047 MVT::ValueType EltVT = Tmp2.getValueType();
1048 MVT::ValueType IdxVT = Tmp3.getValueType();
1049 MVT::ValueType PtrVT = TLI.getPointerTy();
1050 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001051 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001052 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001053
1054 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001055 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1056 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001057 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001058 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1059 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1060 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001061 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001062 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001063 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001064 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001065 break;
1066 }
1067 }
1068 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001069 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001070 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1071 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1072 break;
1073 }
1074
Chris Lattnerce872152006-03-19 06:31:19 +00001075 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1076 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1077 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1078 Node->getValueType(0))) {
1079 default: assert(0 && "This action is not supported yet!");
1080 case TargetLowering::Legal:
1081 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001082 case TargetLowering::Custom:
1083 Tmp3 = TLI.LowerOperation(Result, DAG);
1084 if (Tmp3.Val) {
1085 Result = Tmp3;
1086 break;
1087 }
1088 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001089 case TargetLowering::Expand:
1090 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001091 break;
1092 }
Chris Lattnerce872152006-03-19 06:31:19 +00001093 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001094 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001095 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1096 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1098
1099 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001100 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1101 default: assert(0 && "Unknown operation action!");
1102 case TargetLowering::Legal:
1103 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1104 "vector shuffle should not be created if not legal!");
1105 break;
1106 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001107 Tmp3 = TLI.LowerOperation(Result, DAG);
1108 if (Tmp3.Val) {
1109 Result = Tmp3;
1110 break;
1111 }
1112 // FALLTHROUGH
1113 case TargetLowering::Expand: {
1114 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001115 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001116 MVT::ValueType PtrVT = TLI.getPointerTy();
1117 SDOperand Mask = Node->getOperand(2);
1118 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001119 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001120 for (unsigned i = 0; i != NumElems; ++i) {
1121 SDOperand Arg = Mask.getOperand(i);
1122 if (Arg.getOpcode() == ISD::UNDEF) {
1123 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1124 } else {
1125 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1126 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1127 if (Idx < NumElems)
1128 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1129 DAG.getConstant(Idx, PtrVT)));
1130 else
1131 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1132 DAG.getConstant(Idx - NumElems, PtrVT)));
1133 }
1134 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001135 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001136 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001137 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001138 case TargetLowering::Promote: {
1139 // Change base type to a different vector type.
1140 MVT::ValueType OVT = Node->getValueType(0);
1141 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1142
1143 // Cast the two input vectors.
1144 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1145 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1146
1147 // Convert the shuffle mask to the right # elements.
1148 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1149 assert(Tmp3.Val && "Shuffle not legal?");
1150 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1151 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1152 break;
1153 }
Chris Lattner87100e02006-03-20 01:52:29 +00001154 }
1155 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001156
1157 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001158 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001159 Tmp2 = LegalizeOp(Node->getOperand(1));
1160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001161 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001162 break;
1163
Dan Gohman7f321562007-06-25 16:23:39 +00001164 case ISD::EXTRACT_SUBVECTOR:
1165 Tmp1 = Node->getOperand(0);
1166 Tmp2 = LegalizeOp(Node->getOperand(1));
1167 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1168 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001169 break;
1170
Chris Lattner6831a812006-02-13 09:18:02 +00001171 case ISD::CALLSEQ_START: {
1172 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1173
1174 // Recursively Legalize all of the inputs of the call end that do not lead
1175 // to this call start. This ensures that any libcalls that need be inserted
1176 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001177 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001178 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001179 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1180 NodesLeadingTo);
1181 }
Chris Lattner6831a812006-02-13 09:18:02 +00001182
1183 // Now that we legalized all of the inputs (which may have inserted
1184 // libcalls) create the new CALLSEQ_START node.
1185 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1186
1187 // Merge in the last call, to ensure that this call start after the last
1188 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001189 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001190 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1191 Tmp1 = LegalizeOp(Tmp1);
1192 }
Chris Lattner6831a812006-02-13 09:18:02 +00001193
1194 // Do not try to legalize the target-specific arguments (#1+).
1195 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001196 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001197 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001198 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001199 }
1200
1201 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001202 AddLegalizedOperand(Op.getValue(0), Result);
1203 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1204 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1205
Chris Lattner6831a812006-02-13 09:18:02 +00001206 // Now that the callseq_start and all of the non-call nodes above this call
1207 // sequence have been legalized, legalize the call itself. During this
1208 // process, no libcalls can/will be inserted, guaranteeing that no calls
1209 // can overlap.
1210 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1211 SDOperand InCallSEQ = LastCALLSEQ_END;
1212 // Note that we are selecting this call!
1213 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1214 IsLegalizingCall = true;
1215
1216 // Legalize the call, starting from the CALLSEQ_END.
1217 LegalizeOp(LastCALLSEQ_END);
1218 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1219 return Result;
1220 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001221 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001222 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1223 // will cause this node to be legalized as well as handling libcalls right.
1224 if (LastCALLSEQ_END.Val != Node) {
1225 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001226 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001227 assert(I != LegalizedNodes.end() &&
1228 "Legalizing the call start should have legalized this node!");
1229 return I->second;
1230 }
1231
1232 // Otherwise, the call start has been legalized and everything is going
1233 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001234 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001235 // Do not try to legalize the target-specific arguments (#1+), except for
1236 // an optional flag input.
1237 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1238 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001239 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001240 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001241 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001242 }
1243 } else {
1244 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1245 if (Tmp1 != Node->getOperand(0) ||
1246 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001247 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001248 Ops[0] = Tmp1;
1249 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001250 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001251 }
Chris Lattner6a542892006-01-24 05:48:21 +00001252 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001253 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001254 // This finishes up call legalization.
1255 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001256
1257 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1258 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1259 if (Node->getNumValues() == 2)
1260 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1261 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001262 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001263 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1264 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1265 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001266 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001267
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001269 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001270 switch (TLI.getOperationAction(Node->getOpcode(),
1271 Node->getValueType(0))) {
1272 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001273 case TargetLowering::Expand: {
1274 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1275 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1276 " not tell us which reg is the stack pointer!");
1277 SDOperand Chain = Tmp1.getOperand(0);
1278 SDOperand Size = Tmp2.getOperand(1);
1279 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1280 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1281 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001282 Tmp1 = LegalizeOp(Tmp1);
1283 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001284 break;
1285 }
1286 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001287 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1288 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001289 Tmp1 = LegalizeOp(Tmp3);
1290 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001291 }
Chris Lattner903d2782006-01-15 08:54:32 +00001292 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001293 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001294 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001295 }
Chris Lattner903d2782006-01-15 08:54:32 +00001296 // Since this op produce two values, make sure to remember that we
1297 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001298 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1299 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001300 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001301 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001302 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001303 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001304 bool Changed = false;
1305 // Legalize all of the operands of the inline asm, in case they are nodes
1306 // that need to be expanded or something. Note we skip the asm string and
1307 // all of the TargetConstant flags.
1308 SDOperand Op = LegalizeOp(Ops[0]);
1309 Changed = Op != Ops[0];
1310 Ops[0] = Op;
1311
1312 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1313 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1314 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1315 for (++i; NumVals; ++i, --NumVals) {
1316 SDOperand Op = LegalizeOp(Ops[i]);
1317 if (Op != Ops[i]) {
1318 Changed = true;
1319 Ops[i] = Op;
1320 }
1321 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001322 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001323
1324 if (HasInFlag) {
1325 Op = LegalizeOp(Ops.back());
1326 Changed |= Op != Ops.back();
1327 Ops.back() = Op;
1328 }
1329
1330 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001331 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001332
1333 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001334 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001335 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1336 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001337 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001338 case ISD::BR:
1339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001340 // Ensure that libcalls are emitted before a branch.
1341 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1342 Tmp1 = LegalizeOp(Tmp1);
1343 LastCALLSEQ_END = DAG.getEntryNode();
1344
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001346 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001347 case ISD::BRIND:
1348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1349 // Ensure that libcalls are emitted before a branch.
1350 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1351 Tmp1 = LegalizeOp(Tmp1);
1352 LastCALLSEQ_END = DAG.getEntryNode();
1353
1354 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1355 default: assert(0 && "Indirect target must be legal type (pointer)!");
1356 case Legal:
1357 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1358 break;
1359 }
1360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1361 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001362 case ISD::BR_JT:
1363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1364 // Ensure that libcalls are emitted before a branch.
1365 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1366 Tmp1 = LegalizeOp(Tmp1);
1367 LastCALLSEQ_END = DAG.getEntryNode();
1368
1369 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1370 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1371
1372 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1373 default: assert(0 && "This action is not supported yet!");
1374 case TargetLowering::Legal: break;
1375 case TargetLowering::Custom:
1376 Tmp1 = TLI.LowerOperation(Result, DAG);
1377 if (Tmp1.Val) Result = Tmp1;
1378 break;
1379 case TargetLowering::Expand: {
1380 SDOperand Chain = Result.getOperand(0);
1381 SDOperand Table = Result.getOperand(1);
1382 SDOperand Index = Result.getOperand(2);
1383
1384 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001385 MachineFunction &MF = DAG.getMachineFunction();
1386 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001387 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1388 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001389
1390 SDOperand LD;
1391 switch (EntrySize) {
1392 default: assert(0 && "Size of jump table not supported yet."); break;
1393 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1394 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1395 }
1396
1397 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001398 // For PIC, the sequence is:
1399 // BRIND(load(Jumptable + index) + RelocBase)
1400 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1401 SDOperand Reloc;
1402 if (TLI.usesGlobalOffsetTable())
1403 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1404 else
1405 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001406 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001407 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1408 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1409 } else {
1410 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1411 }
1412 }
1413 }
1414 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001415 case ISD::BRCOND:
1416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001417 // Ensure that libcalls are emitted before a return.
1418 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1419 Tmp1 = LegalizeOp(Tmp1);
1420 LastCALLSEQ_END = DAG.getEntryNode();
1421
Chris Lattner47e92232005-01-18 19:27:06 +00001422 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1423 case Expand: assert(0 && "It's impossible to expand bools");
1424 case Legal:
1425 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1426 break;
1427 case Promote:
1428 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001429
1430 // The top bits of the promoted condition are not necessarily zero, ensure
1431 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001433 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1434 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001435 break;
1436 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001437
1438 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001439 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001440
1441 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1442 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001443 case TargetLowering::Legal: break;
1444 case TargetLowering::Custom:
1445 Tmp1 = TLI.LowerOperation(Result, DAG);
1446 if (Tmp1.Val) Result = Tmp1;
1447 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001448 case TargetLowering::Expand:
1449 // Expand brcond's setcc into its constituent parts and create a BR_CC
1450 // Node.
1451 if (Tmp2.getOpcode() == ISD::SETCC) {
1452 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1453 Tmp2.getOperand(0), Tmp2.getOperand(1),
1454 Node->getOperand(2));
1455 } else {
1456 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1457 DAG.getCondCode(ISD::SETNE), Tmp2,
1458 DAG.getConstant(0, Tmp2.getValueType()),
1459 Node->getOperand(2));
1460 }
1461 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001462 }
1463 break;
1464 case ISD::BR_CC:
1465 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001466 // Ensure that libcalls are emitted before a branch.
1467 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1468 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001469 Tmp2 = Node->getOperand(2); // LHS
1470 Tmp3 = Node->getOperand(3); // RHS
1471 Tmp4 = Node->getOperand(1); // CC
1472
1473 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001474 LastCALLSEQ_END = DAG.getEntryNode();
1475
Nate Begeman750ac1b2006-02-01 07:19:44 +00001476 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1477 // the LHS is a legal SETCC itself. In this case, we need to compare
1478 // the result against zero to select between true and false values.
1479 if (Tmp3.Val == 0) {
1480 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1481 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001482 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001483
1484 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1485 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001486
Chris Lattner181b7a32005-12-17 23:46:46 +00001487 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1488 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001489 case TargetLowering::Legal: break;
1490 case TargetLowering::Custom:
1491 Tmp4 = TLI.LowerOperation(Result, DAG);
1492 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001493 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001494 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001495 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001496 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001497 LoadSDNode *LD = cast<LoadSDNode>(Node);
1498 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1499 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001500
Evan Cheng466685d2006-10-09 20:57:25 +00001501 ISD::LoadExtType ExtType = LD->getExtensionType();
1502 if (ExtType == ISD::NON_EXTLOAD) {
1503 MVT::ValueType VT = Node->getValueType(0);
1504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1505 Tmp3 = Result.getValue(0);
1506 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001507
Evan Cheng466685d2006-10-09 20:57:25 +00001508 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1509 default: assert(0 && "This action is not supported yet!");
1510 case TargetLowering::Legal: break;
1511 case TargetLowering::Custom:
1512 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1513 if (Tmp1.Val) {
1514 Tmp3 = LegalizeOp(Tmp1);
1515 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001516 }
Evan Cheng466685d2006-10-09 20:57:25 +00001517 break;
1518 case TargetLowering::Promote: {
1519 // Only promote a load of vector type to another.
1520 assert(MVT::isVector(VT) && "Cannot promote this load!");
1521 // Change base type to a different vector type.
1522 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1523
1524 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001525 LD->getSrcValueOffset(),
1526 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001527 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1528 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001529 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001530 }
Evan Cheng466685d2006-10-09 20:57:25 +00001531 }
1532 // Since loads produce two values, make sure to remember that we
1533 // legalized both of them.
1534 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1535 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1536 return Op.ResNo ? Tmp4 : Tmp3;
1537 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001538 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001539 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1540 default: assert(0 && "This action is not supported yet!");
1541 case TargetLowering::Promote:
1542 assert(SrcVT == MVT::i1 &&
1543 "Can only promote extending LOAD from i1 -> i8!");
1544 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1545 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001546 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001547 Tmp1 = Result.getValue(0);
1548 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001549 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001550 case TargetLowering::Custom:
1551 isCustom = true;
1552 // FALLTHROUGH
1553 case TargetLowering::Legal:
1554 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1555 Tmp1 = Result.getValue(0);
1556 Tmp2 = Result.getValue(1);
1557
1558 if (isCustom) {
1559 Tmp3 = TLI.LowerOperation(Result, DAG);
1560 if (Tmp3.Val) {
1561 Tmp1 = LegalizeOp(Tmp3);
1562 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1563 }
1564 }
1565 break;
1566 case TargetLowering::Expand:
1567 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1568 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1569 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001570 LD->getSrcValueOffset(),
1571 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001572 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1573 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1574 Tmp2 = LegalizeOp(Load.getValue(1));
1575 break;
1576 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001577 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001578 // Turn the unsupported load into an EXTLOAD followed by an explicit
1579 // zero/sign extend inreg.
1580 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1581 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001582 LD->getSrcValueOffset(), SrcVT,
1583 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001584 SDOperand ValRes;
1585 if (ExtType == ISD::SEXTLOAD)
1586 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1587 Result, DAG.getValueType(SrcVT));
1588 else
1589 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1590 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1591 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1592 break;
1593 }
1594 // Since loads produce two values, make sure to remember that we legalized
1595 // both of them.
1596 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1597 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1598 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001599 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001600 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001601 case ISD::EXTRACT_ELEMENT: {
1602 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1603 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001604 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001605 case Legal:
1606 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1607 // 1 -> Hi
1608 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1609 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1610 TLI.getShiftAmountTy()));
1611 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1612 } else {
1613 // 0 -> Lo
1614 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1615 Node->getOperand(0));
1616 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001617 break;
1618 case Expand:
1619 // Get both the low and high parts.
1620 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1621 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1622 Result = Tmp2; // 1 -> Hi
1623 else
1624 Result = Tmp1; // 0 -> Lo
1625 break;
1626 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001627 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001628 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001629
1630 case ISD::CopyToReg:
1631 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001632
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001633 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001634 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001635 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001636 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001637 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001639 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001640 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001641 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001642 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1644 Tmp3);
1645 } else {
1646 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001647 }
1648
1649 // Since this produces two values, make sure to remember that we legalized
1650 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001651 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001652 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001653 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001654 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001655 break;
1656
1657 case ISD::RET:
1658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001659
1660 // Ensure that libcalls are emitted before a return.
1661 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1662 Tmp1 = LegalizeOp(Tmp1);
1663 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001664
Chris Lattner3e928bb2005-01-07 07:47:09 +00001665 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001666 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001667 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001668 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001669 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001670 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001671 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001672 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001673 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001674 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001675 SDOperand Lo, Hi;
1676 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001677
1678 // Big endian systems want the hi reg first.
1679 if (!TLI.isLittleEndian())
1680 std::swap(Lo, Hi);
1681
Evan Cheng13acce32006-12-11 19:27:14 +00001682 if (Hi.Val)
1683 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1684 else
1685 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001686 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001687 } else {
1688 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001689 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1690 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001691
Dan Gohman7f321562007-06-25 16:23:39 +00001692 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001693 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001694 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001695 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001696 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001697 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001698 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001699 } else if (NumElems == 1) {
1700 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001701 Tmp2 = ScalarizeVectorOp(Tmp2);
1702 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001703 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001704
1705 // FIXME: Returns of gcc generic vectors smaller than a legal type
1706 // should be returned in integer registers!
1707
Chris Lattnerf87324e2006-04-11 01:31:51 +00001708 // The scalarized value type may not be legal, e.g. it might require
1709 // promotion or expansion. Relegalize the return.
1710 Result = LegalizeOp(Result);
1711 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001712 // FIXME: Returns of gcc generic vectors larger than a legal vector
1713 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001714 SDOperand Lo, Hi;
1715 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001716 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001717 Result = LegalizeOp(Result);
1718 }
1719 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001720 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001721 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001722 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001723 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001724 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001725 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001726 }
1727 break;
1728 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001729 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 break;
1731 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001732 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001733 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001734 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001735 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1736 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001737 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001738 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001739 break;
1740 case Expand: {
1741 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001742 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001743 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001744 ExpandOp(Node->getOperand(i), Lo, Hi);
1745 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001746 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001747 if (Hi.Val) {
1748 NewValues.push_back(Hi);
1749 NewValues.push_back(Node->getOperand(i+1));
1750 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001751 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001752 }
1753 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001754 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001755 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001756
1757 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001758 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001759 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001760 Result = DAG.getNode(ISD::RET, MVT::Other,
1761 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001762 break;
1763 }
1764 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001765
Chris Lattner6862dbc2006-01-29 21:02:23 +00001766 if (Result.getOpcode() == ISD::RET) {
1767 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1768 default: assert(0 && "This action is not supported yet!");
1769 case TargetLowering::Legal: break;
1770 case TargetLowering::Custom:
1771 Tmp1 = TLI.LowerOperation(Result, DAG);
1772 if (Tmp1.Val) Result = Tmp1;
1773 break;
1774 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001775 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001776 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001777 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001778 StoreSDNode *ST = cast<StoreSDNode>(Node);
1779 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1780 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001781 int SVOffset = ST->getSrcValueOffset();
1782 unsigned Alignment = ST->getAlignment();
1783 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001784
Evan Cheng8b2794a2006-10-13 21:14:26 +00001785 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001786 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1787 // FIXME: We shouldn't do this for TargetConstantFP's.
1788 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1789 // to phase ordering between legalized code and the dag combiner. This
1790 // probably means that we need to integrate dag combiner and legalizer
1791 // together.
1792 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1793 if (CFP->getValueType(0) == MVT::f32) {
1794 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1795 } else {
1796 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1797 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1798 }
1799 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001800 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001801 break;
1802 }
1803
Evan Cheng8b2794a2006-10-13 21:14:26 +00001804 switch (getTypeAction(ST->getStoredVT())) {
1805 case Legal: {
1806 Tmp3 = LegalizeOp(ST->getValue());
1807 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1808 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001809
Evan Cheng8b2794a2006-10-13 21:14:26 +00001810 MVT::ValueType VT = Tmp3.getValueType();
1811 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1812 default: assert(0 && "This action is not supported yet!");
1813 case TargetLowering::Legal: break;
1814 case TargetLowering::Custom:
1815 Tmp1 = TLI.LowerOperation(Result, DAG);
1816 if (Tmp1.Val) Result = Tmp1;
1817 break;
1818 case TargetLowering::Promote:
1819 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1820 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1821 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1822 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001823 ST->getSrcValue(), SVOffset, isVolatile,
1824 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001825 break;
1826 }
1827 break;
1828 }
1829 case Promote:
1830 // Truncate the value and store the result.
1831 Tmp3 = PromoteOp(ST->getValue());
1832 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001833 SVOffset, ST->getStoredVT(),
1834 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001835 break;
1836
1837 case Expand:
1838 unsigned IncrementSize = 0;
1839 SDOperand Lo, Hi;
1840
1841 // If this is a vector type, then we have to calculate the increment as
1842 // the product of the element size in bytes, and the number of elements
1843 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001844 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001845 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001846 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1847 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001848
Dan Gohman7f321562007-06-25 16:23:39 +00001849 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001850 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001851 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001852 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001853 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001854 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001855 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001856 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001857 Result = LegalizeOp(Result);
1858 break;
1859 } else if (NumElems == 1) {
1860 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001861 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001862 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001863 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001864 // The scalarized value type may not be legal, e.g. it might require
1865 // promotion or expansion. Relegalize the scalar store.
1866 Result = LegalizeOp(Result);
1867 break;
1868 } else {
1869 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1870 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1871 }
1872 } else {
1873 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001874 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001875
1876 if (!TLI.isLittleEndian())
1877 std::swap(Lo, Hi);
1878 }
1879
1880 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001881 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001882
1883 if (Hi.Val == NULL) {
1884 // Must be int <-> float one-to-one expansion.
1885 Result = Lo;
1886 break;
1887 }
1888
Evan Cheng8b2794a2006-10-13 21:14:26 +00001889 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1890 getIntPtrConstant(IncrementSize));
1891 assert(isTypeLegal(Tmp2.getValueType()) &&
1892 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001893 SVOffset += IncrementSize;
1894 if (Alignment > IncrementSize)
1895 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001896 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001897 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001898 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1899 break;
1900 }
1901 } else {
1902 // Truncating store
1903 assert(isTypeLegal(ST->getValue().getValueType()) &&
1904 "Cannot handle illegal TRUNCSTORE yet!");
1905 Tmp3 = LegalizeOp(ST->getValue());
1906
1907 // The only promote case we handle is TRUNCSTORE:i1 X into
1908 // -> TRUNCSTORE:i8 (and X, 1)
1909 if (ST->getStoredVT() == MVT::i1 &&
1910 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1911 // Promote the bool to a mask then store.
1912 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1913 DAG.getConstant(1, Tmp3.getValueType()));
1914 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001915 SVOffset, MVT::i8,
1916 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001917 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1918 Tmp2 != ST->getBasePtr()) {
1919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1920 ST->getOffset());
1921 }
1922
1923 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1924 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001925 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001926 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001927 case TargetLowering::Custom:
1928 Tmp1 = TLI.LowerOperation(Result, DAG);
1929 if (Tmp1.Val) Result = Tmp1;
1930 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001931 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001932 }
1933 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001934 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001935 case ISD::PCMARKER:
1936 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001937 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001938 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001939 case ISD::STACKSAVE:
1940 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001941 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1942 Tmp1 = Result.getValue(0);
1943 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001944
Chris Lattner140d53c2006-01-13 02:50:02 +00001945 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1946 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001947 case TargetLowering::Legal: break;
1948 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001949 Tmp3 = TLI.LowerOperation(Result, DAG);
1950 if (Tmp3.Val) {
1951 Tmp1 = LegalizeOp(Tmp3);
1952 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001953 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001954 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001955 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001956 // Expand to CopyFromReg if the target set
1957 // StackPointerRegisterToSaveRestore.
1958 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001959 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001960 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001961 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001962 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001963 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1964 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001965 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001966 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001967 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001968
1969 // Since stacksave produce two values, make sure to remember that we
1970 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001971 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1972 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1973 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001974
Chris Lattner140d53c2006-01-13 02:50:02 +00001975 case ISD::STACKRESTORE:
1976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1977 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001979
1980 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1981 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001982 case TargetLowering::Legal: break;
1983 case TargetLowering::Custom:
1984 Tmp1 = TLI.LowerOperation(Result, DAG);
1985 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001986 break;
1987 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001988 // Expand to CopyToReg if the target set
1989 // StackPointerRegisterToSaveRestore.
1990 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1991 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1992 } else {
1993 Result = Tmp1;
1994 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001995 break;
1996 }
1997 break;
1998
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001999 case ISD::READCYCLECOUNTER:
2000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002001 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002002 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2003 Node->getValueType(0))) {
2004 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002005 case TargetLowering::Legal:
2006 Tmp1 = Result.getValue(0);
2007 Tmp2 = Result.getValue(1);
2008 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002009 case TargetLowering::Custom:
2010 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002011 Tmp1 = LegalizeOp(Result.getValue(0));
2012 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002013 break;
2014 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002015
2016 // Since rdcc produce two values, make sure to remember that we legalized
2017 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002018 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2019 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002020 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002021
Chris Lattner2ee743f2005-01-14 22:08:15 +00002022 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002023 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2024 case Expand: assert(0 && "It's impossible to expand bools");
2025 case Legal:
2026 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2027 break;
2028 case Promote:
2029 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002030 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002031 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002032 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2033 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002034 break;
2035 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002036 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002037 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002038
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002039 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002040
Nate Begemanb942a3d2005-08-23 04:29:48 +00002041 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002042 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002043 case TargetLowering::Legal: break;
2044 case TargetLowering::Custom: {
2045 Tmp1 = TLI.LowerOperation(Result, DAG);
2046 if (Tmp1.Val) Result = Tmp1;
2047 break;
2048 }
Nate Begeman9373a812005-08-10 20:51:12 +00002049 case TargetLowering::Expand:
2050 if (Tmp1.getOpcode() == ISD::SETCC) {
2051 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2052 Tmp2, Tmp3,
2053 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2054 } else {
2055 Result = DAG.getSelectCC(Tmp1,
2056 DAG.getConstant(0, Tmp1.getValueType()),
2057 Tmp2, Tmp3, ISD::SETNE);
2058 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002059 break;
2060 case TargetLowering::Promote: {
2061 MVT::ValueType NVT =
2062 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2063 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002064 if (MVT::isVector(Tmp2.getValueType())) {
2065 ExtOp = ISD::BIT_CONVERT;
2066 TruncOp = ISD::BIT_CONVERT;
2067 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002068 ExtOp = ISD::ANY_EXTEND;
2069 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002070 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002071 ExtOp = ISD::FP_EXTEND;
2072 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002073 }
2074 // Promote each of the values to the new type.
2075 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2076 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2077 // Perform the larger operation, then round down.
2078 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2079 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2080 break;
2081 }
2082 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002083 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002084 case ISD::SELECT_CC: {
2085 Tmp1 = Node->getOperand(0); // LHS
2086 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002087 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2088 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002089 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002090
Nate Begeman750ac1b2006-02-01 07:19:44 +00002091 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2092
2093 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2094 // the LHS is a legal SETCC itself. In this case, we need to compare
2095 // the result against zero to select between true and false values.
2096 if (Tmp2.Val == 0) {
2097 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2098 CC = DAG.getCondCode(ISD::SETNE);
2099 }
2100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2101
2102 // Everything is legal, see if we should expand this op or something.
2103 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2104 default: assert(0 && "This action is not supported yet!");
2105 case TargetLowering::Legal: break;
2106 case TargetLowering::Custom:
2107 Tmp1 = TLI.LowerOperation(Result, DAG);
2108 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002109 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002110 }
2111 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002112 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002113 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002114 Tmp1 = Node->getOperand(0);
2115 Tmp2 = Node->getOperand(1);
2116 Tmp3 = Node->getOperand(2);
2117 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2118
2119 // If we had to Expand the SetCC operands into a SELECT node, then it may
2120 // not always be possible to return a true LHS & RHS. In this case, just
2121 // return the value we legalized, returned in the LHS
2122 if (Tmp2.Val == 0) {
2123 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002124 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002125 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002126
Chris Lattner73e142f2006-01-30 22:43:50 +00002127 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002128 default: assert(0 && "Cannot handle this action for SETCC yet!");
2129 case TargetLowering::Custom:
2130 isCustom = true;
2131 // FALLTHROUGH.
2132 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002133 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002134 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002135 Tmp4 = TLI.LowerOperation(Result, DAG);
2136 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002137 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002138 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002139 case TargetLowering::Promote: {
2140 // First step, figure out the appropriate operation to use.
2141 // Allow SETCC to not be supported for all legal data types
2142 // Mostly this targets FP
2143 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002144 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002145
2146 // Scan for the appropriate larger type to use.
2147 while (1) {
2148 NewInTy = (MVT::ValueType)(NewInTy+1);
2149
2150 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2151 "Fell off of the edge of the integer world");
2152 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2153 "Fell off of the edge of the floating point world");
2154
2155 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002156 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002157 break;
2158 }
2159 if (MVT::isInteger(NewInTy))
2160 assert(0 && "Cannot promote Legal Integer SETCC yet");
2161 else {
2162 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2163 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2164 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002165 Tmp1 = LegalizeOp(Tmp1);
2166 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002167 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002168 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002169 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002170 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002171 case TargetLowering::Expand:
2172 // Expand a setcc node into a select_cc of the same condition, lhs, and
2173 // rhs that selects between const 1 (true) and const 0 (false).
2174 MVT::ValueType VT = Node->getValueType(0);
2175 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2176 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002177 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002178 break;
2179 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002180 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002181 case ISD::MEMSET:
2182 case ISD::MEMCPY:
2183 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002184 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002185 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2186
2187 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2188 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2189 case Expand: assert(0 && "Cannot expand a byte!");
2190 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002191 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002192 break;
2193 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002194 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002195 break;
2196 }
2197 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002198 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002199 }
Chris Lattner272455b2005-02-02 03:44:41 +00002200
2201 SDOperand Tmp4;
2202 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002203 case Expand: {
2204 // Length is too big, just take the lo-part of the length.
2205 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002206 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002207 break;
2208 }
Chris Lattnere5605212005-01-28 22:29:18 +00002209 case Legal:
2210 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002211 break;
2212 case Promote:
2213 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002214 break;
2215 }
2216
2217 SDOperand Tmp5;
2218 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2219 case Expand: assert(0 && "Cannot expand this yet!");
2220 case Legal:
2221 Tmp5 = LegalizeOp(Node->getOperand(4));
2222 break;
2223 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002224 Tmp5 = PromoteOp(Node->getOperand(4));
2225 break;
2226 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002227
2228 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2229 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002230 case TargetLowering::Custom:
2231 isCustom = true;
2232 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002233 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002234 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002235 if (isCustom) {
2236 Tmp1 = TLI.LowerOperation(Result, DAG);
2237 if (Tmp1.Val) Result = Tmp1;
2238 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002239 break;
2240 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002241 // Otherwise, the target does not support this operation. Lower the
2242 // operation to an explicit libcall as appropriate.
2243 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002244 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002245 TargetLowering::ArgListTy Args;
2246 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002247
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002248 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002249 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002250 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002251 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002252 // Extend the (previously legalized) ubyte argument to be an int value
2253 // for the call.
2254 if (Tmp3.getValueType() > MVT::i32)
2255 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2256 else
2257 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002258 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002259 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002260 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002261 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002262
2263 FnName = "memset";
2264 } else if (Node->getOpcode() == ISD::MEMCPY ||
2265 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002266 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002267 Entry.Node = Tmp2; Args.push_back(Entry);
2268 Entry.Node = Tmp3; Args.push_back(Entry);
2269 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002270 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2271 } else {
2272 assert(0 && "Unknown op!");
2273 }
Chris Lattner45982da2005-05-12 16:53:42 +00002274
Chris Lattnere1bd8222005-01-11 05:57:22 +00002275 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002276 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002277 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002278 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002279 break;
2280 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002281 }
2282 break;
2283 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002284
Chris Lattner5b359c62005-04-02 04:00:59 +00002285 case ISD::SHL_PARTS:
2286 case ISD::SRA_PARTS:
2287 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002288 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002289 bool Changed = false;
2290 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2291 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2292 Changed |= Ops.back() != Node->getOperand(i);
2293 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002294 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002295 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002296
Evan Cheng05a2d562006-01-09 18:31:59 +00002297 switch (TLI.getOperationAction(Node->getOpcode(),
2298 Node->getValueType(0))) {
2299 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002300 case TargetLowering::Legal: break;
2301 case TargetLowering::Custom:
2302 Tmp1 = TLI.LowerOperation(Result, DAG);
2303 if (Tmp1.Val) {
2304 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002305 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002306 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002307 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2308 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002309 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002310 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002311 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002312 return RetVal;
2313 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002314 break;
2315 }
2316
Chris Lattner2c8086f2005-04-02 05:00:07 +00002317 // Since these produce multiple values, make sure to remember that we
2318 // legalized all of them.
2319 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2320 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2321 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002322 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002323
2324 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002325 case ISD::ADD:
2326 case ISD::SUB:
2327 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002328 case ISD::MULHS:
2329 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002330 case ISD::UDIV:
2331 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002332 case ISD::AND:
2333 case ISD::OR:
2334 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002335 case ISD::SHL:
2336 case ISD::SRL:
2337 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002338 case ISD::FADD:
2339 case ISD::FSUB:
2340 case ISD::FMUL:
2341 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002342 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002343 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2344 case Expand: assert(0 && "Not possible");
2345 case Legal:
2346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2347 break;
2348 case Promote:
2349 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2350 break;
2351 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002352
2353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002354
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002355 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002356 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002357 case TargetLowering::Legal: break;
2358 case TargetLowering::Custom:
2359 Tmp1 = TLI.LowerOperation(Result, DAG);
2360 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002361 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002362 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002363 if (Node->getValueType(0) == MVT::i32) {
2364 switch (Node->getOpcode()) {
2365 default: assert(0 && "Do not know how to expand this integer BinOp!");
2366 case ISD::UDIV:
2367 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002368 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2369 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002370 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002371 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002372 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002373 };
2374 break;
2375 }
2376
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002377 assert(MVT::isVector(Node->getValueType(0)) &&
2378 "Cannot expand this binary operator!");
2379 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002380 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002381 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002382 MVT::ValueType PtrVT = TLI.getPointerTy();
2383 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2384 i != e; ++i) {
2385 SDOperand Idx = DAG.getConstant(i, PtrVT);
2386 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2387 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2388 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2389 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002390 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2391 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002392 break;
2393 }
Evan Chengcc987612006-04-12 21:20:24 +00002394 case TargetLowering::Promote: {
2395 switch (Node->getOpcode()) {
2396 default: assert(0 && "Do not know how to promote this BinOp!");
2397 case ISD::AND:
2398 case ISD::OR:
2399 case ISD::XOR: {
2400 MVT::ValueType OVT = Node->getValueType(0);
2401 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2402 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2403 // Bit convert each of the values to the new type.
2404 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2405 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2406 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2407 // Bit convert the result back the original type.
2408 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2409 break;
2410 }
2411 }
2412 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002413 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002414 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002415
2416 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2417 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2418 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2419 case Expand: assert(0 && "Not possible");
2420 case Legal:
2421 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2422 break;
2423 case Promote:
2424 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2425 break;
2426 }
2427
2428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2429
2430 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2431 default: assert(0 && "Operation not supported");
2432 case TargetLowering::Custom:
2433 Tmp1 = TLI.LowerOperation(Result, DAG);
2434 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002435 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002436 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002437 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002438 // If this target supports fabs/fneg natively and select is cheap,
2439 // do this efficiently.
2440 if (!TLI.isSelectExpensive() &&
2441 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2442 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002443 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002444 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002445 // Get the sign bit of the RHS.
2446 MVT::ValueType IVT =
2447 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2448 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2449 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2450 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2451 // Get the absolute value of the result.
2452 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2453 // Select between the nabs and abs value based on the sign bit of
2454 // the input.
2455 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2456 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2457 AbsVal),
2458 AbsVal);
2459 Result = LegalizeOp(Result);
2460 break;
2461 }
2462
2463 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002464 MVT::ValueType NVT =
2465 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2466 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2467 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2468 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002469 break;
2470 }
Evan Cheng912095b2007-01-04 21:56:39 +00002471 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002472 break;
2473
Nate Begeman551bf3f2006-02-17 05:43:56 +00002474 case ISD::ADDC:
2475 case ISD::SUBC:
2476 Tmp1 = LegalizeOp(Node->getOperand(0));
2477 Tmp2 = LegalizeOp(Node->getOperand(1));
2478 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2479 // Since this produces two values, make sure to remember that we legalized
2480 // both of them.
2481 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2482 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2483 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002484
Nate Begeman551bf3f2006-02-17 05:43:56 +00002485 case ISD::ADDE:
2486 case ISD::SUBE:
2487 Tmp1 = LegalizeOp(Node->getOperand(0));
2488 Tmp2 = LegalizeOp(Node->getOperand(1));
2489 Tmp3 = LegalizeOp(Node->getOperand(2));
2490 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2491 // Since this produces two values, make sure to remember that we legalized
2492 // both of them.
2493 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2494 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2495 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002496
Nate Begeman419f8b62005-10-18 00:27:41 +00002497 case ISD::BUILD_PAIR: {
2498 MVT::ValueType PairTy = Node->getValueType(0);
2499 // TODO: handle the case where the Lo and Hi operands are not of legal type
2500 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2501 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2502 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002503 case TargetLowering::Promote:
2504 case TargetLowering::Custom:
2505 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002506 case TargetLowering::Legal:
2507 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2508 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2509 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002510 case TargetLowering::Expand:
2511 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2512 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2513 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2514 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2515 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002516 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002517 break;
2518 }
2519 break;
2520 }
2521
Nate Begemanc105e192005-04-06 00:23:54 +00002522 case ISD::UREM:
2523 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002524 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002525 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2526 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002527
Nate Begemanc105e192005-04-06 00:23:54 +00002528 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002529 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2530 case TargetLowering::Custom:
2531 isCustom = true;
2532 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002533 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002535 if (isCustom) {
2536 Tmp1 = TLI.LowerOperation(Result, DAG);
2537 if (Tmp1.Val) Result = Tmp1;
2538 }
Nate Begemanc105e192005-04-06 00:23:54 +00002539 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002540 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002541 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002542 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002543 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002544 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2545 TargetLowering::Legal) {
2546 // X % Y -> X-X/Y*Y
2547 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002548 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002549 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2550 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2551 } else {
2552 assert(Node->getValueType(0) == MVT::i32 &&
2553 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002554 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2555 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002556 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002557 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002558 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002559 } else {
2560 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002561 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2562 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002563 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002564 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2565 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002566 }
2567 break;
2568 }
2569 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002570 case ISD::VAARG: {
2571 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2572 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2573
Chris Lattner5c62f332006-01-28 07:42:08 +00002574 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002575 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2576 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002577 case TargetLowering::Custom:
2578 isCustom = true;
2579 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002580 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2582 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002583 Tmp1 = Result.getValue(1);
2584
2585 if (isCustom) {
2586 Tmp2 = TLI.LowerOperation(Result, DAG);
2587 if (Tmp2.Val) {
2588 Result = LegalizeOp(Tmp2);
2589 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2590 }
2591 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002592 break;
2593 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002594 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002595 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002596 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002597 // Increment the pointer, VAList, to the next vaarg
2598 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2599 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2600 TLI.getPointerTy()));
2601 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002602 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2603 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002604 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002605 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002606 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002607 Result = LegalizeOp(Result);
2608 break;
2609 }
2610 }
2611 // Since VAARG produces two values, make sure to remember that we
2612 // legalized both of them.
2613 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002614 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2615 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002616 }
2617
2618 case ISD::VACOPY:
2619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2620 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2621 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2622
2623 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2624 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002625 case TargetLowering::Custom:
2626 isCustom = true;
2627 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002628 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2630 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002631 if (isCustom) {
2632 Tmp1 = TLI.LowerOperation(Result, DAG);
2633 if (Tmp1.Val) Result = Tmp1;
2634 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002635 break;
2636 case TargetLowering::Expand:
2637 // This defaults to loading a pointer from the input and storing it to the
2638 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002639 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002640 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002641 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2642 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002643 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2644 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002645 break;
2646 }
2647 break;
2648
2649 case ISD::VAEND:
2650 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2651 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2652
2653 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2654 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002655 case TargetLowering::Custom:
2656 isCustom = true;
2657 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002658 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002659 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002660 if (isCustom) {
2661 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2662 if (Tmp1.Val) Result = Tmp1;
2663 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002664 break;
2665 case TargetLowering::Expand:
2666 Result = Tmp1; // Default to a no-op, return the chain
2667 break;
2668 }
2669 break;
2670
2671 case ISD::VASTART:
2672 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2673 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2674
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2676
Nate Begemanacc398c2006-01-25 18:21:52 +00002677 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2678 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002679 case TargetLowering::Legal: break;
2680 case TargetLowering::Custom:
2681 Tmp1 = TLI.LowerOperation(Result, DAG);
2682 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002683 break;
2684 }
2685 break;
2686
Nate Begeman35ef9132006-01-11 21:21:00 +00002687 case ISD::ROTL:
2688 case ISD::ROTR:
2689 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2690 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002691 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2693 default:
2694 assert(0 && "ROTL/ROTR legalize operation not supported");
2695 break;
2696 case TargetLowering::Legal:
2697 break;
2698 case TargetLowering::Custom:
2699 Tmp1 = TLI.LowerOperation(Result, DAG);
2700 if (Tmp1.Val) Result = Tmp1;
2701 break;
2702 case TargetLowering::Promote:
2703 assert(0 && "Do not know how to promote ROTL/ROTR");
2704 break;
2705 case TargetLowering::Expand:
2706 assert(0 && "Do not know how to expand ROTL/ROTR");
2707 break;
2708 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002709 break;
2710
Nate Begemand88fc032006-01-14 03:14:10 +00002711 case ISD::BSWAP:
2712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2713 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002714 case TargetLowering::Custom:
2715 assert(0 && "Cannot custom legalize this yet!");
2716 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002717 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002718 break;
2719 case TargetLowering::Promote: {
2720 MVT::ValueType OVT = Tmp1.getValueType();
2721 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002722 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002723
Chris Lattner456a93a2006-01-28 07:39:30 +00002724 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2725 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2726 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2727 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2728 break;
2729 }
2730 case TargetLowering::Expand:
2731 Result = ExpandBSWAP(Tmp1);
2732 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002733 }
2734 break;
2735
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002736 case ISD::CTPOP:
2737 case ISD::CTTZ:
2738 case ISD::CTLZ:
2739 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2740 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002741 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002742 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002743 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002744 break;
2745 case TargetLowering::Promote: {
2746 MVT::ValueType OVT = Tmp1.getValueType();
2747 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002748
2749 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002750 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2751 // Perform the larger operation, then subtract if needed.
2752 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002753 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002754 case ISD::CTPOP:
2755 Result = Tmp1;
2756 break;
2757 case ISD::CTTZ:
2758 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002759 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002760 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002761 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002762 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002763 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002764 break;
2765 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002766 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002767 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002768 DAG.getConstant(MVT::getSizeInBits(NVT) -
2769 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002770 break;
2771 }
2772 break;
2773 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002774 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002775 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002776 break;
2777 }
2778 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002779
Chris Lattner2c8086f2005-04-02 05:00:07 +00002780 // Unary operators
2781 case ISD::FABS:
2782 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002783 case ISD::FSQRT:
2784 case ISD::FSIN:
2785 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002786 Tmp1 = LegalizeOp(Node->getOperand(0));
2787 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002788 case TargetLowering::Promote:
2789 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002790 isCustom = true;
2791 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002792 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002793 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002794 if (isCustom) {
2795 Tmp1 = TLI.LowerOperation(Result, DAG);
2796 if (Tmp1.Val) Result = Tmp1;
2797 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002798 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002799 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002800 switch (Node->getOpcode()) {
2801 default: assert(0 && "Unreachable!");
2802 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002803 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2804 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002805 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002806 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002807 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002808 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2809 MVT::ValueType VT = Node->getValueType(0);
2810 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002811 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002812 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2813 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002814 break;
2815 }
2816 case ISD::FSQRT:
2817 case ISD::FSIN:
2818 case ISD::FCOS: {
2819 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002820 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002821 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002822 case ISD::FSQRT:
2823 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2824 break;
2825 case ISD::FSIN:
2826 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2827 break;
2828 case ISD::FCOS:
2829 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2830 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002831 default: assert(0 && "Unreachable!");
2832 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002833 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002834 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2835 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002836 break;
2837 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002838 }
2839 break;
2840 }
2841 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002842 case ISD::FPOWI: {
2843 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002844 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2845 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002846 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002847 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2848 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002849 break;
2850 }
Chris Lattner35481892005-12-23 00:16:34 +00002851 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002852 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002853 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00002854 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2855 // The input has to be a vector type, we have to either scalarize it, pack
2856 // it, or convert it based on whether the input vector type is legal.
2857 SDNode *InVal = Node->getOperand(0).Val;
2858 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2859 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
2860
2861 // Figure out if there is a simple type corresponding to this Vector
2862 // type. If so, convert to the vector type.
2863 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2864 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00002865 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00002866 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2867 LegalizeOp(Node->getOperand(0)));
2868 break;
2869 } else if (NumElems == 1) {
2870 // Turn this into a bit convert of the scalar input.
2871 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2872 ScalarizeVectorOp(Node->getOperand(0)));
2873 break;
2874 } else {
2875 // FIXME: UNIMP! Store then reload
2876 assert(0 && "Cast from unsupported vector type not implemented yet!");
2877 }
Chris Lattner67993f72006-01-23 07:30:46 +00002878 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002879 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2880 Node->getOperand(0).getValueType())) {
2881 default: assert(0 && "Unknown operation action!");
2882 case TargetLowering::Expand:
2883 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2884 break;
2885 case TargetLowering::Legal:
2886 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002887 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002888 break;
2889 }
2890 }
2891 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002892
Chris Lattner2c8086f2005-04-02 05:00:07 +00002893 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002894 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002895 case ISD::UINT_TO_FP: {
2896 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002897 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2898 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002899 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002900 Node->getOperand(0).getValueType())) {
2901 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002902 case TargetLowering::Custom:
2903 isCustom = true;
2904 // FALLTHROUGH
2905 case TargetLowering::Legal:
2906 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002907 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002908 if (isCustom) {
2909 Tmp1 = TLI.LowerOperation(Result, DAG);
2910 if (Tmp1.Val) Result = Tmp1;
2911 }
2912 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002913 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002914 Result = ExpandLegalINT_TO_FP(isSigned,
2915 LegalizeOp(Node->getOperand(0)),
2916 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002917 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002918 case TargetLowering::Promote:
2919 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2920 Node->getValueType(0),
2921 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002922 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002923 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002924 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002925 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002926 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2927 Node->getValueType(0), Node->getOperand(0));
2928 break;
2929 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002930 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002931 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002932 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2933 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002934 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002935 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2936 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002937 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002938 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2939 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002940 break;
2941 }
2942 break;
2943 }
2944 case ISD::TRUNCATE:
2945 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2946 case Legal:
2947 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002948 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002949 break;
2950 case Expand:
2951 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2952
2953 // Since the result is legal, we should just be able to truncate the low
2954 // part of the source.
2955 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2956 break;
2957 case Promote:
2958 Result = PromoteOp(Node->getOperand(0));
2959 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2960 break;
2961 }
2962 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002963
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002964 case ISD::FP_TO_SINT:
2965 case ISD::FP_TO_UINT:
2966 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2967 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002968 Tmp1 = LegalizeOp(Node->getOperand(0));
2969
Chris Lattner1618beb2005-07-29 00:11:56 +00002970 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2971 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002972 case TargetLowering::Custom:
2973 isCustom = true;
2974 // FALLTHROUGH
2975 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002976 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002977 if (isCustom) {
2978 Tmp1 = TLI.LowerOperation(Result, DAG);
2979 if (Tmp1.Val) Result = Tmp1;
2980 }
2981 break;
2982 case TargetLowering::Promote:
2983 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2984 Node->getOpcode() == ISD::FP_TO_SINT);
2985 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002986 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002987 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2988 SDOperand True, False;
2989 MVT::ValueType VT = Node->getOperand(0).getValueType();
2990 MVT::ValueType NVT = Node->getValueType(0);
2991 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2992 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2993 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2994 Node->getOperand(0), Tmp2, ISD::SETLT);
2995 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2996 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002997 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002998 Tmp2));
2999 False = DAG.getNode(ISD::XOR, NVT, False,
3000 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003001 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3002 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003003 } else {
3004 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3005 }
3006 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003007 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003008 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003009 case Expand: {
3010 // Convert f32 / f64 to i32 / i64.
3011 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003012 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003013 switch (Node->getOpcode()) {
3014 case ISD::FP_TO_SINT:
3015 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003016 LC = (VT == MVT::i32)
3017 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003018 else
Evan Cheng56966222007-01-12 02:11:51 +00003019 LC = (VT == MVT::i32)
3020 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003021 break;
3022 case ISD::FP_TO_UINT:
3023 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003024 LC = (VT == MVT::i32)
3025 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003026 else
Evan Cheng56966222007-01-12 02:11:51 +00003027 LC = (VT == MVT::i32)
3028 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003029 break;
3030 default: assert(0 && "Unreachable!");
3031 }
3032 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003033 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3034 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003035 break;
3036 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003037 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003038 Tmp1 = PromoteOp(Node->getOperand(0));
3039 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3040 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003041 break;
3042 }
3043 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003044
Dale Johannesen849f2142007-07-03 00:53:03 +00003045 case ISD::FP_ROUND:
3046 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3047 TargetLowering::Expand) {
3048 // The only way we can lower this is to turn it into a TRUNCSTORE,
3049 // EXTLOAD pair, targetting a temporary location (a stack slot).
3050
3051 // NOTE: there is a choice here between constantly creating new stack
3052 // slots and always reusing the same one. We currently always create
3053 // new ones, as reuse may inhibit scheduling.
3054 MVT::ValueType VT = Op.getValueType(); // 32
3055 const Type *Ty = MVT::getTypeForValueType(VT);
3056 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3057 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3058 MachineFunction &MF = DAG.getMachineFunction();
3059 int SSFI =
3060 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3061 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3062 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3063 StackSlot, NULL, 0, VT);
3064 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3065 break;
3066 }
3067 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003068 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003069 case ISD::ZERO_EXTEND:
3070 case ISD::SIGN_EXTEND:
3071 case ISD::FP_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003072 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003073 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003074 case Legal:
3075 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003076 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003077 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003078 case Promote:
3079 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003080 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003081 Tmp1 = PromoteOp(Node->getOperand(0));
3082 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003083 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003084 case ISD::ZERO_EXTEND:
3085 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003086 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003087 Result = DAG.getZeroExtendInReg(Result,
3088 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003089 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003090 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003091 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003092 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003093 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003094 Result,
3095 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003096 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003097 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003098 Result = PromoteOp(Node->getOperand(0));
3099 if (Result.getValueType() != Op.getValueType())
3100 // Dynamically dead while we have only 2 FP types.
3101 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3102 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003103 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003104 Result = PromoteOp(Node->getOperand(0));
3105 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3106 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003107 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003108 }
3109 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003110 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003111 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003112 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003113 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003114
3115 // If this operation is not supported, convert it to a shl/shr or load/store
3116 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003117 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3118 default: assert(0 && "This action not supported for this op yet!");
3119 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003120 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003121 break;
3122 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003123 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003124 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003125 // NOTE: we could fall back on load/store here too for targets without
3126 // SAR. However, it is doubtful that any exist.
3127 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3128 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003129 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003130 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3131 Node->getOperand(0), ShiftCst);
3132 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3133 Result, ShiftCst);
3134 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003135 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003136 // EXTLOAD pair, targetting a temporary location (a stack slot).
3137
3138 // NOTE: there is a choice here between constantly creating new stack
3139 // slots and always reusing the same one. We currently always create
3140 // new ones, as reuse may inhibit scheduling.
3141 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003142 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003143 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003144 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003145 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003146 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003147 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003148 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3149 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003150 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003151 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003152 } else {
3153 assert(0 && "Unknown op");
3154 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003155 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003156 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003157 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003158 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003159 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003160
Chris Lattner4ddd2832006-04-08 04:13:17 +00003161 assert(Result.getValueType() == Op.getValueType() &&
3162 "Bad legalization!");
3163
Chris Lattner456a93a2006-01-28 07:39:30 +00003164 // Make sure that the generated code is itself legal.
3165 if (Result != Op)
3166 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003167
Chris Lattner45982da2005-05-12 16:53:42 +00003168 // Note that LegalizeOp may be reentered even from single-use nodes, which
3169 // means that we always must cache transformed nodes.
3170 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003171 return Result;
3172}
3173
Chris Lattner8b6fa222005-01-15 22:16:26 +00003174/// PromoteOp - Given an operation that produces a value in an invalid type,
3175/// promote it to compute the value into a larger type. The produced value will
3176/// have the correct bits for the low portion of the register, but no guarantee
3177/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003178SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3179 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003180 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003181 assert(getTypeAction(VT) == Promote &&
3182 "Caller should expand or legalize operands that are not promotable!");
3183 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3184 "Cannot promote to smaller type!");
3185
Chris Lattner03c85462005-01-15 05:21:40 +00003186 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003187 SDOperand Result;
3188 SDNode *Node = Op.Val;
3189
Chris Lattner40030bf2007-02-04 01:17:38 +00003190 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003191 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003192
Chris Lattner03c85462005-01-15 05:21:40 +00003193 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003194 case ISD::CopyFromReg:
3195 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003196 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003197#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003198 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003199#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003200 assert(0 && "Do not know how to promote this operator!");
3201 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003202 case ISD::UNDEF:
3203 Result = DAG.getNode(ISD::UNDEF, NVT);
3204 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003205 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003206 if (VT != MVT::i1)
3207 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3208 else
3209 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003210 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3211 break;
3212 case ISD::ConstantFP:
3213 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3214 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3215 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003216
Chris Lattner82fbfb62005-01-18 02:59:52 +00003217 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003218 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003219 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3220 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003221 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003222
Chris Lattner03c85462005-01-15 05:21:40 +00003223 case ISD::TRUNCATE:
3224 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3225 case Legal:
3226 Result = LegalizeOp(Node->getOperand(0));
3227 assert(Result.getValueType() >= NVT &&
3228 "This truncation doesn't make sense!");
3229 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3230 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3231 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003232 case Promote:
3233 // The truncation is not required, because we don't guarantee anything
3234 // about high bits anyway.
3235 Result = PromoteOp(Node->getOperand(0));
3236 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003237 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003238 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3239 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003240 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003241 }
3242 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003243 case ISD::SIGN_EXTEND:
3244 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003245 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003246 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3247 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3248 case Legal:
3249 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003250 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003251 break;
3252 case Promote:
3253 // Promote the reg if it's smaller.
3254 Result = PromoteOp(Node->getOperand(0));
3255 // The high bits are not guaranteed to be anything. Insert an extend.
3256 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003257 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003258 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003259 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003260 Result = DAG.getZeroExtendInReg(Result,
3261 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003262 break;
3263 }
3264 break;
Chris Lattner35481892005-12-23 00:16:34 +00003265 case ISD::BIT_CONVERT:
3266 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3267 Result = PromoteOp(Result);
3268 break;
3269
Chris Lattner8b6fa222005-01-15 22:16:26 +00003270 case ISD::FP_EXTEND:
3271 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3272 case ISD::FP_ROUND:
3273 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3274 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3275 case Promote: assert(0 && "Unreachable with 2 FP types!");
3276 case Legal:
3277 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003278 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003279 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003280 break;
3281 }
3282 break;
3283
3284 case ISD::SINT_TO_FP:
3285 case ISD::UINT_TO_FP:
3286 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3287 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003288 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003289 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003290 break;
3291
3292 case Promote:
3293 Result = PromoteOp(Node->getOperand(0));
3294 if (Node->getOpcode() == ISD::SINT_TO_FP)
3295 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003296 Result,
3297 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003298 else
Chris Lattner23993562005-04-13 02:38:47 +00003299 Result = DAG.getZeroExtendInReg(Result,
3300 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003301 // No extra round required here.
3302 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003303 break;
3304 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003305 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3306 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003307 // Round if we cannot tolerate excess precision.
3308 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003309 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3310 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003311 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003312 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003313 break;
3314
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003315 case ISD::SIGN_EXTEND_INREG:
3316 Result = PromoteOp(Node->getOperand(0));
3317 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3318 Node->getOperand(1));
3319 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003320 case ISD::FP_TO_SINT:
3321 case ISD::FP_TO_UINT:
3322 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3323 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003324 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003325 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003326 break;
3327 case Promote:
3328 // The input result is prerounded, so we don't have to do anything
3329 // special.
3330 Tmp1 = PromoteOp(Node->getOperand(0));
3331 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003332 }
Nate Begemand2558e32005-08-14 01:20:53 +00003333 // If we're promoting a UINT to a larger size, check to see if the new node
3334 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3335 // we can use that instead. This allows us to generate better code for
3336 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3337 // legal, such as PowerPC.
3338 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003339 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003340 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3341 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003342 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3343 } else {
3344 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3345 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003346 break;
3347
Chris Lattner2c8086f2005-04-02 05:00:07 +00003348 case ISD::FABS:
3349 case ISD::FNEG:
3350 Tmp1 = PromoteOp(Node->getOperand(0));
3351 assert(Tmp1.getValueType() == NVT);
3352 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3353 // NOTE: we do not have to do any extra rounding here for
3354 // NoExcessFPPrecision, because we know the input will have the appropriate
3355 // precision, and these operations don't modify precision at all.
3356 break;
3357
Chris Lattnerda6ba872005-04-28 21:44:33 +00003358 case ISD::FSQRT:
3359 case ISD::FSIN:
3360 case ISD::FCOS:
3361 Tmp1 = PromoteOp(Node->getOperand(0));
3362 assert(Tmp1.getValueType() == NVT);
3363 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003364 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003365 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3366 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003367 break;
3368
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003369 case ISD::FPOWI: {
3370 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3371 // directly as well, which may be better.
3372 Tmp1 = PromoteOp(Node->getOperand(0));
3373 assert(Tmp1.getValueType() == NVT);
3374 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3375 if (NoExcessFPPrecision)
3376 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3377 DAG.getValueType(VT));
3378 break;
3379 }
3380
Chris Lattner03c85462005-01-15 05:21:40 +00003381 case ISD::AND:
3382 case ISD::OR:
3383 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003384 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003385 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003386 case ISD::MUL:
3387 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003388 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003389 // that too is okay if they are integer operations.
3390 Tmp1 = PromoteOp(Node->getOperand(0));
3391 Tmp2 = PromoteOp(Node->getOperand(1));
3392 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3393 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003394 break;
3395 case ISD::FADD:
3396 case ISD::FSUB:
3397 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003398 Tmp1 = PromoteOp(Node->getOperand(0));
3399 Tmp2 = PromoteOp(Node->getOperand(1));
3400 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3401 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3402
3403 // Floating point operations will give excess precision that we may not be
3404 // able to tolerate. If we DO allow excess precision, just leave it,
3405 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003406 // FIXME: Why would we need to round FP ops more than integer ones?
3407 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003408 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003409 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3410 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003411 break;
3412
Chris Lattner8b6fa222005-01-15 22:16:26 +00003413 case ISD::SDIV:
3414 case ISD::SREM:
3415 // These operators require that their input be sign extended.
3416 Tmp1 = PromoteOp(Node->getOperand(0));
3417 Tmp2 = PromoteOp(Node->getOperand(1));
3418 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003419 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3420 DAG.getValueType(VT));
3421 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3422 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003423 }
3424 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3425
3426 // Perform FP_ROUND: this is probably overly pessimistic.
3427 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003428 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3429 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003430 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003431 case ISD::FDIV:
3432 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003433 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003434 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003435 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3436 case Legal:
3437 Tmp1 = LegalizeOp(Node->getOperand(0));
3438 break;
3439 case Promote:
3440 Tmp1 = PromoteOp(Node->getOperand(0));
3441 break;
3442 case Expand:
3443 assert(0 && "not implemented");
3444 }
3445 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3446 case Legal:
3447 Tmp2 = LegalizeOp(Node->getOperand(1));
3448 break;
3449 case Promote:
3450 Tmp2 = PromoteOp(Node->getOperand(1));
3451 break;
3452 case Expand:
3453 assert(0 && "not implemented");
3454 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003455 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3456
3457 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003458 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003459 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3460 DAG.getValueType(VT));
3461 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003462
3463 case ISD::UDIV:
3464 case ISD::UREM:
3465 // These operators require that their input be zero extended.
3466 Tmp1 = PromoteOp(Node->getOperand(0));
3467 Tmp2 = PromoteOp(Node->getOperand(1));
3468 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003469 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3470 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003471 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3472 break;
3473
3474 case ISD::SHL:
3475 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003476 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003477 break;
3478 case ISD::SRA:
3479 // The input value must be properly sign extended.
3480 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003481 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3482 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003483 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003484 break;
3485 case ISD::SRL:
3486 // The input value must be properly zero extended.
3487 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003488 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003489 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003490 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003491
3492 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003493 Tmp1 = Node->getOperand(0); // Get the chain.
3494 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003495 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3496 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3497 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3498 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003499 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003500 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003501 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003502 // Increment the pointer, VAList, to the next vaarg
3503 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3504 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3505 TLI.getPointerTy()));
3506 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003507 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3508 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003509 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003510 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003511 }
3512 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003513 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003514 break;
3515
Evan Cheng466685d2006-10-09 20:57:25 +00003516 case ISD::LOAD: {
3517 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003518 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3519 ? ISD::EXTLOAD : LD->getExtensionType();
3520 Result = DAG.getExtLoad(ExtType, NVT,
3521 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003522 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003523 LD->getLoadedVT(),
3524 LD->isVolatile(),
3525 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003526 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003527 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003528 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003529 }
Chris Lattner03c85462005-01-15 05:21:40 +00003530 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003531 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3532 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003533 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003534 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003535 case ISD::SELECT_CC:
3536 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3537 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3538 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003539 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003540 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003541 case ISD::BSWAP:
3542 Tmp1 = Node->getOperand(0);
3543 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3544 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3545 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003546 DAG.getConstant(MVT::getSizeInBits(NVT) -
3547 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003548 TLI.getShiftAmountTy()));
3549 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003550 case ISD::CTPOP:
3551 case ISD::CTTZ:
3552 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003553 // Zero extend the argument
3554 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003555 // Perform the larger operation, then subtract if needed.
3556 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003557 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003558 case ISD::CTPOP:
3559 Result = Tmp1;
3560 break;
3561 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003562 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003563 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003564 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3565 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003566 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003567 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003568 break;
3569 case ISD::CTLZ:
3570 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003571 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003572 DAG.getConstant(MVT::getSizeInBits(NVT) -
3573 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003574 break;
3575 }
3576 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003577 case ISD::EXTRACT_SUBVECTOR:
3578 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003579 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003580 case ISD::EXTRACT_VECTOR_ELT:
3581 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3582 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003583 }
3584
3585 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003586
3587 // Make sure the result is itself legal.
3588 Result = LegalizeOp(Result);
3589
3590 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003591 AddPromotedOperand(Op, Result);
3592 return Result;
3593}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003594
Dan Gohman7f321562007-06-25 16:23:39 +00003595/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3596/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3597/// based on the vector type. The return type of this matches the element type
3598/// of the vector, which may not be legal for the target.
3599SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003600 // We know that operand #0 is the Vec vector. If the index is a constant
3601 // or if the invec is a supported hardware type, we can use it. Otherwise,
3602 // lower to a store then an indexed load.
3603 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003604 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003605
3606 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003607 MVT::ValueType TVT = InVal->getValueType(0);
3608 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003609
Dan Gohman7f321562007-06-25 16:23:39 +00003610 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3611 default: assert(0 && "This action is not supported yet!");
3612 case TargetLowering::Custom: {
3613 Vec = LegalizeOp(Vec);
3614 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3615 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3616 if (Tmp3.Val)
3617 return Tmp3;
3618 break;
3619 }
3620 case TargetLowering::Legal:
3621 if (isTypeLegal(TVT)) {
3622 Vec = LegalizeOp(Vec);
3623 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003624 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003625 }
3626 break;
3627 case TargetLowering::Expand:
3628 break;
3629 }
3630
3631 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003632 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003633 Op = ScalarizeVectorOp(Vec);
3634 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3635 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003636 SDOperand Lo, Hi;
3637 SplitVectorOp(Vec, Lo, Hi);
3638 if (CIdx->getValue() < NumElems/2) {
3639 Vec = Lo;
3640 } else {
3641 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003642 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3643 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003644 }
Dan Gohman7f321562007-06-25 16:23:39 +00003645
Chris Lattner15972212006-03-31 17:55:51 +00003646 // It's now an extract from the appropriate high or low part. Recurse.
3647 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003648 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003649 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003650 // Store the value to a temporary stack slot, then LOAD the scalar
3651 // element back out.
3652 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3653 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3654
3655 // Add the offset to the index.
3656 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3657 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3658 DAG.getConstant(EltSize, Idx.getValueType()));
3659 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3660
3661 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003662 }
Dan Gohman7f321562007-06-25 16:23:39 +00003663 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003664}
3665
Dan Gohman7f321562007-06-25 16:23:39 +00003666/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003667/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003668SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003669 // We know that operand #0 is the Vec vector. For now we assume the index
3670 // is a constant and that the extracted result is a supported hardware type.
3671 SDOperand Vec = Op.getOperand(0);
3672 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3673
Dan Gohman7f321562007-06-25 16:23:39 +00003674 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003675
3676 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3677 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003678 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003679 }
3680
3681 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3682 SDOperand Lo, Hi;
3683 SplitVectorOp(Vec, Lo, Hi);
3684 if (CIdx->getValue() < NumElems/2) {
3685 Vec = Lo;
3686 } else {
3687 Vec = Hi;
3688 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3689 }
3690
3691 // It's now an extract from the appropriate high or low part. Recurse.
3692 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003693 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003694}
3695
Nate Begeman750ac1b2006-02-01 07:19:44 +00003696/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3697/// with condition CC on the current target. This usually involves legalizing
3698/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3699/// there may be no choice but to create a new SetCC node to represent the
3700/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3701/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3702void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3703 SDOperand &RHS,
3704 SDOperand &CC) {
3705 SDOperand Tmp1, Tmp2, Result;
3706
3707 switch (getTypeAction(LHS.getValueType())) {
3708 case Legal:
3709 Tmp1 = LegalizeOp(LHS); // LHS
3710 Tmp2 = LegalizeOp(RHS); // RHS
3711 break;
3712 case Promote:
3713 Tmp1 = PromoteOp(LHS); // LHS
3714 Tmp2 = PromoteOp(RHS); // RHS
3715
3716 // If this is an FP compare, the operands have already been extended.
3717 if (MVT::isInteger(LHS.getValueType())) {
3718 MVT::ValueType VT = LHS.getValueType();
3719 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3720
3721 // Otherwise, we have to insert explicit sign or zero extends. Note
3722 // that we could insert sign extends for ALL conditions, but zero extend
3723 // is cheaper on many machines (an AND instead of two shifts), so prefer
3724 // it.
3725 switch (cast<CondCodeSDNode>(CC)->get()) {
3726 default: assert(0 && "Unknown integer comparison!");
3727 case ISD::SETEQ:
3728 case ISD::SETNE:
3729 case ISD::SETUGE:
3730 case ISD::SETUGT:
3731 case ISD::SETULE:
3732 case ISD::SETULT:
3733 // ALL of these operations will work if we either sign or zero extend
3734 // the operands (including the unsigned comparisons!). Zero extend is
3735 // usually a simpler/cheaper operation, so prefer it.
3736 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3737 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3738 break;
3739 case ISD::SETGE:
3740 case ISD::SETGT:
3741 case ISD::SETLT:
3742 case ISD::SETLE:
3743 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3744 DAG.getValueType(VT));
3745 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3746 DAG.getValueType(VT));
3747 break;
3748 }
3749 }
3750 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003751 case Expand: {
3752 MVT::ValueType VT = LHS.getValueType();
3753 if (VT == MVT::f32 || VT == MVT::f64) {
3754 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003755 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003756 switch (cast<CondCodeSDNode>(CC)->get()) {
3757 case ISD::SETEQ:
3758 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003759 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003760 break;
3761 case ISD::SETNE:
3762 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003763 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003764 break;
3765 case ISD::SETGE:
3766 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003767 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003768 break;
3769 case ISD::SETLT:
3770 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003771 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003772 break;
3773 case ISD::SETLE:
3774 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003775 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003776 break;
3777 case ISD::SETGT:
3778 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003779 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003780 break;
3781 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003782 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3783 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003784 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003785 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003786 break;
3787 default:
Evan Cheng56966222007-01-12 02:11:51 +00003788 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003789 switch (cast<CondCodeSDNode>(CC)->get()) {
3790 case ISD::SETONE:
3791 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003792 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003793 // Fallthrough
3794 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003795 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003796 break;
3797 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003798 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003799 break;
3800 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003801 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003802 break;
3803 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003804 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003805 break;
Evan Cheng56966222007-01-12 02:11:51 +00003806 case ISD::SETUEQ:
3807 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00003808 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003809 default: assert(0 && "Unsupported FP setcc!");
3810 }
3811 }
3812
3813 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003814 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003815 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003816 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003817 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00003818 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00003819 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003820 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003821 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003822 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003823 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003824 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00003825 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00003826 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3827 Tmp2 = SDOperand();
3828 }
3829 LHS = Tmp1;
3830 RHS = Tmp2;
3831 return;
3832 }
3833
Nate Begeman750ac1b2006-02-01 07:19:44 +00003834 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3835 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003836 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003837 switch (cast<CondCodeSDNode>(CC)->get()) {
3838 case ISD::SETEQ:
3839 case ISD::SETNE:
3840 if (RHSLo == RHSHi)
3841 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3842 if (RHSCST->isAllOnesValue()) {
3843 // Comparison to -1.
3844 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3845 Tmp2 = RHSLo;
3846 break;
3847 }
3848
3849 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3850 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3851 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3852 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3853 break;
3854 default:
3855 // If this is a comparison of the sign bit, just look at the top part.
3856 // X > -1, x < 0
3857 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3858 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3859 CST->getValue() == 0) || // X < 0
3860 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3861 CST->isAllOnesValue())) { // X > -1
3862 Tmp1 = LHSHi;
3863 Tmp2 = RHSHi;
3864 break;
3865 }
3866
3867 // FIXME: This generated code sucks.
3868 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00003869 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3870 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003871 default: assert(0 && "Unknown integer setcc!");
3872 case ISD::SETLT:
3873 case ISD::SETULT: LowCC = ISD::SETULT; break;
3874 case ISD::SETGT:
3875 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3876 case ISD::SETLE:
3877 case ISD::SETULE: LowCC = ISD::SETULE; break;
3878 case ISD::SETGE:
3879 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3880 }
3881
3882 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3883 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3884 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3885
3886 // NOTE: on targets without efficient SELECT of bools, we can always use
3887 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00003888 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3889 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3890 false, DagCombineInfo);
3891 if (!Tmp1.Val)
3892 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3893 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3894 CCCode, false, DagCombineInfo);
3895 if (!Tmp2.Val)
3896 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3897
3898 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3899 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3900 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3901 (Tmp2C && Tmp2C->getValue() == 0 &&
3902 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3903 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3904 (Tmp2C && Tmp2C->getValue() == 1 &&
3905 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3906 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3907 // low part is known false, returns high part.
3908 // For LE / GE, if high part is known false, ignore the low part.
3909 // For LT / GT, if high part is known true, ignore the low part.
3910 Tmp1 = Tmp2;
3911 Tmp2 = SDOperand();
3912 } else {
3913 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3914 ISD::SETEQ, false, DagCombineInfo);
3915 if (!Result.Val)
3916 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3917 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3918 Result, Tmp1, Tmp2));
3919 Tmp1 = Result;
3920 Tmp2 = SDOperand();
3921 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003922 }
3923 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003924 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003925 LHS = Tmp1;
3926 RHS = Tmp2;
3927}
3928
Chris Lattner35481892005-12-23 00:16:34 +00003929/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003930/// The resultant code need not be legal. Note that SrcOp is the input operand
3931/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003932SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3933 SDOperand SrcOp) {
3934 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003935 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003936
3937 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003938 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003939 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003940 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003941}
3942
Chris Lattner4352cc92006-04-04 17:23:26 +00003943SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3944 // Create a vector sized/aligned stack slot, store the value to element #0,
3945 // then load the whole vector back out.
3946 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003947 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003948 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003949 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003950}
3951
3952
Chris Lattnerce872152006-03-19 06:31:19 +00003953/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00003954/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00003955SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3956
3957 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003958 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003959 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003960 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003961 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003962 std::map<SDOperand, std::vector<unsigned> > Values;
3963 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003964 bool isConstant = true;
3965 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3966 SplatValue.getOpcode() != ISD::UNDEF)
3967 isConstant = false;
3968
Evan Cheng033e6812006-03-24 01:17:21 +00003969 for (unsigned i = 1; i < NumElems; ++i) {
3970 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003971 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003972 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003973 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003974 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003975 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003976
3977 // If this isn't a constant element or an undef, we can't use a constant
3978 // pool load.
3979 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3980 V.getOpcode() != ISD::UNDEF)
3981 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003982 }
3983
3984 if (isOnlyLowElement) {
3985 // If the low element is an undef too, then this whole things is an undef.
3986 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3987 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3988 // Otherwise, turn this into a scalar_to_vector node.
3989 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3990 Node->getOperand(0));
3991 }
3992
Chris Lattner2eb86532006-03-24 07:29:17 +00003993 // If all elements are constants, create a load from the constant pool.
3994 if (isConstant) {
3995 MVT::ValueType VT = Node->getValueType(0);
3996 const Type *OpNTy =
3997 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3998 std::vector<Constant*> CV;
3999 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4000 if (ConstantFPSDNode *V =
4001 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4002 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4003 } else if (ConstantSDNode *V =
4004 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004005 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004006 } else {
4007 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4008 CV.push_back(UndefValue::get(OpNTy));
4009 }
4010 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004011 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004012 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004013 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004014 }
4015
Chris Lattner87100e02006-03-20 01:52:29 +00004016 if (SplatValue.Val) { // Splat of one value?
4017 // Build the shuffle constant vector: <0, 0, 0, 0>
4018 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004019 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004020 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004021 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004022 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4023 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004024
4025 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004026 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004027 // Get the splatted value into the low element of a vector register.
4028 SDOperand LowValVec =
4029 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4030
4031 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4032 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4033 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4034 SplatMask);
4035 }
4036 }
4037
Evan Cheng033e6812006-03-24 01:17:21 +00004038 // If there are only two unique elements, we may be able to turn this into a
4039 // vector shuffle.
4040 if (Values.size() == 2) {
4041 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4042 MVT::ValueType MaskVT =
4043 MVT::getIntVectorWithNumElements(NumElems);
4044 std::vector<SDOperand> MaskVec(NumElems);
4045 unsigned i = 0;
4046 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4047 E = Values.end(); I != E; ++I) {
4048 for (std::vector<unsigned>::iterator II = I->second.begin(),
4049 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004050 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004051 i += NumElems;
4052 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004053 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4054 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004055
4056 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004057 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4058 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004059 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004060 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4061 E = Values.end(); I != E; ++I) {
4062 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4063 I->first);
4064 Ops.push_back(Op);
4065 }
4066 Ops.push_back(ShuffleMask);
4067
4068 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004069 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4070 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004071 }
4072 }
Chris Lattnerce872152006-03-19 06:31:19 +00004073
4074 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4075 // aligned object on the stack, store each element into it, then load
4076 // the result as a vector.
4077 MVT::ValueType VT = Node->getValueType(0);
4078 // Create the stack frame object.
4079 SDOperand FIPtr = CreateStackTemporary(VT);
4080
4081 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004082 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004083 unsigned TypeByteSize =
4084 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004085 // Store (in the right endianness) the elements to memory.
4086 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4087 // Ignore undef elements.
4088 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4089
Chris Lattner841c8822006-03-22 01:46:54 +00004090 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004091
4092 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4093 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4094
Evan Cheng786225a2006-10-05 23:01:46 +00004095 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004096 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004097 }
4098
4099 SDOperand StoreChain;
4100 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004101 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4102 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004103 else
4104 StoreChain = DAG.getEntryNode();
4105
4106 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004107 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004108}
4109
4110/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4111/// specified value type.
4112SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4113 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4114 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004115 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004116 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004117 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004118 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4119}
4120
Chris Lattner5b359c62005-04-02 04:00:59 +00004121void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4122 SDOperand Op, SDOperand Amt,
4123 SDOperand &Lo, SDOperand &Hi) {
4124 // Expand the subcomponents.
4125 SDOperand LHSL, LHSH;
4126 ExpandOp(Op, LHSL, LHSH);
4127
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004128 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004129 MVT::ValueType VT = LHSL.getValueType();
4130 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004131 Hi = Lo.getValue(1);
4132}
4133
4134
Chris Lattnere34b3962005-01-19 04:19:40 +00004135/// ExpandShift - Try to find a clever way to expand this shift operation out to
4136/// smaller elements. If we can't find a way that is more efficient than a
4137/// libcall on this target, return false. Otherwise, return true with the
4138/// low-parts expanded into Lo and Hi.
4139bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4140 SDOperand &Lo, SDOperand &Hi) {
4141 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4142 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004143
Chris Lattnere34b3962005-01-19 04:19:40 +00004144 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004145 SDOperand ShAmt = LegalizeOp(Amt);
4146 MVT::ValueType ShTy = ShAmt.getValueType();
4147 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4148 unsigned NVTBits = MVT::getSizeInBits(NVT);
4149
4150 // Handle the case when Amt is an immediate. Other cases are currently broken
4151 // and are disabled.
4152 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4153 unsigned Cst = CN->getValue();
4154 // Expand the incoming operand to be shifted, so that we have its parts
4155 SDOperand InL, InH;
4156 ExpandOp(Op, InL, InH);
4157 switch(Opc) {
4158 case ISD::SHL:
4159 if (Cst > VTBits) {
4160 Lo = DAG.getConstant(0, NVT);
4161 Hi = DAG.getConstant(0, NVT);
4162 } else if (Cst > NVTBits) {
4163 Lo = DAG.getConstant(0, NVT);
4164 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004165 } else if (Cst == NVTBits) {
4166 Lo = DAG.getConstant(0, NVT);
4167 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004168 } else {
4169 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4170 Hi = DAG.getNode(ISD::OR, NVT,
4171 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4172 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4173 }
4174 return true;
4175 case ISD::SRL:
4176 if (Cst > VTBits) {
4177 Lo = DAG.getConstant(0, NVT);
4178 Hi = DAG.getConstant(0, NVT);
4179 } else if (Cst > NVTBits) {
4180 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4181 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004182 } else if (Cst == NVTBits) {
4183 Lo = InH;
4184 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004185 } else {
4186 Lo = DAG.getNode(ISD::OR, NVT,
4187 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4188 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4189 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4190 }
4191 return true;
4192 case ISD::SRA:
4193 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004194 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004195 DAG.getConstant(NVTBits-1, ShTy));
4196 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004197 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004198 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004199 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004200 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004201 } else if (Cst == NVTBits) {
4202 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004203 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004204 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004205 } else {
4206 Lo = DAG.getNode(ISD::OR, NVT,
4207 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4208 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4209 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4210 }
4211 return true;
4212 }
4213 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004214
4215 // Okay, the shift amount isn't constant. However, if we can tell that it is
4216 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4217 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004218 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004219
4220 // If we know that the high bit of the shift amount is one, then we can do
4221 // this as a couple of simple shifts.
4222 if (KnownOne & Mask) {
4223 // Mask out the high bit, which we know is set.
4224 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4225 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4226
4227 // Expand the incoming operand to be shifted, so that we have its parts
4228 SDOperand InL, InH;
4229 ExpandOp(Op, InL, InH);
4230 switch(Opc) {
4231 case ISD::SHL:
4232 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4233 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4234 return true;
4235 case ISD::SRL:
4236 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4237 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4238 return true;
4239 case ISD::SRA:
4240 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4241 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4242 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4243 return true;
4244 }
4245 }
4246
4247 // If we know that the high bit of the shift amount is zero, then we can do
4248 // this as a couple of simple shifts.
4249 if (KnownZero & Mask) {
4250 // Compute 32-amt.
4251 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4252 DAG.getConstant(NVTBits, Amt.getValueType()),
4253 Amt);
4254
4255 // Expand the incoming operand to be shifted, so that we have its parts
4256 SDOperand InL, InH;
4257 ExpandOp(Op, InL, InH);
4258 switch(Opc) {
4259 case ISD::SHL:
4260 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4261 Hi = DAG.getNode(ISD::OR, NVT,
4262 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4263 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4264 return true;
4265 case ISD::SRL:
4266 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4267 Lo = DAG.getNode(ISD::OR, NVT,
4268 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4269 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4270 return true;
4271 case ISD::SRA:
4272 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4273 Lo = DAG.getNode(ISD::OR, NVT,
4274 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4275 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4276 return true;
4277 }
4278 }
4279
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004280 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004281}
Chris Lattner77e77a62005-01-21 06:05:23 +00004282
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004283
Chris Lattner77e77a62005-01-21 06:05:23 +00004284// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4285// does not fit into a register, return the lo part and set the hi part to the
4286// by-reg argument. If it does fit into a single register, return the result
4287// and leave the Hi part unset.
4288SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004289 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004290 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4291 // The input chain to this libcall is the entry node of the function.
4292 // Legalizing the call will automatically add the previous call to the
4293 // dependence.
4294 SDOperand InChain = DAG.getEntryNode();
4295
Chris Lattner77e77a62005-01-21 06:05:23 +00004296 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004297 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004298 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4299 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4300 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004301 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004302 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004303 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004304 }
4305 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004306
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004307 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004308 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004309 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004310 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004311 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004312
Chris Lattner6831a812006-02-13 09:18:02 +00004313 // Legalize the call sequence, starting with the chain. This will advance
4314 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4315 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4316 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004317 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004318 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004319 default: assert(0 && "Unknown thing");
4320 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004321 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004322 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004323 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004324 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004325 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004326 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004327 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004328}
4329
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004330
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004331/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4332///
Chris Lattner77e77a62005-01-21 06:05:23 +00004333SDOperand SelectionDAGLegalize::
4334ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004335 assert(getTypeAction(Source.getValueType()) == Expand &&
4336 "This is not an expansion!");
4337 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4338
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004339 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004340 assert(Source.getValueType() == MVT::i64 &&
4341 "This only works for 64-bit -> FP");
4342 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4343 // incoming integer is set. To handle this, we dynamically test to see if
4344 // it is set, and, if so, add a fudge factor.
4345 SDOperand Lo, Hi;
4346 ExpandOp(Source, Lo, Hi);
4347
Chris Lattner66de05b2005-05-13 04:45:13 +00004348 // If this is unsigned, and not supported, first perform the conversion to
4349 // signed, then adjust the result if the sign bit is set.
4350 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4351 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4352
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004353 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4354 DAG.getConstant(0, Hi.getValueType()),
4355 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004356 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4357 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4358 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004359 uint64_t FF = 0x5f800000ULL;
4360 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004361 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004362
Chris Lattner5839bf22005-08-26 17:15:30 +00004363 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004364 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4365 SDOperand FudgeInReg;
4366 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004367 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004368 else {
4369 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004370 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004371 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004372 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004373 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004374 MVT::ValueType SCVT = SignedConv.getValueType();
4375 if (SCVT != DestTy) {
4376 // Destination type needs to be expanded as well. The FADD now we are
4377 // constructing will be expanded into a libcall.
4378 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4379 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4380 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4381 SignedConv, SignedConv.getValue(1));
4382 }
4383 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4384 }
Chris Lattner473a9902005-09-29 06:44:39 +00004385 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004386 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004387
Chris Lattnera88a2602005-05-14 05:33:54 +00004388 // Check to see if the target has a custom way to lower this. If so, use it.
4389 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4390 default: assert(0 && "This action not implemented for this operation!");
4391 case TargetLowering::Legal:
4392 case TargetLowering::Expand:
4393 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004394 case TargetLowering::Custom: {
4395 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4396 Source), DAG);
4397 if (NV.Val)
4398 return LegalizeOp(NV);
4399 break; // The target decided this was legal after all
4400 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004401 }
4402
Chris Lattner13689e22005-05-12 07:00:44 +00004403 // Expand the source, then glue it back together for the call. We must expand
4404 // the source in case it is shared (this pass of legalize must traverse it).
4405 SDOperand SrcLo, SrcHi;
4406 ExpandOp(Source, SrcLo, SrcHi);
4407 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4408
Evan Cheng56966222007-01-12 02:11:51 +00004409 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004410 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004411 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004412 else {
4413 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004414 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004415 }
Chris Lattner6831a812006-02-13 09:18:02 +00004416
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004417 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004418 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4419 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004420 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4421 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004422}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004423
Chris Lattner22cde6a2006-01-28 08:25:58 +00004424/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4425/// INT_TO_FP operation of the specified operand when the target requests that
4426/// we expand it. At this point, we know that the result and operand types are
4427/// legal for the target.
4428SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4429 SDOperand Op0,
4430 MVT::ValueType DestVT) {
4431 if (Op0.getValueType() == MVT::i32) {
4432 // simple 32-bit [signed|unsigned] integer to float/double expansion
4433
Chris Lattner58092e32007-01-20 22:35:55 +00004434 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004435 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004436 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4437 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004438 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004439 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004440 // get address of 8 byte buffer
4441 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4442 // word offset constant for Hi/Lo address computation
4443 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4444 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004445 SDOperand Hi = StackSlot;
4446 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4447 if (TLI.isLittleEndian())
4448 std::swap(Hi, Lo);
4449
Chris Lattner22cde6a2006-01-28 08:25:58 +00004450 // if signed map to unsigned space
4451 SDOperand Op0Mapped;
4452 if (isSigned) {
4453 // constant used to invert sign bit (signed to unsigned mapping)
4454 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4455 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4456 } else {
4457 Op0Mapped = Op0;
4458 }
4459 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004460 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004461 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004462 // initial hi portion of constructed double
4463 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4464 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004465 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004466 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004467 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004468 // FP constant to bias correct the final result
4469 SDOperand Bias = DAG.getConstantFP(isSigned ?
4470 BitsToDouble(0x4330000080000000ULL)
4471 : BitsToDouble(0x4330000000000000ULL),
4472 MVT::f64);
4473 // subtract the bias
4474 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4475 // final result
4476 SDOperand Result;
4477 // handle final rounding
4478 if (DestVT == MVT::f64) {
4479 // do nothing
4480 Result = Sub;
4481 } else {
4482 // if f32 then cast to f32
4483 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4484 }
4485 return Result;
4486 }
4487 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4488 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4489
4490 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4491 DAG.getConstant(0, Op0.getValueType()),
4492 ISD::SETLT);
4493 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4494 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4495 SignSet, Four, Zero);
4496
4497 // If the sign bit of the integer is set, the large number will be treated
4498 // as a negative number. To counteract this, the dynamic code adds an
4499 // offset depending on the data type.
4500 uint64_t FF;
4501 switch (Op0.getValueType()) {
4502 default: assert(0 && "Unsupported integer type!");
4503 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4504 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4505 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4506 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4507 }
4508 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004509 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004510
4511 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4512 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4513 SDOperand FudgeInReg;
4514 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004515 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004516 else {
4517 assert(DestVT == MVT::f64 && "Unexpected conversion");
4518 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4519 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004520 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004521 }
4522
4523 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4524}
4525
4526/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4527/// *INT_TO_FP operation of the specified operand when the target requests that
4528/// we promote it. At this point, we know that the result and operand types are
4529/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4530/// operation that takes a larger input.
4531SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4532 MVT::ValueType DestVT,
4533 bool isSigned) {
4534 // First step, figure out the appropriate *INT_TO_FP operation to use.
4535 MVT::ValueType NewInTy = LegalOp.getValueType();
4536
4537 unsigned OpToUse = 0;
4538
4539 // Scan for the appropriate larger type to use.
4540 while (1) {
4541 NewInTy = (MVT::ValueType)(NewInTy+1);
4542 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4543
4544 // If the target supports SINT_TO_FP of this type, use it.
4545 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4546 default: break;
4547 case TargetLowering::Legal:
4548 if (!TLI.isTypeLegal(NewInTy))
4549 break; // Can't use this datatype.
4550 // FALL THROUGH.
4551 case TargetLowering::Custom:
4552 OpToUse = ISD::SINT_TO_FP;
4553 break;
4554 }
4555 if (OpToUse) break;
4556 if (isSigned) continue;
4557
4558 // If the target supports UINT_TO_FP of this type, use it.
4559 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4560 default: break;
4561 case TargetLowering::Legal:
4562 if (!TLI.isTypeLegal(NewInTy))
4563 break; // Can't use this datatype.
4564 // FALL THROUGH.
4565 case TargetLowering::Custom:
4566 OpToUse = ISD::UINT_TO_FP;
4567 break;
4568 }
4569 if (OpToUse) break;
4570
4571 // Otherwise, try a larger type.
4572 }
4573
4574 // Okay, we found the operation and type to use. Zero extend our input to the
4575 // desired type then run the operation on it.
4576 return DAG.getNode(OpToUse, DestVT,
4577 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4578 NewInTy, LegalOp));
4579}
4580
4581/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4582/// FP_TO_*INT operation of the specified operand when the target requests that
4583/// we promote it. At this point, we know that the result and operand types are
4584/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4585/// operation that returns a larger result.
4586SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4587 MVT::ValueType DestVT,
4588 bool isSigned) {
4589 // First step, figure out the appropriate FP_TO*INT operation to use.
4590 MVT::ValueType NewOutTy = DestVT;
4591
4592 unsigned OpToUse = 0;
4593
4594 // Scan for the appropriate larger type to use.
4595 while (1) {
4596 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4597 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4598
4599 // If the target supports FP_TO_SINT returning this type, use it.
4600 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4601 default: break;
4602 case TargetLowering::Legal:
4603 if (!TLI.isTypeLegal(NewOutTy))
4604 break; // Can't use this datatype.
4605 // FALL THROUGH.
4606 case TargetLowering::Custom:
4607 OpToUse = ISD::FP_TO_SINT;
4608 break;
4609 }
4610 if (OpToUse) break;
4611
4612 // If the target supports FP_TO_UINT of this type, use it.
4613 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4614 default: break;
4615 case TargetLowering::Legal:
4616 if (!TLI.isTypeLegal(NewOutTy))
4617 break; // Can't use this datatype.
4618 // FALL THROUGH.
4619 case TargetLowering::Custom:
4620 OpToUse = ISD::FP_TO_UINT;
4621 break;
4622 }
4623 if (OpToUse) break;
4624
4625 // Otherwise, try a larger type.
4626 }
4627
4628 // Okay, we found the operation and type to use. Truncate the result of the
4629 // extended FP_TO_*INT operation to the desired size.
4630 return DAG.getNode(ISD::TRUNCATE, DestVT,
4631 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4632}
4633
4634/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4635///
4636SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4637 MVT::ValueType VT = Op.getValueType();
4638 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4639 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4640 switch (VT) {
4641 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4642 case MVT::i16:
4643 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4644 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4645 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4646 case MVT::i32:
4647 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4648 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4649 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4650 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4651 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4652 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4653 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4654 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4655 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4656 case MVT::i64:
4657 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4658 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4659 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4660 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4661 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4662 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4663 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4664 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4665 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4666 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4667 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4668 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4669 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4670 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4671 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4672 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4673 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4674 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4675 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4676 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4677 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4678 }
4679}
4680
4681/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4682///
4683SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4684 switch (Opc) {
4685 default: assert(0 && "Cannot expand this yet!");
4686 case ISD::CTPOP: {
4687 static const uint64_t mask[6] = {
4688 0x5555555555555555ULL, 0x3333333333333333ULL,
4689 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4690 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4691 };
4692 MVT::ValueType VT = Op.getValueType();
4693 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004694 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004695 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4696 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4697 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4698 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4699 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4700 DAG.getNode(ISD::AND, VT,
4701 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4702 }
4703 return Op;
4704 }
4705 case ISD::CTLZ: {
4706 // for now, we do this:
4707 // x = x | (x >> 1);
4708 // x = x | (x >> 2);
4709 // ...
4710 // x = x | (x >>16);
4711 // x = x | (x >>32); // for 64-bit input
4712 // return popcount(~x);
4713 //
4714 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4715 MVT::ValueType VT = Op.getValueType();
4716 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004717 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004718 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4719 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4720 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4721 }
4722 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4723 return DAG.getNode(ISD::CTPOP, VT, Op);
4724 }
4725 case ISD::CTTZ: {
4726 // for now, we use: { return popcount(~x & (x - 1)); }
4727 // unless the target has ctlz but not ctpop, in which case we use:
4728 // { return 32 - nlz(~x & (x-1)); }
4729 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4730 MVT::ValueType VT = Op.getValueType();
4731 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4732 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4733 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4734 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4735 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4736 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4737 TLI.isOperationLegal(ISD::CTLZ, VT))
4738 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004739 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004740 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4741 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4742 }
4743 }
4744}
Chris Lattnere34b3962005-01-19 04:19:40 +00004745
Chris Lattner3e928bb2005-01-07 07:47:09 +00004746/// ExpandOp - Expand the specified SDOperand into its two component pieces
4747/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4748/// LegalizeNodes map is filled in for any results that are not expanded, the
4749/// ExpandedNodes map is filled in for any results that are expanded, and the
4750/// Lo/Hi values are returned.
4751void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4752 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004753 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004754 SDNode *Node = Op.Val;
4755 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004756 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004757 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004758 "Cannot expand to FP value or to larger int value!");
4759
Chris Lattner6fdcb252005-09-02 20:32:45 +00004760 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004761 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004762 = ExpandedNodes.find(Op);
4763 if (I != ExpandedNodes.end()) {
4764 Lo = I->second.first;
4765 Hi = I->second.second;
4766 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004767 }
4768
Chris Lattner3e928bb2005-01-07 07:47:09 +00004769 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004770 case ISD::CopyFromReg:
4771 assert(0 && "CopyFromReg must be legal!");
4772 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004773#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004774 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004775#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004776 assert(0 && "Do not know how to expand this operator!");
4777 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004778 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004779 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004780 Lo = DAG.getNode(ISD::UNDEF, NVT);
4781 Hi = DAG.getNode(ISD::UNDEF, NVT);
4782 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004783 case ISD::Constant: {
4784 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4785 Lo = DAG.getConstant(Cst, NVT);
4786 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4787 break;
4788 }
Evan Cheng00495212006-12-12 21:32:44 +00004789 case ISD::ConstantFP: {
4790 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004791 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004792 if (getTypeAction(Lo.getValueType()) == Expand)
4793 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004794 break;
4795 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004796 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004797 // Return the operands.
4798 Lo = Node->getOperand(0);
4799 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004800 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004801
4802 case ISD::SIGN_EXTEND_INREG:
4803 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004804 // sext_inreg the low part if needed.
4805 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4806
4807 // The high part gets the sign extension from the lo-part. This handles
4808 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004809 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4810 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4811 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004812 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004813
Nate Begemand88fc032006-01-14 03:14:10 +00004814 case ISD::BSWAP: {
4815 ExpandOp(Node->getOperand(0), Lo, Hi);
4816 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4817 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4818 Lo = TempLo;
4819 break;
4820 }
4821
Chris Lattneredb1add2005-05-11 04:51:16 +00004822 case ISD::CTPOP:
4823 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004824 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4825 DAG.getNode(ISD::CTPOP, NVT, Lo),
4826 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004827 Hi = DAG.getConstant(0, NVT);
4828 break;
4829
Chris Lattner39a8f332005-05-12 19:05:01 +00004830 case ISD::CTLZ: {
4831 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004832 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004833 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4834 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004835 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4836 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004837 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4838 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4839
4840 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4841 Hi = DAG.getConstant(0, NVT);
4842 break;
4843 }
4844
4845 case ISD::CTTZ: {
4846 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004847 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004848 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4849 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004850 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4851 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004852 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4853 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4854
4855 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4856 Hi = DAG.getConstant(0, NVT);
4857 break;
4858 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004859
Nate Begemanacc398c2006-01-25 18:21:52 +00004860 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004861 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4862 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004863 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4864 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4865
4866 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004867 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004868 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4869 if (!TLI.isLittleEndian())
4870 std::swap(Lo, Hi);
4871 break;
4872 }
4873
Chris Lattner3e928bb2005-01-07 07:47:09 +00004874 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004875 LoadSDNode *LD = cast<LoadSDNode>(Node);
4876 SDOperand Ch = LD->getChain(); // Legalize the chain.
4877 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4878 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004879 int SVOffset = LD->getSrcValueOffset();
4880 unsigned Alignment = LD->getAlignment();
4881 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004882
Evan Cheng466685d2006-10-09 20:57:25 +00004883 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004884 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4885 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00004886 if (VT == MVT::f32 || VT == MVT::f64) {
4887 // f32->i32 or f64->i64 one to one expansion.
4888 // Remember that we legalized the chain.
4889 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004890 // Recursively expand the new load.
4891 if (getTypeAction(NVT) == Expand)
4892 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004893 break;
4894 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004895
Evan Cheng466685d2006-10-09 20:57:25 +00004896 // Increment the pointer to the other half.
4897 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4898 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4899 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00004900 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004901 if (Alignment > IncrementSize)
4902 Alignment = IncrementSize;
4903 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4904 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004905
Evan Cheng466685d2006-10-09 20:57:25 +00004906 // Build a factor node to remember that this load is independent of the
4907 // other one.
4908 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4909 Hi.getValue(1));
4910
4911 // Remember that we legalized the chain.
4912 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4913 if (!TLI.isLittleEndian())
4914 std::swap(Lo, Hi);
4915 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004916 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004917
4918 if (VT == MVT::f64 && EVT == MVT::f32) {
4919 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4920 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004921 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00004922 // Remember that we legalized the chain.
4923 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4924 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4925 break;
4926 }
Evan Cheng466685d2006-10-09 20:57:25 +00004927
4928 if (EVT == NVT)
4929 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004930 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004931 else
4932 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004933 SVOffset, EVT, isVolatile,
4934 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00004935
4936 // Remember that we legalized the chain.
4937 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4938
4939 if (ExtType == ISD::SEXTLOAD) {
4940 // The high part is obtained by SRA'ing all but one of the bits of the
4941 // lo part.
4942 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4943 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4944 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4945 } else if (ExtType == ISD::ZEXTLOAD) {
4946 // The high part is just a zero.
4947 Hi = DAG.getConstant(0, NVT);
4948 } else /* if (ExtType == ISD::EXTLOAD) */ {
4949 // The high part is undefined.
4950 Hi = DAG.getNode(ISD::UNDEF, NVT);
4951 }
4952 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004953 break;
4954 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004955 case ISD::AND:
4956 case ISD::OR:
4957 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4958 SDOperand LL, LH, RL, RH;
4959 ExpandOp(Node->getOperand(0), LL, LH);
4960 ExpandOp(Node->getOperand(1), RL, RH);
4961 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4962 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4963 break;
4964 }
4965 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004966 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004967 ExpandOp(Node->getOperand(1), LL, LH);
4968 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004969 if (getTypeAction(NVT) == Expand)
4970 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004971 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004972 if (VT != MVT::f32)
4973 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004974 break;
4975 }
Nate Begeman9373a812005-08-10 20:51:12 +00004976 case ISD::SELECT_CC: {
4977 SDOperand TL, TH, FL, FH;
4978 ExpandOp(Node->getOperand(2), TL, TH);
4979 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004980 if (getTypeAction(NVT) == Expand)
4981 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004982 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4983 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004984 if (VT != MVT::f32)
4985 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4986 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004987 break;
4988 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004989 case ISD::ANY_EXTEND:
4990 // The low part is any extension of the input (which degenerates to a copy).
4991 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4992 // The high part is undefined.
4993 Hi = DAG.getNode(ISD::UNDEF, NVT);
4994 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004995 case ISD::SIGN_EXTEND: {
4996 // The low part is just a sign extension of the input (which degenerates to
4997 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004998 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004999
Chris Lattner3e928bb2005-01-07 07:47:09 +00005000 // The high part is obtained by SRA'ing all but one of the bits of the lo
5001 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005002 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005003 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5004 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005005 break;
5006 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005007 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005008 // The low part is just a zero extension of the input (which degenerates to
5009 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005010 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005011
Chris Lattner3e928bb2005-01-07 07:47:09 +00005012 // The high part is just a zero.
5013 Hi = DAG.getConstant(0, NVT);
5014 break;
Chris Lattner35481892005-12-23 00:16:34 +00005015
Chris Lattner4c948eb2007-02-13 23:55:16 +00005016 case ISD::TRUNCATE: {
5017 // The input value must be larger than this value. Expand *it*.
5018 SDOperand NewLo;
5019 ExpandOp(Node->getOperand(0), NewLo, Hi);
5020
5021 // The low part is now either the right size, or it is closer. If not the
5022 // right size, make an illegal truncate so we recursively expand it.
5023 if (NewLo.getValueType() != Node->getValueType(0))
5024 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5025 ExpandOp(NewLo, Lo, Hi);
5026 break;
5027 }
5028
Chris Lattner35481892005-12-23 00:16:34 +00005029 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005030 SDOperand Tmp;
5031 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5032 // If the target wants to, allow it to lower this itself.
5033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5034 case Expand: assert(0 && "cannot expand FP!");
5035 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5036 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5037 }
5038 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5039 }
5040
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005041 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005042 if (VT == MVT::f32 || VT == MVT::f64) {
5043 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005044 if (getTypeAction(NVT) == Expand)
5045 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005046 break;
5047 }
5048
5049 // If source operand will be expanded to the same type as VT, i.e.
5050 // i64 <- f64, i32 <- f32, expand the source operand instead.
5051 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5052 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5053 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005054 break;
5055 }
5056
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005057 // Turn this into a load/store pair by default.
5058 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005059 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005060
Chris Lattner35481892005-12-23 00:16:34 +00005061 ExpandOp(Tmp, Lo, Hi);
5062 break;
5063 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005064
Chris Lattner8137c9e2006-01-28 05:07:51 +00005065 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005066 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5067 TargetLowering::Custom &&
5068 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005069 Lo = TLI.LowerOperation(Op, DAG);
5070 assert(Lo.Val && "Node must be custom expanded!");
5071 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005072 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005073 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005074 break;
5075
Chris Lattner4e6c7462005-01-08 19:27:05 +00005076 // These operators cannot be expanded directly, emit them as calls to
5077 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005078 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005079 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005080 SDOperand Op;
5081 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5082 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005083 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5084 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005085 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005086
Chris Lattnerf20d1832005-07-30 01:40:57 +00005087 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5088
Chris Lattner80a3e942005-07-29 00:33:32 +00005089 // Now that the custom expander is done, expand the result, which is still
5090 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005091 if (Op.Val) {
5092 ExpandOp(Op, Lo, Hi);
5093 break;
5094 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005095 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005096
Evan Cheng56966222007-01-12 02:11:51 +00005097 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005098 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005099 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005100 else
Evan Cheng56966222007-01-12 02:11:51 +00005101 LC = RTLIB::FPTOSINT_F64_I64;
5102 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5103 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005104 break;
Evan Cheng56966222007-01-12 02:11:51 +00005105 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005106
Evan Cheng56966222007-01-12 02:11:51 +00005107 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005108 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005109 SDOperand Op;
5110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5111 case Expand: assert(0 && "cannot expand FP!");
5112 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5113 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5114 }
5115
5116 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5117
5118 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005119 if (Op.Val) {
5120 ExpandOp(Op, Lo, Hi);
5121 break;
5122 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005123 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005124
Evan Cheng56966222007-01-12 02:11:51 +00005125 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005126 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005127 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005128 else
Evan Cheng56966222007-01-12 02:11:51 +00005129 LC = RTLIB::FPTOUINT_F64_I64;
5130 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5131 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005132 break;
Evan Cheng56966222007-01-12 02:11:51 +00005133 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005134
Evan Cheng05a2d562006-01-09 18:31:59 +00005135 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005136 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005137 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005138 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005139 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005140 Op = TLI.LowerOperation(Op, DAG);
5141 if (Op.Val) {
5142 // Now that the custom expander is done, expand the result, which is
5143 // still VT.
5144 ExpandOp(Op, Lo, Hi);
5145 break;
5146 }
5147 }
5148
Chris Lattner79980b02006-09-13 03:50:39 +00005149 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5150 // this X << 1 as X+X.
5151 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5152 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5153 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5154 SDOperand LoOps[2], HiOps[3];
5155 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5156 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5157 LoOps[1] = LoOps[0];
5158 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5159
5160 HiOps[1] = HiOps[0];
5161 HiOps[2] = Lo.getValue(1);
5162 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5163 break;
5164 }
5165 }
5166
Chris Lattnere34b3962005-01-19 04:19:40 +00005167 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005168 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005169 break;
Chris Lattner47599822005-04-02 03:38:53 +00005170
5171 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005172 TargetLowering::LegalizeAction Action =
5173 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5174 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5175 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005176 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005177 break;
5178 }
5179
Chris Lattnere34b3962005-01-19 04:19:40 +00005180 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005181 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5182 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005183 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005184 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005185
Evan Cheng05a2d562006-01-09 18:31:59 +00005186 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005187 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005188 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005189 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005190 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005191 Op = TLI.LowerOperation(Op, DAG);
5192 if (Op.Val) {
5193 // Now that the custom expander is done, expand the result, which is
5194 // still VT.
5195 ExpandOp(Op, Lo, Hi);
5196 break;
5197 }
5198 }
5199
Chris Lattnere34b3962005-01-19 04:19:40 +00005200 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005201 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005202 break;
Chris Lattner47599822005-04-02 03:38:53 +00005203
5204 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005205 TargetLowering::LegalizeAction Action =
5206 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5207 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5208 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005209 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005210 break;
5211 }
5212
Chris Lattnere34b3962005-01-19 04:19:40 +00005213 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005214 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5215 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005216 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005217 }
5218
5219 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005220 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005221 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005222 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005223 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005224 Op = TLI.LowerOperation(Op, DAG);
5225 if (Op.Val) {
5226 // Now that the custom expander is done, expand the result, which is
5227 // still VT.
5228 ExpandOp(Op, Lo, Hi);
5229 break;
5230 }
5231 }
5232
Chris Lattnere34b3962005-01-19 04:19:40 +00005233 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005234 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005235 break;
Chris Lattner47599822005-04-02 03:38:53 +00005236
5237 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005238 TargetLowering::LegalizeAction Action =
5239 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5240 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5241 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005242 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005243 break;
5244 }
5245
Chris Lattnere34b3962005-01-19 04:19:40 +00005246 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005247 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5248 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005249 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005250 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005251
Misha Brukmanedf128a2005-04-21 22:36:52 +00005252 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005253 case ISD::SUB: {
5254 // If the target wants to custom expand this, let them.
5255 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5256 TargetLowering::Custom) {
5257 Op = TLI.LowerOperation(Op, DAG);
5258 if (Op.Val) {
5259 ExpandOp(Op, Lo, Hi);
5260 break;
5261 }
5262 }
5263
5264 // Expand the subcomponents.
5265 SDOperand LHSL, LHSH, RHSL, RHSH;
5266 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5267 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005268 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5269 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005270 LoOps[0] = LHSL;
5271 LoOps[1] = RHSL;
5272 HiOps[0] = LHSH;
5273 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005274 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005275 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005276 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005277 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005278 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005279 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005280 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005281 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005282 }
Chris Lattner84f67882005-01-20 18:52:28 +00005283 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005284 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005285
5286 case ISD::ADDC:
5287 case ISD::SUBC: {
5288 // Expand the subcomponents.
5289 SDOperand LHSL, LHSH, RHSL, RHSH;
5290 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5291 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5292 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5293 SDOperand LoOps[2] = { LHSL, RHSL };
5294 SDOperand HiOps[3] = { LHSH, RHSH };
5295
5296 if (Node->getOpcode() == ISD::ADDC) {
5297 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5298 HiOps[2] = Lo.getValue(1);
5299 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5300 } else {
5301 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5302 HiOps[2] = Lo.getValue(1);
5303 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5304 }
5305 // Remember that we legalized the flag.
5306 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5307 break;
5308 }
5309 case ISD::ADDE:
5310 case ISD::SUBE: {
5311 // Expand the subcomponents.
5312 SDOperand LHSL, LHSH, RHSL, RHSH;
5313 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5314 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5315 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5316 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5317 SDOperand HiOps[3] = { LHSH, RHSH };
5318
5319 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5320 HiOps[2] = Lo.getValue(1);
5321 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5322
5323 // Remember that we legalized the flag.
5324 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5325 break;
5326 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005327 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005328 // If the target wants to custom expand this, let them.
5329 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005330 SDOperand New = TLI.LowerOperation(Op, DAG);
5331 if (New.Val) {
5332 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005333 break;
5334 }
5335 }
5336
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005337 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5338 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005339 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005340 SDOperand LL, LH, RL, RH;
5341 ExpandOp(Node->getOperand(0), LL, LH);
5342 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005343 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005344 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005345 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5346 // extended the sign bit of the low half through the upper half, and if so
5347 // emit a MULHS instead of the alternate sequence that is valid for any
5348 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005349 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005350 // is RH an extension of the sign bit of RL?
5351 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5352 RH.getOperand(1).getOpcode() == ISD::Constant &&
5353 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5354 // is LH an extension of the sign bit of LL?
5355 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5356 LH.getOperand(1).getOpcode() == ISD::Constant &&
5357 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005358 // Low part:
5359 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5360 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005361 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005362 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005363 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005364 // Low part:
5365 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5366
5367 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005368 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5369 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5370 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5371 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5372 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005373 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005374 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005375 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005376
Evan Cheng56966222007-01-12 02:11:51 +00005377 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5378 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005379 break;
5380 }
Evan Cheng56966222007-01-12 02:11:51 +00005381 case ISD::SDIV:
5382 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5383 break;
5384 case ISD::UDIV:
5385 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5386 break;
5387 case ISD::SREM:
5388 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5389 break;
5390 case ISD::UREM:
5391 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5392 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005393
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005394 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005395 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5396 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5397 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005398 break;
5399 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005400 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5401 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5402 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005403 break;
5404 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005405 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5406 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5407 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005408 break;
5409 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005410 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5411 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5412 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005413 break;
5414 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005415 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005416 break;
5417 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005418 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005419 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005420 case ISD::FSQRT:
5421 case ISD::FSIN:
5422 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005423 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005424 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005425 case ISD::FSQRT:
5426 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5427 break;
5428 case ISD::FSIN:
5429 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5430 break;
5431 case ISD::FCOS:
5432 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5433 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005434 default: assert(0 && "Unreachable!");
5435 }
Evan Cheng56966222007-01-12 02:11:51 +00005436 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005437 break;
5438 }
Evan Cheng966bf242006-12-16 00:52:40 +00005439 case ISD::FABS: {
5440 SDOperand Mask = (VT == MVT::f64)
5441 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5442 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5443 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5444 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5445 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5446 if (getTypeAction(NVT) == Expand)
5447 ExpandOp(Lo, Lo, Hi);
5448 break;
5449 }
5450 case ISD::FNEG: {
5451 SDOperand Mask = (VT == MVT::f64)
5452 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5453 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5454 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5455 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5456 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5457 if (getTypeAction(NVT) == Expand)
5458 ExpandOp(Lo, Lo, Hi);
5459 break;
5460 }
Evan Cheng912095b2007-01-04 21:56:39 +00005461 case ISD::FCOPYSIGN: {
5462 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5463 if (getTypeAction(NVT) == Expand)
5464 ExpandOp(Lo, Lo, Hi);
5465 break;
5466 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005467 case ISD::SINT_TO_FP:
5468 case ISD::UINT_TO_FP: {
5469 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5470 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005471 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005472 if (Node->getOperand(0).getValueType() == MVT::i64) {
5473 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005474 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005475 else
Evan Cheng56966222007-01-12 02:11:51 +00005476 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005477 } else {
5478 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005479 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005480 else
Evan Cheng56966222007-01-12 02:11:51 +00005481 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005482 }
5483
5484 // Promote the operand if needed.
5485 if (getTypeAction(SrcVT) == Promote) {
5486 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5487 Tmp = isSigned
5488 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5489 DAG.getValueType(SrcVT))
5490 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5491 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5492 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005493
5494 const char *LibCall = TLI.getLibcallName(LC);
5495 if (LibCall)
5496 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5497 else {
5498 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5499 Node->getOperand(0));
5500 if (getTypeAction(Lo.getValueType()) == Expand)
5501 ExpandOp(Lo, Lo, Hi);
5502 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005503 break;
5504 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005505 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005506
Chris Lattner83397362005-12-21 18:02:52 +00005507 // Make sure the resultant values have been legalized themselves, unless this
5508 // is a type that requires multi-step expansion.
5509 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5510 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005511 if (Hi.Val)
5512 // Don't legalize the high part if it is expanded to a single node.
5513 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005514 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005515
5516 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005517 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005518 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005519}
5520
Dan Gohman7f321562007-06-25 16:23:39 +00005521/// SplitVectorOp - Given an operand of vector type, break it down into
5522/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005523void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5524 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005525 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005526 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005527 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005528 assert(NumElements > 1 && "Cannot split a single element vector!");
5529 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005530 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5531 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005532
5533 // See if we already split it.
5534 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5535 = SplitNodes.find(Op);
5536 if (I != SplitNodes.end()) {
5537 Lo = I->second.first;
5538 Hi = I->second.second;
5539 return;
5540 }
5541
5542 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005543 default:
5544#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005545 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005546#endif
5547 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005548 case ISD::BUILD_PAIR:
5549 Lo = Node->getOperand(0);
5550 Hi = Node->getOperand(1);
5551 break;
5552 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005553 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5554 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005555 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005556
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005557 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005558 Node->op_end());
5559 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005560 break;
5561 }
Dan Gohman7f321562007-06-25 16:23:39 +00005562 case ISD::CONCAT_VECTORS: {
5563 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5564 if (NewNumSubvectors == 1) {
5565 Lo = Node->getOperand(0);
5566 Hi = Node->getOperand(1);
5567 } else {
5568 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5569 Node->op_begin()+NewNumSubvectors);
5570 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005571
Dan Gohman7f321562007-06-25 16:23:39 +00005572 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5573 Node->op_end());
5574 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5575 }
Dan Gohman65956352007-06-13 15:12:02 +00005576 break;
5577 }
Dan Gohman7f321562007-06-25 16:23:39 +00005578 case ISD::ADD:
5579 case ISD::SUB:
5580 case ISD::MUL:
5581 case ISD::FADD:
5582 case ISD::FSUB:
5583 case ISD::FMUL:
5584 case ISD::SDIV:
5585 case ISD::UDIV:
5586 case ISD::FDIV:
5587 case ISD::AND:
5588 case ISD::OR:
5589 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005590 SDOperand LL, LH, RL, RH;
5591 SplitVectorOp(Node->getOperand(0), LL, LH);
5592 SplitVectorOp(Node->getOperand(1), RL, RH);
5593
Dan Gohman7f321562007-06-25 16:23:39 +00005594 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5595 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005596 break;
5597 }
Dan Gohman7f321562007-06-25 16:23:39 +00005598 case ISD::LOAD: {
5599 LoadSDNode *LD = cast<LoadSDNode>(Node);
5600 SDOperand Ch = LD->getChain();
5601 SDOperand Ptr = LD->getBasePtr();
5602 const Value *SV = LD->getSrcValue();
5603 int SVOffset = LD->getSrcValueOffset();
5604 unsigned Alignment = LD->getAlignment();
5605 bool isVolatile = LD->isVolatile();
5606
5607 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5608 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005609 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5610 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005611 SVOffset += IncrementSize;
5612 if (Alignment > IncrementSize)
5613 Alignment = IncrementSize;
5614 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005615
5616 // Build a factor node to remember that this load is independent of the
5617 // other one.
5618 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5619 Hi.getValue(1));
5620
5621 // Remember that we legalized the chain.
5622 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005623 break;
5624 }
Dan Gohman7f321562007-06-25 16:23:39 +00005625 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005626 // We know the result is a vector. The input may be either a vector or a
5627 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005628 SDOperand InOp = Node->getOperand(0);
5629 if (!MVT::isVector(InOp.getValueType()) ||
5630 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5631 // The input is a scalar or single-element vector.
5632 // Lower to a store/load so that it can be split.
5633 // FIXME: this could be improved probably.
5634 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005635
Evan Cheng786225a2006-10-05 23:01:46 +00005636 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005637 InOp, Ptr, NULL, 0);
5638 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005639 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005640 // Split the vector and convert each of the pieces now.
5641 SplitVectorOp(InOp, Lo, Hi);
5642 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5643 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5644 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005645 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005646 }
5647
5648 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005649 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005650 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005651 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005652}
5653
5654
Dan Gohman89b20c02007-06-27 14:06:22 +00005655/// ScalarizeVectorOp - Given an operand of single-element vector type
5656/// (e.g. v1f32), convert it into the equivalent operation that returns a
5657/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005658SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5659 assert(MVT::isVector(Op.getValueType()) &&
5660 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005661 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005662 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5663 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005664
Dan Gohman7f321562007-06-25 16:23:39 +00005665 // See if we already scalarized it.
5666 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5667 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005668
5669 SDOperand Result;
5670 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005671 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005672#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005673 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005674#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005675 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5676 case ISD::ADD:
5677 case ISD::FADD:
5678 case ISD::SUB:
5679 case ISD::FSUB:
5680 case ISD::MUL:
5681 case ISD::FMUL:
5682 case ISD::SDIV:
5683 case ISD::UDIV:
5684 case ISD::FDIV:
5685 case ISD::SREM:
5686 case ISD::UREM:
5687 case ISD::FREM:
5688 case ISD::AND:
5689 case ISD::OR:
5690 case ISD::XOR:
5691 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005692 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005693 ScalarizeVectorOp(Node->getOperand(0)),
5694 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005695 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005696 case ISD::FNEG:
5697 case ISD::FABS:
5698 case ISD::FSQRT:
5699 case ISD::FSIN:
5700 case ISD::FCOS:
5701 Result = DAG.getNode(Node->getOpcode(),
5702 NewVT,
5703 ScalarizeVectorOp(Node->getOperand(0)));
5704 break;
5705 case ISD::LOAD: {
5706 LoadSDNode *LD = cast<LoadSDNode>(Node);
5707 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5708 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005709
Dan Gohman7f321562007-06-25 16:23:39 +00005710 const Value *SV = LD->getSrcValue();
5711 int SVOffset = LD->getSrcValueOffset();
5712 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5713 LD->isVolatile(), LD->getAlignment());
5714
Chris Lattnerc7029802006-03-18 01:44:44 +00005715 // Remember that we legalized the chain.
5716 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5717 break;
5718 }
Dan Gohman7f321562007-06-25 16:23:39 +00005719 case ISD::BUILD_VECTOR:
5720 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005721 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005722 case ISD::INSERT_VECTOR_ELT:
5723 // Returning the inserted scalar element.
5724 Result = Node->getOperand(1);
5725 break;
5726 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005727 assert(Node->getOperand(0).getValueType() == NewVT &&
5728 "Concat of non-legal vectors not yet supported!");
5729 Result = Node->getOperand(0);
5730 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005731 case ISD::VECTOR_SHUFFLE: {
5732 // Figure out if the scalar is the LHS or RHS and return it.
5733 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5734 if (cast<ConstantSDNode>(EltNum)->getValue())
5735 Result = ScalarizeVectorOp(Node->getOperand(1));
5736 else
5737 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005738 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005739 }
5740 case ISD::EXTRACT_SUBVECTOR:
5741 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005742 assert(Result.getValueType() == NewVT);
5743 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005744 case ISD::BIT_CONVERT:
5745 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005746 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005747 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005748 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005749 ScalarizeVectorOp(Op.getOperand(1)),
5750 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005751 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005752 }
5753
5754 if (TLI.isTypeLegal(NewVT))
5755 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005756 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5757 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005758 return Result;
5759}
5760
Chris Lattner3e928bb2005-01-07 07:47:09 +00005761
5762// SelectionDAG::Legalize - This is the entry point for the file.
5763//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005764void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005765 if (ViewLegalizeDAGs) viewGraph();
5766
Chris Lattner3e928bb2005-01-07 07:47:09 +00005767 /// run - This is the main entry point to this class.
5768 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005769 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005770}
5771