blob: e1540ea1d2e80ab71946d1d8c20af006ba1f6ad1 [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
Dan Gohmana3466152007-07-13 20:14:11 +0000740/// LegalizeOp - We know that the specified value has a legal type, and
741/// that its operands are legal. Now ensure that the operation itself
742/// is legal, recursively ensuring that the operands' operations remain
743/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000744SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000745 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
746 return Op;
747
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000748 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000749 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000750 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000751
Chris Lattner3e928bb2005-01-07 07:47:09 +0000752 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000753 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000754 if (Node->getNumValues() > 1) {
755 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000756 if (getTypeAction(Node->getValueType(i)) != Legal) {
757 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000758 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000759 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000760 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000761 }
762 }
763
Chris Lattner45982da2005-05-12 16:53:42 +0000764 // Note that LegalizeOp may be reentered even from single-use nodes, which
765 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000766 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000767 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000768
Nate Begeman9373a812005-08-10 20:51:12 +0000769 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000770 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000771 bool isCustom = false;
772
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000774 case ISD::FrameIndex:
775 case ISD::EntryToken:
776 case ISD::Register:
777 case ISD::BasicBlock:
778 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000779 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000780 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000781 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000782 case ISD::TargetConstantPool:
783 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000784 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000785 case ISD::TargetExternalSymbol:
786 case ISD::VALUETYPE:
787 case ISD::SRCVALUE:
788 case ISD::STRING:
789 case ISD::CONDCODE:
790 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000791 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000792 "This must be legal!");
793 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000794 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000795 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
796 // If this is a target node, legalize it by legalizing the operands then
797 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000798 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000799 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000800 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000801
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000802 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000803
804 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
805 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
806 return Result.getValue(Op.ResNo);
807 }
808 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000809#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000810 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000811#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000812 assert(0 && "Do not know how to legalize this operator!");
813 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000814 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000816 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000817 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000818 case ISD::ConstantPool:
819 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000820 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
821 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000822 case TargetLowering::Custom:
823 Tmp1 = TLI.LowerOperation(Op, DAG);
824 if (Tmp1.Val) Result = Tmp1;
825 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000826 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000827 break;
828 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000830 case ISD::FRAMEADDR:
831 case ISD::RETURNADDR:
832 // The only option for these nodes is to custom lower them. If the target
833 // does not custom lower them, then return zero.
834 Tmp1 = TLI.LowerOperation(Op, DAG);
835 if (Tmp1.Val)
836 Result = Tmp1;
837 else
838 Result = DAG.getConstant(0, TLI.getPointerTy());
839 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000840 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000841 MVT::ValueType VT = Node->getValueType(0);
842 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
843 default: assert(0 && "This action is not supported yet!");
844 case TargetLowering::Custom:
845 Result = TLI.LowerOperation(Op, DAG);
846 if (Result.Val) break;
847 // Fall Thru
848 case TargetLowering::Legal:
849 Result = DAG.getConstant(0, VT);
850 break;
851 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000852 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000853 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000854 case ISD::EXCEPTIONADDR: {
855 Tmp1 = LegalizeOp(Node->getOperand(0));
856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000860 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000861 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
862 }
863 break;
864 case TargetLowering::Custom:
865 Result = TLI.LowerOperation(Op, DAG);
866 if (Result.Val) break;
867 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000868 case TargetLowering::Legal: {
869 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
870 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
871 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000872 break;
873 }
874 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000875 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000876 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000877 case ISD::EHSELECTION: {
878 Tmp1 = LegalizeOp(Node->getOperand(0));
879 Tmp2 = LegalizeOp(Node->getOperand(1));
880 MVT::ValueType VT = Node->getValueType(0);
881 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
882 default: assert(0 && "This action is not supported yet!");
883 case TargetLowering::Expand: {
884 unsigned Reg = TLI.getExceptionSelectorRegister();
885 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
886 }
887 break;
888 case TargetLowering::Custom:
889 Result = TLI.LowerOperation(Op, DAG);
890 if (Result.Val) break;
891 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000892 case TargetLowering::Legal: {
893 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
894 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
895 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000896 break;
897 }
898 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000899 }
Jim Laskey8782d482007-02-28 20:43:58 +0000900 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000901 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000902 MVT::ValueType VT = Node->getValueType(0);
903 // The only "good" option for this node is to custom lower it.
904 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
905 default: assert(0 && "This action is not supported at all!");
906 case TargetLowering::Custom:
907 Result = TLI.LowerOperation(Op, DAG);
908 if (Result.Val) break;
909 // Fall Thru
910 case TargetLowering::Legal:
911 // Target does not know, how to lower this, lower to noop
912 Result = LegalizeOp(Node->getOperand(0));
913 break;
914 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000915 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000916 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000917 case ISD::AssertSext:
918 case ISD::AssertZext:
919 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000921 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000922 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000923 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000924 Result = Node->getOperand(Op.ResNo);
925 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000926 case ISD::CopyFromReg:
927 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000928 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000929 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000930 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000931 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000932 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000933 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000934 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
936 } else {
937 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
938 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000939 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
940 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000941 // Since CopyFromReg produces two values, make sure to remember that we
942 // legalized both of them.
943 AddLegalizedOperand(Op.getValue(0), Result);
944 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
945 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000946 case ISD::UNDEF: {
947 MVT::ValueType VT = Op.getValueType();
948 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000949 default: assert(0 && "This action is not supported yet!");
950 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000951 if (MVT::isInteger(VT))
952 Result = DAG.getConstant(0, VT);
953 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000954 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
955 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000956 else
957 assert(0 && "Unknown value type!");
958 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000959 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000960 break;
961 }
962 break;
963 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000964
Chris Lattner48b61a72006-03-28 00:40:33 +0000965 case ISD::INTRINSIC_W_CHAIN:
966 case ISD::INTRINSIC_WO_CHAIN:
967 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000968 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000969 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
970 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000971 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000972
973 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000974 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000975 TargetLowering::Custom) {
976 Tmp3 = TLI.LowerOperation(Result, DAG);
977 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000978 }
979
980 if (Result.Val->getNumValues() == 1) break;
981
982 // Must have return value and chain result.
983 assert(Result.Val->getNumValues() == 2 &&
984 "Cannot return more than two values!");
985
986 // Since loads produce two values, make sure to remember that we
987 // legalized both of them.
988 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
989 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
990 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000991 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000992
993 case ISD::LOCATION:
994 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
995 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
996
997 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
998 case TargetLowering::Promote:
999 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001000 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001001 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001002 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001003 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001004
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001005 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001006 const std::string &FName =
1007 cast<StringSDNode>(Node->getOperand(3))->getValue();
1008 const std::string &DirName =
1009 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001010 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001011
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001012 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001013 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001014 SDOperand LineOp = Node->getOperand(1);
1015 SDOperand ColOp = Node->getOperand(2);
1016
1017 if (useDEBUG_LOC) {
1018 Ops.push_back(LineOp); // line #
1019 Ops.push_back(ColOp); // col #
1020 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001021 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001022 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001023 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1024 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001025 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001026 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001027 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001028 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001029 } else {
1030 Result = Tmp1; // chain
1031 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001032 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001033 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001034 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001035 if (Tmp1 != Node->getOperand(0) ||
1036 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001037 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001038 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001039 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1040 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1041 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1042 } else {
1043 // Otherwise promote them.
1044 Ops.push_back(PromoteOp(Node->getOperand(1)));
1045 Ops.push_back(PromoteOp(Node->getOperand(2)));
1046 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001047 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1048 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001049 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001050 }
1051 break;
1052 }
1053 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001054
1055 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001056 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001057 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001058 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001059 case TargetLowering::Legal:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1061 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1062 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1063 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001064 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001065 break;
1066 }
1067 break;
1068
Jim Laskey1ee29252007-01-26 14:34:52 +00001069 case ISD::LABEL:
1070 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1071 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001072 default: assert(0 && "This action is not supported yet!");
1073 case TargetLowering::Legal:
1074 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1075 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001077 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001078 case TargetLowering::Expand:
1079 Result = LegalizeOp(Node->getOperand(0));
1080 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001081 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001082 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001083
Scott Michelc1513d22007-08-08 23:23:31 +00001084 case ISD::Constant: {
1085 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1086 unsigned opAction =
1087 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1088
Chris Lattner3e928bb2005-01-07 07:47:09 +00001089 // We know we don't need to expand constants here, constants only have one
1090 // value and we check that it is fine above.
1091
Scott Michelc1513d22007-08-08 23:23:31 +00001092 if (opAction == TargetLowering::Custom) {
1093 Tmp1 = TLI.LowerOperation(Result, DAG);
1094 if (Tmp1.Val)
1095 Result = Tmp1;
1096 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001097 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001098 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001099 case ISD::ConstantFP: {
1100 // Spill FP immediates to the constant pool if the target cannot directly
1101 // codegen them. Targets often have some immediate values that can be
1102 // efficiently generated into an FP register without a load. We explicitly
1103 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001104 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1105
1106 // Check to see if this FP immediate is already legal.
1107 bool isLegal = false;
1108 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1109 E = TLI.legal_fpimm_end(); I != E; ++I)
1110 if (CFP->isExactlyValue(*I)) {
1111 isLegal = true;
1112 break;
1113 }
1114
Chris Lattner3181a772006-01-29 06:26:56 +00001115 // If this is a legal constant, turn it into a TargetConstantFP node.
1116 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001117 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1118 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001119 break;
1120 }
1121
1122 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1123 default: assert(0 && "This action is not supported yet!");
1124 case TargetLowering::Custom:
1125 Tmp3 = TLI.LowerOperation(Result, DAG);
1126 if (Tmp3.Val) {
1127 Result = Tmp3;
1128 break;
1129 }
1130 // FALLTHROUGH
1131 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001132 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001133 }
1134 break;
1135 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001136 case ISD::TokenFactor:
1137 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001138 Tmp1 = LegalizeOp(Node->getOperand(0));
1139 Tmp2 = LegalizeOp(Node->getOperand(1));
1140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1141 } else if (Node->getNumOperands() == 3) {
1142 Tmp1 = LegalizeOp(Node->getOperand(0));
1143 Tmp2 = LegalizeOp(Node->getOperand(1));
1144 Tmp3 = LegalizeOp(Node->getOperand(2));
1145 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001146 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001147 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001148 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001149 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1150 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001151 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001152 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001153 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001154
1155 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001156 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001157 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001158 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1159 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001160
1161 // The number of incoming and outgoing values should match; unless the final
1162 // outgoing value is a flag.
1163 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1164 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1165 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1166 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001167 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001168
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001169 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001170 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001171 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001172 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1173 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001174 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001175 if (Op.ResNo == i)
1176 Tmp2 = Tmp1;
1177 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1178 }
1179 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001180 case ISD::EXTRACT_SUBREG: {
1181 Tmp1 = LegalizeOp(Node->getOperand(0));
1182 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1183 assert(idx && "Operand must be a constant");
1184 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1186 }
1187 break;
1188 case ISD::INSERT_SUBREG: {
1189 Tmp1 = LegalizeOp(Node->getOperand(0));
1190 Tmp2 = LegalizeOp(Node->getOperand(1));
1191 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1192 assert(idx && "Operand must be a constant");
1193 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1195 }
1196 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001197 case ISD::BUILD_VECTOR:
1198 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001199 default: assert(0 && "This action is not supported yet!");
1200 case TargetLowering::Custom:
1201 Tmp3 = TLI.LowerOperation(Result, DAG);
1202 if (Tmp3.Val) {
1203 Result = Tmp3;
1204 break;
1205 }
1206 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001207 case TargetLowering::Expand:
1208 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001209 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001210 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001211 break;
1212 case ISD::INSERT_VECTOR_ELT:
1213 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1215 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1217
1218 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1219 Node->getValueType(0))) {
1220 default: assert(0 && "This action is not supported yet!");
1221 case TargetLowering::Legal:
1222 break;
1223 case TargetLowering::Custom:
1224 Tmp3 = TLI.LowerOperation(Result, DAG);
1225 if (Tmp3.Val) {
1226 Result = Tmp3;
1227 break;
1228 }
1229 // FALLTHROUGH
1230 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001231 // If the insert index is a constant, codegen this as a scalar_to_vector,
1232 // then a shuffle that inserts it into the right position in the vector.
1233 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1234 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1235 Tmp1.getValueType(), Tmp2);
1236
1237 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1238 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001239 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001240
1241 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1242 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1243 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001244 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001245 for (unsigned i = 0; i != NumElts; ++i) {
1246 if (i != InsertPos->getValue())
1247 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1248 else
1249 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1250 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001251 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1252 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001253
1254 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1255 Tmp1, ScVec, ShufMask);
1256 Result = LegalizeOp(Result);
1257 break;
1258 }
1259
Chris Lattner2332b9f2006-03-19 01:17:20 +00001260 // If the target doesn't support this, we have to spill the input vector
1261 // to a temporary stack slot, update the element, then reload it. This is
1262 // badness. We could also load the value into a vector register (either
1263 // with a "move to register" or "extload into register" instruction, then
1264 // permute it into place, if the idx is a constant and if the idx is
1265 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001266 MVT::ValueType VT = Tmp1.getValueType();
1267 MVT::ValueType EltVT = Tmp2.getValueType();
1268 MVT::ValueType IdxVT = Tmp3.getValueType();
1269 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001270 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001271 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001272 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001273
1274 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001275 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1276 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001277 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001278 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1279 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1280 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001281 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001282 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001283 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001284 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001285 break;
1286 }
1287 }
1288 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001289 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001290 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1291 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1292 break;
1293 }
1294
Chris Lattnerce872152006-03-19 06:31:19 +00001295 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1296 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1297 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1298 Node->getValueType(0))) {
1299 default: assert(0 && "This action is not supported yet!");
1300 case TargetLowering::Legal:
1301 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001302 case TargetLowering::Custom:
1303 Tmp3 = TLI.LowerOperation(Result, DAG);
1304 if (Tmp3.Val) {
1305 Result = Tmp3;
1306 break;
1307 }
1308 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001309 case TargetLowering::Expand:
1310 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001311 break;
1312 }
Chris Lattnerce872152006-03-19 06:31:19 +00001313 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001314 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1316 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1318
1319 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001320 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1321 default: assert(0 && "Unknown operation action!");
1322 case TargetLowering::Legal:
1323 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1324 "vector shuffle should not be created if not legal!");
1325 break;
1326 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001327 Tmp3 = TLI.LowerOperation(Result, DAG);
1328 if (Tmp3.Val) {
1329 Result = Tmp3;
1330 break;
1331 }
1332 // FALLTHROUGH
1333 case TargetLowering::Expand: {
1334 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001335 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001336 MVT::ValueType PtrVT = TLI.getPointerTy();
1337 SDOperand Mask = Node->getOperand(2);
1338 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001339 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001340 for (unsigned i = 0; i != NumElems; ++i) {
1341 SDOperand Arg = Mask.getOperand(i);
1342 if (Arg.getOpcode() == ISD::UNDEF) {
1343 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1344 } else {
1345 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1346 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1347 if (Idx < NumElems)
1348 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1349 DAG.getConstant(Idx, PtrVT)));
1350 else
1351 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1352 DAG.getConstant(Idx - NumElems, PtrVT)));
1353 }
1354 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001355 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001356 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001357 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001358 case TargetLowering::Promote: {
1359 // Change base type to a different vector type.
1360 MVT::ValueType OVT = Node->getValueType(0);
1361 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1362
1363 // Cast the two input vectors.
1364 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1365 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1366
1367 // Convert the shuffle mask to the right # elements.
1368 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1369 assert(Tmp3.Val && "Shuffle not legal?");
1370 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1371 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1372 break;
1373 }
Chris Lattner87100e02006-03-20 01:52:29 +00001374 }
1375 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001376
1377 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001378 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001379 Tmp2 = LegalizeOp(Node->getOperand(1));
1380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001381 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001382 break;
1383
Dan Gohman7f321562007-06-25 16:23:39 +00001384 case ISD::EXTRACT_SUBVECTOR:
1385 Tmp1 = Node->getOperand(0);
1386 Tmp2 = LegalizeOp(Node->getOperand(1));
1387 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1388 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001389 break;
1390
Chris Lattner6831a812006-02-13 09:18:02 +00001391 case ISD::CALLSEQ_START: {
1392 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1393
1394 // Recursively Legalize all of the inputs of the call end that do not lead
1395 // to this call start. This ensures that any libcalls that need be inserted
1396 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001397 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001398 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001399 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1400 NodesLeadingTo);
1401 }
Chris Lattner6831a812006-02-13 09:18:02 +00001402
1403 // Now that we legalized all of the inputs (which may have inserted
1404 // libcalls) create the new CALLSEQ_START node.
1405 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1406
1407 // Merge in the last call, to ensure that this call start after the last
1408 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001409 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001410 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1411 Tmp1 = LegalizeOp(Tmp1);
1412 }
Chris Lattner6831a812006-02-13 09:18:02 +00001413
1414 // Do not try to legalize the target-specific arguments (#1+).
1415 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001416 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001417 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001418 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001419 }
1420
1421 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001422 AddLegalizedOperand(Op.getValue(0), Result);
1423 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1424 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1425
Chris Lattner6831a812006-02-13 09:18:02 +00001426 // Now that the callseq_start and all of the non-call nodes above this call
1427 // sequence have been legalized, legalize the call itself. During this
1428 // process, no libcalls can/will be inserted, guaranteeing that no calls
1429 // can overlap.
1430 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1431 SDOperand InCallSEQ = LastCALLSEQ_END;
1432 // Note that we are selecting this call!
1433 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1434 IsLegalizingCall = true;
1435
1436 // Legalize the call, starting from the CALLSEQ_END.
1437 LegalizeOp(LastCALLSEQ_END);
1438 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1439 return Result;
1440 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001441 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001442 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1443 // will cause this node to be legalized as well as handling libcalls right.
1444 if (LastCALLSEQ_END.Val != Node) {
1445 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001446 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001447 assert(I != LegalizedNodes.end() &&
1448 "Legalizing the call start should have legalized this node!");
1449 return I->second;
1450 }
1451
1452 // Otherwise, the call start has been legalized and everything is going
1453 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001454 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001455 // Do not try to legalize the target-specific arguments (#1+), except for
1456 // an optional flag input.
1457 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1458 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001459 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001460 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001461 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001462 }
1463 } else {
1464 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1465 if (Tmp1 != Node->getOperand(0) ||
1466 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001467 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001468 Ops[0] = Tmp1;
1469 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001470 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001471 }
Chris Lattner6a542892006-01-24 05:48:21 +00001472 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001473 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001474 // This finishes up call legalization.
1475 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001476
1477 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1478 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1479 if (Node->getNumValues() == 2)
1480 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1481 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001482 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001483 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1485 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1486 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001487 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001488
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001489 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001490 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001491 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001492 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001493 case TargetLowering::Expand: {
1494 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1495 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1496 " not tell us which reg is the stack pointer!");
1497 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001498
1499 // Chain the dynamic stack allocation so that it doesn't modify the stack
1500 // pointer when other instructions are using the stack.
1501 Chain = DAG.getCALLSEQ_START(Chain,
1502 DAG.getConstant(0, TLI.getPointerTy()));
1503
Chris Lattner903d2782006-01-15 08:54:32 +00001504 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001505 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1506 Chain = SP.getValue(1);
1507 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1508 unsigned StackAlign =
1509 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1510 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001511 SP = DAG.getNode(ISD::AND, VT, SP,
1512 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001513 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001514 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1515
1516 Tmp2 =
1517 DAG.getCALLSEQ_END(Chain,
1518 DAG.getConstant(0, TLI.getPointerTy()),
1519 DAG.getConstant(0, TLI.getPointerTy()),
1520 SDOperand());
1521
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001522 Tmp1 = LegalizeOp(Tmp1);
1523 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001524 break;
1525 }
1526 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001527 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1528 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001529 Tmp1 = LegalizeOp(Tmp3);
1530 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001531 }
Chris Lattner903d2782006-01-15 08:54:32 +00001532 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001533 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001534 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001535 }
Chris Lattner903d2782006-01-15 08:54:32 +00001536 // Since this op produce two values, make sure to remember that we
1537 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001538 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1539 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001540 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001541 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001542 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001543 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001544 bool Changed = false;
1545 // Legalize all of the operands of the inline asm, in case they are nodes
1546 // that need to be expanded or something. Note we skip the asm string and
1547 // all of the TargetConstant flags.
1548 SDOperand Op = LegalizeOp(Ops[0]);
1549 Changed = Op != Ops[0];
1550 Ops[0] = Op;
1551
1552 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1553 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1554 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1555 for (++i; NumVals; ++i, --NumVals) {
1556 SDOperand Op = LegalizeOp(Ops[i]);
1557 if (Op != Ops[i]) {
1558 Changed = true;
1559 Ops[i] = Op;
1560 }
1561 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001562 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001563
1564 if (HasInFlag) {
1565 Op = LegalizeOp(Ops.back());
1566 Changed |= Op != Ops.back();
1567 Ops.back() = Op;
1568 }
1569
1570 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001571 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001572
1573 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001574 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001575 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1576 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001577 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001578 case ISD::BR:
1579 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001580 // Ensure that libcalls are emitted before a branch.
1581 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1582 Tmp1 = LegalizeOp(Tmp1);
1583 LastCALLSEQ_END = DAG.getEntryNode();
1584
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001585 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001586 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001587 case ISD::BRIND:
1588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1589 // Ensure that libcalls are emitted before a branch.
1590 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1591 Tmp1 = LegalizeOp(Tmp1);
1592 LastCALLSEQ_END = DAG.getEntryNode();
1593
1594 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1595 default: assert(0 && "Indirect target must be legal type (pointer)!");
1596 case Legal:
1597 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1598 break;
1599 }
1600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1601 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001602 case ISD::BR_JT:
1603 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1604 // Ensure that libcalls are emitted before a branch.
1605 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1606 Tmp1 = LegalizeOp(Tmp1);
1607 LastCALLSEQ_END = DAG.getEntryNode();
1608
1609 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1611
1612 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1613 default: assert(0 && "This action is not supported yet!");
1614 case TargetLowering::Legal: break;
1615 case TargetLowering::Custom:
1616 Tmp1 = TLI.LowerOperation(Result, DAG);
1617 if (Tmp1.Val) Result = Tmp1;
1618 break;
1619 case TargetLowering::Expand: {
1620 SDOperand Chain = Result.getOperand(0);
1621 SDOperand Table = Result.getOperand(1);
1622 SDOperand Index = Result.getOperand(2);
1623
1624 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001625 MachineFunction &MF = DAG.getMachineFunction();
1626 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001627 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1628 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001629
1630 SDOperand LD;
1631 switch (EntrySize) {
1632 default: assert(0 && "Size of jump table not supported yet."); break;
1633 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1634 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1635 }
1636
Evan Chengcc415862007-11-09 01:32:10 +00001637 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001638 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001639 // For PIC, the sequence is:
1640 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001641 // RelocBase can be JumpTable, GOT or some sort of global base.
1642 if (PTy != MVT::i32)
1643 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1644 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1645 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001646 }
Evan Chengcc415862007-11-09 01:32:10 +00001647 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001648 }
1649 }
1650 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001651 case ISD::BRCOND:
1652 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001653 // Ensure that libcalls are emitted before a return.
1654 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1655 Tmp1 = LegalizeOp(Tmp1);
1656 LastCALLSEQ_END = DAG.getEntryNode();
1657
Chris Lattner47e92232005-01-18 19:27:06 +00001658 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1659 case Expand: assert(0 && "It's impossible to expand bools");
1660 case Legal:
1661 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1662 break;
1663 case Promote:
1664 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001665
1666 // The top bits of the promoted condition are not necessarily zero, ensure
1667 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001668 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001669 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1670 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001671 break;
1672 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001673
1674 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001676
1677 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1678 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001679 case TargetLowering::Legal: break;
1680 case TargetLowering::Custom:
1681 Tmp1 = TLI.LowerOperation(Result, DAG);
1682 if (Tmp1.Val) Result = Tmp1;
1683 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001684 case TargetLowering::Expand:
1685 // Expand brcond's setcc into its constituent parts and create a BR_CC
1686 // Node.
1687 if (Tmp2.getOpcode() == ISD::SETCC) {
1688 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1689 Tmp2.getOperand(0), Tmp2.getOperand(1),
1690 Node->getOperand(2));
1691 } else {
1692 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1693 DAG.getCondCode(ISD::SETNE), Tmp2,
1694 DAG.getConstant(0, Tmp2.getValueType()),
1695 Node->getOperand(2));
1696 }
1697 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001698 }
1699 break;
1700 case ISD::BR_CC:
1701 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001702 // Ensure that libcalls are emitted before a branch.
1703 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1704 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001705 Tmp2 = Node->getOperand(2); // LHS
1706 Tmp3 = Node->getOperand(3); // RHS
1707 Tmp4 = Node->getOperand(1); // CC
1708
1709 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001710 LastCALLSEQ_END = DAG.getEntryNode();
1711
Nate Begeman750ac1b2006-02-01 07:19:44 +00001712 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1713 // the LHS is a legal SETCC itself. In this case, we need to compare
1714 // the result against zero to select between true and false values.
1715 if (Tmp3.Val == 0) {
1716 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1717 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001718 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001719
1720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1721 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001722
Chris Lattner181b7a32005-12-17 23:46:46 +00001723 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1724 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001725 case TargetLowering::Legal: break;
1726 case TargetLowering::Custom:
1727 Tmp4 = TLI.LowerOperation(Result, DAG);
1728 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001729 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001730 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001731 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001732 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001733 LoadSDNode *LD = cast<LoadSDNode>(Node);
1734 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1735 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001736
Evan Cheng466685d2006-10-09 20:57:25 +00001737 ISD::LoadExtType ExtType = LD->getExtensionType();
1738 if (ExtType == ISD::NON_EXTLOAD) {
1739 MVT::ValueType VT = Node->getValueType(0);
1740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1741 Tmp3 = Result.getValue(0);
1742 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001743
Evan Cheng466685d2006-10-09 20:57:25 +00001744 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1745 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001746 case TargetLowering::Legal:
1747 // If this is an unaligned load and the target doesn't support it,
1748 // expand it.
1749 if (!TLI.allowsUnalignedMemoryAccesses()) {
1750 unsigned ABIAlignment = TLI.getTargetData()->
1751 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1752 if (LD->getAlignment() < ABIAlignment){
1753 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1754 TLI);
1755 Tmp3 = Result.getOperand(0);
1756 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001757 Tmp3 = LegalizeOp(Tmp3);
1758 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001759 }
1760 }
1761 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001762 case TargetLowering::Custom:
1763 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1764 if (Tmp1.Val) {
1765 Tmp3 = LegalizeOp(Tmp1);
1766 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001767 }
Evan Cheng466685d2006-10-09 20:57:25 +00001768 break;
1769 case TargetLowering::Promote: {
1770 // Only promote a load of vector type to another.
1771 assert(MVT::isVector(VT) && "Cannot promote this load!");
1772 // Change base type to a different vector type.
1773 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1774
1775 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001776 LD->getSrcValueOffset(),
1777 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001778 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1779 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001780 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001781 }
Evan Cheng466685d2006-10-09 20:57:25 +00001782 }
1783 // Since loads produce two values, make sure to remember that we
1784 // legalized both of them.
1785 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1786 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1787 return Op.ResNo ? Tmp4 : Tmp3;
1788 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001789 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001790 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1791 default: assert(0 && "This action is not supported yet!");
1792 case TargetLowering::Promote:
1793 assert(SrcVT == MVT::i1 &&
1794 "Can only promote extending LOAD from i1 -> i8!");
1795 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1796 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001797 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001798 Tmp1 = Result.getValue(0);
1799 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001800 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001801 case TargetLowering::Custom:
1802 isCustom = true;
1803 // FALLTHROUGH
1804 case TargetLowering::Legal:
1805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1806 Tmp1 = Result.getValue(0);
1807 Tmp2 = Result.getValue(1);
1808
1809 if (isCustom) {
1810 Tmp3 = TLI.LowerOperation(Result, DAG);
1811 if (Tmp3.Val) {
1812 Tmp1 = LegalizeOp(Tmp3);
1813 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1814 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001815 } else {
1816 // If this is an unaligned load and the target doesn't support it,
1817 // expand it.
1818 if (!TLI.allowsUnalignedMemoryAccesses()) {
1819 unsigned ABIAlignment = TLI.getTargetData()->
1820 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1821 if (LD->getAlignment() < ABIAlignment){
1822 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1823 TLI);
1824 Tmp1 = Result.getOperand(0);
1825 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001826 Tmp1 = LegalizeOp(Tmp1);
1827 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001828 }
1829 }
Evan Cheng466685d2006-10-09 20:57:25 +00001830 }
1831 break;
1832 case TargetLowering::Expand:
1833 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1834 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1835 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001836 LD->getSrcValueOffset(),
1837 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001838 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1839 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1840 Tmp2 = LegalizeOp(Load.getValue(1));
1841 break;
1842 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001843 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001844 // Turn the unsupported load into an EXTLOAD followed by an explicit
1845 // zero/sign extend inreg.
1846 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1847 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001848 LD->getSrcValueOffset(), SrcVT,
1849 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001850 SDOperand ValRes;
1851 if (ExtType == ISD::SEXTLOAD)
1852 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1853 Result, DAG.getValueType(SrcVT));
1854 else
1855 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1856 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1857 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1858 break;
1859 }
1860 // Since loads produce two values, make sure to remember that we legalized
1861 // both of them.
1862 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1863 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1864 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001865 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001866 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001867 case ISD::EXTRACT_ELEMENT: {
1868 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1869 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001870 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001871 case Legal:
1872 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1873 // 1 -> Hi
1874 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1875 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1876 TLI.getShiftAmountTy()));
1877 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1878 } else {
1879 // 0 -> Lo
1880 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1881 Node->getOperand(0));
1882 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001883 break;
1884 case Expand:
1885 // Get both the low and high parts.
1886 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1887 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1888 Result = Tmp2; // 1 -> Hi
1889 else
1890 Result = Tmp1; // 0 -> Lo
1891 break;
1892 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001893 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001894 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001895
1896 case ISD::CopyToReg:
1897 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001898
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001899 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001900 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001901 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001902 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001903 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001905 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001906 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001907 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001908 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1910 Tmp3);
1911 } else {
1912 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001913 }
1914
1915 // Since this produces two values, make sure to remember that we legalized
1916 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001917 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001918 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001919 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001920 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001921 break;
1922
1923 case ISD::RET:
1924 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001925
1926 // Ensure that libcalls are emitted before a return.
1927 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1928 Tmp1 = LegalizeOp(Tmp1);
1929 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001930
Chris Lattner3e928bb2005-01-07 07:47:09 +00001931 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001932 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001933 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001934 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001935 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001936 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001937 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001938 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001939 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001940 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001941 SDOperand Lo, Hi;
1942 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001943
1944 // Big endian systems want the hi reg first.
1945 if (!TLI.isLittleEndian())
1946 std::swap(Lo, Hi);
1947
Evan Cheng13acce32006-12-11 19:27:14 +00001948 if (Hi.Val)
1949 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1950 else
1951 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001952 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001953 } else {
1954 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00001955 int InIx = Tmp2.ResNo;
1956 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1957 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001958
Dan Gohman7f321562007-06-25 16:23:39 +00001959 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001960 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001961 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001962 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001963 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001964 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001966 } else if (NumElems == 1) {
1967 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001968 Tmp2 = ScalarizeVectorOp(Tmp2);
1969 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001971
1972 // FIXME: Returns of gcc generic vectors smaller than a legal type
1973 // should be returned in integer registers!
1974
Chris Lattnerf87324e2006-04-11 01:31:51 +00001975 // The scalarized value type may not be legal, e.g. it might require
1976 // promotion or expansion. Relegalize the return.
1977 Result = LegalizeOp(Result);
1978 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001979 // FIXME: Returns of gcc generic vectors larger than a legal vector
1980 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001981 SDOperand Lo, Hi;
1982 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001983 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001984 Result = LegalizeOp(Result);
1985 }
1986 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001987 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001988 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001989 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001991 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001992 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001993 }
1994 break;
1995 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001996 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001997 break;
1998 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001999 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002000 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002001 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002002 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2003 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002004 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002005 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002006 break;
2007 case Expand: {
2008 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002009 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002010 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002011 ExpandOp(Node->getOperand(i), Lo, Hi);
2012 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002013 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002014 if (Hi.Val) {
2015 NewValues.push_back(Hi);
2016 NewValues.push_back(Node->getOperand(i+1));
2017 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002018 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002019 }
2020 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002021 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002022 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002023
2024 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002025 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002026 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002027 Result = DAG.getNode(ISD::RET, MVT::Other,
2028 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002029 break;
2030 }
2031 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002032
Chris Lattner6862dbc2006-01-29 21:02:23 +00002033 if (Result.getOpcode() == ISD::RET) {
2034 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2035 default: assert(0 && "This action is not supported yet!");
2036 case TargetLowering::Legal: break;
2037 case TargetLowering::Custom:
2038 Tmp1 = TLI.LowerOperation(Result, DAG);
2039 if (Tmp1.Val) Result = Tmp1;
2040 break;
2041 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002042 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002043 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002044 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002045 StoreSDNode *ST = cast<StoreSDNode>(Node);
2046 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2047 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002048 int SVOffset = ST->getSrcValueOffset();
2049 unsigned Alignment = ST->getAlignment();
2050 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002051
Evan Cheng8b2794a2006-10-13 21:14:26 +00002052 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002053 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2054 // FIXME: We shouldn't do this for TargetConstantFP's.
2055 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2056 // to phase ordering between legalized code and the dag combiner. This
2057 // probably means that we need to integrate dag combiner and legalizer
2058 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002059 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002060 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002061 if (CFP->getValueType(0) == MVT::f32 &&
2062 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002063 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2064 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002065 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002066 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2067 SVOffset, isVolatile, Alignment);
2068 break;
2069 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002070 // If this target supports 64-bit registers, do a single 64-bit store.
2071 if (getTypeAction(MVT::i64) == Legal) {
2072 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2073 getZExtValue(), MVT::i64);
2074 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2075 SVOffset, isVolatile, Alignment);
2076 break;
2077 } else if (getTypeAction(MVT::i32) == Legal) {
2078 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2079 // stores. If the target supports neither 32- nor 64-bits, this
2080 // xform is certainly not worth it.
2081 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2082 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2083 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2084 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2085
2086 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2087 SVOffset, isVolatile, Alignment);
2088 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2089 getIntPtrConstant(4));
2090 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002091 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002092
2093 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2094 break;
2095 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002096 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002097 }
2098
Evan Cheng8b2794a2006-10-13 21:14:26 +00002099 switch (getTypeAction(ST->getStoredVT())) {
2100 case Legal: {
2101 Tmp3 = LegalizeOp(ST->getValue());
2102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2103 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002104
Evan Cheng8b2794a2006-10-13 21:14:26 +00002105 MVT::ValueType VT = Tmp3.getValueType();
2106 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2107 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002108 case TargetLowering::Legal:
2109 // If this is an unaligned store and the target doesn't support it,
2110 // expand it.
2111 if (!TLI.allowsUnalignedMemoryAccesses()) {
2112 unsigned ABIAlignment = TLI.getTargetData()->
2113 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2114 if (ST->getAlignment() < ABIAlignment)
2115 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2116 TLI);
2117 }
2118 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002119 case TargetLowering::Custom:
2120 Tmp1 = TLI.LowerOperation(Result, DAG);
2121 if (Tmp1.Val) Result = Tmp1;
2122 break;
2123 case TargetLowering::Promote:
2124 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2125 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2126 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2127 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002128 ST->getSrcValue(), SVOffset, isVolatile,
2129 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002130 break;
2131 }
2132 break;
2133 }
2134 case Promote:
2135 // Truncate the value and store the result.
2136 Tmp3 = PromoteOp(ST->getValue());
2137 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002138 SVOffset, ST->getStoredVT(),
2139 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002140 break;
2141
2142 case Expand:
2143 unsigned IncrementSize = 0;
2144 SDOperand Lo, Hi;
2145
2146 // If this is a vector type, then we have to calculate the increment as
2147 // the product of the element size in bytes, and the number of elements
2148 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002149 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002150 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002151 int InIx = ST->getValue().ResNo;
2152 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2153 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002154
Dan Gohman7f321562007-06-25 16:23:39 +00002155 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002156 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002157 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002158 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002159 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002160 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002161 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002162 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002163 Result = LegalizeOp(Result);
2164 break;
2165 } else if (NumElems == 1) {
2166 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002167 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002168 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002169 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002170 // The scalarized value type may not be legal, e.g. it might require
2171 // promotion or expansion. Relegalize the scalar store.
2172 Result = LegalizeOp(Result);
2173 break;
2174 } else {
2175 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002176 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2177 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002178 }
2179 } else {
2180 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002181 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002182
2183 if (!TLI.isLittleEndian())
2184 std::swap(Lo, Hi);
2185 }
2186
2187 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002188 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002189
2190 if (Hi.Val == NULL) {
2191 // Must be int <-> float one-to-one expansion.
2192 Result = Lo;
2193 break;
2194 }
2195
Evan Cheng8b2794a2006-10-13 21:14:26 +00002196 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2197 getIntPtrConstant(IncrementSize));
2198 assert(isTypeLegal(Tmp2.getValueType()) &&
2199 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002200 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002201 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002202 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002203 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002204 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2205 break;
2206 }
2207 } else {
2208 // Truncating store
2209 assert(isTypeLegal(ST->getValue().getValueType()) &&
2210 "Cannot handle illegal TRUNCSTORE yet!");
2211 Tmp3 = LegalizeOp(ST->getValue());
2212
2213 // The only promote case we handle is TRUNCSTORE:i1 X into
2214 // -> TRUNCSTORE:i8 (and X, 1)
2215 if (ST->getStoredVT() == MVT::i1 &&
2216 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2217 // Promote the bool to a mask then store.
2218 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2219 DAG.getConstant(1, Tmp3.getValueType()));
2220 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002221 SVOffset, MVT::i8,
2222 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002223 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2224 Tmp2 != ST->getBasePtr()) {
2225 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2226 ST->getOffset());
2227 }
2228
2229 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2230 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002231 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002232 case TargetLowering::Legal:
2233 // If this is an unaligned store and the target doesn't support it,
2234 // expand it.
2235 if (!TLI.allowsUnalignedMemoryAccesses()) {
2236 unsigned ABIAlignment = TLI.getTargetData()->
2237 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2238 if (ST->getAlignment() < ABIAlignment)
2239 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2240 TLI);
2241 }
2242 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002243 case TargetLowering::Custom:
2244 Tmp1 = TLI.LowerOperation(Result, DAG);
2245 if (Tmp1.Val) Result = Tmp1;
2246 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002247 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002248 }
2249 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002250 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002251 case ISD::PCMARKER:
2252 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002254 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002255 case ISD::STACKSAVE:
2256 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002257 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2258 Tmp1 = Result.getValue(0);
2259 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002260
Chris Lattner140d53c2006-01-13 02:50:02 +00002261 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2262 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002263 case TargetLowering::Legal: break;
2264 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002265 Tmp3 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp3.Val) {
2267 Tmp1 = LegalizeOp(Tmp3);
2268 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002269 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002270 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002271 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002272 // Expand to CopyFromReg if the target set
2273 // StackPointerRegisterToSaveRestore.
2274 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002275 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002276 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002277 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002278 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2280 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002281 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002282 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002283 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002284
2285 // Since stacksave produce two values, make sure to remember that we
2286 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002287 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2288 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2289 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002290
Chris Lattner140d53c2006-01-13 02:50:02 +00002291 case ISD::STACKRESTORE:
2292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002294 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002295
2296 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2297 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002298 case TargetLowering::Legal: break;
2299 case TargetLowering::Custom:
2300 Tmp1 = TLI.LowerOperation(Result, DAG);
2301 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002302 break;
2303 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002304 // Expand to CopyToReg if the target set
2305 // StackPointerRegisterToSaveRestore.
2306 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2307 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2308 } else {
2309 Result = Tmp1;
2310 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002311 break;
2312 }
2313 break;
2314
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002315 case ISD::READCYCLECOUNTER:
2316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002317 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002318 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2319 Node->getValueType(0))) {
2320 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002321 case TargetLowering::Legal:
2322 Tmp1 = Result.getValue(0);
2323 Tmp2 = Result.getValue(1);
2324 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002325 case TargetLowering::Custom:
2326 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002327 Tmp1 = LegalizeOp(Result.getValue(0));
2328 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002329 break;
2330 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002331
2332 // Since rdcc produce two values, make sure to remember that we legalized
2333 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002334 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2335 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002336 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002337
Chris Lattner2ee743f2005-01-14 22:08:15 +00002338 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002339 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2340 case Expand: assert(0 && "It's impossible to expand bools");
2341 case Legal:
2342 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2343 break;
2344 case Promote:
2345 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002346 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002347 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002348 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2349 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002350 break;
2351 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002352 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002353 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002354
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002355 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002356
Nate Begemanb942a3d2005-08-23 04:29:48 +00002357 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002358 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002359 case TargetLowering::Legal: break;
2360 case TargetLowering::Custom: {
2361 Tmp1 = TLI.LowerOperation(Result, DAG);
2362 if (Tmp1.Val) Result = Tmp1;
2363 break;
2364 }
Nate Begeman9373a812005-08-10 20:51:12 +00002365 case TargetLowering::Expand:
2366 if (Tmp1.getOpcode() == ISD::SETCC) {
2367 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2368 Tmp2, Tmp3,
2369 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2370 } else {
2371 Result = DAG.getSelectCC(Tmp1,
2372 DAG.getConstant(0, Tmp1.getValueType()),
2373 Tmp2, Tmp3, ISD::SETNE);
2374 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002375 break;
2376 case TargetLowering::Promote: {
2377 MVT::ValueType NVT =
2378 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2379 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002380 if (MVT::isVector(Tmp2.getValueType())) {
2381 ExtOp = ISD::BIT_CONVERT;
2382 TruncOp = ISD::BIT_CONVERT;
2383 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002384 ExtOp = ISD::ANY_EXTEND;
2385 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002386 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002387 ExtOp = ISD::FP_EXTEND;
2388 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002389 }
2390 // Promote each of the values to the new type.
2391 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2392 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2393 // Perform the larger operation, then round down.
2394 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2395 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2396 break;
2397 }
2398 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002399 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002400 case ISD::SELECT_CC: {
2401 Tmp1 = Node->getOperand(0); // LHS
2402 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002403 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2404 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002405 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002406
Nate Begeman750ac1b2006-02-01 07:19:44 +00002407 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2408
2409 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2410 // the LHS is a legal SETCC itself. In this case, we need to compare
2411 // the result against zero to select between true and false values.
2412 if (Tmp2.Val == 0) {
2413 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2414 CC = DAG.getCondCode(ISD::SETNE);
2415 }
2416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2417
2418 // Everything is legal, see if we should expand this op or something.
2419 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2420 default: assert(0 && "This action is not supported yet!");
2421 case TargetLowering::Legal: break;
2422 case TargetLowering::Custom:
2423 Tmp1 = TLI.LowerOperation(Result, DAG);
2424 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002425 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002426 }
2427 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002428 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002429 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002430 Tmp1 = Node->getOperand(0);
2431 Tmp2 = Node->getOperand(1);
2432 Tmp3 = Node->getOperand(2);
2433 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2434
2435 // If we had to Expand the SetCC operands into a SELECT node, then it may
2436 // not always be possible to return a true LHS & RHS. In this case, just
2437 // return the value we legalized, returned in the LHS
2438 if (Tmp2.Val == 0) {
2439 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002440 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002441 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002442
Chris Lattner73e142f2006-01-30 22:43:50 +00002443 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002444 default: assert(0 && "Cannot handle this action for SETCC yet!");
2445 case TargetLowering::Custom:
2446 isCustom = true;
2447 // FALLTHROUGH.
2448 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002449 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002450 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002451 Tmp4 = TLI.LowerOperation(Result, DAG);
2452 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002453 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002454 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002455 case TargetLowering::Promote: {
2456 // First step, figure out the appropriate operation to use.
2457 // Allow SETCC to not be supported for all legal data types
2458 // Mostly this targets FP
2459 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002460 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002461
2462 // Scan for the appropriate larger type to use.
2463 while (1) {
2464 NewInTy = (MVT::ValueType)(NewInTy+1);
2465
2466 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2467 "Fell off of the edge of the integer world");
2468 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2469 "Fell off of the edge of the floating point world");
2470
2471 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002472 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002473 break;
2474 }
2475 if (MVT::isInteger(NewInTy))
2476 assert(0 && "Cannot promote Legal Integer SETCC yet");
2477 else {
2478 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2479 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2480 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002481 Tmp1 = LegalizeOp(Tmp1);
2482 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002483 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002484 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002485 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002486 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002487 case TargetLowering::Expand:
2488 // Expand a setcc node into a select_cc of the same condition, lhs, and
2489 // rhs that selects between const 1 (true) and const 0 (false).
2490 MVT::ValueType VT = Node->getValueType(0);
2491 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2492 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002493 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002494 break;
2495 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002496 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002497 case ISD::MEMSET:
2498 case ISD::MEMCPY:
2499 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002500 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002501 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2502
2503 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2504 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2505 case Expand: assert(0 && "Cannot expand a byte!");
2506 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002507 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002508 break;
2509 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002510 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002511 break;
2512 }
2513 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002514 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002515 }
Chris Lattner272455b2005-02-02 03:44:41 +00002516
2517 SDOperand Tmp4;
2518 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002519 case Expand: {
2520 // Length is too big, just take the lo-part of the length.
2521 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002522 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002523 break;
2524 }
Chris Lattnere5605212005-01-28 22:29:18 +00002525 case Legal:
2526 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002527 break;
2528 case Promote:
2529 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002530 break;
2531 }
2532
2533 SDOperand Tmp5;
2534 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2535 case Expand: assert(0 && "Cannot expand this yet!");
2536 case Legal:
2537 Tmp5 = LegalizeOp(Node->getOperand(4));
2538 break;
2539 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002540 Tmp5 = PromoteOp(Node->getOperand(4));
2541 break;
2542 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002543
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002544 SDOperand Tmp6;
2545 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2546 case Expand: assert(0 && "Cannot expand this yet!");
2547 case Legal:
2548 Tmp6 = LegalizeOp(Node->getOperand(5));
2549 break;
2550 case Promote:
2551 Tmp6 = PromoteOp(Node->getOperand(5));
2552 break;
2553 }
2554
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002555 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2556 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002557 case TargetLowering::Custom:
2558 isCustom = true;
2559 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002560 case TargetLowering::Legal: {
2561 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2562 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002563 if (isCustom) {
2564 Tmp1 = TLI.LowerOperation(Result, DAG);
2565 if (Tmp1.Val) Result = Tmp1;
2566 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002567 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002568 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002569 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002570 // Otherwise, the target does not support this operation. Lower the
2571 // operation to an explicit libcall as appropriate.
2572 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002573 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002574 TargetLowering::ArgListTy Args;
2575 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002576
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002577 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002578 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002579 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002580 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002581 // Extend the (previously legalized) ubyte argument to be an int value
2582 // for the call.
2583 if (Tmp3.getValueType() > MVT::i32)
2584 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2585 else
2586 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002587 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002588 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002589 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002590 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002591
2592 FnName = "memset";
2593 } else if (Node->getOpcode() == ISD::MEMCPY ||
2594 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002595 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002596 Entry.Node = Tmp2; Args.push_back(Entry);
2597 Entry.Node = Tmp3; Args.push_back(Entry);
2598 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002599 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2600 } else {
2601 assert(0 && "Unknown op!");
2602 }
Chris Lattner45982da2005-05-12 16:53:42 +00002603
Chris Lattnere1bd8222005-01-11 05:57:22 +00002604 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002605 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002606 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002607 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002608 break;
2609 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002610 }
2611 break;
2612 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002613
Chris Lattner5b359c62005-04-02 04:00:59 +00002614 case ISD::SHL_PARTS:
2615 case ISD::SRA_PARTS:
2616 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002617 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002618 bool Changed = false;
2619 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2620 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2621 Changed |= Ops.back() != Node->getOperand(i);
2622 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002623 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002624 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002625
Evan Cheng05a2d562006-01-09 18:31:59 +00002626 switch (TLI.getOperationAction(Node->getOpcode(),
2627 Node->getValueType(0))) {
2628 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002629 case TargetLowering::Legal: break;
2630 case TargetLowering::Custom:
2631 Tmp1 = TLI.LowerOperation(Result, DAG);
2632 if (Tmp1.Val) {
2633 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002634 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002635 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002636 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2637 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002638 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002639 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002640 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002641 return RetVal;
2642 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002643 break;
2644 }
2645
Chris Lattner2c8086f2005-04-02 05:00:07 +00002646 // Since these produce multiple values, make sure to remember that we
2647 // legalized all of them.
2648 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2649 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2650 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002651 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002652
2653 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002654 case ISD::ADD:
2655 case ISD::SUB:
2656 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002657 case ISD::MULHS:
2658 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002659 case ISD::UDIV:
2660 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002661 case ISD::AND:
2662 case ISD::OR:
2663 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002664 case ISD::SHL:
2665 case ISD::SRL:
2666 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002667 case ISD::FADD:
2668 case ISD::FSUB:
2669 case ISD::FMUL:
2670 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002671 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002672 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002673 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2674 case Expand: assert(0 && "Not possible");
2675 case Legal:
2676 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2677 break;
2678 case Promote:
2679 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2680 break;
2681 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002682
2683 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002684
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002685 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002686 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002687 case TargetLowering::Legal: break;
2688 case TargetLowering::Custom:
2689 Tmp1 = TLI.LowerOperation(Result, DAG);
2690 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002691 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002692 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002693 MVT::ValueType VT = Op.getValueType();
2694
2695 // See if multiply or divide can be lowered using two-result operations.
2696 SDVTList VTs = DAG.getVTList(VT, VT);
2697 if (Node->getOpcode() == ISD::MUL) {
2698 // We just need the low half of the multiply; try both the signed
2699 // and unsigned forms. If the target supports both SMUL_LOHI and
2700 // UMUL_LOHI, form a preference by checking which forms of plain
2701 // MULH it supports.
2702 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2703 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2704 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2705 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2706 unsigned OpToUse = 0;
2707 if (HasSMUL_LOHI && !HasMULHS) {
2708 OpToUse = ISD::SMUL_LOHI;
2709 } else if (HasUMUL_LOHI && !HasMULHU) {
2710 OpToUse = ISD::UMUL_LOHI;
2711 } else if (HasSMUL_LOHI) {
2712 OpToUse = ISD::SMUL_LOHI;
2713 } else if (HasUMUL_LOHI) {
2714 OpToUse = ISD::UMUL_LOHI;
2715 }
2716 if (OpToUse) {
2717 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2718 break;
2719 }
2720 }
2721 if (Node->getOpcode() == ISD::MULHS &&
2722 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2723 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2724 break;
2725 }
2726 if (Node->getOpcode() == ISD::MULHU &&
2727 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2728 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2729 break;
2730 }
2731 if (Node->getOpcode() == ISD::SDIV &&
2732 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2733 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2734 break;
2735 }
2736 if (Node->getOpcode() == ISD::UDIV &&
2737 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2738 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2739 break;
2740 }
2741
Dan Gohman82669522007-10-11 23:57:53 +00002742 // Check to see if we have a libcall for this operator.
2743 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2744 bool isSigned = false;
2745 switch (Node->getOpcode()) {
2746 case ISD::UDIV:
2747 case ISD::SDIV:
2748 if (VT == MVT::i32) {
2749 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002750 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002751 isSigned = Node->getOpcode() == ISD::SDIV;
2752 }
2753 break;
2754 case ISD::FPOW:
2755 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2756 VT == MVT::f64 ? RTLIB::POW_F64 :
2757 VT == MVT::f80 ? RTLIB::POW_F80 :
2758 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2759 RTLIB::UNKNOWN_LIBCALL;
2760 break;
2761 default: break;
2762 }
2763 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2764 SDOperand Dummy;
2765 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002766 break;
2767 }
2768
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002769 assert(MVT::isVector(Node->getValueType(0)) &&
2770 "Cannot expand this binary operator!");
2771 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002772 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002773 break;
2774 }
Evan Chengcc987612006-04-12 21:20:24 +00002775 case TargetLowering::Promote: {
2776 switch (Node->getOpcode()) {
2777 default: assert(0 && "Do not know how to promote this BinOp!");
2778 case ISD::AND:
2779 case ISD::OR:
2780 case ISD::XOR: {
2781 MVT::ValueType OVT = Node->getValueType(0);
2782 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2783 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2784 // Bit convert each of the values to the new type.
2785 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2786 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2787 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2788 // Bit convert the result back the original type.
2789 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2790 break;
2791 }
2792 }
2793 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002794 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002795 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002796
Dan Gohmane14ea862007-10-05 14:17:22 +00002797 case ISD::SMUL_LOHI:
2798 case ISD::UMUL_LOHI:
2799 case ISD::SDIVREM:
2800 case ISD::UDIVREM:
2801 // These nodes will only be produced by target-specific lowering, so
2802 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002803 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002804 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002805
2806 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2807 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002809 break;
2810
Chris Lattnera09f8482006-03-05 05:09:38 +00002811 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2812 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2813 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2814 case Expand: assert(0 && "Not possible");
2815 case Legal:
2816 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2817 break;
2818 case Promote:
2819 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2820 break;
2821 }
2822
2823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2824
2825 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2826 default: assert(0 && "Operation not supported");
2827 case TargetLowering::Custom:
2828 Tmp1 = TLI.LowerOperation(Result, DAG);
2829 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002830 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002831 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002832 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002833 // If this target supports fabs/fneg natively and select is cheap,
2834 // do this efficiently.
2835 if (!TLI.isSelectExpensive() &&
2836 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2837 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002838 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002839 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002840 // Get the sign bit of the RHS.
2841 MVT::ValueType IVT =
2842 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2843 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2844 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2845 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2846 // Get the absolute value of the result.
2847 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2848 // Select between the nabs and abs value based on the sign bit of
2849 // the input.
2850 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2851 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2852 AbsVal),
2853 AbsVal);
2854 Result = LegalizeOp(Result);
2855 break;
2856 }
2857
2858 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002859 MVT::ValueType NVT =
2860 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2861 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2862 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2863 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002864 break;
2865 }
Evan Cheng912095b2007-01-04 21:56:39 +00002866 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002867 break;
2868
Nate Begeman551bf3f2006-02-17 05:43:56 +00002869 case ISD::ADDC:
2870 case ISD::SUBC:
2871 Tmp1 = LegalizeOp(Node->getOperand(0));
2872 Tmp2 = LegalizeOp(Node->getOperand(1));
2873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2874 // Since this produces two values, make sure to remember that we legalized
2875 // both of them.
2876 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2877 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2878 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002879
Nate Begeman551bf3f2006-02-17 05:43:56 +00002880 case ISD::ADDE:
2881 case ISD::SUBE:
2882 Tmp1 = LegalizeOp(Node->getOperand(0));
2883 Tmp2 = LegalizeOp(Node->getOperand(1));
2884 Tmp3 = LegalizeOp(Node->getOperand(2));
2885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2886 // Since this produces two values, make sure to remember that we legalized
2887 // both of them.
2888 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2889 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2890 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002891
Nate Begeman419f8b62005-10-18 00:27:41 +00002892 case ISD::BUILD_PAIR: {
2893 MVT::ValueType PairTy = Node->getValueType(0);
2894 // TODO: handle the case where the Lo and Hi operands are not of legal type
2895 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2896 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2897 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002898 case TargetLowering::Promote:
2899 case TargetLowering::Custom:
2900 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002901 case TargetLowering::Legal:
2902 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2903 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2904 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002905 case TargetLowering::Expand:
2906 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2907 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2908 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2909 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2910 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002911 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002912 break;
2913 }
2914 break;
2915 }
2916
Nate Begemanc105e192005-04-06 00:23:54 +00002917 case ISD::UREM:
2918 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002919 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002920 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2921 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002922
Nate Begemanc105e192005-04-06 00:23:54 +00002923 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002924 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2925 case TargetLowering::Custom:
2926 isCustom = true;
2927 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002928 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002930 if (isCustom) {
2931 Tmp1 = TLI.LowerOperation(Result, DAG);
2932 if (Tmp1.Val) Result = Tmp1;
2933 }
Nate Begemanc105e192005-04-06 00:23:54 +00002934 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002935 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002936 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002937 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002938 MVT::ValueType VT = Node->getValueType(0);
2939
2940 // See if remainder can be lowered using two-result operations.
2941 SDVTList VTs = DAG.getVTList(VT, VT);
2942 if (Node->getOpcode() == ISD::SREM &&
2943 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2944 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2945 break;
2946 }
2947 if (Node->getOpcode() == ISD::UREM &&
2948 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2949 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2950 break;
2951 }
2952
2953 if (MVT::isInteger(VT)) {
2954 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002955 TargetLowering::Legal) {
2956 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002957 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002958 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2959 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00002960 } else if (MVT::isVector(VT)) {
2961 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002962 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002963 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002964 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002965 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2966 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002967 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002968 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002969 }
Dan Gohman0d974262007-11-06 22:11:54 +00002970 } else {
2971 assert(MVT::isFloatingPoint(VT) &&
2972 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00002973 if (MVT::isVector(VT)) {
2974 Result = LegalizeOp(UnrollVectorOp(Op));
2975 } else {
2976 // Floating point mod -> fmod libcall.
2977 RTLIB::Libcall LC = VT == MVT::f32
2978 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2979 SDOperand Dummy;
2980 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2981 false/*sign irrelevant*/, Dummy);
2982 }
Nate Begemanc105e192005-04-06 00:23:54 +00002983 }
2984 break;
2985 }
Dan Gohman525178c2007-10-08 18:33:35 +00002986 }
Nate Begemanc105e192005-04-06 00:23:54 +00002987 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002988 case ISD::VAARG: {
2989 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2990 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2991
Chris Lattner5c62f332006-01-28 07:42:08 +00002992 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002993 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2994 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002995 case TargetLowering::Custom:
2996 isCustom = true;
2997 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002998 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002999 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3000 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003001 Tmp1 = Result.getValue(1);
3002
3003 if (isCustom) {
3004 Tmp2 = TLI.LowerOperation(Result, DAG);
3005 if (Tmp2.Val) {
3006 Result = LegalizeOp(Tmp2);
3007 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3008 }
3009 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003010 break;
3011 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003012 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003013 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003014 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003015 // Increment the pointer, VAList, to the next vaarg
3016 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3017 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3018 TLI.getPointerTy()));
3019 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003020 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3021 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003022 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003023 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003024 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003025 Result = LegalizeOp(Result);
3026 break;
3027 }
3028 }
3029 // Since VAARG produces two values, make sure to remember that we
3030 // legalized both of them.
3031 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003032 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3033 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003034 }
3035
3036 case ISD::VACOPY:
3037 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3038 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3039 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3040
3041 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3042 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003043 case TargetLowering::Custom:
3044 isCustom = true;
3045 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003046 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3048 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003049 if (isCustom) {
3050 Tmp1 = TLI.LowerOperation(Result, DAG);
3051 if (Tmp1.Val) Result = Tmp1;
3052 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003053 break;
3054 case TargetLowering::Expand:
3055 // This defaults to loading a pointer from the input and storing it to the
3056 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003057 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003058 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003059 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3060 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003061 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3062 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003063 break;
3064 }
3065 break;
3066
3067 case ISD::VAEND:
3068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3069 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3070
3071 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3072 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003073 case TargetLowering::Custom:
3074 isCustom = true;
3075 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003076 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003078 if (isCustom) {
3079 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3080 if (Tmp1.Val) Result = Tmp1;
3081 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003082 break;
3083 case TargetLowering::Expand:
3084 Result = Tmp1; // Default to a no-op, return the chain
3085 break;
3086 }
3087 break;
3088
3089 case ISD::VASTART:
3090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3091 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3092
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003093 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3094
Nate Begemanacc398c2006-01-25 18:21:52 +00003095 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3096 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003097 case TargetLowering::Legal: break;
3098 case TargetLowering::Custom:
3099 Tmp1 = TLI.LowerOperation(Result, DAG);
3100 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003101 break;
3102 }
3103 break;
3104
Nate Begeman35ef9132006-01-11 21:21:00 +00003105 case ISD::ROTL:
3106 case ISD::ROTR:
3107 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3108 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003110 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3111 default:
3112 assert(0 && "ROTL/ROTR legalize operation not supported");
3113 break;
3114 case TargetLowering::Legal:
3115 break;
3116 case TargetLowering::Custom:
3117 Tmp1 = TLI.LowerOperation(Result, DAG);
3118 if (Tmp1.Val) Result = Tmp1;
3119 break;
3120 case TargetLowering::Promote:
3121 assert(0 && "Do not know how to promote ROTL/ROTR");
3122 break;
3123 case TargetLowering::Expand:
3124 assert(0 && "Do not know how to expand ROTL/ROTR");
3125 break;
3126 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003127 break;
3128
Nate Begemand88fc032006-01-14 03:14:10 +00003129 case ISD::BSWAP:
3130 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3131 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003132 case TargetLowering::Custom:
3133 assert(0 && "Cannot custom legalize this yet!");
3134 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003135 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003136 break;
3137 case TargetLowering::Promote: {
3138 MVT::ValueType OVT = Tmp1.getValueType();
3139 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003140 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003141
Chris Lattner456a93a2006-01-28 07:39:30 +00003142 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3143 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3144 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3145 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3146 break;
3147 }
3148 case TargetLowering::Expand:
3149 Result = ExpandBSWAP(Tmp1);
3150 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003151 }
3152 break;
3153
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003154 case ISD::CTPOP:
3155 case ISD::CTTZ:
3156 case ISD::CTLZ:
3157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3158 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003159 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003160 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003161 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003162 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003163 TargetLowering::Custom) {
3164 Tmp1 = TLI.LowerOperation(Result, DAG);
3165 if (Tmp1.Val) {
3166 Result = Tmp1;
3167 }
Scott Michel910b66d2007-07-30 21:00:31 +00003168 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003169 break;
3170 case TargetLowering::Promote: {
3171 MVT::ValueType OVT = Tmp1.getValueType();
3172 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003173
3174 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003175 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3176 // Perform the larger operation, then subtract if needed.
3177 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003178 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003179 case ISD::CTPOP:
3180 Result = Tmp1;
3181 break;
3182 case ISD::CTTZ:
3183 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003184 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003185 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003186 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003187 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003188 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003189 break;
3190 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003191 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003192 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003193 DAG.getConstant(MVT::getSizeInBits(NVT) -
3194 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003195 break;
3196 }
3197 break;
3198 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003199 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003200 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003201 break;
3202 }
3203 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003204
Chris Lattner2c8086f2005-04-02 05:00:07 +00003205 // Unary operators
3206 case ISD::FABS:
3207 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003208 case ISD::FSQRT:
3209 case ISD::FSIN:
3210 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003211 Tmp1 = LegalizeOp(Node->getOperand(0));
3212 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003213 case TargetLowering::Promote:
3214 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003215 isCustom = true;
3216 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003217 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003218 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003219 if (isCustom) {
3220 Tmp1 = TLI.LowerOperation(Result, DAG);
3221 if (Tmp1.Val) Result = Tmp1;
3222 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003223 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003224 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003225 switch (Node->getOpcode()) {
3226 default: assert(0 && "Unreachable!");
3227 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003228 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3229 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003230 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003231 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003232 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003233 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3234 MVT::ValueType VT = Node->getValueType(0);
3235 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003236 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003237 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3238 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003239 break;
3240 }
3241 case ISD::FSQRT:
3242 case ISD::FSIN:
3243 case ISD::FCOS: {
3244 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003245
3246 // Expand unsupported unary vector operators by unrolling them.
3247 if (MVT::isVector(VT)) {
3248 Result = LegalizeOp(UnrollVectorOp(Op));
3249 break;
3250 }
3251
Evan Cheng56966222007-01-12 02:11:51 +00003252 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003253 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003254 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00003255 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003256 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3257 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3258 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3259 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00003260 break;
3261 case ISD::FSIN:
3262 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3263 break;
3264 case ISD::FCOS:
3265 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3266 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003267 default: assert(0 && "Unreachable!");
3268 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003269 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003270 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3271 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003272 break;
3273 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003274 }
3275 break;
3276 }
3277 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003278 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003279 MVT::ValueType VT = Node->getValueType(0);
3280
3281 // Expand unsupported unary vector operators by unrolling them.
3282 if (MVT::isVector(VT)) {
3283 Result = LegalizeOp(UnrollVectorOp(Op));
3284 break;
3285 }
3286
3287 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen317096a2007-09-28 01:08:20 +00003288 RTLIB::Libcall LC =
Dan Gohman82669522007-10-11 23:57:53 +00003289 VT == MVT::f32 ? RTLIB::POWI_F32 :
3290 VT == MVT::f64 ? RTLIB::POWI_F64 :
3291 VT == MVT::f80 ? RTLIB::POWI_F80 :
3292 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003293 RTLIB::UNKNOWN_LIBCALL;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003294 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003295 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3296 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003297 break;
3298 }
Chris Lattner35481892005-12-23 00:16:34 +00003299 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003300 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003301 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003302 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3303 // The input has to be a vector type, we have to either scalarize it, pack
3304 // it, or convert it based on whether the input vector type is legal.
3305 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003306 int InIx = Node->getOperand(0).ResNo;
3307 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3308 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003309
3310 // Figure out if there is a simple type corresponding to this Vector
3311 // type. If so, convert to the vector type.
3312 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3313 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003314 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003315 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3316 LegalizeOp(Node->getOperand(0)));
3317 break;
3318 } else if (NumElems == 1) {
3319 // Turn this into a bit convert of the scalar input.
3320 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3321 ScalarizeVectorOp(Node->getOperand(0)));
3322 break;
3323 } else {
3324 // FIXME: UNIMP! Store then reload
3325 assert(0 && "Cast from unsupported vector type not implemented yet!");
3326 }
Chris Lattner67993f72006-01-23 07:30:46 +00003327 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003328 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3329 Node->getOperand(0).getValueType())) {
3330 default: assert(0 && "Unknown operation action!");
3331 case TargetLowering::Expand:
3332 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3333 break;
3334 case TargetLowering::Legal:
3335 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003336 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003337 break;
3338 }
3339 }
3340 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003341
Chris Lattner2c8086f2005-04-02 05:00:07 +00003342 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003343 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003344 case ISD::UINT_TO_FP: {
3345 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003346 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3347 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003348 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003349 Node->getOperand(0).getValueType())) {
3350 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003351 case TargetLowering::Custom:
3352 isCustom = true;
3353 // FALLTHROUGH
3354 case TargetLowering::Legal:
3355 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003356 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003357 if (isCustom) {
3358 Tmp1 = TLI.LowerOperation(Result, DAG);
3359 if (Tmp1.Val) Result = Tmp1;
3360 }
3361 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003362 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003363 Result = ExpandLegalINT_TO_FP(isSigned,
3364 LegalizeOp(Node->getOperand(0)),
3365 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003366 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003367 case TargetLowering::Promote:
3368 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3369 Node->getValueType(0),
3370 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003371 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003372 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003373 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003374 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003375 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3376 Node->getValueType(0), Node->getOperand(0));
3377 break;
3378 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003379 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003380 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003381 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3382 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003383 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003384 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3385 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003386 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3388 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003389 break;
3390 }
3391 break;
3392 }
3393 case ISD::TRUNCATE:
3394 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3395 case Legal:
3396 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003397 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003398 break;
3399 case Expand:
3400 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3401
3402 // Since the result is legal, we should just be able to truncate the low
3403 // part of the source.
3404 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3405 break;
3406 case Promote:
3407 Result = PromoteOp(Node->getOperand(0));
3408 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3409 break;
3410 }
3411 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003412
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003413 case ISD::FP_TO_SINT:
3414 case ISD::FP_TO_UINT:
3415 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3416 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003417 Tmp1 = LegalizeOp(Node->getOperand(0));
3418
Chris Lattner1618beb2005-07-29 00:11:56 +00003419 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3420 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003421 case TargetLowering::Custom:
3422 isCustom = true;
3423 // FALLTHROUGH
3424 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003425 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003426 if (isCustom) {
3427 Tmp1 = TLI.LowerOperation(Result, DAG);
3428 if (Tmp1.Val) Result = Tmp1;
3429 }
3430 break;
3431 case TargetLowering::Promote:
3432 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3433 Node->getOpcode() == ISD::FP_TO_SINT);
3434 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003435 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003436 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3437 SDOperand True, False;
3438 MVT::ValueType VT = Node->getOperand(0).getValueType();
3439 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003440 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003441 const uint64_t zero[] = {0, 0};
3442 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3443 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003444 (void)apf.convertFromZeroExtendedInteger
3445 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003446 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003447 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3448 Node->getOperand(0), Tmp2, ISD::SETLT);
3449 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3450 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003451 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003452 Tmp2));
3453 False = DAG.getNode(ISD::XOR, NVT, False,
3454 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003455 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3456 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003457 } else {
3458 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3459 }
3460 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003461 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003462 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003463 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003464 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003465 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003466 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003467 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003468 if (Node->getOpcode()==ISD::FP_TO_SINT)
3469 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003470 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3471 (DAG.getNode(ISD::FP_ROUND_INREG,
3472 MVT::ppcf128, Node->getOperand(0),
3473 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003474 else {
3475 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3476 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3477 Tmp2 = DAG.getConstantFP(apf, OVT);
3478 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3479 // FIXME: generated code sucks.
3480 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3481 DAG.getNode(ISD::ADD, MVT::i32,
3482 DAG.getNode(ISD::FP_TO_SINT, VT,
3483 DAG.getNode(ISD::FSUB, OVT,
3484 Node->getOperand(0), Tmp2)),
3485 DAG.getConstant(0x80000000, MVT::i32)),
3486 DAG.getNode(ISD::FP_TO_SINT, VT,
3487 Node->getOperand(0)),
3488 DAG.getCondCode(ISD::SETGE));
3489 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003490 break;
3491 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003492 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003493 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003494 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003495 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003496 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003497 LC = (VT == MVT::i32)
3498 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003499 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003500 LC = (VT == MVT::i32)
3501 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003502 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003503 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003504 LC = RTLIB::FPTOSINT_F80_I64;
3505 }
3506 else if (OVT == MVT::ppcf128) {
3507 assert(VT == MVT::i64);
3508 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003509 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003510 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003511 }
3512 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003513 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003514 LC = (VT == MVT::i32)
3515 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003516 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003517 LC = (VT == MVT::i32)
3518 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003519 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003520 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003521 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3522 }
3523 else if (OVT == MVT::ppcf128) {
3524 assert(VT == MVT::i64);
3525 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003526 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003527 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003528 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003529 default: assert(0 && "Unreachable!");
3530 }
3531 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003532 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3533 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003534 break;
3535 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003536 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003537 Tmp1 = PromoteOp(Node->getOperand(0));
3538 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3539 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003540 break;
3541 }
3542 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003543
Dale Johannesenab081c72007-08-09 17:27:48 +00003544 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003545 case ISD::FP_ROUND: {
3546 MVT::ValueType newVT = Op.getValueType();
3547 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3548 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003549 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3550 SDOperand Lo, Hi;
3551 ExpandOp(Node->getOperand(0), Lo, Hi);
3552 if (newVT == MVT::f64)
3553 Result = Hi;
3554 else
3555 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3556 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003557 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003558 // The only other way we can lower this is to turn it into a STORE,
3559 // LOAD pair, targetting a temporary location (a stack slot).
3560
3561 // NOTE: there is a choice here between constantly creating new stack
3562 // slots and always reusing the same one. We currently always create
3563 // new ones, as reuse may inhibit scheduling.
3564 MVT::ValueType slotVT =
3565 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3566 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003567 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003568 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3569 MachineFunction &MF = DAG.getMachineFunction();
3570 int SSFI =
3571 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3572 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3573 if (Node->getOpcode() == ISD::FP_EXTEND) {
3574 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3575 StackSlot, NULL, 0);
3576 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3577 Result, StackSlot, NULL, 0, oldVT);
3578 } else {
3579 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3580 StackSlot, NULL, 0, newVT);
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003581 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003582 }
3583 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003584 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003585 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003586 }
3587 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003588 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003589 case ISD::ZERO_EXTEND:
3590 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003591 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003592 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003593 case Legal:
3594 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003595 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003596 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003597 case Promote:
3598 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003599 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003600 Tmp1 = PromoteOp(Node->getOperand(0));
3601 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003602 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003603 case ISD::ZERO_EXTEND:
3604 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003605 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003606 Result = DAG.getZeroExtendInReg(Result,
3607 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003608 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003609 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003610 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003611 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003612 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003613 Result,
3614 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003615 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003616 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003617 Result = PromoteOp(Node->getOperand(0));
3618 if (Result.getValueType() != Op.getValueType())
3619 // Dynamically dead while we have only 2 FP types.
3620 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3621 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003622 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003623 Result = PromoteOp(Node->getOperand(0));
3624 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3625 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003626 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003627 }
3628 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003629 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003630 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003631 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003632 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003633
3634 // If this operation is not supported, convert it to a shl/shr or load/store
3635 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003636 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3637 default: assert(0 && "This action not supported for this op yet!");
3638 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003640 break;
3641 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003642 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003643 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003644 // NOTE: we could fall back on load/store here too for targets without
3645 // SAR. However, it is doubtful that any exist.
3646 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3647 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003648 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003649 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3650 Node->getOperand(0), ShiftCst);
3651 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3652 Result, ShiftCst);
3653 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003654 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003655 // EXTLOAD pair, targetting a temporary location (a stack slot).
3656
3657 // NOTE: there is a choice here between constantly creating new stack
3658 // slots and always reusing the same one. We currently always create
3659 // new ones, as reuse may inhibit scheduling.
3660 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003661 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003662 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003663 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003664 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003665 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003666 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003667 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3668 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003669 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003670 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003671 } else {
3672 assert(0 && "Unknown op");
3673 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003674 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003675 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003676 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003677 }
Duncan Sands36397f52007-07-27 12:58:54 +00003678 case ISD::TRAMPOLINE: {
3679 SDOperand Ops[6];
3680 for (unsigned i = 0; i != 6; ++i)
3681 Ops[i] = LegalizeOp(Node->getOperand(i));
3682 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3683 // The only option for this node is to custom lower it.
3684 Result = TLI.LowerOperation(Result, DAG);
3685 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003686
3687 // Since trampoline produces two values, make sure to remember that we
3688 // legalized both of them.
3689 Tmp1 = LegalizeOp(Result.getValue(1));
3690 Result = LegalizeOp(Result);
3691 AddLegalizedOperand(SDOperand(Node, 0), Result);
3692 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3693 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003694 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003695 case ISD::FLT_ROUNDS: {
3696 MVT::ValueType VT = Node->getValueType(0);
3697 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3698 default: assert(0 && "This action not supported for this op yet!");
3699 case TargetLowering::Custom:
3700 Result = TLI.LowerOperation(Op, DAG);
3701 if (Result.Val) break;
3702 // Fall Thru
3703 case TargetLowering::Legal:
3704 // If this operation is not supported, lower it to constant 1
3705 Result = DAG.getConstant(1, VT);
3706 break;
3707 }
3708 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003709 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003710
Chris Lattner4ddd2832006-04-08 04:13:17 +00003711 assert(Result.getValueType() == Op.getValueType() &&
3712 "Bad legalization!");
3713
Chris Lattner456a93a2006-01-28 07:39:30 +00003714 // Make sure that the generated code is itself legal.
3715 if (Result != Op)
3716 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003717
Chris Lattner45982da2005-05-12 16:53:42 +00003718 // Note that LegalizeOp may be reentered even from single-use nodes, which
3719 // means that we always must cache transformed nodes.
3720 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003721 return Result;
3722}
3723
Chris Lattner8b6fa222005-01-15 22:16:26 +00003724/// PromoteOp - Given an operation that produces a value in an invalid type,
3725/// promote it to compute the value into a larger type. The produced value will
3726/// have the correct bits for the low portion of the register, but no guarantee
3727/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003728SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3729 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003730 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003731 assert(getTypeAction(VT) == Promote &&
3732 "Caller should expand or legalize operands that are not promotable!");
3733 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3734 "Cannot promote to smaller type!");
3735
Chris Lattner03c85462005-01-15 05:21:40 +00003736 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003737 SDOperand Result;
3738 SDNode *Node = Op.Val;
3739
Chris Lattner40030bf2007-02-04 01:17:38 +00003740 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003741 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003742
Chris Lattner03c85462005-01-15 05:21:40 +00003743 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003744 case ISD::CopyFromReg:
3745 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003746 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003747#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003748 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003749#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003750 assert(0 && "Do not know how to promote this operator!");
3751 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003752 case ISD::UNDEF:
3753 Result = DAG.getNode(ISD::UNDEF, NVT);
3754 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003755 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003756 if (VT != MVT::i1)
3757 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3758 else
3759 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003760 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3761 break;
3762 case ISD::ConstantFP:
3763 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3764 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3765 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003766
Chris Lattner82fbfb62005-01-18 02:59:52 +00003767 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003768 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003769 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3770 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003771 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003772
Chris Lattner03c85462005-01-15 05:21:40 +00003773 case ISD::TRUNCATE:
3774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3775 case Legal:
3776 Result = LegalizeOp(Node->getOperand(0));
3777 assert(Result.getValueType() >= NVT &&
3778 "This truncation doesn't make sense!");
3779 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3780 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3781 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003782 case Promote:
3783 // The truncation is not required, because we don't guarantee anything
3784 // about high bits anyway.
3785 Result = PromoteOp(Node->getOperand(0));
3786 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003787 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003788 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3789 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003790 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003791 }
3792 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003793 case ISD::SIGN_EXTEND:
3794 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003795 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3797 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3798 case Legal:
3799 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003800 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003801 break;
3802 case Promote:
3803 // Promote the reg if it's smaller.
3804 Result = PromoteOp(Node->getOperand(0));
3805 // The high bits are not guaranteed to be anything. Insert an extend.
3806 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003807 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003808 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003809 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003810 Result = DAG.getZeroExtendInReg(Result,
3811 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003812 break;
3813 }
3814 break;
Chris Lattner35481892005-12-23 00:16:34 +00003815 case ISD::BIT_CONVERT:
3816 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3817 Result = PromoteOp(Result);
3818 break;
3819
Chris Lattner8b6fa222005-01-15 22:16:26 +00003820 case ISD::FP_EXTEND:
3821 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3822 case ISD::FP_ROUND:
3823 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3824 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3825 case Promote: assert(0 && "Unreachable with 2 FP types!");
3826 case Legal:
3827 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003828 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003829 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003830 break;
3831 }
3832 break;
3833
3834 case ISD::SINT_TO_FP:
3835 case ISD::UINT_TO_FP:
3836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3837 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003838 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003839 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003840 break;
3841
3842 case Promote:
3843 Result = PromoteOp(Node->getOperand(0));
3844 if (Node->getOpcode() == ISD::SINT_TO_FP)
3845 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003846 Result,
3847 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003848 else
Chris Lattner23993562005-04-13 02:38:47 +00003849 Result = DAG.getZeroExtendInReg(Result,
3850 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003851 // No extra round required here.
3852 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003853 break;
3854 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003855 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3856 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003857 // Round if we cannot tolerate excess precision.
3858 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003859 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3860 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003861 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003862 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003863 break;
3864
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003865 case ISD::SIGN_EXTEND_INREG:
3866 Result = PromoteOp(Node->getOperand(0));
3867 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3868 Node->getOperand(1));
3869 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003870 case ISD::FP_TO_SINT:
3871 case ISD::FP_TO_UINT:
3872 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3873 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003874 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003875 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003876 break;
3877 case Promote:
3878 // The input result is prerounded, so we don't have to do anything
3879 // special.
3880 Tmp1 = PromoteOp(Node->getOperand(0));
3881 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003882 }
Nate Begemand2558e32005-08-14 01:20:53 +00003883 // If we're promoting a UINT to a larger size, check to see if the new node
3884 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3885 // we can use that instead. This allows us to generate better code for
3886 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3887 // legal, such as PowerPC.
3888 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003889 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003890 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3891 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003892 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3893 } else {
3894 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3895 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003896 break;
3897
Chris Lattner2c8086f2005-04-02 05:00:07 +00003898 case ISD::FABS:
3899 case ISD::FNEG:
3900 Tmp1 = PromoteOp(Node->getOperand(0));
3901 assert(Tmp1.getValueType() == NVT);
3902 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3903 // NOTE: we do not have to do any extra rounding here for
3904 // NoExcessFPPrecision, because we know the input will have the appropriate
3905 // precision, and these operations don't modify precision at all.
3906 break;
3907
Chris Lattnerda6ba872005-04-28 21:44:33 +00003908 case ISD::FSQRT:
3909 case ISD::FSIN:
3910 case ISD::FCOS:
3911 Tmp1 = PromoteOp(Node->getOperand(0));
3912 assert(Tmp1.getValueType() == NVT);
3913 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003914 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003915 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3916 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003917 break;
3918
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003919 case ISD::FPOWI: {
3920 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3921 // directly as well, which may be better.
3922 Tmp1 = PromoteOp(Node->getOperand(0));
3923 assert(Tmp1.getValueType() == NVT);
3924 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3925 if (NoExcessFPPrecision)
3926 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3927 DAG.getValueType(VT));
3928 break;
3929 }
3930
Chris Lattner03c85462005-01-15 05:21:40 +00003931 case ISD::AND:
3932 case ISD::OR:
3933 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003934 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003935 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003936 case ISD::MUL:
3937 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003938 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003939 // that too is okay if they are integer operations.
3940 Tmp1 = PromoteOp(Node->getOperand(0));
3941 Tmp2 = PromoteOp(Node->getOperand(1));
3942 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3943 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003944 break;
3945 case ISD::FADD:
3946 case ISD::FSUB:
3947 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003948 Tmp1 = PromoteOp(Node->getOperand(0));
3949 Tmp2 = PromoteOp(Node->getOperand(1));
3950 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3951 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3952
3953 // Floating point operations will give excess precision that we may not be
3954 // able to tolerate. If we DO allow excess precision, just leave it,
3955 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003956 // FIXME: Why would we need to round FP ops more than integer ones?
3957 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003958 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003959 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3960 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003961 break;
3962
Chris Lattner8b6fa222005-01-15 22:16:26 +00003963 case ISD::SDIV:
3964 case ISD::SREM:
3965 // These operators require that their input be sign extended.
3966 Tmp1 = PromoteOp(Node->getOperand(0));
3967 Tmp2 = PromoteOp(Node->getOperand(1));
3968 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003969 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3970 DAG.getValueType(VT));
3971 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3972 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003973 }
3974 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3975
3976 // Perform FP_ROUND: this is probably overly pessimistic.
3977 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003978 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3979 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003980 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003981 case ISD::FDIV:
3982 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003983 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003984 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003985 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3986 case Legal:
3987 Tmp1 = LegalizeOp(Node->getOperand(0));
3988 break;
3989 case Promote:
3990 Tmp1 = PromoteOp(Node->getOperand(0));
3991 break;
3992 case Expand:
3993 assert(0 && "not implemented");
3994 }
3995 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3996 case Legal:
3997 Tmp2 = LegalizeOp(Node->getOperand(1));
3998 break;
3999 case Promote:
4000 Tmp2 = PromoteOp(Node->getOperand(1));
4001 break;
4002 case Expand:
4003 assert(0 && "not implemented");
4004 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004005 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4006
4007 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004008 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004009 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4010 DAG.getValueType(VT));
4011 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004012
4013 case ISD::UDIV:
4014 case ISD::UREM:
4015 // These operators require that their input be zero extended.
4016 Tmp1 = PromoteOp(Node->getOperand(0));
4017 Tmp2 = PromoteOp(Node->getOperand(1));
4018 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004019 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4020 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004021 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4022 break;
4023
4024 case ISD::SHL:
4025 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004026 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004027 break;
4028 case ISD::SRA:
4029 // The input value must be properly sign extended.
4030 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004031 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4032 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004033 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004034 break;
4035 case ISD::SRL:
4036 // The input value must be properly zero extended.
4037 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004038 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004039 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004040 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004041
4042 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004043 Tmp1 = Node->getOperand(0); // Get the chain.
4044 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004045 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4046 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4047 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4048 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004049 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004050 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004051 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004052 // Increment the pointer, VAList, to the next vaarg
4053 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4054 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4055 TLI.getPointerTy()));
4056 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004057 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4058 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004059 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004060 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004061 }
4062 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004063 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004064 break;
4065
Evan Cheng466685d2006-10-09 20:57:25 +00004066 case ISD::LOAD: {
4067 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004068 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4069 ? ISD::EXTLOAD : LD->getExtensionType();
4070 Result = DAG.getExtLoad(ExtType, NVT,
4071 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004072 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004073 LD->getLoadedVT(),
4074 LD->isVolatile(),
4075 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004076 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004077 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004078 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004079 }
Chris Lattner03c85462005-01-15 05:21:40 +00004080 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004081 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4082 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004083 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004084 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004085 case ISD::SELECT_CC:
4086 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4087 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4088 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004089 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004090 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004091 case ISD::BSWAP:
4092 Tmp1 = Node->getOperand(0);
4093 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4094 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4095 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004096 DAG.getConstant(MVT::getSizeInBits(NVT) -
4097 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004098 TLI.getShiftAmountTy()));
4099 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004100 case ISD::CTPOP:
4101 case ISD::CTTZ:
4102 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004103 // Zero extend the argument
4104 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004105 // Perform the larger operation, then subtract if needed.
4106 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004107 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004108 case ISD::CTPOP:
4109 Result = Tmp1;
4110 break;
4111 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004112 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004113 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004114 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4115 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004116 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004117 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004118 break;
4119 case ISD::CTLZ:
4120 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004121 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004122 DAG.getConstant(MVT::getSizeInBits(NVT) -
4123 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004124 break;
4125 }
4126 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004127 case ISD::EXTRACT_SUBVECTOR:
4128 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004129 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004130 case ISD::EXTRACT_VECTOR_ELT:
4131 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4132 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004133 }
4134
4135 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004136
4137 // Make sure the result is itself legal.
4138 Result = LegalizeOp(Result);
4139
4140 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004141 AddPromotedOperand(Op, Result);
4142 return Result;
4143}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004144
Dan Gohman7f321562007-06-25 16:23:39 +00004145/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4146/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4147/// based on the vector type. The return type of this matches the element type
4148/// of the vector, which may not be legal for the target.
4149SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004150 // We know that operand #0 is the Vec vector. If the index is a constant
4151 // or if the invec is a supported hardware type, we can use it. Otherwise,
4152 // lower to a store then an indexed load.
4153 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004154 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004155
Dan Gohmane40c7b02007-09-24 15:54:53 +00004156 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004157 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004158
Dan Gohman7f321562007-06-25 16:23:39 +00004159 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4160 default: assert(0 && "This action is not supported yet!");
4161 case TargetLowering::Custom: {
4162 Vec = LegalizeOp(Vec);
4163 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4164 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4165 if (Tmp3.Val)
4166 return Tmp3;
4167 break;
4168 }
4169 case TargetLowering::Legal:
4170 if (isTypeLegal(TVT)) {
4171 Vec = LegalizeOp(Vec);
4172 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004173 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004174 }
4175 break;
4176 case TargetLowering::Expand:
4177 break;
4178 }
4179
4180 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004181 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004182 Op = ScalarizeVectorOp(Vec);
4183 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4184 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004185 SDOperand Lo, Hi;
4186 SplitVectorOp(Vec, Lo, Hi);
4187 if (CIdx->getValue() < NumElems/2) {
4188 Vec = Lo;
4189 } else {
4190 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004191 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4192 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004193 }
Dan Gohman7f321562007-06-25 16:23:39 +00004194
Chris Lattner15972212006-03-31 17:55:51 +00004195 // It's now an extract from the appropriate high or low part. Recurse.
4196 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004197 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004198 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004199 // Store the value to a temporary stack slot, then LOAD the scalar
4200 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004201 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004202 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4203
4204 // Add the offset to the index.
4205 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4206 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4207 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004208
4209 if (MVT::getSizeInBits(Idx.getValueType()) >
4210 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004211 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004212 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004213 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004214
Dan Gohman7f321562007-06-25 16:23:39 +00004215 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4216
4217 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004218 }
Dan Gohman7f321562007-06-25 16:23:39 +00004219 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004220}
4221
Dan Gohman7f321562007-06-25 16:23:39 +00004222/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004223/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004224SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004225 // We know that operand #0 is the Vec vector. For now we assume the index
4226 // is a constant and that the extracted result is a supported hardware type.
4227 SDOperand Vec = Op.getOperand(0);
4228 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4229
Dan Gohman7f321562007-06-25 16:23:39 +00004230 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004231
4232 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4233 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004234 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004235 }
4236
4237 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4238 SDOperand Lo, Hi;
4239 SplitVectorOp(Vec, Lo, Hi);
4240 if (CIdx->getValue() < NumElems/2) {
4241 Vec = Lo;
4242 } else {
4243 Vec = Hi;
4244 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4245 }
4246
4247 // It's now an extract from the appropriate high or low part. Recurse.
4248 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004249 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004250}
4251
Nate Begeman750ac1b2006-02-01 07:19:44 +00004252/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4253/// with condition CC on the current target. This usually involves legalizing
4254/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4255/// there may be no choice but to create a new SetCC node to represent the
4256/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4257/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4258void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4259 SDOperand &RHS,
4260 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004261 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004262
4263 switch (getTypeAction(LHS.getValueType())) {
4264 case Legal:
4265 Tmp1 = LegalizeOp(LHS); // LHS
4266 Tmp2 = LegalizeOp(RHS); // RHS
4267 break;
4268 case Promote:
4269 Tmp1 = PromoteOp(LHS); // LHS
4270 Tmp2 = PromoteOp(RHS); // RHS
4271
4272 // If this is an FP compare, the operands have already been extended.
4273 if (MVT::isInteger(LHS.getValueType())) {
4274 MVT::ValueType VT = LHS.getValueType();
4275 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4276
4277 // Otherwise, we have to insert explicit sign or zero extends. Note
4278 // that we could insert sign extends for ALL conditions, but zero extend
4279 // is cheaper on many machines (an AND instead of two shifts), so prefer
4280 // it.
4281 switch (cast<CondCodeSDNode>(CC)->get()) {
4282 default: assert(0 && "Unknown integer comparison!");
4283 case ISD::SETEQ:
4284 case ISD::SETNE:
4285 case ISD::SETUGE:
4286 case ISD::SETUGT:
4287 case ISD::SETULE:
4288 case ISD::SETULT:
4289 // ALL of these operations will work if we either sign or zero extend
4290 // the operands (including the unsigned comparisons!). Zero extend is
4291 // usually a simpler/cheaper operation, so prefer it.
4292 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4293 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4294 break;
4295 case ISD::SETGE:
4296 case ISD::SETGT:
4297 case ISD::SETLT:
4298 case ISD::SETLE:
4299 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4300 DAG.getValueType(VT));
4301 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4302 DAG.getValueType(VT));
4303 break;
4304 }
4305 }
4306 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004307 case Expand: {
4308 MVT::ValueType VT = LHS.getValueType();
4309 if (VT == MVT::f32 || VT == MVT::f64) {
4310 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004311 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004312 switch (cast<CondCodeSDNode>(CC)->get()) {
4313 case ISD::SETEQ:
4314 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004315 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004316 break;
4317 case ISD::SETNE:
4318 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004319 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004320 break;
4321 case ISD::SETGE:
4322 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004323 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004324 break;
4325 case ISD::SETLT:
4326 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004327 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004328 break;
4329 case ISD::SETLE:
4330 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004331 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004332 break;
4333 case ISD::SETGT:
4334 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004335 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004336 break;
4337 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004338 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4339 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004340 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004341 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004342 break;
4343 default:
Evan Cheng56966222007-01-12 02:11:51 +00004344 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004345 switch (cast<CondCodeSDNode>(CC)->get()) {
4346 case ISD::SETONE:
4347 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004348 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004349 // Fallthrough
4350 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004351 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004352 break;
4353 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004354 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004355 break;
4356 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004357 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004358 break;
4359 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004360 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004361 break;
Evan Cheng56966222007-01-12 02:11:51 +00004362 case ISD::SETUEQ:
4363 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004364 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004365 default: assert(0 && "Unsupported FP setcc!");
4366 }
4367 }
4368
4369 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004370 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004371 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004372 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004373 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004374 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004375 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004376 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004377 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004378 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004379 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004380 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004381 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004382 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4383 Tmp2 = SDOperand();
4384 }
4385 LHS = Tmp1;
4386 RHS = Tmp2;
4387 return;
4388 }
4389
Nate Begeman750ac1b2006-02-01 07:19:44 +00004390 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4391 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004392 ExpandOp(RHS, RHSLo, RHSHi);
4393 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4394
4395 if (VT==MVT::ppcf128) {
4396 // FIXME: This generated code sucks. We want to generate
4397 // FCMP crN, hi1, hi2
4398 // BNE crN, L:
4399 // FCMP crN, lo1, lo2
4400 // The following can be improved, but not that much.
4401 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4402 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4403 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4404 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4405 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4406 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4407 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4408 Tmp2 = SDOperand();
4409 break;
4410 }
4411
4412 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004413 case ISD::SETEQ:
4414 case ISD::SETNE:
4415 if (RHSLo == RHSHi)
4416 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4417 if (RHSCST->isAllOnesValue()) {
4418 // Comparison to -1.
4419 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4420 Tmp2 = RHSLo;
4421 break;
4422 }
4423
4424 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4425 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4426 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4427 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4428 break;
4429 default:
4430 // If this is a comparison of the sign bit, just look at the top part.
4431 // X > -1, x < 0
4432 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4433 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4434 CST->getValue() == 0) || // X < 0
4435 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4436 CST->isAllOnesValue())) { // X > -1
4437 Tmp1 = LHSHi;
4438 Tmp2 = RHSHi;
4439 break;
4440 }
4441
4442 // FIXME: This generated code sucks.
4443 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004444 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004445 default: assert(0 && "Unknown integer setcc!");
4446 case ISD::SETLT:
4447 case ISD::SETULT: LowCC = ISD::SETULT; break;
4448 case ISD::SETGT:
4449 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4450 case ISD::SETLE:
4451 case ISD::SETULE: LowCC = ISD::SETULE; break;
4452 case ISD::SETGE:
4453 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4454 }
4455
4456 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4457 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4458 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4459
4460 // NOTE: on targets without efficient SELECT of bools, we can always use
4461 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004462 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4463 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4464 false, DagCombineInfo);
4465 if (!Tmp1.Val)
4466 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4467 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4468 CCCode, false, DagCombineInfo);
4469 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004470 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004471
4472 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4473 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4474 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4475 (Tmp2C && Tmp2C->getValue() == 0 &&
4476 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4477 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4478 (Tmp2C && Tmp2C->getValue() == 1 &&
4479 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4480 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4481 // low part is known false, returns high part.
4482 // For LE / GE, if high part is known false, ignore the low part.
4483 // For LT / GT, if high part is known true, ignore the low part.
4484 Tmp1 = Tmp2;
4485 Tmp2 = SDOperand();
4486 } else {
4487 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4488 ISD::SETEQ, false, DagCombineInfo);
4489 if (!Result.Val)
4490 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4491 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4492 Result, Tmp1, Tmp2));
4493 Tmp1 = Result;
4494 Tmp2 = SDOperand();
4495 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004496 }
4497 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004498 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004499 LHS = Tmp1;
4500 RHS = Tmp2;
4501}
4502
Chris Lattner35481892005-12-23 00:16:34 +00004503/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004504/// The resultant code need not be legal. Note that SrcOp is the input operand
4505/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004506SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4507 SDOperand SrcOp) {
4508 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004509 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004510
4511 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004512 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004513 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004514 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004515}
4516
Chris Lattner4352cc92006-04-04 17:23:26 +00004517SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4518 // Create a vector sized/aligned stack slot, store the value to element #0,
4519 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004520 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004521 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004522 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004523 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004524}
4525
4526
Chris Lattnerce872152006-03-19 06:31:19 +00004527/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004528/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004529SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4530
4531 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004532 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004533 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004534 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004535 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004536 std::map<SDOperand, std::vector<unsigned> > Values;
4537 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004538 bool isConstant = true;
4539 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4540 SplatValue.getOpcode() != ISD::UNDEF)
4541 isConstant = false;
4542
Evan Cheng033e6812006-03-24 01:17:21 +00004543 for (unsigned i = 1; i < NumElems; ++i) {
4544 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004545 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004546 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004547 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004548 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004549 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004550
4551 // If this isn't a constant element or an undef, we can't use a constant
4552 // pool load.
4553 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4554 V.getOpcode() != ISD::UNDEF)
4555 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004556 }
4557
4558 if (isOnlyLowElement) {
4559 // If the low element is an undef too, then this whole things is an undef.
4560 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4561 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4562 // Otherwise, turn this into a scalar_to_vector node.
4563 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4564 Node->getOperand(0));
4565 }
4566
Chris Lattner2eb86532006-03-24 07:29:17 +00004567 // If all elements are constants, create a load from the constant pool.
4568 if (isConstant) {
4569 MVT::ValueType VT = Node->getValueType(0);
4570 const Type *OpNTy =
4571 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4572 std::vector<Constant*> CV;
4573 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4574 if (ConstantFPSDNode *V =
4575 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004576 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004577 } else if (ConstantSDNode *V =
4578 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004579 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004580 } else {
4581 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4582 CV.push_back(UndefValue::get(OpNTy));
4583 }
4584 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004585 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004586 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004587 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004588 }
4589
Chris Lattner87100e02006-03-20 01:52:29 +00004590 if (SplatValue.Val) { // Splat of one value?
4591 // Build the shuffle constant vector: <0, 0, 0, 0>
4592 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004593 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004594 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004595 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004596 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4597 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004598
4599 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004600 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004601 // Get the splatted value into the low element of a vector register.
4602 SDOperand LowValVec =
4603 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4604
4605 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4606 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4607 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4608 SplatMask);
4609 }
4610 }
4611
Evan Cheng033e6812006-03-24 01:17:21 +00004612 // If there are only two unique elements, we may be able to turn this into a
4613 // vector shuffle.
4614 if (Values.size() == 2) {
4615 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4616 MVT::ValueType MaskVT =
4617 MVT::getIntVectorWithNumElements(NumElems);
4618 std::vector<SDOperand> MaskVec(NumElems);
4619 unsigned i = 0;
4620 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4621 E = Values.end(); I != E; ++I) {
4622 for (std::vector<unsigned>::iterator II = I->second.begin(),
4623 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004624 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004625 i += NumElems;
4626 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004627 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4628 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004629
4630 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004631 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4632 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004633 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004634 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4635 E = Values.end(); I != E; ++I) {
4636 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4637 I->first);
4638 Ops.push_back(Op);
4639 }
4640 Ops.push_back(ShuffleMask);
4641
4642 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004643 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4644 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004645 }
4646 }
Chris Lattnerce872152006-03-19 06:31:19 +00004647
4648 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4649 // aligned object on the stack, store each element into it, then load
4650 // the result as a vector.
4651 MVT::ValueType VT = Node->getValueType(0);
4652 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004653 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004654
4655 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004656 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004657 unsigned TypeByteSize =
4658 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004659 // Store (in the right endianness) the elements to memory.
4660 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4661 // Ignore undef elements.
4662 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4663
Chris Lattner841c8822006-03-22 01:46:54 +00004664 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004665
4666 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4667 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4668
Evan Cheng786225a2006-10-05 23:01:46 +00004669 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004670 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004671 }
4672
4673 SDOperand StoreChain;
4674 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004675 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4676 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004677 else
4678 StoreChain = DAG.getEntryNode();
4679
4680 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004681 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004682}
4683
Chris Lattner5b359c62005-04-02 04:00:59 +00004684void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4685 SDOperand Op, SDOperand Amt,
4686 SDOperand &Lo, SDOperand &Hi) {
4687 // Expand the subcomponents.
4688 SDOperand LHSL, LHSH;
4689 ExpandOp(Op, LHSL, LHSH);
4690
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004691 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004692 MVT::ValueType VT = LHSL.getValueType();
4693 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004694 Hi = Lo.getValue(1);
4695}
4696
4697
Chris Lattnere34b3962005-01-19 04:19:40 +00004698/// ExpandShift - Try to find a clever way to expand this shift operation out to
4699/// smaller elements. If we can't find a way that is more efficient than a
4700/// libcall on this target, return false. Otherwise, return true with the
4701/// low-parts expanded into Lo and Hi.
4702bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4703 SDOperand &Lo, SDOperand &Hi) {
4704 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4705 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004706
Chris Lattnere34b3962005-01-19 04:19:40 +00004707 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004708 SDOperand ShAmt = LegalizeOp(Amt);
4709 MVT::ValueType ShTy = ShAmt.getValueType();
4710 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4711 unsigned NVTBits = MVT::getSizeInBits(NVT);
4712
Chris Lattner7a3c8552007-10-14 20:35:12 +00004713 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004714 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4715 unsigned Cst = CN->getValue();
4716 // Expand the incoming operand to be shifted, so that we have its parts
4717 SDOperand InL, InH;
4718 ExpandOp(Op, InL, InH);
4719 switch(Opc) {
4720 case ISD::SHL:
4721 if (Cst > VTBits) {
4722 Lo = DAG.getConstant(0, NVT);
4723 Hi = DAG.getConstant(0, NVT);
4724 } else if (Cst > NVTBits) {
4725 Lo = DAG.getConstant(0, NVT);
4726 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004727 } else if (Cst == NVTBits) {
4728 Lo = DAG.getConstant(0, NVT);
4729 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004730 } else {
4731 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4732 Hi = DAG.getNode(ISD::OR, NVT,
4733 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4734 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4735 }
4736 return true;
4737 case ISD::SRL:
4738 if (Cst > VTBits) {
4739 Lo = DAG.getConstant(0, NVT);
4740 Hi = DAG.getConstant(0, NVT);
4741 } else if (Cst > NVTBits) {
4742 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4743 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004744 } else if (Cst == NVTBits) {
4745 Lo = InH;
4746 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004747 } else {
4748 Lo = DAG.getNode(ISD::OR, NVT,
4749 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4750 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4751 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4752 }
4753 return true;
4754 case ISD::SRA:
4755 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004756 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004757 DAG.getConstant(NVTBits-1, ShTy));
4758 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004759 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004760 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004761 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004762 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004763 } else if (Cst == NVTBits) {
4764 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004765 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004766 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004767 } else {
4768 Lo = DAG.getNode(ISD::OR, NVT,
4769 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4770 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4771 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4772 }
4773 return true;
4774 }
4775 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004776
4777 // Okay, the shift amount isn't constant. However, if we can tell that it is
4778 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4779 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004780 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004781
4782 // If we know that the high bit of the shift amount is one, then we can do
4783 // this as a couple of simple shifts.
4784 if (KnownOne & Mask) {
4785 // Mask out the high bit, which we know is set.
4786 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4787 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4788
4789 // Expand the incoming operand to be shifted, so that we have its parts
4790 SDOperand InL, InH;
4791 ExpandOp(Op, InL, InH);
4792 switch(Opc) {
4793 case ISD::SHL:
4794 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4795 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4796 return true;
4797 case ISD::SRL:
4798 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4799 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4800 return true;
4801 case ISD::SRA:
4802 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4803 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4804 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4805 return true;
4806 }
4807 }
4808
4809 // If we know that the high bit of the shift amount is zero, then we can do
4810 // this as a couple of simple shifts.
4811 if (KnownZero & Mask) {
4812 // Compute 32-amt.
4813 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4814 DAG.getConstant(NVTBits, Amt.getValueType()),
4815 Amt);
4816
4817 // Expand the incoming operand to be shifted, so that we have its parts
4818 SDOperand InL, InH;
4819 ExpandOp(Op, InL, InH);
4820 switch(Opc) {
4821 case ISD::SHL:
4822 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4823 Hi = DAG.getNode(ISD::OR, NVT,
4824 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4825 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4826 return true;
4827 case ISD::SRL:
4828 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4829 Lo = DAG.getNode(ISD::OR, NVT,
4830 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4831 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4832 return true;
4833 case ISD::SRA:
4834 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4835 Lo = DAG.getNode(ISD::OR, NVT,
4836 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4837 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4838 return true;
4839 }
4840 }
4841
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004842 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004843}
Chris Lattner77e77a62005-01-21 06:05:23 +00004844
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004845
Chris Lattner77e77a62005-01-21 06:05:23 +00004846// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4847// does not fit into a register, return the lo part and set the hi part to the
4848// by-reg argument. If it does fit into a single register, return the result
4849// and leave the Hi part unset.
4850SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004851 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004852 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4853 // The input chain to this libcall is the entry node of the function.
4854 // Legalizing the call will automatically add the previous call to the
4855 // dependence.
4856 SDOperand InChain = DAG.getEntryNode();
4857
Chris Lattner77e77a62005-01-21 06:05:23 +00004858 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004859 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004860 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4861 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4862 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004863 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004864 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004865 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004866 }
4867 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004868
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004869 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004870 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004871 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004872 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004873 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004874
Chris Lattner6831a812006-02-13 09:18:02 +00004875 // Legalize the call sequence, starting with the chain. This will advance
4876 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4877 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4878 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004879 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004880 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004881 default: assert(0 && "Unknown thing");
4882 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004883 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004884 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004885 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004886 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004887 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004888 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004889 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004890}
4891
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004892
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004893/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4894///
Chris Lattner77e77a62005-01-21 06:05:23 +00004895SDOperand SelectionDAGLegalize::
4896ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004897 assert(getTypeAction(Source.getValueType()) == Expand &&
4898 "This is not an expansion!");
4899 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4900
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004901 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004902 assert(Source.getValueType() == MVT::i64 &&
4903 "This only works for 64-bit -> FP");
4904 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4905 // incoming integer is set. To handle this, we dynamically test to see if
4906 // it is set, and, if so, add a fudge factor.
4907 SDOperand Lo, Hi;
4908 ExpandOp(Source, Lo, Hi);
4909
Chris Lattner66de05b2005-05-13 04:45:13 +00004910 // If this is unsigned, and not supported, first perform the conversion to
4911 // signed, then adjust the result if the sign bit is set.
4912 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4913 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4914
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004915 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4916 DAG.getConstant(0, Hi.getValueType()),
4917 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004918 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4919 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4920 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004921 uint64_t FF = 0x5f800000ULL;
4922 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004923 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004924
Chris Lattner5839bf22005-08-26 17:15:30 +00004925 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004926 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4927 SDOperand FudgeInReg;
4928 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004929 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004930 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004931 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004932 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004933 CPIdx, NULL, 0, MVT::f32);
4934 else
4935 assert(0 && "Unexpected conversion");
4936
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004937 MVT::ValueType SCVT = SignedConv.getValueType();
4938 if (SCVT != DestTy) {
4939 // Destination type needs to be expanded as well. The FADD now we are
4940 // constructing will be expanded into a libcall.
4941 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4942 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4943 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4944 SignedConv, SignedConv.getValue(1));
4945 }
4946 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4947 }
Chris Lattner473a9902005-09-29 06:44:39 +00004948 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004949 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004950
Chris Lattnera88a2602005-05-14 05:33:54 +00004951 // Check to see if the target has a custom way to lower this. If so, use it.
4952 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4953 default: assert(0 && "This action not implemented for this operation!");
4954 case TargetLowering::Legal:
4955 case TargetLowering::Expand:
4956 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004957 case TargetLowering::Custom: {
4958 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4959 Source), DAG);
4960 if (NV.Val)
4961 return LegalizeOp(NV);
4962 break; // The target decided this was legal after all
4963 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004964 }
4965
Chris Lattner13689e22005-05-12 07:00:44 +00004966 // Expand the source, then glue it back together for the call. We must expand
4967 // the source in case it is shared (this pass of legalize must traverse it).
4968 SDOperand SrcLo, SrcHi;
4969 ExpandOp(Source, SrcLo, SrcHi);
4970 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4971
Evan Cheng56966222007-01-12 02:11:51 +00004972 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004973 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004974 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004975 else {
4976 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004977 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004978 }
Chris Lattner6831a812006-02-13 09:18:02 +00004979
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004980 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004981 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4982 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004983 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4984 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004985}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004986
Chris Lattner22cde6a2006-01-28 08:25:58 +00004987/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4988/// INT_TO_FP operation of the specified operand when the target requests that
4989/// we expand it. At this point, we know that the result and operand types are
4990/// legal for the target.
4991SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4992 SDOperand Op0,
4993 MVT::ValueType DestVT) {
4994 if (Op0.getValueType() == MVT::i32) {
4995 // simple 32-bit [signed|unsigned] integer to float/double expansion
4996
Chris Lattner58092e32007-01-20 22:35:55 +00004997 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004998 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004999 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
5000 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00005001 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00005002 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005003 // get address of 8 byte buffer
5004 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5005 // word offset constant for Hi/Lo address computation
5006 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5007 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005008 SDOperand Hi = StackSlot;
5009 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5010 if (TLI.isLittleEndian())
5011 std::swap(Hi, Lo);
5012
Chris Lattner22cde6a2006-01-28 08:25:58 +00005013 // if signed map to unsigned space
5014 SDOperand Op0Mapped;
5015 if (isSigned) {
5016 // constant used to invert sign bit (signed to unsigned mapping)
5017 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5018 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5019 } else {
5020 Op0Mapped = Op0;
5021 }
5022 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005023 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005024 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005025 // initial hi portion of constructed double
5026 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5027 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005028 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005029 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005030 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005031 // FP constant to bias correct the final result
5032 SDOperand Bias = DAG.getConstantFP(isSigned ?
5033 BitsToDouble(0x4330000080000000ULL)
5034 : BitsToDouble(0x4330000000000000ULL),
5035 MVT::f64);
5036 // subtract the bias
5037 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5038 // final result
5039 SDOperand Result;
5040 // handle final rounding
5041 if (DestVT == MVT::f64) {
5042 // do nothing
5043 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005044 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5045 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5046 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5047 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005048 }
5049 return Result;
5050 }
5051 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5052 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5053
5054 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5055 DAG.getConstant(0, Op0.getValueType()),
5056 ISD::SETLT);
5057 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5058 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5059 SignSet, Four, Zero);
5060
5061 // If the sign bit of the integer is set, the large number will be treated
5062 // as a negative number. To counteract this, the dynamic code adds an
5063 // offset depending on the data type.
5064 uint64_t FF;
5065 switch (Op0.getValueType()) {
5066 default: assert(0 && "Unsupported integer type!");
5067 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5068 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5069 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5070 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5071 }
5072 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005073 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005074
5075 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5076 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5077 SDOperand FudgeInReg;
5078 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005079 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005080 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005081 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005082 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005083 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005084 }
5085
5086 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5087}
5088
5089/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5090/// *INT_TO_FP operation of the specified operand when the target requests that
5091/// we promote it. At this point, we know that the result and operand types are
5092/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5093/// operation that takes a larger input.
5094SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5095 MVT::ValueType DestVT,
5096 bool isSigned) {
5097 // First step, figure out the appropriate *INT_TO_FP operation to use.
5098 MVT::ValueType NewInTy = LegalOp.getValueType();
5099
5100 unsigned OpToUse = 0;
5101
5102 // Scan for the appropriate larger type to use.
5103 while (1) {
5104 NewInTy = (MVT::ValueType)(NewInTy+1);
5105 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5106
5107 // If the target supports SINT_TO_FP of this type, use it.
5108 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5109 default: break;
5110 case TargetLowering::Legal:
5111 if (!TLI.isTypeLegal(NewInTy))
5112 break; // Can't use this datatype.
5113 // FALL THROUGH.
5114 case TargetLowering::Custom:
5115 OpToUse = ISD::SINT_TO_FP;
5116 break;
5117 }
5118 if (OpToUse) break;
5119 if (isSigned) continue;
5120
5121 // If the target supports UINT_TO_FP of this type, use it.
5122 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5123 default: break;
5124 case TargetLowering::Legal:
5125 if (!TLI.isTypeLegal(NewInTy))
5126 break; // Can't use this datatype.
5127 // FALL THROUGH.
5128 case TargetLowering::Custom:
5129 OpToUse = ISD::UINT_TO_FP;
5130 break;
5131 }
5132 if (OpToUse) break;
5133
5134 // Otherwise, try a larger type.
5135 }
5136
5137 // Okay, we found the operation and type to use. Zero extend our input to the
5138 // desired type then run the operation on it.
5139 return DAG.getNode(OpToUse, DestVT,
5140 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5141 NewInTy, LegalOp));
5142}
5143
5144/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5145/// FP_TO_*INT operation of the specified operand when the target requests that
5146/// we promote it. At this point, we know that the result and operand types are
5147/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5148/// operation that returns a larger result.
5149SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5150 MVT::ValueType DestVT,
5151 bool isSigned) {
5152 // First step, figure out the appropriate FP_TO*INT operation to use.
5153 MVT::ValueType NewOutTy = DestVT;
5154
5155 unsigned OpToUse = 0;
5156
5157 // Scan for the appropriate larger type to use.
5158 while (1) {
5159 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5160 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5161
5162 // If the target supports FP_TO_SINT returning this type, use it.
5163 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5164 default: break;
5165 case TargetLowering::Legal:
5166 if (!TLI.isTypeLegal(NewOutTy))
5167 break; // Can't use this datatype.
5168 // FALL THROUGH.
5169 case TargetLowering::Custom:
5170 OpToUse = ISD::FP_TO_SINT;
5171 break;
5172 }
5173 if (OpToUse) break;
5174
5175 // If the target supports FP_TO_UINT of this type, use it.
5176 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5177 default: break;
5178 case TargetLowering::Legal:
5179 if (!TLI.isTypeLegal(NewOutTy))
5180 break; // Can't use this datatype.
5181 // FALL THROUGH.
5182 case TargetLowering::Custom:
5183 OpToUse = ISD::FP_TO_UINT;
5184 break;
5185 }
5186 if (OpToUse) break;
5187
5188 // Otherwise, try a larger type.
5189 }
5190
Chris Lattner27a6c732007-11-24 07:07:01 +00005191
5192 // Okay, we found the operation and type to use.
5193 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5194
5195 // If the operation produces an invalid type, it must be custom lowered. Use
5196 // the target lowering hooks to expand it. Just keep the low part of the
5197 // expanded operation, we know that we're truncating anyway.
5198 if (getTypeAction(NewOutTy) == Expand) {
5199 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5200 assert(Operation.Val && "Didn't return anything");
5201 }
5202
5203 // Truncate the result of the extended FP_TO_*INT operation to the desired
5204 // size.
5205 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005206}
5207
5208/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5209///
5210SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5211 MVT::ValueType VT = Op.getValueType();
5212 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5213 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5214 switch (VT) {
5215 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5216 case MVT::i16:
5217 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5218 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5219 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5220 case MVT::i32:
5221 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5222 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5223 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5224 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5225 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5226 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5227 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5228 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5229 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5230 case MVT::i64:
5231 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5232 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5233 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5234 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5235 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5236 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5237 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5238 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5239 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5240 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5241 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5242 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5243 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5244 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5245 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5246 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5247 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5248 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5249 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5250 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5251 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5252 }
5253}
5254
5255/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5256///
5257SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5258 switch (Opc) {
5259 default: assert(0 && "Cannot expand this yet!");
5260 case ISD::CTPOP: {
5261 static const uint64_t mask[6] = {
5262 0x5555555555555555ULL, 0x3333333333333333ULL,
5263 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5264 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5265 };
5266 MVT::ValueType VT = Op.getValueType();
5267 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005268 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005269 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5270 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5271 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5272 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5273 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5274 DAG.getNode(ISD::AND, VT,
5275 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5276 }
5277 return Op;
5278 }
5279 case ISD::CTLZ: {
5280 // for now, we do this:
5281 // x = x | (x >> 1);
5282 // x = x | (x >> 2);
5283 // ...
5284 // x = x | (x >>16);
5285 // x = x | (x >>32); // for 64-bit input
5286 // return popcount(~x);
5287 //
5288 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5289 MVT::ValueType VT = Op.getValueType();
5290 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005291 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005292 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5293 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5294 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5295 }
5296 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5297 return DAG.getNode(ISD::CTPOP, VT, Op);
5298 }
5299 case ISD::CTTZ: {
5300 // for now, we use: { return popcount(~x & (x - 1)); }
5301 // unless the target has ctlz but not ctpop, in which case we use:
5302 // { return 32 - nlz(~x & (x-1)); }
5303 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5304 MVT::ValueType VT = Op.getValueType();
5305 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5306 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5307 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5308 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5309 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5310 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5311 TLI.isOperationLegal(ISD::CTLZ, VT))
5312 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005313 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005314 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5315 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5316 }
5317 }
5318}
Chris Lattnere34b3962005-01-19 04:19:40 +00005319
Chris Lattner3e928bb2005-01-07 07:47:09 +00005320/// ExpandOp - Expand the specified SDOperand into its two component pieces
5321/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5322/// LegalizeNodes map is filled in for any results that are not expanded, the
5323/// ExpandedNodes map is filled in for any results that are expanded, and the
5324/// Lo/Hi values are returned.
5325void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5326 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005327 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005328 SDNode *Node = Op.Val;
5329 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005330 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005331 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005332 "Cannot expand to FP value or to larger int value!");
5333
Chris Lattner6fdcb252005-09-02 20:32:45 +00005334 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005335 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005336 = ExpandedNodes.find(Op);
5337 if (I != ExpandedNodes.end()) {
5338 Lo = I->second.first;
5339 Hi = I->second.second;
5340 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005341 }
5342
Chris Lattner3e928bb2005-01-07 07:47:09 +00005343 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005344 case ISD::CopyFromReg:
5345 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005346 case ISD::FP_ROUND_INREG:
5347 if (VT == MVT::ppcf128 &&
5348 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5349 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005350 SDOperand SrcLo, SrcHi, Src;
5351 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5352 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5353 SDOperand Result = TLI.LowerOperation(
5354 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005355 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5356 Lo = Result.Val->getOperand(0);
5357 Hi = Result.Val->getOperand(1);
5358 break;
5359 }
5360 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005361 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005362#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005363 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005364#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005365 assert(0 && "Do not know how to expand this operator!");
5366 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005367 case ISD::EXTRACT_VECTOR_ELT:
5368 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5369 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5370 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5371 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005372 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005373 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005374 Lo = DAG.getNode(ISD::UNDEF, NVT);
5375 Hi = DAG.getNode(ISD::UNDEF, NVT);
5376 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005377 case ISD::Constant: {
5378 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5379 Lo = DAG.getConstant(Cst, NVT);
5380 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5381 break;
5382 }
Evan Cheng00495212006-12-12 21:32:44 +00005383 case ISD::ConstantFP: {
5384 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005385 if (CFP->getValueType(0) == MVT::ppcf128) {
5386 APInt api = CFP->getValueAPF().convertToAPInt();
5387 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5388 MVT::f64);
5389 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5390 MVT::f64);
5391 break;
5392 }
Evan Cheng279101e2006-12-12 22:19:28 +00005393 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005394 if (getTypeAction(Lo.getValueType()) == Expand)
5395 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005396 break;
5397 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005398 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005399 // Return the operands.
5400 Lo = Node->getOperand(0);
5401 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005402 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005403
5404 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005405 if (Node->getNumValues() == 1) {
5406 ExpandOp(Op.getOperand(0), Lo, Hi);
5407 break;
5408 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005409 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5410 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5411 Op.getValue(1).getValueType() == MVT::Other &&
5412 "unhandled MERGE_VALUES");
5413 ExpandOp(Op.getOperand(0), Lo, Hi);
5414 // Remember that we legalized the chain.
5415 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5416 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005417
5418 case ISD::SIGN_EXTEND_INREG:
5419 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005420 // sext_inreg the low part if needed.
5421 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5422
5423 // The high part gets the sign extension from the lo-part. This handles
5424 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005425 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5426 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5427 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005428 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005429
Nate Begemand88fc032006-01-14 03:14:10 +00005430 case ISD::BSWAP: {
5431 ExpandOp(Node->getOperand(0), Lo, Hi);
5432 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5433 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5434 Lo = TempLo;
5435 break;
5436 }
5437
Chris Lattneredb1add2005-05-11 04:51:16 +00005438 case ISD::CTPOP:
5439 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005440 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5441 DAG.getNode(ISD::CTPOP, NVT, Lo),
5442 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005443 Hi = DAG.getConstant(0, NVT);
5444 break;
5445
Chris Lattner39a8f332005-05-12 19:05:01 +00005446 case ISD::CTLZ: {
5447 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005448 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005449 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5450 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005451 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5452 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005453 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5454 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5455
5456 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5457 Hi = DAG.getConstant(0, NVT);
5458 break;
5459 }
5460
5461 case ISD::CTTZ: {
5462 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005463 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005464 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5465 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005466 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5467 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005468 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5469 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5470
5471 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5472 Hi = DAG.getConstant(0, NVT);
5473 break;
5474 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005475
Nate Begemanacc398c2006-01-25 18:21:52 +00005476 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005477 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5478 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005479 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5480 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5481
5482 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005483 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005484 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5485 if (!TLI.isLittleEndian())
5486 std::swap(Lo, Hi);
5487 break;
5488 }
5489
Chris Lattner3e928bb2005-01-07 07:47:09 +00005490 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005491 LoadSDNode *LD = cast<LoadSDNode>(Node);
5492 SDOperand Ch = LD->getChain(); // Legalize the chain.
5493 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5494 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005495 int SVOffset = LD->getSrcValueOffset();
5496 unsigned Alignment = LD->getAlignment();
5497 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005498
Evan Cheng466685d2006-10-09 20:57:25 +00005499 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005500 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5501 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005502 if (VT == MVT::f32 || VT == MVT::f64) {
5503 // f32->i32 or f64->i64 one to one expansion.
5504 // Remember that we legalized the chain.
5505 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005506 // Recursively expand the new load.
5507 if (getTypeAction(NVT) == Expand)
5508 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005509 break;
5510 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005511
Evan Cheng466685d2006-10-09 20:57:25 +00005512 // Increment the pointer to the other half.
5513 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5514 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5515 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005516 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005517 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005518 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5519 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005520
Evan Cheng466685d2006-10-09 20:57:25 +00005521 // Build a factor node to remember that this load is independent of the
5522 // other one.
5523 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5524 Hi.getValue(1));
5525
5526 // Remember that we legalized the chain.
5527 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5528 if (!TLI.isLittleEndian())
5529 std::swap(Lo, Hi);
5530 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005531 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005532
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005533 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5534 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005535 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5536 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005537 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005538 // Remember that we legalized the chain.
5539 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5540 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5541 break;
5542 }
Evan Cheng466685d2006-10-09 20:57:25 +00005543
5544 if (EVT == NVT)
5545 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005546 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005547 else
5548 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005549 SVOffset, EVT, isVolatile,
5550 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005551
5552 // Remember that we legalized the chain.
5553 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5554
5555 if (ExtType == ISD::SEXTLOAD) {
5556 // The high part is obtained by SRA'ing all but one of the bits of the
5557 // lo part.
5558 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5559 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5560 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5561 } else if (ExtType == ISD::ZEXTLOAD) {
5562 // The high part is just a zero.
5563 Hi = DAG.getConstant(0, NVT);
5564 } else /* if (ExtType == ISD::EXTLOAD) */ {
5565 // The high part is undefined.
5566 Hi = DAG.getNode(ISD::UNDEF, NVT);
5567 }
5568 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005569 break;
5570 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005571 case ISD::AND:
5572 case ISD::OR:
5573 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5574 SDOperand LL, LH, RL, RH;
5575 ExpandOp(Node->getOperand(0), LL, LH);
5576 ExpandOp(Node->getOperand(1), RL, RH);
5577 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5578 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5579 break;
5580 }
5581 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005582 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005583 ExpandOp(Node->getOperand(1), LL, LH);
5584 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005585 if (getTypeAction(NVT) == Expand)
5586 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005587 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005588 if (VT != MVT::f32)
5589 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005590 break;
5591 }
Nate Begeman9373a812005-08-10 20:51:12 +00005592 case ISD::SELECT_CC: {
5593 SDOperand TL, TH, FL, FH;
5594 ExpandOp(Node->getOperand(2), TL, TH);
5595 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005596 if (getTypeAction(NVT) == Expand)
5597 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005598 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5599 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005600 if (VT != MVT::f32)
5601 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5602 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005603 break;
5604 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005605 case ISD::ANY_EXTEND:
5606 // The low part is any extension of the input (which degenerates to a copy).
5607 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5608 // The high part is undefined.
5609 Hi = DAG.getNode(ISD::UNDEF, NVT);
5610 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005611 case ISD::SIGN_EXTEND: {
5612 // The low part is just a sign extension of the input (which degenerates to
5613 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005614 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005615
Chris Lattner3e928bb2005-01-07 07:47:09 +00005616 // The high part is obtained by SRA'ing all but one of the bits of the lo
5617 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005618 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005619 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5620 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005621 break;
5622 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005623 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005624 // The low part is just a zero extension of the input (which degenerates to
5625 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005626 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005627
Chris Lattner3e928bb2005-01-07 07:47:09 +00005628 // The high part is just a zero.
5629 Hi = DAG.getConstant(0, NVT);
5630 break;
Chris Lattner35481892005-12-23 00:16:34 +00005631
Chris Lattner4c948eb2007-02-13 23:55:16 +00005632 case ISD::TRUNCATE: {
5633 // The input value must be larger than this value. Expand *it*.
5634 SDOperand NewLo;
5635 ExpandOp(Node->getOperand(0), NewLo, Hi);
5636
5637 // The low part is now either the right size, or it is closer. If not the
5638 // right size, make an illegal truncate so we recursively expand it.
5639 if (NewLo.getValueType() != Node->getValueType(0))
5640 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5641 ExpandOp(NewLo, Lo, Hi);
5642 break;
5643 }
5644
Chris Lattner35481892005-12-23 00:16:34 +00005645 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005646 SDOperand Tmp;
5647 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5648 // If the target wants to, allow it to lower this itself.
5649 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5650 case Expand: assert(0 && "cannot expand FP!");
5651 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5652 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5653 }
5654 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5655 }
5656
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005657 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005658 if (VT == MVT::f32 || VT == MVT::f64) {
5659 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005660 if (getTypeAction(NVT) == Expand)
5661 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005662 break;
5663 }
5664
5665 // If source operand will be expanded to the same type as VT, i.e.
5666 // i64 <- f64, i32 <- f32, expand the source operand instead.
5667 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5668 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5669 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005670 break;
5671 }
5672
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005673 // Turn this into a load/store pair by default.
5674 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005675 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005676
Chris Lattner35481892005-12-23 00:16:34 +00005677 ExpandOp(Tmp, Lo, Hi);
5678 break;
5679 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005680
Chris Lattner27a6c732007-11-24 07:07:01 +00005681 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005682 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5683 TargetLowering::Custom &&
5684 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005685 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5686 assert(Tmp.Val && "Node must be custom expanded!");
5687 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005688 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005689 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005690 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005691 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005692
Chris Lattner4e6c7462005-01-08 19:27:05 +00005693 // These operators cannot be expanded directly, emit them as calls to
5694 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005695 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005696 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005697 SDOperand Op;
5698 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5699 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005700 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5701 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005702 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005703
Chris Lattnerf20d1832005-07-30 01:40:57 +00005704 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5705
Chris Lattner80a3e942005-07-29 00:33:32 +00005706 // Now that the custom expander is done, expand the result, which is still
5707 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005708 if (Op.Val) {
5709 ExpandOp(Op, Lo, Hi);
5710 break;
5711 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005712 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005713
Dale Johannesen161e8972007-10-05 20:04:43 +00005714 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005715 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005716 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005717 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005718 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005719 else if (Node->getOperand(0).getValueType() == MVT::f80)
5720 LC = RTLIB::FPTOSINT_F80_I64;
5721 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5722 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005723 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5724 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005725 break;
Evan Cheng56966222007-01-12 02:11:51 +00005726 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005727
Evan Cheng56966222007-01-12 02:11:51 +00005728 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005729 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005730 SDOperand Op;
5731 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5732 case Expand: assert(0 && "cannot expand FP!");
5733 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5734 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5735 }
5736
5737 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5738
5739 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005740 if (Op.Val) {
5741 ExpandOp(Op, Lo, Hi);
5742 break;
5743 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005744 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005745
Evan Chengdaccea182007-10-05 01:09:32 +00005746 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005747 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005748 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005749 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005750 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005751 else if (Node->getOperand(0).getValueType() == MVT::f80)
5752 LC = RTLIB::FPTOUINT_F80_I64;
5753 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5754 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005755 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5756 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005757 break;
Evan Cheng56966222007-01-12 02:11:51 +00005758 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005759
Evan Cheng05a2d562006-01-09 18:31:59 +00005760 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005761 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005762 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005763 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005764 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005765 Op = TLI.LowerOperation(Op, DAG);
5766 if (Op.Val) {
5767 // Now that the custom expander is done, expand the result, which is
5768 // still VT.
5769 ExpandOp(Op, Lo, Hi);
5770 break;
5771 }
5772 }
5773
Chris Lattner79980b02006-09-13 03:50:39 +00005774 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5775 // this X << 1 as X+X.
5776 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5777 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5778 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5779 SDOperand LoOps[2], HiOps[3];
5780 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5781 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5782 LoOps[1] = LoOps[0];
5783 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5784
5785 HiOps[1] = HiOps[0];
5786 HiOps[2] = Lo.getValue(1);
5787 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5788 break;
5789 }
5790 }
5791
Chris Lattnere34b3962005-01-19 04:19:40 +00005792 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005793 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005794 break;
Chris Lattner47599822005-04-02 03:38:53 +00005795
5796 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005797 TargetLowering::LegalizeAction Action =
5798 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5799 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5800 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005801 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005802 break;
5803 }
5804
Chris Lattnere34b3962005-01-19 04:19:40 +00005805 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005806 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5807 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005808 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005809 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005810
Evan Cheng05a2d562006-01-09 18:31:59 +00005811 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005812 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005813 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005814 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005815 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005816 Op = TLI.LowerOperation(Op, DAG);
5817 if (Op.Val) {
5818 // Now that the custom expander is done, expand the result, which is
5819 // still VT.
5820 ExpandOp(Op, Lo, Hi);
5821 break;
5822 }
5823 }
5824
Chris Lattnere34b3962005-01-19 04:19:40 +00005825 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005826 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005827 break;
Chris Lattner47599822005-04-02 03:38:53 +00005828
5829 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005830 TargetLowering::LegalizeAction Action =
5831 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5832 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5833 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005834 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005835 break;
5836 }
5837
Chris Lattnere34b3962005-01-19 04:19:40 +00005838 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005839 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5840 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005841 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005842 }
5843
5844 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005845 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005846 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005847 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005848 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005849 Op = TLI.LowerOperation(Op, DAG);
5850 if (Op.Val) {
5851 // Now that the custom expander is done, expand the result, which is
5852 // still VT.
5853 ExpandOp(Op, Lo, Hi);
5854 break;
5855 }
5856 }
5857
Chris Lattnere34b3962005-01-19 04:19:40 +00005858 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005859 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005860 break;
Chris Lattner47599822005-04-02 03:38:53 +00005861
5862 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005863 TargetLowering::LegalizeAction Action =
5864 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5865 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5866 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005867 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005868 break;
5869 }
5870
Chris Lattnere34b3962005-01-19 04:19:40 +00005871 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005872 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5873 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005874 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005875 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005876
Misha Brukmanedf128a2005-04-21 22:36:52 +00005877 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005878 case ISD::SUB: {
5879 // If the target wants to custom expand this, let them.
5880 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5881 TargetLowering::Custom) {
5882 Op = TLI.LowerOperation(Op, DAG);
5883 if (Op.Val) {
5884 ExpandOp(Op, Lo, Hi);
5885 break;
5886 }
5887 }
5888
5889 // Expand the subcomponents.
5890 SDOperand LHSL, LHSH, RHSL, RHSH;
5891 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5892 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005893 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5894 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005895 LoOps[0] = LHSL;
5896 LoOps[1] = RHSL;
5897 HiOps[0] = LHSH;
5898 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005899 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005900 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005901 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005902 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005903 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005904 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005905 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005906 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005907 }
Chris Lattner84f67882005-01-20 18:52:28 +00005908 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005909 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005910
5911 case ISD::ADDC:
5912 case ISD::SUBC: {
5913 // Expand the subcomponents.
5914 SDOperand LHSL, LHSH, RHSL, RHSH;
5915 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5916 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5917 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5918 SDOperand LoOps[2] = { LHSL, RHSL };
5919 SDOperand HiOps[3] = { LHSH, RHSH };
5920
5921 if (Node->getOpcode() == ISD::ADDC) {
5922 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5923 HiOps[2] = Lo.getValue(1);
5924 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5925 } else {
5926 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5927 HiOps[2] = Lo.getValue(1);
5928 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5929 }
5930 // Remember that we legalized the flag.
5931 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5932 break;
5933 }
5934 case ISD::ADDE:
5935 case ISD::SUBE: {
5936 // Expand the subcomponents.
5937 SDOperand LHSL, LHSH, RHSL, RHSH;
5938 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5939 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5940 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5941 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5942 SDOperand HiOps[3] = { LHSH, RHSH };
5943
5944 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5945 HiOps[2] = Lo.getValue(1);
5946 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5947
5948 // Remember that we legalized the flag.
5949 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5950 break;
5951 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005952 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005953 // If the target wants to custom expand this, let them.
5954 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005955 SDOperand New = TLI.LowerOperation(Op, DAG);
5956 if (New.Val) {
5957 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005958 break;
5959 }
5960 }
5961
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005962 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5963 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00005964 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5965 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5966 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005967 SDOperand LL, LH, RL, RH;
5968 ExpandOp(Node->getOperand(0), LL, LH);
5969 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00005970 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5971 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5972 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5973 // FIXME: generalize this to handle other bit sizes
5974 if (LHSSB == 32 && RHSSB == 32 &&
5975 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5976 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5977 // The inputs are both zero-extended.
5978 if (HasUMUL_LOHI) {
5979 // We can emit a umul_lohi.
5980 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5981 Hi = SDOperand(Lo.Val, 1);
5982 break;
5983 }
5984 if (HasMULHU) {
5985 // We can emit a mulhu+mul.
5986 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5987 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5988 break;
5989 }
Dan Gohman525178c2007-10-08 18:33:35 +00005990 }
5991 if (LHSSB > BitSize && RHSSB > BitSize) {
5992 // The input values are both sign-extended.
5993 if (HasSMUL_LOHI) {
5994 // We can emit a smul_lohi.
5995 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5996 Hi = SDOperand(Lo.Val, 1);
5997 break;
5998 }
5999 if (HasMULHS) {
6000 // We can emit a mulhs+mul.
6001 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6002 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6003 break;
6004 }
6005 }
6006 if (HasUMUL_LOHI) {
6007 // Lo,Hi = umul LHS, RHS.
6008 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6009 DAG.getVTList(NVT, NVT), LL, RL);
6010 Lo = UMulLOHI;
6011 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006012 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6013 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6014 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6015 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006016 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006017 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006018 if (HasMULHU) {
6019 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6020 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6021 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6022 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6023 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6024 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6025 break;
6026 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006027 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006028
Dan Gohman525178c2007-10-08 18:33:35 +00006029 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006030 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6031 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006032 break;
6033 }
Evan Cheng56966222007-01-12 02:11:51 +00006034 case ISD::SDIV:
6035 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6036 break;
6037 case ISD::UDIV:
6038 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6039 break;
6040 case ISD::SREM:
6041 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6042 break;
6043 case ISD::UREM:
6044 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6045 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006046
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006047 case ISD::FADD:
Dale Johannesen161e8972007-10-05 20:04:43 +00006048 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
6049 VT == MVT::f64 ? RTLIB::ADD_F64 :
6050 VT == MVT::ppcf128 ?
6051 RTLIB::ADD_PPCF128 :
6052 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006053 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006054 break;
6055 case ISD::FSUB:
Dale Johannesen161e8972007-10-05 20:04:43 +00006056 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
6057 VT == MVT::f64 ? RTLIB::SUB_F64 :
6058 VT == MVT::ppcf128 ?
6059 RTLIB::SUB_PPCF128 :
6060 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006061 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006062 break;
6063 case ISD::FMUL:
Dale Johannesen161e8972007-10-05 20:04:43 +00006064 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
6065 VT == MVT::f64 ? RTLIB::MUL_F64 :
6066 VT == MVT::ppcf128 ?
6067 RTLIB::MUL_PPCF128 :
6068 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006069 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006070 break;
6071 case ISD::FDIV:
Dale Johannesen161e8972007-10-05 20:04:43 +00006072 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
6073 VT == MVT::f64 ? RTLIB::DIV_F64 :
6074 VT == MVT::ppcf128 ?
6075 RTLIB::DIV_PPCF128 :
6076 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006077 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006078 break;
6079 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006080 if (VT == MVT::ppcf128) {
6081 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6082 Node->getOperand(0).getValueType()==MVT::f64);
6083 const uint64_t zero = 0;
6084 if (Node->getOperand(0).getValueType()==MVT::f32)
6085 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6086 else
6087 Hi = Node->getOperand(0);
6088 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6089 break;
6090 }
Evan Cheng56966222007-01-12 02:11:51 +00006091 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006092 break;
6093 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006094 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006095 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006096 case ISD::FPOWI:
Dale Johannesen317096a2007-09-28 01:08:20 +00006097 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6098 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006099 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6100 (VT == MVT::ppcf128) ?
6101 RTLIB::POWI_PPCF128 :
6102 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006103 Node, false, Hi);
6104 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006105 case ISD::FSQRT:
6106 case ISD::FSIN:
6107 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006108 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006109 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006110 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00006111 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006112 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6113 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6114 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6115 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00006116 break;
6117 case ISD::FSIN:
6118 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6119 break;
6120 case ISD::FCOS:
6121 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6122 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006123 default: assert(0 && "Unreachable!");
6124 }
Evan Cheng56966222007-01-12 02:11:51 +00006125 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006126 break;
6127 }
Evan Cheng966bf242006-12-16 00:52:40 +00006128 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006129 if (VT == MVT::ppcf128) {
6130 SDOperand Tmp;
6131 ExpandOp(Node->getOperand(0), Lo, Tmp);
6132 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6133 // lo = hi==fabs(hi) ? lo : -lo;
6134 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6135 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6136 DAG.getCondCode(ISD::SETEQ));
6137 break;
6138 }
Evan Cheng966bf242006-12-16 00:52:40 +00006139 SDOperand Mask = (VT == MVT::f64)
6140 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6141 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6142 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6143 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6144 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6145 if (getTypeAction(NVT) == Expand)
6146 ExpandOp(Lo, Lo, Hi);
6147 break;
6148 }
6149 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006150 if (VT == MVT::ppcf128) {
6151 ExpandOp(Node->getOperand(0), Lo, Hi);
6152 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6153 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6154 break;
6155 }
Evan Cheng966bf242006-12-16 00:52:40 +00006156 SDOperand Mask = (VT == MVT::f64)
6157 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6158 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6159 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6160 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6161 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6162 if (getTypeAction(NVT) == Expand)
6163 ExpandOp(Lo, Lo, Hi);
6164 break;
6165 }
Evan Cheng912095b2007-01-04 21:56:39 +00006166 case ISD::FCOPYSIGN: {
6167 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6168 if (getTypeAction(NVT) == Expand)
6169 ExpandOp(Lo, Lo, Hi);
6170 break;
6171 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006172 case ISD::SINT_TO_FP:
6173 case ISD::UINT_TO_FP: {
6174 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6175 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006176 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006177 static uint64_t zero = 0;
6178 if (isSigned) {
6179 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6180 Node->getOperand(0)));
6181 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6182 } else {
6183 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6184 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6185 Node->getOperand(0)));
6186 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6187 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006188 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006189 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6190 DAG.getConstant(0, MVT::i32),
6191 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6192 DAG.getConstantFP(
6193 APFloat(APInt(128, 2, TwoE32)),
6194 MVT::ppcf128)),
6195 Hi,
6196 DAG.getCondCode(ISD::SETLT)),
6197 Lo, Hi);
6198 }
6199 break;
6200 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006201 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6202 // si64->ppcf128 done by libcall, below
6203 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6204 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6205 Lo, Hi);
6206 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6207 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6208 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6209 DAG.getConstant(0, MVT::i64),
6210 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6211 DAG.getConstantFP(
6212 APFloat(APInt(128, 2, TwoE64)),
6213 MVT::ppcf128)),
6214 Hi,
6215 DAG.getCondCode(ISD::SETLT)),
6216 Lo, Hi);
6217 break;
6218 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006219 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006220 if (Node->getOperand(0).getValueType() == MVT::i64) {
6221 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006222 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006223 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006224 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006225 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006226 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006227 LC = RTLIB::SINTTOFP_I64_F80;
6228 }
6229 else if (VT == MVT::ppcf128) {
6230 assert(isSigned);
6231 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006232 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006233 } else {
6234 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006235 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006236 else
Evan Cheng56966222007-01-12 02:11:51 +00006237 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006238 }
6239
6240 // Promote the operand if needed.
6241 if (getTypeAction(SrcVT) == Promote) {
6242 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6243 Tmp = isSigned
6244 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6245 DAG.getValueType(SrcVT))
6246 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6247 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6248 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006249
6250 const char *LibCall = TLI.getLibcallName(LC);
6251 if (LibCall)
6252 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6253 else {
6254 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6255 Node->getOperand(0));
6256 if (getTypeAction(Lo.getValueType()) == Expand)
6257 ExpandOp(Lo, Lo, Hi);
6258 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006259 break;
6260 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006261 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006262
Chris Lattner83397362005-12-21 18:02:52 +00006263 // Make sure the resultant values have been legalized themselves, unless this
6264 // is a type that requires multi-step expansion.
6265 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6266 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006267 if (Hi.Val)
6268 // Don't legalize the high part if it is expanded to a single node.
6269 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006270 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006271
6272 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006273 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006274 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006275}
6276
Dan Gohman7f321562007-06-25 16:23:39 +00006277/// SplitVectorOp - Given an operand of vector type, break it down into
6278/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006279void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6280 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006281 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006282 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006283 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006284 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006285
Dan Gohmane40c7b02007-09-24 15:54:53 +00006286 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006287
6288 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6289 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6290
6291 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6292 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6293
Chris Lattnerc7029802006-03-18 01:44:44 +00006294 // See if we already split it.
6295 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6296 = SplitNodes.find(Op);
6297 if (I != SplitNodes.end()) {
6298 Lo = I->second.first;
6299 Hi = I->second.second;
6300 return;
6301 }
6302
6303 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006304 default:
6305#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006306 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006307#endif
6308 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006309 case ISD::UNDEF:
6310 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6311 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6312 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006313 case ISD::BUILD_PAIR:
6314 Lo = Node->getOperand(0);
6315 Hi = Node->getOperand(1);
6316 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006317 case ISD::INSERT_VECTOR_ELT: {
6318 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6319 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6320 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006321 if (Index < NewNumElts_Lo)
6322 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006323 DAG.getConstant(Index, TLI.getPointerTy()));
6324 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006325 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6326 DAG.getConstant(Index - NewNumElts_Lo,
6327 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006328 break;
6329 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006330 case ISD::VECTOR_SHUFFLE: {
6331 // Build the low part.
6332 SDOperand Mask = Node->getOperand(2);
6333 SmallVector<SDOperand, 8> Ops;
6334 MVT::ValueType PtrVT = TLI.getPointerTy();
6335
6336 // Insert all of the elements from the input that are needed. We use
6337 // buildvector of extractelement here because the input vectors will have
6338 // to be legalized, so this makes the code simpler.
6339 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6340 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6341 SDOperand InVec = Node->getOperand(0);
6342 if (Idx >= NumElements) {
6343 InVec = Node->getOperand(1);
6344 Idx -= NumElements;
6345 }
6346 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6347 DAG.getConstant(Idx, PtrVT)));
6348 }
6349 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6350 Ops.clear();
6351
6352 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6353 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6354 SDOperand InVec = Node->getOperand(0);
6355 if (Idx >= NumElements) {
6356 InVec = Node->getOperand(1);
6357 Idx -= NumElements;
6358 }
6359 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6360 DAG.getConstant(Idx, PtrVT)));
6361 }
6362 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6363 break;
6364 }
Dan Gohman7f321562007-06-25 16:23:39 +00006365 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006366 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006367 Node->op_begin()+NewNumElts_Lo);
6368 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006369
Nate Begeman5db1afb2007-11-15 21:15:26 +00006370 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006371 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006372 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006373 break;
6374 }
Dan Gohman7f321562007-06-25 16:23:39 +00006375 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006376 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006377 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6378 if (NewNumSubvectors == 1) {
6379 Lo = Node->getOperand(0);
6380 Hi = Node->getOperand(1);
6381 } else {
6382 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6383 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006384 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006385
Dan Gohman7f321562007-06-25 16:23:39 +00006386 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6387 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006388 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006389 }
Dan Gohman65956352007-06-13 15:12:02 +00006390 break;
6391 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006392 case ISD::SELECT: {
6393 SDOperand Cond = Node->getOperand(0);
6394
6395 SDOperand LL, LH, RL, RH;
6396 SplitVectorOp(Node->getOperand(1), LL, LH);
6397 SplitVectorOp(Node->getOperand(2), RL, RH);
6398
6399 if (MVT::isVector(Cond.getValueType())) {
6400 // Handle a vector merge.
6401 SDOperand CL, CH;
6402 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006403 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6404 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006405 } else {
6406 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006407 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6408 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006409 }
6410 break;
6411 }
Dan Gohman7f321562007-06-25 16:23:39 +00006412 case ISD::ADD:
6413 case ISD::SUB:
6414 case ISD::MUL:
6415 case ISD::FADD:
6416 case ISD::FSUB:
6417 case ISD::FMUL:
6418 case ISD::SDIV:
6419 case ISD::UDIV:
6420 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006421 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006422 case ISD::AND:
6423 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006424 case ISD::XOR:
6425 case ISD::UREM:
6426 case ISD::SREM:
6427 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006428 SDOperand LL, LH, RL, RH;
6429 SplitVectorOp(Node->getOperand(0), LL, LH);
6430 SplitVectorOp(Node->getOperand(1), RL, RH);
6431
Nate Begeman5db1afb2007-11-15 21:15:26 +00006432 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6433 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006434 break;
6435 }
Dan Gohman82669522007-10-11 23:57:53 +00006436 case ISD::FPOWI: {
6437 SDOperand L, H;
6438 SplitVectorOp(Node->getOperand(0), L, H);
6439
Nate Begeman5db1afb2007-11-15 21:15:26 +00006440 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6441 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006442 break;
6443 }
6444 case ISD::CTTZ:
6445 case ISD::CTLZ:
6446 case ISD::CTPOP:
6447 case ISD::FNEG:
6448 case ISD::FABS:
6449 case ISD::FSQRT:
6450 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006451 case ISD::FCOS:
6452 case ISD::FP_TO_SINT:
6453 case ISD::FP_TO_UINT:
6454 case ISD::SINT_TO_FP:
6455 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006456 SDOperand L, H;
6457 SplitVectorOp(Node->getOperand(0), L, H);
6458
Nate Begeman5db1afb2007-11-15 21:15:26 +00006459 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6460 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006461 break;
6462 }
Dan Gohman7f321562007-06-25 16:23:39 +00006463 case ISD::LOAD: {
6464 LoadSDNode *LD = cast<LoadSDNode>(Node);
6465 SDOperand Ch = LD->getChain();
6466 SDOperand Ptr = LD->getBasePtr();
6467 const Value *SV = LD->getSrcValue();
6468 int SVOffset = LD->getSrcValueOffset();
6469 unsigned Alignment = LD->getAlignment();
6470 bool isVolatile = LD->isVolatile();
6471
Nate Begeman5db1afb2007-11-15 21:15:26 +00006472 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6473 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006474 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6475 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006476 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006477 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006478 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006479
6480 // Build a factor node to remember that this load is independent of the
6481 // other one.
6482 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6483 Hi.getValue(1));
6484
6485 // Remember that we legalized the chain.
6486 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006487 break;
6488 }
Dan Gohman7f321562007-06-25 16:23:39 +00006489 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006490 // We know the result is a vector. The input may be either a vector or a
6491 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006492 SDOperand InOp = Node->getOperand(0);
6493 if (!MVT::isVector(InOp.getValueType()) ||
6494 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6495 // The input is a scalar or single-element vector.
6496 // Lower to a store/load so that it can be split.
6497 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006498 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006499
Evan Cheng786225a2006-10-05 23:01:46 +00006500 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006501 InOp, Ptr, NULL, 0);
6502 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006503 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006504 // Split the vector and convert each of the pieces now.
6505 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006506 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6507 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006508 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006509 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006510 }
6511
6512 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006513 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006514 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006515 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006516}
6517
6518
Dan Gohman89b20c02007-06-27 14:06:22 +00006519/// ScalarizeVectorOp - Given an operand of single-element vector type
6520/// (e.g. v1f32), convert it into the equivalent operation that returns a
6521/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006522SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6523 assert(MVT::isVector(Op.getValueType()) &&
6524 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006525 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006526 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6527 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006528
Dan Gohman7f321562007-06-25 16:23:39 +00006529 // See if we already scalarized it.
6530 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6531 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006532
6533 SDOperand Result;
6534 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006535 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006536#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006537 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006538#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006539 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6540 case ISD::ADD:
6541 case ISD::FADD:
6542 case ISD::SUB:
6543 case ISD::FSUB:
6544 case ISD::MUL:
6545 case ISD::FMUL:
6546 case ISD::SDIV:
6547 case ISD::UDIV:
6548 case ISD::FDIV:
6549 case ISD::SREM:
6550 case ISD::UREM:
6551 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006552 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006553 case ISD::AND:
6554 case ISD::OR:
6555 case ISD::XOR:
6556 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006557 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006558 ScalarizeVectorOp(Node->getOperand(0)),
6559 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006560 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006561 case ISD::FNEG:
6562 case ISD::FABS:
6563 case ISD::FSQRT:
6564 case ISD::FSIN:
6565 case ISD::FCOS:
6566 Result = DAG.getNode(Node->getOpcode(),
6567 NewVT,
6568 ScalarizeVectorOp(Node->getOperand(0)));
6569 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006570 case ISD::FPOWI:
6571 Result = DAG.getNode(Node->getOpcode(),
6572 NewVT,
6573 ScalarizeVectorOp(Node->getOperand(0)),
6574 Node->getOperand(1));
6575 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006576 case ISD::LOAD: {
6577 LoadSDNode *LD = cast<LoadSDNode>(Node);
6578 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6579 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006580
Dan Gohman7f321562007-06-25 16:23:39 +00006581 const Value *SV = LD->getSrcValue();
6582 int SVOffset = LD->getSrcValueOffset();
6583 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6584 LD->isVolatile(), LD->getAlignment());
6585
Chris Lattnerc7029802006-03-18 01:44:44 +00006586 // Remember that we legalized the chain.
6587 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6588 break;
6589 }
Dan Gohman7f321562007-06-25 16:23:39 +00006590 case ISD::BUILD_VECTOR:
6591 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006592 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006593 case ISD::INSERT_VECTOR_ELT:
6594 // Returning the inserted scalar element.
6595 Result = Node->getOperand(1);
6596 break;
6597 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006598 assert(Node->getOperand(0).getValueType() == NewVT &&
6599 "Concat of non-legal vectors not yet supported!");
6600 Result = Node->getOperand(0);
6601 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006602 case ISD::VECTOR_SHUFFLE: {
6603 // Figure out if the scalar is the LHS or RHS and return it.
6604 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6605 if (cast<ConstantSDNode>(EltNum)->getValue())
6606 Result = ScalarizeVectorOp(Node->getOperand(1));
6607 else
6608 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006609 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006610 }
6611 case ISD::EXTRACT_SUBVECTOR:
6612 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006613 assert(Result.getValueType() == NewVT);
6614 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006615 case ISD::BIT_CONVERT:
6616 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006617 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006618 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006619 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006620 ScalarizeVectorOp(Op.getOperand(1)),
6621 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006622 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006623 }
6624
6625 if (TLI.isTypeLegal(NewVT))
6626 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006627 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6628 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006629 return Result;
6630}
6631
Chris Lattner3e928bb2005-01-07 07:47:09 +00006632
6633// SelectionDAG::Legalize - This is the entry point for the file.
6634//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006635void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006636 if (ViewLegalizeDAGs) viewGraph();
6637
Chris Lattner3e928bb2005-01-07 07:47:09 +00006638 /// run - This is the main entry point to this class.
6639 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006640 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006641}
6642