blob: d6685e646603a2afeac0102f3dc8c0b27350bd62 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000019#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000021#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000024#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000025#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000026#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000032#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000033#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000034using namespace llvm;
35
Chris Lattner5e46a192006-04-02 03:07:27 +000036#ifndef NDEBUG
37static cl::opt<bool>
38ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
39 cl::desc("Pop up a window to show dags before legalize"));
40#else
41static const bool ViewLegalizeDAGs = 0;
42#endif
43
Chris Lattner3e928bb2005-01-07 07:47:09 +000044//===----------------------------------------------------------------------===//
45/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
46/// hacks on it until the target machine can handle it. This involves
47/// eliminating value sizes the machine cannot handle (promoting small sizes to
48/// large sizes or splitting up large values into small values) as well as
49/// eliminating operations the machine cannot handle.
50///
51/// This code also does a small amount of optimization and recognition of idioms
52/// as part of its processing. For example, if a target does not support a
53/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
54/// will attempt merge setcc and brc instructions into brcc's.
55///
56namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000057class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000058 TargetLowering &TLI;
59 SelectionDAG &DAG;
60
Chris Lattner6831a812006-02-13 09:18:02 +000061 // Libcall insertion helpers.
62
63 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
64 /// legalized. We use this to ensure that calls are properly serialized
65 /// against each other, including inserted libcalls.
66 SDOperand LastCALLSEQ_END;
67
68 /// IsLegalizingCall - This member is used *only* for purposes of providing
69 /// helpful assertions that a libcall isn't created while another call is
70 /// being legalized (which could lead to non-serialized call sequences).
71 bool IsLegalizingCall;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000074 Legal, // The target natively supports this operation.
75 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000076 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 };
Chris Lattner6831a812006-02-13 09:18:02 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 /// ValueTypeActions - This is a bitvector that contains two bits for each
80 /// value type, where the two bits correspond to the LegalizeAction enum.
81 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000082 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000083
Chris Lattner3e928bb2005-01-07 07:47:09 +000084 /// LegalizedNodes - For nodes that are of legal width, and that have more
85 /// than one use, this map indicates what regularized operand to use. This
86 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000087 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000088
Chris Lattner03c85462005-01-15 05:21:40 +000089 /// PromotedNodes - For nodes that are below legal width, and that have more
90 /// than one use, this map indicates what promoted value to use. This allows
91 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000092 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000093
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// ExpandedNodes - For nodes that need to be expanded this map indicates
95 /// which which operands are the expanded version of the input. This allows
96 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000097 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000098
Chris Lattnerc7029802006-03-18 01:44:44 +000099 /// SplitNodes - For vector nodes that need to be split, this map indicates
100 /// which which operands are the split version of the input. This allows us
101 /// to avoid splitting the same node more than once.
102 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
103
Dan Gohman7f321562007-06-25 16:23:39 +0000104 /// ScalarizedNodes - For nodes that need to be converted from vector types to
105 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000106 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000107 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000108
Chris Lattner8afc48e2005-01-07 22:28:47 +0000109 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000114 }
Chris Lattner03c85462005-01-15 05:21:40 +0000115 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000117 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000120 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122public:
123
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000124 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126 /// getTypeAction - Return how we should legalize values of this type, either
127 /// it is already legal or we need to expand it into multiple registers of
128 /// smaller integer type, or we need to promote it to a larger type.
129 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000130 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131 }
132
133 /// isTypeLegal - Return true if this type is legal on this target.
134 ///
135 bool isTypeLegal(MVT::ValueType VT) const {
136 return getTypeAction(VT) == Legal;
137 }
138
Chris Lattner3e928bb2005-01-07 07:47:09 +0000139 void LegalizeDAG();
140
Chris Lattner456a93a2006-01-28 07:39:30 +0000141private:
Dan Gohman7f321562007-06-25 16:23:39 +0000142 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000143 /// appropriate for its type.
144 void HandleOp(SDOperand Op);
145
146 /// LegalizeOp - We know that the specified value has a legal type.
147 /// Recursively ensure that the operands have legal types, then return the
148 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000150
Dan Gohman82669522007-10-11 23:57:53 +0000151 /// UnrollVectorOp - We know that the given vector has a legal type, however
152 /// the operation it performs is not legal and is an operation that we have
153 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
154 /// operating on each element individually.
155 SDOperand UnrollVectorOp(SDOperand O);
156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000162 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000163
Chris Lattnerc7029802006-03-18 01:44:44 +0000164 /// ExpandOp - Expand the specified SDOperand into its two component pieces
165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
170 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
171
Dan Gohman7f321562007-06-25 16:23:39 +0000172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000174 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
Dan Gohman89b20c02007-06-27 14:06:22 +0000176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000179 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000180
Chris Lattner4352cc92006-04-04 17:23:26 +0000181 /// isShuffleLegal - Return true if a vector shuffle is legal with the
182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
190 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
191
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000194
Nate Begeman750ac1b2006-02-01 07:19:44 +0000195 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
196
Reid Spencer47857812006-12-31 05:55:36 +0000197 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000198 SDOperand &Hi);
199 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
200 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000201
Chris Lattner35481892005-12-23 00:16:34 +0000202 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000203 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000204 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000205 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
206 SDOperand LegalOp,
207 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000208 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
209 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000210 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000212
Chris Lattner456a93a2006-01-28 07:39:30 +0000213 SDOperand ExpandBSWAP(SDOperand Op);
214 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000215 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
216 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000217 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000219
Dan Gohman7f321562007-06-25 16:23:39 +0000220 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000221 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000222
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223 SDOperand getIntPtrConstant(uint64_t Val) {
224 return DAG.getConstant(Val, TLI.getPointerTy());
225 }
226};
227}
228
Chris Lattner4352cc92006-04-04 17:23:26 +0000229/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
230/// specified mask and type. Targets can specify exactly which masks they
231/// support and the code generator is tasked with not creating illegal masks.
232///
233/// Note that this will also return true for shuffles that are promoted to a
234/// different type.
235SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
236 SDOperand Mask) const {
237 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
238 default: return 0;
239 case TargetLowering::Legal:
240 case TargetLowering::Custom:
241 break;
242 case TargetLowering::Promote: {
243 // If this is promoted to a different type, convert the shuffle mask and
244 // ask if it is legal in the promoted type!
245 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
246
247 // If we changed # elements, change the shuffle mask.
248 unsigned NumEltsGrowth =
249 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
250 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
251 if (NumEltsGrowth > 1) {
252 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000253 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000254 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
255 SDOperand InOp = Mask.getOperand(i);
256 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
257 if (InOp.getOpcode() == ISD::UNDEF)
258 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
259 else {
260 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
261 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
262 }
263 }
264 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000265 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000266 }
267 VT = NVT;
268 break;
269 }
270 }
271 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
272}
273
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000274SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
275 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
276 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000277 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000278 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279}
280
Chris Lattnere10e6f72007-06-18 21:28:10 +0000281/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
282/// contains all of a nodes operands before it contains the node.
283static void ComputeTopDownOrdering(SelectionDAG &DAG,
284 SmallVector<SDNode*, 64> &Order) {
285
286 DenseMap<SDNode*, unsigned> Visited;
287 std::vector<SDNode*> Worklist;
288 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000289
Chris Lattnere10e6f72007-06-18 21:28:10 +0000290 // Compute ordering from all of the leaves in the graphs, those (like the
291 // entry node) that have no operands.
292 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
293 E = DAG.allnodes_end(); I != E; ++I) {
294 if (I->getNumOperands() == 0) {
295 Visited[I] = 0 - 1U;
296 Worklist.push_back(I);
297 }
Chris Lattner32fca002005-10-06 01:20:27 +0000298 }
299
Chris Lattnere10e6f72007-06-18 21:28:10 +0000300 while (!Worklist.empty()) {
301 SDNode *N = Worklist.back();
302 Worklist.pop_back();
303
304 if (++Visited[N] != N->getNumOperands())
305 continue; // Haven't visited all operands yet
306
307 Order.push_back(N);
308
309 // Now that we have N in, add anything that uses it if all of their operands
310 // are now done.
311 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
312 UI != E; ++UI)
313 Worklist.push_back(*UI);
314 }
315
316 assert(Order.size() == Visited.size() &&
317 Order.size() ==
318 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
319 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000320}
321
Chris Lattner1618beb2005-07-29 00:11:56 +0000322
Chris Lattner3e928bb2005-01-07 07:47:09 +0000323void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000324 LastCALLSEQ_END = DAG.getEntryNode();
325 IsLegalizingCall = false;
326
Chris Lattnerab510a72005-10-02 17:49:46 +0000327 // The legalize process is inherently a bottom-up recursive process (users
328 // legalize their uses before themselves). Given infinite stack space, we
329 // could just start legalizing on the root and traverse the whole graph. In
330 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000331 // blocks. To avoid this problem, compute an ordering of the nodes where each
332 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000333 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000334 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000335
Chris Lattnerc7029802006-03-18 01:44:44 +0000336 for (unsigned i = 0, e = Order.size(); i != e; ++i)
337 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000338
339 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000340 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000341 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
342 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343
344 ExpandedNodes.clear();
345 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000346 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000347 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000348 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349
350 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000351 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352}
353
Chris Lattner6831a812006-02-13 09:18:02 +0000354
355/// FindCallEndFromCallStart - Given a chained node that is part of a call
356/// sequence, find the CALLSEQ_END node that terminates the call sequence.
357static SDNode *FindCallEndFromCallStart(SDNode *Node) {
358 if (Node->getOpcode() == ISD::CALLSEQ_END)
359 return Node;
360 if (Node->use_empty())
361 return 0; // No CallSeqEnd
362
363 // The chain is usually at the end.
364 SDOperand TheChain(Node, Node->getNumValues()-1);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Sometimes it's at the beginning.
367 TheChain = SDOperand(Node, 0);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Otherwise, hunt for it.
370 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
371 if (Node->getValueType(i) == MVT::Other) {
372 TheChain = SDOperand(Node, i);
373 break;
374 }
375
376 // Otherwise, we walked into a node without a chain.
377 if (TheChain.getValueType() != MVT::Other)
378 return 0;
379 }
380 }
381
382 for (SDNode::use_iterator UI = Node->use_begin(),
383 E = Node->use_end(); UI != E; ++UI) {
384
385 // Make sure to only follow users of our token chain.
386 SDNode *User = *UI;
387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
388 if (User->getOperand(i) == TheChain)
389 if (SDNode *Result = FindCallEndFromCallStart(User))
390 return Result;
391 }
392 return 0;
393}
394
395/// FindCallStartFromCallEnd - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_START node that initiates the call sequence.
397static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
398 assert(Node && "Didn't find callseq_start for a call??");
399 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
400
401 assert(Node->getOperand(0).getValueType() == MVT::Other &&
402 "Node doesn't have a token chain argument!");
403 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
404}
405
406/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
407/// see if any uses can reach Dest. If no dest operands can get to dest,
408/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000409///
410/// Keep track of the nodes we fine that actually do lead to Dest in
411/// NodesLeadingTo. This avoids retraversing them exponential number of times.
412///
413bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000414 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000415 if (N == Dest) return true; // N certainly leads to Dest :)
416
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000417 // If we've already processed this node and it does lead to Dest, there is no
418 // need to reprocess it.
419 if (NodesLeadingTo.count(N)) return true;
420
Chris Lattner6831a812006-02-13 09:18:02 +0000421 // If the first result of this node has been already legalized, then it cannot
422 // reach N.
423 switch (getTypeAction(N->getValueType(0))) {
424 case Legal:
425 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 case Promote:
428 if (PromotedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Expand:
431 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 }
434
435 // Okay, this node has not already been legalized. Check and legalize all
436 // operands. If none lead to Dest, then we can legalize this node.
437 bool OperandsLeadToDest = false;
438 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
439 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000440 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000441
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000442 if (OperandsLeadToDest) {
443 NodesLeadingTo.insert(N);
444 return true;
445 }
Chris Lattner6831a812006-02-13 09:18:02 +0000446
447 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000448 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000449 return false;
450}
451
Dan Gohman7f321562007-06-25 16:23:39 +0000452/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000455 MVT::ValueType VT = Op.getValueType();
456 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000457 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000458 case Legal: (void)LegalizeOp(Op); break;
459 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000460 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000461 if (!MVT::isVector(VT)) {
462 // If this is an illegal scalar, expand it into its two component
463 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000464 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000465 if (Op.getOpcode() == ISD::TargetConstant)
466 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000467 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000468 } else if (MVT::getVectorNumElements(VT) == 1) {
469 // If this is an illegal single element vector, convert it to a
470 // scalar operation.
471 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000472 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000473 // Otherwise, this is an illegal multiple element vector.
474 // Split it in half and legalize both parts.
475 SDOperand X, Y;
476 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000477 }
478 break;
479 }
480}
Chris Lattner6831a812006-02-13 09:18:02 +0000481
Evan Cheng9f877882006-12-13 20:57:08 +0000482/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
483/// a load from the constant pool.
484static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000485 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000486 bool Extend = false;
487
488 // If a FP immediate is precise when represented as a float and if the
489 // target can do an extending load from float to double, we put it into
490 // the constant pool as a float, even if it's is statically typed as a
491 // double.
492 MVT::ValueType VT = CFP->getValueType(0);
493 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000494 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000495 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000496 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
500 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000501 }
502
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000503 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000504 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000505 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000506 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
507 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
508 VT = MVT::f32;
509 Extend = true;
510 }
511
512 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
513 if (Extend) {
514 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
515 CPIdx, NULL, 0, MVT::f32);
516 } else {
517 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
518 }
519}
520
Chris Lattner6831a812006-02-13 09:18:02 +0000521
Evan Cheng912095b2007-01-04 21:56:39 +0000522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000527 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000532
Evan Cheng912095b2007-01-04 21:56:39 +0000533 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
572 if (MVT::isFloatingPoint(ST->getStoredVT())) {
573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
587 assert(MVT::isInteger(ST->getStoredVT()) &&
588 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000589 // Get the half-size VT
590 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000622 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000623 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000627 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000628 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000629 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
Chris Lattnere400af82007-11-19 21:38:03 +0000645 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
646 "Unaligned load of unsupported type.");
647
648 // Compute the new VT that is half the size of the old one. We either have an
649 // integer MVT or we have a vector MVT.
650 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
651 MVT::ValueType NewLoadedVT;
652 if (!MVT::isVector(LoadedVT)) {
653 NewLoadedVT = MVT::getIntegerType(NumBits/2);
654 } else {
655 // FIXME: This is not right for <1 x anything> it is also not right for
656 // non-power-of-two vectors.
657 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
658 MVT::getVectorNumElements(LoadedVT)/2);
659 }
660 NumBits >>= 1;
661
662 unsigned Alignment = LD->getAlignment();
663 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000664 ISD::LoadExtType HiExtType = LD->getExtensionType();
665
666 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
667 if (HiExtType == ISD::NON_EXTLOAD)
668 HiExtType = ISD::ZEXTLOAD;
669
670 // Load the value in two parts
671 SDOperand Lo, Hi;
672 if (TLI.isLittleEndian()) {
673 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
674 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
675 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
676 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
677 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
678 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000679 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000680 } else {
681 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
682 NewLoadedVT,LD->isVolatile(), Alignment);
683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
684 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
686 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000687 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000688 }
689
690 // aggregate the two parts
691 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
692 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
693 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
694
695 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
696 Hi.getValue(1));
697
698 SDOperand Ops[] = { Result, TF };
699 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
700}
Evan Cheng912095b2007-01-04 21:56:39 +0000701
Dan Gohman82669522007-10-11 23:57:53 +0000702/// UnrollVectorOp - We know that the given vector has a legal type, however
703/// the operation it performs is not legal and is an operation that we have
704/// no way of lowering. "Unroll" the vector, splitting out the scalars and
705/// operating on each element individually.
706SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
707 MVT::ValueType VT = Op.getValueType();
708 assert(isTypeLegal(VT) &&
709 "Caller should expand or promote operands that are not legal!");
710 assert(Op.Val->getNumValues() == 1 &&
711 "Can't unroll a vector with multiple results!");
712 unsigned NE = MVT::getVectorNumElements(VT);
713 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
714
715 SmallVector<SDOperand, 8> Scalars;
716 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
717 for (unsigned i = 0; i != NE; ++i) {
718 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
719 SDOperand Operand = Op.getOperand(j);
720 MVT::ValueType OperandVT = Operand.getValueType();
721 if (MVT::isVector(OperandVT)) {
722 // A vector operand; extract a single element.
723 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
724 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
725 OperandEltVT,
726 Operand,
727 DAG.getConstant(i, MVT::i32));
728 } else {
729 // A scalar operand; just use it as is.
730 Operands[j] = Operand;
731 }
732 }
733 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
734 &Operands[0], Operands.size()));
735 }
736
737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
738}
739
Duncan Sands007f9842008-01-10 10:28:30 +0000740/// GetFPLibCall - Return the right libcall for the given floating point type.
741static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
742 RTLIB::Libcall Call_F32,
743 RTLIB::Libcall Call_F64,
744 RTLIB::Libcall Call_F80,
745 RTLIB::Libcall Call_PPCF128) {
746 return
747 VT == MVT::f32 ? Call_F32 :
748 VT == MVT::f64 ? Call_F64 :
749 VT == MVT::f80 ? Call_F80 :
750 VT == MVT::ppcf128 ? Call_PPCF128 :
751 RTLIB::UNKNOWN_LIBCALL;
752}
753
Dan Gohmana3466152007-07-13 20:14:11 +0000754/// LegalizeOp - We know that the specified value has a legal type, and
755/// that its operands are legal. Now ensure that the operation itself
756/// is legal, recursively ensuring that the operands' operations remain
757/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000758SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000759 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
760 return Op;
761
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000762 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000763 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000764 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000765
Chris Lattner3e928bb2005-01-07 07:47:09 +0000766 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000767 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000768 if (Node->getNumValues() > 1) {
769 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000770 if (getTypeAction(Node->getValueType(i)) != Legal) {
771 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000773 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000774 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000775 }
776 }
777
Chris Lattner45982da2005-05-12 16:53:42 +0000778 // Note that LegalizeOp may be reentered even from single-use nodes, which
779 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000780 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000781 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000782
Nate Begeman9373a812005-08-10 20:51:12 +0000783 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000784 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000785 bool isCustom = false;
786
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000788 case ISD::FrameIndex:
789 case ISD::EntryToken:
790 case ISD::Register:
791 case ISD::BasicBlock:
792 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000793 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000794 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000795 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000796 case ISD::TargetConstantPool:
797 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000798 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000799 case ISD::TargetExternalSymbol:
800 case ISD::VALUETYPE:
801 case ISD::SRCVALUE:
802 case ISD::STRING:
803 case ISD::CONDCODE:
804 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000805 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 "This must be legal!");
807 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000808 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000809 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
810 // If this is a target node, legalize it by legalizing the operands then
811 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000812 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000813 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000814 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000815
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000816 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000817
818 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
819 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
820 return Result.getValue(Op.ResNo);
821 }
822 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000823#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000824 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000825#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000826 assert(0 && "Do not know how to legalize this operator!");
827 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000828 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000830 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000831 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000832 case ISD::ConstantPool:
833 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000834 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
835 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000836 case TargetLowering::Custom:
837 Tmp1 = TLI.LowerOperation(Op, DAG);
838 if (Tmp1.Val) Result = Tmp1;
839 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000840 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000841 break;
842 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000843 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000844 case ISD::FRAMEADDR:
845 case ISD::RETURNADDR:
846 // The only option for these nodes is to custom lower them. If the target
847 // does not custom lower them, then return zero.
848 Tmp1 = TLI.LowerOperation(Op, DAG);
849 if (Tmp1.Val)
850 Result = Tmp1;
851 else
852 Result = DAG.getConstant(0, TLI.getPointerTy());
853 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000854 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000855 MVT::ValueType VT = Node->getValueType(0);
856 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
857 default: assert(0 && "This action is not supported yet!");
858 case TargetLowering::Custom:
859 Result = TLI.LowerOperation(Op, DAG);
860 if (Result.Val) break;
861 // Fall Thru
862 case TargetLowering::Legal:
863 Result = DAG.getConstant(0, VT);
864 break;
865 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000866 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000867 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000868 case ISD::EXCEPTIONADDR: {
869 Tmp1 = LegalizeOp(Node->getOperand(0));
870 MVT::ValueType VT = Node->getValueType(0);
871 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
872 default: assert(0 && "This action is not supported yet!");
873 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000874 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000875 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000876 }
877 break;
878 case TargetLowering::Custom:
879 Result = TLI.LowerOperation(Op, DAG);
880 if (Result.Val) break;
881 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000882 case TargetLowering::Legal: {
883 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
884 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000885 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000886 break;
887 }
888 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000889 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000890 if (Result.Val->getNumValues() == 1) break;
891
892 assert(Result.Val->getNumValues() == 2 &&
893 "Cannot return more than two values!");
894
895 // Since we produced two values, make sure to remember that we
896 // legalized both of them.
897 Tmp1 = LegalizeOp(Result);
898 Tmp2 = LegalizeOp(Result.getValue(1));
899 AddLegalizedOperand(Op.getValue(0), Tmp1);
900 AddLegalizedOperand(Op.getValue(1), Tmp2);
901 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000902 case ISD::EHSELECTION: {
903 Tmp1 = LegalizeOp(Node->getOperand(0));
904 Tmp2 = LegalizeOp(Node->getOperand(1));
905 MVT::ValueType VT = Node->getValueType(0);
906 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
907 default: assert(0 && "This action is not supported yet!");
908 case TargetLowering::Expand: {
909 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000910 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000911 }
912 break;
913 case TargetLowering::Custom:
914 Result = TLI.LowerOperation(Op, DAG);
915 if (Result.Val) break;
916 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000917 case TargetLowering::Legal: {
918 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
919 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000920 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000921 break;
922 }
923 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000924 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000925 if (Result.Val->getNumValues() == 1) break;
926
927 assert(Result.Val->getNumValues() == 2 &&
928 "Cannot return more than two values!");
929
930 // Since we produced two values, make sure to remember that we
931 // legalized both of them.
932 Tmp1 = LegalizeOp(Result);
933 Tmp2 = LegalizeOp(Result.getValue(1));
934 AddLegalizedOperand(Op.getValue(0), Tmp1);
935 AddLegalizedOperand(Op.getValue(1), Tmp2);
936 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000937 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000938 MVT::ValueType VT = Node->getValueType(0);
939 // The only "good" option for this node is to custom lower it.
940 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
941 default: assert(0 && "This action is not supported at all!");
942 case TargetLowering::Custom:
943 Result = TLI.LowerOperation(Op, DAG);
944 if (Result.Val) break;
945 // Fall Thru
946 case TargetLowering::Legal:
947 // Target does not know, how to lower this, lower to noop
948 Result = LegalizeOp(Node->getOperand(0));
949 break;
950 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000951 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000952 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000953 case ISD::AssertSext:
954 case ISD::AssertZext:
955 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000957 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000958 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000959 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000960 Result = Node->getOperand(Op.ResNo);
961 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000962 case ISD::CopyFromReg:
963 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000964 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000965 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000967 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000968 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000969 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000970 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
972 } else {
973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
974 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000975 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
976 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000977 // Since CopyFromReg produces two values, make sure to remember that we
978 // legalized both of them.
979 AddLegalizedOperand(Op.getValue(0), Result);
980 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
981 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000982 case ISD::UNDEF: {
983 MVT::ValueType VT = Op.getValueType();
984 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000985 default: assert(0 && "This action is not supported yet!");
986 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000987 if (MVT::isInteger(VT))
988 Result = DAG.getConstant(0, VT);
989 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000990 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
991 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000992 else
993 assert(0 && "Unknown value type!");
994 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000995 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000996 break;
997 }
998 break;
999 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001000
Chris Lattner48b61a72006-03-28 00:40:33 +00001001 case ISD::INTRINSIC_W_CHAIN:
1002 case ISD::INTRINSIC_WO_CHAIN:
1003 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001004 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001005 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1006 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001007 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001008
1009 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001010 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001011 TargetLowering::Custom) {
1012 Tmp3 = TLI.LowerOperation(Result, DAG);
1013 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001014 }
1015
1016 if (Result.Val->getNumValues() == 1) break;
1017
1018 // Must have return value and chain result.
1019 assert(Result.Val->getNumValues() == 2 &&
1020 "Cannot return more than two values!");
1021
1022 // Since loads produce two values, make sure to remember that we
1023 // legalized both of them.
1024 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1025 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1026 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001027 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001028
1029 case ISD::LOCATION:
1030 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1031 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1032
1033 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1034 case TargetLowering::Promote:
1035 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001036 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001037 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001038 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001039 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001040
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001041 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001042 const std::string &FName =
1043 cast<StringSDNode>(Node->getOperand(3))->getValue();
1044 const std::string &DirName =
1045 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001046 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001047
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001048 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001049 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001050 SDOperand LineOp = Node->getOperand(1);
1051 SDOperand ColOp = Node->getOperand(2);
1052
1053 if (useDEBUG_LOC) {
1054 Ops.push_back(LineOp); // line #
1055 Ops.push_back(ColOp); // col #
1056 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001057 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001058 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001059 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1060 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001061 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001062 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001063 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001064 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001065 } else {
1066 Result = Tmp1; // chain
1067 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001068 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001069 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001070 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001071 if (Tmp1 != Node->getOperand(0) ||
1072 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001073 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001074 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001075 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1076 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1077 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1078 } else {
1079 // Otherwise promote them.
1080 Ops.push_back(PromoteOp(Node->getOperand(1)));
1081 Ops.push_back(PromoteOp(Node->getOperand(2)));
1082 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001083 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1084 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001085 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001086 }
1087 break;
1088 }
1089 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001090
1091 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001092 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001093 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001094 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001095 case TargetLowering::Legal:
1096 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1097 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1098 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1099 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001101 break;
1102 }
1103 break;
1104
Jim Laskey1ee29252007-01-26 14:34:52 +00001105 case ISD::LABEL:
1106 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1107 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001108 default: assert(0 && "This action is not supported yet!");
1109 case TargetLowering::Legal:
1110 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1111 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001112 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001113 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001114 case TargetLowering::Expand:
1115 Result = LegalizeOp(Node->getOperand(0));
1116 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001117 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001118 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001119
Scott Michelc1513d22007-08-08 23:23:31 +00001120 case ISD::Constant: {
1121 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1122 unsigned opAction =
1123 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1124
Chris Lattner3e928bb2005-01-07 07:47:09 +00001125 // We know we don't need to expand constants here, constants only have one
1126 // value and we check that it is fine above.
1127
Scott Michelc1513d22007-08-08 23:23:31 +00001128 if (opAction == TargetLowering::Custom) {
1129 Tmp1 = TLI.LowerOperation(Result, DAG);
1130 if (Tmp1.Val)
1131 Result = Tmp1;
1132 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001133 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001134 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001135 case ISD::ConstantFP: {
1136 // Spill FP immediates to the constant pool if the target cannot directly
1137 // codegen them. Targets often have some immediate values that can be
1138 // efficiently generated into an FP register without a load. We explicitly
1139 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001140 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1141
1142 // Check to see if this FP immediate is already legal.
1143 bool isLegal = false;
1144 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1145 E = TLI.legal_fpimm_end(); I != E; ++I)
1146 if (CFP->isExactlyValue(*I)) {
1147 isLegal = true;
1148 break;
1149 }
1150
Chris Lattner3181a772006-01-29 06:26:56 +00001151 // If this is a legal constant, turn it into a TargetConstantFP node.
1152 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001153 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1154 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001155 break;
1156 }
1157
1158 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1159 default: assert(0 && "This action is not supported yet!");
1160 case TargetLowering::Custom:
1161 Tmp3 = TLI.LowerOperation(Result, DAG);
1162 if (Tmp3.Val) {
1163 Result = Tmp3;
1164 break;
1165 }
1166 // FALLTHROUGH
1167 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001168 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001169 }
1170 break;
1171 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001172 case ISD::TokenFactor:
1173 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001174 Tmp1 = LegalizeOp(Node->getOperand(0));
1175 Tmp2 = LegalizeOp(Node->getOperand(1));
1176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1177 } else if (Node->getNumOperands() == 3) {
1178 Tmp1 = LegalizeOp(Node->getOperand(0));
1179 Tmp2 = LegalizeOp(Node->getOperand(1));
1180 Tmp3 = LegalizeOp(Node->getOperand(2));
1181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001182 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001183 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001184 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001185 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1186 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001187 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001188 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001189 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001190
1191 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001192 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001193 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001194 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1195 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001196
1197 // The number of incoming and outgoing values should match; unless the final
1198 // outgoing value is a flag.
1199 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1200 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1201 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1202 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001203 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001204
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001205 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001206 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001207 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001208 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1209 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001210 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001211 if (Op.ResNo == i)
1212 Tmp2 = Tmp1;
1213 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1214 }
1215 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001216 case ISD::EXTRACT_SUBREG: {
1217 Tmp1 = LegalizeOp(Node->getOperand(0));
1218 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1219 assert(idx && "Operand must be a constant");
1220 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1221 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1222 }
1223 break;
1224 case ISD::INSERT_SUBREG: {
1225 Tmp1 = LegalizeOp(Node->getOperand(0));
1226 Tmp2 = LegalizeOp(Node->getOperand(1));
1227 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1228 assert(idx && "Operand must be a constant");
1229 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1230 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1231 }
1232 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001233 case ISD::BUILD_VECTOR:
1234 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001235 default: assert(0 && "This action is not supported yet!");
1236 case TargetLowering::Custom:
1237 Tmp3 = TLI.LowerOperation(Result, DAG);
1238 if (Tmp3.Val) {
1239 Result = Tmp3;
1240 break;
1241 }
1242 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001243 case TargetLowering::Expand:
1244 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001245 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001246 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001247 break;
1248 case ISD::INSERT_VECTOR_ELT:
1249 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1250 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1251 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1252 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1253
1254 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1255 Node->getValueType(0))) {
1256 default: assert(0 && "This action is not supported yet!");
1257 case TargetLowering::Legal:
1258 break;
1259 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001260 Tmp4 = TLI.LowerOperation(Result, DAG);
1261 if (Tmp4.Val) {
1262 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001263 break;
1264 }
1265 // FALLTHROUGH
1266 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001267 // If the insert index is a constant, codegen this as a scalar_to_vector,
1268 // then a shuffle that inserts it into the right position in the vector.
1269 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1270 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1271 Tmp1.getValueType(), Tmp2);
1272
1273 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1274 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001275 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001276
1277 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1278 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1279 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001280 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001281 for (unsigned i = 0; i != NumElts; ++i) {
1282 if (i != InsertPos->getValue())
1283 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1284 else
1285 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1286 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001287 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1288 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001289
1290 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1291 Tmp1, ScVec, ShufMask);
1292 Result = LegalizeOp(Result);
1293 break;
1294 }
1295
Chris Lattner2332b9f2006-03-19 01:17:20 +00001296 // If the target doesn't support this, we have to spill the input vector
1297 // to a temporary stack slot, update the element, then reload it. This is
1298 // badness. We could also load the value into a vector register (either
1299 // with a "move to register" or "extload into register" instruction, then
1300 // permute it into place, if the idx is a constant and if the idx is
1301 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001302 MVT::ValueType VT = Tmp1.getValueType();
1303 MVT::ValueType EltVT = Tmp2.getValueType();
1304 MVT::ValueType IdxVT = Tmp3.getValueType();
1305 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001306 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001307 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001308 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001309
1310 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001311 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1312 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001313 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001314 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1315 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1316 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001317 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001318 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001319 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001320 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001321 break;
1322 }
1323 }
1324 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001325 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001326 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1327 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1328 break;
1329 }
1330
Chris Lattnerce872152006-03-19 06:31:19 +00001331 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1332 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1333 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1334 Node->getValueType(0))) {
1335 default: assert(0 && "This action is not supported yet!");
1336 case TargetLowering::Legal:
1337 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001338 case TargetLowering::Custom:
1339 Tmp3 = TLI.LowerOperation(Result, DAG);
1340 if (Tmp3.Val) {
1341 Result = Tmp3;
1342 break;
1343 }
1344 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001345 case TargetLowering::Expand:
1346 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001347 break;
1348 }
Chris Lattnerce872152006-03-19 06:31:19 +00001349 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001350 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001351 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1352 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1354
1355 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001356 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1357 default: assert(0 && "Unknown operation action!");
1358 case TargetLowering::Legal:
1359 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1360 "vector shuffle should not be created if not legal!");
1361 break;
1362 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001363 Tmp3 = TLI.LowerOperation(Result, DAG);
1364 if (Tmp3.Val) {
1365 Result = Tmp3;
1366 break;
1367 }
1368 // FALLTHROUGH
1369 case TargetLowering::Expand: {
1370 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001371 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001372 MVT::ValueType PtrVT = TLI.getPointerTy();
1373 SDOperand Mask = Node->getOperand(2);
1374 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001375 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001376 for (unsigned i = 0; i != NumElems; ++i) {
1377 SDOperand Arg = Mask.getOperand(i);
1378 if (Arg.getOpcode() == ISD::UNDEF) {
1379 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1380 } else {
1381 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1382 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1383 if (Idx < NumElems)
1384 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1385 DAG.getConstant(Idx, PtrVT)));
1386 else
1387 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1388 DAG.getConstant(Idx - NumElems, PtrVT)));
1389 }
1390 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001391 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001392 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001393 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001394 case TargetLowering::Promote: {
1395 // Change base type to a different vector type.
1396 MVT::ValueType OVT = Node->getValueType(0);
1397 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1398
1399 // Cast the two input vectors.
1400 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1401 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1402
1403 // Convert the shuffle mask to the right # elements.
1404 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1405 assert(Tmp3.Val && "Shuffle not legal?");
1406 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1407 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1408 break;
1409 }
Chris Lattner87100e02006-03-20 01:52:29 +00001410 }
1411 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001412
1413 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001414 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001415 Tmp2 = LegalizeOp(Node->getOperand(1));
1416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001417 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001418 break;
1419
Dan Gohman7f321562007-06-25 16:23:39 +00001420 case ISD::EXTRACT_SUBVECTOR:
1421 Tmp1 = Node->getOperand(0);
1422 Tmp2 = LegalizeOp(Node->getOperand(1));
1423 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1424 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001425 break;
1426
Chris Lattner6831a812006-02-13 09:18:02 +00001427 case ISD::CALLSEQ_START: {
1428 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1429
1430 // Recursively Legalize all of the inputs of the call end that do not lead
1431 // to this call start. This ensures that any libcalls that need be inserted
1432 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001433 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001434 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001435 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1436 NodesLeadingTo);
1437 }
Chris Lattner6831a812006-02-13 09:18:02 +00001438
1439 // Now that we legalized all of the inputs (which may have inserted
1440 // libcalls) create the new CALLSEQ_START node.
1441 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1442
1443 // Merge in the last call, to ensure that this call start after the last
1444 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001445 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001446 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1447 Tmp1 = LegalizeOp(Tmp1);
1448 }
Chris Lattner6831a812006-02-13 09:18:02 +00001449
1450 // Do not try to legalize the target-specific arguments (#1+).
1451 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001452 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001453 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001454 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001455 }
1456
1457 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001458 AddLegalizedOperand(Op.getValue(0), Result);
1459 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1460 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1461
Chris Lattner6831a812006-02-13 09:18:02 +00001462 // Now that the callseq_start and all of the non-call nodes above this call
1463 // sequence have been legalized, legalize the call itself. During this
1464 // process, no libcalls can/will be inserted, guaranteeing that no calls
1465 // can overlap.
1466 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1467 SDOperand InCallSEQ = LastCALLSEQ_END;
1468 // Note that we are selecting this call!
1469 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1470 IsLegalizingCall = true;
1471
1472 // Legalize the call, starting from the CALLSEQ_END.
1473 LegalizeOp(LastCALLSEQ_END);
1474 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1475 return Result;
1476 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001477 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001478 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1479 // will cause this node to be legalized as well as handling libcalls right.
1480 if (LastCALLSEQ_END.Val != Node) {
1481 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001482 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001483 assert(I != LegalizedNodes.end() &&
1484 "Legalizing the call start should have legalized this node!");
1485 return I->second;
1486 }
1487
1488 // Otherwise, the call start has been legalized and everything is going
1489 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001490 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001491 // Do not try to legalize the target-specific arguments (#1+), except for
1492 // an optional flag input.
1493 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1494 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001495 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001496 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001497 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001498 }
1499 } else {
1500 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1501 if (Tmp1 != Node->getOperand(0) ||
1502 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001503 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001504 Ops[0] = Tmp1;
1505 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001506 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001507 }
Chris Lattner6a542892006-01-24 05:48:21 +00001508 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001509 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001510 // This finishes up call legalization.
1511 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001512
1513 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1514 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1515 if (Node->getNumValues() == 2)
1516 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1517 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001518 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001519 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001520 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1521 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1522 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001523 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001524
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001525 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001526 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001527 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001528 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001529 case TargetLowering::Expand: {
1530 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1531 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1532 " not tell us which reg is the stack pointer!");
1533 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001534
1535 // Chain the dynamic stack allocation so that it doesn't modify the stack
1536 // pointer when other instructions are using the stack.
1537 Chain = DAG.getCALLSEQ_START(Chain,
1538 DAG.getConstant(0, TLI.getPointerTy()));
1539
Chris Lattner903d2782006-01-15 08:54:32 +00001540 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001541 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1542 Chain = SP.getValue(1);
1543 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1544 unsigned StackAlign =
1545 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1546 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001547 SP = DAG.getNode(ISD::AND, VT, SP,
1548 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001549 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001550 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1551
1552 Tmp2 =
1553 DAG.getCALLSEQ_END(Chain,
1554 DAG.getConstant(0, TLI.getPointerTy()),
1555 DAG.getConstant(0, TLI.getPointerTy()),
1556 SDOperand());
1557
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001558 Tmp1 = LegalizeOp(Tmp1);
1559 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001560 break;
1561 }
1562 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001563 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1564 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001565 Tmp1 = LegalizeOp(Tmp3);
1566 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001567 }
Chris Lattner903d2782006-01-15 08:54:32 +00001568 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001569 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001570 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001571 }
Chris Lattner903d2782006-01-15 08:54:32 +00001572 // Since this op produce two values, make sure to remember that we
1573 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001574 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1575 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001576 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001577 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001578 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001579 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001580 bool Changed = false;
1581 // Legalize all of the operands of the inline asm, in case they are nodes
1582 // that need to be expanded or something. Note we skip the asm string and
1583 // all of the TargetConstant flags.
1584 SDOperand Op = LegalizeOp(Ops[0]);
1585 Changed = Op != Ops[0];
1586 Ops[0] = Op;
1587
1588 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1589 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1590 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1591 for (++i; NumVals; ++i, --NumVals) {
1592 SDOperand Op = LegalizeOp(Ops[i]);
1593 if (Op != Ops[i]) {
1594 Changed = true;
1595 Ops[i] = Op;
1596 }
1597 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001598 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001599
1600 if (HasInFlag) {
1601 Op = LegalizeOp(Ops.back());
1602 Changed |= Op != Ops.back();
1603 Ops.back() = Op;
1604 }
1605
1606 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001607 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001608
1609 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001610 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001611 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1612 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001613 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001614 case ISD::BR:
1615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001616 // Ensure that libcalls are emitted before a branch.
1617 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1618 Tmp1 = LegalizeOp(Tmp1);
1619 LastCALLSEQ_END = DAG.getEntryNode();
1620
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001622 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001623 case ISD::BRIND:
1624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1625 // Ensure that libcalls are emitted before a branch.
1626 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1627 Tmp1 = LegalizeOp(Tmp1);
1628 LastCALLSEQ_END = DAG.getEntryNode();
1629
1630 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1631 default: assert(0 && "Indirect target must be legal type (pointer)!");
1632 case Legal:
1633 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1634 break;
1635 }
1636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1637 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001638 case ISD::BR_JT:
1639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1640 // Ensure that libcalls are emitted before a branch.
1641 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1642 Tmp1 = LegalizeOp(Tmp1);
1643 LastCALLSEQ_END = DAG.getEntryNode();
1644
1645 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1646 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1647
1648 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1649 default: assert(0 && "This action is not supported yet!");
1650 case TargetLowering::Legal: break;
1651 case TargetLowering::Custom:
1652 Tmp1 = TLI.LowerOperation(Result, DAG);
1653 if (Tmp1.Val) Result = Tmp1;
1654 break;
1655 case TargetLowering::Expand: {
1656 SDOperand Chain = Result.getOperand(0);
1657 SDOperand Table = Result.getOperand(1);
1658 SDOperand Index = Result.getOperand(2);
1659
1660 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001661 MachineFunction &MF = DAG.getMachineFunction();
1662 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001663 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1664 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001665
1666 SDOperand LD;
1667 switch (EntrySize) {
1668 default: assert(0 && "Size of jump table not supported yet."); break;
1669 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1670 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1671 }
1672
Evan Chengcc415862007-11-09 01:32:10 +00001673 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001674 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001675 // For PIC, the sequence is:
1676 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001677 // RelocBase can be JumpTable, GOT or some sort of global base.
1678 if (PTy != MVT::i32)
1679 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1680 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1681 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001682 }
Evan Chengcc415862007-11-09 01:32:10 +00001683 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001684 }
1685 }
1686 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001687 case ISD::BRCOND:
1688 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001689 // Ensure that libcalls are emitted before a return.
1690 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1691 Tmp1 = LegalizeOp(Tmp1);
1692 LastCALLSEQ_END = DAG.getEntryNode();
1693
Chris Lattner47e92232005-01-18 19:27:06 +00001694 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1695 case Expand: assert(0 && "It's impossible to expand bools");
1696 case Legal:
1697 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1698 break;
1699 case Promote:
1700 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001701
1702 // The top bits of the promoted condition are not necessarily zero, ensure
1703 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001705 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1706 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001707 break;
1708 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001709
1710 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001712
1713 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1714 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001715 case TargetLowering::Legal: break;
1716 case TargetLowering::Custom:
1717 Tmp1 = TLI.LowerOperation(Result, DAG);
1718 if (Tmp1.Val) Result = Tmp1;
1719 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001720 case TargetLowering::Expand:
1721 // Expand brcond's setcc into its constituent parts and create a BR_CC
1722 // Node.
1723 if (Tmp2.getOpcode() == ISD::SETCC) {
1724 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1725 Tmp2.getOperand(0), Tmp2.getOperand(1),
1726 Node->getOperand(2));
1727 } else {
1728 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1729 DAG.getCondCode(ISD::SETNE), Tmp2,
1730 DAG.getConstant(0, Tmp2.getValueType()),
1731 Node->getOperand(2));
1732 }
1733 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001734 }
1735 break;
1736 case ISD::BR_CC:
1737 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001738 // Ensure that libcalls are emitted before a branch.
1739 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1740 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001741 Tmp2 = Node->getOperand(2); // LHS
1742 Tmp3 = Node->getOperand(3); // RHS
1743 Tmp4 = Node->getOperand(1); // CC
1744
1745 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001746 LastCALLSEQ_END = DAG.getEntryNode();
1747
Nate Begeman750ac1b2006-02-01 07:19:44 +00001748 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1749 // the LHS is a legal SETCC itself. In this case, we need to compare
1750 // the result against zero to select between true and false values.
1751 if (Tmp3.Val == 0) {
1752 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1753 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001754 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001755
1756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1757 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001758
Chris Lattner181b7a32005-12-17 23:46:46 +00001759 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1760 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001761 case TargetLowering::Legal: break;
1762 case TargetLowering::Custom:
1763 Tmp4 = TLI.LowerOperation(Result, DAG);
1764 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001765 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001766 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001767 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001768 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001769 LoadSDNode *LD = cast<LoadSDNode>(Node);
1770 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1771 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001772
Evan Cheng466685d2006-10-09 20:57:25 +00001773 ISD::LoadExtType ExtType = LD->getExtensionType();
1774 if (ExtType == ISD::NON_EXTLOAD) {
1775 MVT::ValueType VT = Node->getValueType(0);
1776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1777 Tmp3 = Result.getValue(0);
1778 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001779
Evan Cheng466685d2006-10-09 20:57:25 +00001780 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1781 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001782 case TargetLowering::Legal:
1783 // If this is an unaligned load and the target doesn't support it,
1784 // expand it.
1785 if (!TLI.allowsUnalignedMemoryAccesses()) {
1786 unsigned ABIAlignment = TLI.getTargetData()->
1787 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1788 if (LD->getAlignment() < ABIAlignment){
1789 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1790 TLI);
1791 Tmp3 = Result.getOperand(0);
1792 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001793 Tmp3 = LegalizeOp(Tmp3);
1794 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001795 }
1796 }
1797 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001798 case TargetLowering::Custom:
1799 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1800 if (Tmp1.Val) {
1801 Tmp3 = LegalizeOp(Tmp1);
1802 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001803 }
Evan Cheng466685d2006-10-09 20:57:25 +00001804 break;
1805 case TargetLowering::Promote: {
1806 // Only promote a load of vector type to another.
1807 assert(MVT::isVector(VT) && "Cannot promote this load!");
1808 // Change base type to a different vector type.
1809 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1810
1811 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001812 LD->getSrcValueOffset(),
1813 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001814 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1815 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001816 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001817 }
Evan Cheng466685d2006-10-09 20:57:25 +00001818 }
1819 // Since loads produce two values, make sure to remember that we
1820 // legalized both of them.
1821 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1822 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1823 return Op.ResNo ? Tmp4 : Tmp3;
1824 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001825 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001826 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1827 default: assert(0 && "This action is not supported yet!");
1828 case TargetLowering::Promote:
1829 assert(SrcVT == MVT::i1 &&
1830 "Can only promote extending LOAD from i1 -> i8!");
1831 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1832 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001833 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001834 Tmp1 = Result.getValue(0);
1835 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001836 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001837 case TargetLowering::Custom:
1838 isCustom = true;
1839 // FALLTHROUGH
1840 case TargetLowering::Legal:
1841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1842 Tmp1 = Result.getValue(0);
1843 Tmp2 = Result.getValue(1);
1844
1845 if (isCustom) {
1846 Tmp3 = TLI.LowerOperation(Result, DAG);
1847 if (Tmp3.Val) {
1848 Tmp1 = LegalizeOp(Tmp3);
1849 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1850 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001851 } else {
1852 // If this is an unaligned load and the target doesn't support it,
1853 // expand it.
1854 if (!TLI.allowsUnalignedMemoryAccesses()) {
1855 unsigned ABIAlignment = TLI.getTargetData()->
1856 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1857 if (LD->getAlignment() < ABIAlignment){
1858 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1859 TLI);
1860 Tmp1 = Result.getOperand(0);
1861 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001862 Tmp1 = LegalizeOp(Tmp1);
1863 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001864 }
1865 }
Evan Cheng466685d2006-10-09 20:57:25 +00001866 }
1867 break;
1868 case TargetLowering::Expand:
1869 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1870 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1871 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001872 LD->getSrcValueOffset(),
1873 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001874 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1875 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1876 Tmp2 = LegalizeOp(Load.getValue(1));
1877 break;
1878 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001879 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001880 // Turn the unsupported load into an EXTLOAD followed by an explicit
1881 // zero/sign extend inreg.
1882 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1883 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001884 LD->getSrcValueOffset(), SrcVT,
1885 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001886 SDOperand ValRes;
1887 if (ExtType == ISD::SEXTLOAD)
1888 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1889 Result, DAG.getValueType(SrcVT));
1890 else
1891 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1892 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1893 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1894 break;
1895 }
1896 // Since loads produce two values, make sure to remember that we legalized
1897 // both of them.
1898 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1899 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1900 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001901 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001902 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001903 case ISD::EXTRACT_ELEMENT: {
1904 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1905 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001906 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001907 case Legal:
1908 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1909 // 1 -> Hi
1910 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1911 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1912 TLI.getShiftAmountTy()));
1913 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1914 } else {
1915 // 0 -> Lo
1916 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1917 Node->getOperand(0));
1918 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001919 break;
1920 case Expand:
1921 // Get both the low and high parts.
1922 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1923 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1924 Result = Tmp2; // 1 -> Hi
1925 else
1926 Result = Tmp1; // 0 -> Lo
1927 break;
1928 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001929 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001930 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001931
1932 case ISD::CopyToReg:
1933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001934
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001935 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001936 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001937 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001938 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001939 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001940 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001941 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001942 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001943 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001944 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1946 Tmp3);
1947 } else {
1948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001949 }
1950
1951 // Since this produces two values, make sure to remember that we legalized
1952 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001953 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001954 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001955 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001956 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001957 break;
1958
1959 case ISD::RET:
1960 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001961
1962 // Ensure that libcalls are emitted before a return.
1963 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1964 Tmp1 = LegalizeOp(Tmp1);
1965 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001966
Chris Lattner3e928bb2005-01-07 07:47:09 +00001967 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001968 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001969 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001970 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001971 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001972 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001973 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001974 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001975 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001976 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001977 SDOperand Lo, Hi;
1978 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001979
1980 // Big endian systems want the hi reg first.
1981 if (!TLI.isLittleEndian())
1982 std::swap(Lo, Hi);
1983
Evan Cheng13acce32006-12-11 19:27:14 +00001984 if (Hi.Val)
1985 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1986 else
1987 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001988 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001989 } else {
1990 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00001991 int InIx = Tmp2.ResNo;
1992 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1993 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001994
Dan Gohman7f321562007-06-25 16:23:39 +00001995 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001996 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001997 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001998 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001999 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002000 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002001 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002002 } else if (NumElems == 1) {
2003 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002004 Tmp2 = ScalarizeVectorOp(Tmp2);
2005 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002007
2008 // FIXME: Returns of gcc generic vectors smaller than a legal type
2009 // should be returned in integer registers!
2010
Chris Lattnerf87324e2006-04-11 01:31:51 +00002011 // The scalarized value type may not be legal, e.g. it might require
2012 // promotion or expansion. Relegalize the return.
2013 Result = LegalizeOp(Result);
2014 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002015 // FIXME: Returns of gcc generic vectors larger than a legal vector
2016 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002017 SDOperand Lo, Hi;
2018 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002019 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002020 Result = LegalizeOp(Result);
2021 }
2022 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002023 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002024 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002025 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002027 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002028 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002029 }
2030 break;
2031 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002032 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002033 break;
2034 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002035 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002036 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002037 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002038 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2039 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002040 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002041 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002042 break;
2043 case Expand: {
2044 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002045 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002046 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002047 ExpandOp(Node->getOperand(i), Lo, Hi);
2048 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002049 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002050 if (Hi.Val) {
2051 NewValues.push_back(Hi);
2052 NewValues.push_back(Node->getOperand(i+1));
2053 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002054 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002055 }
2056 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002057 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002058 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002059
2060 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002061 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002062 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002063 Result = DAG.getNode(ISD::RET, MVT::Other,
2064 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002065 break;
2066 }
2067 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002068
Chris Lattner6862dbc2006-01-29 21:02:23 +00002069 if (Result.getOpcode() == ISD::RET) {
2070 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2071 default: assert(0 && "This action is not supported yet!");
2072 case TargetLowering::Legal: break;
2073 case TargetLowering::Custom:
2074 Tmp1 = TLI.LowerOperation(Result, DAG);
2075 if (Tmp1.Val) Result = Tmp1;
2076 break;
2077 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002078 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002079 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002080 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002081 StoreSDNode *ST = cast<StoreSDNode>(Node);
2082 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2083 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002084 int SVOffset = ST->getSrcValueOffset();
2085 unsigned Alignment = ST->getAlignment();
2086 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002087
Evan Cheng8b2794a2006-10-13 21:14:26 +00002088 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002089 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2090 // FIXME: We shouldn't do this for TargetConstantFP's.
2091 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2092 // to phase ordering between legalized code and the dag combiner. This
2093 // probably means that we need to integrate dag combiner and legalizer
2094 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002095 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002096 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002097 if (CFP->getValueType(0) == MVT::f32 &&
2098 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002099 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2100 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002101 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002102 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2103 SVOffset, isVolatile, Alignment);
2104 break;
2105 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002106 // If this target supports 64-bit registers, do a single 64-bit store.
2107 if (getTypeAction(MVT::i64) == Legal) {
2108 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2109 getZExtValue(), MVT::i64);
2110 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2111 SVOffset, isVolatile, Alignment);
2112 break;
2113 } else if (getTypeAction(MVT::i32) == Legal) {
2114 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2115 // stores. If the target supports neither 32- nor 64-bits, this
2116 // xform is certainly not worth it.
2117 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2118 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2119 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2120 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2121
2122 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2123 SVOffset, isVolatile, Alignment);
2124 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2125 getIntPtrConstant(4));
2126 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002127 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002128
2129 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2130 break;
2131 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002132 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002133 }
2134
Evan Cheng8b2794a2006-10-13 21:14:26 +00002135 switch (getTypeAction(ST->getStoredVT())) {
2136 case Legal: {
2137 Tmp3 = LegalizeOp(ST->getValue());
2138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2139 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002140
Evan Cheng8b2794a2006-10-13 21:14:26 +00002141 MVT::ValueType VT = Tmp3.getValueType();
2142 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2143 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002144 case TargetLowering::Legal:
2145 // If this is an unaligned store and the target doesn't support it,
2146 // expand it.
2147 if (!TLI.allowsUnalignedMemoryAccesses()) {
2148 unsigned ABIAlignment = TLI.getTargetData()->
2149 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2150 if (ST->getAlignment() < ABIAlignment)
2151 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2152 TLI);
2153 }
2154 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002155 case TargetLowering::Custom:
2156 Tmp1 = TLI.LowerOperation(Result, DAG);
2157 if (Tmp1.Val) Result = Tmp1;
2158 break;
2159 case TargetLowering::Promote:
2160 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2161 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2162 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2163 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002164 ST->getSrcValue(), SVOffset, isVolatile,
2165 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002166 break;
2167 }
2168 break;
2169 }
2170 case Promote:
2171 // Truncate the value and store the result.
2172 Tmp3 = PromoteOp(ST->getValue());
2173 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002174 SVOffset, ST->getStoredVT(),
2175 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002176 break;
2177
2178 case Expand:
2179 unsigned IncrementSize = 0;
2180 SDOperand Lo, Hi;
2181
2182 // If this is a vector type, then we have to calculate the increment as
2183 // the product of the element size in bytes, and the number of elements
2184 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002185 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002186 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002187 int InIx = ST->getValue().ResNo;
2188 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2189 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002190
Dan Gohman7f321562007-06-25 16:23:39 +00002191 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002192 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002193 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002194 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002195 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002196 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002197 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002198 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002199 Result = LegalizeOp(Result);
2200 break;
2201 } else if (NumElems == 1) {
2202 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002203 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002204 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002205 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002206 // The scalarized value type may not be legal, e.g. it might require
2207 // promotion or expansion. Relegalize the scalar store.
2208 Result = LegalizeOp(Result);
2209 break;
2210 } else {
2211 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002212 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2213 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002214 }
2215 } else {
2216 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002217 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002218
2219 if (!TLI.isLittleEndian())
2220 std::swap(Lo, Hi);
2221 }
2222
2223 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002224 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002225
2226 if (Hi.Val == NULL) {
2227 // Must be int <-> float one-to-one expansion.
2228 Result = Lo;
2229 break;
2230 }
2231
Evan Cheng8b2794a2006-10-13 21:14:26 +00002232 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2233 getIntPtrConstant(IncrementSize));
2234 assert(isTypeLegal(Tmp2.getValueType()) &&
2235 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002236 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002237 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002238 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002239 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002240 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2241 break;
2242 }
2243 } else {
2244 // Truncating store
2245 assert(isTypeLegal(ST->getValue().getValueType()) &&
2246 "Cannot handle illegal TRUNCSTORE yet!");
2247 Tmp3 = LegalizeOp(ST->getValue());
2248
2249 // The only promote case we handle is TRUNCSTORE:i1 X into
2250 // -> TRUNCSTORE:i8 (and X, 1)
2251 if (ST->getStoredVT() == MVT::i1 &&
2252 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2253 // Promote the bool to a mask then store.
2254 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2255 DAG.getConstant(1, Tmp3.getValueType()));
2256 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002257 SVOffset, MVT::i8,
2258 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002259 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2260 Tmp2 != ST->getBasePtr()) {
2261 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2262 ST->getOffset());
2263 }
2264
2265 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2266 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002267 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002268 case TargetLowering::Legal:
2269 // If this is an unaligned store and the target doesn't support it,
2270 // expand it.
2271 if (!TLI.allowsUnalignedMemoryAccesses()) {
2272 unsigned ABIAlignment = TLI.getTargetData()->
2273 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2274 if (ST->getAlignment() < ABIAlignment)
2275 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2276 TLI);
2277 }
2278 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002279 case TargetLowering::Custom:
2280 Tmp1 = TLI.LowerOperation(Result, DAG);
2281 if (Tmp1.Val) Result = Tmp1;
2282 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002283 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002284 }
2285 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002286 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002287 case ISD::PCMARKER:
2288 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002289 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002290 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002291 case ISD::STACKSAVE:
2292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002293 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2294 Tmp1 = Result.getValue(0);
2295 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002296
Chris Lattner140d53c2006-01-13 02:50:02 +00002297 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2298 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002299 case TargetLowering::Legal: break;
2300 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002301 Tmp3 = TLI.LowerOperation(Result, DAG);
2302 if (Tmp3.Val) {
2303 Tmp1 = LegalizeOp(Tmp3);
2304 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002305 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002306 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002307 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002308 // Expand to CopyFromReg if the target set
2309 // StackPointerRegisterToSaveRestore.
2310 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002311 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002312 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002313 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002314 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002315 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2316 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002317 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002318 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002319 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002320
2321 // Since stacksave produce two values, make sure to remember that we
2322 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002323 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2324 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2325 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002326
Chris Lattner140d53c2006-01-13 02:50:02 +00002327 case ISD::STACKRESTORE:
2328 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2329 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002331
2332 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2333 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002334 case TargetLowering::Legal: break;
2335 case TargetLowering::Custom:
2336 Tmp1 = TLI.LowerOperation(Result, DAG);
2337 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002338 break;
2339 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002340 // Expand to CopyToReg if the target set
2341 // StackPointerRegisterToSaveRestore.
2342 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2343 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2344 } else {
2345 Result = Tmp1;
2346 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002347 break;
2348 }
2349 break;
2350
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002351 case ISD::READCYCLECOUNTER:
2352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002353 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002354 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2355 Node->getValueType(0))) {
2356 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002357 case TargetLowering::Legal:
2358 Tmp1 = Result.getValue(0);
2359 Tmp2 = Result.getValue(1);
2360 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002361 case TargetLowering::Custom:
2362 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002363 Tmp1 = LegalizeOp(Result.getValue(0));
2364 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002365 break;
2366 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002367
2368 // Since rdcc produce two values, make sure to remember that we legalized
2369 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002370 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2371 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002372 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002373
Chris Lattner2ee743f2005-01-14 22:08:15 +00002374 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002375 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2376 case Expand: assert(0 && "It's impossible to expand bools");
2377 case Legal:
2378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2379 break;
2380 case Promote:
2381 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002382 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002383 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002384 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2385 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002386 break;
2387 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002388 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002389 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002390
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002392
Nate Begemanb942a3d2005-08-23 04:29:48 +00002393 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002394 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002395 case TargetLowering::Legal: break;
2396 case TargetLowering::Custom: {
2397 Tmp1 = TLI.LowerOperation(Result, DAG);
2398 if (Tmp1.Val) Result = Tmp1;
2399 break;
2400 }
Nate Begeman9373a812005-08-10 20:51:12 +00002401 case TargetLowering::Expand:
2402 if (Tmp1.getOpcode() == ISD::SETCC) {
2403 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2404 Tmp2, Tmp3,
2405 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2406 } else {
2407 Result = DAG.getSelectCC(Tmp1,
2408 DAG.getConstant(0, Tmp1.getValueType()),
2409 Tmp2, Tmp3, ISD::SETNE);
2410 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002411 break;
2412 case TargetLowering::Promote: {
2413 MVT::ValueType NVT =
2414 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2415 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002416 if (MVT::isVector(Tmp2.getValueType())) {
2417 ExtOp = ISD::BIT_CONVERT;
2418 TruncOp = ISD::BIT_CONVERT;
2419 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002420 ExtOp = ISD::ANY_EXTEND;
2421 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002422 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002423 ExtOp = ISD::FP_EXTEND;
2424 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002425 }
2426 // Promote each of the values to the new type.
2427 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2428 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2429 // Perform the larger operation, then round down.
2430 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2431 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2432 break;
2433 }
2434 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002435 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002436 case ISD::SELECT_CC: {
2437 Tmp1 = Node->getOperand(0); // LHS
2438 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002439 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2440 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002441 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002442
Nate Begeman750ac1b2006-02-01 07:19:44 +00002443 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2444
2445 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2446 // the LHS is a legal SETCC itself. In this case, we need to compare
2447 // the result against zero to select between true and false values.
2448 if (Tmp2.Val == 0) {
2449 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2450 CC = DAG.getCondCode(ISD::SETNE);
2451 }
2452 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2453
2454 // Everything is legal, see if we should expand this op or something.
2455 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2456 default: assert(0 && "This action is not supported yet!");
2457 case TargetLowering::Legal: break;
2458 case TargetLowering::Custom:
2459 Tmp1 = TLI.LowerOperation(Result, DAG);
2460 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002461 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002462 }
2463 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002464 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002465 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002466 Tmp1 = Node->getOperand(0);
2467 Tmp2 = Node->getOperand(1);
2468 Tmp3 = Node->getOperand(2);
2469 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2470
2471 // If we had to Expand the SetCC operands into a SELECT node, then it may
2472 // not always be possible to return a true LHS & RHS. In this case, just
2473 // return the value we legalized, returned in the LHS
2474 if (Tmp2.Val == 0) {
2475 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002476 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002477 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002478
Chris Lattner73e142f2006-01-30 22:43:50 +00002479 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002480 default: assert(0 && "Cannot handle this action for SETCC yet!");
2481 case TargetLowering::Custom:
2482 isCustom = true;
2483 // FALLTHROUGH.
2484 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002485 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002486 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002487 Tmp4 = TLI.LowerOperation(Result, DAG);
2488 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002489 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002490 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002491 case TargetLowering::Promote: {
2492 // First step, figure out the appropriate operation to use.
2493 // Allow SETCC to not be supported for all legal data types
2494 // Mostly this targets FP
2495 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002496 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002497
2498 // Scan for the appropriate larger type to use.
2499 while (1) {
2500 NewInTy = (MVT::ValueType)(NewInTy+1);
2501
2502 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2503 "Fell off of the edge of the integer world");
2504 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2505 "Fell off of the edge of the floating point world");
2506
2507 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002508 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002509 break;
2510 }
2511 if (MVT::isInteger(NewInTy))
2512 assert(0 && "Cannot promote Legal Integer SETCC yet");
2513 else {
2514 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2515 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2516 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002517 Tmp1 = LegalizeOp(Tmp1);
2518 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002520 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002521 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002522 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002523 case TargetLowering::Expand:
2524 // Expand a setcc node into a select_cc of the same condition, lhs, and
2525 // rhs that selects between const 1 (true) and const 0 (false).
2526 MVT::ValueType VT = Node->getValueType(0);
2527 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2528 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002529 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002530 break;
2531 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002532 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002533 case ISD::MEMSET:
2534 case ISD::MEMCPY:
2535 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002536 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002537 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2538
2539 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2540 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2541 case Expand: assert(0 && "Cannot expand a byte!");
2542 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002543 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002544 break;
2545 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002546 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002547 break;
2548 }
2549 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002550 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002551 }
Chris Lattner272455b2005-02-02 03:44:41 +00002552
2553 SDOperand Tmp4;
2554 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002555 case Expand: {
2556 // Length is too big, just take the lo-part of the length.
2557 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002558 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002559 break;
2560 }
Chris Lattnere5605212005-01-28 22:29:18 +00002561 case Legal:
2562 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002563 break;
2564 case Promote:
2565 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002566 break;
2567 }
2568
2569 SDOperand Tmp5;
2570 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2571 case Expand: assert(0 && "Cannot expand this yet!");
2572 case Legal:
2573 Tmp5 = LegalizeOp(Node->getOperand(4));
2574 break;
2575 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002576 Tmp5 = PromoteOp(Node->getOperand(4));
2577 break;
2578 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002579
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002580 SDOperand Tmp6;
2581 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2582 case Expand: assert(0 && "Cannot expand this yet!");
2583 case Legal:
2584 Tmp6 = LegalizeOp(Node->getOperand(5));
2585 break;
2586 case Promote:
2587 Tmp6 = PromoteOp(Node->getOperand(5));
2588 break;
2589 }
2590
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002591 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2592 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002593 case TargetLowering::Custom:
2594 isCustom = true;
2595 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002596 case TargetLowering::Legal: {
2597 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2598 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002599 if (isCustom) {
2600 Tmp1 = TLI.LowerOperation(Result, DAG);
2601 if (Tmp1.Val) Result = Tmp1;
2602 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002603 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002604 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002605 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002606 // Otherwise, the target does not support this operation. Lower the
2607 // operation to an explicit libcall as appropriate.
2608 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002609 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002610 TargetLowering::ArgListTy Args;
2611 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002612
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002613 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002614 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002615 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002616 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002617 // Extend the (previously legalized) ubyte argument to be an int value
2618 // for the call.
2619 if (Tmp3.getValueType() > MVT::i32)
2620 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2621 else
2622 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002623 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002624 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002625 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002626 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002627
2628 FnName = "memset";
2629 } else if (Node->getOpcode() == ISD::MEMCPY ||
2630 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002631 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002632 Entry.Node = Tmp2; Args.push_back(Entry);
2633 Entry.Node = Tmp3; Args.push_back(Entry);
2634 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002635 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2636 } else {
2637 assert(0 && "Unknown op!");
2638 }
Chris Lattner45982da2005-05-12 16:53:42 +00002639
Chris Lattnere1bd8222005-01-11 05:57:22 +00002640 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002641 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002642 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002643 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002644 break;
2645 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002646 }
2647 break;
2648 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002649
Chris Lattner5b359c62005-04-02 04:00:59 +00002650 case ISD::SHL_PARTS:
2651 case ISD::SRA_PARTS:
2652 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002653 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002654 bool Changed = false;
2655 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2656 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2657 Changed |= Ops.back() != Node->getOperand(i);
2658 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002659 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002660 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002661
Evan Cheng05a2d562006-01-09 18:31:59 +00002662 switch (TLI.getOperationAction(Node->getOpcode(),
2663 Node->getValueType(0))) {
2664 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002665 case TargetLowering::Legal: break;
2666 case TargetLowering::Custom:
2667 Tmp1 = TLI.LowerOperation(Result, DAG);
2668 if (Tmp1.Val) {
2669 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002670 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002671 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002672 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2673 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002674 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002675 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002676 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002677 return RetVal;
2678 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002679 break;
2680 }
2681
Chris Lattner2c8086f2005-04-02 05:00:07 +00002682 // Since these produce multiple values, make sure to remember that we
2683 // legalized all of them.
2684 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2685 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2686 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002687 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002688
2689 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002690 case ISD::ADD:
2691 case ISD::SUB:
2692 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002693 case ISD::MULHS:
2694 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002695 case ISD::UDIV:
2696 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002697 case ISD::AND:
2698 case ISD::OR:
2699 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002700 case ISD::SHL:
2701 case ISD::SRL:
2702 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002703 case ISD::FADD:
2704 case ISD::FSUB:
2705 case ISD::FMUL:
2706 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002707 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002708 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002709 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2710 case Expand: assert(0 && "Not possible");
2711 case Legal:
2712 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2713 break;
2714 case Promote:
2715 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2716 break;
2717 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002718
2719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002720
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002721 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002722 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002723 case TargetLowering::Legal: break;
2724 case TargetLowering::Custom:
2725 Tmp1 = TLI.LowerOperation(Result, DAG);
2726 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002727 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002728 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002729 MVT::ValueType VT = Op.getValueType();
2730
2731 // See if multiply or divide can be lowered using two-result operations.
2732 SDVTList VTs = DAG.getVTList(VT, VT);
2733 if (Node->getOpcode() == ISD::MUL) {
2734 // We just need the low half of the multiply; try both the signed
2735 // and unsigned forms. If the target supports both SMUL_LOHI and
2736 // UMUL_LOHI, form a preference by checking which forms of plain
2737 // MULH it supports.
2738 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2739 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2740 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2741 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2742 unsigned OpToUse = 0;
2743 if (HasSMUL_LOHI && !HasMULHS) {
2744 OpToUse = ISD::SMUL_LOHI;
2745 } else if (HasUMUL_LOHI && !HasMULHU) {
2746 OpToUse = ISD::UMUL_LOHI;
2747 } else if (HasSMUL_LOHI) {
2748 OpToUse = ISD::SMUL_LOHI;
2749 } else if (HasUMUL_LOHI) {
2750 OpToUse = ISD::UMUL_LOHI;
2751 }
2752 if (OpToUse) {
2753 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2754 break;
2755 }
2756 }
2757 if (Node->getOpcode() == ISD::MULHS &&
2758 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2759 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2760 break;
2761 }
2762 if (Node->getOpcode() == ISD::MULHU &&
2763 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2764 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2765 break;
2766 }
2767 if (Node->getOpcode() == ISD::SDIV &&
2768 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2769 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2770 break;
2771 }
2772 if (Node->getOpcode() == ISD::UDIV &&
2773 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2774 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2775 break;
2776 }
2777
Dan Gohman82669522007-10-11 23:57:53 +00002778 // Check to see if we have a libcall for this operator.
2779 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2780 bool isSigned = false;
2781 switch (Node->getOpcode()) {
2782 case ISD::UDIV:
2783 case ISD::SDIV:
2784 if (VT == MVT::i32) {
2785 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002786 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002787 isSigned = Node->getOpcode() == ISD::SDIV;
2788 }
2789 break;
2790 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002791 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2792 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002793 break;
2794 default: break;
2795 }
2796 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2797 SDOperand Dummy;
2798 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002799 break;
2800 }
2801
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002802 assert(MVT::isVector(Node->getValueType(0)) &&
2803 "Cannot expand this binary operator!");
2804 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002805 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002806 break;
2807 }
Evan Chengcc987612006-04-12 21:20:24 +00002808 case TargetLowering::Promote: {
2809 switch (Node->getOpcode()) {
2810 default: assert(0 && "Do not know how to promote this BinOp!");
2811 case ISD::AND:
2812 case ISD::OR:
2813 case ISD::XOR: {
2814 MVT::ValueType OVT = Node->getValueType(0);
2815 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2816 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2817 // Bit convert each of the values to the new type.
2818 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2819 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2820 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2821 // Bit convert the result back the original type.
2822 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2823 break;
2824 }
2825 }
2826 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002827 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002828 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002829
Dan Gohmane14ea862007-10-05 14:17:22 +00002830 case ISD::SMUL_LOHI:
2831 case ISD::UMUL_LOHI:
2832 case ISD::SDIVREM:
2833 case ISD::UDIVREM:
2834 // These nodes will only be produced by target-specific lowering, so
2835 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002836 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002837 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002838
2839 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2840 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002842 break;
2843
Chris Lattnera09f8482006-03-05 05:09:38 +00002844 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2845 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2846 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2847 case Expand: assert(0 && "Not possible");
2848 case Legal:
2849 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2850 break;
2851 case Promote:
2852 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2853 break;
2854 }
2855
2856 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2857
2858 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2859 default: assert(0 && "Operation not supported");
2860 case TargetLowering::Custom:
2861 Tmp1 = TLI.LowerOperation(Result, DAG);
2862 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002863 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002864 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002865 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002866 // If this target supports fabs/fneg natively and select is cheap,
2867 // do this efficiently.
2868 if (!TLI.isSelectExpensive() &&
2869 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2870 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002871 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002872 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002873 // Get the sign bit of the RHS.
2874 MVT::ValueType IVT =
2875 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2876 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2877 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2878 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2879 // Get the absolute value of the result.
2880 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2881 // Select between the nabs and abs value based on the sign bit of
2882 // the input.
2883 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2884 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2885 AbsVal),
2886 AbsVal);
2887 Result = LegalizeOp(Result);
2888 break;
2889 }
2890
2891 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002892 MVT::ValueType NVT =
2893 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2894 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2895 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2896 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002897 break;
2898 }
Evan Cheng912095b2007-01-04 21:56:39 +00002899 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002900 break;
2901
Nate Begeman551bf3f2006-02-17 05:43:56 +00002902 case ISD::ADDC:
2903 case ISD::SUBC:
2904 Tmp1 = LegalizeOp(Node->getOperand(0));
2905 Tmp2 = LegalizeOp(Node->getOperand(1));
2906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2907 // Since this produces two values, make sure to remember that we legalized
2908 // both of them.
2909 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2910 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2911 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002912
Nate Begeman551bf3f2006-02-17 05:43:56 +00002913 case ISD::ADDE:
2914 case ISD::SUBE:
2915 Tmp1 = LegalizeOp(Node->getOperand(0));
2916 Tmp2 = LegalizeOp(Node->getOperand(1));
2917 Tmp3 = LegalizeOp(Node->getOperand(2));
2918 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2919 // Since this produces two values, make sure to remember that we legalized
2920 // both of them.
2921 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2922 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2923 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002924
Nate Begeman419f8b62005-10-18 00:27:41 +00002925 case ISD::BUILD_PAIR: {
2926 MVT::ValueType PairTy = Node->getValueType(0);
2927 // TODO: handle the case where the Lo and Hi operands are not of legal type
2928 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2929 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2930 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002931 case TargetLowering::Promote:
2932 case TargetLowering::Custom:
2933 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002934 case TargetLowering::Legal:
2935 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2936 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2937 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002938 case TargetLowering::Expand:
2939 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2940 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2941 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2942 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2943 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002944 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002945 break;
2946 }
2947 break;
2948 }
2949
Nate Begemanc105e192005-04-06 00:23:54 +00002950 case ISD::UREM:
2951 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002952 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002953 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2954 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002955
Nate Begemanc105e192005-04-06 00:23:54 +00002956 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002957 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2958 case TargetLowering::Custom:
2959 isCustom = true;
2960 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002961 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002962 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002963 if (isCustom) {
2964 Tmp1 = TLI.LowerOperation(Result, DAG);
2965 if (Tmp1.Val) Result = Tmp1;
2966 }
Nate Begemanc105e192005-04-06 00:23:54 +00002967 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002968 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002969 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002970 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002971 MVT::ValueType VT = Node->getValueType(0);
2972
2973 // See if remainder can be lowered using two-result operations.
2974 SDVTList VTs = DAG.getVTList(VT, VT);
2975 if (Node->getOpcode() == ISD::SREM &&
2976 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2977 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2978 break;
2979 }
2980 if (Node->getOpcode() == ISD::UREM &&
2981 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2982 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2983 break;
2984 }
2985
2986 if (MVT::isInteger(VT)) {
2987 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002988 TargetLowering::Legal) {
2989 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002990 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002991 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2992 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00002993 } else if (MVT::isVector(VT)) {
2994 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002995 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002996 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002997 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002998 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2999 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003000 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003001 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003002 }
Dan Gohman0d974262007-11-06 22:11:54 +00003003 } else {
3004 assert(MVT::isFloatingPoint(VT) &&
3005 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003006 if (MVT::isVector(VT)) {
3007 Result = LegalizeOp(UnrollVectorOp(Op));
3008 } else {
3009 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003010 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3011 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003012 SDOperand Dummy;
3013 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3014 false/*sign irrelevant*/, Dummy);
3015 }
Nate Begemanc105e192005-04-06 00:23:54 +00003016 }
3017 break;
3018 }
Dan Gohman525178c2007-10-08 18:33:35 +00003019 }
Nate Begemanc105e192005-04-06 00:23:54 +00003020 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003021 case ISD::VAARG: {
3022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3023 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3024
Chris Lattner5c62f332006-01-28 07:42:08 +00003025 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003026 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3027 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003028 case TargetLowering::Custom:
3029 isCustom = true;
3030 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003031 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3033 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003034 Tmp1 = Result.getValue(1);
3035
3036 if (isCustom) {
3037 Tmp2 = TLI.LowerOperation(Result, DAG);
3038 if (Tmp2.Val) {
3039 Result = LegalizeOp(Tmp2);
3040 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3041 }
3042 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003043 break;
3044 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003045 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003046 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003047 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003048 // Increment the pointer, VAList, to the next vaarg
3049 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3050 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3051 TLI.getPointerTy()));
3052 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003053 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3054 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003055 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003056 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003057 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003058 Result = LegalizeOp(Result);
3059 break;
3060 }
3061 }
3062 // Since VAARG produces two values, make sure to remember that we
3063 // legalized both of them.
3064 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003065 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3066 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003067 }
3068
3069 case ISD::VACOPY:
3070 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3071 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3072 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3073
3074 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3075 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003076 case TargetLowering::Custom:
3077 isCustom = true;
3078 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003079 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3081 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003082 if (isCustom) {
3083 Tmp1 = TLI.LowerOperation(Result, DAG);
3084 if (Tmp1.Val) Result = Tmp1;
3085 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003086 break;
3087 case TargetLowering::Expand:
3088 // This defaults to loading a pointer from the input and storing it to the
3089 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003090 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003091 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003092 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3093 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003094 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3095 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003096 break;
3097 }
3098 break;
3099
3100 case ISD::VAEND:
3101 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3102 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3103
3104 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3105 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003106 case TargetLowering::Custom:
3107 isCustom = true;
3108 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003109 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003110 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003111 if (isCustom) {
3112 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3113 if (Tmp1.Val) Result = Tmp1;
3114 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003115 break;
3116 case TargetLowering::Expand:
3117 Result = Tmp1; // Default to a no-op, return the chain
3118 break;
3119 }
3120 break;
3121
3122 case ISD::VASTART:
3123 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3124 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3125
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3127
Nate Begemanacc398c2006-01-25 18:21:52 +00003128 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3129 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003130 case TargetLowering::Legal: break;
3131 case TargetLowering::Custom:
3132 Tmp1 = TLI.LowerOperation(Result, DAG);
3133 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003134 break;
3135 }
3136 break;
3137
Nate Begeman35ef9132006-01-11 21:21:00 +00003138 case ISD::ROTL:
3139 case ISD::ROTR:
3140 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3141 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003142 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003143 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3144 default:
3145 assert(0 && "ROTL/ROTR legalize operation not supported");
3146 break;
3147 case TargetLowering::Legal:
3148 break;
3149 case TargetLowering::Custom:
3150 Tmp1 = TLI.LowerOperation(Result, DAG);
3151 if (Tmp1.Val) Result = Tmp1;
3152 break;
3153 case TargetLowering::Promote:
3154 assert(0 && "Do not know how to promote ROTL/ROTR");
3155 break;
3156 case TargetLowering::Expand:
3157 assert(0 && "Do not know how to expand ROTL/ROTR");
3158 break;
3159 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003160 break;
3161
Nate Begemand88fc032006-01-14 03:14:10 +00003162 case ISD::BSWAP:
3163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3164 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003165 case TargetLowering::Custom:
3166 assert(0 && "Cannot custom legalize this yet!");
3167 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003168 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003169 break;
3170 case TargetLowering::Promote: {
3171 MVT::ValueType OVT = Tmp1.getValueType();
3172 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003173 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003174
Chris Lattner456a93a2006-01-28 07:39:30 +00003175 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3176 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3177 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3178 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3179 break;
3180 }
3181 case TargetLowering::Expand:
3182 Result = ExpandBSWAP(Tmp1);
3183 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003184 }
3185 break;
3186
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003187 case ISD::CTPOP:
3188 case ISD::CTTZ:
3189 case ISD::CTLZ:
3190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3191 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003192 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003193 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003194 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003195 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003196 TargetLowering::Custom) {
3197 Tmp1 = TLI.LowerOperation(Result, DAG);
3198 if (Tmp1.Val) {
3199 Result = Tmp1;
3200 }
Scott Michel910b66d2007-07-30 21:00:31 +00003201 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003202 break;
3203 case TargetLowering::Promote: {
3204 MVT::ValueType OVT = Tmp1.getValueType();
3205 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003206
3207 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003208 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3209 // Perform the larger operation, then subtract if needed.
3210 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003211 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003212 case ISD::CTPOP:
3213 Result = Tmp1;
3214 break;
3215 case ISD::CTTZ:
3216 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003217 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003218 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003219 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003220 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003221 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003222 break;
3223 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003224 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003225 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003226 DAG.getConstant(MVT::getSizeInBits(NVT) -
3227 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003228 break;
3229 }
3230 break;
3231 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003232 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003233 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003234 break;
3235 }
3236 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003237
Chris Lattner2c8086f2005-04-02 05:00:07 +00003238 // Unary operators
3239 case ISD::FABS:
3240 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003241 case ISD::FSQRT:
3242 case ISD::FSIN:
3243 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003244 Tmp1 = LegalizeOp(Node->getOperand(0));
3245 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003246 case TargetLowering::Promote:
3247 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003248 isCustom = true;
3249 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003250 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003251 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003252 if (isCustom) {
3253 Tmp1 = TLI.LowerOperation(Result, DAG);
3254 if (Tmp1.Val) Result = Tmp1;
3255 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003256 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003257 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003258 switch (Node->getOpcode()) {
3259 default: assert(0 && "Unreachable!");
3260 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003261 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3262 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003263 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003264 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003265 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003266 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3267 MVT::ValueType VT = Node->getValueType(0);
3268 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003269 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003270 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3271 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003272 break;
3273 }
3274 case ISD::FSQRT:
3275 case ISD::FSIN:
3276 case ISD::FCOS: {
3277 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003278
3279 // Expand unsupported unary vector operators by unrolling them.
3280 if (MVT::isVector(VT)) {
3281 Result = LegalizeOp(UnrollVectorOp(Op));
3282 break;
3283 }
3284
Evan Cheng56966222007-01-12 02:11:51 +00003285 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003286 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003287 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003288 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3289 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003290 break;
3291 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003292 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3293 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003294 break;
3295 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003296 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3297 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003298 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003299 default: assert(0 && "Unreachable!");
3300 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003301 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003302 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3303 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003304 break;
3305 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003306 }
3307 break;
3308 }
3309 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003310 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003311 MVT::ValueType VT = Node->getValueType(0);
3312
3313 // Expand unsupported unary vector operators by unrolling them.
3314 if (MVT::isVector(VT)) {
3315 Result = LegalizeOp(UnrollVectorOp(Op));
3316 break;
3317 }
3318
3319 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003320 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3321 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003322 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003323 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3324 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003325 break;
3326 }
Chris Lattner35481892005-12-23 00:16:34 +00003327 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003328 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003329 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003330 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3331 // The input has to be a vector type, we have to either scalarize it, pack
3332 // it, or convert it based on whether the input vector type is legal.
3333 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003334 int InIx = Node->getOperand(0).ResNo;
3335 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3336 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003337
3338 // Figure out if there is a simple type corresponding to this Vector
3339 // type. If so, convert to the vector type.
3340 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3341 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003342 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003343 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3344 LegalizeOp(Node->getOperand(0)));
3345 break;
3346 } else if (NumElems == 1) {
3347 // Turn this into a bit convert of the scalar input.
3348 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3349 ScalarizeVectorOp(Node->getOperand(0)));
3350 break;
3351 } else {
3352 // FIXME: UNIMP! Store then reload
3353 assert(0 && "Cast from unsupported vector type not implemented yet!");
3354 }
Chris Lattner67993f72006-01-23 07:30:46 +00003355 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003356 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3357 Node->getOperand(0).getValueType())) {
3358 default: assert(0 && "Unknown operation action!");
3359 case TargetLowering::Expand:
3360 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3361 break;
3362 case TargetLowering::Legal:
3363 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003364 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003365 break;
3366 }
3367 }
3368 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003369
Chris Lattner2c8086f2005-04-02 05:00:07 +00003370 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003371 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003372 case ISD::UINT_TO_FP: {
3373 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003374 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3375 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003376 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003377 Node->getOperand(0).getValueType())) {
3378 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003379 case TargetLowering::Custom:
3380 isCustom = true;
3381 // FALLTHROUGH
3382 case TargetLowering::Legal:
3383 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003384 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003385 if (isCustom) {
3386 Tmp1 = TLI.LowerOperation(Result, DAG);
3387 if (Tmp1.Val) Result = Tmp1;
3388 }
3389 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003390 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003391 Result = ExpandLegalINT_TO_FP(isSigned,
3392 LegalizeOp(Node->getOperand(0)),
3393 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003394 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003395 case TargetLowering::Promote:
3396 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3397 Node->getValueType(0),
3398 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003399 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003400 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003401 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003402 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003403 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3404 Node->getValueType(0), Node->getOperand(0));
3405 break;
3406 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003407 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003408 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003409 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3410 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003411 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003412 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3413 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003414 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003415 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3416 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003417 break;
3418 }
3419 break;
3420 }
3421 case ISD::TRUNCATE:
3422 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3423 case Legal:
3424 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003425 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003426 break;
3427 case Expand:
3428 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3429
3430 // Since the result is legal, we should just be able to truncate the low
3431 // part of the source.
3432 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3433 break;
3434 case Promote:
3435 Result = PromoteOp(Node->getOperand(0));
3436 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3437 break;
3438 }
3439 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003440
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003441 case ISD::FP_TO_SINT:
3442 case ISD::FP_TO_UINT:
3443 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3444 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003445 Tmp1 = LegalizeOp(Node->getOperand(0));
3446
Chris Lattner1618beb2005-07-29 00:11:56 +00003447 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3448 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003449 case TargetLowering::Custom:
3450 isCustom = true;
3451 // FALLTHROUGH
3452 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003453 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003454 if (isCustom) {
3455 Tmp1 = TLI.LowerOperation(Result, DAG);
3456 if (Tmp1.Val) Result = Tmp1;
3457 }
3458 break;
3459 case TargetLowering::Promote:
3460 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3461 Node->getOpcode() == ISD::FP_TO_SINT);
3462 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003463 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003464 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3465 SDOperand True, False;
3466 MVT::ValueType VT = Node->getOperand(0).getValueType();
3467 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003468 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003469 const uint64_t zero[] = {0, 0};
3470 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3471 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003472 (void)apf.convertFromZeroExtendedInteger
3473 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003474 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003475 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3476 Node->getOperand(0), Tmp2, ISD::SETLT);
3477 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3478 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003479 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003480 Tmp2));
3481 False = DAG.getNode(ISD::XOR, NVT, False,
3482 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003483 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3484 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003485 } else {
3486 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3487 }
3488 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003489 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003490 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003491 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003492 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003493 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003494 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003495 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003496 if (Node->getOpcode()==ISD::FP_TO_SINT)
3497 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003498 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3499 (DAG.getNode(ISD::FP_ROUND_INREG,
3500 MVT::ppcf128, Node->getOperand(0),
3501 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003502 else {
3503 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3504 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3505 Tmp2 = DAG.getConstantFP(apf, OVT);
3506 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3507 // FIXME: generated code sucks.
3508 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3509 DAG.getNode(ISD::ADD, MVT::i32,
3510 DAG.getNode(ISD::FP_TO_SINT, VT,
3511 DAG.getNode(ISD::FSUB, OVT,
3512 Node->getOperand(0), Tmp2)),
3513 DAG.getConstant(0x80000000, MVT::i32)),
3514 DAG.getNode(ISD::FP_TO_SINT, VT,
3515 Node->getOperand(0)),
3516 DAG.getCondCode(ISD::SETGE));
3517 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003518 break;
3519 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003520 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003521 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003522 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003523 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003524 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003525 LC = (VT == MVT::i32)
3526 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003527 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003528 LC = (VT == MVT::i32)
3529 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003530 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003531 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003532 LC = RTLIB::FPTOSINT_F80_I64;
3533 }
3534 else if (OVT == MVT::ppcf128) {
3535 assert(VT == MVT::i64);
3536 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003537 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003538 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003539 }
3540 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003541 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003542 LC = (VT == MVT::i32)
3543 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003544 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003545 LC = (VT == MVT::i32)
3546 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003547 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003548 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003549 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3550 }
3551 else if (OVT == MVT::ppcf128) {
3552 assert(VT == MVT::i64);
3553 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003554 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003555 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003556 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003557 default: assert(0 && "Unreachable!");
3558 }
3559 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003560 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3561 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003562 break;
3563 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003564 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003565 Tmp1 = PromoteOp(Node->getOperand(0));
3566 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3567 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003568 break;
3569 }
3570 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003571
Chris Lattnerf2670a82008-01-16 06:57:07 +00003572 case ISD::FP_EXTEND: {
3573 MVT::ValueType newVT = Op.getValueType();
3574 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3575 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
3576 // The only other way we can lower this is to turn it into a STORE,
3577 // LOAD pair, targetting a temporary location (a stack slot).
3578
3579 // NOTE: there is a choice here between constantly creating new stack
3580 // slots and always reusing the same one. We currently always create
3581 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003582 SDOperand StackSlot = DAG.CreateStackTemporary(oldVT);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003583 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3584 StackSlot, NULL, 0);
3585 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3586 Result, StackSlot, NULL, 0, oldVT);
3587 break;
3588 }
3589 }
3590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3591 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3592 case Legal:
3593 Tmp1 = LegalizeOp(Node->getOperand(0));
3594 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3595 break;
3596 case Promote:
3597 Tmp1 = PromoteOp(Node->getOperand(0));
3598 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3599 break;
3600 }
3601 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003602 case ISD::FP_ROUND: {
3603 MVT::ValueType newVT = Op.getValueType();
3604 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3605 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Chris Lattnerf2670a82008-01-16 06:57:07 +00003606 if (oldVT == MVT::ppcf128) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003607 SDOperand Lo, Hi;
3608 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003609 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003610 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003611 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003612 // The only other way we can lower this is to turn it into a STORE,
3613 // LOAD pair, targetting a temporary location (a stack slot).
3614
3615 // NOTE: there is a choice here between constantly creating new stack
3616 // slots and always reusing the same one. We currently always create
3617 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003618 SDOperand StackSlot = DAG.CreateStackTemporary(newVT);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003619 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3620 StackSlot, NULL, 0, newVT);
3621 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003622 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003623 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003624 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003625 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003626 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3627 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3628 case Legal:
3629 Tmp1 = LegalizeOp(Node->getOperand(0));
3630 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3631 break;
3632 case Promote:
3633 Tmp1 = PromoteOp(Node->getOperand(0));
3634 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1);
3635 break;
3636 }
3637 break;
Chris Lattner13c78e22005-09-02 00:18:10 +00003638 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003639 case ISD::ZERO_EXTEND:
3640 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003641 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003642 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003643 case Legal:
3644 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003645 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003646 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003647 case Promote:
3648 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003649 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003650 Tmp1 = PromoteOp(Node->getOperand(0));
3651 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003652 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003653 case ISD::ZERO_EXTEND:
3654 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003655 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003656 Result = DAG.getZeroExtendInReg(Result,
3657 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003658 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003659 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003660 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003661 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003662 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003663 Result,
3664 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003665 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003666 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003667 }
3668 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003669 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003670 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003671 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003672 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003673
3674 // If this operation is not supported, convert it to a shl/shr or load/store
3675 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003676 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3677 default: assert(0 && "This action not supported for this op yet!");
3678 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003679 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003680 break;
3681 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003682 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003683 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003684 // NOTE: we could fall back on load/store here too for targets without
3685 // SAR. However, it is doubtful that any exist.
3686 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3687 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003688 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003689 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3690 Node->getOperand(0), ShiftCst);
3691 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3692 Result, ShiftCst);
3693 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003694 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003695 // EXTLOAD pair, targetting a temporary location (a stack slot).
3696
3697 // NOTE: there is a choice here between constantly creating new stack
3698 // slots and always reusing the same one. We currently always create
3699 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003700 SDOperand StackSlot = DAG.CreateStackTemporary(ExtraVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003701 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3702 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003703 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003704 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003705 } else {
3706 assert(0 && "Unknown op");
3707 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003708 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003709 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003710 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003711 }
Duncan Sands36397f52007-07-27 12:58:54 +00003712 case ISD::TRAMPOLINE: {
3713 SDOperand Ops[6];
3714 for (unsigned i = 0; i != 6; ++i)
3715 Ops[i] = LegalizeOp(Node->getOperand(i));
3716 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3717 // The only option for this node is to custom lower it.
3718 Result = TLI.LowerOperation(Result, DAG);
3719 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003720
3721 // Since trampoline produces two values, make sure to remember that we
3722 // legalized both of them.
3723 Tmp1 = LegalizeOp(Result.getValue(1));
3724 Result = LegalizeOp(Result);
3725 AddLegalizedOperand(SDOperand(Node, 0), Result);
3726 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3727 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003728 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003729 case ISD::FLT_ROUNDS: {
3730 MVT::ValueType VT = Node->getValueType(0);
3731 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3732 default: assert(0 && "This action not supported for this op yet!");
3733 case TargetLowering::Custom:
3734 Result = TLI.LowerOperation(Op, DAG);
3735 if (Result.Val) break;
3736 // Fall Thru
3737 case TargetLowering::Legal:
3738 // If this operation is not supported, lower it to constant 1
3739 Result = DAG.getConstant(1, VT);
3740 break;
3741 }
3742 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003743 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003744 MVT::ValueType VT = Node->getValueType(0);
3745 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3746 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003747 case TargetLowering::Legal:
3748 Tmp1 = LegalizeOp(Node->getOperand(0));
3749 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3750 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003751 case TargetLowering::Custom:
3752 Result = TLI.LowerOperation(Op, DAG);
3753 if (Result.Val) break;
3754 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003755 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003756 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003757 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003758 TargetLowering::ArgListTy Args;
3759 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner41bab0b2008-01-15 21:58:08 +00003760 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003761 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3762 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003763 Result = CallResult.second;
3764 break;
3765 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003766 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003767 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003768 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003769
Chris Lattner4ddd2832006-04-08 04:13:17 +00003770 assert(Result.getValueType() == Op.getValueType() &&
3771 "Bad legalization!");
3772
Chris Lattner456a93a2006-01-28 07:39:30 +00003773 // Make sure that the generated code is itself legal.
3774 if (Result != Op)
3775 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003776
Chris Lattner45982da2005-05-12 16:53:42 +00003777 // Note that LegalizeOp may be reentered even from single-use nodes, which
3778 // means that we always must cache transformed nodes.
3779 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003780 return Result;
3781}
3782
Chris Lattner8b6fa222005-01-15 22:16:26 +00003783/// PromoteOp - Given an operation that produces a value in an invalid type,
3784/// promote it to compute the value into a larger type. The produced value will
3785/// have the correct bits for the low portion of the register, but no guarantee
3786/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003787SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3788 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003789 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003790 assert(getTypeAction(VT) == Promote &&
3791 "Caller should expand or legalize operands that are not promotable!");
3792 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3793 "Cannot promote to smaller type!");
3794
Chris Lattner03c85462005-01-15 05:21:40 +00003795 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003796 SDOperand Result;
3797 SDNode *Node = Op.Val;
3798
Chris Lattner40030bf2007-02-04 01:17:38 +00003799 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003800 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003801
Chris Lattner03c85462005-01-15 05:21:40 +00003802 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003803 case ISD::CopyFromReg:
3804 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003805 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003806#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003807 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003808#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003809 assert(0 && "Do not know how to promote this operator!");
3810 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003811 case ISD::UNDEF:
3812 Result = DAG.getNode(ISD::UNDEF, NVT);
3813 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003814 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003815 if (VT != MVT::i1)
3816 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3817 else
3818 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003819 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3820 break;
3821 case ISD::ConstantFP:
3822 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3823 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3824 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003825
Chris Lattner82fbfb62005-01-18 02:59:52 +00003826 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003827 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003828 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3829 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003830 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003831
Chris Lattner03c85462005-01-15 05:21:40 +00003832 case ISD::TRUNCATE:
3833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3834 case Legal:
3835 Result = LegalizeOp(Node->getOperand(0));
3836 assert(Result.getValueType() >= NVT &&
3837 "This truncation doesn't make sense!");
3838 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3839 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3840 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003841 case Promote:
3842 // The truncation is not required, because we don't guarantee anything
3843 // about high bits anyway.
3844 Result = PromoteOp(Node->getOperand(0));
3845 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003846 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003847 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3848 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003849 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003850 }
3851 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003852 case ISD::SIGN_EXTEND:
3853 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003854 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003855 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3856 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3857 case Legal:
3858 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003859 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003860 break;
3861 case Promote:
3862 // Promote the reg if it's smaller.
3863 Result = PromoteOp(Node->getOperand(0));
3864 // The high bits are not guaranteed to be anything. Insert an extend.
3865 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003866 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003867 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003868 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003869 Result = DAG.getZeroExtendInReg(Result,
3870 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003871 break;
3872 }
3873 break;
Chris Lattner35481892005-12-23 00:16:34 +00003874 case ISD::BIT_CONVERT:
3875 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3876 Result = PromoteOp(Result);
3877 break;
3878
Chris Lattner8b6fa222005-01-15 22:16:26 +00003879 case ISD::FP_EXTEND:
3880 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3881 case ISD::FP_ROUND:
3882 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3883 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3884 case Promote: assert(0 && "Unreachable with 2 FP types!");
3885 case Legal:
3886 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003887 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003888 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003889 break;
3890 }
3891 break;
3892
3893 case ISD::SINT_TO_FP:
3894 case ISD::UINT_TO_FP:
3895 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3896 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003897 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003898 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003899 break;
3900
3901 case Promote:
3902 Result = PromoteOp(Node->getOperand(0));
3903 if (Node->getOpcode() == ISD::SINT_TO_FP)
3904 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003905 Result,
3906 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003907 else
Chris Lattner23993562005-04-13 02:38:47 +00003908 Result = DAG.getZeroExtendInReg(Result,
3909 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003910 // No extra round required here.
3911 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003912 break;
3913 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003914 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3915 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003916 // Round if we cannot tolerate excess precision.
3917 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003918 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3919 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003920 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003921 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003922 break;
3923
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003924 case ISD::SIGN_EXTEND_INREG:
3925 Result = PromoteOp(Node->getOperand(0));
3926 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3927 Node->getOperand(1));
3928 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003929 case ISD::FP_TO_SINT:
3930 case ISD::FP_TO_UINT:
3931 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3932 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003933 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003934 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003935 break;
3936 case Promote:
3937 // The input result is prerounded, so we don't have to do anything
3938 // special.
3939 Tmp1 = PromoteOp(Node->getOperand(0));
3940 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003941 }
Nate Begemand2558e32005-08-14 01:20:53 +00003942 // If we're promoting a UINT to a larger size, check to see if the new node
3943 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3944 // we can use that instead. This allows us to generate better code for
3945 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3946 // legal, such as PowerPC.
3947 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003948 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003949 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3950 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003951 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3952 } else {
3953 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3954 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003955 break;
3956
Chris Lattner2c8086f2005-04-02 05:00:07 +00003957 case ISD::FABS:
3958 case ISD::FNEG:
3959 Tmp1 = PromoteOp(Node->getOperand(0));
3960 assert(Tmp1.getValueType() == NVT);
3961 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3962 // NOTE: we do not have to do any extra rounding here for
3963 // NoExcessFPPrecision, because we know the input will have the appropriate
3964 // precision, and these operations don't modify precision at all.
3965 break;
3966
Chris Lattnerda6ba872005-04-28 21:44:33 +00003967 case ISD::FSQRT:
3968 case ISD::FSIN:
3969 case ISD::FCOS:
3970 Tmp1 = PromoteOp(Node->getOperand(0));
3971 assert(Tmp1.getValueType() == NVT);
3972 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003973 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003974 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3975 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003976 break;
3977
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003978 case ISD::FPOWI: {
3979 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3980 // directly as well, which may be better.
3981 Tmp1 = PromoteOp(Node->getOperand(0));
3982 assert(Tmp1.getValueType() == NVT);
3983 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3984 if (NoExcessFPPrecision)
3985 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3986 DAG.getValueType(VT));
3987 break;
3988 }
3989
Chris Lattner03c85462005-01-15 05:21:40 +00003990 case ISD::AND:
3991 case ISD::OR:
3992 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003993 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003994 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003995 case ISD::MUL:
3996 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003997 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003998 // that too is okay if they are integer operations.
3999 Tmp1 = PromoteOp(Node->getOperand(0));
4000 Tmp2 = PromoteOp(Node->getOperand(1));
4001 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4002 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004003 break;
4004 case ISD::FADD:
4005 case ISD::FSUB:
4006 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004007 Tmp1 = PromoteOp(Node->getOperand(0));
4008 Tmp2 = PromoteOp(Node->getOperand(1));
4009 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4010 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4011
4012 // Floating point operations will give excess precision that we may not be
4013 // able to tolerate. If we DO allow excess precision, just leave it,
4014 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004015 // FIXME: Why would we need to round FP ops more than integer ones?
4016 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004017 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004018 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4019 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004020 break;
4021
Chris Lattner8b6fa222005-01-15 22:16:26 +00004022 case ISD::SDIV:
4023 case ISD::SREM:
4024 // These operators require that their input be sign extended.
4025 Tmp1 = PromoteOp(Node->getOperand(0));
4026 Tmp2 = PromoteOp(Node->getOperand(1));
4027 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004028 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4029 DAG.getValueType(VT));
4030 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4031 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004032 }
4033 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4034
4035 // Perform FP_ROUND: this is probably overly pessimistic.
4036 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004037 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4038 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004039 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004040 case ISD::FDIV:
4041 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004042 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004043 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004044 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4045 case Legal:
4046 Tmp1 = LegalizeOp(Node->getOperand(0));
4047 break;
4048 case Promote:
4049 Tmp1 = PromoteOp(Node->getOperand(0));
4050 break;
4051 case Expand:
4052 assert(0 && "not implemented");
4053 }
4054 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4055 case Legal:
4056 Tmp2 = LegalizeOp(Node->getOperand(1));
4057 break;
4058 case Promote:
4059 Tmp2 = PromoteOp(Node->getOperand(1));
4060 break;
4061 case Expand:
4062 assert(0 && "not implemented");
4063 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004064 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4065
4066 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004067 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004068 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4069 DAG.getValueType(VT));
4070 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004071
4072 case ISD::UDIV:
4073 case ISD::UREM:
4074 // These operators require that their input be zero extended.
4075 Tmp1 = PromoteOp(Node->getOperand(0));
4076 Tmp2 = PromoteOp(Node->getOperand(1));
4077 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004078 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4079 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004080 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4081 break;
4082
4083 case ISD::SHL:
4084 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004085 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004086 break;
4087 case ISD::SRA:
4088 // The input value must be properly sign extended.
4089 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004090 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4091 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004092 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004093 break;
4094 case ISD::SRL:
4095 // The input value must be properly zero extended.
4096 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004097 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004098 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004099 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004100
4101 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004102 Tmp1 = Node->getOperand(0); // Get the chain.
4103 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004104 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4105 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4106 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4107 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004108 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004109 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004110 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004111 // Increment the pointer, VAList, to the next vaarg
4112 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4113 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4114 TLI.getPointerTy()));
4115 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004116 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4117 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004118 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004119 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004120 }
4121 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004122 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004123 break;
4124
Evan Cheng466685d2006-10-09 20:57:25 +00004125 case ISD::LOAD: {
4126 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004127 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4128 ? ISD::EXTLOAD : LD->getExtensionType();
4129 Result = DAG.getExtLoad(ExtType, NVT,
4130 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004131 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004132 LD->getLoadedVT(),
4133 LD->isVolatile(),
4134 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004135 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004136 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004137 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004138 }
Chris Lattner03c85462005-01-15 05:21:40 +00004139 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004140 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4141 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004142 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004143 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004144 case ISD::SELECT_CC:
4145 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4146 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4147 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004148 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004149 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004150 case ISD::BSWAP:
4151 Tmp1 = Node->getOperand(0);
4152 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4153 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4154 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004155 DAG.getConstant(MVT::getSizeInBits(NVT) -
4156 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004157 TLI.getShiftAmountTy()));
4158 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004159 case ISD::CTPOP:
4160 case ISD::CTTZ:
4161 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004162 // Zero extend the argument
4163 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004164 // Perform the larger operation, then subtract if needed.
4165 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004166 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004167 case ISD::CTPOP:
4168 Result = Tmp1;
4169 break;
4170 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004171 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004172 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004173 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4174 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004175 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004176 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004177 break;
4178 case ISD::CTLZ:
4179 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004180 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004181 DAG.getConstant(MVT::getSizeInBits(NVT) -
4182 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004183 break;
4184 }
4185 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004186 case ISD::EXTRACT_SUBVECTOR:
4187 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004188 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004189 case ISD::EXTRACT_VECTOR_ELT:
4190 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4191 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004192 }
4193
4194 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004195
4196 // Make sure the result is itself legal.
4197 Result = LegalizeOp(Result);
4198
4199 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004200 AddPromotedOperand(Op, Result);
4201 return Result;
4202}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004203
Dan Gohman7f321562007-06-25 16:23:39 +00004204/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4205/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4206/// based on the vector type. The return type of this matches the element type
4207/// of the vector, which may not be legal for the target.
4208SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004209 // We know that operand #0 is the Vec vector. If the index is a constant
4210 // or if the invec is a supported hardware type, we can use it. Otherwise,
4211 // lower to a store then an indexed load.
4212 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004213 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004214
Dan Gohmane40c7b02007-09-24 15:54:53 +00004215 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004216 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004217
Dan Gohman7f321562007-06-25 16:23:39 +00004218 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4219 default: assert(0 && "This action is not supported yet!");
4220 case TargetLowering::Custom: {
4221 Vec = LegalizeOp(Vec);
4222 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4223 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4224 if (Tmp3.Val)
4225 return Tmp3;
4226 break;
4227 }
4228 case TargetLowering::Legal:
4229 if (isTypeLegal(TVT)) {
4230 Vec = LegalizeOp(Vec);
4231 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004232 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004233 }
4234 break;
4235 case TargetLowering::Expand:
4236 break;
4237 }
4238
4239 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004240 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004241 Op = ScalarizeVectorOp(Vec);
4242 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4243 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004244 SDOperand Lo, Hi;
4245 SplitVectorOp(Vec, Lo, Hi);
4246 if (CIdx->getValue() < NumElems/2) {
4247 Vec = Lo;
4248 } else {
4249 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004250 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4251 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004252 }
Dan Gohman7f321562007-06-25 16:23:39 +00004253
Chris Lattner15972212006-03-31 17:55:51 +00004254 // It's now an extract from the appropriate high or low part. Recurse.
4255 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004256 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004257 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004258 // Store the value to a temporary stack slot, then LOAD the scalar
4259 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004260 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004261 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4262
4263 // Add the offset to the index.
4264 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4265 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4266 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004267
4268 if (MVT::getSizeInBits(Idx.getValueType()) >
4269 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004270 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004271 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004272 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004273
Dan Gohman7f321562007-06-25 16:23:39 +00004274 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4275
4276 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004277 }
Dan Gohman7f321562007-06-25 16:23:39 +00004278 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004279}
4280
Dan Gohman7f321562007-06-25 16:23:39 +00004281/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004282/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004283SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004284 // We know that operand #0 is the Vec vector. For now we assume the index
4285 // is a constant and that the extracted result is a supported hardware type.
4286 SDOperand Vec = Op.getOperand(0);
4287 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4288
Dan Gohman7f321562007-06-25 16:23:39 +00004289 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004290
4291 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4292 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004293 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004294 }
4295
4296 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4297 SDOperand Lo, Hi;
4298 SplitVectorOp(Vec, Lo, Hi);
4299 if (CIdx->getValue() < NumElems/2) {
4300 Vec = Lo;
4301 } else {
4302 Vec = Hi;
4303 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4304 }
4305
4306 // It's now an extract from the appropriate high or low part. Recurse.
4307 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004308 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004309}
4310
Nate Begeman750ac1b2006-02-01 07:19:44 +00004311/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4312/// with condition CC on the current target. This usually involves legalizing
4313/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4314/// there may be no choice but to create a new SetCC node to represent the
4315/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4316/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4317void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4318 SDOperand &RHS,
4319 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004320 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004321
4322 switch (getTypeAction(LHS.getValueType())) {
4323 case Legal:
4324 Tmp1 = LegalizeOp(LHS); // LHS
4325 Tmp2 = LegalizeOp(RHS); // RHS
4326 break;
4327 case Promote:
4328 Tmp1 = PromoteOp(LHS); // LHS
4329 Tmp2 = PromoteOp(RHS); // RHS
4330
4331 // If this is an FP compare, the operands have already been extended.
4332 if (MVT::isInteger(LHS.getValueType())) {
4333 MVT::ValueType VT = LHS.getValueType();
4334 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4335
4336 // Otherwise, we have to insert explicit sign or zero extends. Note
4337 // that we could insert sign extends for ALL conditions, but zero extend
4338 // is cheaper on many machines (an AND instead of two shifts), so prefer
4339 // it.
4340 switch (cast<CondCodeSDNode>(CC)->get()) {
4341 default: assert(0 && "Unknown integer comparison!");
4342 case ISD::SETEQ:
4343 case ISD::SETNE:
4344 case ISD::SETUGE:
4345 case ISD::SETUGT:
4346 case ISD::SETULE:
4347 case ISD::SETULT:
4348 // ALL of these operations will work if we either sign or zero extend
4349 // the operands (including the unsigned comparisons!). Zero extend is
4350 // usually a simpler/cheaper operation, so prefer it.
4351 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4352 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4353 break;
4354 case ISD::SETGE:
4355 case ISD::SETGT:
4356 case ISD::SETLT:
4357 case ISD::SETLE:
4358 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4359 DAG.getValueType(VT));
4360 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4361 DAG.getValueType(VT));
4362 break;
4363 }
4364 }
4365 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004366 case Expand: {
4367 MVT::ValueType VT = LHS.getValueType();
4368 if (VT == MVT::f32 || VT == MVT::f64) {
4369 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004370 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004371 switch (cast<CondCodeSDNode>(CC)->get()) {
4372 case ISD::SETEQ:
4373 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004374 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004375 break;
4376 case ISD::SETNE:
4377 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004378 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004379 break;
4380 case ISD::SETGE:
4381 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004382 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004383 break;
4384 case ISD::SETLT:
4385 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004386 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004387 break;
4388 case ISD::SETLE:
4389 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004390 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004391 break;
4392 case ISD::SETGT:
4393 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004394 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004395 break;
4396 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004397 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4398 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004399 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004400 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004401 break;
4402 default:
Evan Cheng56966222007-01-12 02:11:51 +00004403 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004404 switch (cast<CondCodeSDNode>(CC)->get()) {
4405 case ISD::SETONE:
4406 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004407 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004408 // Fallthrough
4409 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004410 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004411 break;
4412 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004413 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004414 break;
4415 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004416 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004417 break;
4418 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004419 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004420 break;
Evan Cheng56966222007-01-12 02:11:51 +00004421 case ISD::SETUEQ:
4422 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004423 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004424 default: assert(0 && "Unsupported FP setcc!");
4425 }
4426 }
4427
4428 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004429 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004430 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004431 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004432 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004433 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004434 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004435 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004436 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004437 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004438 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004439 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004440 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004441 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4442 Tmp2 = SDOperand();
4443 }
4444 LHS = Tmp1;
4445 RHS = Tmp2;
4446 return;
4447 }
4448
Nate Begeman750ac1b2006-02-01 07:19:44 +00004449 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4450 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004451 ExpandOp(RHS, RHSLo, RHSHi);
4452 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4453
4454 if (VT==MVT::ppcf128) {
4455 // FIXME: This generated code sucks. We want to generate
4456 // FCMP crN, hi1, hi2
4457 // BNE crN, L:
4458 // FCMP crN, lo1, lo2
4459 // The following can be improved, but not that much.
4460 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4461 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4462 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4463 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4464 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4465 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4466 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4467 Tmp2 = SDOperand();
4468 break;
4469 }
4470
4471 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004472 case ISD::SETEQ:
4473 case ISD::SETNE:
4474 if (RHSLo == RHSHi)
4475 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4476 if (RHSCST->isAllOnesValue()) {
4477 // Comparison to -1.
4478 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4479 Tmp2 = RHSLo;
4480 break;
4481 }
4482
4483 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4484 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4485 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4486 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4487 break;
4488 default:
4489 // If this is a comparison of the sign bit, just look at the top part.
4490 // X > -1, x < 0
4491 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4492 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4493 CST->getValue() == 0) || // X < 0
4494 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4495 CST->isAllOnesValue())) { // X > -1
4496 Tmp1 = LHSHi;
4497 Tmp2 = RHSHi;
4498 break;
4499 }
4500
4501 // FIXME: This generated code sucks.
4502 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004503 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004504 default: assert(0 && "Unknown integer setcc!");
4505 case ISD::SETLT:
4506 case ISD::SETULT: LowCC = ISD::SETULT; break;
4507 case ISD::SETGT:
4508 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4509 case ISD::SETLE:
4510 case ISD::SETULE: LowCC = ISD::SETULE; break;
4511 case ISD::SETGE:
4512 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4513 }
4514
4515 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4516 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4517 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4518
4519 // NOTE: on targets without efficient SELECT of bools, we can always use
4520 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004521 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4522 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4523 false, DagCombineInfo);
4524 if (!Tmp1.Val)
4525 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4526 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4527 CCCode, false, DagCombineInfo);
4528 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004529 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004530
4531 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4532 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4533 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4534 (Tmp2C && Tmp2C->getValue() == 0 &&
4535 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4536 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4537 (Tmp2C && Tmp2C->getValue() == 1 &&
4538 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4539 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4540 // low part is known false, returns high part.
4541 // For LE / GE, if high part is known false, ignore the low part.
4542 // For LT / GT, if high part is known true, ignore the low part.
4543 Tmp1 = Tmp2;
4544 Tmp2 = SDOperand();
4545 } else {
4546 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4547 ISD::SETEQ, false, DagCombineInfo);
4548 if (!Result.Val)
4549 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4550 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4551 Result, Tmp1, Tmp2));
4552 Tmp1 = Result;
4553 Tmp2 = SDOperand();
4554 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004555 }
4556 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004557 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004558 LHS = Tmp1;
4559 RHS = Tmp2;
4560}
4561
Chris Lattner35481892005-12-23 00:16:34 +00004562/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004563/// The resultant code need not be legal. Note that SrcOp is the input operand
4564/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004565SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4566 SDOperand SrcOp) {
4567 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004568 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004569
4570 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004571 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004572 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004573 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004574}
4575
Chris Lattner4352cc92006-04-04 17:23:26 +00004576SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4577 // Create a vector sized/aligned stack slot, store the value to element #0,
4578 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004579 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004580 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004581 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004582 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004583}
4584
4585
Chris Lattnerce872152006-03-19 06:31:19 +00004586/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004587/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004588SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4589
4590 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004591 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004592 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004593 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004594 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004595 std::map<SDOperand, std::vector<unsigned> > Values;
4596 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004597 bool isConstant = true;
4598 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4599 SplatValue.getOpcode() != ISD::UNDEF)
4600 isConstant = false;
4601
Evan Cheng033e6812006-03-24 01:17:21 +00004602 for (unsigned i = 1; i < NumElems; ++i) {
4603 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004604 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004605 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004606 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004607 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004608 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004609
4610 // If this isn't a constant element or an undef, we can't use a constant
4611 // pool load.
4612 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4613 V.getOpcode() != ISD::UNDEF)
4614 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004615 }
4616
4617 if (isOnlyLowElement) {
4618 // If the low element is an undef too, then this whole things is an undef.
4619 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4620 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4621 // Otherwise, turn this into a scalar_to_vector node.
4622 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4623 Node->getOperand(0));
4624 }
4625
Chris Lattner2eb86532006-03-24 07:29:17 +00004626 // If all elements are constants, create a load from the constant pool.
4627 if (isConstant) {
4628 MVT::ValueType VT = Node->getValueType(0);
4629 const Type *OpNTy =
4630 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4631 std::vector<Constant*> CV;
4632 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4633 if (ConstantFPSDNode *V =
4634 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004635 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004636 } else if (ConstantSDNode *V =
4637 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004638 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004639 } else {
4640 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4641 CV.push_back(UndefValue::get(OpNTy));
4642 }
4643 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004644 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004645 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004646 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004647 }
4648
Chris Lattner87100e02006-03-20 01:52:29 +00004649 if (SplatValue.Val) { // Splat of one value?
4650 // Build the shuffle constant vector: <0, 0, 0, 0>
4651 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004652 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004653 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004654 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004655 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4656 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004657
4658 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004659 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004660 // Get the splatted value into the low element of a vector register.
4661 SDOperand LowValVec =
4662 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4663
4664 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4665 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4666 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4667 SplatMask);
4668 }
4669 }
4670
Evan Cheng033e6812006-03-24 01:17:21 +00004671 // If there are only two unique elements, we may be able to turn this into a
4672 // vector shuffle.
4673 if (Values.size() == 2) {
4674 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4675 MVT::ValueType MaskVT =
4676 MVT::getIntVectorWithNumElements(NumElems);
4677 std::vector<SDOperand> MaskVec(NumElems);
4678 unsigned i = 0;
4679 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4680 E = Values.end(); I != E; ++I) {
4681 for (std::vector<unsigned>::iterator II = I->second.begin(),
4682 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004683 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004684 i += NumElems;
4685 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004686 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4687 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004688
4689 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004690 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4691 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004692 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004693 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4694 E = Values.end(); I != E; ++I) {
4695 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4696 I->first);
4697 Ops.push_back(Op);
4698 }
4699 Ops.push_back(ShuffleMask);
4700
4701 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004702 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4703 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004704 }
4705 }
Chris Lattnerce872152006-03-19 06:31:19 +00004706
4707 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4708 // aligned object on the stack, store each element into it, then load
4709 // the result as a vector.
4710 MVT::ValueType VT = Node->getValueType(0);
4711 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004712 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004713
4714 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004715 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004716 unsigned TypeByteSize =
4717 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004718 // Store (in the right endianness) the elements to memory.
4719 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4720 // Ignore undef elements.
4721 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4722
Chris Lattner841c8822006-03-22 01:46:54 +00004723 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004724
4725 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4726 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4727
Evan Cheng786225a2006-10-05 23:01:46 +00004728 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004729 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004730 }
4731
4732 SDOperand StoreChain;
4733 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004734 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4735 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004736 else
4737 StoreChain = DAG.getEntryNode();
4738
4739 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004740 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004741}
4742
Chris Lattner5b359c62005-04-02 04:00:59 +00004743void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4744 SDOperand Op, SDOperand Amt,
4745 SDOperand &Lo, SDOperand &Hi) {
4746 // Expand the subcomponents.
4747 SDOperand LHSL, LHSH;
4748 ExpandOp(Op, LHSL, LHSH);
4749
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004750 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004751 MVT::ValueType VT = LHSL.getValueType();
4752 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004753 Hi = Lo.getValue(1);
4754}
4755
4756
Chris Lattnere34b3962005-01-19 04:19:40 +00004757/// ExpandShift - Try to find a clever way to expand this shift operation out to
4758/// smaller elements. If we can't find a way that is more efficient than a
4759/// libcall on this target, return false. Otherwise, return true with the
4760/// low-parts expanded into Lo and Hi.
4761bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4762 SDOperand &Lo, SDOperand &Hi) {
4763 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4764 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004765
Chris Lattnere34b3962005-01-19 04:19:40 +00004766 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004767 SDOperand ShAmt = LegalizeOp(Amt);
4768 MVT::ValueType ShTy = ShAmt.getValueType();
4769 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4770 unsigned NVTBits = MVT::getSizeInBits(NVT);
4771
Chris Lattner7a3c8552007-10-14 20:35:12 +00004772 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004773 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4774 unsigned Cst = CN->getValue();
4775 // Expand the incoming operand to be shifted, so that we have its parts
4776 SDOperand InL, InH;
4777 ExpandOp(Op, InL, InH);
4778 switch(Opc) {
4779 case ISD::SHL:
4780 if (Cst > VTBits) {
4781 Lo = DAG.getConstant(0, NVT);
4782 Hi = DAG.getConstant(0, NVT);
4783 } else if (Cst > NVTBits) {
4784 Lo = DAG.getConstant(0, NVT);
4785 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004786 } else if (Cst == NVTBits) {
4787 Lo = DAG.getConstant(0, NVT);
4788 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004789 } else {
4790 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4791 Hi = DAG.getNode(ISD::OR, NVT,
4792 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4793 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4794 }
4795 return true;
4796 case ISD::SRL:
4797 if (Cst > VTBits) {
4798 Lo = DAG.getConstant(0, NVT);
4799 Hi = DAG.getConstant(0, NVT);
4800 } else if (Cst > NVTBits) {
4801 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4802 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004803 } else if (Cst == NVTBits) {
4804 Lo = InH;
4805 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004806 } else {
4807 Lo = DAG.getNode(ISD::OR, NVT,
4808 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4809 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4810 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4811 }
4812 return true;
4813 case ISD::SRA:
4814 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004815 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004816 DAG.getConstant(NVTBits-1, ShTy));
4817 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004818 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004819 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004820 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004821 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004822 } else if (Cst == NVTBits) {
4823 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004824 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004825 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004826 } else {
4827 Lo = DAG.getNode(ISD::OR, NVT,
4828 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4829 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4830 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4831 }
4832 return true;
4833 }
4834 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004835
4836 // Okay, the shift amount isn't constant. However, if we can tell that it is
4837 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4838 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004839 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004840
4841 // If we know that the high bit of the shift amount is one, then we can do
4842 // this as a couple of simple shifts.
4843 if (KnownOne & Mask) {
4844 // Mask out the high bit, which we know is set.
4845 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4846 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4847
4848 // Expand the incoming operand to be shifted, so that we have its parts
4849 SDOperand InL, InH;
4850 ExpandOp(Op, InL, InH);
4851 switch(Opc) {
4852 case ISD::SHL:
4853 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4854 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4855 return true;
4856 case ISD::SRL:
4857 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4858 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4859 return true;
4860 case ISD::SRA:
4861 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4862 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4863 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4864 return true;
4865 }
4866 }
4867
4868 // If we know that the high bit of the shift amount is zero, then we can do
4869 // this as a couple of simple shifts.
4870 if (KnownZero & Mask) {
4871 // Compute 32-amt.
4872 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4873 DAG.getConstant(NVTBits, Amt.getValueType()),
4874 Amt);
4875
4876 // Expand the incoming operand to be shifted, so that we have its parts
4877 SDOperand InL, InH;
4878 ExpandOp(Op, InL, InH);
4879 switch(Opc) {
4880 case ISD::SHL:
4881 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4882 Hi = DAG.getNode(ISD::OR, NVT,
4883 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4884 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4885 return true;
4886 case ISD::SRL:
4887 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4888 Lo = DAG.getNode(ISD::OR, NVT,
4889 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4890 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4891 return true;
4892 case ISD::SRA:
4893 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4894 Lo = DAG.getNode(ISD::OR, NVT,
4895 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4896 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4897 return true;
4898 }
4899 }
4900
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004901 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004902}
Chris Lattner77e77a62005-01-21 06:05:23 +00004903
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004904
Chris Lattner77e77a62005-01-21 06:05:23 +00004905// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4906// does not fit into a register, return the lo part and set the hi part to the
4907// by-reg argument. If it does fit into a single register, return the result
4908// and leave the Hi part unset.
4909SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004910 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004911 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4912 // The input chain to this libcall is the entry node of the function.
4913 // Legalizing the call will automatically add the previous call to the
4914 // dependence.
4915 SDOperand InChain = DAG.getEntryNode();
4916
Chris Lattner77e77a62005-01-21 06:05:23 +00004917 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004918 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004919 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4920 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4921 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004922 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004923 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004924 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004925 }
4926 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004927
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004928 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004929 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004930 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004931 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004932 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004933
Chris Lattner6831a812006-02-13 09:18:02 +00004934 // Legalize the call sequence, starting with the chain. This will advance
4935 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4936 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4937 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004938 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004939 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004940 default: assert(0 && "Unknown thing");
4941 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004942 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004943 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004944 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004945 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004946 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004947 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004948 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004949}
4950
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004951
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004952/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4953///
Chris Lattner77e77a62005-01-21 06:05:23 +00004954SDOperand SelectionDAGLegalize::
4955ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004956 assert(getTypeAction(Source.getValueType()) == Expand &&
4957 "This is not an expansion!");
4958 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4959
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004960 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004961 assert(Source.getValueType() == MVT::i64 &&
4962 "This only works for 64-bit -> FP");
4963 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4964 // incoming integer is set. To handle this, we dynamically test to see if
4965 // it is set, and, if so, add a fudge factor.
4966 SDOperand Lo, Hi;
4967 ExpandOp(Source, Lo, Hi);
4968
Chris Lattner66de05b2005-05-13 04:45:13 +00004969 // If this is unsigned, and not supported, first perform the conversion to
4970 // signed, then adjust the result if the sign bit is set.
4971 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4972 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4973
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004974 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4975 DAG.getConstant(0, Hi.getValueType()),
4976 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004977 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4978 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4979 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004980 uint64_t FF = 0x5f800000ULL;
4981 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004982 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004983
Chris Lattner5839bf22005-08-26 17:15:30 +00004984 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004985 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4986 SDOperand FudgeInReg;
4987 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004988 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004989 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004990 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004991 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004992 CPIdx, NULL, 0, MVT::f32);
4993 else
4994 assert(0 && "Unexpected conversion");
4995
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004996 MVT::ValueType SCVT = SignedConv.getValueType();
4997 if (SCVT != DestTy) {
4998 // Destination type needs to be expanded as well. The FADD now we are
4999 // constructing will be expanded into a libcall.
5000 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5001 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5002 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5003 SignedConv, SignedConv.getValue(1));
5004 }
5005 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5006 }
Chris Lattner473a9902005-09-29 06:44:39 +00005007 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005008 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005009
Chris Lattnera88a2602005-05-14 05:33:54 +00005010 // Check to see if the target has a custom way to lower this. If so, use it.
5011 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5012 default: assert(0 && "This action not implemented for this operation!");
5013 case TargetLowering::Legal:
5014 case TargetLowering::Expand:
5015 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005016 case TargetLowering::Custom: {
5017 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5018 Source), DAG);
5019 if (NV.Val)
5020 return LegalizeOp(NV);
5021 break; // The target decided this was legal after all
5022 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005023 }
5024
Chris Lattner13689e22005-05-12 07:00:44 +00005025 // Expand the source, then glue it back together for the call. We must expand
5026 // the source in case it is shared (this pass of legalize must traverse it).
5027 SDOperand SrcLo, SrcHi;
5028 ExpandOp(Source, SrcLo, SrcHi);
5029 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5030
Evan Cheng56966222007-01-12 02:11:51 +00005031 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005032 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005033 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005034 else {
5035 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00005036 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005037 }
Chris Lattner6831a812006-02-13 09:18:02 +00005038
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005039 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005040 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5041 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005042 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5043 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005044}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005045
Chris Lattner22cde6a2006-01-28 08:25:58 +00005046/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5047/// INT_TO_FP operation of the specified operand when the target requests that
5048/// we expand it. At this point, we know that the result and operand types are
5049/// legal for the target.
5050SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5051 SDOperand Op0,
5052 MVT::ValueType DestVT) {
5053 if (Op0.getValueType() == MVT::i32) {
5054 // simple 32-bit [signed|unsigned] integer to float/double expansion
5055
Chris Lattner23594d42008-01-16 07:03:22 +00005056 // Get the stack frame index of a 8 byte buffer.
5057 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5058
Chris Lattner22cde6a2006-01-28 08:25:58 +00005059 // word offset constant for Hi/Lo address computation
5060 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5061 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005062 SDOperand Hi = StackSlot;
5063 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5064 if (TLI.isLittleEndian())
5065 std::swap(Hi, Lo);
5066
Chris Lattner22cde6a2006-01-28 08:25:58 +00005067 // if signed map to unsigned space
5068 SDOperand Op0Mapped;
5069 if (isSigned) {
5070 // constant used to invert sign bit (signed to unsigned mapping)
5071 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5072 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5073 } else {
5074 Op0Mapped = Op0;
5075 }
5076 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005077 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005078 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005079 // initial hi portion of constructed double
5080 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5081 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005082 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005083 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005084 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005085 // FP constant to bias correct the final result
5086 SDOperand Bias = DAG.getConstantFP(isSigned ?
5087 BitsToDouble(0x4330000080000000ULL)
5088 : BitsToDouble(0x4330000000000000ULL),
5089 MVT::f64);
5090 // subtract the bias
5091 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5092 // final result
5093 SDOperand Result;
5094 // handle final rounding
5095 if (DestVT == MVT::f64) {
5096 // do nothing
5097 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005098 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5099 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5100 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5101 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005102 }
5103 return Result;
5104 }
5105 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5106 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5107
5108 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5109 DAG.getConstant(0, Op0.getValueType()),
5110 ISD::SETLT);
5111 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5112 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5113 SignSet, Four, Zero);
5114
5115 // If the sign bit of the integer is set, the large number will be treated
5116 // as a negative number. To counteract this, the dynamic code adds an
5117 // offset depending on the data type.
5118 uint64_t FF;
5119 switch (Op0.getValueType()) {
5120 default: assert(0 && "Unsupported integer type!");
5121 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5122 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5123 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5124 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5125 }
5126 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005127 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005128
5129 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5130 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5131 SDOperand FudgeInReg;
5132 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005133 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005134 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005135 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005136 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005137 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005138 }
5139
5140 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5141}
5142
5143/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5144/// *INT_TO_FP operation of the specified operand when the target requests that
5145/// we promote it. At this point, we know that the result and operand types are
5146/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5147/// operation that takes a larger input.
5148SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5149 MVT::ValueType DestVT,
5150 bool isSigned) {
5151 // First step, figure out the appropriate *INT_TO_FP operation to use.
5152 MVT::ValueType NewInTy = LegalOp.getValueType();
5153
5154 unsigned OpToUse = 0;
5155
5156 // Scan for the appropriate larger type to use.
5157 while (1) {
5158 NewInTy = (MVT::ValueType)(NewInTy+1);
5159 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5160
5161 // If the target supports SINT_TO_FP of this type, use it.
5162 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5163 default: break;
5164 case TargetLowering::Legal:
5165 if (!TLI.isTypeLegal(NewInTy))
5166 break; // Can't use this datatype.
5167 // FALL THROUGH.
5168 case TargetLowering::Custom:
5169 OpToUse = ISD::SINT_TO_FP;
5170 break;
5171 }
5172 if (OpToUse) break;
5173 if (isSigned) continue;
5174
5175 // If the target supports UINT_TO_FP of this type, use it.
5176 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5177 default: break;
5178 case TargetLowering::Legal:
5179 if (!TLI.isTypeLegal(NewInTy))
5180 break; // Can't use this datatype.
5181 // FALL THROUGH.
5182 case TargetLowering::Custom:
5183 OpToUse = ISD::UINT_TO_FP;
5184 break;
5185 }
5186 if (OpToUse) break;
5187
5188 // Otherwise, try a larger type.
5189 }
5190
5191 // Okay, we found the operation and type to use. Zero extend our input to the
5192 // desired type then run the operation on it.
5193 return DAG.getNode(OpToUse, DestVT,
5194 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5195 NewInTy, LegalOp));
5196}
5197
5198/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5199/// FP_TO_*INT operation of the specified operand when the target requests that
5200/// we promote it. At this point, we know that the result and operand types are
5201/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5202/// operation that returns a larger result.
5203SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5204 MVT::ValueType DestVT,
5205 bool isSigned) {
5206 // First step, figure out the appropriate FP_TO*INT operation to use.
5207 MVT::ValueType NewOutTy = DestVT;
5208
5209 unsigned OpToUse = 0;
5210
5211 // Scan for the appropriate larger type to use.
5212 while (1) {
5213 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5214 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5215
5216 // If the target supports FP_TO_SINT returning this type, use it.
5217 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5218 default: break;
5219 case TargetLowering::Legal:
5220 if (!TLI.isTypeLegal(NewOutTy))
5221 break; // Can't use this datatype.
5222 // FALL THROUGH.
5223 case TargetLowering::Custom:
5224 OpToUse = ISD::FP_TO_SINT;
5225 break;
5226 }
5227 if (OpToUse) break;
5228
5229 // If the target supports FP_TO_UINT of this type, use it.
5230 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5231 default: break;
5232 case TargetLowering::Legal:
5233 if (!TLI.isTypeLegal(NewOutTy))
5234 break; // Can't use this datatype.
5235 // FALL THROUGH.
5236 case TargetLowering::Custom:
5237 OpToUse = ISD::FP_TO_UINT;
5238 break;
5239 }
5240 if (OpToUse) break;
5241
5242 // Otherwise, try a larger type.
5243 }
5244
Chris Lattner27a6c732007-11-24 07:07:01 +00005245
5246 // Okay, we found the operation and type to use.
5247 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5248
5249 // If the operation produces an invalid type, it must be custom lowered. Use
5250 // the target lowering hooks to expand it. Just keep the low part of the
5251 // expanded operation, we know that we're truncating anyway.
5252 if (getTypeAction(NewOutTy) == Expand) {
5253 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5254 assert(Operation.Val && "Didn't return anything");
5255 }
5256
5257 // Truncate the result of the extended FP_TO_*INT operation to the desired
5258 // size.
5259 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005260}
5261
5262/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5263///
5264SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5265 MVT::ValueType VT = Op.getValueType();
5266 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5267 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5268 switch (VT) {
5269 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5270 case MVT::i16:
5271 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5272 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5273 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5274 case MVT::i32:
5275 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5276 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5277 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5278 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5279 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5280 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5281 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5282 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5283 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5284 case MVT::i64:
5285 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5286 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5287 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5288 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5289 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5290 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5291 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5292 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5293 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5294 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5295 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5296 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5297 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5298 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5299 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5300 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5301 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5302 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5303 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5304 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5305 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5306 }
5307}
5308
5309/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5310///
5311SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5312 switch (Opc) {
5313 default: assert(0 && "Cannot expand this yet!");
5314 case ISD::CTPOP: {
5315 static const uint64_t mask[6] = {
5316 0x5555555555555555ULL, 0x3333333333333333ULL,
5317 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5318 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5319 };
5320 MVT::ValueType VT = Op.getValueType();
5321 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005322 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005323 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5324 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5325 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5326 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5327 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5328 DAG.getNode(ISD::AND, VT,
5329 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5330 }
5331 return Op;
5332 }
5333 case ISD::CTLZ: {
5334 // for now, we do this:
5335 // x = x | (x >> 1);
5336 // x = x | (x >> 2);
5337 // ...
5338 // x = x | (x >>16);
5339 // x = x | (x >>32); // for 64-bit input
5340 // return popcount(~x);
5341 //
5342 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5343 MVT::ValueType VT = Op.getValueType();
5344 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005345 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005346 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5347 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5348 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5349 }
5350 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5351 return DAG.getNode(ISD::CTPOP, VT, Op);
5352 }
5353 case ISD::CTTZ: {
5354 // for now, we use: { return popcount(~x & (x - 1)); }
5355 // unless the target has ctlz but not ctpop, in which case we use:
5356 // { return 32 - nlz(~x & (x-1)); }
5357 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5358 MVT::ValueType VT = Op.getValueType();
5359 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5360 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5361 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5362 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5363 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5364 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5365 TLI.isOperationLegal(ISD::CTLZ, VT))
5366 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005367 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005368 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5369 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5370 }
5371 }
5372}
Chris Lattnere34b3962005-01-19 04:19:40 +00005373
Chris Lattner3e928bb2005-01-07 07:47:09 +00005374/// ExpandOp - Expand the specified SDOperand into its two component pieces
5375/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5376/// LegalizeNodes map is filled in for any results that are not expanded, the
5377/// ExpandedNodes map is filled in for any results that are expanded, and the
5378/// Lo/Hi values are returned.
5379void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5380 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005381 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005382 SDNode *Node = Op.Val;
5383 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005384 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005385 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005386 "Cannot expand to FP value or to larger int value!");
5387
Chris Lattner6fdcb252005-09-02 20:32:45 +00005388 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005389 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005390 = ExpandedNodes.find(Op);
5391 if (I != ExpandedNodes.end()) {
5392 Lo = I->second.first;
5393 Hi = I->second.second;
5394 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005395 }
5396
Chris Lattner3e928bb2005-01-07 07:47:09 +00005397 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005398 case ISD::CopyFromReg:
5399 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005400 case ISD::FP_ROUND_INREG:
5401 if (VT == MVT::ppcf128 &&
5402 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5403 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005404 SDOperand SrcLo, SrcHi, Src;
5405 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5406 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5407 SDOperand Result = TLI.LowerOperation(
5408 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005409 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5410 Lo = Result.Val->getOperand(0);
5411 Hi = Result.Val->getOperand(1);
5412 break;
5413 }
5414 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005415 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005416#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005417 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005418#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005419 assert(0 && "Do not know how to expand this operator!");
5420 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005421 case ISD::EXTRACT_VECTOR_ELT:
5422 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5423 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5424 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5425 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005426 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005427 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005428 Lo = DAG.getNode(ISD::UNDEF, NVT);
5429 Hi = DAG.getNode(ISD::UNDEF, NVT);
5430 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005431 case ISD::Constant: {
5432 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5433 Lo = DAG.getConstant(Cst, NVT);
5434 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5435 break;
5436 }
Evan Cheng00495212006-12-12 21:32:44 +00005437 case ISD::ConstantFP: {
5438 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005439 if (CFP->getValueType(0) == MVT::ppcf128) {
5440 APInt api = CFP->getValueAPF().convertToAPInt();
5441 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5442 MVT::f64);
5443 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5444 MVT::f64);
5445 break;
5446 }
Evan Cheng279101e2006-12-12 22:19:28 +00005447 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005448 if (getTypeAction(Lo.getValueType()) == Expand)
5449 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005450 break;
5451 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005452 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005453 // Return the operands.
5454 Lo = Node->getOperand(0);
5455 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005456 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005457
5458 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005459 if (Node->getNumValues() == 1) {
5460 ExpandOp(Op.getOperand(0), Lo, Hi);
5461 break;
5462 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005463 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5464 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5465 Op.getValue(1).getValueType() == MVT::Other &&
5466 "unhandled MERGE_VALUES");
5467 ExpandOp(Op.getOperand(0), Lo, Hi);
5468 // Remember that we legalized the chain.
5469 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5470 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005471
5472 case ISD::SIGN_EXTEND_INREG:
5473 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005474 // sext_inreg the low part if needed.
5475 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5476
5477 // The high part gets the sign extension from the lo-part. This handles
5478 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005479 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5480 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5481 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005482 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005483
Nate Begemand88fc032006-01-14 03:14:10 +00005484 case ISD::BSWAP: {
5485 ExpandOp(Node->getOperand(0), Lo, Hi);
5486 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5487 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5488 Lo = TempLo;
5489 break;
5490 }
5491
Chris Lattneredb1add2005-05-11 04:51:16 +00005492 case ISD::CTPOP:
5493 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005494 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5495 DAG.getNode(ISD::CTPOP, NVT, Lo),
5496 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005497 Hi = DAG.getConstant(0, NVT);
5498 break;
5499
Chris Lattner39a8f332005-05-12 19:05:01 +00005500 case ISD::CTLZ: {
5501 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005502 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005503 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5504 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005505 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5506 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005507 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5508 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5509
5510 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5511 Hi = DAG.getConstant(0, NVT);
5512 break;
5513 }
5514
5515 case ISD::CTTZ: {
5516 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005517 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005518 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5519 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005520 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5521 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005522 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5523 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5524
5525 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5526 Hi = DAG.getConstant(0, NVT);
5527 break;
5528 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005529
Nate Begemanacc398c2006-01-25 18:21:52 +00005530 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005531 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5532 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005533 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5534 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5535
5536 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005537 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005538 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5539 if (!TLI.isLittleEndian())
5540 std::swap(Lo, Hi);
5541 break;
5542 }
5543
Chris Lattner3e928bb2005-01-07 07:47:09 +00005544 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005545 LoadSDNode *LD = cast<LoadSDNode>(Node);
5546 SDOperand Ch = LD->getChain(); // Legalize the chain.
5547 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5548 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005549 int SVOffset = LD->getSrcValueOffset();
5550 unsigned Alignment = LD->getAlignment();
5551 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005552
Evan Cheng466685d2006-10-09 20:57:25 +00005553 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005554 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5555 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005556 if (VT == MVT::f32 || VT == MVT::f64) {
5557 // f32->i32 or f64->i64 one to one expansion.
5558 // Remember that we legalized the chain.
5559 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005560 // Recursively expand the new load.
5561 if (getTypeAction(NVT) == Expand)
5562 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005563 break;
5564 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005565
Evan Cheng466685d2006-10-09 20:57:25 +00005566 // Increment the pointer to the other half.
5567 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5568 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5569 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005570 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005571 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005572 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5573 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005574
Evan Cheng466685d2006-10-09 20:57:25 +00005575 // Build a factor node to remember that this load is independent of the
5576 // other one.
5577 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5578 Hi.getValue(1));
5579
5580 // Remember that we legalized the chain.
5581 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5582 if (!TLI.isLittleEndian())
5583 std::swap(Lo, Hi);
5584 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005585 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005586
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005587 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5588 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005589 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5590 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005591 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005592 // Remember that we legalized the chain.
5593 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5594 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5595 break;
5596 }
Evan Cheng466685d2006-10-09 20:57:25 +00005597
5598 if (EVT == NVT)
5599 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005600 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005601 else
5602 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005603 SVOffset, EVT, isVolatile,
5604 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005605
5606 // Remember that we legalized the chain.
5607 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5608
5609 if (ExtType == ISD::SEXTLOAD) {
5610 // The high part is obtained by SRA'ing all but one of the bits of the
5611 // lo part.
5612 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5613 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5614 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5615 } else if (ExtType == ISD::ZEXTLOAD) {
5616 // The high part is just a zero.
5617 Hi = DAG.getConstant(0, NVT);
5618 } else /* if (ExtType == ISD::EXTLOAD) */ {
5619 // The high part is undefined.
5620 Hi = DAG.getNode(ISD::UNDEF, NVT);
5621 }
5622 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005623 break;
5624 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005625 case ISD::AND:
5626 case ISD::OR:
5627 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5628 SDOperand LL, LH, RL, RH;
5629 ExpandOp(Node->getOperand(0), LL, LH);
5630 ExpandOp(Node->getOperand(1), RL, RH);
5631 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5632 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5633 break;
5634 }
5635 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005636 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005637 ExpandOp(Node->getOperand(1), LL, LH);
5638 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005639 if (getTypeAction(NVT) == Expand)
5640 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005641 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005642 if (VT != MVT::f32)
5643 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005644 break;
5645 }
Nate Begeman9373a812005-08-10 20:51:12 +00005646 case ISD::SELECT_CC: {
5647 SDOperand TL, TH, FL, FH;
5648 ExpandOp(Node->getOperand(2), TL, TH);
5649 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005650 if (getTypeAction(NVT) == Expand)
5651 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005652 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5653 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005654 if (VT != MVT::f32)
5655 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5656 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005657 break;
5658 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005659 case ISD::ANY_EXTEND:
5660 // The low part is any extension of the input (which degenerates to a copy).
5661 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5662 // The high part is undefined.
5663 Hi = DAG.getNode(ISD::UNDEF, NVT);
5664 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005665 case ISD::SIGN_EXTEND: {
5666 // The low part is just a sign extension of the input (which degenerates to
5667 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005668 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005669
Chris Lattner3e928bb2005-01-07 07:47:09 +00005670 // The high part is obtained by SRA'ing all but one of the bits of the lo
5671 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005672 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005673 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5674 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005675 break;
5676 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005677 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005678 // The low part is just a zero extension of the input (which degenerates to
5679 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005680 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005681
Chris Lattner3e928bb2005-01-07 07:47:09 +00005682 // The high part is just a zero.
5683 Hi = DAG.getConstant(0, NVT);
5684 break;
Chris Lattner35481892005-12-23 00:16:34 +00005685
Chris Lattner4c948eb2007-02-13 23:55:16 +00005686 case ISD::TRUNCATE: {
5687 // The input value must be larger than this value. Expand *it*.
5688 SDOperand NewLo;
5689 ExpandOp(Node->getOperand(0), NewLo, Hi);
5690
5691 // The low part is now either the right size, or it is closer. If not the
5692 // right size, make an illegal truncate so we recursively expand it.
5693 if (NewLo.getValueType() != Node->getValueType(0))
5694 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5695 ExpandOp(NewLo, Lo, Hi);
5696 break;
5697 }
5698
Chris Lattner35481892005-12-23 00:16:34 +00005699 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005700 SDOperand Tmp;
5701 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5702 // If the target wants to, allow it to lower this itself.
5703 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5704 case Expand: assert(0 && "cannot expand FP!");
5705 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5706 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5707 }
5708 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5709 }
5710
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005711 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005712 if (VT == MVT::f32 || VT == MVT::f64) {
5713 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005714 if (getTypeAction(NVT) == Expand)
5715 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005716 break;
5717 }
5718
5719 // If source operand will be expanded to the same type as VT, i.e.
5720 // i64 <- f64, i32 <- f32, expand the source operand instead.
5721 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5722 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5723 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005724 break;
5725 }
5726
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005727 // Turn this into a load/store pair by default.
5728 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005729 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005730
Chris Lattner35481892005-12-23 00:16:34 +00005731 ExpandOp(Tmp, Lo, Hi);
5732 break;
5733 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005734
Chris Lattner27a6c732007-11-24 07:07:01 +00005735 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005736 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5737 TargetLowering::Custom &&
5738 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005739 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5740 assert(Tmp.Val && "Node must be custom expanded!");
5741 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005742 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005743 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005744 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005745 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005746
Chris Lattner4e6c7462005-01-08 19:27:05 +00005747 // These operators cannot be expanded directly, emit them as calls to
5748 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005749 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005750 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005751 SDOperand Op;
5752 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5753 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005754 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5755 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005756 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005757
Chris Lattnerf20d1832005-07-30 01:40:57 +00005758 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5759
Chris Lattner80a3e942005-07-29 00:33:32 +00005760 // Now that the custom expander is done, expand the result, which is still
5761 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005762 if (Op.Val) {
5763 ExpandOp(Op, Lo, Hi);
5764 break;
5765 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005766 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005767
Dale Johannesen161e8972007-10-05 20:04:43 +00005768 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005769 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005770 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005771 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005772 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005773 else if (Node->getOperand(0).getValueType() == MVT::f80)
5774 LC = RTLIB::FPTOSINT_F80_I64;
5775 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5776 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005777 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5778 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005779 break;
Evan Cheng56966222007-01-12 02:11:51 +00005780 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005781
Evan Cheng56966222007-01-12 02:11:51 +00005782 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005783 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005784 SDOperand Op;
5785 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5786 case Expand: assert(0 && "cannot expand FP!");
5787 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5788 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5789 }
5790
5791 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5792
5793 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005794 if (Op.Val) {
5795 ExpandOp(Op, Lo, Hi);
5796 break;
5797 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005798 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005799
Evan Chengdaccea182007-10-05 01:09:32 +00005800 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005801 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005802 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005803 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005804 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005805 else if (Node->getOperand(0).getValueType() == MVT::f80)
5806 LC = RTLIB::FPTOUINT_F80_I64;
5807 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5808 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005809 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5810 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005811 break;
Evan Cheng56966222007-01-12 02:11:51 +00005812 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005813
Evan Cheng05a2d562006-01-09 18:31:59 +00005814 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005815 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005816 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005817 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005818 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005819 Op = TLI.LowerOperation(Op, DAG);
5820 if (Op.Val) {
5821 // Now that the custom expander is done, expand the result, which is
5822 // still VT.
5823 ExpandOp(Op, Lo, Hi);
5824 break;
5825 }
5826 }
5827
Chris Lattner79980b02006-09-13 03:50:39 +00005828 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5829 // this X << 1 as X+X.
5830 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5831 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5832 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5833 SDOperand LoOps[2], HiOps[3];
5834 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5835 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5836 LoOps[1] = LoOps[0];
5837 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5838
5839 HiOps[1] = HiOps[0];
5840 HiOps[2] = Lo.getValue(1);
5841 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5842 break;
5843 }
5844 }
5845
Chris Lattnere34b3962005-01-19 04:19:40 +00005846 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005847 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005848 break;
Chris Lattner47599822005-04-02 03:38:53 +00005849
5850 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005851 TargetLowering::LegalizeAction Action =
5852 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5853 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5854 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005855 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005856 break;
5857 }
5858
Chris Lattnere34b3962005-01-19 04:19:40 +00005859 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005860 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5861 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005862 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005863 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005864
Evan Cheng05a2d562006-01-09 18:31:59 +00005865 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005866 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005867 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005868 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005869 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005870 Op = TLI.LowerOperation(Op, DAG);
5871 if (Op.Val) {
5872 // Now that the custom expander is done, expand the result, which is
5873 // still VT.
5874 ExpandOp(Op, Lo, Hi);
5875 break;
5876 }
5877 }
5878
Chris Lattnere34b3962005-01-19 04:19:40 +00005879 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005880 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005881 break;
Chris Lattner47599822005-04-02 03:38:53 +00005882
5883 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005884 TargetLowering::LegalizeAction Action =
5885 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5886 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5887 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005888 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005889 break;
5890 }
5891
Chris Lattnere34b3962005-01-19 04:19:40 +00005892 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005893 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5894 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005895 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005896 }
5897
5898 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005899 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005900 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005901 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005902 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005903 Op = TLI.LowerOperation(Op, DAG);
5904 if (Op.Val) {
5905 // Now that the custom expander is done, expand the result, which is
5906 // still VT.
5907 ExpandOp(Op, Lo, Hi);
5908 break;
5909 }
5910 }
5911
Chris Lattnere34b3962005-01-19 04:19:40 +00005912 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005913 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005914 break;
Chris Lattner47599822005-04-02 03:38:53 +00005915
5916 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005917 TargetLowering::LegalizeAction Action =
5918 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5919 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5920 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005921 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005922 break;
5923 }
5924
Chris Lattnere34b3962005-01-19 04:19:40 +00005925 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005926 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5927 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005928 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005929 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005930
Misha Brukmanedf128a2005-04-21 22:36:52 +00005931 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005932 case ISD::SUB: {
5933 // If the target wants to custom expand this, let them.
5934 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5935 TargetLowering::Custom) {
5936 Op = TLI.LowerOperation(Op, DAG);
5937 if (Op.Val) {
5938 ExpandOp(Op, Lo, Hi);
5939 break;
5940 }
5941 }
5942
5943 // Expand the subcomponents.
5944 SDOperand LHSL, LHSH, RHSL, RHSH;
5945 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5946 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005947 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5948 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005949 LoOps[0] = LHSL;
5950 LoOps[1] = RHSL;
5951 HiOps[0] = LHSH;
5952 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005953 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005954 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005955 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005956 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005957 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005958 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005959 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005960 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005961 }
Chris Lattner84f67882005-01-20 18:52:28 +00005962 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005963 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005964
5965 case ISD::ADDC:
5966 case ISD::SUBC: {
5967 // Expand the subcomponents.
5968 SDOperand LHSL, LHSH, RHSL, RHSH;
5969 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5970 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5971 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5972 SDOperand LoOps[2] = { LHSL, RHSL };
5973 SDOperand HiOps[3] = { LHSH, RHSH };
5974
5975 if (Node->getOpcode() == ISD::ADDC) {
5976 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5977 HiOps[2] = Lo.getValue(1);
5978 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5979 } else {
5980 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5981 HiOps[2] = Lo.getValue(1);
5982 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5983 }
5984 // Remember that we legalized the flag.
5985 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5986 break;
5987 }
5988 case ISD::ADDE:
5989 case ISD::SUBE: {
5990 // Expand the subcomponents.
5991 SDOperand LHSL, LHSH, RHSL, RHSH;
5992 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5993 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5994 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5995 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5996 SDOperand HiOps[3] = { LHSH, RHSH };
5997
5998 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5999 HiOps[2] = Lo.getValue(1);
6000 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6001
6002 // Remember that we legalized the flag.
6003 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6004 break;
6005 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006006 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006007 // If the target wants to custom expand this, let them.
6008 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006009 SDOperand New = TLI.LowerOperation(Op, DAG);
6010 if (New.Val) {
6011 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006012 break;
6013 }
6014 }
6015
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006016 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6017 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006018 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6019 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6020 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006021 SDOperand LL, LH, RL, RH;
6022 ExpandOp(Node->getOperand(0), LL, LH);
6023 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00006024 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6025 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6026 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6027 // FIXME: generalize this to handle other bit sizes
6028 if (LHSSB == 32 && RHSSB == 32 &&
6029 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6030 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6031 // The inputs are both zero-extended.
6032 if (HasUMUL_LOHI) {
6033 // We can emit a umul_lohi.
6034 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6035 Hi = SDOperand(Lo.Val, 1);
6036 break;
6037 }
6038 if (HasMULHU) {
6039 // We can emit a mulhu+mul.
6040 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6041 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6042 break;
6043 }
Dan Gohman525178c2007-10-08 18:33:35 +00006044 }
6045 if (LHSSB > BitSize && RHSSB > BitSize) {
6046 // The input values are both sign-extended.
6047 if (HasSMUL_LOHI) {
6048 // We can emit a smul_lohi.
6049 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6050 Hi = SDOperand(Lo.Val, 1);
6051 break;
6052 }
6053 if (HasMULHS) {
6054 // We can emit a mulhs+mul.
6055 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6056 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6057 break;
6058 }
6059 }
6060 if (HasUMUL_LOHI) {
6061 // Lo,Hi = umul LHS, RHS.
6062 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6063 DAG.getVTList(NVT, NVT), LL, RL);
6064 Lo = UMulLOHI;
6065 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006066 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6067 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6068 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6069 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006070 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006071 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006072 if (HasMULHU) {
6073 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6074 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6075 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6076 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6077 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6078 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6079 break;
6080 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006081 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006082
Dan Gohman525178c2007-10-08 18:33:35 +00006083 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006084 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6085 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006086 break;
6087 }
Evan Cheng56966222007-01-12 02:11:51 +00006088 case ISD::SDIV:
6089 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6090 break;
6091 case ISD::UDIV:
6092 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6093 break;
6094 case ISD::SREM:
6095 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6096 break;
6097 case ISD::UREM:
6098 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6099 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006100
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006101 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006102 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6103 RTLIB::ADD_F64,
6104 RTLIB::ADD_F80,
6105 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006106 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006107 break;
6108 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006109 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6110 RTLIB::SUB_F64,
6111 RTLIB::SUB_F80,
6112 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006113 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006114 break;
6115 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006116 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6117 RTLIB::MUL_F64,
6118 RTLIB::MUL_F80,
6119 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006120 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006121 break;
6122 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006123 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6124 RTLIB::DIV_F64,
6125 RTLIB::DIV_F80,
6126 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006127 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006128 break;
6129 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006130 if (VT == MVT::ppcf128) {
6131 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6132 Node->getOperand(0).getValueType()==MVT::f64);
6133 const uint64_t zero = 0;
6134 if (Node->getOperand(0).getValueType()==MVT::f32)
6135 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6136 else
6137 Hi = Node->getOperand(0);
6138 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6139 break;
6140 }
Evan Cheng56966222007-01-12 02:11:51 +00006141 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006142 break;
6143 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006144 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006145 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006146 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006147 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6148 RTLIB::POWI_F64,
6149 RTLIB::POWI_F80,
6150 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006151 Node, false, Hi);
6152 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006153 case ISD::FSQRT:
6154 case ISD::FSIN:
6155 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006156 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006157 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006158 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006159 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6160 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006161 break;
6162 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006163 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6164 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006165 break;
6166 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006167 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6168 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006169 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006170 default: assert(0 && "Unreachable!");
6171 }
Evan Cheng56966222007-01-12 02:11:51 +00006172 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006173 break;
6174 }
Evan Cheng966bf242006-12-16 00:52:40 +00006175 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006176 if (VT == MVT::ppcf128) {
6177 SDOperand Tmp;
6178 ExpandOp(Node->getOperand(0), Lo, Tmp);
6179 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6180 // lo = hi==fabs(hi) ? lo : -lo;
6181 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6182 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6183 DAG.getCondCode(ISD::SETEQ));
6184 break;
6185 }
Evan Cheng966bf242006-12-16 00:52:40 +00006186 SDOperand Mask = (VT == MVT::f64)
6187 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6188 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6189 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6190 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6191 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6192 if (getTypeAction(NVT) == Expand)
6193 ExpandOp(Lo, Lo, Hi);
6194 break;
6195 }
6196 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006197 if (VT == MVT::ppcf128) {
6198 ExpandOp(Node->getOperand(0), Lo, Hi);
6199 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6200 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6201 break;
6202 }
Evan Cheng966bf242006-12-16 00:52:40 +00006203 SDOperand Mask = (VT == MVT::f64)
6204 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6205 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6206 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6207 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6208 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6209 if (getTypeAction(NVT) == Expand)
6210 ExpandOp(Lo, Lo, Hi);
6211 break;
6212 }
Evan Cheng912095b2007-01-04 21:56:39 +00006213 case ISD::FCOPYSIGN: {
6214 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6215 if (getTypeAction(NVT) == Expand)
6216 ExpandOp(Lo, Lo, Hi);
6217 break;
6218 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006219 case ISD::SINT_TO_FP:
6220 case ISD::UINT_TO_FP: {
6221 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6222 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006223 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006224 static uint64_t zero = 0;
6225 if (isSigned) {
6226 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6227 Node->getOperand(0)));
6228 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6229 } else {
6230 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6231 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6232 Node->getOperand(0)));
6233 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6234 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006235 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006236 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6237 DAG.getConstant(0, MVT::i32),
6238 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6239 DAG.getConstantFP(
6240 APFloat(APInt(128, 2, TwoE32)),
6241 MVT::ppcf128)),
6242 Hi,
6243 DAG.getCondCode(ISD::SETLT)),
6244 Lo, Hi);
6245 }
6246 break;
6247 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006248 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6249 // si64->ppcf128 done by libcall, below
6250 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6251 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6252 Lo, Hi);
6253 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6254 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6255 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6256 DAG.getConstant(0, MVT::i64),
6257 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6258 DAG.getConstantFP(
6259 APFloat(APInt(128, 2, TwoE64)),
6260 MVT::ppcf128)),
6261 Hi,
6262 DAG.getCondCode(ISD::SETLT)),
6263 Lo, Hi);
6264 break;
6265 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006266 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006267 if (Node->getOperand(0).getValueType() == MVT::i64) {
6268 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006269 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006270 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006271 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006272 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006273 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006274 LC = RTLIB::SINTTOFP_I64_F80;
6275 }
6276 else if (VT == MVT::ppcf128) {
6277 assert(isSigned);
6278 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006279 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006280 } else {
6281 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006282 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006283 else
Evan Cheng56966222007-01-12 02:11:51 +00006284 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006285 }
6286
6287 // Promote the operand if needed.
6288 if (getTypeAction(SrcVT) == Promote) {
6289 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6290 Tmp = isSigned
6291 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6292 DAG.getValueType(SrcVT))
6293 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6294 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6295 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006296
6297 const char *LibCall = TLI.getLibcallName(LC);
6298 if (LibCall)
6299 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6300 else {
6301 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6302 Node->getOperand(0));
6303 if (getTypeAction(Lo.getValueType()) == Expand)
6304 ExpandOp(Lo, Lo, Hi);
6305 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006306 break;
6307 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006308 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006309
Chris Lattner83397362005-12-21 18:02:52 +00006310 // Make sure the resultant values have been legalized themselves, unless this
6311 // is a type that requires multi-step expansion.
6312 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6313 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006314 if (Hi.Val)
6315 // Don't legalize the high part if it is expanded to a single node.
6316 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006317 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006318
6319 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006320 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006321 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006322}
6323
Dan Gohman7f321562007-06-25 16:23:39 +00006324/// SplitVectorOp - Given an operand of vector type, break it down into
6325/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006326void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6327 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006328 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006329 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006330 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006331 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006332
Dan Gohmane40c7b02007-09-24 15:54:53 +00006333 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006334
6335 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6336 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6337
6338 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6339 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6340
Chris Lattnerc7029802006-03-18 01:44:44 +00006341 // See if we already split it.
6342 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6343 = SplitNodes.find(Op);
6344 if (I != SplitNodes.end()) {
6345 Lo = I->second.first;
6346 Hi = I->second.second;
6347 return;
6348 }
6349
6350 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006351 default:
6352#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006353 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006354#endif
6355 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006356 case ISD::UNDEF:
6357 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6358 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6359 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006360 case ISD::BUILD_PAIR:
6361 Lo = Node->getOperand(0);
6362 Hi = Node->getOperand(1);
6363 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006364 case ISD::INSERT_VECTOR_ELT: {
6365 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6366 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6367 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006368 if (Index < NewNumElts_Lo)
6369 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006370 DAG.getConstant(Index, TLI.getPointerTy()));
6371 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006372 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6373 DAG.getConstant(Index - NewNumElts_Lo,
6374 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006375 break;
6376 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006377 case ISD::VECTOR_SHUFFLE: {
6378 // Build the low part.
6379 SDOperand Mask = Node->getOperand(2);
6380 SmallVector<SDOperand, 8> Ops;
6381 MVT::ValueType PtrVT = TLI.getPointerTy();
6382
6383 // Insert all of the elements from the input that are needed. We use
6384 // buildvector of extractelement here because the input vectors will have
6385 // to be legalized, so this makes the code simpler.
6386 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6387 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6388 SDOperand InVec = Node->getOperand(0);
6389 if (Idx >= NumElements) {
6390 InVec = Node->getOperand(1);
6391 Idx -= NumElements;
6392 }
6393 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6394 DAG.getConstant(Idx, PtrVT)));
6395 }
6396 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6397 Ops.clear();
6398
6399 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6400 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6401 SDOperand InVec = Node->getOperand(0);
6402 if (Idx >= NumElements) {
6403 InVec = Node->getOperand(1);
6404 Idx -= NumElements;
6405 }
6406 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6407 DAG.getConstant(Idx, PtrVT)));
6408 }
6409 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6410 break;
6411 }
Dan Gohman7f321562007-06-25 16:23:39 +00006412 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006413 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006414 Node->op_begin()+NewNumElts_Lo);
6415 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006416
Nate Begeman5db1afb2007-11-15 21:15:26 +00006417 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006418 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006419 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006420 break;
6421 }
Dan Gohman7f321562007-06-25 16:23:39 +00006422 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006423 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006424 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6425 if (NewNumSubvectors == 1) {
6426 Lo = Node->getOperand(0);
6427 Hi = Node->getOperand(1);
6428 } else {
6429 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6430 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006431 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006432
Dan Gohman7f321562007-06-25 16:23:39 +00006433 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6434 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006435 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006436 }
Dan Gohman65956352007-06-13 15:12:02 +00006437 break;
6438 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006439 case ISD::SELECT: {
6440 SDOperand Cond = Node->getOperand(0);
6441
6442 SDOperand LL, LH, RL, RH;
6443 SplitVectorOp(Node->getOperand(1), LL, LH);
6444 SplitVectorOp(Node->getOperand(2), RL, RH);
6445
6446 if (MVT::isVector(Cond.getValueType())) {
6447 // Handle a vector merge.
6448 SDOperand CL, CH;
6449 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006450 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6451 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006452 } else {
6453 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006454 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6455 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006456 }
6457 break;
6458 }
Dan Gohman7f321562007-06-25 16:23:39 +00006459 case ISD::ADD:
6460 case ISD::SUB:
6461 case ISD::MUL:
6462 case ISD::FADD:
6463 case ISD::FSUB:
6464 case ISD::FMUL:
6465 case ISD::SDIV:
6466 case ISD::UDIV:
6467 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006468 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006469 case ISD::AND:
6470 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006471 case ISD::XOR:
6472 case ISD::UREM:
6473 case ISD::SREM:
6474 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006475 SDOperand LL, LH, RL, RH;
6476 SplitVectorOp(Node->getOperand(0), LL, LH);
6477 SplitVectorOp(Node->getOperand(1), RL, RH);
6478
Nate Begeman5db1afb2007-11-15 21:15:26 +00006479 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6480 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006481 break;
6482 }
Dan Gohman82669522007-10-11 23:57:53 +00006483 case ISD::FPOWI: {
6484 SDOperand L, H;
6485 SplitVectorOp(Node->getOperand(0), L, H);
6486
Nate Begeman5db1afb2007-11-15 21:15:26 +00006487 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6488 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006489 break;
6490 }
6491 case ISD::CTTZ:
6492 case ISD::CTLZ:
6493 case ISD::CTPOP:
6494 case ISD::FNEG:
6495 case ISD::FABS:
6496 case ISD::FSQRT:
6497 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006498 case ISD::FCOS:
6499 case ISD::FP_TO_SINT:
6500 case ISD::FP_TO_UINT:
6501 case ISD::SINT_TO_FP:
6502 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006503 SDOperand L, H;
6504 SplitVectorOp(Node->getOperand(0), L, H);
6505
Nate Begeman5db1afb2007-11-15 21:15:26 +00006506 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6507 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006508 break;
6509 }
Dan Gohman7f321562007-06-25 16:23:39 +00006510 case ISD::LOAD: {
6511 LoadSDNode *LD = cast<LoadSDNode>(Node);
6512 SDOperand Ch = LD->getChain();
6513 SDOperand Ptr = LD->getBasePtr();
6514 const Value *SV = LD->getSrcValue();
6515 int SVOffset = LD->getSrcValueOffset();
6516 unsigned Alignment = LD->getAlignment();
6517 bool isVolatile = LD->isVolatile();
6518
Nate Begeman5db1afb2007-11-15 21:15:26 +00006519 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6520 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006521 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6522 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006523 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006524 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006525 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006526
6527 // Build a factor node to remember that this load is independent of the
6528 // other one.
6529 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6530 Hi.getValue(1));
6531
6532 // Remember that we legalized the chain.
6533 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006534 break;
6535 }
Dan Gohman7f321562007-06-25 16:23:39 +00006536 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006537 // We know the result is a vector. The input may be either a vector or a
6538 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006539 SDOperand InOp = Node->getOperand(0);
6540 if (!MVT::isVector(InOp.getValueType()) ||
6541 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6542 // The input is a scalar or single-element vector.
6543 // Lower to a store/load so that it can be split.
6544 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006545 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006546
Evan Cheng786225a2006-10-05 23:01:46 +00006547 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006548 InOp, Ptr, NULL, 0);
6549 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006550 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006551 // Split the vector and convert each of the pieces now.
6552 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006553 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6554 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006555 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006556 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006557 }
6558
6559 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006560 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006561 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006562 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006563}
6564
6565
Dan Gohman89b20c02007-06-27 14:06:22 +00006566/// ScalarizeVectorOp - Given an operand of single-element vector type
6567/// (e.g. v1f32), convert it into the equivalent operation that returns a
6568/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006569SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6570 assert(MVT::isVector(Op.getValueType()) &&
6571 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006572 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006573 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6574 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006575
Dan Gohman7f321562007-06-25 16:23:39 +00006576 // See if we already scalarized it.
6577 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6578 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006579
6580 SDOperand Result;
6581 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006582 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006583#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006584 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006585#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006586 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6587 case ISD::ADD:
6588 case ISD::FADD:
6589 case ISD::SUB:
6590 case ISD::FSUB:
6591 case ISD::MUL:
6592 case ISD::FMUL:
6593 case ISD::SDIV:
6594 case ISD::UDIV:
6595 case ISD::FDIV:
6596 case ISD::SREM:
6597 case ISD::UREM:
6598 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006599 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006600 case ISD::AND:
6601 case ISD::OR:
6602 case ISD::XOR:
6603 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006604 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006605 ScalarizeVectorOp(Node->getOperand(0)),
6606 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006607 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006608 case ISD::FNEG:
6609 case ISD::FABS:
6610 case ISD::FSQRT:
6611 case ISD::FSIN:
6612 case ISD::FCOS:
6613 Result = DAG.getNode(Node->getOpcode(),
6614 NewVT,
6615 ScalarizeVectorOp(Node->getOperand(0)));
6616 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006617 case ISD::FPOWI:
6618 Result = DAG.getNode(Node->getOpcode(),
6619 NewVT,
6620 ScalarizeVectorOp(Node->getOperand(0)),
6621 Node->getOperand(1));
6622 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006623 case ISD::LOAD: {
6624 LoadSDNode *LD = cast<LoadSDNode>(Node);
6625 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6626 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006627
Dan Gohman7f321562007-06-25 16:23:39 +00006628 const Value *SV = LD->getSrcValue();
6629 int SVOffset = LD->getSrcValueOffset();
6630 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6631 LD->isVolatile(), LD->getAlignment());
6632
Chris Lattnerc7029802006-03-18 01:44:44 +00006633 // Remember that we legalized the chain.
6634 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6635 break;
6636 }
Dan Gohman7f321562007-06-25 16:23:39 +00006637 case ISD::BUILD_VECTOR:
6638 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006639 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006640 case ISD::INSERT_VECTOR_ELT:
6641 // Returning the inserted scalar element.
6642 Result = Node->getOperand(1);
6643 break;
6644 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006645 assert(Node->getOperand(0).getValueType() == NewVT &&
6646 "Concat of non-legal vectors not yet supported!");
6647 Result = Node->getOperand(0);
6648 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006649 case ISD::VECTOR_SHUFFLE: {
6650 // Figure out if the scalar is the LHS or RHS and return it.
6651 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6652 if (cast<ConstantSDNode>(EltNum)->getValue())
6653 Result = ScalarizeVectorOp(Node->getOperand(1));
6654 else
6655 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006656 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006657 }
6658 case ISD::EXTRACT_SUBVECTOR:
6659 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006660 assert(Result.getValueType() == NewVT);
6661 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006662 case ISD::BIT_CONVERT:
6663 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006664 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006665 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006666 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006667 ScalarizeVectorOp(Op.getOperand(1)),
6668 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006669 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006670 }
6671
6672 if (TLI.isTypeLegal(NewVT))
6673 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006674 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6675 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006676 return Result;
6677}
6678
Chris Lattner3e928bb2005-01-07 07:47:09 +00006679
6680// SelectionDAG::Legalize - This is the entry point for the file.
6681//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006682void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006683 if (ViewLegalizeDAGs) viewGraph();
6684
Chris Lattner3e928bb2005-01-07 07:47:09 +00006685 /// run - This is the main entry point to this class.
6686 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006687 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006688}
6689