blob: 8e4d09af1104250762beb4c4d88f5b44f6e03505 [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"
Evan Cheng61bbbab2007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000032#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattner5e46a192006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattner3e928bb2005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Chris Lattner6831a812006-02-13 09:18:02 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Chris Lattner03c85462005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattnerc7029802006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohman7f321562007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000107
Chris Lattner8afc48e2005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Chris Lattner03c85462005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121public:
122
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner456a93a2006-01-28 07:39:30 +0000140private:
Dan Gohman7f321562007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000149
150 /// PromoteOp - Given an operation that produces a value in an invalid type,
151 /// promote it to compute the value into a larger type. The produced value
152 /// will have the correct bits for the low portion of the register, but no
153 /// guarantee is made about the top bits: it may be zero, sign-extended, or
154 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000155 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// ExpandOp - Expand the specified SDOperand into its two component pieces
158 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
159 /// the LegalizeNodes map is filled in for any results that are not expanded,
160 /// the ExpandedNodes map is filled in for any results that are expanded, and
161 /// the Lo/Hi values are returned. This applies to integer types and Vector
162 /// types.
163 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
Dan Gohman7f321562007-06-25 16:23:39 +0000165 /// SplitVectorOp - Given an operand of vector type, break it down into
166 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000167 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168
Dan Gohman89b20c02007-06-27 14:06:22 +0000169 /// ScalarizeVectorOp - Given an operand of single-element vector type
170 /// (e.g. v1f32), convert it into the equivalent operation that returns a
171 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000172 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000173
Chris Lattner4352cc92006-04-04 17:23:26 +0000174 /// isShuffleLegal - Return true if a vector shuffle is legal with the
175 /// specified mask and type. Targets can specify exactly which masks they
176 /// support and the code generator is tasked with not creating illegal masks.
177 ///
178 /// Note that this will also return true for shuffles that are promoted to a
179 /// different type.
180 ///
181 /// If this is a legal shuffle, this method returns the (possibly promoted)
182 /// build_vector Mask. If it's not a legal shuffle, it returns null.
183 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000187
Nate Begeman750ac1b2006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattnerce872152006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencer47857812006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000196
Chris Lattner35481892005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Chris Lattner456a93a2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000214
Dan Gohman7f321562007-06-25 16:23:39 +0000215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000217
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner4352cc92006-04-04 17:23:26 +0000224/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
225/// specified mask and type. Targets can specify exactly which masks they
226/// support and the code generator is tasked with not creating illegal masks.
227///
228/// Note that this will also return true for shuffles that are promoted to a
229/// different type.
230SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
231 SDOperand Mask) const {
232 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 default: return 0;
234 case TargetLowering::Legal:
235 case TargetLowering::Custom:
236 break;
237 case TargetLowering::Promote: {
238 // If this is promoted to a different type, convert the shuffle mask and
239 // ask if it is legal in the promoted type!
240 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241
242 // If we changed # elements, change the shuffle mask.
243 unsigned NumEltsGrowth =
244 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
245 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
246 if (NumEltsGrowth > 1) {
247 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000249 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
250 SDOperand InOp = Mask.getOperand(i);
251 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
252 if (InOp.getOpcode() == ISD::UNDEF)
253 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 else {
255 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
256 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
257 }
258 }
259 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000261 }
262 VT = NVT;
263 break;
264 }
265 }
266 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
267}
268
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000269SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
270 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
271 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000272 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000274}
275
Chris Lattnere10e6f72007-06-18 21:28:10 +0000276/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
277/// contains all of a nodes operands before it contains the node.
278static void ComputeTopDownOrdering(SelectionDAG &DAG,
279 SmallVector<SDNode*, 64> &Order) {
280
281 DenseMap<SDNode*, unsigned> Visited;
282 std::vector<SDNode*> Worklist;
283 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000284
Chris Lattnere10e6f72007-06-18 21:28:10 +0000285 // Compute ordering from all of the leaves in the graphs, those (like the
286 // entry node) that have no operands.
287 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
288 E = DAG.allnodes_end(); I != E; ++I) {
289 if (I->getNumOperands() == 0) {
290 Visited[I] = 0 - 1U;
291 Worklist.push_back(I);
292 }
Chris Lattner32fca002005-10-06 01:20:27 +0000293 }
294
Chris Lattnere10e6f72007-06-18 21:28:10 +0000295 while (!Worklist.empty()) {
296 SDNode *N = Worklist.back();
297 Worklist.pop_back();
298
299 if (++Visited[N] != N->getNumOperands())
300 continue; // Haven't visited all operands yet
301
302 Order.push_back(N);
303
304 // Now that we have N in, add anything that uses it if all of their operands
305 // are now done.
306 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
307 UI != E; ++UI)
308 Worklist.push_back(*UI);
309 }
310
311 assert(Order.size() == Visited.size() &&
312 Order.size() ==
313 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
314 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000315}
316
Chris Lattner1618beb2005-07-29 00:11:56 +0000317
Chris Lattner3e928bb2005-01-07 07:47:09 +0000318void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000319 LastCALLSEQ_END = DAG.getEntryNode();
320 IsLegalizingCall = false;
321
Chris Lattnerab510a72005-10-02 17:49:46 +0000322 // The legalize process is inherently a bottom-up recursive process (users
323 // legalize their uses before themselves). Given infinite stack space, we
324 // could just start legalizing on the root and traverse the whole graph. In
325 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000326 // blocks. To avoid this problem, compute an ordering of the nodes where each
327 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000328 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000329 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000330
Chris Lattnerc7029802006-03-18 01:44:44 +0000331 for (unsigned i = 0, e = Order.size(); i != e; ++i)
332 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000333
334 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000335 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000336 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
337 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338
339 ExpandedNodes.clear();
340 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000341 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000342 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000343 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344
345 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000346 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347}
348
Chris Lattner6831a812006-02-13 09:18:02 +0000349
350/// FindCallEndFromCallStart - Given a chained node that is part of a call
351/// sequence, find the CALLSEQ_END node that terminates the call sequence.
352static SDNode *FindCallEndFromCallStart(SDNode *Node) {
353 if (Node->getOpcode() == ISD::CALLSEQ_END)
354 return Node;
355 if (Node->use_empty())
356 return 0; // No CallSeqEnd
357
358 // The chain is usually at the end.
359 SDOperand TheChain(Node, Node->getNumValues()-1);
360 if (TheChain.getValueType() != MVT::Other) {
361 // Sometimes it's at the beginning.
362 TheChain = SDOperand(Node, 0);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Otherwise, hunt for it.
365 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
366 if (Node->getValueType(i) == MVT::Other) {
367 TheChain = SDOperand(Node, i);
368 break;
369 }
370
371 // Otherwise, we walked into a node without a chain.
372 if (TheChain.getValueType() != MVT::Other)
373 return 0;
374 }
375 }
376
377 for (SDNode::use_iterator UI = Node->use_begin(),
378 E = Node->use_end(); UI != E; ++UI) {
379
380 // Make sure to only follow users of our token chain.
381 SDNode *User = *UI;
382 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
383 if (User->getOperand(i) == TheChain)
384 if (SDNode *Result = FindCallEndFromCallStart(User))
385 return Result;
386 }
387 return 0;
388}
389
390/// FindCallStartFromCallEnd - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_START node that initiates the call sequence.
392static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
393 assert(Node && "Didn't find callseq_start for a call??");
394 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
395
396 assert(Node->getOperand(0).getValueType() == MVT::Other &&
397 "Node doesn't have a token chain argument!");
398 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
399}
400
401/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
402/// see if any uses can reach Dest. If no dest operands can get to dest,
403/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000404///
405/// Keep track of the nodes we fine that actually do lead to Dest in
406/// NodesLeadingTo. This avoids retraversing them exponential number of times.
407///
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000409 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000410 if (N == Dest) return true; // N certainly leads to Dest :)
411
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000412 // If we've already processed this node and it does lead to Dest, there is no
413 // need to reprocess it.
414 if (NodesLeadingTo.count(N)) return true;
415
Chris Lattner6831a812006-02-13 09:18:02 +0000416 // If the first result of this node has been already legalized, then it cannot
417 // reach N.
418 switch (getTypeAction(N->getValueType(0))) {
419 case Legal:
420 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
421 break;
422 case Promote:
423 if (PromotedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Expand:
426 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 }
429
430 // Okay, this node has not already been legalized. Check and legalize all
431 // operands. If none lead to Dest, then we can legalize this node.
432 bool OperandsLeadToDest = false;
433 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
434 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000435 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000436
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000437 if (OperandsLeadToDest) {
438 NodesLeadingTo.insert(N);
439 return true;
440 }
Chris Lattner6831a812006-02-13 09:18:02 +0000441
442 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000443 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000444 return false;
445}
446
Dan Gohman7f321562007-06-25 16:23:39 +0000447/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000448/// appropriate for its type.
449void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000450 MVT::ValueType VT = Op.getValueType();
451 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000452 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000453 case Legal: (void)LegalizeOp(Op); break;
454 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000456 if (!MVT::isVector(VT)) {
457 // If this is an illegal scalar, expand it into its two component
458 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000459 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000460 if (Op.getOpcode() == ISD::TargetConstant)
461 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000462 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000463 } else if (MVT::getVectorNumElements(VT) == 1) {
464 // If this is an illegal single element vector, convert it to a
465 // scalar operation.
466 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000467 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000468 // Otherwise, this is an illegal multiple element vector.
469 // Split it in half and legalize both parts.
470 SDOperand X, Y;
471 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000472 }
473 break;
474 }
475}
Chris Lattner6831a812006-02-13 09:18:02 +0000476
Evan Cheng9f877882006-12-13 20:57:08 +0000477/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
478/// a load from the constant pool.
479static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000480 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000481 bool Extend = false;
482
483 // If a FP immediate is precise when represented as a float and if the
484 // target can do an extending load from float to double, we put it into
485 // the constant pool as a float, even if it's is statically typed as a
486 // double.
487 MVT::ValueType VT = CFP->getValueType(0);
488 bool isDouble = VT == MVT::f64;
489 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
490 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000491 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000492 double Val = LLVMC->getValue();
493 return isDouble
494 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
495 : DAG.getConstant(FloatToBits(Val), MVT::i32);
496 }
497
Evan Cheng00495212006-12-12 21:32:44 +0000498 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
499 // Only do this if the target has a native EXTLOAD instruction from f32.
500 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
501 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
502 VT = MVT::f32;
503 Extend = true;
504 }
505
506 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
507 if (Extend) {
508 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
509 CPIdx, NULL, 0, MVT::f32);
510 } else {
511 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
512 }
513}
514
Chris Lattner6831a812006-02-13 09:18:02 +0000515
Evan Cheng912095b2007-01-04 21:56:39 +0000516/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
517/// operations.
518static
519SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
520 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000521 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000522 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000523 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
524 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000525 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000526
Evan Cheng912095b2007-01-04 21:56:39 +0000527 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000529 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
530 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000531 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000532 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000534 // Shift right or sign-extend it if the two operands have different types.
535 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
536 if (SizeDiff > 0) {
537 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
538 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
539 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
540 } else if (SizeDiff < 0)
541 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000542
543 // Clear the sign bit of first operand.
544 SDOperand Mask2 = (VT == MVT::f64)
545 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
546 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
547 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000548 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000549 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
550
551 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000552 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
553 return Result;
554}
555
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000556/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
557static
558SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
559 TargetLowering &TLI) {
560 assert(MVT::isInteger(ST->getStoredVT()) &&
561 "Non integer unaligned stores not implemented.");
562 int SVOffset = ST->getSrcValueOffset();
563 SDOperand Chain = ST->getChain();
564 SDOperand Ptr = ST->getBasePtr();
565 SDOperand Val = ST->getValue();
566 MVT::ValueType VT = Val.getValueType();
567 // Get the half-size VT
568 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
569 int NumBits = MVT::getSizeInBits(NewStoredVT);
570 int Alignment = ST->getAlignment();
571 int IncrementSize = NumBits / 8;
572
573 // Divide the stored value in two parts.
574 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
575 SDOperand Lo = Val;
576 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
577
578 // Store the two parts
579 SDOperand Store1, Store2;
580 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
581 ST->getSrcValue(), SVOffset, NewStoredVT,
582 ST->isVolatile(), Alignment);
583 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
584 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
585 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
586 ST->getSrcValue(), SVOffset + IncrementSize,
587 NewStoredVT, ST->isVolatile(), Alignment);
588
589 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
590}
591
592/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
593static
594SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
595 TargetLowering &TLI) {
596 assert(MVT::isInteger(LD->getLoadedVT()) &&
597 "Non integer unaligned loads not implemented.");
598 int SVOffset = LD->getSrcValueOffset();
599 SDOperand Chain = LD->getChain();
600 SDOperand Ptr = LD->getBasePtr();
601 MVT::ValueType VT = LD->getValueType(0);
602 MVT::ValueType NewLoadedVT = LD->getLoadedVT() - 1;
603 int NumBits = MVT::getSizeInBits(NewLoadedVT);
604 int Alignment = LD->getAlignment();
605 int IncrementSize = NumBits / 8;
606 ISD::LoadExtType HiExtType = LD->getExtensionType();
607
608 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
609 if (HiExtType == ISD::NON_EXTLOAD)
610 HiExtType = ISD::ZEXTLOAD;
611
612 // Load the value in two parts
613 SDOperand Lo, Hi;
614 if (TLI.isLittleEndian()) {
615 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
616 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
617 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
618 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
619 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
620 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
621 Alignment);
622 } else {
623 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
624 NewLoadedVT,LD->isVolatile(), Alignment);
625 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
626 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
627 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
628 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
629 Alignment);
630 }
631
632 // aggregate the two parts
633 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
634 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
635 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
636
637 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
638 Hi.getValue(1));
639
640 SDOperand Ops[] = { Result, TF };
641 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
642}
Evan Cheng912095b2007-01-04 21:56:39 +0000643
Dan Gohmana3466152007-07-13 20:14:11 +0000644/// LegalizeOp - We know that the specified value has a legal type, and
645/// that its operands are legal. Now ensure that the operation itself
646/// is legal, recursively ensuring that the operands' operations remain
647/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000648SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000649 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
650 return Op;
651
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000652 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000653 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000654 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000655
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000657 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000658 if (Node->getNumValues() > 1) {
659 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000660 if (getTypeAction(Node->getValueType(i)) != Legal) {
661 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000662 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000663 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000664 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000665 }
666 }
667
Chris Lattner45982da2005-05-12 16:53:42 +0000668 // Note that LegalizeOp may be reentered even from single-use nodes, which
669 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000670 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000671 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000672
Nate Begeman9373a812005-08-10 20:51:12 +0000673 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000674 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000675 bool isCustom = false;
676
Chris Lattner3e928bb2005-01-07 07:47:09 +0000677 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000678 case ISD::FrameIndex:
679 case ISD::EntryToken:
680 case ISD::Register:
681 case ISD::BasicBlock:
682 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000683 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000684 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000685 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000686 case ISD::TargetConstantPool:
687 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000688 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000689 case ISD::TargetExternalSymbol:
690 case ISD::VALUETYPE:
691 case ISD::SRCVALUE:
692 case ISD::STRING:
693 case ISD::CONDCODE:
694 // Primitives must all be legal.
695 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
696 "This must be legal!");
697 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000698 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000699 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
700 // If this is a target node, legalize it by legalizing the operands then
701 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000702 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000703 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000704 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000705
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000706 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000707
708 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
709 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
710 return Result.getValue(Op.ResNo);
711 }
712 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000713#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000714 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000715#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000716 assert(0 && "Do not know how to legalize this operator!");
717 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000718 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000719 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000720 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000721 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000722 case ISD::ConstantPool:
723 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000724 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
725 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000726 case TargetLowering::Custom:
727 Tmp1 = TLI.LowerOperation(Op, DAG);
728 if (Tmp1.Val) Result = Tmp1;
729 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000730 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000731 break;
732 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000733 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000734 case ISD::FRAMEADDR:
735 case ISD::RETURNADDR:
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000736 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemanbcc5f362007-01-29 22:58:52 +0000737 // The only option for these nodes is to custom lower them. If the target
738 // does not custom lower them, then return zero.
739 Tmp1 = TLI.LowerOperation(Op, DAG);
740 if (Tmp1.Val)
741 Result = Tmp1;
742 else
743 Result = DAG.getConstant(0, TLI.getPointerTy());
744 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000745 case ISD::EXCEPTIONADDR: {
746 Tmp1 = LegalizeOp(Node->getOperand(0));
747 MVT::ValueType VT = Node->getValueType(0);
748 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
749 default: assert(0 && "This action is not supported yet!");
750 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000751 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000752 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
753 }
754 break;
755 case TargetLowering::Custom:
756 Result = TLI.LowerOperation(Op, DAG);
757 if (Result.Val) break;
758 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000759 case TargetLowering::Legal: {
760 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
761 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
762 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000763 break;
764 }
765 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000766 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000767 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000768 case ISD::EHSELECTION: {
769 Tmp1 = LegalizeOp(Node->getOperand(0));
770 Tmp2 = LegalizeOp(Node->getOperand(1));
771 MVT::ValueType VT = Node->getValueType(0);
772 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
773 default: assert(0 && "This action is not supported yet!");
774 case TargetLowering::Expand: {
775 unsigned Reg = TLI.getExceptionSelectorRegister();
776 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
777 }
778 break;
779 case TargetLowering::Custom:
780 Result = TLI.LowerOperation(Op, DAG);
781 if (Result.Val) break;
782 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000783 case TargetLowering::Legal: {
784 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
785 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
786 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000787 break;
788 }
789 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000790 }
Jim Laskey8782d482007-02-28 20:43:58 +0000791 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000792 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000793 MVT::ValueType VT = Node->getValueType(0);
794 // The only "good" option for this node is to custom lower it.
795 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
796 default: assert(0 && "This action is not supported at all!");
797 case TargetLowering::Custom:
798 Result = TLI.LowerOperation(Op, DAG);
799 if (Result.Val) break;
800 // Fall Thru
801 case TargetLowering::Legal:
802 // Target does not know, how to lower this, lower to noop
803 Result = LegalizeOp(Node->getOperand(0));
804 break;
805 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000806 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000807 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000808 case ISD::AssertSext:
809 case ISD::AssertZext:
810 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000811 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000812 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000813 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000814 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000815 Result = Node->getOperand(Op.ResNo);
816 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000817 case ISD::CopyFromReg:
818 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000819 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000820 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000821 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000822 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000823 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000824 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000825 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000826 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
827 } else {
828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
829 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000830 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
831 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000832 // Since CopyFromReg produces two values, make sure to remember that we
833 // legalized both of them.
834 AddLegalizedOperand(Op.getValue(0), Result);
835 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
836 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000837 case ISD::UNDEF: {
838 MVT::ValueType VT = Op.getValueType();
839 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000840 default: assert(0 && "This action is not supported yet!");
841 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000842 if (MVT::isInteger(VT))
843 Result = DAG.getConstant(0, VT);
844 else if (MVT::isFloatingPoint(VT))
845 Result = DAG.getConstantFP(0, VT);
846 else
847 assert(0 && "Unknown value type!");
848 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000849 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000850 break;
851 }
852 break;
853 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000854
Chris Lattner48b61a72006-03-28 00:40:33 +0000855 case ISD::INTRINSIC_W_CHAIN:
856 case ISD::INTRINSIC_WO_CHAIN:
857 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000858 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000859 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
860 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000861 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000862
863 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000864 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000865 TargetLowering::Custom) {
866 Tmp3 = TLI.LowerOperation(Result, DAG);
867 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000868 }
869
870 if (Result.Val->getNumValues() == 1) break;
871
872 // Must have return value and chain result.
873 assert(Result.Val->getNumValues() == 2 &&
874 "Cannot return more than two values!");
875
876 // Since loads produce two values, make sure to remember that we
877 // legalized both of them.
878 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
879 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
880 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000881 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000882
883 case ISD::LOCATION:
884 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
885 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
886
887 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
888 case TargetLowering::Promote:
889 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000890 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000891 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000892 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000893 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000894
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000895 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000896 const std::string &FName =
897 cast<StringSDNode>(Node->getOperand(3))->getValue();
898 const std::string &DirName =
899 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000900 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000901
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000902 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000903 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000904 SDOperand LineOp = Node->getOperand(1);
905 SDOperand ColOp = Node->getOperand(2);
906
907 if (useDEBUG_LOC) {
908 Ops.push_back(LineOp); // line #
909 Ops.push_back(ColOp); // col #
910 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000911 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000912 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000913 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
914 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000915 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000916 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000917 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000918 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000919 } else {
920 Result = Tmp1; // chain
921 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000922 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000923 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000924 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000925 if (Tmp1 != Node->getOperand(0) ||
926 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000927 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000928 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000929 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
930 Ops.push_back(Node->getOperand(1)); // line # must be legal.
931 Ops.push_back(Node->getOperand(2)); // col # must be legal.
932 } else {
933 // Otherwise promote them.
934 Ops.push_back(PromoteOp(Node->getOperand(1)));
935 Ops.push_back(PromoteOp(Node->getOperand(2)));
936 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000937 Ops.push_back(Node->getOperand(3)); // filename must be legal.
938 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000939 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000940 }
941 break;
942 }
943 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000944
945 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000946 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000947 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000948 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000949 case TargetLowering::Legal:
950 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
951 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
952 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
953 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000954 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000955 break;
956 }
957 break;
958
Jim Laskey1ee29252007-01-26 14:34:52 +0000959 case ISD::LABEL:
960 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
961 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000962 default: assert(0 && "This action is not supported yet!");
963 case TargetLowering::Legal:
964 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
965 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000967 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000968 case TargetLowering::Expand:
969 Result = LegalizeOp(Node->getOperand(0));
970 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000971 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000972 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000973
Scott Michelc1513d22007-08-08 23:23:31 +0000974 case ISD::Constant: {
975 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
976 unsigned opAction =
977 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
978
Chris Lattner3e928bb2005-01-07 07:47:09 +0000979 // We know we don't need to expand constants here, constants only have one
980 // value and we check that it is fine above.
981
Scott Michelc1513d22007-08-08 23:23:31 +0000982 if (opAction == TargetLowering::Custom) {
983 Tmp1 = TLI.LowerOperation(Result, DAG);
984 if (Tmp1.Val)
985 Result = Tmp1;
986 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000987 break;
Scott Michelc1513d22007-08-08 23:23:31 +0000988 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000989 case ISD::ConstantFP: {
990 // Spill FP immediates to the constant pool if the target cannot directly
991 // codegen them. Targets often have some immediate values that can be
992 // efficiently generated into an FP register without a load. We explicitly
993 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000994 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
995
996 // Check to see if this FP immediate is already legal.
997 bool isLegal = false;
998 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
999 E = TLI.legal_fpimm_end(); I != E; ++I)
1000 if (CFP->isExactlyValue(*I)) {
1001 isLegal = true;
1002 break;
1003 }
1004
Chris Lattner3181a772006-01-29 06:26:56 +00001005 // If this is a legal constant, turn it into a TargetConstantFP node.
1006 if (isLegal) {
1007 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
1008 break;
1009 }
1010
1011 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1012 default: assert(0 && "This action is not supported yet!");
1013 case TargetLowering::Custom:
1014 Tmp3 = TLI.LowerOperation(Result, DAG);
1015 if (Tmp3.Val) {
1016 Result = Tmp3;
1017 break;
1018 }
1019 // FALLTHROUGH
1020 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001021 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001022 }
1023 break;
1024 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001025 case ISD::TokenFactor:
1026 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001027 Tmp1 = LegalizeOp(Node->getOperand(0));
1028 Tmp2 = LegalizeOp(Node->getOperand(1));
1029 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1030 } else if (Node->getNumOperands() == 3) {
1031 Tmp1 = LegalizeOp(Node->getOperand(0));
1032 Tmp2 = LegalizeOp(Node->getOperand(1));
1033 Tmp3 = LegalizeOp(Node->getOperand(2));
1034 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001035 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001036 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001037 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001038 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1039 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001040 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001041 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001042 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001043
1044 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001045 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001046 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001047 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1048 assert(Tmp3.Val && "Target didn't custom lower this node!");
1049 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1050 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001051
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001052 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001053 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001054 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1055 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001056 if (Op.ResNo == i)
1057 Tmp2 = Tmp1;
1058 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1059 }
1060 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001061 case ISD::EXTRACT_SUBREG: {
1062 Tmp1 = LegalizeOp(Node->getOperand(0));
1063 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1064 assert(idx && "Operand must be a constant");
1065 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1067 }
1068 break;
1069 case ISD::INSERT_SUBREG: {
1070 Tmp1 = LegalizeOp(Node->getOperand(0));
1071 Tmp2 = LegalizeOp(Node->getOperand(1));
1072 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1073 assert(idx && "Operand must be a constant");
1074 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1076 }
1077 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001078 case ISD::BUILD_VECTOR:
1079 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001080 default: assert(0 && "This action is not supported yet!");
1081 case TargetLowering::Custom:
1082 Tmp3 = TLI.LowerOperation(Result, DAG);
1083 if (Tmp3.Val) {
1084 Result = Tmp3;
1085 break;
1086 }
1087 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001088 case TargetLowering::Expand:
1089 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001090 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001091 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001092 break;
1093 case ISD::INSERT_VECTOR_ELT:
1094 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1095 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1096 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1098
1099 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1100 Node->getValueType(0))) {
1101 default: assert(0 && "This action is not supported yet!");
1102 case TargetLowering::Legal:
1103 break;
1104 case TargetLowering::Custom:
1105 Tmp3 = TLI.LowerOperation(Result, DAG);
1106 if (Tmp3.Val) {
1107 Result = Tmp3;
1108 break;
1109 }
1110 // FALLTHROUGH
1111 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001112 // If the insert index is a constant, codegen this as a scalar_to_vector,
1113 // then a shuffle that inserts it into the right position in the vector.
1114 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1115 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1116 Tmp1.getValueType(), Tmp2);
1117
1118 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1119 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001120 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001121
1122 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1123 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1124 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001125 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001126 for (unsigned i = 0; i != NumElts; ++i) {
1127 if (i != InsertPos->getValue())
1128 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1129 else
1130 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1131 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001132 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1133 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001134
1135 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1136 Tmp1, ScVec, ShufMask);
1137 Result = LegalizeOp(Result);
1138 break;
1139 }
1140
Chris Lattner2332b9f2006-03-19 01:17:20 +00001141 // If the target doesn't support this, we have to spill the input vector
1142 // to a temporary stack slot, update the element, then reload it. This is
1143 // badness. We could also load the value into a vector register (either
1144 // with a "move to register" or "extload into register" instruction, then
1145 // permute it into place, if the idx is a constant and if the idx is
1146 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001147 MVT::ValueType VT = Tmp1.getValueType();
1148 MVT::ValueType EltVT = Tmp2.getValueType();
1149 MVT::ValueType IdxVT = Tmp3.getValueType();
1150 MVT::ValueType PtrVT = TLI.getPointerTy();
1151 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001152 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001153 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001154
1155 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001156 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1157 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001158 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001159 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1160 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1161 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001162 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001163 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001164 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001165 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001166 break;
1167 }
1168 }
1169 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001170 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001171 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1172 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1173 break;
1174 }
1175
Chris Lattnerce872152006-03-19 06:31:19 +00001176 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1177 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1178 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1179 Node->getValueType(0))) {
1180 default: assert(0 && "This action is not supported yet!");
1181 case TargetLowering::Legal:
1182 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001183 case TargetLowering::Custom:
1184 Tmp3 = TLI.LowerOperation(Result, DAG);
1185 if (Tmp3.Val) {
1186 Result = Tmp3;
1187 break;
1188 }
1189 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001190 case TargetLowering::Expand:
1191 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001192 break;
1193 }
Chris Lattnerce872152006-03-19 06:31:19 +00001194 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001195 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1197 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1199
1200 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001201 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1202 default: assert(0 && "Unknown operation action!");
1203 case TargetLowering::Legal:
1204 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1205 "vector shuffle should not be created if not legal!");
1206 break;
1207 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001208 Tmp3 = TLI.LowerOperation(Result, DAG);
1209 if (Tmp3.Val) {
1210 Result = Tmp3;
1211 break;
1212 }
1213 // FALLTHROUGH
1214 case TargetLowering::Expand: {
1215 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001216 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001217 MVT::ValueType PtrVT = TLI.getPointerTy();
1218 SDOperand Mask = Node->getOperand(2);
1219 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001220 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001221 for (unsigned i = 0; i != NumElems; ++i) {
1222 SDOperand Arg = Mask.getOperand(i);
1223 if (Arg.getOpcode() == ISD::UNDEF) {
1224 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1225 } else {
1226 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1227 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1228 if (Idx < NumElems)
1229 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1230 DAG.getConstant(Idx, PtrVT)));
1231 else
1232 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1233 DAG.getConstant(Idx - NumElems, PtrVT)));
1234 }
1235 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001236 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001237 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001238 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001239 case TargetLowering::Promote: {
1240 // Change base type to a different vector type.
1241 MVT::ValueType OVT = Node->getValueType(0);
1242 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1243
1244 // Cast the two input vectors.
1245 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1246 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1247
1248 // Convert the shuffle mask to the right # elements.
1249 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1250 assert(Tmp3.Val && "Shuffle not legal?");
1251 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1252 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1253 break;
1254 }
Chris Lattner87100e02006-03-20 01:52:29 +00001255 }
1256 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001257
1258 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001259 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001260 Tmp2 = LegalizeOp(Node->getOperand(1));
1261 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001262 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001263 break;
1264
Dan Gohman7f321562007-06-25 16:23:39 +00001265 case ISD::EXTRACT_SUBVECTOR:
1266 Tmp1 = Node->getOperand(0);
1267 Tmp2 = LegalizeOp(Node->getOperand(1));
1268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1269 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001270 break;
1271
Chris Lattner6831a812006-02-13 09:18:02 +00001272 case ISD::CALLSEQ_START: {
1273 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1274
1275 // Recursively Legalize all of the inputs of the call end that do not lead
1276 // to this call start. This ensures that any libcalls that need be inserted
1277 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001278 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001279 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001280 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1281 NodesLeadingTo);
1282 }
Chris Lattner6831a812006-02-13 09:18:02 +00001283
1284 // Now that we legalized all of the inputs (which may have inserted
1285 // libcalls) create the new CALLSEQ_START node.
1286 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1287
1288 // Merge in the last call, to ensure that this call start after the last
1289 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001290 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001291 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1292 Tmp1 = LegalizeOp(Tmp1);
1293 }
Chris Lattner6831a812006-02-13 09:18:02 +00001294
1295 // Do not try to legalize the target-specific arguments (#1+).
1296 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001297 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001298 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001299 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001300 }
1301
1302 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001303 AddLegalizedOperand(Op.getValue(0), Result);
1304 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1305 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1306
Chris Lattner6831a812006-02-13 09:18:02 +00001307 // Now that the callseq_start and all of the non-call nodes above this call
1308 // sequence have been legalized, legalize the call itself. During this
1309 // process, no libcalls can/will be inserted, guaranteeing that no calls
1310 // can overlap.
1311 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1312 SDOperand InCallSEQ = LastCALLSEQ_END;
1313 // Note that we are selecting this call!
1314 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1315 IsLegalizingCall = true;
1316
1317 // Legalize the call, starting from the CALLSEQ_END.
1318 LegalizeOp(LastCALLSEQ_END);
1319 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1320 return Result;
1321 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001322 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001323 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1324 // will cause this node to be legalized as well as handling libcalls right.
1325 if (LastCALLSEQ_END.Val != Node) {
1326 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001327 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001328 assert(I != LegalizedNodes.end() &&
1329 "Legalizing the call start should have legalized this node!");
1330 return I->second;
1331 }
1332
1333 // Otherwise, the call start has been legalized and everything is going
1334 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001335 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001336 // Do not try to legalize the target-specific arguments (#1+), except for
1337 // an optional flag input.
1338 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1339 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001340 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001341 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001342 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001343 }
1344 } else {
1345 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1346 if (Tmp1 != Node->getOperand(0) ||
1347 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001348 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001349 Ops[0] = Tmp1;
1350 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001351 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001352 }
Chris Lattner6a542892006-01-24 05:48:21 +00001353 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001354 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001355 // This finishes up call legalization.
1356 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001357
1358 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1359 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1360 if (Node->getNumValues() == 2)
1361 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1362 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001363 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001364 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1366 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1367 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001368 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001369
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001370 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001371 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001372 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001373 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001374 case TargetLowering::Expand: {
1375 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1376 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1377 " not tell us which reg is the stack pointer!");
1378 SDOperand Chain = Tmp1.getOperand(0);
1379 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001380 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1381 Chain = SP.getValue(1);
1382 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1383 unsigned StackAlign =
1384 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1385 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001386 SP = DAG.getNode(ISD::AND, VT, SP,
1387 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001388 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1389 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001390 Tmp1 = LegalizeOp(Tmp1);
1391 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001392 break;
1393 }
1394 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001395 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1396 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001397 Tmp1 = LegalizeOp(Tmp3);
1398 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001399 }
Chris Lattner903d2782006-01-15 08:54:32 +00001400 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001401 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001402 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001403 }
Chris Lattner903d2782006-01-15 08:54:32 +00001404 // Since this op produce two values, make sure to remember that we
1405 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001406 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1407 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001408 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001409 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001410 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001411 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001412 bool Changed = false;
1413 // Legalize all of the operands of the inline asm, in case they are nodes
1414 // that need to be expanded or something. Note we skip the asm string and
1415 // all of the TargetConstant flags.
1416 SDOperand Op = LegalizeOp(Ops[0]);
1417 Changed = Op != Ops[0];
1418 Ops[0] = Op;
1419
1420 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1421 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1422 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1423 for (++i; NumVals; ++i, --NumVals) {
1424 SDOperand Op = LegalizeOp(Ops[i]);
1425 if (Op != Ops[i]) {
1426 Changed = true;
1427 Ops[i] = Op;
1428 }
1429 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001430 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001431
1432 if (HasInFlag) {
1433 Op = LegalizeOp(Ops.back());
1434 Changed |= Op != Ops.back();
1435 Ops.back() = Op;
1436 }
1437
1438 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001439 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001440
1441 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001443 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1444 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001445 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001446 case ISD::BR:
1447 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001448 // Ensure that libcalls are emitted before a branch.
1449 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1450 Tmp1 = LegalizeOp(Tmp1);
1451 LastCALLSEQ_END = DAG.getEntryNode();
1452
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001454 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001455 case ISD::BRIND:
1456 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1457 // Ensure that libcalls are emitted before a branch.
1458 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1459 Tmp1 = LegalizeOp(Tmp1);
1460 LastCALLSEQ_END = DAG.getEntryNode();
1461
1462 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1463 default: assert(0 && "Indirect target must be legal type (pointer)!");
1464 case Legal:
1465 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1466 break;
1467 }
1468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1469 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001470 case ISD::BR_JT:
1471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1472 // Ensure that libcalls are emitted before a branch.
1473 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1474 Tmp1 = LegalizeOp(Tmp1);
1475 LastCALLSEQ_END = DAG.getEntryNode();
1476
1477 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1478 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1479
1480 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1481 default: assert(0 && "This action is not supported yet!");
1482 case TargetLowering::Legal: break;
1483 case TargetLowering::Custom:
1484 Tmp1 = TLI.LowerOperation(Result, DAG);
1485 if (Tmp1.Val) Result = Tmp1;
1486 break;
1487 case TargetLowering::Expand: {
1488 SDOperand Chain = Result.getOperand(0);
1489 SDOperand Table = Result.getOperand(1);
1490 SDOperand Index = Result.getOperand(2);
1491
1492 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001495 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1496 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001497
1498 SDOperand LD;
1499 switch (EntrySize) {
1500 default: assert(0 && "Size of jump table not supported yet."); break;
1501 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1502 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1503 }
1504
1505 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001506 // For PIC, the sequence is:
1507 // BRIND(load(Jumptable + index) + RelocBase)
1508 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1509 SDOperand Reloc;
1510 if (TLI.usesGlobalOffsetTable())
1511 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1512 else
1513 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001514 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001515 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1516 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1517 } else {
1518 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1519 }
1520 }
1521 }
1522 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001523 case ISD::BRCOND:
1524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001525 // Ensure that libcalls are emitted before a return.
1526 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1527 Tmp1 = LegalizeOp(Tmp1);
1528 LastCALLSEQ_END = DAG.getEntryNode();
1529
Chris Lattner47e92232005-01-18 19:27:06 +00001530 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1531 case Expand: assert(0 && "It's impossible to expand bools");
1532 case Legal:
1533 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1534 break;
1535 case Promote:
1536 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001537
1538 // The top bits of the promoted condition are not necessarily zero, ensure
1539 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001541 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1542 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001543 break;
1544 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001545
1546 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001547 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001548
1549 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1550 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001551 case TargetLowering::Legal: break;
1552 case TargetLowering::Custom:
1553 Tmp1 = TLI.LowerOperation(Result, DAG);
1554 if (Tmp1.Val) Result = Tmp1;
1555 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001556 case TargetLowering::Expand:
1557 // Expand brcond's setcc into its constituent parts and create a BR_CC
1558 // Node.
1559 if (Tmp2.getOpcode() == ISD::SETCC) {
1560 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1561 Tmp2.getOperand(0), Tmp2.getOperand(1),
1562 Node->getOperand(2));
1563 } else {
1564 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1565 DAG.getCondCode(ISD::SETNE), Tmp2,
1566 DAG.getConstant(0, Tmp2.getValueType()),
1567 Node->getOperand(2));
1568 }
1569 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001570 }
1571 break;
1572 case ISD::BR_CC:
1573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001574 // Ensure that libcalls are emitted before a branch.
1575 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1576 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001577 Tmp2 = Node->getOperand(2); // LHS
1578 Tmp3 = Node->getOperand(3); // RHS
1579 Tmp4 = Node->getOperand(1); // CC
1580
1581 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001582 LastCALLSEQ_END = DAG.getEntryNode();
1583
Nate Begeman750ac1b2006-02-01 07:19:44 +00001584 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1585 // the LHS is a legal SETCC itself. In this case, we need to compare
1586 // the result against zero to select between true and false values.
1587 if (Tmp3.Val == 0) {
1588 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1589 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001590 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001591
1592 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1593 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001594
Chris Lattner181b7a32005-12-17 23:46:46 +00001595 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1596 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001597 case TargetLowering::Legal: break;
1598 case TargetLowering::Custom:
1599 Tmp4 = TLI.LowerOperation(Result, DAG);
1600 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001601 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001602 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001603 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001604 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001605 LoadSDNode *LD = cast<LoadSDNode>(Node);
1606 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1607 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001608
Evan Cheng466685d2006-10-09 20:57:25 +00001609 ISD::LoadExtType ExtType = LD->getExtensionType();
1610 if (ExtType == ISD::NON_EXTLOAD) {
1611 MVT::ValueType VT = Node->getValueType(0);
1612 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1613 Tmp3 = Result.getValue(0);
1614 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001615
Evan Cheng466685d2006-10-09 20:57:25 +00001616 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1617 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001618 case TargetLowering::Legal:
1619 // If this is an unaligned load and the target doesn't support it,
1620 // expand it.
1621 if (!TLI.allowsUnalignedMemoryAccesses()) {
1622 unsigned ABIAlignment = TLI.getTargetData()->
1623 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1624 if (LD->getAlignment() < ABIAlignment){
1625 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1626 TLI);
1627 Tmp3 = Result.getOperand(0);
1628 Tmp4 = Result.getOperand(1);
1629 LegalizeOp(Tmp3);
1630 LegalizeOp(Tmp4);
1631 }
1632 }
1633 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001634 case TargetLowering::Custom:
1635 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1636 if (Tmp1.Val) {
1637 Tmp3 = LegalizeOp(Tmp1);
1638 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001639 }
Evan Cheng466685d2006-10-09 20:57:25 +00001640 break;
1641 case TargetLowering::Promote: {
1642 // Only promote a load of vector type to another.
1643 assert(MVT::isVector(VT) && "Cannot promote this load!");
1644 // Change base type to a different vector type.
1645 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1646
1647 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001648 LD->getSrcValueOffset(),
1649 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001650 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1651 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001652 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001653 }
Evan Cheng466685d2006-10-09 20:57:25 +00001654 }
1655 // Since loads produce two values, make sure to remember that we
1656 // legalized both of them.
1657 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1658 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1659 return Op.ResNo ? Tmp4 : Tmp3;
1660 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001661 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001662 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1663 default: assert(0 && "This action is not supported yet!");
1664 case TargetLowering::Promote:
1665 assert(SrcVT == MVT::i1 &&
1666 "Can only promote extending LOAD from i1 -> i8!");
1667 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1668 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001669 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001670 Tmp1 = Result.getValue(0);
1671 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001672 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001673 case TargetLowering::Custom:
1674 isCustom = true;
1675 // FALLTHROUGH
1676 case TargetLowering::Legal:
1677 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1678 Tmp1 = Result.getValue(0);
1679 Tmp2 = Result.getValue(1);
1680
1681 if (isCustom) {
1682 Tmp3 = TLI.LowerOperation(Result, DAG);
1683 if (Tmp3.Val) {
1684 Tmp1 = LegalizeOp(Tmp3);
1685 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1686 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001687 } else {
1688 // If this is an unaligned load and the target doesn't support it,
1689 // expand it.
1690 if (!TLI.allowsUnalignedMemoryAccesses()) {
1691 unsigned ABIAlignment = TLI.getTargetData()->
1692 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1693 if (LD->getAlignment() < ABIAlignment){
1694 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1695 TLI);
1696 Tmp1 = Result.getOperand(0);
1697 Tmp2 = Result.getOperand(1);
1698 LegalizeOp(Tmp1);
1699 LegalizeOp(Tmp2);
1700 }
1701 }
Evan Cheng466685d2006-10-09 20:57:25 +00001702 }
1703 break;
1704 case TargetLowering::Expand:
1705 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1706 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1707 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001708 LD->getSrcValueOffset(),
1709 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001710 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1711 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1712 Tmp2 = LegalizeOp(Load.getValue(1));
1713 break;
1714 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001715 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001716 // Turn the unsupported load into an EXTLOAD followed by an explicit
1717 // zero/sign extend inreg.
1718 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1719 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001720 LD->getSrcValueOffset(), SrcVT,
1721 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001722 SDOperand ValRes;
1723 if (ExtType == ISD::SEXTLOAD)
1724 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1725 Result, DAG.getValueType(SrcVT));
1726 else
1727 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1728 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1729 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1730 break;
1731 }
1732 // Since loads produce two values, make sure to remember that we legalized
1733 // both of them.
1734 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1735 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1736 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001737 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001738 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001739 case ISD::EXTRACT_ELEMENT: {
1740 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1741 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001742 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001743 case Legal:
1744 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1745 // 1 -> Hi
1746 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1747 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1748 TLI.getShiftAmountTy()));
1749 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1750 } else {
1751 // 0 -> Lo
1752 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1753 Node->getOperand(0));
1754 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001755 break;
1756 case Expand:
1757 // Get both the low and high parts.
1758 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1759 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1760 Result = Tmp2; // 1 -> Hi
1761 else
1762 Result = Tmp1; // 0 -> Lo
1763 break;
1764 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001765 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001766 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001767
1768 case ISD::CopyToReg:
1769 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001770
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001771 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001772 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001773 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001774 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001775 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001777 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001778 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001779 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001780 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001781 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1782 Tmp3);
1783 } else {
1784 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001785 }
1786
1787 // Since this produces two values, make sure to remember that we legalized
1788 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001789 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001790 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001791 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001792 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001793 break;
1794
1795 case ISD::RET:
1796 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001797
1798 // Ensure that libcalls are emitted before a return.
1799 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1800 Tmp1 = LegalizeOp(Tmp1);
1801 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001802
Chris Lattner3e928bb2005-01-07 07:47:09 +00001803 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001804 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001805 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001806 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001807 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001808 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001809 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001810 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001811 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001812 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001813 SDOperand Lo, Hi;
1814 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001815
1816 // Big endian systems want the hi reg first.
1817 if (!TLI.isLittleEndian())
1818 std::swap(Lo, Hi);
1819
Evan Cheng13acce32006-12-11 19:27:14 +00001820 if (Hi.Val)
1821 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1822 else
1823 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001824 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001825 } else {
1826 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001827 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1828 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001829
Dan Gohman7f321562007-06-25 16:23:39 +00001830 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001831 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001832 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001833 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001834 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001835 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001837 } else if (NumElems == 1) {
1838 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001839 Tmp2 = ScalarizeVectorOp(Tmp2);
1840 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001842
1843 // FIXME: Returns of gcc generic vectors smaller than a legal type
1844 // should be returned in integer registers!
1845
Chris Lattnerf87324e2006-04-11 01:31:51 +00001846 // The scalarized value type may not be legal, e.g. it might require
1847 // promotion or expansion. Relegalize the return.
1848 Result = LegalizeOp(Result);
1849 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001850 // FIXME: Returns of gcc generic vectors larger than a legal vector
1851 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001852 SDOperand Lo, Hi;
1853 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001854 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001855 Result = LegalizeOp(Result);
1856 }
1857 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001858 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001859 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001860 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001862 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001863 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001864 }
1865 break;
1866 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001867 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001868 break;
1869 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001870 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001871 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001872 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001873 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1874 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001875 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001876 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001877 break;
1878 case Expand: {
1879 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001880 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001881 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001882 ExpandOp(Node->getOperand(i), Lo, Hi);
1883 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001884 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001885 if (Hi.Val) {
1886 NewValues.push_back(Hi);
1887 NewValues.push_back(Node->getOperand(i+1));
1888 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001889 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001890 }
1891 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001892 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001893 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001894
1895 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001896 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001897 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001898 Result = DAG.getNode(ISD::RET, MVT::Other,
1899 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001900 break;
1901 }
1902 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001903
Chris Lattner6862dbc2006-01-29 21:02:23 +00001904 if (Result.getOpcode() == ISD::RET) {
1905 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1906 default: assert(0 && "This action is not supported yet!");
1907 case TargetLowering::Legal: break;
1908 case TargetLowering::Custom:
1909 Tmp1 = TLI.LowerOperation(Result, DAG);
1910 if (Tmp1.Val) Result = Tmp1;
1911 break;
1912 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001913 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001914 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001915 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001916 StoreSDNode *ST = cast<StoreSDNode>(Node);
1917 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1918 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001919 int SVOffset = ST->getSrcValueOffset();
1920 unsigned Alignment = ST->getAlignment();
1921 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001922
Evan Cheng8b2794a2006-10-13 21:14:26 +00001923 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001924 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1925 // FIXME: We shouldn't do this for TargetConstantFP's.
1926 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1927 // to phase ordering between legalized code and the dag combiner. This
1928 // probably means that we need to integrate dag combiner and legalizer
1929 // together.
1930 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1931 if (CFP->getValueType(0) == MVT::f32) {
1932 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1933 } else {
1934 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1935 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1936 }
1937 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001938 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001939 break;
1940 }
1941
Evan Cheng8b2794a2006-10-13 21:14:26 +00001942 switch (getTypeAction(ST->getStoredVT())) {
1943 case Legal: {
1944 Tmp3 = LegalizeOp(ST->getValue());
1945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1946 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001947
Evan Cheng8b2794a2006-10-13 21:14:26 +00001948 MVT::ValueType VT = Tmp3.getValueType();
1949 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1950 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001951 case TargetLowering::Legal:
1952 // If this is an unaligned store and the target doesn't support it,
1953 // expand it.
1954 if (!TLI.allowsUnalignedMemoryAccesses()) {
1955 unsigned ABIAlignment = TLI.getTargetData()->
1956 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
1957 if (ST->getAlignment() < ABIAlignment)
1958 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
1959 TLI);
1960 }
1961 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001962 case TargetLowering::Custom:
1963 Tmp1 = TLI.LowerOperation(Result, DAG);
1964 if (Tmp1.Val) Result = Tmp1;
1965 break;
1966 case TargetLowering::Promote:
1967 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1968 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1969 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1970 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001971 ST->getSrcValue(), SVOffset, isVolatile,
1972 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001973 break;
1974 }
1975 break;
1976 }
1977 case Promote:
1978 // Truncate the value and store the result.
1979 Tmp3 = PromoteOp(ST->getValue());
1980 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001981 SVOffset, ST->getStoredVT(),
1982 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001983 break;
1984
1985 case Expand:
1986 unsigned IncrementSize = 0;
1987 SDOperand Lo, Hi;
1988
1989 // If this is a vector type, then we have to calculate the increment as
1990 // the product of the element size in bytes, and the number of elements
1991 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001992 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001993 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001994 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1995 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001996
Dan Gohman7f321562007-06-25 16:23:39 +00001997 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001998 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001999 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002000 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002001 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002002 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002003 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002004 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002005 Result = LegalizeOp(Result);
2006 break;
2007 } else if (NumElems == 1) {
2008 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002009 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002010 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002011 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002012 // The scalarized value type may not be legal, e.g. it might require
2013 // promotion or expansion. Relegalize the scalar store.
2014 Result = LegalizeOp(Result);
2015 break;
2016 } else {
2017 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2018 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2019 }
2020 } else {
2021 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002022 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002023
2024 if (!TLI.isLittleEndian())
2025 std::swap(Lo, Hi);
2026 }
2027
2028 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002029 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002030
2031 if (Hi.Val == NULL) {
2032 // Must be int <-> float one-to-one expansion.
2033 Result = Lo;
2034 break;
2035 }
2036
Evan Cheng8b2794a2006-10-13 21:14:26 +00002037 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2038 getIntPtrConstant(IncrementSize));
2039 assert(isTypeLegal(Tmp2.getValueType()) &&
2040 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002041 SVOffset += IncrementSize;
2042 if (Alignment > IncrementSize)
2043 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002044 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002045 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002046 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2047 break;
2048 }
2049 } else {
2050 // Truncating store
2051 assert(isTypeLegal(ST->getValue().getValueType()) &&
2052 "Cannot handle illegal TRUNCSTORE yet!");
2053 Tmp3 = LegalizeOp(ST->getValue());
2054
2055 // The only promote case we handle is TRUNCSTORE:i1 X into
2056 // -> TRUNCSTORE:i8 (and X, 1)
2057 if (ST->getStoredVT() == MVT::i1 &&
2058 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2059 // Promote the bool to a mask then store.
2060 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2061 DAG.getConstant(1, Tmp3.getValueType()));
2062 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002063 SVOffset, MVT::i8,
2064 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002065 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2066 Tmp2 != ST->getBasePtr()) {
2067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2068 ST->getOffset());
2069 }
2070
2071 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2072 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002073 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002074 case TargetLowering::Legal:
2075 // If this is an unaligned store and the target doesn't support it,
2076 // expand it.
2077 if (!TLI.allowsUnalignedMemoryAccesses()) {
2078 unsigned ABIAlignment = TLI.getTargetData()->
2079 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2080 if (ST->getAlignment() < ABIAlignment)
2081 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2082 TLI);
2083 }
2084 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002085 case TargetLowering::Custom:
2086 Tmp1 = TLI.LowerOperation(Result, DAG);
2087 if (Tmp1.Val) Result = Tmp1;
2088 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002089 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002090 }
2091 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002092 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002093 case ISD::PCMARKER:
2094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002095 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002096 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002097 case ISD::STACKSAVE:
2098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002099 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2100 Tmp1 = Result.getValue(0);
2101 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002102
Chris Lattner140d53c2006-01-13 02:50:02 +00002103 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2104 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002105 case TargetLowering::Legal: break;
2106 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002107 Tmp3 = TLI.LowerOperation(Result, DAG);
2108 if (Tmp3.Val) {
2109 Tmp1 = LegalizeOp(Tmp3);
2110 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002111 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002112 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002113 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002114 // Expand to CopyFromReg if the target set
2115 // StackPointerRegisterToSaveRestore.
2116 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002117 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002118 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002119 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002120 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002121 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2122 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002123 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002124 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002125 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002126
2127 // Since stacksave produce two values, make sure to remember that we
2128 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002129 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2130 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2131 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002132
Chris Lattner140d53c2006-01-13 02:50:02 +00002133 case ISD::STACKRESTORE:
2134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002136 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002137
2138 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2139 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002140 case TargetLowering::Legal: break;
2141 case TargetLowering::Custom:
2142 Tmp1 = TLI.LowerOperation(Result, DAG);
2143 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002144 break;
2145 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002146 // Expand to CopyToReg if the target set
2147 // StackPointerRegisterToSaveRestore.
2148 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2149 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2150 } else {
2151 Result = Tmp1;
2152 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002153 break;
2154 }
2155 break;
2156
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002157 case ISD::READCYCLECOUNTER:
2158 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002159 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002160 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2161 Node->getValueType(0))) {
2162 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002163 case TargetLowering::Legal:
2164 Tmp1 = Result.getValue(0);
2165 Tmp2 = Result.getValue(1);
2166 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002167 case TargetLowering::Custom:
2168 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002169 Tmp1 = LegalizeOp(Result.getValue(0));
2170 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002171 break;
2172 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002173
2174 // Since rdcc produce two values, make sure to remember that we legalized
2175 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002176 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2177 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002178 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002179
Chris Lattner2ee743f2005-01-14 22:08:15 +00002180 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002181 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2182 case Expand: assert(0 && "It's impossible to expand bools");
2183 case Legal:
2184 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2185 break;
2186 case Promote:
2187 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002188 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002189 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002190 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2191 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002192 break;
2193 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002194 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002195 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002196
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002198
Nate Begemanb942a3d2005-08-23 04:29:48 +00002199 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002200 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002201 case TargetLowering::Legal: break;
2202 case TargetLowering::Custom: {
2203 Tmp1 = TLI.LowerOperation(Result, DAG);
2204 if (Tmp1.Val) Result = Tmp1;
2205 break;
2206 }
Nate Begeman9373a812005-08-10 20:51:12 +00002207 case TargetLowering::Expand:
2208 if (Tmp1.getOpcode() == ISD::SETCC) {
2209 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2210 Tmp2, Tmp3,
2211 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2212 } else {
2213 Result = DAG.getSelectCC(Tmp1,
2214 DAG.getConstant(0, Tmp1.getValueType()),
2215 Tmp2, Tmp3, ISD::SETNE);
2216 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002217 break;
2218 case TargetLowering::Promote: {
2219 MVT::ValueType NVT =
2220 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2221 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002222 if (MVT::isVector(Tmp2.getValueType())) {
2223 ExtOp = ISD::BIT_CONVERT;
2224 TruncOp = ISD::BIT_CONVERT;
2225 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002226 ExtOp = ISD::ANY_EXTEND;
2227 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002228 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002229 ExtOp = ISD::FP_EXTEND;
2230 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002231 }
2232 // Promote each of the values to the new type.
2233 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2234 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2235 // Perform the larger operation, then round down.
2236 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2237 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2238 break;
2239 }
2240 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002241 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002242 case ISD::SELECT_CC: {
2243 Tmp1 = Node->getOperand(0); // LHS
2244 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002245 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2246 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002247 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002248
Nate Begeman750ac1b2006-02-01 07:19:44 +00002249 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2250
2251 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2252 // the LHS is a legal SETCC itself. In this case, we need to compare
2253 // the result against zero to select between true and false values.
2254 if (Tmp2.Val == 0) {
2255 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2256 CC = DAG.getCondCode(ISD::SETNE);
2257 }
2258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2259
2260 // Everything is legal, see if we should expand this op or something.
2261 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2262 default: assert(0 && "This action is not supported yet!");
2263 case TargetLowering::Legal: break;
2264 case TargetLowering::Custom:
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002267 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002268 }
2269 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002270 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002271 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002272 Tmp1 = Node->getOperand(0);
2273 Tmp2 = Node->getOperand(1);
2274 Tmp3 = Node->getOperand(2);
2275 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2276
2277 // If we had to Expand the SetCC operands into a SELECT node, then it may
2278 // not always be possible to return a true LHS & RHS. In this case, just
2279 // return the value we legalized, returned in the LHS
2280 if (Tmp2.Val == 0) {
2281 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002282 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002283 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002284
Chris Lattner73e142f2006-01-30 22:43:50 +00002285 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002286 default: assert(0 && "Cannot handle this action for SETCC yet!");
2287 case TargetLowering::Custom:
2288 isCustom = true;
2289 // FALLTHROUGH.
2290 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002292 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002293 Tmp4 = TLI.LowerOperation(Result, DAG);
2294 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002295 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002296 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002297 case TargetLowering::Promote: {
2298 // First step, figure out the appropriate operation to use.
2299 // Allow SETCC to not be supported for all legal data types
2300 // Mostly this targets FP
2301 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002302 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002303
2304 // Scan for the appropriate larger type to use.
2305 while (1) {
2306 NewInTy = (MVT::ValueType)(NewInTy+1);
2307
2308 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2309 "Fell off of the edge of the integer world");
2310 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2311 "Fell off of the edge of the floating point world");
2312
2313 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002314 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002315 break;
2316 }
2317 if (MVT::isInteger(NewInTy))
2318 assert(0 && "Cannot promote Legal Integer SETCC yet");
2319 else {
2320 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2321 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2322 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002323 Tmp1 = LegalizeOp(Tmp1);
2324 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002326 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002327 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002328 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002329 case TargetLowering::Expand:
2330 // Expand a setcc node into a select_cc of the same condition, lhs, and
2331 // rhs that selects between const 1 (true) and const 0 (false).
2332 MVT::ValueType VT = Node->getValueType(0);
2333 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2334 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002335 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002336 break;
2337 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002338 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002339 case ISD::MEMSET:
2340 case ISD::MEMCPY:
2341 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002342 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002343 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2344
2345 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2346 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2347 case Expand: assert(0 && "Cannot expand a byte!");
2348 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002349 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002350 break;
2351 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002352 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002353 break;
2354 }
2355 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002356 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002357 }
Chris Lattner272455b2005-02-02 03:44:41 +00002358
2359 SDOperand Tmp4;
2360 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002361 case Expand: {
2362 // Length is too big, just take the lo-part of the length.
2363 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002364 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002365 break;
2366 }
Chris Lattnere5605212005-01-28 22:29:18 +00002367 case Legal:
2368 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002369 break;
2370 case Promote:
2371 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002372 break;
2373 }
2374
2375 SDOperand Tmp5;
2376 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2377 case Expand: assert(0 && "Cannot expand this yet!");
2378 case Legal:
2379 Tmp5 = LegalizeOp(Node->getOperand(4));
2380 break;
2381 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002382 Tmp5 = PromoteOp(Node->getOperand(4));
2383 break;
2384 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002385
2386 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2387 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002388 case TargetLowering::Custom:
2389 isCustom = true;
2390 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002391 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002393 if (isCustom) {
2394 Tmp1 = TLI.LowerOperation(Result, DAG);
2395 if (Tmp1.Val) Result = Tmp1;
2396 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002397 break;
2398 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002399 // Otherwise, the target does not support this operation. Lower the
2400 // operation to an explicit libcall as appropriate.
2401 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002402 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002403 TargetLowering::ArgListTy Args;
2404 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002405
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002406 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002407 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002408 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002409 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002410 // Extend the (previously legalized) ubyte argument to be an int value
2411 // for the call.
2412 if (Tmp3.getValueType() > MVT::i32)
2413 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2414 else
2415 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002416 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002417 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002418 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002419 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002420
2421 FnName = "memset";
2422 } else if (Node->getOpcode() == ISD::MEMCPY ||
2423 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002424 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002425 Entry.Node = Tmp2; Args.push_back(Entry);
2426 Entry.Node = Tmp3; Args.push_back(Entry);
2427 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002428 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2429 } else {
2430 assert(0 && "Unknown op!");
2431 }
Chris Lattner45982da2005-05-12 16:53:42 +00002432
Chris Lattnere1bd8222005-01-11 05:57:22 +00002433 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002434 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002435 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002436 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002437 break;
2438 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002439 }
2440 break;
2441 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002442
Chris Lattner5b359c62005-04-02 04:00:59 +00002443 case ISD::SHL_PARTS:
2444 case ISD::SRA_PARTS:
2445 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002446 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002447 bool Changed = false;
2448 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2449 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2450 Changed |= Ops.back() != Node->getOperand(i);
2451 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002452 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002453 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002454
Evan Cheng05a2d562006-01-09 18:31:59 +00002455 switch (TLI.getOperationAction(Node->getOpcode(),
2456 Node->getValueType(0))) {
2457 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002458 case TargetLowering::Legal: break;
2459 case TargetLowering::Custom:
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
2461 if (Tmp1.Val) {
2462 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002463 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002464 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002465 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2466 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002467 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002468 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002469 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002470 return RetVal;
2471 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002472 break;
2473 }
2474
Chris Lattner2c8086f2005-04-02 05:00:07 +00002475 // Since these produce multiple values, make sure to remember that we
2476 // legalized all of them.
2477 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2478 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2479 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002480 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002481
2482 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002483 case ISD::ADD:
2484 case ISD::SUB:
2485 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002486 case ISD::MULHS:
2487 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002488 case ISD::UDIV:
2489 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002490 case ISD::AND:
2491 case ISD::OR:
2492 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002493 case ISD::SHL:
2494 case ISD::SRL:
2495 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002496 case ISD::FADD:
2497 case ISD::FSUB:
2498 case ISD::FMUL:
2499 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002500 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002501 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2502 case Expand: assert(0 && "Not possible");
2503 case Legal:
2504 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2505 break;
2506 case Promote:
2507 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2508 break;
2509 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002510
2511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002512
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002513 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002514 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002515 case TargetLowering::Legal: break;
2516 case TargetLowering::Custom:
2517 Tmp1 = TLI.LowerOperation(Result, DAG);
2518 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002519 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002520 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002521 if (Node->getValueType(0) == MVT::i32) {
2522 switch (Node->getOpcode()) {
2523 default: assert(0 && "Do not know how to expand this integer BinOp!");
2524 case ISD::UDIV:
2525 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002526 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2527 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002528 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002529 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002530 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002531 };
2532 break;
2533 }
2534
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002535 assert(MVT::isVector(Node->getValueType(0)) &&
2536 "Cannot expand this binary operator!");
2537 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002538 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002539 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002540 MVT::ValueType PtrVT = TLI.getPointerTy();
2541 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2542 i != e; ++i) {
2543 SDOperand Idx = DAG.getConstant(i, PtrVT);
2544 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2545 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2546 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2547 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002548 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2549 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002550 break;
2551 }
Evan Chengcc987612006-04-12 21:20:24 +00002552 case TargetLowering::Promote: {
2553 switch (Node->getOpcode()) {
2554 default: assert(0 && "Do not know how to promote this BinOp!");
2555 case ISD::AND:
2556 case ISD::OR:
2557 case ISD::XOR: {
2558 MVT::ValueType OVT = Node->getValueType(0);
2559 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2560 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2561 // Bit convert each of the values to the new type.
2562 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2563 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2564 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2565 // Bit convert the result back the original type.
2566 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2567 break;
2568 }
2569 }
2570 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002571 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002572 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002573
2574 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2575 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2576 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2577 case Expand: assert(0 && "Not possible");
2578 case Legal:
2579 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2580 break;
2581 case Promote:
2582 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2583 break;
2584 }
2585
2586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2587
2588 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2589 default: assert(0 && "Operation not supported");
2590 case TargetLowering::Custom:
2591 Tmp1 = TLI.LowerOperation(Result, DAG);
2592 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002593 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002594 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002595 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002596 // If this target supports fabs/fneg natively and select is cheap,
2597 // do this efficiently.
2598 if (!TLI.isSelectExpensive() &&
2599 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2600 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002601 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002602 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002603 // Get the sign bit of the RHS.
2604 MVT::ValueType IVT =
2605 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2606 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2607 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2608 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2609 // Get the absolute value of the result.
2610 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2611 // Select between the nabs and abs value based on the sign bit of
2612 // the input.
2613 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2614 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2615 AbsVal),
2616 AbsVal);
2617 Result = LegalizeOp(Result);
2618 break;
2619 }
2620
2621 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002622 MVT::ValueType NVT =
2623 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2624 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2625 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2626 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002627 break;
2628 }
Evan Cheng912095b2007-01-04 21:56:39 +00002629 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002630 break;
2631
Nate Begeman551bf3f2006-02-17 05:43:56 +00002632 case ISD::ADDC:
2633 case ISD::SUBC:
2634 Tmp1 = LegalizeOp(Node->getOperand(0));
2635 Tmp2 = LegalizeOp(Node->getOperand(1));
2636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2637 // Since this produces two values, make sure to remember that we legalized
2638 // both of them.
2639 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2640 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2641 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002642
Nate Begeman551bf3f2006-02-17 05:43:56 +00002643 case ISD::ADDE:
2644 case ISD::SUBE:
2645 Tmp1 = LegalizeOp(Node->getOperand(0));
2646 Tmp2 = LegalizeOp(Node->getOperand(1));
2647 Tmp3 = LegalizeOp(Node->getOperand(2));
2648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2649 // Since this produces two values, make sure to remember that we legalized
2650 // both of them.
2651 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2652 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2653 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002654
Nate Begeman419f8b62005-10-18 00:27:41 +00002655 case ISD::BUILD_PAIR: {
2656 MVT::ValueType PairTy = Node->getValueType(0);
2657 // TODO: handle the case where the Lo and Hi operands are not of legal type
2658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2659 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2660 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002661 case TargetLowering::Promote:
2662 case TargetLowering::Custom:
2663 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002664 case TargetLowering::Legal:
2665 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2666 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2667 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002668 case TargetLowering::Expand:
2669 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2670 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2671 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2672 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2673 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002674 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002675 break;
2676 }
2677 break;
2678 }
2679
Nate Begemanc105e192005-04-06 00:23:54 +00002680 case ISD::UREM:
2681 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002682 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002683 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2684 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002685
Nate Begemanc105e192005-04-06 00:23:54 +00002686 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002687 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2688 case TargetLowering::Custom:
2689 isCustom = true;
2690 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002691 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002692 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002693 if (isCustom) {
2694 Tmp1 = TLI.LowerOperation(Result, DAG);
2695 if (Tmp1.Val) Result = Tmp1;
2696 }
Nate Begemanc105e192005-04-06 00:23:54 +00002697 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002698 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002699 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002700 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002701 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002702 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2703 TargetLowering::Legal) {
2704 // X % Y -> X-X/Y*Y
2705 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002706 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002707 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2708 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2709 } else {
2710 assert(Node->getValueType(0) == MVT::i32 &&
2711 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002712 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2713 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002714 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002715 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002716 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002717 } else {
2718 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002719 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2720 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002721 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002722 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2723 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002724 }
2725 break;
2726 }
2727 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002728 case ISD::VAARG: {
2729 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2730 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2731
Chris Lattner5c62f332006-01-28 07:42:08 +00002732 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002733 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2734 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002735 case TargetLowering::Custom:
2736 isCustom = true;
2737 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002738 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002739 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2740 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002741 Tmp1 = Result.getValue(1);
2742
2743 if (isCustom) {
2744 Tmp2 = TLI.LowerOperation(Result, DAG);
2745 if (Tmp2.Val) {
2746 Result = LegalizeOp(Tmp2);
2747 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2748 }
2749 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002750 break;
2751 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002752 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002753 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002754 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002755 // Increment the pointer, VAList, to the next vaarg
2756 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2757 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2758 TLI.getPointerTy()));
2759 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002760 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2761 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002762 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002763 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002764 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002765 Result = LegalizeOp(Result);
2766 break;
2767 }
2768 }
2769 // Since VAARG produces two values, make sure to remember that we
2770 // legalized both of them.
2771 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002772 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2773 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002774 }
2775
2776 case ISD::VACOPY:
2777 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2778 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2779 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2780
2781 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2782 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002783 case TargetLowering::Custom:
2784 isCustom = true;
2785 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002786 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002787 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2788 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002789 if (isCustom) {
2790 Tmp1 = TLI.LowerOperation(Result, DAG);
2791 if (Tmp1.Val) Result = Tmp1;
2792 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002793 break;
2794 case TargetLowering::Expand:
2795 // This defaults to loading a pointer from the input and storing it to the
2796 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002797 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002798 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002799 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2800 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002801 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2802 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002803 break;
2804 }
2805 break;
2806
2807 case ISD::VAEND:
2808 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2809 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2810
2811 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2812 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002813 case TargetLowering::Custom:
2814 isCustom = true;
2815 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002816 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002817 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002818 if (isCustom) {
2819 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2820 if (Tmp1.Val) Result = Tmp1;
2821 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002822 break;
2823 case TargetLowering::Expand:
2824 Result = Tmp1; // Default to a no-op, return the chain
2825 break;
2826 }
2827 break;
2828
2829 case ISD::VASTART:
2830 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2831 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2832
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2834
Nate Begemanacc398c2006-01-25 18:21:52 +00002835 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2836 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002837 case TargetLowering::Legal: break;
2838 case TargetLowering::Custom:
2839 Tmp1 = TLI.LowerOperation(Result, DAG);
2840 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002841 break;
2842 }
2843 break;
2844
Nate Begeman35ef9132006-01-11 21:21:00 +00002845 case ISD::ROTL:
2846 case ISD::ROTR:
2847 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2848 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002850 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2851 default:
2852 assert(0 && "ROTL/ROTR legalize operation not supported");
2853 break;
2854 case TargetLowering::Legal:
2855 break;
2856 case TargetLowering::Custom:
2857 Tmp1 = TLI.LowerOperation(Result, DAG);
2858 if (Tmp1.Val) Result = Tmp1;
2859 break;
2860 case TargetLowering::Promote:
2861 assert(0 && "Do not know how to promote ROTL/ROTR");
2862 break;
2863 case TargetLowering::Expand:
2864 assert(0 && "Do not know how to expand ROTL/ROTR");
2865 break;
2866 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002867 break;
2868
Nate Begemand88fc032006-01-14 03:14:10 +00002869 case ISD::BSWAP:
2870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2871 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002872 case TargetLowering::Custom:
2873 assert(0 && "Cannot custom legalize this yet!");
2874 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002875 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002876 break;
2877 case TargetLowering::Promote: {
2878 MVT::ValueType OVT = Tmp1.getValueType();
2879 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002880 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002881
Chris Lattner456a93a2006-01-28 07:39:30 +00002882 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2883 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2884 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2885 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2886 break;
2887 }
2888 case TargetLowering::Expand:
2889 Result = ExpandBSWAP(Tmp1);
2890 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002891 }
2892 break;
2893
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002894 case ISD::CTPOP:
2895 case ISD::CTTZ:
2896 case ISD::CTLZ:
2897 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2898 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00002899 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002900 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002901 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00002902 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00002903 TargetLowering::Custom) {
2904 Tmp1 = TLI.LowerOperation(Result, DAG);
2905 if (Tmp1.Val) {
2906 Result = Tmp1;
2907 }
Scott Michel910b66d2007-07-30 21:00:31 +00002908 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002909 break;
2910 case TargetLowering::Promote: {
2911 MVT::ValueType OVT = Tmp1.getValueType();
2912 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002913
2914 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002915 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2916 // Perform the larger operation, then subtract if needed.
2917 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002918 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002919 case ISD::CTPOP:
2920 Result = Tmp1;
2921 break;
2922 case ISD::CTTZ:
2923 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002924 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002925 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002926 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002927 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00002928 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002929 break;
2930 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002931 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002932 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002933 DAG.getConstant(MVT::getSizeInBits(NVT) -
2934 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002935 break;
2936 }
2937 break;
2938 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002939 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002940 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002941 break;
2942 }
2943 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002944
Chris Lattner2c8086f2005-04-02 05:00:07 +00002945 // Unary operators
2946 case ISD::FABS:
2947 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002948 case ISD::FSQRT:
2949 case ISD::FSIN:
2950 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002951 Tmp1 = LegalizeOp(Node->getOperand(0));
2952 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002953 case TargetLowering::Promote:
2954 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002955 isCustom = true;
2956 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002957 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002958 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002959 if (isCustom) {
2960 Tmp1 = TLI.LowerOperation(Result, DAG);
2961 if (Tmp1.Val) Result = Tmp1;
2962 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002963 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002964 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002965 switch (Node->getOpcode()) {
2966 default: assert(0 && "Unreachable!");
2967 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002968 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2969 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002970 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002971 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002972 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002973 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2974 MVT::ValueType VT = Node->getValueType(0);
2975 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002976 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002977 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2978 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002979 break;
2980 }
2981 case ISD::FSQRT:
2982 case ISD::FSIN:
2983 case ISD::FCOS: {
2984 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002985 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002986 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002987 case ISD::FSQRT:
2988 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2989 break;
2990 case ISD::FSIN:
2991 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2992 break;
2993 case ISD::FCOS:
2994 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2995 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002996 default: assert(0 && "Unreachable!");
2997 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002998 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002999 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3000 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003001 break;
3002 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003003 }
3004 break;
3005 }
3006 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003007 case ISD::FPOWI: {
3008 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00003009 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3010 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003011 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003012 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3013 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003014 break;
3015 }
Chris Lattner35481892005-12-23 00:16:34 +00003016 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003017 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003018 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003019 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3020 // The input has to be a vector type, we have to either scalarize it, pack
3021 // it, or convert it based on whether the input vector type is legal.
3022 SDNode *InVal = Node->getOperand(0).Val;
3023 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3024 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3025
3026 // Figure out if there is a simple type corresponding to this Vector
3027 // type. If so, convert to the vector type.
3028 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3029 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003030 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003031 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3032 LegalizeOp(Node->getOperand(0)));
3033 break;
3034 } else if (NumElems == 1) {
3035 // Turn this into a bit convert of the scalar input.
3036 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3037 ScalarizeVectorOp(Node->getOperand(0)));
3038 break;
3039 } else {
3040 // FIXME: UNIMP! Store then reload
3041 assert(0 && "Cast from unsupported vector type not implemented yet!");
3042 }
Chris Lattner67993f72006-01-23 07:30:46 +00003043 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003044 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3045 Node->getOperand(0).getValueType())) {
3046 default: assert(0 && "Unknown operation action!");
3047 case TargetLowering::Expand:
3048 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3049 break;
3050 case TargetLowering::Legal:
3051 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003052 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003053 break;
3054 }
3055 }
3056 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003057
Chris Lattner2c8086f2005-04-02 05:00:07 +00003058 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003059 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003060 case ISD::UINT_TO_FP: {
3061 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003062 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3063 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003064 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003065 Node->getOperand(0).getValueType())) {
3066 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003067 case TargetLowering::Custom:
3068 isCustom = true;
3069 // FALLTHROUGH
3070 case TargetLowering::Legal:
3071 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003072 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003073 if (isCustom) {
3074 Tmp1 = TLI.LowerOperation(Result, DAG);
3075 if (Tmp1.Val) Result = Tmp1;
3076 }
3077 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003078 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003079 Result = ExpandLegalINT_TO_FP(isSigned,
3080 LegalizeOp(Node->getOperand(0)),
3081 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003082 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003083 case TargetLowering::Promote:
3084 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3085 Node->getValueType(0),
3086 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003087 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003088 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003089 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003090 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003091 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3092 Node->getValueType(0), Node->getOperand(0));
3093 break;
3094 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003095 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003096 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003097 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3098 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003099 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003100 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3101 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003102 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003103 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3104 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003105 break;
3106 }
3107 break;
3108 }
3109 case ISD::TRUNCATE:
3110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3111 case Legal:
3112 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003113 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003114 break;
3115 case Expand:
3116 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3117
3118 // Since the result is legal, we should just be able to truncate the low
3119 // part of the source.
3120 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3121 break;
3122 case Promote:
3123 Result = PromoteOp(Node->getOperand(0));
3124 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3125 break;
3126 }
3127 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003128
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003129 case ISD::FP_TO_SINT:
3130 case ISD::FP_TO_UINT:
3131 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3132 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003133 Tmp1 = LegalizeOp(Node->getOperand(0));
3134
Chris Lattner1618beb2005-07-29 00:11:56 +00003135 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3136 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003137 case TargetLowering::Custom:
3138 isCustom = true;
3139 // FALLTHROUGH
3140 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003141 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003142 if (isCustom) {
3143 Tmp1 = TLI.LowerOperation(Result, DAG);
3144 if (Tmp1.Val) Result = Tmp1;
3145 }
3146 break;
3147 case TargetLowering::Promote:
3148 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3149 Node->getOpcode() == ISD::FP_TO_SINT);
3150 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003151 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003152 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3153 SDOperand True, False;
3154 MVT::ValueType VT = Node->getOperand(0).getValueType();
3155 MVT::ValueType NVT = Node->getValueType(0);
3156 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3157 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3158 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3159 Node->getOperand(0), Tmp2, ISD::SETLT);
3160 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3161 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003162 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003163 Tmp2));
3164 False = DAG.getNode(ISD::XOR, NVT, False,
3165 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003166 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3167 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003168 } else {
3169 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3170 }
3171 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003172 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003173 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003174 case Expand: {
3175 // Convert f32 / f64 to i32 / i64.
3176 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003177 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003178 switch (Node->getOpcode()) {
3179 case ISD::FP_TO_SINT:
3180 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003181 LC = (VT == MVT::i32)
3182 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003183 else
Evan Cheng56966222007-01-12 02:11:51 +00003184 LC = (VT == MVT::i32)
3185 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003186 break;
3187 case ISD::FP_TO_UINT:
3188 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003189 LC = (VT == MVT::i32)
3190 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003191 else
Evan Cheng56966222007-01-12 02:11:51 +00003192 LC = (VT == MVT::i32)
3193 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003194 break;
3195 default: assert(0 && "Unreachable!");
3196 }
3197 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003198 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3199 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003200 break;
3201 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003202 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003203 Tmp1 = PromoteOp(Node->getOperand(0));
3204 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3205 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003206 break;
3207 }
3208 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003209
Dale Johannesenab081c72007-08-09 17:27:48 +00003210 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003211 case ISD::FP_ROUND: {
3212 MVT::ValueType newVT = Op.getValueType();
3213 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3214 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenab081c72007-08-09 17:27:48 +00003215 // The only way we can lower this is to turn it into a STORE,
Dale Johannesen5411a392007-08-09 01:04:01 +00003216 // LOAD pair, targetting a temporary location (a stack slot).
3217
3218 // NOTE: there is a choice here between constantly creating new stack
3219 // slots and always reusing the same one. We currently always create
3220 // new ones, as reuse may inhibit scheduling.
Dale Johannesenab081c72007-08-09 17:27:48 +00003221 MVT::ValueType slotVT =
3222 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3223 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesen5411a392007-08-09 01:04:01 +00003224 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3225 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3226 MachineFunction &MF = DAG.getMachineFunction();
3227 int SSFI =
3228 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3229 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenab081c72007-08-09 17:27:48 +00003230 if (Node->getOpcode() == ISD::FP_EXTEND) {
3231 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3232 StackSlot, NULL, 0);
3233 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3234 Result, StackSlot, NULL, 0, oldVT);
3235 } else {
3236 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3237 StackSlot, NULL, 0, newVT);
3238 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3239 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003240 break;
3241 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003242 }
3243 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003244 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003245 case ISD::ZERO_EXTEND:
3246 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003247 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003248 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003249 case Legal:
3250 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003251 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003252 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003253 case Promote:
3254 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003255 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003256 Tmp1 = PromoteOp(Node->getOperand(0));
3257 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003258 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003259 case ISD::ZERO_EXTEND:
3260 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003261 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003262 Result = DAG.getZeroExtendInReg(Result,
3263 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003264 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003265 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003266 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003267 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003268 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003269 Result,
3270 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003271 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003272 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003273 Result = PromoteOp(Node->getOperand(0));
3274 if (Result.getValueType() != Op.getValueType())
3275 // Dynamically dead while we have only 2 FP types.
3276 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3277 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003278 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003279 Result = PromoteOp(Node->getOperand(0));
3280 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3281 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003282 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003283 }
3284 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003285 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003286 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003287 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003288 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003289
3290 // If this operation is not supported, convert it to a shl/shr or load/store
3291 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003292 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3293 default: assert(0 && "This action not supported for this op yet!");
3294 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003295 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003296 break;
3297 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003298 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003299 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003300 // NOTE: we could fall back on load/store here too for targets without
3301 // SAR. However, it is doubtful that any exist.
3302 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3303 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003304 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003305 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3306 Node->getOperand(0), ShiftCst);
3307 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3308 Result, ShiftCst);
3309 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003310 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003311 // EXTLOAD pair, targetting a temporary location (a stack slot).
3312
3313 // NOTE: there is a choice here between constantly creating new stack
3314 // slots and always reusing the same one. We currently always create
3315 // new ones, as reuse may inhibit scheduling.
3316 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003317 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003318 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003319 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003320 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003321 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003322 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003323 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3324 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003325 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003326 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003327 } else {
3328 assert(0 && "Unknown op");
3329 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003330 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003331 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003332 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003333 }
Duncan Sands36397f52007-07-27 12:58:54 +00003334 case ISD::ADJUST_TRAMP: {
3335 Tmp1 = LegalizeOp(Node->getOperand(0));
3336 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3337 default: assert(0 && "This action is not supported yet!");
3338 case TargetLowering::Custom:
3339 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3340 Result = TLI.LowerOperation(Result, DAG);
3341 if (Result.Val) break;
3342 // FALL THROUGH
3343 case TargetLowering::Expand:
3344 Result = Tmp1;
3345 break;
3346 }
3347 break;
3348 }
3349 case ISD::TRAMPOLINE: {
3350 SDOperand Ops[6];
3351 for (unsigned i = 0; i != 6; ++i)
3352 Ops[i] = LegalizeOp(Node->getOperand(i));
3353 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3354 // The only option for this node is to custom lower it.
3355 Result = TLI.LowerOperation(Result, DAG);
3356 assert(Result.Val && "Should always custom lower!");
3357 break;
3358 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003359 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003360
Chris Lattner4ddd2832006-04-08 04:13:17 +00003361 assert(Result.getValueType() == Op.getValueType() &&
3362 "Bad legalization!");
3363
Chris Lattner456a93a2006-01-28 07:39:30 +00003364 // Make sure that the generated code is itself legal.
3365 if (Result != Op)
3366 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003367
Chris Lattner45982da2005-05-12 16:53:42 +00003368 // Note that LegalizeOp may be reentered even from single-use nodes, which
3369 // means that we always must cache transformed nodes.
3370 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003371 return Result;
3372}
3373
Chris Lattner8b6fa222005-01-15 22:16:26 +00003374/// PromoteOp - Given an operation that produces a value in an invalid type,
3375/// promote it to compute the value into a larger type. The produced value will
3376/// have the correct bits for the low portion of the register, but no guarantee
3377/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003378SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3379 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003380 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003381 assert(getTypeAction(VT) == Promote &&
3382 "Caller should expand or legalize operands that are not promotable!");
3383 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3384 "Cannot promote to smaller type!");
3385
Chris Lattner03c85462005-01-15 05:21:40 +00003386 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003387 SDOperand Result;
3388 SDNode *Node = Op.Val;
3389
Chris Lattner40030bf2007-02-04 01:17:38 +00003390 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003391 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003392
Chris Lattner03c85462005-01-15 05:21:40 +00003393 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003394 case ISD::CopyFromReg:
3395 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003396 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003397#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003398 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003399#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003400 assert(0 && "Do not know how to promote this operator!");
3401 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003402 case ISD::UNDEF:
3403 Result = DAG.getNode(ISD::UNDEF, NVT);
3404 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003405 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003406 if (VT != MVT::i1)
3407 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3408 else
3409 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003410 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3411 break;
3412 case ISD::ConstantFP:
3413 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3414 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3415 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003416
Chris Lattner82fbfb62005-01-18 02:59:52 +00003417 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003418 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003419 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3420 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003421 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003422
Chris Lattner03c85462005-01-15 05:21:40 +00003423 case ISD::TRUNCATE:
3424 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3425 case Legal:
3426 Result = LegalizeOp(Node->getOperand(0));
3427 assert(Result.getValueType() >= NVT &&
3428 "This truncation doesn't make sense!");
3429 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3430 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3431 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003432 case Promote:
3433 // The truncation is not required, because we don't guarantee anything
3434 // about high bits anyway.
3435 Result = PromoteOp(Node->getOperand(0));
3436 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003437 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003438 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3439 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003440 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003441 }
3442 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003443 case ISD::SIGN_EXTEND:
3444 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003445 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3447 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3448 case Legal:
3449 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003450 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003451 break;
3452 case Promote:
3453 // Promote the reg if it's smaller.
3454 Result = PromoteOp(Node->getOperand(0));
3455 // The high bits are not guaranteed to be anything. Insert an extend.
3456 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003457 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003458 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003459 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003460 Result = DAG.getZeroExtendInReg(Result,
3461 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003462 break;
3463 }
3464 break;
Chris Lattner35481892005-12-23 00:16:34 +00003465 case ISD::BIT_CONVERT:
3466 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3467 Result = PromoteOp(Result);
3468 break;
3469
Chris Lattner8b6fa222005-01-15 22:16:26 +00003470 case ISD::FP_EXTEND:
3471 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3472 case ISD::FP_ROUND:
3473 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3474 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3475 case Promote: assert(0 && "Unreachable with 2 FP types!");
3476 case Legal:
3477 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003478 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003479 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003480 break;
3481 }
3482 break;
3483
3484 case ISD::SINT_TO_FP:
3485 case ISD::UINT_TO_FP:
3486 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3487 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003488 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003489 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003490 break;
3491
3492 case Promote:
3493 Result = PromoteOp(Node->getOperand(0));
3494 if (Node->getOpcode() == ISD::SINT_TO_FP)
3495 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003496 Result,
3497 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003498 else
Chris Lattner23993562005-04-13 02:38:47 +00003499 Result = DAG.getZeroExtendInReg(Result,
3500 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003501 // No extra round required here.
3502 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003503 break;
3504 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003505 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3506 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003507 // Round if we cannot tolerate excess precision.
3508 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003509 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3510 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003511 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003512 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003513 break;
3514
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003515 case ISD::SIGN_EXTEND_INREG:
3516 Result = PromoteOp(Node->getOperand(0));
3517 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3518 Node->getOperand(1));
3519 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003520 case ISD::FP_TO_SINT:
3521 case ISD::FP_TO_UINT:
3522 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3523 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003524 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003525 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003526 break;
3527 case Promote:
3528 // The input result is prerounded, so we don't have to do anything
3529 // special.
3530 Tmp1 = PromoteOp(Node->getOperand(0));
3531 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003532 }
Nate Begemand2558e32005-08-14 01:20:53 +00003533 // If we're promoting a UINT to a larger size, check to see if the new node
3534 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3535 // we can use that instead. This allows us to generate better code for
3536 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3537 // legal, such as PowerPC.
3538 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003539 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003540 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3541 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003542 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3543 } else {
3544 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3545 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003546 break;
3547
Chris Lattner2c8086f2005-04-02 05:00:07 +00003548 case ISD::FABS:
3549 case ISD::FNEG:
3550 Tmp1 = PromoteOp(Node->getOperand(0));
3551 assert(Tmp1.getValueType() == NVT);
3552 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3553 // NOTE: we do not have to do any extra rounding here for
3554 // NoExcessFPPrecision, because we know the input will have the appropriate
3555 // precision, and these operations don't modify precision at all.
3556 break;
3557
Chris Lattnerda6ba872005-04-28 21:44:33 +00003558 case ISD::FSQRT:
3559 case ISD::FSIN:
3560 case ISD::FCOS:
3561 Tmp1 = PromoteOp(Node->getOperand(0));
3562 assert(Tmp1.getValueType() == NVT);
3563 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003564 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003565 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3566 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003567 break;
3568
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003569 case ISD::FPOWI: {
3570 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3571 // directly as well, which may be better.
3572 Tmp1 = PromoteOp(Node->getOperand(0));
3573 assert(Tmp1.getValueType() == NVT);
3574 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3575 if (NoExcessFPPrecision)
3576 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3577 DAG.getValueType(VT));
3578 break;
3579 }
3580
Chris Lattner03c85462005-01-15 05:21:40 +00003581 case ISD::AND:
3582 case ISD::OR:
3583 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003584 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003585 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003586 case ISD::MUL:
3587 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003588 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003589 // that too is okay if they are integer operations.
3590 Tmp1 = PromoteOp(Node->getOperand(0));
3591 Tmp2 = PromoteOp(Node->getOperand(1));
3592 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3593 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003594 break;
3595 case ISD::FADD:
3596 case ISD::FSUB:
3597 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003598 Tmp1 = PromoteOp(Node->getOperand(0));
3599 Tmp2 = PromoteOp(Node->getOperand(1));
3600 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3601 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3602
3603 // Floating point operations will give excess precision that we may not be
3604 // able to tolerate. If we DO allow excess precision, just leave it,
3605 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003606 // FIXME: Why would we need to round FP ops more than integer ones?
3607 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003608 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003609 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3610 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003611 break;
3612
Chris Lattner8b6fa222005-01-15 22:16:26 +00003613 case ISD::SDIV:
3614 case ISD::SREM:
3615 // These operators require that their input be sign extended.
3616 Tmp1 = PromoteOp(Node->getOperand(0));
3617 Tmp2 = PromoteOp(Node->getOperand(1));
3618 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003619 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3620 DAG.getValueType(VT));
3621 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3622 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003623 }
3624 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3625
3626 // Perform FP_ROUND: this is probably overly pessimistic.
3627 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003628 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3629 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003630 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003631 case ISD::FDIV:
3632 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003633 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003634 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003635 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3636 case Legal:
3637 Tmp1 = LegalizeOp(Node->getOperand(0));
3638 break;
3639 case Promote:
3640 Tmp1 = PromoteOp(Node->getOperand(0));
3641 break;
3642 case Expand:
3643 assert(0 && "not implemented");
3644 }
3645 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3646 case Legal:
3647 Tmp2 = LegalizeOp(Node->getOperand(1));
3648 break;
3649 case Promote:
3650 Tmp2 = PromoteOp(Node->getOperand(1));
3651 break;
3652 case Expand:
3653 assert(0 && "not implemented");
3654 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003655 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3656
3657 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003658 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003659 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3660 DAG.getValueType(VT));
3661 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003662
3663 case ISD::UDIV:
3664 case ISD::UREM:
3665 // These operators require that their input be zero extended.
3666 Tmp1 = PromoteOp(Node->getOperand(0));
3667 Tmp2 = PromoteOp(Node->getOperand(1));
3668 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003669 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3670 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003671 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3672 break;
3673
3674 case ISD::SHL:
3675 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003676 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003677 break;
3678 case ISD::SRA:
3679 // The input value must be properly sign extended.
3680 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003681 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3682 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003683 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003684 break;
3685 case ISD::SRL:
3686 // The input value must be properly zero extended.
3687 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003688 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003689 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003690 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003691
3692 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003693 Tmp1 = Node->getOperand(0); // Get the chain.
3694 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003695 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3696 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3697 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3698 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003699 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003700 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003701 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003702 // Increment the pointer, VAList, to the next vaarg
3703 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3704 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3705 TLI.getPointerTy()));
3706 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003707 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3708 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003709 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003710 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003711 }
3712 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003713 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003714 break;
3715
Evan Cheng466685d2006-10-09 20:57:25 +00003716 case ISD::LOAD: {
3717 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003718 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3719 ? ISD::EXTLOAD : LD->getExtensionType();
3720 Result = DAG.getExtLoad(ExtType, NVT,
3721 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003722 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003723 LD->getLoadedVT(),
3724 LD->isVolatile(),
3725 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003726 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003727 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003728 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003729 }
Chris Lattner03c85462005-01-15 05:21:40 +00003730 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003731 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3732 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003733 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003734 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003735 case ISD::SELECT_CC:
3736 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3737 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3738 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003739 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003740 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003741 case ISD::BSWAP:
3742 Tmp1 = Node->getOperand(0);
3743 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3744 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3745 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003746 DAG.getConstant(MVT::getSizeInBits(NVT) -
3747 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003748 TLI.getShiftAmountTy()));
3749 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003750 case ISD::CTPOP:
3751 case ISD::CTTZ:
3752 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003753 // Zero extend the argument
3754 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003755 // Perform the larger operation, then subtract if needed.
3756 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003757 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003758 case ISD::CTPOP:
3759 Result = Tmp1;
3760 break;
3761 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003762 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003763 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003764 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3765 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003766 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003767 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003768 break;
3769 case ISD::CTLZ:
3770 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003771 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003772 DAG.getConstant(MVT::getSizeInBits(NVT) -
3773 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003774 break;
3775 }
3776 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003777 case ISD::EXTRACT_SUBVECTOR:
3778 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003779 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003780 case ISD::EXTRACT_VECTOR_ELT:
3781 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3782 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003783 }
3784
3785 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003786
3787 // Make sure the result is itself legal.
3788 Result = LegalizeOp(Result);
3789
3790 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003791 AddPromotedOperand(Op, Result);
3792 return Result;
3793}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003794
Dan Gohman7f321562007-06-25 16:23:39 +00003795/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3796/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3797/// based on the vector type. The return type of this matches the element type
3798/// of the vector, which may not be legal for the target.
3799SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003800 // We know that operand #0 is the Vec vector. If the index is a constant
3801 // or if the invec is a supported hardware type, we can use it. Otherwise,
3802 // lower to a store then an indexed load.
3803 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003804 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003805
3806 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003807 MVT::ValueType TVT = InVal->getValueType(0);
3808 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003809
Dan Gohman7f321562007-06-25 16:23:39 +00003810 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3811 default: assert(0 && "This action is not supported yet!");
3812 case TargetLowering::Custom: {
3813 Vec = LegalizeOp(Vec);
3814 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3815 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3816 if (Tmp3.Val)
3817 return Tmp3;
3818 break;
3819 }
3820 case TargetLowering::Legal:
3821 if (isTypeLegal(TVT)) {
3822 Vec = LegalizeOp(Vec);
3823 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003824 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003825 }
3826 break;
3827 case TargetLowering::Expand:
3828 break;
3829 }
3830
3831 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003832 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003833 Op = ScalarizeVectorOp(Vec);
3834 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3835 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003836 SDOperand Lo, Hi;
3837 SplitVectorOp(Vec, Lo, Hi);
3838 if (CIdx->getValue() < NumElems/2) {
3839 Vec = Lo;
3840 } else {
3841 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003842 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3843 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003844 }
Dan Gohman7f321562007-06-25 16:23:39 +00003845
Chris Lattner15972212006-03-31 17:55:51 +00003846 // It's now an extract from the appropriate high or low part. Recurse.
3847 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003848 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003849 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003850 // Store the value to a temporary stack slot, then LOAD the scalar
3851 // element back out.
3852 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3853 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3854
3855 // Add the offset to the index.
3856 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3857 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3858 DAG.getConstant(EltSize, Idx.getValueType()));
3859 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3860
3861 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003862 }
Dan Gohman7f321562007-06-25 16:23:39 +00003863 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003864}
3865
Dan Gohman7f321562007-06-25 16:23:39 +00003866/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003867/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003868SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003869 // We know that operand #0 is the Vec vector. For now we assume the index
3870 // is a constant and that the extracted result is a supported hardware type.
3871 SDOperand Vec = Op.getOperand(0);
3872 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3873
Dan Gohman7f321562007-06-25 16:23:39 +00003874 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003875
3876 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3877 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003878 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003879 }
3880
3881 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3882 SDOperand Lo, Hi;
3883 SplitVectorOp(Vec, Lo, Hi);
3884 if (CIdx->getValue() < NumElems/2) {
3885 Vec = Lo;
3886 } else {
3887 Vec = Hi;
3888 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3889 }
3890
3891 // It's now an extract from the appropriate high or low part. Recurse.
3892 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003893 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003894}
3895
Nate Begeman750ac1b2006-02-01 07:19:44 +00003896/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3897/// with condition CC on the current target. This usually involves legalizing
3898/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3899/// there may be no choice but to create a new SetCC node to represent the
3900/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3901/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3902void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3903 SDOperand &RHS,
3904 SDOperand &CC) {
3905 SDOperand Tmp1, Tmp2, Result;
3906
3907 switch (getTypeAction(LHS.getValueType())) {
3908 case Legal:
3909 Tmp1 = LegalizeOp(LHS); // LHS
3910 Tmp2 = LegalizeOp(RHS); // RHS
3911 break;
3912 case Promote:
3913 Tmp1 = PromoteOp(LHS); // LHS
3914 Tmp2 = PromoteOp(RHS); // RHS
3915
3916 // If this is an FP compare, the operands have already been extended.
3917 if (MVT::isInteger(LHS.getValueType())) {
3918 MVT::ValueType VT = LHS.getValueType();
3919 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3920
3921 // Otherwise, we have to insert explicit sign or zero extends. Note
3922 // that we could insert sign extends for ALL conditions, but zero extend
3923 // is cheaper on many machines (an AND instead of two shifts), so prefer
3924 // it.
3925 switch (cast<CondCodeSDNode>(CC)->get()) {
3926 default: assert(0 && "Unknown integer comparison!");
3927 case ISD::SETEQ:
3928 case ISD::SETNE:
3929 case ISD::SETUGE:
3930 case ISD::SETUGT:
3931 case ISD::SETULE:
3932 case ISD::SETULT:
3933 // ALL of these operations will work if we either sign or zero extend
3934 // the operands (including the unsigned comparisons!). Zero extend is
3935 // usually a simpler/cheaper operation, so prefer it.
3936 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3937 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3938 break;
3939 case ISD::SETGE:
3940 case ISD::SETGT:
3941 case ISD::SETLT:
3942 case ISD::SETLE:
3943 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3944 DAG.getValueType(VT));
3945 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3946 DAG.getValueType(VT));
3947 break;
3948 }
3949 }
3950 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003951 case Expand: {
3952 MVT::ValueType VT = LHS.getValueType();
3953 if (VT == MVT::f32 || VT == MVT::f64) {
3954 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003955 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003956 switch (cast<CondCodeSDNode>(CC)->get()) {
3957 case ISD::SETEQ:
3958 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003959 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003960 break;
3961 case ISD::SETNE:
3962 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003963 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003964 break;
3965 case ISD::SETGE:
3966 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003967 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003968 break;
3969 case ISD::SETLT:
3970 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003971 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003972 break;
3973 case ISD::SETLE:
3974 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003975 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003976 break;
3977 case ISD::SETGT:
3978 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003979 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003980 break;
3981 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003982 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3983 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003984 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003985 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003986 break;
3987 default:
Evan Cheng56966222007-01-12 02:11:51 +00003988 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003989 switch (cast<CondCodeSDNode>(CC)->get()) {
3990 case ISD::SETONE:
3991 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003992 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003993 // Fallthrough
3994 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003995 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003996 break;
3997 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003998 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003999 break;
4000 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004001 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004002 break;
4003 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004004 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004005 break;
Evan Cheng56966222007-01-12 02:11:51 +00004006 case ISD::SETUEQ:
4007 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004008 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004009 default: assert(0 && "Unsupported FP setcc!");
4010 }
4011 }
4012
4013 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004014 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004015 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004016 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004017 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004018 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004019 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004020 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004021 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004022 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004023 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004024 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004025 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004026 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4027 Tmp2 = SDOperand();
4028 }
4029 LHS = Tmp1;
4030 RHS = Tmp2;
4031 return;
4032 }
4033
Nate Begeman750ac1b2006-02-01 07:19:44 +00004034 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4035 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00004036 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004037 switch (cast<CondCodeSDNode>(CC)->get()) {
4038 case ISD::SETEQ:
4039 case ISD::SETNE:
4040 if (RHSLo == RHSHi)
4041 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4042 if (RHSCST->isAllOnesValue()) {
4043 // Comparison to -1.
4044 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4045 Tmp2 = RHSLo;
4046 break;
4047 }
4048
4049 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4050 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4051 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4052 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4053 break;
4054 default:
4055 // If this is a comparison of the sign bit, just look at the top part.
4056 // X > -1, x < 0
4057 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4058 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4059 CST->getValue() == 0) || // X < 0
4060 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4061 CST->isAllOnesValue())) { // X > -1
4062 Tmp1 = LHSHi;
4063 Tmp2 = RHSHi;
4064 break;
4065 }
4066
4067 // FIXME: This generated code sucks.
4068 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004069 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4070 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004071 default: assert(0 && "Unknown integer setcc!");
4072 case ISD::SETLT:
4073 case ISD::SETULT: LowCC = ISD::SETULT; break;
4074 case ISD::SETGT:
4075 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4076 case ISD::SETLE:
4077 case ISD::SETULE: LowCC = ISD::SETULE; break;
4078 case ISD::SETGE:
4079 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4080 }
4081
4082 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4083 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4084 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4085
4086 // NOTE: on targets without efficient SELECT of bools, we can always use
4087 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004088 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4089 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4090 false, DagCombineInfo);
4091 if (!Tmp1.Val)
4092 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4093 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4094 CCCode, false, DagCombineInfo);
4095 if (!Tmp2.Val)
4096 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4097
4098 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4099 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4100 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4101 (Tmp2C && Tmp2C->getValue() == 0 &&
4102 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4103 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4104 (Tmp2C && Tmp2C->getValue() == 1 &&
4105 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4106 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4107 // low part is known false, returns high part.
4108 // For LE / GE, if high part is known false, ignore the low part.
4109 // For LT / GT, if high part is known true, ignore the low part.
4110 Tmp1 = Tmp2;
4111 Tmp2 = SDOperand();
4112 } else {
4113 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4114 ISD::SETEQ, false, DagCombineInfo);
4115 if (!Result.Val)
4116 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4117 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4118 Result, Tmp1, Tmp2));
4119 Tmp1 = Result;
4120 Tmp2 = SDOperand();
4121 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004122 }
4123 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004124 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004125 LHS = Tmp1;
4126 RHS = Tmp2;
4127}
4128
Chris Lattner35481892005-12-23 00:16:34 +00004129/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004130/// The resultant code need not be legal. Note that SrcOp is the input operand
4131/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004132SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4133 SDOperand SrcOp) {
4134 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004135 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004136
4137 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004138 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004139 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004140 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004141}
4142
Chris Lattner4352cc92006-04-04 17:23:26 +00004143SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4144 // Create a vector sized/aligned stack slot, store the value to element #0,
4145 // then load the whole vector back out.
4146 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004147 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004148 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004149 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004150}
4151
4152
Chris Lattnerce872152006-03-19 06:31:19 +00004153/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004154/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004155SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4156
4157 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004158 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004159 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004160 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004161 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004162 std::map<SDOperand, std::vector<unsigned> > Values;
4163 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004164 bool isConstant = true;
4165 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4166 SplatValue.getOpcode() != ISD::UNDEF)
4167 isConstant = false;
4168
Evan Cheng033e6812006-03-24 01:17:21 +00004169 for (unsigned i = 1; i < NumElems; ++i) {
4170 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004171 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004172 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004173 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004174 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004175 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004176
4177 // If this isn't a constant element or an undef, we can't use a constant
4178 // pool load.
4179 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4180 V.getOpcode() != ISD::UNDEF)
4181 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004182 }
4183
4184 if (isOnlyLowElement) {
4185 // If the low element is an undef too, then this whole things is an undef.
4186 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4187 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4188 // Otherwise, turn this into a scalar_to_vector node.
4189 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4190 Node->getOperand(0));
4191 }
4192
Chris Lattner2eb86532006-03-24 07:29:17 +00004193 // If all elements are constants, create a load from the constant pool.
4194 if (isConstant) {
4195 MVT::ValueType VT = Node->getValueType(0);
4196 const Type *OpNTy =
4197 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4198 std::vector<Constant*> CV;
4199 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4200 if (ConstantFPSDNode *V =
4201 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4202 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4203 } else if (ConstantSDNode *V =
4204 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004205 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004206 } else {
4207 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4208 CV.push_back(UndefValue::get(OpNTy));
4209 }
4210 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004211 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004212 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004213 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004214 }
4215
Chris Lattner87100e02006-03-20 01:52:29 +00004216 if (SplatValue.Val) { // Splat of one value?
4217 // Build the shuffle constant vector: <0, 0, 0, 0>
4218 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004219 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004220 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004221 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004222 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4223 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004224
4225 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004226 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004227 // Get the splatted value into the low element of a vector register.
4228 SDOperand LowValVec =
4229 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4230
4231 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4232 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4233 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4234 SplatMask);
4235 }
4236 }
4237
Evan Cheng033e6812006-03-24 01:17:21 +00004238 // If there are only two unique elements, we may be able to turn this into a
4239 // vector shuffle.
4240 if (Values.size() == 2) {
4241 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4242 MVT::ValueType MaskVT =
4243 MVT::getIntVectorWithNumElements(NumElems);
4244 std::vector<SDOperand> MaskVec(NumElems);
4245 unsigned i = 0;
4246 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4247 E = Values.end(); I != E; ++I) {
4248 for (std::vector<unsigned>::iterator II = I->second.begin(),
4249 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004250 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004251 i += NumElems;
4252 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004253 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4254 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004255
4256 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004257 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4258 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004259 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004260 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4261 E = Values.end(); I != E; ++I) {
4262 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4263 I->first);
4264 Ops.push_back(Op);
4265 }
4266 Ops.push_back(ShuffleMask);
4267
4268 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004269 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4270 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004271 }
4272 }
Chris Lattnerce872152006-03-19 06:31:19 +00004273
4274 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4275 // aligned object on the stack, store each element into it, then load
4276 // the result as a vector.
4277 MVT::ValueType VT = Node->getValueType(0);
4278 // Create the stack frame object.
4279 SDOperand FIPtr = CreateStackTemporary(VT);
4280
4281 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004282 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004283 unsigned TypeByteSize =
4284 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004285 // Store (in the right endianness) the elements to memory.
4286 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4287 // Ignore undef elements.
4288 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4289
Chris Lattner841c8822006-03-22 01:46:54 +00004290 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004291
4292 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4293 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4294
Evan Cheng786225a2006-10-05 23:01:46 +00004295 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004296 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004297 }
4298
4299 SDOperand StoreChain;
4300 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004301 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4302 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004303 else
4304 StoreChain = DAG.getEntryNode();
4305
4306 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004307 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004308}
4309
4310/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4311/// specified value type.
4312SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4313 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4314 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004315 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004316 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004317 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004318 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4319}
4320
Chris Lattner5b359c62005-04-02 04:00:59 +00004321void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4322 SDOperand Op, SDOperand Amt,
4323 SDOperand &Lo, SDOperand &Hi) {
4324 // Expand the subcomponents.
4325 SDOperand LHSL, LHSH;
4326 ExpandOp(Op, LHSL, LHSH);
4327
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004328 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004329 MVT::ValueType VT = LHSL.getValueType();
4330 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004331 Hi = Lo.getValue(1);
4332}
4333
4334
Chris Lattnere34b3962005-01-19 04:19:40 +00004335/// ExpandShift - Try to find a clever way to expand this shift operation out to
4336/// smaller elements. If we can't find a way that is more efficient than a
4337/// libcall on this target, return false. Otherwise, return true with the
4338/// low-parts expanded into Lo and Hi.
4339bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4340 SDOperand &Lo, SDOperand &Hi) {
4341 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4342 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004343
Chris Lattnere34b3962005-01-19 04:19:40 +00004344 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004345 SDOperand ShAmt = LegalizeOp(Amt);
4346 MVT::ValueType ShTy = ShAmt.getValueType();
4347 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4348 unsigned NVTBits = MVT::getSizeInBits(NVT);
4349
4350 // Handle the case when Amt is an immediate. Other cases are currently broken
4351 // and are disabled.
4352 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4353 unsigned Cst = CN->getValue();
4354 // Expand the incoming operand to be shifted, so that we have its parts
4355 SDOperand InL, InH;
4356 ExpandOp(Op, InL, InH);
4357 switch(Opc) {
4358 case ISD::SHL:
4359 if (Cst > VTBits) {
4360 Lo = DAG.getConstant(0, NVT);
4361 Hi = DAG.getConstant(0, NVT);
4362 } else if (Cst > NVTBits) {
4363 Lo = DAG.getConstant(0, NVT);
4364 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004365 } else if (Cst == NVTBits) {
4366 Lo = DAG.getConstant(0, NVT);
4367 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004368 } else {
4369 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4370 Hi = DAG.getNode(ISD::OR, NVT,
4371 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4372 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4373 }
4374 return true;
4375 case ISD::SRL:
4376 if (Cst > VTBits) {
4377 Lo = DAG.getConstant(0, NVT);
4378 Hi = DAG.getConstant(0, NVT);
4379 } else if (Cst > NVTBits) {
4380 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4381 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004382 } else if (Cst == NVTBits) {
4383 Lo = InH;
4384 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004385 } else {
4386 Lo = DAG.getNode(ISD::OR, NVT,
4387 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4388 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4389 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4390 }
4391 return true;
4392 case ISD::SRA:
4393 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004394 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004395 DAG.getConstant(NVTBits-1, ShTy));
4396 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004397 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004398 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004399 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004400 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004401 } else if (Cst == NVTBits) {
4402 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004403 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004404 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004405 } else {
4406 Lo = DAG.getNode(ISD::OR, NVT,
4407 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4408 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4409 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4410 }
4411 return true;
4412 }
4413 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004414
4415 // Okay, the shift amount isn't constant. However, if we can tell that it is
4416 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4417 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004418 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004419
4420 // If we know that the high bit of the shift amount is one, then we can do
4421 // this as a couple of simple shifts.
4422 if (KnownOne & Mask) {
4423 // Mask out the high bit, which we know is set.
4424 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4425 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4426
4427 // Expand the incoming operand to be shifted, so that we have its parts
4428 SDOperand InL, InH;
4429 ExpandOp(Op, InL, InH);
4430 switch(Opc) {
4431 case ISD::SHL:
4432 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4433 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4434 return true;
4435 case ISD::SRL:
4436 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4437 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4438 return true;
4439 case ISD::SRA:
4440 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4441 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4442 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4443 return true;
4444 }
4445 }
4446
4447 // If we know that the high bit of the shift amount is zero, then we can do
4448 // this as a couple of simple shifts.
4449 if (KnownZero & Mask) {
4450 // Compute 32-amt.
4451 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4452 DAG.getConstant(NVTBits, Amt.getValueType()),
4453 Amt);
4454
4455 // Expand the incoming operand to be shifted, so that we have its parts
4456 SDOperand InL, InH;
4457 ExpandOp(Op, InL, InH);
4458 switch(Opc) {
4459 case ISD::SHL:
4460 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4461 Hi = DAG.getNode(ISD::OR, NVT,
4462 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4463 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4464 return true;
4465 case ISD::SRL:
4466 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4467 Lo = DAG.getNode(ISD::OR, NVT,
4468 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4469 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4470 return true;
4471 case ISD::SRA:
4472 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4473 Lo = DAG.getNode(ISD::OR, NVT,
4474 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4475 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4476 return true;
4477 }
4478 }
4479
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004480 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004481}
Chris Lattner77e77a62005-01-21 06:05:23 +00004482
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004483
Chris Lattner77e77a62005-01-21 06:05:23 +00004484// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4485// does not fit into a register, return the lo part and set the hi part to the
4486// by-reg argument. If it does fit into a single register, return the result
4487// and leave the Hi part unset.
4488SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004489 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004490 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4491 // The input chain to this libcall is the entry node of the function.
4492 // Legalizing the call will automatically add the previous call to the
4493 // dependence.
4494 SDOperand InChain = DAG.getEntryNode();
4495
Chris Lattner77e77a62005-01-21 06:05:23 +00004496 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004497 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004498 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4499 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4500 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004501 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004502 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004503 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004504 }
4505 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004506
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004507 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004508 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004509 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004510 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004511 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004512
Chris Lattner6831a812006-02-13 09:18:02 +00004513 // Legalize the call sequence, starting with the chain. This will advance
4514 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4515 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4516 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004517 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004518 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004519 default: assert(0 && "Unknown thing");
4520 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004521 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004522 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004523 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004524 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004525 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004526 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004527 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004528}
4529
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004530
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004531/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4532///
Chris Lattner77e77a62005-01-21 06:05:23 +00004533SDOperand SelectionDAGLegalize::
4534ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004535 assert(getTypeAction(Source.getValueType()) == Expand &&
4536 "This is not an expansion!");
4537 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4538
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004539 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004540 assert(Source.getValueType() == MVT::i64 &&
4541 "This only works for 64-bit -> FP");
4542 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4543 // incoming integer is set. To handle this, we dynamically test to see if
4544 // it is set, and, if so, add a fudge factor.
4545 SDOperand Lo, Hi;
4546 ExpandOp(Source, Lo, Hi);
4547
Chris Lattner66de05b2005-05-13 04:45:13 +00004548 // If this is unsigned, and not supported, first perform the conversion to
4549 // signed, then adjust the result if the sign bit is set.
4550 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4551 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4552
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004553 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4554 DAG.getConstant(0, Hi.getValueType()),
4555 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004556 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4557 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4558 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004559 uint64_t FF = 0x5f800000ULL;
4560 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004561 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004562
Chris Lattner5839bf22005-08-26 17:15:30 +00004563 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004564 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4565 SDOperand FudgeInReg;
4566 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004567 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004568 else {
4569 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004570 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004571 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004572 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004573 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004574 MVT::ValueType SCVT = SignedConv.getValueType();
4575 if (SCVT != DestTy) {
4576 // Destination type needs to be expanded as well. The FADD now we are
4577 // constructing will be expanded into a libcall.
4578 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4579 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4580 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4581 SignedConv, SignedConv.getValue(1));
4582 }
4583 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4584 }
Chris Lattner473a9902005-09-29 06:44:39 +00004585 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004586 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004587
Chris Lattnera88a2602005-05-14 05:33:54 +00004588 // Check to see if the target has a custom way to lower this. If so, use it.
4589 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4590 default: assert(0 && "This action not implemented for this operation!");
4591 case TargetLowering::Legal:
4592 case TargetLowering::Expand:
4593 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004594 case TargetLowering::Custom: {
4595 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4596 Source), DAG);
4597 if (NV.Val)
4598 return LegalizeOp(NV);
4599 break; // The target decided this was legal after all
4600 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004601 }
4602
Chris Lattner13689e22005-05-12 07:00:44 +00004603 // Expand the source, then glue it back together for the call. We must expand
4604 // the source in case it is shared (this pass of legalize must traverse it).
4605 SDOperand SrcLo, SrcHi;
4606 ExpandOp(Source, SrcLo, SrcHi);
4607 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4608
Evan Cheng56966222007-01-12 02:11:51 +00004609 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004610 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004611 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004612 else {
4613 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004614 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004615 }
Chris Lattner6831a812006-02-13 09:18:02 +00004616
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004617 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004618 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4619 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004620 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4621 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004622}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004623
Chris Lattner22cde6a2006-01-28 08:25:58 +00004624/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4625/// INT_TO_FP operation of the specified operand when the target requests that
4626/// we expand it. At this point, we know that the result and operand types are
4627/// legal for the target.
4628SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4629 SDOperand Op0,
4630 MVT::ValueType DestVT) {
4631 if (Op0.getValueType() == MVT::i32) {
4632 // simple 32-bit [signed|unsigned] integer to float/double expansion
4633
Chris Lattner58092e32007-01-20 22:35:55 +00004634 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004635 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004636 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4637 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004638 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004639 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004640 // get address of 8 byte buffer
4641 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4642 // word offset constant for Hi/Lo address computation
4643 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4644 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004645 SDOperand Hi = StackSlot;
4646 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4647 if (TLI.isLittleEndian())
4648 std::swap(Hi, Lo);
4649
Chris Lattner22cde6a2006-01-28 08:25:58 +00004650 // if signed map to unsigned space
4651 SDOperand Op0Mapped;
4652 if (isSigned) {
4653 // constant used to invert sign bit (signed to unsigned mapping)
4654 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4655 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4656 } else {
4657 Op0Mapped = Op0;
4658 }
4659 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004660 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004661 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004662 // initial hi portion of constructed double
4663 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4664 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004665 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004666 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004667 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004668 // FP constant to bias correct the final result
4669 SDOperand Bias = DAG.getConstantFP(isSigned ?
4670 BitsToDouble(0x4330000080000000ULL)
4671 : BitsToDouble(0x4330000000000000ULL),
4672 MVT::f64);
4673 // subtract the bias
4674 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4675 // final result
4676 SDOperand Result;
4677 // handle final rounding
4678 if (DestVT == MVT::f64) {
4679 // do nothing
4680 Result = Sub;
4681 } else {
4682 // if f32 then cast to f32
4683 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4684 }
4685 return Result;
4686 }
4687 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4688 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4689
4690 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4691 DAG.getConstant(0, Op0.getValueType()),
4692 ISD::SETLT);
4693 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4694 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4695 SignSet, Four, Zero);
4696
4697 // If the sign bit of the integer is set, the large number will be treated
4698 // as a negative number. To counteract this, the dynamic code adds an
4699 // offset depending on the data type.
4700 uint64_t FF;
4701 switch (Op0.getValueType()) {
4702 default: assert(0 && "Unsupported integer type!");
4703 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4704 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4705 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4706 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4707 }
4708 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004709 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004710
4711 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4712 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4713 SDOperand FudgeInReg;
4714 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004715 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004716 else {
4717 assert(DestVT == MVT::f64 && "Unexpected conversion");
4718 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4719 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004720 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004721 }
4722
4723 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4724}
4725
4726/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4727/// *INT_TO_FP operation of the specified operand when the target requests that
4728/// we promote it. At this point, we know that the result and operand types are
4729/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4730/// operation that takes a larger input.
4731SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4732 MVT::ValueType DestVT,
4733 bool isSigned) {
4734 // First step, figure out the appropriate *INT_TO_FP operation to use.
4735 MVT::ValueType NewInTy = LegalOp.getValueType();
4736
4737 unsigned OpToUse = 0;
4738
4739 // Scan for the appropriate larger type to use.
4740 while (1) {
4741 NewInTy = (MVT::ValueType)(NewInTy+1);
4742 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4743
4744 // If the target supports SINT_TO_FP of this type, use it.
4745 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4746 default: break;
4747 case TargetLowering::Legal:
4748 if (!TLI.isTypeLegal(NewInTy))
4749 break; // Can't use this datatype.
4750 // FALL THROUGH.
4751 case TargetLowering::Custom:
4752 OpToUse = ISD::SINT_TO_FP;
4753 break;
4754 }
4755 if (OpToUse) break;
4756 if (isSigned) continue;
4757
4758 // If the target supports UINT_TO_FP of this type, use it.
4759 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4760 default: break;
4761 case TargetLowering::Legal:
4762 if (!TLI.isTypeLegal(NewInTy))
4763 break; // Can't use this datatype.
4764 // FALL THROUGH.
4765 case TargetLowering::Custom:
4766 OpToUse = ISD::UINT_TO_FP;
4767 break;
4768 }
4769 if (OpToUse) break;
4770
4771 // Otherwise, try a larger type.
4772 }
4773
4774 // Okay, we found the operation and type to use. Zero extend our input to the
4775 // desired type then run the operation on it.
4776 return DAG.getNode(OpToUse, DestVT,
4777 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4778 NewInTy, LegalOp));
4779}
4780
4781/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4782/// FP_TO_*INT operation of the specified operand when the target requests that
4783/// we promote it. At this point, we know that the result and operand types are
4784/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4785/// operation that returns a larger result.
4786SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4787 MVT::ValueType DestVT,
4788 bool isSigned) {
4789 // First step, figure out the appropriate FP_TO*INT operation to use.
4790 MVT::ValueType NewOutTy = DestVT;
4791
4792 unsigned OpToUse = 0;
4793
4794 // Scan for the appropriate larger type to use.
4795 while (1) {
4796 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4797 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4798
4799 // If the target supports FP_TO_SINT returning this type, use it.
4800 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4801 default: break;
4802 case TargetLowering::Legal:
4803 if (!TLI.isTypeLegal(NewOutTy))
4804 break; // Can't use this datatype.
4805 // FALL THROUGH.
4806 case TargetLowering::Custom:
4807 OpToUse = ISD::FP_TO_SINT;
4808 break;
4809 }
4810 if (OpToUse) break;
4811
4812 // If the target supports FP_TO_UINT of this type, use it.
4813 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4814 default: break;
4815 case TargetLowering::Legal:
4816 if (!TLI.isTypeLegal(NewOutTy))
4817 break; // Can't use this datatype.
4818 // FALL THROUGH.
4819 case TargetLowering::Custom:
4820 OpToUse = ISD::FP_TO_UINT;
4821 break;
4822 }
4823 if (OpToUse) break;
4824
4825 // Otherwise, try a larger type.
4826 }
4827
4828 // Okay, we found the operation and type to use. Truncate the result of the
4829 // extended FP_TO_*INT operation to the desired size.
4830 return DAG.getNode(ISD::TRUNCATE, DestVT,
4831 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4832}
4833
4834/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4835///
4836SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4837 MVT::ValueType VT = Op.getValueType();
4838 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4839 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4840 switch (VT) {
4841 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4842 case MVT::i16:
4843 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4844 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4845 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4846 case MVT::i32:
4847 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4848 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4849 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4850 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4851 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4852 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4853 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4854 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4855 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4856 case MVT::i64:
4857 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4858 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4859 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4860 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4861 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4862 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4863 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4864 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4865 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4866 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4867 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4868 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4869 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4870 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4871 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4872 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4873 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4874 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4875 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4876 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4877 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4878 }
4879}
4880
4881/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4882///
4883SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4884 switch (Opc) {
4885 default: assert(0 && "Cannot expand this yet!");
4886 case ISD::CTPOP: {
4887 static const uint64_t mask[6] = {
4888 0x5555555555555555ULL, 0x3333333333333333ULL,
4889 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4890 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4891 };
4892 MVT::ValueType VT = Op.getValueType();
4893 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004894 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004895 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4896 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4897 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4898 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4899 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4900 DAG.getNode(ISD::AND, VT,
4901 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4902 }
4903 return Op;
4904 }
4905 case ISD::CTLZ: {
4906 // for now, we do this:
4907 // x = x | (x >> 1);
4908 // x = x | (x >> 2);
4909 // ...
4910 // x = x | (x >>16);
4911 // x = x | (x >>32); // for 64-bit input
4912 // return popcount(~x);
4913 //
4914 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4915 MVT::ValueType VT = Op.getValueType();
4916 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004917 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004918 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4919 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4920 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4921 }
4922 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4923 return DAG.getNode(ISD::CTPOP, VT, Op);
4924 }
4925 case ISD::CTTZ: {
4926 // for now, we use: { return popcount(~x & (x - 1)); }
4927 // unless the target has ctlz but not ctpop, in which case we use:
4928 // { return 32 - nlz(~x & (x-1)); }
4929 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4930 MVT::ValueType VT = Op.getValueType();
4931 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4932 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4933 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4934 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4935 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4936 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4937 TLI.isOperationLegal(ISD::CTLZ, VT))
4938 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004939 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004940 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4941 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4942 }
4943 }
4944}
Chris Lattnere34b3962005-01-19 04:19:40 +00004945
Chris Lattner3e928bb2005-01-07 07:47:09 +00004946/// ExpandOp - Expand the specified SDOperand into its two component pieces
4947/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4948/// LegalizeNodes map is filled in for any results that are not expanded, the
4949/// ExpandedNodes map is filled in for any results that are expanded, and the
4950/// Lo/Hi values are returned.
4951void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4952 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004953 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004954 SDNode *Node = Op.Val;
4955 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004956 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004957 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004958 "Cannot expand to FP value or to larger int value!");
4959
Chris Lattner6fdcb252005-09-02 20:32:45 +00004960 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004961 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004962 = ExpandedNodes.find(Op);
4963 if (I != ExpandedNodes.end()) {
4964 Lo = I->second.first;
4965 Hi = I->second.second;
4966 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004967 }
4968
Chris Lattner3e928bb2005-01-07 07:47:09 +00004969 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004970 case ISD::CopyFromReg:
4971 assert(0 && "CopyFromReg must be legal!");
4972 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004973#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004974 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004975#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004976 assert(0 && "Do not know how to expand this operator!");
4977 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004978 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004979 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004980 Lo = DAG.getNode(ISD::UNDEF, NVT);
4981 Hi = DAG.getNode(ISD::UNDEF, NVT);
4982 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004983 case ISD::Constant: {
4984 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4985 Lo = DAG.getConstant(Cst, NVT);
4986 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4987 break;
4988 }
Evan Cheng00495212006-12-12 21:32:44 +00004989 case ISD::ConstantFP: {
4990 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004991 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004992 if (getTypeAction(Lo.getValueType()) == Expand)
4993 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004994 break;
4995 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004996 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004997 // Return the operands.
4998 Lo = Node->getOperand(0);
4999 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005000 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005001
5002 case ISD::SIGN_EXTEND_INREG:
5003 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005004 // sext_inreg the low part if needed.
5005 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5006
5007 // The high part gets the sign extension from the lo-part. This handles
5008 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005009 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5010 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5011 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005012 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005013
Nate Begemand88fc032006-01-14 03:14:10 +00005014 case ISD::BSWAP: {
5015 ExpandOp(Node->getOperand(0), Lo, Hi);
5016 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5017 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5018 Lo = TempLo;
5019 break;
5020 }
5021
Chris Lattneredb1add2005-05-11 04:51:16 +00005022 case ISD::CTPOP:
5023 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005024 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5025 DAG.getNode(ISD::CTPOP, NVT, Lo),
5026 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005027 Hi = DAG.getConstant(0, NVT);
5028 break;
5029
Chris Lattner39a8f332005-05-12 19:05:01 +00005030 case ISD::CTLZ: {
5031 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005032 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005033 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5034 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005035 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5036 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005037 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5038 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5039
5040 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5041 Hi = DAG.getConstant(0, NVT);
5042 break;
5043 }
5044
5045 case ISD::CTTZ: {
5046 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005047 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005048 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5049 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005050 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5051 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005052 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5053 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5054
5055 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5056 Hi = DAG.getConstant(0, NVT);
5057 break;
5058 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005059
Nate Begemanacc398c2006-01-25 18:21:52 +00005060 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005061 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5062 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005063 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5064 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5065
5066 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005067 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005068 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5069 if (!TLI.isLittleEndian())
5070 std::swap(Lo, Hi);
5071 break;
5072 }
5073
Chris Lattner3e928bb2005-01-07 07:47:09 +00005074 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005075 LoadSDNode *LD = cast<LoadSDNode>(Node);
5076 SDOperand Ch = LD->getChain(); // Legalize the chain.
5077 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5078 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005079 int SVOffset = LD->getSrcValueOffset();
5080 unsigned Alignment = LD->getAlignment();
5081 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005082
Evan Cheng466685d2006-10-09 20:57:25 +00005083 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005084 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5085 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005086 if (VT == MVT::f32 || VT == MVT::f64) {
5087 // f32->i32 or f64->i64 one to one expansion.
5088 // Remember that we legalized the chain.
5089 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005090 // Recursively expand the new load.
5091 if (getTypeAction(NVT) == Expand)
5092 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005093 break;
5094 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005095
Evan Cheng466685d2006-10-09 20:57:25 +00005096 // Increment the pointer to the other half.
5097 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5098 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5099 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005100 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005101 if (Alignment > IncrementSize)
5102 Alignment = IncrementSize;
5103 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5104 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005105
Evan Cheng466685d2006-10-09 20:57:25 +00005106 // Build a factor node to remember that this load is independent of the
5107 // other one.
5108 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5109 Hi.getValue(1));
5110
5111 // Remember that we legalized the chain.
5112 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5113 if (!TLI.isLittleEndian())
5114 std::swap(Lo, Hi);
5115 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005116 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005117
5118 if (VT == MVT::f64 && EVT == MVT::f32) {
5119 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5120 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005121 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005122 // Remember that we legalized the chain.
5123 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5124 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5125 break;
5126 }
Evan Cheng466685d2006-10-09 20:57:25 +00005127
5128 if (EVT == NVT)
5129 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005130 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005131 else
5132 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005133 SVOffset, EVT, isVolatile,
5134 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005135
5136 // Remember that we legalized the chain.
5137 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5138
5139 if (ExtType == ISD::SEXTLOAD) {
5140 // The high part is obtained by SRA'ing all but one of the bits of the
5141 // lo part.
5142 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5143 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5144 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5145 } else if (ExtType == ISD::ZEXTLOAD) {
5146 // The high part is just a zero.
5147 Hi = DAG.getConstant(0, NVT);
5148 } else /* if (ExtType == ISD::EXTLOAD) */ {
5149 // The high part is undefined.
5150 Hi = DAG.getNode(ISD::UNDEF, NVT);
5151 }
5152 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005153 break;
5154 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005155 case ISD::AND:
5156 case ISD::OR:
5157 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5158 SDOperand LL, LH, RL, RH;
5159 ExpandOp(Node->getOperand(0), LL, LH);
5160 ExpandOp(Node->getOperand(1), RL, RH);
5161 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5162 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5163 break;
5164 }
5165 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005166 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005167 ExpandOp(Node->getOperand(1), LL, LH);
5168 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005169 if (getTypeAction(NVT) == Expand)
5170 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005171 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005172 if (VT != MVT::f32)
5173 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005174 break;
5175 }
Nate Begeman9373a812005-08-10 20:51:12 +00005176 case ISD::SELECT_CC: {
5177 SDOperand TL, TH, FL, FH;
5178 ExpandOp(Node->getOperand(2), TL, TH);
5179 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005180 if (getTypeAction(NVT) == Expand)
5181 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005182 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5183 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005184 if (VT != MVT::f32)
5185 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5186 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005187 break;
5188 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005189 case ISD::ANY_EXTEND:
5190 // The low part is any extension of the input (which degenerates to a copy).
5191 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5192 // The high part is undefined.
5193 Hi = DAG.getNode(ISD::UNDEF, NVT);
5194 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005195 case ISD::SIGN_EXTEND: {
5196 // The low part is just a sign extension of the input (which degenerates to
5197 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005198 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005199
Chris Lattner3e928bb2005-01-07 07:47:09 +00005200 // The high part is obtained by SRA'ing all but one of the bits of the lo
5201 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005202 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005203 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5204 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005205 break;
5206 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005207 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005208 // The low part is just a zero extension of the input (which degenerates to
5209 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005210 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005211
Chris Lattner3e928bb2005-01-07 07:47:09 +00005212 // The high part is just a zero.
5213 Hi = DAG.getConstant(0, NVT);
5214 break;
Chris Lattner35481892005-12-23 00:16:34 +00005215
Chris Lattner4c948eb2007-02-13 23:55:16 +00005216 case ISD::TRUNCATE: {
5217 // The input value must be larger than this value. Expand *it*.
5218 SDOperand NewLo;
5219 ExpandOp(Node->getOperand(0), NewLo, Hi);
5220
5221 // The low part is now either the right size, or it is closer. If not the
5222 // right size, make an illegal truncate so we recursively expand it.
5223 if (NewLo.getValueType() != Node->getValueType(0))
5224 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5225 ExpandOp(NewLo, Lo, Hi);
5226 break;
5227 }
5228
Chris Lattner35481892005-12-23 00:16:34 +00005229 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005230 SDOperand Tmp;
5231 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5232 // If the target wants to, allow it to lower this itself.
5233 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5234 case Expand: assert(0 && "cannot expand FP!");
5235 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5236 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5237 }
5238 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5239 }
5240
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005241 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005242 if (VT == MVT::f32 || VT == MVT::f64) {
5243 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005244 if (getTypeAction(NVT) == Expand)
5245 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005246 break;
5247 }
5248
5249 // If source operand will be expanded to the same type as VT, i.e.
5250 // i64 <- f64, i32 <- f32, expand the source operand instead.
5251 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5252 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5253 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005254 break;
5255 }
5256
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005257 // Turn this into a load/store pair by default.
5258 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005259 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005260
Chris Lattner35481892005-12-23 00:16:34 +00005261 ExpandOp(Tmp, Lo, Hi);
5262 break;
5263 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005264
Chris Lattner8137c9e2006-01-28 05:07:51 +00005265 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005266 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5267 TargetLowering::Custom &&
5268 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005269 Lo = TLI.LowerOperation(Op, DAG);
5270 assert(Lo.Val && "Node must be custom expanded!");
5271 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005272 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005273 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005274 break;
5275
Chris Lattner4e6c7462005-01-08 19:27:05 +00005276 // These operators cannot be expanded directly, emit them as calls to
5277 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005278 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005279 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005280 SDOperand Op;
5281 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5282 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005283 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5284 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005285 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005286
Chris Lattnerf20d1832005-07-30 01:40:57 +00005287 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5288
Chris Lattner80a3e942005-07-29 00:33:32 +00005289 // Now that the custom expander is done, expand the result, which is still
5290 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005291 if (Op.Val) {
5292 ExpandOp(Op, Lo, Hi);
5293 break;
5294 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005295 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005296
Evan Cheng56966222007-01-12 02:11:51 +00005297 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005298 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005299 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005300 else
Evan Cheng56966222007-01-12 02:11:51 +00005301 LC = RTLIB::FPTOSINT_F64_I64;
5302 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5303 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005304 break;
Evan Cheng56966222007-01-12 02:11:51 +00005305 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005306
Evan Cheng56966222007-01-12 02:11:51 +00005307 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005308 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005309 SDOperand Op;
5310 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5311 case Expand: assert(0 && "cannot expand FP!");
5312 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5313 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5314 }
5315
5316 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5317
5318 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005319 if (Op.Val) {
5320 ExpandOp(Op, Lo, Hi);
5321 break;
5322 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005323 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005324
Evan Cheng56966222007-01-12 02:11:51 +00005325 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005326 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005327 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005328 else
Evan Cheng56966222007-01-12 02:11:51 +00005329 LC = RTLIB::FPTOUINT_F64_I64;
5330 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5331 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005332 break;
Evan Cheng56966222007-01-12 02:11:51 +00005333 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005334
Evan Cheng05a2d562006-01-09 18:31:59 +00005335 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005336 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005337 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005338 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005339 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005340 Op = TLI.LowerOperation(Op, DAG);
5341 if (Op.Val) {
5342 // Now that the custom expander is done, expand the result, which is
5343 // still VT.
5344 ExpandOp(Op, Lo, Hi);
5345 break;
5346 }
5347 }
5348
Chris Lattner79980b02006-09-13 03:50:39 +00005349 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5350 // this X << 1 as X+X.
5351 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5352 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5353 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5354 SDOperand LoOps[2], HiOps[3];
5355 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5356 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5357 LoOps[1] = LoOps[0];
5358 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5359
5360 HiOps[1] = HiOps[0];
5361 HiOps[2] = Lo.getValue(1);
5362 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5363 break;
5364 }
5365 }
5366
Chris Lattnere34b3962005-01-19 04:19:40 +00005367 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005368 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005369 break;
Chris Lattner47599822005-04-02 03:38:53 +00005370
5371 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005372 TargetLowering::LegalizeAction Action =
5373 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5374 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5375 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005376 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005377 break;
5378 }
5379
Chris Lattnere34b3962005-01-19 04:19:40 +00005380 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005381 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5382 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005383 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005384 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005385
Evan Cheng05a2d562006-01-09 18:31:59 +00005386 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005387 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005388 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005389 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005390 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005391 Op = TLI.LowerOperation(Op, DAG);
5392 if (Op.Val) {
5393 // Now that the custom expander is done, expand the result, which is
5394 // still VT.
5395 ExpandOp(Op, Lo, Hi);
5396 break;
5397 }
5398 }
5399
Chris Lattnere34b3962005-01-19 04:19:40 +00005400 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005401 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005402 break;
Chris Lattner47599822005-04-02 03:38:53 +00005403
5404 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005405 TargetLowering::LegalizeAction Action =
5406 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5407 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5408 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005409 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005410 break;
5411 }
5412
Chris Lattnere34b3962005-01-19 04:19:40 +00005413 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005414 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5415 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005416 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005417 }
5418
5419 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005420 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005421 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005422 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005423 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005424 Op = TLI.LowerOperation(Op, DAG);
5425 if (Op.Val) {
5426 // Now that the custom expander is done, expand the result, which is
5427 // still VT.
5428 ExpandOp(Op, Lo, Hi);
5429 break;
5430 }
5431 }
5432
Chris Lattnere34b3962005-01-19 04:19:40 +00005433 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005434 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005435 break;
Chris Lattner47599822005-04-02 03:38:53 +00005436
5437 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005438 TargetLowering::LegalizeAction Action =
5439 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5440 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5441 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005442 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005443 break;
5444 }
5445
Chris Lattnere34b3962005-01-19 04:19:40 +00005446 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005447 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5448 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005449 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005450 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005451
Misha Brukmanedf128a2005-04-21 22:36:52 +00005452 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005453 case ISD::SUB: {
5454 // If the target wants to custom expand this, let them.
5455 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5456 TargetLowering::Custom) {
5457 Op = TLI.LowerOperation(Op, DAG);
5458 if (Op.Val) {
5459 ExpandOp(Op, Lo, Hi);
5460 break;
5461 }
5462 }
5463
5464 // Expand the subcomponents.
5465 SDOperand LHSL, LHSH, RHSL, RHSH;
5466 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5467 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005468 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5469 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005470 LoOps[0] = LHSL;
5471 LoOps[1] = RHSL;
5472 HiOps[0] = LHSH;
5473 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005474 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005475 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005476 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005477 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005478 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005479 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005480 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005481 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005482 }
Chris Lattner84f67882005-01-20 18:52:28 +00005483 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005484 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005485
5486 case ISD::ADDC:
5487 case ISD::SUBC: {
5488 // Expand the subcomponents.
5489 SDOperand LHSL, LHSH, RHSL, RHSH;
5490 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5491 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5492 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5493 SDOperand LoOps[2] = { LHSL, RHSL };
5494 SDOperand HiOps[3] = { LHSH, RHSH };
5495
5496 if (Node->getOpcode() == ISD::ADDC) {
5497 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5498 HiOps[2] = Lo.getValue(1);
5499 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5500 } else {
5501 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5502 HiOps[2] = Lo.getValue(1);
5503 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5504 }
5505 // Remember that we legalized the flag.
5506 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5507 break;
5508 }
5509 case ISD::ADDE:
5510 case ISD::SUBE: {
5511 // Expand the subcomponents.
5512 SDOperand LHSL, LHSH, RHSL, RHSH;
5513 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5514 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5515 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5516 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5517 SDOperand HiOps[3] = { LHSH, RHSH };
5518
5519 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5520 HiOps[2] = Lo.getValue(1);
5521 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5522
5523 // Remember that we legalized the flag.
5524 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5525 break;
5526 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005527 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005528 // If the target wants to custom expand this, let them.
5529 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005530 SDOperand New = TLI.LowerOperation(Op, DAG);
5531 if (New.Val) {
5532 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005533 break;
5534 }
5535 }
5536
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005537 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5538 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005539 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005540 SDOperand LL, LH, RL, RH;
5541 ExpandOp(Node->getOperand(0), LL, LH);
5542 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005543 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005544 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005545 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5546 // extended the sign bit of the low half through the upper half, and if so
5547 // emit a MULHS instead of the alternate sequence that is valid for any
5548 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005549 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005550 // is RH an extension of the sign bit of RL?
5551 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5552 RH.getOperand(1).getOpcode() == ISD::Constant &&
5553 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5554 // is LH an extension of the sign bit of LL?
5555 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5556 LH.getOperand(1).getOpcode() == ISD::Constant &&
5557 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005558 // Low part:
5559 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5560 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005561 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005562 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005563 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005564 // Low part:
5565 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5566
5567 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005568 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5569 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5570 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5571 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5572 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005573 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005574 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005575 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005576
Evan Cheng56966222007-01-12 02:11:51 +00005577 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5578 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005579 break;
5580 }
Evan Cheng56966222007-01-12 02:11:51 +00005581 case ISD::SDIV:
5582 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5583 break;
5584 case ISD::UDIV:
5585 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5586 break;
5587 case ISD::SREM:
5588 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5589 break;
5590 case ISD::UREM:
5591 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5592 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005593
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005594 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005595 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5596 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5597 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005598 break;
5599 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005600 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5601 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5602 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005603 break;
5604 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005605 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5606 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5607 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005608 break;
5609 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005610 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5611 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5612 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005613 break;
5614 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005615 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005616 break;
5617 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005618 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005619 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005620 case ISD::FPOWI:
5621 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5622 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5623 Node, false, Hi);
5624 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005625 case ISD::FSQRT:
5626 case ISD::FSIN:
5627 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005628 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005629 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005630 case ISD::FSQRT:
5631 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5632 break;
5633 case ISD::FSIN:
5634 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5635 break;
5636 case ISD::FCOS:
5637 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5638 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005639 default: assert(0 && "Unreachable!");
5640 }
Evan Cheng56966222007-01-12 02:11:51 +00005641 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005642 break;
5643 }
Evan Cheng966bf242006-12-16 00:52:40 +00005644 case ISD::FABS: {
5645 SDOperand Mask = (VT == MVT::f64)
5646 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5647 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5648 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5649 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5650 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5651 if (getTypeAction(NVT) == Expand)
5652 ExpandOp(Lo, Lo, Hi);
5653 break;
5654 }
5655 case ISD::FNEG: {
5656 SDOperand Mask = (VT == MVT::f64)
5657 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5658 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5659 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5660 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5661 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5662 if (getTypeAction(NVT) == Expand)
5663 ExpandOp(Lo, Lo, Hi);
5664 break;
5665 }
Evan Cheng912095b2007-01-04 21:56:39 +00005666 case ISD::FCOPYSIGN: {
5667 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5668 if (getTypeAction(NVT) == Expand)
5669 ExpandOp(Lo, Lo, Hi);
5670 break;
5671 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005672 case ISD::SINT_TO_FP:
5673 case ISD::UINT_TO_FP: {
5674 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5675 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005676 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005677 if (Node->getOperand(0).getValueType() == MVT::i64) {
5678 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005679 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005680 else
Evan Cheng56966222007-01-12 02:11:51 +00005681 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005682 } else {
5683 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005684 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005685 else
Evan Cheng56966222007-01-12 02:11:51 +00005686 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005687 }
5688
5689 // Promote the operand if needed.
5690 if (getTypeAction(SrcVT) == Promote) {
5691 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5692 Tmp = isSigned
5693 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5694 DAG.getValueType(SrcVT))
5695 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5696 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5697 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005698
5699 const char *LibCall = TLI.getLibcallName(LC);
5700 if (LibCall)
5701 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5702 else {
5703 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5704 Node->getOperand(0));
5705 if (getTypeAction(Lo.getValueType()) == Expand)
5706 ExpandOp(Lo, Lo, Hi);
5707 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005708 break;
5709 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005710 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005711
Chris Lattner83397362005-12-21 18:02:52 +00005712 // Make sure the resultant values have been legalized themselves, unless this
5713 // is a type that requires multi-step expansion.
5714 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5715 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005716 if (Hi.Val)
5717 // Don't legalize the high part if it is expanded to a single node.
5718 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005719 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005720
5721 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005722 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005723 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005724}
5725
Dan Gohman7f321562007-06-25 16:23:39 +00005726/// SplitVectorOp - Given an operand of vector type, break it down into
5727/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005728void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5729 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005730 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005731 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005732 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005733 assert(NumElements > 1 && "Cannot split a single element vector!");
5734 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005735 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5736 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005737
5738 // See if we already split it.
5739 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5740 = SplitNodes.find(Op);
5741 if (I != SplitNodes.end()) {
5742 Lo = I->second.first;
5743 Hi = I->second.second;
5744 return;
5745 }
5746
5747 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005748 default:
5749#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005750 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005751#endif
5752 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005753 case ISD::BUILD_PAIR:
5754 Lo = Node->getOperand(0);
5755 Hi = Node->getOperand(1);
5756 break;
5757 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005758 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5759 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005760 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005761
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005762 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005763 Node->op_end());
5764 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005765 break;
5766 }
Dan Gohman7f321562007-06-25 16:23:39 +00005767 case ISD::CONCAT_VECTORS: {
5768 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5769 if (NewNumSubvectors == 1) {
5770 Lo = Node->getOperand(0);
5771 Hi = Node->getOperand(1);
5772 } else {
5773 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5774 Node->op_begin()+NewNumSubvectors);
5775 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005776
Dan Gohman7f321562007-06-25 16:23:39 +00005777 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5778 Node->op_end());
5779 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5780 }
Dan Gohman65956352007-06-13 15:12:02 +00005781 break;
5782 }
Dan Gohman7f321562007-06-25 16:23:39 +00005783 case ISD::ADD:
5784 case ISD::SUB:
5785 case ISD::MUL:
5786 case ISD::FADD:
5787 case ISD::FSUB:
5788 case ISD::FMUL:
5789 case ISD::SDIV:
5790 case ISD::UDIV:
5791 case ISD::FDIV:
5792 case ISD::AND:
5793 case ISD::OR:
5794 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005795 SDOperand LL, LH, RL, RH;
5796 SplitVectorOp(Node->getOperand(0), LL, LH);
5797 SplitVectorOp(Node->getOperand(1), RL, RH);
5798
Dan Gohman7f321562007-06-25 16:23:39 +00005799 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5800 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005801 break;
5802 }
Dan Gohman7f321562007-06-25 16:23:39 +00005803 case ISD::LOAD: {
5804 LoadSDNode *LD = cast<LoadSDNode>(Node);
5805 SDOperand Ch = LD->getChain();
5806 SDOperand Ptr = LD->getBasePtr();
5807 const Value *SV = LD->getSrcValue();
5808 int SVOffset = LD->getSrcValueOffset();
5809 unsigned Alignment = LD->getAlignment();
5810 bool isVolatile = LD->isVolatile();
5811
5812 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5813 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005814 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5815 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005816 SVOffset += IncrementSize;
5817 if (Alignment > IncrementSize)
5818 Alignment = IncrementSize;
5819 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005820
5821 // Build a factor node to remember that this load is independent of the
5822 // other one.
5823 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5824 Hi.getValue(1));
5825
5826 // Remember that we legalized the chain.
5827 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005828 break;
5829 }
Dan Gohman7f321562007-06-25 16:23:39 +00005830 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005831 // We know the result is a vector. The input may be either a vector or a
5832 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005833 SDOperand InOp = Node->getOperand(0);
5834 if (!MVT::isVector(InOp.getValueType()) ||
5835 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5836 // The input is a scalar or single-element vector.
5837 // Lower to a store/load so that it can be split.
5838 // FIXME: this could be improved probably.
5839 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005840
Evan Cheng786225a2006-10-05 23:01:46 +00005841 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005842 InOp, Ptr, NULL, 0);
5843 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005844 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005845 // Split the vector and convert each of the pieces now.
5846 SplitVectorOp(InOp, Lo, Hi);
5847 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5848 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5849 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005850 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005851 }
5852
5853 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005854 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005855 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005856 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005857}
5858
5859
Dan Gohman89b20c02007-06-27 14:06:22 +00005860/// ScalarizeVectorOp - Given an operand of single-element vector type
5861/// (e.g. v1f32), convert it into the equivalent operation that returns a
5862/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005863SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5864 assert(MVT::isVector(Op.getValueType()) &&
5865 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005866 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005867 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5868 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005869
Dan Gohman7f321562007-06-25 16:23:39 +00005870 // See if we already scalarized it.
5871 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5872 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005873
5874 SDOperand Result;
5875 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005876 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005877#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005878 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005879#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005880 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5881 case ISD::ADD:
5882 case ISD::FADD:
5883 case ISD::SUB:
5884 case ISD::FSUB:
5885 case ISD::MUL:
5886 case ISD::FMUL:
5887 case ISD::SDIV:
5888 case ISD::UDIV:
5889 case ISD::FDIV:
5890 case ISD::SREM:
5891 case ISD::UREM:
5892 case ISD::FREM:
5893 case ISD::AND:
5894 case ISD::OR:
5895 case ISD::XOR:
5896 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005897 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005898 ScalarizeVectorOp(Node->getOperand(0)),
5899 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005900 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005901 case ISD::FNEG:
5902 case ISD::FABS:
5903 case ISD::FSQRT:
5904 case ISD::FSIN:
5905 case ISD::FCOS:
5906 Result = DAG.getNode(Node->getOpcode(),
5907 NewVT,
5908 ScalarizeVectorOp(Node->getOperand(0)));
5909 break;
5910 case ISD::LOAD: {
5911 LoadSDNode *LD = cast<LoadSDNode>(Node);
5912 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5913 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005914
Dan Gohman7f321562007-06-25 16:23:39 +00005915 const Value *SV = LD->getSrcValue();
5916 int SVOffset = LD->getSrcValueOffset();
5917 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5918 LD->isVolatile(), LD->getAlignment());
5919
Chris Lattnerc7029802006-03-18 01:44:44 +00005920 // Remember that we legalized the chain.
5921 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5922 break;
5923 }
Dan Gohman7f321562007-06-25 16:23:39 +00005924 case ISD::BUILD_VECTOR:
5925 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005926 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005927 case ISD::INSERT_VECTOR_ELT:
5928 // Returning the inserted scalar element.
5929 Result = Node->getOperand(1);
5930 break;
5931 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005932 assert(Node->getOperand(0).getValueType() == NewVT &&
5933 "Concat of non-legal vectors not yet supported!");
5934 Result = Node->getOperand(0);
5935 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005936 case ISD::VECTOR_SHUFFLE: {
5937 // Figure out if the scalar is the LHS or RHS and return it.
5938 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5939 if (cast<ConstantSDNode>(EltNum)->getValue())
5940 Result = ScalarizeVectorOp(Node->getOperand(1));
5941 else
5942 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005943 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005944 }
5945 case ISD::EXTRACT_SUBVECTOR:
5946 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005947 assert(Result.getValueType() == NewVT);
5948 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005949 case ISD::BIT_CONVERT:
5950 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005951 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005952 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005953 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005954 ScalarizeVectorOp(Op.getOperand(1)),
5955 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005956 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005957 }
5958
5959 if (TLI.isTypeLegal(NewVT))
5960 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005961 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5962 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005963 return Result;
5964}
5965
Chris Lattner3e928bb2005-01-07 07:47:09 +00005966
5967// SelectionDAG::Legalize - This is the entry point for the file.
5968//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005969void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005970 if (ViewLegalizeDAGs) viewGraph();
5971
Chris Lattner3e928bb2005-01-07 07:47:09 +00005972 /// run - This is the main entry point to this class.
5973 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005974 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005975}
5976