blob: 3f3ffd7d56289514a452c8e0dc7ad3347c97371b [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000032#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattner5e46a192006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattner3e928bb2005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Chris Lattner6831a812006-02-13 09:18:02 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Chris Lattner03c85462005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattnerc7029802006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohman7f321562007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000107
Chris Lattner8afc48e2005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Chris Lattner03c85462005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121public:
122
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner456a93a2006-01-28 07:39:30 +0000140private:
Dan Gohman7f321562007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000149
Dan Gohman82669522007-10-11 23:57:53 +0000150 /// UnrollVectorOp - We know that the given vector has a legal type, however
151 /// the operation it performs is not legal and is an operation that we have
152 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
153 /// operating on each element individually.
154 SDOperand UnrollVectorOp(SDOperand O);
155
Chris Lattnerc7029802006-03-18 01:44:44 +0000156 /// PromoteOp - Given an operation that produces a value in an invalid type,
157 /// promote it to compute the value into a larger type. The produced value
158 /// will have the correct bits for the low portion of the register, but no
159 /// guarantee is made about the top bits: it may be zero, sign-extended, or
160 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000161 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000162
Chris Lattnerc7029802006-03-18 01:44:44 +0000163 /// ExpandOp - Expand the specified SDOperand into its two component pieces
164 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
165 /// the LegalizeNodes map is filled in for any results that are not expanded,
166 /// the ExpandedNodes map is filled in for any results that are expanded, and
167 /// the Lo/Hi values are returned. This applies to integer types and Vector
168 /// types.
169 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
170
Dan Gohman7f321562007-06-25 16:23:39 +0000171 /// SplitVectorOp - Given an operand of vector type, break it down into
172 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000173 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
174
Dan Gohman89b20c02007-06-27 14:06:22 +0000175 /// ScalarizeVectorOp - Given an operand of single-element vector type
176 /// (e.g. v1f32), convert it into the equivalent operation that returns a
177 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000178 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000179
Chris Lattner4352cc92006-04-04 17:23:26 +0000180 /// isShuffleLegal - Return true if a vector shuffle is legal with the
181 /// specified mask and type. Targets can specify exactly which masks they
182 /// support and the code generator is tasked with not creating illegal masks.
183 ///
184 /// Note that this will also return true for shuffles that are promoted to a
185 /// different type.
186 ///
187 /// If this is a legal shuffle, this method returns the (possibly promoted)
188 /// build_vector Mask. If it's not a legal shuffle, it returns null.
189 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
190
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000191 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000192 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000193
Nate Begeman750ac1b2006-02-01 07:19:44 +0000194 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
195
Chris Lattnerce872152006-03-19 06:31:19 +0000196 SDOperand CreateStackTemporary(MVT::ValueType VT);
197
Reid Spencer47857812006-12-31 05:55:36 +0000198 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000199 SDOperand &Hi);
200 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
201 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000202
Chris Lattner35481892005-12-23 00:16:34 +0000203 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000204 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000205 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000206 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
207 SDOperand LegalOp,
208 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000209 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000211 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000213
Chris Lattner456a93a2006-01-28 07:39:30 +0000214 SDOperand ExpandBSWAP(SDOperand Op);
215 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000216 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000218 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000220
Dan Gohman7f321562007-06-25 16:23:39 +0000221 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000222 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000223
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224 SDOperand getIntPtrConstant(uint64_t Val) {
225 return DAG.getConstant(Val, TLI.getPointerTy());
226 }
227};
228}
229
Chris Lattner4352cc92006-04-04 17:23:26 +0000230/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
231/// specified mask and type. Targets can specify exactly which masks they
232/// support and the code generator is tasked with not creating illegal masks.
233///
234/// Note that this will also return true for shuffles that are promoted to a
235/// different type.
236SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
237 SDOperand Mask) const {
238 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
239 default: return 0;
240 case TargetLowering::Legal:
241 case TargetLowering::Custom:
242 break;
243 case TargetLowering::Promote: {
244 // If this is promoted to a different type, convert the shuffle mask and
245 // ask if it is legal in the promoted type!
246 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
247
248 // If we changed # elements, change the shuffle mask.
249 unsigned NumEltsGrowth =
250 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
251 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
252 if (NumEltsGrowth > 1) {
253 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000254 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000255 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
256 SDOperand InOp = Mask.getOperand(i);
257 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
258 if (InOp.getOpcode() == ISD::UNDEF)
259 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
260 else {
261 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
262 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
263 }
264 }
265 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000266 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000267 }
268 VT = NVT;
269 break;
270 }
271 }
272 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
273}
274
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000275SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
276 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
277 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000278 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000280}
281
Chris Lattnere10e6f72007-06-18 21:28:10 +0000282/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
283/// contains all of a nodes operands before it contains the node.
284static void ComputeTopDownOrdering(SelectionDAG &DAG,
285 SmallVector<SDNode*, 64> &Order) {
286
287 DenseMap<SDNode*, unsigned> Visited;
288 std::vector<SDNode*> Worklist;
289 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000290
Chris Lattnere10e6f72007-06-18 21:28:10 +0000291 // Compute ordering from all of the leaves in the graphs, those (like the
292 // entry node) that have no operands.
293 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
294 E = DAG.allnodes_end(); I != E; ++I) {
295 if (I->getNumOperands() == 0) {
296 Visited[I] = 0 - 1U;
297 Worklist.push_back(I);
298 }
Chris Lattner32fca002005-10-06 01:20:27 +0000299 }
300
Chris Lattnere10e6f72007-06-18 21:28:10 +0000301 while (!Worklist.empty()) {
302 SDNode *N = Worklist.back();
303 Worklist.pop_back();
304
305 if (++Visited[N] != N->getNumOperands())
306 continue; // Haven't visited all operands yet
307
308 Order.push_back(N);
309
310 // Now that we have N in, add anything that uses it if all of their operands
311 // are now done.
312 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
313 UI != E; ++UI)
314 Worklist.push_back(*UI);
315 }
316
317 assert(Order.size() == Visited.size() &&
318 Order.size() ==
319 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
320 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000321}
322
Chris Lattner1618beb2005-07-29 00:11:56 +0000323
Chris Lattner3e928bb2005-01-07 07:47:09 +0000324void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000325 LastCALLSEQ_END = DAG.getEntryNode();
326 IsLegalizingCall = false;
327
Chris Lattnerab510a72005-10-02 17:49:46 +0000328 // The legalize process is inherently a bottom-up recursive process (users
329 // legalize their uses before themselves). Given infinite stack space, we
330 // could just start legalizing on the root and traverse the whole graph. In
331 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000332 // blocks. To avoid this problem, compute an ordering of the nodes where each
333 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000334 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000335 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000336
Chris Lattnerc7029802006-03-18 01:44:44 +0000337 for (unsigned i = 0, e = Order.size(); i != e; ++i)
338 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000339
340 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000341 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000342 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
343 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344
345 ExpandedNodes.clear();
346 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000347 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000348 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000349 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350
351 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000352 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000353}
354
Chris Lattner6831a812006-02-13 09:18:02 +0000355
356/// FindCallEndFromCallStart - Given a chained node that is part of a call
357/// sequence, find the CALLSEQ_END node that terminates the call sequence.
358static SDNode *FindCallEndFromCallStart(SDNode *Node) {
359 if (Node->getOpcode() == ISD::CALLSEQ_END)
360 return Node;
361 if (Node->use_empty())
362 return 0; // No CallSeqEnd
363
364 // The chain is usually at the end.
365 SDOperand TheChain(Node, Node->getNumValues()-1);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Sometimes it's at the beginning.
368 TheChain = SDOperand(Node, 0);
369 if (TheChain.getValueType() != MVT::Other) {
370 // Otherwise, hunt for it.
371 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
372 if (Node->getValueType(i) == MVT::Other) {
373 TheChain = SDOperand(Node, i);
374 break;
375 }
376
377 // Otherwise, we walked into a node without a chain.
378 if (TheChain.getValueType() != MVT::Other)
379 return 0;
380 }
381 }
382
383 for (SDNode::use_iterator UI = Node->use_begin(),
384 E = Node->use_end(); UI != E; ++UI) {
385
386 // Make sure to only follow users of our token chain.
387 SDNode *User = *UI;
388 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
389 if (User->getOperand(i) == TheChain)
390 if (SDNode *Result = FindCallEndFromCallStart(User))
391 return Result;
392 }
393 return 0;
394}
395
396/// FindCallStartFromCallEnd - Given a chained node that is part of a call
397/// sequence, find the CALLSEQ_START node that initiates the call sequence.
398static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
399 assert(Node && "Didn't find callseq_start for a call??");
400 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
401
402 assert(Node->getOperand(0).getValueType() == MVT::Other &&
403 "Node doesn't have a token chain argument!");
404 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
405}
406
407/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
408/// see if any uses can reach Dest. If no dest operands can get to dest,
409/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000410///
411/// Keep track of the nodes we fine that actually do lead to Dest in
412/// NodesLeadingTo. This avoids retraversing them exponential number of times.
413///
414bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000415 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000416 if (N == Dest) return true; // N certainly leads to Dest :)
417
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000418 // If we've already processed this node and it does lead to Dest, there is no
419 // need to reprocess it.
420 if (NodesLeadingTo.count(N)) return true;
421
Chris Lattner6831a812006-02-13 09:18:02 +0000422 // If the first result of this node has been already legalized, then it cannot
423 // reach N.
424 switch (getTypeAction(N->getValueType(0))) {
425 case Legal:
426 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Promote:
429 if (PromotedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 case Expand:
432 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
433 break;
434 }
435
436 // Okay, this node has not already been legalized. Check and legalize all
437 // operands. If none lead to Dest, then we can legalize this node.
438 bool OperandsLeadToDest = false;
439 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
440 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000441 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000442
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000443 if (OperandsLeadToDest) {
444 NodesLeadingTo.insert(N);
445 return true;
446 }
Chris Lattner6831a812006-02-13 09:18:02 +0000447
448 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000449 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000450 return false;
451}
452
Dan Gohman7f321562007-06-25 16:23:39 +0000453/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000454/// appropriate for its type.
455void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000456 MVT::ValueType VT = Op.getValueType();
457 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000459 case Legal: (void)LegalizeOp(Op); break;
460 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000461 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000462 if (!MVT::isVector(VT)) {
463 // If this is an illegal scalar, expand it into its two component
464 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000466 if (Op.getOpcode() == ISD::TargetConstant)
467 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000468 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000469 } else if (MVT::getVectorNumElements(VT) == 1) {
470 // If this is an illegal single element vector, convert it to a
471 // scalar operation.
472 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000473 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000474 // Otherwise, this is an illegal multiple element vector.
475 // Split it in half and legalize both parts.
476 SDOperand X, Y;
477 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000478 }
479 break;
480 }
481}
Chris Lattner6831a812006-02-13 09:18:02 +0000482
Evan Cheng9f877882006-12-13 20:57:08 +0000483/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
484/// a load from the constant pool.
485static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000486 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000487 bool Extend = false;
488
489 // If a FP immediate is precise when represented as a float and if the
490 // target can do an extending load from float to double, we put it into
491 // the constant pool as a float, even if it's is statically typed as a
492 // double.
493 MVT::ValueType VT = CFP->getValueType(0);
494 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000495 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000496 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000497 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000498 if (VT!=MVT::f64 && VT!=MVT::f32)
499 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000500 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
501 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000502 }
503
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000505 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000506 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000507 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
508 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
509 VT = MVT::f32;
510 Extend = true;
511 }
512
513 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
514 if (Extend) {
515 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
516 CPIdx, NULL, 0, MVT::f32);
517 } else {
518 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
519 }
520}
521
Chris Lattner6831a812006-02-13 09:18:02 +0000522
Evan Cheng912095b2007-01-04 21:56:39 +0000523/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
524/// operations.
525static
526SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
527 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000529 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000530 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
531 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000532 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000533
Evan Cheng912095b2007-01-04 21:56:39 +0000534 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000535 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000536 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
537 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000538 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000539 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000541 // Shift right or sign-extend it if the two operands have different types.
542 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
543 if (SizeDiff > 0) {
544 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
545 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
546 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
547 } else if (SizeDiff < 0)
548 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000549
550 // Clear the sign bit of first operand.
551 SDOperand Mask2 = (VT == MVT::f64)
552 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
553 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
554 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000555 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000556 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
557
558 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000559 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
560 return Result;
561}
562
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000563/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
564static
565SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
566 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000567 SDOperand Chain = ST->getChain();
568 SDOperand Ptr = ST->getBasePtr();
569 SDOperand Val = ST->getValue();
570 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000571 int Alignment = ST->getAlignment();
572 int SVOffset = ST->getSrcValueOffset();
573 if (MVT::isFloatingPoint(ST->getStoredVT())) {
574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
576 MVT::ValueType intVT;
577 if (VT==MVT::f64)
578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
582 assert(0 && "Unaligned load of unsupported floating point type");
583
584 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
588 assert(MVT::isInteger(ST->getStoredVT()) &&
589 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000590 // Get the half-size VT
591 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
592 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000593 int IncrementSize = NumBits / 8;
594
595 // Divide the stored value in two parts.
596 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
597 SDOperand Lo = Val;
598 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
599
600 // Store the two parts
601 SDOperand Store1, Store2;
602 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
603 ST->getSrcValue(), SVOffset, NewStoredVT,
604 ST->isVolatile(), Alignment);
605 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
607 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();
623 if (MVT::isFloatingPoint(VT)) {
624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
627 if (LoadedVT==MVT::f64)
628 intVT = MVT::i64;
629 else if (LoadedVT==MVT::f32)
630 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 }
645 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
646 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000647 int NumBits = MVT::getSizeInBits(NewLoadedVT);
648 int Alignment = LD->getAlignment();
649 int IncrementSize = NumBits / 8;
650 ISD::LoadExtType HiExtType = LD->getExtensionType();
651
652 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
653 if (HiExtType == ISD::NON_EXTLOAD)
654 HiExtType = ISD::ZEXTLOAD;
655
656 // Load the value in two parts
657 SDOperand Lo, Hi;
658 if (TLI.isLittleEndian()) {
659 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
660 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
661 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
662 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
663 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
664 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
665 Alignment);
666 } else {
667 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
668 NewLoadedVT,LD->isVolatile(), Alignment);
669 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
670 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
671 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
672 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
673 Alignment);
674 }
675
676 // aggregate the two parts
677 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
678 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
679 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
680
681 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
682 Hi.getValue(1));
683
684 SDOperand Ops[] = { Result, TF };
685 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
686}
Evan Cheng912095b2007-01-04 21:56:39 +0000687
Dan Gohman82669522007-10-11 23:57:53 +0000688/// UnrollVectorOp - We know that the given vector has a legal type, however
689/// the operation it performs is not legal and is an operation that we have
690/// no way of lowering. "Unroll" the vector, splitting out the scalars and
691/// operating on each element individually.
692SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
693 MVT::ValueType VT = Op.getValueType();
694 assert(isTypeLegal(VT) &&
695 "Caller should expand or promote operands that are not legal!");
696 assert(Op.Val->getNumValues() == 1 &&
697 "Can't unroll a vector with multiple results!");
698 unsigned NE = MVT::getVectorNumElements(VT);
699 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
700
701 SmallVector<SDOperand, 8> Scalars;
702 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
703 for (unsigned i = 0; i != NE; ++i) {
704 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
705 SDOperand Operand = Op.getOperand(j);
706 MVT::ValueType OperandVT = Operand.getValueType();
707 if (MVT::isVector(OperandVT)) {
708 // A vector operand; extract a single element.
709 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
710 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
711 OperandEltVT,
712 Operand,
713 DAG.getConstant(i, MVT::i32));
714 } else {
715 // A scalar operand; just use it as is.
716 Operands[j] = Operand;
717 }
718 }
719 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
720 &Operands[0], Operands.size()));
721 }
722
723 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
724}
725
Dan Gohmana3466152007-07-13 20:14:11 +0000726/// LegalizeOp - We know that the specified value has a legal type, and
727/// that its operands are legal. Now ensure that the operation itself
728/// is legal, recursively ensuring that the operands' operations remain
729/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000730SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000731 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
732 return Op;
733
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000734 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000735 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000736 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000737
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000739 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000740 if (Node->getNumValues() > 1) {
741 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000742 if (getTypeAction(Node->getValueType(i)) != Legal) {
743 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000744 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000745 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000746 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000747 }
748 }
749
Chris Lattner45982da2005-05-12 16:53:42 +0000750 // Note that LegalizeOp may be reentered even from single-use nodes, which
751 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000752 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000753 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000754
Nate Begeman9373a812005-08-10 20:51:12 +0000755 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000756 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000757 bool isCustom = false;
758
Chris Lattner3e928bb2005-01-07 07:47:09 +0000759 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000760 case ISD::FrameIndex:
761 case ISD::EntryToken:
762 case ISD::Register:
763 case ISD::BasicBlock:
764 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000765 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000766 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000767 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000768 case ISD::TargetConstantPool:
769 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000770 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000771 case ISD::TargetExternalSymbol:
772 case ISD::VALUETYPE:
773 case ISD::SRCVALUE:
774 case ISD::STRING:
775 case ISD::CONDCODE:
776 // Primitives must all be legal.
777 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
778 "This must be legal!");
779 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000780 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000781 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
782 // If this is a target node, legalize it by legalizing the operands then
783 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000784 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000785 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000786 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000787
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000788 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000789
790 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
791 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
792 return Result.getValue(Op.ResNo);
793 }
794 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000795#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000796 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000797#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000798 assert(0 && "Do not know how to legalize this operator!");
799 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000800 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000801 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000802 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000803 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000804 case ISD::ConstantPool:
805 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000806 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
807 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000808 case TargetLowering::Custom:
809 Tmp1 = TLI.LowerOperation(Op, DAG);
810 if (Tmp1.Val) Result = Tmp1;
811 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000812 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000813 break;
814 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000816 case ISD::FRAMEADDR:
817 case ISD::RETURNADDR:
818 // The only option for these nodes is to custom lower them. If the target
819 // does not custom lower them, then return zero.
820 Tmp1 = TLI.LowerOperation(Op, DAG);
821 if (Tmp1.Val)
822 Result = Tmp1;
823 else
824 Result = DAG.getConstant(0, TLI.getPointerTy());
825 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000826 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000827 MVT::ValueType VT = Node->getValueType(0);
828 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
829 default: assert(0 && "This action is not supported yet!");
830 case TargetLowering::Custom:
831 Result = TLI.LowerOperation(Op, DAG);
832 if (Result.Val) break;
833 // Fall Thru
834 case TargetLowering::Legal:
835 Result = DAG.getConstant(0, VT);
836 break;
837 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000838 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000839 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000840 case ISD::EXCEPTIONADDR: {
841 Tmp1 = LegalizeOp(Node->getOperand(0));
842 MVT::ValueType VT = Node->getValueType(0);
843 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
844 default: assert(0 && "This action is not supported yet!");
845 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000846 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000847 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
848 }
849 break;
850 case TargetLowering::Custom:
851 Result = TLI.LowerOperation(Op, DAG);
852 if (Result.Val) break;
853 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000854 case TargetLowering::Legal: {
855 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
856 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
857 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000858 break;
859 }
860 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000861 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000862 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000863 case ISD::EHSELECTION: {
864 Tmp1 = LegalizeOp(Node->getOperand(0));
865 Tmp2 = LegalizeOp(Node->getOperand(1));
866 MVT::ValueType VT = Node->getValueType(0);
867 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
868 default: assert(0 && "This action is not supported yet!");
869 case TargetLowering::Expand: {
870 unsigned Reg = TLI.getExceptionSelectorRegister();
871 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
872 }
873 break;
874 case TargetLowering::Custom:
875 Result = TLI.LowerOperation(Op, DAG);
876 if (Result.Val) break;
877 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000878 case TargetLowering::Legal: {
879 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
880 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
881 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000882 break;
883 }
884 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000885 }
Jim Laskey8782d482007-02-28 20:43:58 +0000886 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000887 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000888 MVT::ValueType VT = Node->getValueType(0);
889 // The only "good" option for this node is to custom lower it.
890 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
891 default: assert(0 && "This action is not supported at all!");
892 case TargetLowering::Custom:
893 Result = TLI.LowerOperation(Op, DAG);
894 if (Result.Val) break;
895 // Fall Thru
896 case TargetLowering::Legal:
897 // Target does not know, how to lower this, lower to noop
898 Result = LegalizeOp(Node->getOperand(0));
899 break;
900 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000901 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000902 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000903 case ISD::AssertSext:
904 case ISD::AssertZext:
905 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000907 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000908 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000909 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000910 Result = Node->getOperand(Op.ResNo);
911 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000912 case ISD::CopyFromReg:
913 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000914 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000915 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000917 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000918 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000919 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000920 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
922 } else {
923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
924 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000925 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
926 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000927 // Since CopyFromReg produces two values, make sure to remember that we
928 // legalized both of them.
929 AddLegalizedOperand(Op.getValue(0), Result);
930 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
931 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000932 case ISD::UNDEF: {
933 MVT::ValueType VT = Op.getValueType();
934 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000937 if (MVT::isInteger(VT))
938 Result = DAG.getConstant(0, VT);
939 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000940 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
941 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000942 else
943 assert(0 && "Unknown value type!");
944 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000945 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000946 break;
947 }
948 break;
949 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000950
Chris Lattner48b61a72006-03-28 00:40:33 +0000951 case ISD::INTRINSIC_W_CHAIN:
952 case ISD::INTRINSIC_WO_CHAIN:
953 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000954 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000955 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
956 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000957 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000958
959 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000960 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000961 TargetLowering::Custom) {
962 Tmp3 = TLI.LowerOperation(Result, DAG);
963 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000964 }
965
966 if (Result.Val->getNumValues() == 1) break;
967
968 // Must have return value and chain result.
969 assert(Result.Val->getNumValues() == 2 &&
970 "Cannot return more than two values!");
971
972 // Since loads produce two values, make sure to remember that we
973 // legalized both of them.
974 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
975 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
976 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000977 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000978
979 case ISD::LOCATION:
980 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
982
983 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
984 case TargetLowering::Promote:
985 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000986 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000987 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000988 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000989 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000990
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000991 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000992 const std::string &FName =
993 cast<StringSDNode>(Node->getOperand(3))->getValue();
994 const std::string &DirName =
995 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000996 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000997
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000998 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000999 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001000 SDOperand LineOp = Node->getOperand(1);
1001 SDOperand ColOp = Node->getOperand(2);
1002
1003 if (useDEBUG_LOC) {
1004 Ops.push_back(LineOp); // line #
1005 Ops.push_back(ColOp); // col #
1006 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001007 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001008 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001009 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1010 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001011 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001012 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001013 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001014 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001015 } else {
1016 Result = Tmp1; // chain
1017 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001018 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001019 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001020 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001021 if (Tmp1 != Node->getOperand(0) ||
1022 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001023 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001024 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001025 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1026 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1027 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1028 } else {
1029 // Otherwise promote them.
1030 Ops.push_back(PromoteOp(Node->getOperand(1)));
1031 Ops.push_back(PromoteOp(Node->getOperand(2)));
1032 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001033 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1034 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001035 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001036 }
1037 break;
1038 }
1039 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001040
1041 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001042 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001043 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001044 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001045 case TargetLowering::Legal:
1046 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1047 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1048 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1049 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001050 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001051 break;
1052 }
1053 break;
1054
Jim Laskey1ee29252007-01-26 14:34:52 +00001055 case ISD::LABEL:
1056 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1057 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001058 default: assert(0 && "This action is not supported yet!");
1059 case TargetLowering::Legal:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1061 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001063 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001064 case TargetLowering::Expand:
1065 Result = LegalizeOp(Node->getOperand(0));
1066 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001067 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001068 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001069
Scott Michelc1513d22007-08-08 23:23:31 +00001070 case ISD::Constant: {
1071 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1072 unsigned opAction =
1073 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1074
Chris Lattner3e928bb2005-01-07 07:47:09 +00001075 // We know we don't need to expand constants here, constants only have one
1076 // value and we check that it is fine above.
1077
Scott Michelc1513d22007-08-08 23:23:31 +00001078 if (opAction == TargetLowering::Custom) {
1079 Tmp1 = TLI.LowerOperation(Result, DAG);
1080 if (Tmp1.Val)
1081 Result = Tmp1;
1082 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001083 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001084 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001085 case ISD::ConstantFP: {
1086 // Spill FP immediates to the constant pool if the target cannot directly
1087 // codegen them. Targets often have some immediate values that can be
1088 // efficiently generated into an FP register without a load. We explicitly
1089 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001090 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1091
1092 // Check to see if this FP immediate is already legal.
1093 bool isLegal = false;
1094 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1095 E = TLI.legal_fpimm_end(); I != E; ++I)
1096 if (CFP->isExactlyValue(*I)) {
1097 isLegal = true;
1098 break;
1099 }
1100
Chris Lattner3181a772006-01-29 06:26:56 +00001101 // If this is a legal constant, turn it into a TargetConstantFP node.
1102 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001103 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1104 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001105 break;
1106 }
1107
1108 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Custom:
1111 Tmp3 = TLI.LowerOperation(Result, DAG);
1112 if (Tmp3.Val) {
1113 Result = Tmp3;
1114 break;
1115 }
1116 // FALLTHROUGH
1117 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001118 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001119 }
1120 break;
1121 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001122 case ISD::TokenFactor:
1123 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001124 Tmp1 = LegalizeOp(Node->getOperand(0));
1125 Tmp2 = LegalizeOp(Node->getOperand(1));
1126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1127 } else if (Node->getNumOperands() == 3) {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
1130 Tmp3 = LegalizeOp(Node->getOperand(2));
1131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001132 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001133 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001134 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1136 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001137 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001138 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001139 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001140
1141 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001142 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001143 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001144 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1145 assert(Tmp3.Val && "Target didn't custom lower this node!");
1146 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1147 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001148
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001149 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001150 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001151 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1152 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001153 if (Op.ResNo == i)
1154 Tmp2 = Tmp1;
1155 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1156 }
1157 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001158 case ISD::EXTRACT_SUBREG: {
1159 Tmp1 = LegalizeOp(Node->getOperand(0));
1160 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1161 assert(idx && "Operand must be a constant");
1162 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1164 }
1165 break;
1166 case ISD::INSERT_SUBREG: {
1167 Tmp1 = LegalizeOp(Node->getOperand(0));
1168 Tmp2 = LegalizeOp(Node->getOperand(1));
1169 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1170 assert(idx && "Operand must be a constant");
1171 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1172 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1173 }
1174 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001175 case ISD::BUILD_VECTOR:
1176 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001177 default: assert(0 && "This action is not supported yet!");
1178 case TargetLowering::Custom:
1179 Tmp3 = TLI.LowerOperation(Result, DAG);
1180 if (Tmp3.Val) {
1181 Result = Tmp3;
1182 break;
1183 }
1184 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001185 case TargetLowering::Expand:
1186 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001187 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001188 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001189 break;
1190 case ISD::INSERT_VECTOR_ELT:
1191 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1192 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1193 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1195
1196 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1197 Node->getValueType(0))) {
1198 default: assert(0 && "This action is not supported yet!");
1199 case TargetLowering::Legal:
1200 break;
1201 case TargetLowering::Custom:
1202 Tmp3 = TLI.LowerOperation(Result, DAG);
1203 if (Tmp3.Val) {
1204 Result = Tmp3;
1205 break;
1206 }
1207 // FALLTHROUGH
1208 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001209 // If the insert index is a constant, codegen this as a scalar_to_vector,
1210 // then a shuffle that inserts it into the right position in the vector.
1211 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1212 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1213 Tmp1.getValueType(), Tmp2);
1214
1215 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1216 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001217 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001218
1219 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1220 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1221 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001222 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001223 for (unsigned i = 0; i != NumElts; ++i) {
1224 if (i != InsertPos->getValue())
1225 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1226 else
1227 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1228 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001229 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1230 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001231
1232 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1233 Tmp1, ScVec, ShufMask);
1234 Result = LegalizeOp(Result);
1235 break;
1236 }
1237
Chris Lattner2332b9f2006-03-19 01:17:20 +00001238 // If the target doesn't support this, we have to spill the input vector
1239 // to a temporary stack slot, update the element, then reload it. This is
1240 // badness. We could also load the value into a vector register (either
1241 // with a "move to register" or "extload into register" instruction, then
1242 // permute it into place, if the idx is a constant and if the idx is
1243 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001244 MVT::ValueType VT = Tmp1.getValueType();
1245 MVT::ValueType EltVT = Tmp2.getValueType();
1246 MVT::ValueType IdxVT = Tmp3.getValueType();
1247 MVT::ValueType PtrVT = TLI.getPointerTy();
1248 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001249 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001250 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001251
1252 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001253 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1254 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001255 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001256 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1257 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1258 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001259 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001260 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001261 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001262 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001263 break;
1264 }
1265 }
1266 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001267 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001268 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1269 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1270 break;
1271 }
1272
Chris Lattnerce872152006-03-19 06:31:19 +00001273 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1274 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1275 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1276 Node->getValueType(0))) {
1277 default: assert(0 && "This action is not supported yet!");
1278 case TargetLowering::Legal:
1279 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001280 case TargetLowering::Custom:
1281 Tmp3 = TLI.LowerOperation(Result, DAG);
1282 if (Tmp3.Val) {
1283 Result = Tmp3;
1284 break;
1285 }
1286 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001287 case TargetLowering::Expand:
1288 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001289 break;
1290 }
Chris Lattnerce872152006-03-19 06:31:19 +00001291 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001292 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1294 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1295 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1296
1297 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001298 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1299 default: assert(0 && "Unknown operation action!");
1300 case TargetLowering::Legal:
1301 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1302 "vector shuffle should not be created if not legal!");
1303 break;
1304 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001305 Tmp3 = TLI.LowerOperation(Result, DAG);
1306 if (Tmp3.Val) {
1307 Result = Tmp3;
1308 break;
1309 }
1310 // FALLTHROUGH
1311 case TargetLowering::Expand: {
1312 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001313 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001314 MVT::ValueType PtrVT = TLI.getPointerTy();
1315 SDOperand Mask = Node->getOperand(2);
1316 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001317 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001318 for (unsigned i = 0; i != NumElems; ++i) {
1319 SDOperand Arg = Mask.getOperand(i);
1320 if (Arg.getOpcode() == ISD::UNDEF) {
1321 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1322 } else {
1323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1324 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1325 if (Idx < NumElems)
1326 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1327 DAG.getConstant(Idx, PtrVT)));
1328 else
1329 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1330 DAG.getConstant(Idx - NumElems, PtrVT)));
1331 }
1332 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001333 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001334 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001335 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001336 case TargetLowering::Promote: {
1337 // Change base type to a different vector type.
1338 MVT::ValueType OVT = Node->getValueType(0);
1339 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1340
1341 // Cast the two input vectors.
1342 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1343 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1344
1345 // Convert the shuffle mask to the right # elements.
1346 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1347 assert(Tmp3.Val && "Shuffle not legal?");
1348 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1349 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1350 break;
1351 }
Chris Lattner87100e02006-03-20 01:52:29 +00001352 }
1353 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001354
1355 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001356 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001357 Tmp2 = LegalizeOp(Node->getOperand(1));
1358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001359 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001360 break;
1361
Dan Gohman7f321562007-06-25 16:23:39 +00001362 case ISD::EXTRACT_SUBVECTOR:
1363 Tmp1 = Node->getOperand(0);
1364 Tmp2 = LegalizeOp(Node->getOperand(1));
1365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1366 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001367 break;
1368
Chris Lattner6831a812006-02-13 09:18:02 +00001369 case ISD::CALLSEQ_START: {
1370 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1371
1372 // Recursively Legalize all of the inputs of the call end that do not lead
1373 // to this call start. This ensures that any libcalls that need be inserted
1374 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001375 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001376 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001377 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1378 NodesLeadingTo);
1379 }
Chris Lattner6831a812006-02-13 09:18:02 +00001380
1381 // Now that we legalized all of the inputs (which may have inserted
1382 // libcalls) create the new CALLSEQ_START node.
1383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1384
1385 // Merge in the last call, to ensure that this call start after the last
1386 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001387 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001388 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1389 Tmp1 = LegalizeOp(Tmp1);
1390 }
Chris Lattner6831a812006-02-13 09:18:02 +00001391
1392 // Do not try to legalize the target-specific arguments (#1+).
1393 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001394 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001395 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001396 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001397 }
1398
1399 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001400 AddLegalizedOperand(Op.getValue(0), Result);
1401 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1402 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1403
Chris Lattner6831a812006-02-13 09:18:02 +00001404 // Now that the callseq_start and all of the non-call nodes above this call
1405 // sequence have been legalized, legalize the call itself. During this
1406 // process, no libcalls can/will be inserted, guaranteeing that no calls
1407 // can overlap.
1408 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1409 SDOperand InCallSEQ = LastCALLSEQ_END;
1410 // Note that we are selecting this call!
1411 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1412 IsLegalizingCall = true;
1413
1414 // Legalize the call, starting from the CALLSEQ_END.
1415 LegalizeOp(LastCALLSEQ_END);
1416 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1417 return Result;
1418 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001419 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001420 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1421 // will cause this node to be legalized as well as handling libcalls right.
1422 if (LastCALLSEQ_END.Val != Node) {
1423 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001424 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001425 assert(I != LegalizedNodes.end() &&
1426 "Legalizing the call start should have legalized this node!");
1427 return I->second;
1428 }
1429
1430 // Otherwise, the call start has been legalized and everything is going
1431 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001433 // Do not try to legalize the target-specific arguments (#1+), except for
1434 // an optional flag input.
1435 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1436 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001437 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001438 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001439 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001440 }
1441 } else {
1442 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1443 if (Tmp1 != Node->getOperand(0) ||
1444 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001445 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001446 Ops[0] = Tmp1;
1447 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001448 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001449 }
Chris Lattner6a542892006-01-24 05:48:21 +00001450 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001451 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001452 // This finishes up call legalization.
1453 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001454
1455 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1456 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1457 if (Node->getNumValues() == 2)
1458 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1459 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001460 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001461 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1463 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1464 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001466
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001467 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001468 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001469 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001470 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001471 case TargetLowering::Expand: {
1472 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1473 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1474 " not tell us which reg is the stack pointer!");
1475 SDOperand Chain = Tmp1.getOperand(0);
1476 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001477 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1478 Chain = SP.getValue(1);
1479 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1480 unsigned StackAlign =
1481 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1482 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001483 SP = DAG.getNode(ISD::AND, VT, SP,
1484 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001485 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1486 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001487 Tmp1 = LegalizeOp(Tmp1);
1488 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001489 break;
1490 }
1491 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001492 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1493 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001494 Tmp1 = LegalizeOp(Tmp3);
1495 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001496 }
Chris Lattner903d2782006-01-15 08:54:32 +00001497 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001498 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001499 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001500 }
Chris Lattner903d2782006-01-15 08:54:32 +00001501 // Since this op produce two values, make sure to remember that we
1502 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001503 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1504 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001505 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001506 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001507 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001508 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001509 bool Changed = false;
1510 // Legalize all of the operands of the inline asm, in case they are nodes
1511 // that need to be expanded or something. Note we skip the asm string and
1512 // all of the TargetConstant flags.
1513 SDOperand Op = LegalizeOp(Ops[0]);
1514 Changed = Op != Ops[0];
1515 Ops[0] = Op;
1516
1517 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1518 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1519 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1520 for (++i; NumVals; ++i, --NumVals) {
1521 SDOperand Op = LegalizeOp(Ops[i]);
1522 if (Op != Ops[i]) {
1523 Changed = true;
1524 Ops[i] = Op;
1525 }
1526 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001527 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001528
1529 if (HasInFlag) {
1530 Op = LegalizeOp(Ops.back());
1531 Changed |= Op != Ops.back();
1532 Ops.back() = Op;
1533 }
1534
1535 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001536 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001537
1538 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001539 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001540 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1541 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001542 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001543 case ISD::BR:
1544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001545 // Ensure that libcalls are emitted before a branch.
1546 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1547 Tmp1 = LegalizeOp(Tmp1);
1548 LastCALLSEQ_END = DAG.getEntryNode();
1549
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001550 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001551 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001552 case ISD::BRIND:
1553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1554 // Ensure that libcalls are emitted before a branch.
1555 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1556 Tmp1 = LegalizeOp(Tmp1);
1557 LastCALLSEQ_END = DAG.getEntryNode();
1558
1559 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1560 default: assert(0 && "Indirect target must be legal type (pointer)!");
1561 case Legal:
1562 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1563 break;
1564 }
1565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1566 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001567 case ISD::BR_JT:
1568 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1569 // Ensure that libcalls are emitted before a branch.
1570 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1571 Tmp1 = LegalizeOp(Tmp1);
1572 LastCALLSEQ_END = DAG.getEntryNode();
1573
1574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1576
1577 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1578 default: assert(0 && "This action is not supported yet!");
1579 case TargetLowering::Legal: break;
1580 case TargetLowering::Custom:
1581 Tmp1 = TLI.LowerOperation(Result, DAG);
1582 if (Tmp1.Val) Result = Tmp1;
1583 break;
1584 case TargetLowering::Expand: {
1585 SDOperand Chain = Result.getOperand(0);
1586 SDOperand Table = Result.getOperand(1);
1587 SDOperand Index = Result.getOperand(2);
1588
1589 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001590 MachineFunction &MF = DAG.getMachineFunction();
1591 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001592 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1593 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001594
1595 SDOperand LD;
1596 switch (EntrySize) {
1597 default: assert(0 && "Size of jump table not supported yet."); break;
1598 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1599 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1600 }
1601
1602 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001603 // For PIC, the sequence is:
1604 // BRIND(load(Jumptable + index) + RelocBase)
1605 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1606 SDOperand Reloc;
1607 if (TLI.usesGlobalOffsetTable())
1608 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1609 else
1610 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001611 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001612 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1613 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1614 } else {
1615 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1616 }
1617 }
1618 }
1619 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001620 case ISD::BRCOND:
1621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001622 // Ensure that libcalls are emitted before a return.
1623 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1624 Tmp1 = LegalizeOp(Tmp1);
1625 LastCALLSEQ_END = DAG.getEntryNode();
1626
Chris Lattner47e92232005-01-18 19:27:06 +00001627 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1628 case Expand: assert(0 && "It's impossible to expand bools");
1629 case Legal:
1630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1631 break;
1632 case Promote:
1633 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001634
1635 // The top bits of the promoted condition are not necessarily zero, ensure
1636 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001638 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1639 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001640 break;
1641 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001642
1643 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001645
1646 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1647 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001648 case TargetLowering::Legal: break;
1649 case TargetLowering::Custom:
1650 Tmp1 = TLI.LowerOperation(Result, DAG);
1651 if (Tmp1.Val) Result = Tmp1;
1652 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001653 case TargetLowering::Expand:
1654 // Expand brcond's setcc into its constituent parts and create a BR_CC
1655 // Node.
1656 if (Tmp2.getOpcode() == ISD::SETCC) {
1657 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1658 Tmp2.getOperand(0), Tmp2.getOperand(1),
1659 Node->getOperand(2));
1660 } else {
1661 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1662 DAG.getCondCode(ISD::SETNE), Tmp2,
1663 DAG.getConstant(0, Tmp2.getValueType()),
1664 Node->getOperand(2));
1665 }
1666 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001667 }
1668 break;
1669 case ISD::BR_CC:
1670 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001671 // Ensure that libcalls are emitted before a branch.
1672 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1673 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001674 Tmp2 = Node->getOperand(2); // LHS
1675 Tmp3 = Node->getOperand(3); // RHS
1676 Tmp4 = Node->getOperand(1); // CC
1677
1678 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001679 LastCALLSEQ_END = DAG.getEntryNode();
1680
Nate Begeman750ac1b2006-02-01 07:19:44 +00001681 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1682 // the LHS is a legal SETCC itself. In this case, we need to compare
1683 // the result against zero to select between true and false values.
1684 if (Tmp3.Val == 0) {
1685 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1686 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001687 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001688
1689 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1690 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001691
Chris Lattner181b7a32005-12-17 23:46:46 +00001692 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1693 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001694 case TargetLowering::Legal: break;
1695 case TargetLowering::Custom:
1696 Tmp4 = TLI.LowerOperation(Result, DAG);
1697 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001698 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001699 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001700 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001701 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001702 LoadSDNode *LD = cast<LoadSDNode>(Node);
1703 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1704 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001705
Evan Cheng466685d2006-10-09 20:57:25 +00001706 ISD::LoadExtType ExtType = LD->getExtensionType();
1707 if (ExtType == ISD::NON_EXTLOAD) {
1708 MVT::ValueType VT = Node->getValueType(0);
1709 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1710 Tmp3 = Result.getValue(0);
1711 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001712
Evan Cheng466685d2006-10-09 20:57:25 +00001713 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1714 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001715 case TargetLowering::Legal:
1716 // If this is an unaligned load and the target doesn't support it,
1717 // expand it.
1718 if (!TLI.allowsUnalignedMemoryAccesses()) {
1719 unsigned ABIAlignment = TLI.getTargetData()->
1720 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1721 if (LD->getAlignment() < ABIAlignment){
1722 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1723 TLI);
1724 Tmp3 = Result.getOperand(0);
1725 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001726 Tmp3 = LegalizeOp(Tmp3);
1727 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001728 }
1729 }
1730 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001731 case TargetLowering::Custom:
1732 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1733 if (Tmp1.Val) {
1734 Tmp3 = LegalizeOp(Tmp1);
1735 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001736 }
Evan Cheng466685d2006-10-09 20:57:25 +00001737 break;
1738 case TargetLowering::Promote: {
1739 // Only promote a load of vector type to another.
1740 assert(MVT::isVector(VT) && "Cannot promote this load!");
1741 // Change base type to a different vector type.
1742 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1743
1744 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001745 LD->getSrcValueOffset(),
1746 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001747 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1748 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001749 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001750 }
Evan Cheng466685d2006-10-09 20:57:25 +00001751 }
1752 // Since loads produce two values, make sure to remember that we
1753 // legalized both of them.
1754 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1755 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1756 return Op.ResNo ? Tmp4 : Tmp3;
1757 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001758 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001759 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1760 default: assert(0 && "This action is not supported yet!");
1761 case TargetLowering::Promote:
1762 assert(SrcVT == MVT::i1 &&
1763 "Can only promote extending LOAD from i1 -> i8!");
1764 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1765 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001766 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001767 Tmp1 = Result.getValue(0);
1768 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001769 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001770 case TargetLowering::Custom:
1771 isCustom = true;
1772 // FALLTHROUGH
1773 case TargetLowering::Legal:
1774 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1775 Tmp1 = Result.getValue(0);
1776 Tmp2 = Result.getValue(1);
1777
1778 if (isCustom) {
1779 Tmp3 = TLI.LowerOperation(Result, DAG);
1780 if (Tmp3.Val) {
1781 Tmp1 = LegalizeOp(Tmp3);
1782 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1783 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001784 } else {
1785 // If this is an unaligned load and the target doesn't support it,
1786 // expand it.
1787 if (!TLI.allowsUnalignedMemoryAccesses()) {
1788 unsigned ABIAlignment = TLI.getTargetData()->
1789 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1790 if (LD->getAlignment() < ABIAlignment){
1791 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1792 TLI);
1793 Tmp1 = Result.getOperand(0);
1794 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001795 Tmp1 = LegalizeOp(Tmp1);
1796 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001797 }
1798 }
Evan Cheng466685d2006-10-09 20:57:25 +00001799 }
1800 break;
1801 case TargetLowering::Expand:
1802 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1803 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1804 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001805 LD->getSrcValueOffset(),
1806 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001807 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1808 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1809 Tmp2 = LegalizeOp(Load.getValue(1));
1810 break;
1811 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001812 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001813 // Turn the unsupported load into an EXTLOAD followed by an explicit
1814 // zero/sign extend inreg.
1815 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1816 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001817 LD->getSrcValueOffset(), SrcVT,
1818 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001819 SDOperand ValRes;
1820 if (ExtType == ISD::SEXTLOAD)
1821 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1822 Result, DAG.getValueType(SrcVT));
1823 else
1824 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1825 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1826 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1827 break;
1828 }
1829 // Since loads produce two values, make sure to remember that we legalized
1830 // both of them.
1831 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1832 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1833 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001834 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001835 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001836 case ISD::EXTRACT_ELEMENT: {
1837 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1838 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001839 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001840 case Legal:
1841 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1842 // 1 -> Hi
1843 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1844 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1845 TLI.getShiftAmountTy()));
1846 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1847 } else {
1848 // 0 -> Lo
1849 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1850 Node->getOperand(0));
1851 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001852 break;
1853 case Expand:
1854 // Get both the low and high parts.
1855 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1856 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1857 Result = Tmp2; // 1 -> Hi
1858 else
1859 Result = Tmp1; // 0 -> Lo
1860 break;
1861 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001862 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001863 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001864
1865 case ISD::CopyToReg:
1866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001867
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001868 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001869 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001870 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001871 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001872 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001874 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001875 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001876 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001877 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1879 Tmp3);
1880 } else {
1881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001882 }
1883
1884 // Since this produces two values, make sure to remember that we legalized
1885 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001886 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001887 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001888 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001889 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001890 break;
1891
1892 case ISD::RET:
1893 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001894
1895 // Ensure that libcalls are emitted before a return.
1896 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1897 Tmp1 = LegalizeOp(Tmp1);
1898 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001899
Chris Lattner3e928bb2005-01-07 07:47:09 +00001900 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001901 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001902 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001903 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001904 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001905 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001906 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001907 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001908 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001909 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001910 SDOperand Lo, Hi;
1911 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001912
1913 // Big endian systems want the hi reg first.
1914 if (!TLI.isLittleEndian())
1915 std::swap(Lo, Hi);
1916
Evan Cheng13acce32006-12-11 19:27:14 +00001917 if (Hi.Val)
1918 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1919 else
1920 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001921 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001922 } else {
1923 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001924 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1925 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001926
Dan Gohman7f321562007-06-25 16:23:39 +00001927 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001928 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001929 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001930 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001931 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001932 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001933 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001934 } else if (NumElems == 1) {
1935 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001936 Tmp2 = ScalarizeVectorOp(Tmp2);
1937 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001938 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001939
1940 // FIXME: Returns of gcc generic vectors smaller than a legal type
1941 // should be returned in integer registers!
1942
Chris Lattnerf87324e2006-04-11 01:31:51 +00001943 // The scalarized value type may not be legal, e.g. it might require
1944 // promotion or expansion. Relegalize the return.
1945 Result = LegalizeOp(Result);
1946 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001947 // FIXME: Returns of gcc generic vectors larger than a legal vector
1948 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001949 SDOperand Lo, Hi;
1950 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001951 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001952 Result = LegalizeOp(Result);
1953 }
1954 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001955 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001956 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001957 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001958 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001959 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001960 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001961 }
1962 break;
1963 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001964 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001965 break;
1966 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001967 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001968 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001969 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001970 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1971 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001972 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001973 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001974 break;
1975 case Expand: {
1976 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001977 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001978 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001979 ExpandOp(Node->getOperand(i), Lo, Hi);
1980 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001981 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001982 if (Hi.Val) {
1983 NewValues.push_back(Hi);
1984 NewValues.push_back(Node->getOperand(i+1));
1985 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001986 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001987 }
1988 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001989 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001990 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001991
1992 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001993 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001994 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001995 Result = DAG.getNode(ISD::RET, MVT::Other,
1996 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001997 break;
1998 }
1999 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002000
Chris Lattner6862dbc2006-01-29 21:02:23 +00002001 if (Result.getOpcode() == ISD::RET) {
2002 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2003 default: assert(0 && "This action is not supported yet!");
2004 case TargetLowering::Legal: break;
2005 case TargetLowering::Custom:
2006 Tmp1 = TLI.LowerOperation(Result, DAG);
2007 if (Tmp1.Val) Result = Tmp1;
2008 break;
2009 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002010 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002011 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002012 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002013 StoreSDNode *ST = cast<StoreSDNode>(Node);
2014 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2015 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002016 int SVOffset = ST->getSrcValueOffset();
2017 unsigned Alignment = ST->getAlignment();
2018 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002019
Evan Cheng8b2794a2006-10-13 21:14:26 +00002020 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002021 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2022 // FIXME: We shouldn't do this for TargetConstantFP's.
2023 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2024 // to phase ordering between legalized code and the dag combiner. This
2025 // probably means that we need to integrate dag combiner and legalizer
2026 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002027 // We generally can't do this one for long doubles.
2028 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002029 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002030 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2031 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002032 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002033 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2034 SVOffset, isVolatile, Alignment);
2035 break;
2036 } else if (CFP->getValueType(0) == MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002037 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2038 getZExtValue(), MVT::i64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002039 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2040 SVOffset, isVolatile, Alignment);
2041 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002042 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002043 }
2044
Evan Cheng8b2794a2006-10-13 21:14:26 +00002045 switch (getTypeAction(ST->getStoredVT())) {
2046 case Legal: {
2047 Tmp3 = LegalizeOp(ST->getValue());
2048 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2049 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002050
Evan Cheng8b2794a2006-10-13 21:14:26 +00002051 MVT::ValueType VT = Tmp3.getValueType();
2052 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2053 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002054 case TargetLowering::Legal:
2055 // If this is an unaligned store and the target doesn't support it,
2056 // expand it.
2057 if (!TLI.allowsUnalignedMemoryAccesses()) {
2058 unsigned ABIAlignment = TLI.getTargetData()->
2059 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2060 if (ST->getAlignment() < ABIAlignment)
2061 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2062 TLI);
2063 }
2064 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002065 case TargetLowering::Custom:
2066 Tmp1 = TLI.LowerOperation(Result, DAG);
2067 if (Tmp1.Val) Result = Tmp1;
2068 break;
2069 case TargetLowering::Promote:
2070 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2071 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2072 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2073 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002074 ST->getSrcValue(), SVOffset, isVolatile,
2075 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002076 break;
2077 }
2078 break;
2079 }
2080 case Promote:
2081 // Truncate the value and store the result.
2082 Tmp3 = PromoteOp(ST->getValue());
2083 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002084 SVOffset, ST->getStoredVT(),
2085 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002086 break;
2087
2088 case Expand:
2089 unsigned IncrementSize = 0;
2090 SDOperand Lo, Hi;
2091
2092 // If this is a vector type, then we have to calculate the increment as
2093 // the product of the element size in bytes, and the number of elements
2094 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002095 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002096 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00002097 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2098 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002099
Dan Gohman7f321562007-06-25 16:23:39 +00002100 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002101 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002102 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002103 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002104 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002105 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002106 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002107 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002108 Result = LegalizeOp(Result);
2109 break;
2110 } else if (NumElems == 1) {
2111 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002112 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002113 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002114 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002115 // The scalarized value type may not be legal, e.g. it might require
2116 // promotion or expansion. Relegalize the scalar store.
2117 Result = LegalizeOp(Result);
2118 break;
2119 } else {
2120 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2121 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2122 }
2123 } else {
2124 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002125 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002126
2127 if (!TLI.isLittleEndian())
2128 std::swap(Lo, Hi);
2129 }
2130
2131 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002132 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002133
2134 if (Hi.Val == NULL) {
2135 // Must be int <-> float one-to-one expansion.
2136 Result = Lo;
2137 break;
2138 }
2139
Evan Cheng8b2794a2006-10-13 21:14:26 +00002140 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2141 getIntPtrConstant(IncrementSize));
2142 assert(isTypeLegal(Tmp2.getValueType()) &&
2143 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002144 SVOffset += IncrementSize;
2145 if (Alignment > IncrementSize)
2146 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002147 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002148 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002149 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2150 break;
2151 }
2152 } else {
2153 // Truncating store
2154 assert(isTypeLegal(ST->getValue().getValueType()) &&
2155 "Cannot handle illegal TRUNCSTORE yet!");
2156 Tmp3 = LegalizeOp(ST->getValue());
2157
2158 // The only promote case we handle is TRUNCSTORE:i1 X into
2159 // -> TRUNCSTORE:i8 (and X, 1)
2160 if (ST->getStoredVT() == MVT::i1 &&
2161 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2162 // Promote the bool to a mask then store.
2163 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2164 DAG.getConstant(1, Tmp3.getValueType()));
2165 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002166 SVOffset, MVT::i8,
2167 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002168 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2169 Tmp2 != ST->getBasePtr()) {
2170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2171 ST->getOffset());
2172 }
2173
2174 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2175 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002176 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002177 case TargetLowering::Legal:
2178 // If this is an unaligned store and the target doesn't support it,
2179 // expand it.
2180 if (!TLI.allowsUnalignedMemoryAccesses()) {
2181 unsigned ABIAlignment = TLI.getTargetData()->
2182 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2183 if (ST->getAlignment() < ABIAlignment)
2184 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2185 TLI);
2186 }
2187 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002188 case TargetLowering::Custom:
2189 Tmp1 = TLI.LowerOperation(Result, DAG);
2190 if (Tmp1.Val) Result = Tmp1;
2191 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002192 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002193 }
2194 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002195 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002196 case ISD::PCMARKER:
2197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002199 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002200 case ISD::STACKSAVE:
2201 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002202 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2203 Tmp1 = Result.getValue(0);
2204 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002205
Chris Lattner140d53c2006-01-13 02:50:02 +00002206 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2207 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002208 case TargetLowering::Legal: break;
2209 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002210 Tmp3 = TLI.LowerOperation(Result, DAG);
2211 if (Tmp3.Val) {
2212 Tmp1 = LegalizeOp(Tmp3);
2213 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002214 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002215 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002216 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002217 // Expand to CopyFromReg if the target set
2218 // StackPointerRegisterToSaveRestore.
2219 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002220 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002221 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002222 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002223 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002224 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2225 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002226 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002227 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002228 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002229
2230 // Since stacksave produce two values, make sure to remember that we
2231 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002232 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2233 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2234 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002235
Chris Lattner140d53c2006-01-13 02:50:02 +00002236 case ISD::STACKRESTORE:
2237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2238 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002240
2241 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2242 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002243 case TargetLowering::Legal: break;
2244 case TargetLowering::Custom:
2245 Tmp1 = TLI.LowerOperation(Result, DAG);
2246 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002247 break;
2248 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002249 // Expand to CopyToReg if the target set
2250 // StackPointerRegisterToSaveRestore.
2251 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2252 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2253 } else {
2254 Result = Tmp1;
2255 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002256 break;
2257 }
2258 break;
2259
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002260 case ISD::READCYCLECOUNTER:
2261 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002262 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002263 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2264 Node->getValueType(0))) {
2265 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002266 case TargetLowering::Legal:
2267 Tmp1 = Result.getValue(0);
2268 Tmp2 = Result.getValue(1);
2269 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002270 case TargetLowering::Custom:
2271 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002272 Tmp1 = LegalizeOp(Result.getValue(0));
2273 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002274 break;
2275 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002276
2277 // Since rdcc produce two values, make sure to remember that we legalized
2278 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002279 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2280 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002281 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002282
Chris Lattner2ee743f2005-01-14 22:08:15 +00002283 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002284 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2285 case Expand: assert(0 && "It's impossible to expand bools");
2286 case Legal:
2287 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2288 break;
2289 case Promote:
2290 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002291 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002292 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002293 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2294 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002295 break;
2296 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002297 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002298 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002299
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002300 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002301
Nate Begemanb942a3d2005-08-23 04:29:48 +00002302 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002303 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002304 case TargetLowering::Legal: break;
2305 case TargetLowering::Custom: {
2306 Tmp1 = TLI.LowerOperation(Result, DAG);
2307 if (Tmp1.Val) Result = Tmp1;
2308 break;
2309 }
Nate Begeman9373a812005-08-10 20:51:12 +00002310 case TargetLowering::Expand:
2311 if (Tmp1.getOpcode() == ISD::SETCC) {
2312 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2313 Tmp2, Tmp3,
2314 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2315 } else {
2316 Result = DAG.getSelectCC(Tmp1,
2317 DAG.getConstant(0, Tmp1.getValueType()),
2318 Tmp2, Tmp3, ISD::SETNE);
2319 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002320 break;
2321 case TargetLowering::Promote: {
2322 MVT::ValueType NVT =
2323 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2324 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002325 if (MVT::isVector(Tmp2.getValueType())) {
2326 ExtOp = ISD::BIT_CONVERT;
2327 TruncOp = ISD::BIT_CONVERT;
2328 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002329 ExtOp = ISD::ANY_EXTEND;
2330 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002331 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002332 ExtOp = ISD::FP_EXTEND;
2333 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002334 }
2335 // Promote each of the values to the new type.
2336 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2337 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2338 // Perform the larger operation, then round down.
2339 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2340 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2341 break;
2342 }
2343 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002344 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002345 case ISD::SELECT_CC: {
2346 Tmp1 = Node->getOperand(0); // LHS
2347 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002348 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2349 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002350 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002351
Nate Begeman750ac1b2006-02-01 07:19:44 +00002352 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2353
2354 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2355 // the LHS is a legal SETCC itself. In this case, we need to compare
2356 // the result against zero to select between true and false values.
2357 if (Tmp2.Val == 0) {
2358 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2359 CC = DAG.getCondCode(ISD::SETNE);
2360 }
2361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2362
2363 // Everything is legal, see if we should expand this op or something.
2364 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2365 default: assert(0 && "This action is not supported yet!");
2366 case TargetLowering::Legal: break;
2367 case TargetLowering::Custom:
2368 Tmp1 = TLI.LowerOperation(Result, DAG);
2369 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002370 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002371 }
2372 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002373 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002374 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002375 Tmp1 = Node->getOperand(0);
2376 Tmp2 = Node->getOperand(1);
2377 Tmp3 = Node->getOperand(2);
2378 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2379
2380 // If we had to Expand the SetCC operands into a SELECT node, then it may
2381 // not always be possible to return a true LHS & RHS. In this case, just
2382 // return the value we legalized, returned in the LHS
2383 if (Tmp2.Val == 0) {
2384 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002385 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002386 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002387
Chris Lattner73e142f2006-01-30 22:43:50 +00002388 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002389 default: assert(0 && "Cannot handle this action for SETCC yet!");
2390 case TargetLowering::Custom:
2391 isCustom = true;
2392 // FALLTHROUGH.
2393 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002394 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002395 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002396 Tmp4 = TLI.LowerOperation(Result, DAG);
2397 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002398 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002399 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002400 case TargetLowering::Promote: {
2401 // First step, figure out the appropriate operation to use.
2402 // Allow SETCC to not be supported for all legal data types
2403 // Mostly this targets FP
2404 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002405 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002406
2407 // Scan for the appropriate larger type to use.
2408 while (1) {
2409 NewInTy = (MVT::ValueType)(NewInTy+1);
2410
2411 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2412 "Fell off of the edge of the integer world");
2413 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2414 "Fell off of the edge of the floating point world");
2415
2416 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002417 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002418 break;
2419 }
2420 if (MVT::isInteger(NewInTy))
2421 assert(0 && "Cannot promote Legal Integer SETCC yet");
2422 else {
2423 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2424 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2425 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002426 Tmp1 = LegalizeOp(Tmp1);
2427 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002429 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002430 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002431 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002432 case TargetLowering::Expand:
2433 // Expand a setcc node into a select_cc of the same condition, lhs, and
2434 // rhs that selects between const 1 (true) and const 0 (false).
2435 MVT::ValueType VT = Node->getValueType(0);
2436 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2437 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002438 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002439 break;
2440 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002441 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002442 case ISD::MEMSET:
2443 case ISD::MEMCPY:
2444 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002445 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002446 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2447
2448 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2449 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2450 case Expand: assert(0 && "Cannot expand a byte!");
2451 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002452 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002453 break;
2454 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002455 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002456 break;
2457 }
2458 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002459 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002460 }
Chris Lattner272455b2005-02-02 03:44:41 +00002461
2462 SDOperand Tmp4;
2463 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002464 case Expand: {
2465 // Length is too big, just take the lo-part of the length.
2466 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002467 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002468 break;
2469 }
Chris Lattnere5605212005-01-28 22:29:18 +00002470 case Legal:
2471 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002472 break;
2473 case Promote:
2474 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002475 break;
2476 }
2477
2478 SDOperand Tmp5;
2479 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2480 case Expand: assert(0 && "Cannot expand this yet!");
2481 case Legal:
2482 Tmp5 = LegalizeOp(Node->getOperand(4));
2483 break;
2484 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002485 Tmp5 = PromoteOp(Node->getOperand(4));
2486 break;
2487 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002488
2489 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2490 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002491 case TargetLowering::Custom:
2492 isCustom = true;
2493 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002494 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002495 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002496 if (isCustom) {
2497 Tmp1 = TLI.LowerOperation(Result, DAG);
2498 if (Tmp1.Val) Result = Tmp1;
2499 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002500 break;
2501 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002502 // Otherwise, the target does not support this operation. Lower the
2503 // operation to an explicit libcall as appropriate.
2504 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002505 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002506 TargetLowering::ArgListTy Args;
2507 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002508
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002509 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002510 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002511 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002512 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002513 // Extend the (previously legalized) ubyte argument to be an int value
2514 // for the call.
2515 if (Tmp3.getValueType() > MVT::i32)
2516 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2517 else
2518 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002519 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002520 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002521 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002522 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002523
2524 FnName = "memset";
2525 } else if (Node->getOpcode() == ISD::MEMCPY ||
2526 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002527 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002528 Entry.Node = Tmp2; Args.push_back(Entry);
2529 Entry.Node = Tmp3; Args.push_back(Entry);
2530 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002531 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2532 } else {
2533 assert(0 && "Unknown op!");
2534 }
Chris Lattner45982da2005-05-12 16:53:42 +00002535
Chris Lattnere1bd8222005-01-11 05:57:22 +00002536 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002537 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002538 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002539 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002540 break;
2541 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002542 }
2543 break;
2544 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002545
Chris Lattner5b359c62005-04-02 04:00:59 +00002546 case ISD::SHL_PARTS:
2547 case ISD::SRA_PARTS:
2548 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002549 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002550 bool Changed = false;
2551 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2552 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2553 Changed |= Ops.back() != Node->getOperand(i);
2554 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002555 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002556 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002557
Evan Cheng05a2d562006-01-09 18:31:59 +00002558 switch (TLI.getOperationAction(Node->getOpcode(),
2559 Node->getValueType(0))) {
2560 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002561 case TargetLowering::Legal: break;
2562 case TargetLowering::Custom:
2563 Tmp1 = TLI.LowerOperation(Result, DAG);
2564 if (Tmp1.Val) {
2565 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002566 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002567 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002568 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2569 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002570 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002571 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002572 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002573 return RetVal;
2574 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002575 break;
2576 }
2577
Chris Lattner2c8086f2005-04-02 05:00:07 +00002578 // Since these produce multiple values, make sure to remember that we
2579 // legalized all of them.
2580 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2581 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2582 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002583 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002584
2585 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002586 case ISD::ADD:
2587 case ISD::SUB:
2588 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002589 case ISD::MULHS:
2590 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002591 case ISD::UDIV:
2592 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002593 case ISD::AND:
2594 case ISD::OR:
2595 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002596 case ISD::SHL:
2597 case ISD::SRL:
2598 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002599 case ISD::FADD:
2600 case ISD::FSUB:
2601 case ISD::FMUL:
2602 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002603 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002604 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002605 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2606 case Expand: assert(0 && "Not possible");
2607 case Legal:
2608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2609 break;
2610 case Promote:
2611 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2612 break;
2613 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002614
2615 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002616
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002617 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002618 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002619 case TargetLowering::Legal: break;
2620 case TargetLowering::Custom:
2621 Tmp1 = TLI.LowerOperation(Result, DAG);
2622 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002623 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002624 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002625 MVT::ValueType VT = Op.getValueType();
2626
2627 // See if multiply or divide can be lowered using two-result operations.
2628 SDVTList VTs = DAG.getVTList(VT, VT);
2629 if (Node->getOpcode() == ISD::MUL) {
2630 // We just need the low half of the multiply; try both the signed
2631 // and unsigned forms. If the target supports both SMUL_LOHI and
2632 // UMUL_LOHI, form a preference by checking which forms of plain
2633 // MULH it supports.
2634 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2635 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2636 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2637 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2638 unsigned OpToUse = 0;
2639 if (HasSMUL_LOHI && !HasMULHS) {
2640 OpToUse = ISD::SMUL_LOHI;
2641 } else if (HasUMUL_LOHI && !HasMULHU) {
2642 OpToUse = ISD::UMUL_LOHI;
2643 } else if (HasSMUL_LOHI) {
2644 OpToUse = ISD::SMUL_LOHI;
2645 } else if (HasUMUL_LOHI) {
2646 OpToUse = ISD::UMUL_LOHI;
2647 }
2648 if (OpToUse) {
2649 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2650 break;
2651 }
2652 }
2653 if (Node->getOpcode() == ISD::MULHS &&
2654 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2655 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2656 break;
2657 }
2658 if (Node->getOpcode() == ISD::MULHU &&
2659 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2660 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2661 break;
2662 }
2663 if (Node->getOpcode() == ISD::SDIV &&
2664 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2665 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2666 break;
2667 }
2668 if (Node->getOpcode() == ISD::UDIV &&
2669 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2670 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2671 break;
2672 }
2673
Dan Gohman82669522007-10-11 23:57:53 +00002674 // Check to see if we have a libcall for this operator.
2675 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2676 bool isSigned = false;
2677 switch (Node->getOpcode()) {
2678 case ISD::UDIV:
2679 case ISD::SDIV:
2680 if (VT == MVT::i32) {
2681 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002682 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002683 isSigned = Node->getOpcode() == ISD::SDIV;
2684 }
2685 break;
2686 case ISD::FPOW:
2687 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2688 VT == MVT::f64 ? RTLIB::POW_F64 :
2689 VT == MVT::f80 ? RTLIB::POW_F80 :
2690 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2691 RTLIB::UNKNOWN_LIBCALL;
2692 break;
2693 default: break;
2694 }
2695 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2696 SDOperand Dummy;
2697 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002698 break;
2699 }
2700
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002701 assert(MVT::isVector(Node->getValueType(0)) &&
2702 "Cannot expand this binary operator!");
2703 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002704 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002705 break;
2706 }
Evan Chengcc987612006-04-12 21:20:24 +00002707 case TargetLowering::Promote: {
2708 switch (Node->getOpcode()) {
2709 default: assert(0 && "Do not know how to promote this BinOp!");
2710 case ISD::AND:
2711 case ISD::OR:
2712 case ISD::XOR: {
2713 MVT::ValueType OVT = Node->getValueType(0);
2714 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2715 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2716 // Bit convert each of the values to the new type.
2717 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2718 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2719 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2720 // Bit convert the result back the original type.
2721 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2722 break;
2723 }
2724 }
2725 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002726 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002727 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002728
Dan Gohmane14ea862007-10-05 14:17:22 +00002729 case ISD::SMUL_LOHI:
2730 case ISD::UMUL_LOHI:
2731 case ISD::SDIVREM:
2732 case ISD::UDIVREM:
2733 // These nodes will only be produced by target-specific lowering, so
2734 // they shouldn't be here if they aren't legal.
2735 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
2736 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002737
2738 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2739 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002741 break;
2742
Chris Lattnera09f8482006-03-05 05:09:38 +00002743 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2744 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2745 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2746 case Expand: assert(0 && "Not possible");
2747 case Legal:
2748 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2749 break;
2750 case Promote:
2751 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2752 break;
2753 }
2754
2755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2756
2757 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2758 default: assert(0 && "Operation not supported");
2759 case TargetLowering::Custom:
2760 Tmp1 = TLI.LowerOperation(Result, DAG);
2761 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002762 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002763 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002764 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002765 // If this target supports fabs/fneg natively and select is cheap,
2766 // do this efficiently.
2767 if (!TLI.isSelectExpensive() &&
2768 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2769 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002770 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002771 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002772 // Get the sign bit of the RHS.
2773 MVT::ValueType IVT =
2774 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2775 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2776 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2777 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2778 // Get the absolute value of the result.
2779 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2780 // Select between the nabs and abs value based on the sign bit of
2781 // the input.
2782 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2783 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2784 AbsVal),
2785 AbsVal);
2786 Result = LegalizeOp(Result);
2787 break;
2788 }
2789
2790 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002791 MVT::ValueType NVT =
2792 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2793 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2794 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2795 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002796 break;
2797 }
Evan Cheng912095b2007-01-04 21:56:39 +00002798 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002799 break;
2800
Nate Begeman551bf3f2006-02-17 05:43:56 +00002801 case ISD::ADDC:
2802 case ISD::SUBC:
2803 Tmp1 = LegalizeOp(Node->getOperand(0));
2804 Tmp2 = LegalizeOp(Node->getOperand(1));
2805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2806 // Since this produces two values, make sure to remember that we legalized
2807 // both of them.
2808 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2809 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2810 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002811
Nate Begeman551bf3f2006-02-17 05:43:56 +00002812 case ISD::ADDE:
2813 case ISD::SUBE:
2814 Tmp1 = LegalizeOp(Node->getOperand(0));
2815 Tmp2 = LegalizeOp(Node->getOperand(1));
2816 Tmp3 = LegalizeOp(Node->getOperand(2));
2817 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2818 // Since this produces two values, make sure to remember that we legalized
2819 // both of them.
2820 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2821 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2822 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002823
Nate Begeman419f8b62005-10-18 00:27:41 +00002824 case ISD::BUILD_PAIR: {
2825 MVT::ValueType PairTy = Node->getValueType(0);
2826 // TODO: handle the case where the Lo and Hi operands are not of legal type
2827 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2828 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2829 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002830 case TargetLowering::Promote:
2831 case TargetLowering::Custom:
2832 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002833 case TargetLowering::Legal:
2834 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2835 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2836 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002837 case TargetLowering::Expand:
2838 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2839 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2840 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2841 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2842 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002843 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002844 break;
2845 }
2846 break;
2847 }
2848
Nate Begemanc105e192005-04-06 00:23:54 +00002849 case ISD::UREM:
2850 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002851 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002852 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2853 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002854
Nate Begemanc105e192005-04-06 00:23:54 +00002855 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002856 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2857 case TargetLowering::Custom:
2858 isCustom = true;
2859 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002860 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002862 if (isCustom) {
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
2864 if (Tmp1.Val) Result = Tmp1;
2865 }
Nate Begemanc105e192005-04-06 00:23:54 +00002866 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002867 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002868 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002869 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002870 MVT::ValueType VT = Node->getValueType(0);
2871
2872 // See if remainder can be lowered using two-result operations.
2873 SDVTList VTs = DAG.getVTList(VT, VT);
2874 if (Node->getOpcode() == ISD::SREM &&
2875 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2876 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2877 break;
2878 }
2879 if (Node->getOpcode() == ISD::UREM &&
2880 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2881 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2882 break;
2883 }
2884
2885 if (MVT::isInteger(VT)) {
2886 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002887 TargetLowering::Legal) {
2888 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002889 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002890 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2891 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2892 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002893 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002894 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002895 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2896 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002897 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002898 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002899 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002900 } else {
2901 // Floating point mod -> fmod libcall.
Dan Gohman525178c2007-10-08 18:33:35 +00002902 RTLIB::Libcall LC = VT == MVT::f32
Evan Cheng56966222007-01-12 02:11:51 +00002903 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002904 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002905 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2906 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002907 }
2908 break;
2909 }
Dan Gohman525178c2007-10-08 18:33:35 +00002910 }
Nate Begemanc105e192005-04-06 00:23:54 +00002911 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002912 case ISD::VAARG: {
2913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2914 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2915
Chris Lattner5c62f332006-01-28 07:42:08 +00002916 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002917 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2918 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002919 case TargetLowering::Custom:
2920 isCustom = true;
2921 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002922 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2924 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002925 Tmp1 = Result.getValue(1);
2926
2927 if (isCustom) {
2928 Tmp2 = TLI.LowerOperation(Result, DAG);
2929 if (Tmp2.Val) {
2930 Result = LegalizeOp(Tmp2);
2931 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2932 }
2933 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002934 break;
2935 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002936 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002937 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002938 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002939 // Increment the pointer, VAList, to the next vaarg
2940 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2941 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2942 TLI.getPointerTy()));
2943 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002944 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2945 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002946 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002947 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002948 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002949 Result = LegalizeOp(Result);
2950 break;
2951 }
2952 }
2953 // Since VAARG produces two values, make sure to remember that we
2954 // legalized both of them.
2955 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002956 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2957 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002958 }
2959
2960 case ISD::VACOPY:
2961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2962 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2963 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2964
2965 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2966 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002967 case TargetLowering::Custom:
2968 isCustom = true;
2969 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002970 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2972 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002973 if (isCustom) {
2974 Tmp1 = TLI.LowerOperation(Result, DAG);
2975 if (Tmp1.Val) Result = Tmp1;
2976 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002977 break;
2978 case TargetLowering::Expand:
2979 // This defaults to loading a pointer from the input and storing it to the
2980 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002981 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002982 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002983 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2984 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002985 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2986 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002987 break;
2988 }
2989 break;
2990
2991 case ISD::VAEND:
2992 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2993 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2994
2995 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2996 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002997 case TargetLowering::Custom:
2998 isCustom = true;
2999 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003000 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003001 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003002 if (isCustom) {
3003 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3004 if (Tmp1.Val) Result = Tmp1;
3005 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003006 break;
3007 case TargetLowering::Expand:
3008 Result = Tmp1; // Default to a no-op, return the chain
3009 break;
3010 }
3011 break;
3012
3013 case ISD::VASTART:
3014 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3015 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3016
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003017 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3018
Nate Begemanacc398c2006-01-25 18:21:52 +00003019 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3020 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003021 case TargetLowering::Legal: break;
3022 case TargetLowering::Custom:
3023 Tmp1 = TLI.LowerOperation(Result, DAG);
3024 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003025 break;
3026 }
3027 break;
3028
Nate Begeman35ef9132006-01-11 21:21:00 +00003029 case ISD::ROTL:
3030 case ISD::ROTR:
3031 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3032 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003034 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3035 default:
3036 assert(0 && "ROTL/ROTR legalize operation not supported");
3037 break;
3038 case TargetLowering::Legal:
3039 break;
3040 case TargetLowering::Custom:
3041 Tmp1 = TLI.LowerOperation(Result, DAG);
3042 if (Tmp1.Val) Result = Tmp1;
3043 break;
3044 case TargetLowering::Promote:
3045 assert(0 && "Do not know how to promote ROTL/ROTR");
3046 break;
3047 case TargetLowering::Expand:
3048 assert(0 && "Do not know how to expand ROTL/ROTR");
3049 break;
3050 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003051 break;
3052
Nate Begemand88fc032006-01-14 03:14:10 +00003053 case ISD::BSWAP:
3054 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3055 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003056 case TargetLowering::Custom:
3057 assert(0 && "Cannot custom legalize this yet!");
3058 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003060 break;
3061 case TargetLowering::Promote: {
3062 MVT::ValueType OVT = Tmp1.getValueType();
3063 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003064 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003065
Chris Lattner456a93a2006-01-28 07:39:30 +00003066 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3067 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3068 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3069 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3070 break;
3071 }
3072 case TargetLowering::Expand:
3073 Result = ExpandBSWAP(Tmp1);
3074 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003075 }
3076 break;
3077
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003078 case ISD::CTPOP:
3079 case ISD::CTTZ:
3080 case ISD::CTLZ:
3081 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3082 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003083 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003084 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003085 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003086 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003087 TargetLowering::Custom) {
3088 Tmp1 = TLI.LowerOperation(Result, DAG);
3089 if (Tmp1.Val) {
3090 Result = Tmp1;
3091 }
Scott Michel910b66d2007-07-30 21:00:31 +00003092 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003093 break;
3094 case TargetLowering::Promote: {
3095 MVT::ValueType OVT = Tmp1.getValueType();
3096 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003097
3098 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003099 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3100 // Perform the larger operation, then subtract if needed.
3101 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003102 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003103 case ISD::CTPOP:
3104 Result = Tmp1;
3105 break;
3106 case ISD::CTTZ:
3107 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003108 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003109 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003110 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003111 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003112 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003113 break;
3114 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003115 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003116 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003117 DAG.getConstant(MVT::getSizeInBits(NVT) -
3118 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003119 break;
3120 }
3121 break;
3122 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003123 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003124 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003125 break;
3126 }
3127 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003128
Chris Lattner2c8086f2005-04-02 05:00:07 +00003129 // Unary operators
3130 case ISD::FABS:
3131 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003132 case ISD::FSQRT:
3133 case ISD::FSIN:
3134 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003135 Tmp1 = LegalizeOp(Node->getOperand(0));
3136 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003137 case TargetLowering::Promote:
3138 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003139 isCustom = true;
3140 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003141 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003142 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003143 if (isCustom) {
3144 Tmp1 = TLI.LowerOperation(Result, DAG);
3145 if (Tmp1.Val) Result = Tmp1;
3146 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003147 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003148 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003149 switch (Node->getOpcode()) {
3150 default: assert(0 && "Unreachable!");
3151 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003152 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3153 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003155 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003156 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003157 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3158 MVT::ValueType VT = Node->getValueType(0);
3159 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003160 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003161 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3162 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003163 break;
3164 }
3165 case ISD::FSQRT:
3166 case ISD::FSIN:
3167 case ISD::FCOS: {
3168 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003169
3170 // Expand unsupported unary vector operators by unrolling them.
3171 if (MVT::isVector(VT)) {
3172 Result = LegalizeOp(UnrollVectorOp(Op));
3173 break;
3174 }
3175
Evan Cheng56966222007-01-12 02:11:51 +00003176 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003177 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003178 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00003179 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003180 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3181 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3182 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3183 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00003184 break;
3185 case ISD::FSIN:
3186 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3187 break;
3188 case ISD::FCOS:
3189 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3190 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003191 default: assert(0 && "Unreachable!");
3192 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003193 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003194 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3195 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003196 break;
3197 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003198 }
3199 break;
3200 }
3201 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003202 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003203 MVT::ValueType VT = Node->getValueType(0);
3204
3205 // Expand unsupported unary vector operators by unrolling them.
3206 if (MVT::isVector(VT)) {
3207 Result = LegalizeOp(UnrollVectorOp(Op));
3208 break;
3209 }
3210
3211 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen317096a2007-09-28 01:08:20 +00003212 RTLIB::Libcall LC =
Dan Gohman82669522007-10-11 23:57:53 +00003213 VT == MVT::f32 ? RTLIB::POWI_F32 :
3214 VT == MVT::f64 ? RTLIB::POWI_F64 :
3215 VT == MVT::f80 ? RTLIB::POWI_F80 :
3216 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003217 RTLIB::UNKNOWN_LIBCALL;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003218 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003219 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3220 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003221 break;
3222 }
Chris Lattner35481892005-12-23 00:16:34 +00003223 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003224 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003225 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003226 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3227 // The input has to be a vector type, we have to either scalarize it, pack
3228 // it, or convert it based on whether the input vector type is legal.
3229 SDNode *InVal = Node->getOperand(0).Val;
3230 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3231 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3232
3233 // Figure out if there is a simple type corresponding to this Vector
3234 // type. If so, convert to the vector type.
3235 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3236 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003237 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003238 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3239 LegalizeOp(Node->getOperand(0)));
3240 break;
3241 } else if (NumElems == 1) {
3242 // Turn this into a bit convert of the scalar input.
3243 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3244 ScalarizeVectorOp(Node->getOperand(0)));
3245 break;
3246 } else {
3247 // FIXME: UNIMP! Store then reload
3248 assert(0 && "Cast from unsupported vector type not implemented yet!");
3249 }
Chris Lattner67993f72006-01-23 07:30:46 +00003250 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003251 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3252 Node->getOperand(0).getValueType())) {
3253 default: assert(0 && "Unknown operation action!");
3254 case TargetLowering::Expand:
3255 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3256 break;
3257 case TargetLowering::Legal:
3258 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003259 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003260 break;
3261 }
3262 }
3263 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003264
Chris Lattner2c8086f2005-04-02 05:00:07 +00003265 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003266 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003267 case ISD::UINT_TO_FP: {
3268 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003269 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3270 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003271 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003272 Node->getOperand(0).getValueType())) {
3273 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003274 case TargetLowering::Custom:
3275 isCustom = true;
3276 // FALLTHROUGH
3277 case TargetLowering::Legal:
3278 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003279 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003280 if (isCustom) {
3281 Tmp1 = TLI.LowerOperation(Result, DAG);
3282 if (Tmp1.Val) Result = Tmp1;
3283 }
3284 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003285 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003286 Result = ExpandLegalINT_TO_FP(isSigned,
3287 LegalizeOp(Node->getOperand(0)),
3288 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003289 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003290 case TargetLowering::Promote:
3291 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3292 Node->getValueType(0),
3293 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003294 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003295 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003296 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003297 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003298 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3299 Node->getValueType(0), Node->getOperand(0));
3300 break;
3301 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003302 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003303 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003304 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3305 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003306 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003307 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3308 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003309 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003310 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3311 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003312 break;
3313 }
3314 break;
3315 }
3316 case ISD::TRUNCATE:
3317 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3318 case Legal:
3319 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003320 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003321 break;
3322 case Expand:
3323 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3324
3325 // Since the result is legal, we should just be able to truncate the low
3326 // part of the source.
3327 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3328 break;
3329 case Promote:
3330 Result = PromoteOp(Node->getOperand(0));
3331 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3332 break;
3333 }
3334 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003335
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003336 case ISD::FP_TO_SINT:
3337 case ISD::FP_TO_UINT:
3338 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3339 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003340 Tmp1 = LegalizeOp(Node->getOperand(0));
3341
Chris Lattner1618beb2005-07-29 00:11:56 +00003342 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3343 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003344 case TargetLowering::Custom:
3345 isCustom = true;
3346 // FALLTHROUGH
3347 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003348 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003349 if (isCustom) {
3350 Tmp1 = TLI.LowerOperation(Result, DAG);
3351 if (Tmp1.Val) Result = Tmp1;
3352 }
3353 break;
3354 case TargetLowering::Promote:
3355 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3356 Node->getOpcode() == ISD::FP_TO_SINT);
3357 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003358 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003359 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3360 SDOperand True, False;
3361 MVT::ValueType VT = Node->getOperand(0).getValueType();
3362 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003363 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003364 const uint64_t zero[] = {0, 0};
3365 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3366 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003367 (void)apf.convertFromZeroExtendedInteger
3368 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003369 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003370 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3371 Node->getOperand(0), Tmp2, ISD::SETLT);
3372 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3373 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003374 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003375 Tmp2));
3376 False = DAG.getNode(ISD::XOR, NVT, False,
3377 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003378 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3379 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003380 } else {
3381 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3382 }
3383 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003384 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003385 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003386 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003387 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003388 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003389 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003390 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003391 if (Node->getOpcode()==ISD::FP_TO_SINT)
3392 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003393 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3394 (DAG.getNode(ISD::FP_ROUND_INREG,
3395 MVT::ppcf128, Node->getOperand(0),
3396 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003397 else {
3398 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3399 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3400 Tmp2 = DAG.getConstantFP(apf, OVT);
3401 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3402 // FIXME: generated code sucks.
3403 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3404 DAG.getNode(ISD::ADD, MVT::i32,
3405 DAG.getNode(ISD::FP_TO_SINT, VT,
3406 DAG.getNode(ISD::FSUB, OVT,
3407 Node->getOperand(0), Tmp2)),
3408 DAG.getConstant(0x80000000, MVT::i32)),
3409 DAG.getNode(ISD::FP_TO_SINT, VT,
3410 Node->getOperand(0)),
3411 DAG.getCondCode(ISD::SETGE));
3412 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003413 break;
3414 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003415 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003416 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003417 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003418 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003419 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003420 LC = (VT == MVT::i32)
3421 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003422 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003423 LC = (VT == MVT::i32)
3424 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003425 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003426 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003427 LC = RTLIB::FPTOSINT_F80_I64;
3428 }
3429 else if (OVT == MVT::ppcf128) {
3430 assert(VT == MVT::i64);
3431 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003432 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003433 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003434 }
3435 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003436 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003437 LC = (VT == MVT::i32)
3438 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003439 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003440 LC = (VT == MVT::i32)
3441 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003442 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003443 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003444 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3445 }
3446 else if (OVT == MVT::ppcf128) {
3447 assert(VT == MVT::i64);
3448 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003449 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003450 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003451 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003452 default: assert(0 && "Unreachable!");
3453 }
3454 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003455 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3456 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003457 break;
3458 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003459 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003460 Tmp1 = PromoteOp(Node->getOperand(0));
3461 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3462 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003463 break;
3464 }
3465 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003466
Dale Johannesenab081c72007-08-09 17:27:48 +00003467 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003468 case ISD::FP_ROUND: {
3469 MVT::ValueType newVT = Op.getValueType();
3470 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3471 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003472 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3473 SDOperand Lo, Hi;
3474 ExpandOp(Node->getOperand(0), Lo, Hi);
3475 if (newVT == MVT::f64)
3476 Result = Hi;
3477 else
3478 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3479 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003480 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003481 // The only other way we can lower this is to turn it into a STORE,
3482 // LOAD pair, targetting a temporary location (a stack slot).
3483
3484 // NOTE: there is a choice here between constantly creating new stack
3485 // slots and always reusing the same one. We currently always create
3486 // new ones, as reuse may inhibit scheduling.
3487 MVT::ValueType slotVT =
3488 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3489 const Type *Ty = MVT::getTypeForValueType(slotVT);
3490 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3491 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3492 MachineFunction &MF = DAG.getMachineFunction();
3493 int SSFI =
3494 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3495 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3496 if (Node->getOpcode() == ISD::FP_EXTEND) {
3497 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3498 StackSlot, NULL, 0);
3499 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3500 Result, StackSlot, NULL, 0, oldVT);
3501 } else {
3502 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3503 StackSlot, NULL, 0, newVT);
3504 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3505 }
3506 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003507 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003508 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003509 }
3510 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003511 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003512 case ISD::ZERO_EXTEND:
3513 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003514 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003515 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003516 case Legal:
3517 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003518 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003519 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003520 case Promote:
3521 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003522 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003523 Tmp1 = PromoteOp(Node->getOperand(0));
3524 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003525 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003526 case ISD::ZERO_EXTEND:
3527 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003528 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003529 Result = DAG.getZeroExtendInReg(Result,
3530 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003531 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003532 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003533 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003534 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003535 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003536 Result,
3537 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003538 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003539 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003540 Result = PromoteOp(Node->getOperand(0));
3541 if (Result.getValueType() != Op.getValueType())
3542 // Dynamically dead while we have only 2 FP types.
3543 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3544 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003545 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003546 Result = PromoteOp(Node->getOperand(0));
3547 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3548 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003549 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003550 }
3551 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003552 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003553 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003554 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003555 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003556
3557 // If this operation is not supported, convert it to a shl/shr or load/store
3558 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003559 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3560 default: assert(0 && "This action not supported for this op yet!");
3561 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003563 break;
3564 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003565 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003566 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003567 // NOTE: we could fall back on load/store here too for targets without
3568 // SAR. However, it is doubtful that any exist.
3569 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3570 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003571 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003572 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3573 Node->getOperand(0), ShiftCst);
3574 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3575 Result, ShiftCst);
3576 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003577 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003578 // EXTLOAD pair, targetting a temporary location (a stack slot).
3579
3580 // NOTE: there is a choice here between constantly creating new stack
3581 // slots and always reusing the same one. We currently always create
3582 // new ones, as reuse may inhibit scheduling.
3583 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003584 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003585 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003586 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003587 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003588 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003589 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003590 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3591 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003592 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003593 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003594 } else {
3595 assert(0 && "Unknown op");
3596 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003597 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003598 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003599 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003600 }
Duncan Sands36397f52007-07-27 12:58:54 +00003601 case ISD::TRAMPOLINE: {
3602 SDOperand Ops[6];
3603 for (unsigned i = 0; i != 6; ++i)
3604 Ops[i] = LegalizeOp(Node->getOperand(i));
3605 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3606 // The only option for this node is to custom lower it.
3607 Result = TLI.LowerOperation(Result, DAG);
3608 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003609
3610 // Since trampoline produces two values, make sure to remember that we
3611 // legalized both of them.
3612 Tmp1 = LegalizeOp(Result.getValue(1));
3613 Result = LegalizeOp(Result);
3614 AddLegalizedOperand(SDOperand(Node, 0), Result);
3615 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3616 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003617 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003618 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003619
Chris Lattner4ddd2832006-04-08 04:13:17 +00003620 assert(Result.getValueType() == Op.getValueType() &&
3621 "Bad legalization!");
3622
Chris Lattner456a93a2006-01-28 07:39:30 +00003623 // Make sure that the generated code is itself legal.
3624 if (Result != Op)
3625 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003626
Chris Lattner45982da2005-05-12 16:53:42 +00003627 // Note that LegalizeOp may be reentered even from single-use nodes, which
3628 // means that we always must cache transformed nodes.
3629 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003630 return Result;
3631}
3632
Chris Lattner8b6fa222005-01-15 22:16:26 +00003633/// PromoteOp - Given an operation that produces a value in an invalid type,
3634/// promote it to compute the value into a larger type. The produced value will
3635/// have the correct bits for the low portion of the register, but no guarantee
3636/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003637SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3638 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003639 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003640 assert(getTypeAction(VT) == Promote &&
3641 "Caller should expand or legalize operands that are not promotable!");
3642 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3643 "Cannot promote to smaller type!");
3644
Chris Lattner03c85462005-01-15 05:21:40 +00003645 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003646 SDOperand Result;
3647 SDNode *Node = Op.Val;
3648
Chris Lattner40030bf2007-02-04 01:17:38 +00003649 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003650 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003651
Chris Lattner03c85462005-01-15 05:21:40 +00003652 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003653 case ISD::CopyFromReg:
3654 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003655 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003656#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003657 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003658#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003659 assert(0 && "Do not know how to promote this operator!");
3660 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003661 case ISD::UNDEF:
3662 Result = DAG.getNode(ISD::UNDEF, NVT);
3663 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003664 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003665 if (VT != MVT::i1)
3666 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3667 else
3668 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003669 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3670 break;
3671 case ISD::ConstantFP:
3672 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3673 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3674 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003675
Chris Lattner82fbfb62005-01-18 02:59:52 +00003676 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003677 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003678 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3679 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003680 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003681
Chris Lattner03c85462005-01-15 05:21:40 +00003682 case ISD::TRUNCATE:
3683 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3684 case Legal:
3685 Result = LegalizeOp(Node->getOperand(0));
3686 assert(Result.getValueType() >= NVT &&
3687 "This truncation doesn't make sense!");
3688 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3689 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3690 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003691 case Promote:
3692 // The truncation is not required, because we don't guarantee anything
3693 // about high bits anyway.
3694 Result = PromoteOp(Node->getOperand(0));
3695 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003696 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003697 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3698 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003699 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003700 }
3701 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003702 case ISD::SIGN_EXTEND:
3703 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003704 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003705 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3706 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3707 case Legal:
3708 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003709 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003710 break;
3711 case Promote:
3712 // Promote the reg if it's smaller.
3713 Result = PromoteOp(Node->getOperand(0));
3714 // The high bits are not guaranteed to be anything. Insert an extend.
3715 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003716 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003717 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003718 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003719 Result = DAG.getZeroExtendInReg(Result,
3720 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003721 break;
3722 }
3723 break;
Chris Lattner35481892005-12-23 00:16:34 +00003724 case ISD::BIT_CONVERT:
3725 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3726 Result = PromoteOp(Result);
3727 break;
3728
Chris Lattner8b6fa222005-01-15 22:16:26 +00003729 case ISD::FP_EXTEND:
3730 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3731 case ISD::FP_ROUND:
3732 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3733 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3734 case Promote: assert(0 && "Unreachable with 2 FP types!");
3735 case Legal:
3736 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003737 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003738 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003739 break;
3740 }
3741 break;
3742
3743 case ISD::SINT_TO_FP:
3744 case ISD::UINT_TO_FP:
3745 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3746 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003747 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003748 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003749 break;
3750
3751 case Promote:
3752 Result = PromoteOp(Node->getOperand(0));
3753 if (Node->getOpcode() == ISD::SINT_TO_FP)
3754 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003755 Result,
3756 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003757 else
Chris Lattner23993562005-04-13 02:38:47 +00003758 Result = DAG.getZeroExtendInReg(Result,
3759 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003760 // No extra round required here.
3761 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003762 break;
3763 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003764 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3765 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003766 // Round if we cannot tolerate excess precision.
3767 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003768 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3769 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003770 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003771 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003772 break;
3773
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003774 case ISD::SIGN_EXTEND_INREG:
3775 Result = PromoteOp(Node->getOperand(0));
3776 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3777 Node->getOperand(1));
3778 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003779 case ISD::FP_TO_SINT:
3780 case ISD::FP_TO_UINT:
3781 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3782 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003783 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003784 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003785 break;
3786 case Promote:
3787 // The input result is prerounded, so we don't have to do anything
3788 // special.
3789 Tmp1 = PromoteOp(Node->getOperand(0));
3790 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003791 }
Nate Begemand2558e32005-08-14 01:20:53 +00003792 // If we're promoting a UINT to a larger size, check to see if the new node
3793 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3794 // we can use that instead. This allows us to generate better code for
3795 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3796 // legal, such as PowerPC.
3797 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003798 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003799 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3800 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003801 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3802 } else {
3803 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3804 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003805 break;
3806
Chris Lattner2c8086f2005-04-02 05:00:07 +00003807 case ISD::FABS:
3808 case ISD::FNEG:
3809 Tmp1 = PromoteOp(Node->getOperand(0));
3810 assert(Tmp1.getValueType() == NVT);
3811 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3812 // NOTE: we do not have to do any extra rounding here for
3813 // NoExcessFPPrecision, because we know the input will have the appropriate
3814 // precision, and these operations don't modify precision at all.
3815 break;
3816
Chris Lattnerda6ba872005-04-28 21:44:33 +00003817 case ISD::FSQRT:
3818 case ISD::FSIN:
3819 case ISD::FCOS:
3820 Tmp1 = PromoteOp(Node->getOperand(0));
3821 assert(Tmp1.getValueType() == NVT);
3822 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003823 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003824 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3825 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003826 break;
3827
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003828 case ISD::FPOWI: {
3829 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3830 // directly as well, which may be better.
3831 Tmp1 = PromoteOp(Node->getOperand(0));
3832 assert(Tmp1.getValueType() == NVT);
3833 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3834 if (NoExcessFPPrecision)
3835 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3836 DAG.getValueType(VT));
3837 break;
3838 }
3839
Chris Lattner03c85462005-01-15 05:21:40 +00003840 case ISD::AND:
3841 case ISD::OR:
3842 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003843 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003844 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003845 case ISD::MUL:
3846 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003847 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003848 // that too is okay if they are integer operations.
3849 Tmp1 = PromoteOp(Node->getOperand(0));
3850 Tmp2 = PromoteOp(Node->getOperand(1));
3851 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3852 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003853 break;
3854 case ISD::FADD:
3855 case ISD::FSUB:
3856 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003857 Tmp1 = PromoteOp(Node->getOperand(0));
3858 Tmp2 = PromoteOp(Node->getOperand(1));
3859 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3860 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3861
3862 // Floating point operations will give excess precision that we may not be
3863 // able to tolerate. If we DO allow excess precision, just leave it,
3864 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003865 // FIXME: Why would we need to round FP ops more than integer ones?
3866 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003867 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003868 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3869 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003870 break;
3871
Chris Lattner8b6fa222005-01-15 22:16:26 +00003872 case ISD::SDIV:
3873 case ISD::SREM:
3874 // These operators require that their input be sign extended.
3875 Tmp1 = PromoteOp(Node->getOperand(0));
3876 Tmp2 = PromoteOp(Node->getOperand(1));
3877 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003878 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3879 DAG.getValueType(VT));
3880 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3881 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003882 }
3883 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3884
3885 // Perform FP_ROUND: this is probably overly pessimistic.
3886 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003887 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3888 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003889 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003890 case ISD::FDIV:
3891 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003892 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003893 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3895 case Legal:
3896 Tmp1 = LegalizeOp(Node->getOperand(0));
3897 break;
3898 case Promote:
3899 Tmp1 = PromoteOp(Node->getOperand(0));
3900 break;
3901 case Expand:
3902 assert(0 && "not implemented");
3903 }
3904 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3905 case Legal:
3906 Tmp2 = LegalizeOp(Node->getOperand(1));
3907 break;
3908 case Promote:
3909 Tmp2 = PromoteOp(Node->getOperand(1));
3910 break;
3911 case Expand:
3912 assert(0 && "not implemented");
3913 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003914 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3915
3916 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003917 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003918 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3919 DAG.getValueType(VT));
3920 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003921
3922 case ISD::UDIV:
3923 case ISD::UREM:
3924 // These operators require that their input be zero extended.
3925 Tmp1 = PromoteOp(Node->getOperand(0));
3926 Tmp2 = PromoteOp(Node->getOperand(1));
3927 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003928 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3929 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003930 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3931 break;
3932
3933 case ISD::SHL:
3934 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003935 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003936 break;
3937 case ISD::SRA:
3938 // The input value must be properly sign extended.
3939 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003940 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3941 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003942 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003943 break;
3944 case ISD::SRL:
3945 // The input value must be properly zero extended.
3946 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003947 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003948 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003949 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003950
3951 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003952 Tmp1 = Node->getOperand(0); // Get the chain.
3953 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003954 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3955 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3956 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3957 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003958 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003959 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003960 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003961 // Increment the pointer, VAList, to the next vaarg
3962 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3963 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3964 TLI.getPointerTy()));
3965 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003966 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3967 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003968 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003969 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003970 }
3971 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003972 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003973 break;
3974
Evan Cheng466685d2006-10-09 20:57:25 +00003975 case ISD::LOAD: {
3976 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003977 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3978 ? ISD::EXTLOAD : LD->getExtensionType();
3979 Result = DAG.getExtLoad(ExtType, NVT,
3980 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003981 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003982 LD->getLoadedVT(),
3983 LD->isVolatile(),
3984 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003985 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003986 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003987 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003988 }
Chris Lattner03c85462005-01-15 05:21:40 +00003989 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003990 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3991 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003992 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003993 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003994 case ISD::SELECT_CC:
3995 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3996 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3997 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003998 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003999 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004000 case ISD::BSWAP:
4001 Tmp1 = Node->getOperand(0);
4002 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4003 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4004 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004005 DAG.getConstant(MVT::getSizeInBits(NVT) -
4006 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004007 TLI.getShiftAmountTy()));
4008 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004009 case ISD::CTPOP:
4010 case ISD::CTTZ:
4011 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004012 // Zero extend the argument
4013 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004014 // Perform the larger operation, then subtract if needed.
4015 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004016 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004017 case ISD::CTPOP:
4018 Result = Tmp1;
4019 break;
4020 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004021 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004022 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004023 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4024 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004025 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004026 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004027 break;
4028 case ISD::CTLZ:
4029 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004030 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004031 DAG.getConstant(MVT::getSizeInBits(NVT) -
4032 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004033 break;
4034 }
4035 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004036 case ISD::EXTRACT_SUBVECTOR:
4037 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004038 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004039 case ISD::EXTRACT_VECTOR_ELT:
4040 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4041 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004042 }
4043
4044 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004045
4046 // Make sure the result is itself legal.
4047 Result = LegalizeOp(Result);
4048
4049 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004050 AddPromotedOperand(Op, Result);
4051 return Result;
4052}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004053
Dan Gohman7f321562007-06-25 16:23:39 +00004054/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4055/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4056/// based on the vector type. The return type of this matches the element type
4057/// of the vector, which may not be legal for the target.
4058SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004059 // We know that operand #0 is the Vec vector. If the index is a constant
4060 // or if the invec is a supported hardware type, we can use it. Otherwise,
4061 // lower to a store then an indexed load.
4062 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004063 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004064
Dan Gohmane40c7b02007-09-24 15:54:53 +00004065 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004066 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004067
Dan Gohman7f321562007-06-25 16:23:39 +00004068 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4069 default: assert(0 && "This action is not supported yet!");
4070 case TargetLowering::Custom: {
4071 Vec = LegalizeOp(Vec);
4072 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4073 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4074 if (Tmp3.Val)
4075 return Tmp3;
4076 break;
4077 }
4078 case TargetLowering::Legal:
4079 if (isTypeLegal(TVT)) {
4080 Vec = LegalizeOp(Vec);
4081 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004082 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004083 }
4084 break;
4085 case TargetLowering::Expand:
4086 break;
4087 }
4088
4089 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004090 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004091 Op = ScalarizeVectorOp(Vec);
4092 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4093 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004094 SDOperand Lo, Hi;
4095 SplitVectorOp(Vec, Lo, Hi);
4096 if (CIdx->getValue() < NumElems/2) {
4097 Vec = Lo;
4098 } else {
4099 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004100 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4101 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004102 }
Dan Gohman7f321562007-06-25 16:23:39 +00004103
Chris Lattner15972212006-03-31 17:55:51 +00004104 // It's now an extract from the appropriate high or low part. Recurse.
4105 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004106 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004107 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004108 // Store the value to a temporary stack slot, then LOAD the scalar
4109 // element back out.
4110 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
4111 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4112
4113 // Add the offset to the index.
4114 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4115 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4116 DAG.getConstant(EltSize, Idx.getValueType()));
4117 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4118
4119 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004120 }
Dan Gohman7f321562007-06-25 16:23:39 +00004121 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004122}
4123
Dan Gohman7f321562007-06-25 16:23:39 +00004124/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004125/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004126SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004127 // We know that operand #0 is the Vec vector. For now we assume the index
4128 // is a constant and that the extracted result is a supported hardware type.
4129 SDOperand Vec = Op.getOperand(0);
4130 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4131
Dan Gohman7f321562007-06-25 16:23:39 +00004132 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004133
4134 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4135 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004136 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004137 }
4138
4139 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4140 SDOperand Lo, Hi;
4141 SplitVectorOp(Vec, Lo, Hi);
4142 if (CIdx->getValue() < NumElems/2) {
4143 Vec = Lo;
4144 } else {
4145 Vec = Hi;
4146 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4147 }
4148
4149 // It's now an extract from the appropriate high or low part. Recurse.
4150 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004151 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004152}
4153
Nate Begeman750ac1b2006-02-01 07:19:44 +00004154/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4155/// with condition CC on the current target. This usually involves legalizing
4156/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4157/// there may be no choice but to create a new SetCC node to represent the
4158/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4159/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4160void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4161 SDOperand &RHS,
4162 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004163 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004164
4165 switch (getTypeAction(LHS.getValueType())) {
4166 case Legal:
4167 Tmp1 = LegalizeOp(LHS); // LHS
4168 Tmp2 = LegalizeOp(RHS); // RHS
4169 break;
4170 case Promote:
4171 Tmp1 = PromoteOp(LHS); // LHS
4172 Tmp2 = PromoteOp(RHS); // RHS
4173
4174 // If this is an FP compare, the operands have already been extended.
4175 if (MVT::isInteger(LHS.getValueType())) {
4176 MVT::ValueType VT = LHS.getValueType();
4177 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4178
4179 // Otherwise, we have to insert explicit sign or zero extends. Note
4180 // that we could insert sign extends for ALL conditions, but zero extend
4181 // is cheaper on many machines (an AND instead of two shifts), so prefer
4182 // it.
4183 switch (cast<CondCodeSDNode>(CC)->get()) {
4184 default: assert(0 && "Unknown integer comparison!");
4185 case ISD::SETEQ:
4186 case ISD::SETNE:
4187 case ISD::SETUGE:
4188 case ISD::SETUGT:
4189 case ISD::SETULE:
4190 case ISD::SETULT:
4191 // ALL of these operations will work if we either sign or zero extend
4192 // the operands (including the unsigned comparisons!). Zero extend is
4193 // usually a simpler/cheaper operation, so prefer it.
4194 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4195 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4196 break;
4197 case ISD::SETGE:
4198 case ISD::SETGT:
4199 case ISD::SETLT:
4200 case ISD::SETLE:
4201 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4202 DAG.getValueType(VT));
4203 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4204 DAG.getValueType(VT));
4205 break;
4206 }
4207 }
4208 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004209 case Expand: {
4210 MVT::ValueType VT = LHS.getValueType();
4211 if (VT == MVT::f32 || VT == MVT::f64) {
4212 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004213 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004214 switch (cast<CondCodeSDNode>(CC)->get()) {
4215 case ISD::SETEQ:
4216 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004217 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004218 break;
4219 case ISD::SETNE:
4220 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004221 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004222 break;
4223 case ISD::SETGE:
4224 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004225 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004226 break;
4227 case ISD::SETLT:
4228 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004229 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004230 break;
4231 case ISD::SETLE:
4232 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004233 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004234 break;
4235 case ISD::SETGT:
4236 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004237 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004238 break;
4239 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004240 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4241 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004242 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004243 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004244 break;
4245 default:
Evan Cheng56966222007-01-12 02:11:51 +00004246 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004247 switch (cast<CondCodeSDNode>(CC)->get()) {
4248 case ISD::SETONE:
4249 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004250 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004251 // Fallthrough
4252 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004253 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004254 break;
4255 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004256 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004257 break;
4258 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004259 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004260 break;
4261 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004262 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004263 break;
Evan Cheng56966222007-01-12 02:11:51 +00004264 case ISD::SETUEQ:
4265 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004266 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004267 default: assert(0 && "Unsupported FP setcc!");
4268 }
4269 }
4270
4271 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004272 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004273 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004274 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004275 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004276 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004277 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004278 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004279 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004280 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004281 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004282 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004283 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004284 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4285 Tmp2 = SDOperand();
4286 }
4287 LHS = Tmp1;
4288 RHS = Tmp2;
4289 return;
4290 }
4291
Nate Begeman750ac1b2006-02-01 07:19:44 +00004292 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4293 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004294 ExpandOp(RHS, RHSLo, RHSHi);
4295 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4296
4297 if (VT==MVT::ppcf128) {
4298 // FIXME: This generated code sucks. We want to generate
4299 // FCMP crN, hi1, hi2
4300 // BNE crN, L:
4301 // FCMP crN, lo1, lo2
4302 // The following can be improved, but not that much.
4303 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4304 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4305 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4306 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4307 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4308 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4309 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4310 Tmp2 = SDOperand();
4311 break;
4312 }
4313
4314 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004315 case ISD::SETEQ:
4316 case ISD::SETNE:
4317 if (RHSLo == RHSHi)
4318 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4319 if (RHSCST->isAllOnesValue()) {
4320 // Comparison to -1.
4321 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4322 Tmp2 = RHSLo;
4323 break;
4324 }
4325
4326 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4327 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4328 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4329 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4330 break;
4331 default:
4332 // If this is a comparison of the sign bit, just look at the top part.
4333 // X > -1, x < 0
4334 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4335 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4336 CST->getValue() == 0) || // X < 0
4337 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4338 CST->isAllOnesValue())) { // X > -1
4339 Tmp1 = LHSHi;
4340 Tmp2 = RHSHi;
4341 break;
4342 }
4343
4344 // FIXME: This generated code sucks.
4345 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004346 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004347 default: assert(0 && "Unknown integer setcc!");
4348 case ISD::SETLT:
4349 case ISD::SETULT: LowCC = ISD::SETULT; break;
4350 case ISD::SETGT:
4351 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4352 case ISD::SETLE:
4353 case ISD::SETULE: LowCC = ISD::SETULE; break;
4354 case ISD::SETGE:
4355 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4356 }
4357
4358 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4359 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4360 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4361
4362 // NOTE: on targets without efficient SELECT of bools, we can always use
4363 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004364 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4365 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4366 false, DagCombineInfo);
4367 if (!Tmp1.Val)
4368 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4369 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4370 CCCode, false, DagCombineInfo);
4371 if (!Tmp2.Val)
4372 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4373
4374 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4375 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4376 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4377 (Tmp2C && Tmp2C->getValue() == 0 &&
4378 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4379 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4380 (Tmp2C && Tmp2C->getValue() == 1 &&
4381 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4382 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4383 // low part is known false, returns high part.
4384 // For LE / GE, if high part is known false, ignore the low part.
4385 // For LT / GT, if high part is known true, ignore the low part.
4386 Tmp1 = Tmp2;
4387 Tmp2 = SDOperand();
4388 } else {
4389 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4390 ISD::SETEQ, false, DagCombineInfo);
4391 if (!Result.Val)
4392 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4393 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4394 Result, Tmp1, Tmp2));
4395 Tmp1 = Result;
4396 Tmp2 = SDOperand();
4397 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004398 }
4399 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004400 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004401 LHS = Tmp1;
4402 RHS = Tmp2;
4403}
4404
Chris Lattner35481892005-12-23 00:16:34 +00004405/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004406/// The resultant code need not be legal. Note that SrcOp is the input operand
4407/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004408SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4409 SDOperand SrcOp) {
4410 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004411 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004412
4413 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004414 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004415 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004416 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004417}
4418
Chris Lattner4352cc92006-04-04 17:23:26 +00004419SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4420 // Create a vector sized/aligned stack slot, store the value to element #0,
4421 // then load the whole vector back out.
4422 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004423 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004424 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004425 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004426}
4427
4428
Chris Lattnerce872152006-03-19 06:31:19 +00004429/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004430/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004431SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4432
4433 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004434 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004435 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004436 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004437 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004438 std::map<SDOperand, std::vector<unsigned> > Values;
4439 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004440 bool isConstant = true;
4441 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4442 SplatValue.getOpcode() != ISD::UNDEF)
4443 isConstant = false;
4444
Evan Cheng033e6812006-03-24 01:17:21 +00004445 for (unsigned i = 1; i < NumElems; ++i) {
4446 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004447 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004448 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004449 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004450 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004451 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004452
4453 // If this isn't a constant element or an undef, we can't use a constant
4454 // pool load.
4455 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4456 V.getOpcode() != ISD::UNDEF)
4457 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004458 }
4459
4460 if (isOnlyLowElement) {
4461 // If the low element is an undef too, then this whole things is an undef.
4462 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4463 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4464 // Otherwise, turn this into a scalar_to_vector node.
4465 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4466 Node->getOperand(0));
4467 }
4468
Chris Lattner2eb86532006-03-24 07:29:17 +00004469 // If all elements are constants, create a load from the constant pool.
4470 if (isConstant) {
4471 MVT::ValueType VT = Node->getValueType(0);
4472 const Type *OpNTy =
4473 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4474 std::vector<Constant*> CV;
4475 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4476 if (ConstantFPSDNode *V =
4477 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004478 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004479 } else if (ConstantSDNode *V =
4480 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004481 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004482 } else {
4483 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4484 CV.push_back(UndefValue::get(OpNTy));
4485 }
4486 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004487 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004488 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004489 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004490 }
4491
Chris Lattner87100e02006-03-20 01:52:29 +00004492 if (SplatValue.Val) { // Splat of one value?
4493 // Build the shuffle constant vector: <0, 0, 0, 0>
4494 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004495 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004496 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004497 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004498 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4499 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004500
4501 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004502 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004503 // Get the splatted value into the low element of a vector register.
4504 SDOperand LowValVec =
4505 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4506
4507 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4508 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4509 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4510 SplatMask);
4511 }
4512 }
4513
Evan Cheng033e6812006-03-24 01:17:21 +00004514 // If there are only two unique elements, we may be able to turn this into a
4515 // vector shuffle.
4516 if (Values.size() == 2) {
4517 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4518 MVT::ValueType MaskVT =
4519 MVT::getIntVectorWithNumElements(NumElems);
4520 std::vector<SDOperand> MaskVec(NumElems);
4521 unsigned i = 0;
4522 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4523 E = Values.end(); I != E; ++I) {
4524 for (std::vector<unsigned>::iterator II = I->second.begin(),
4525 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004526 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004527 i += NumElems;
4528 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004529 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4530 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004531
4532 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004533 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4534 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004535 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004536 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4537 E = Values.end(); I != E; ++I) {
4538 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4539 I->first);
4540 Ops.push_back(Op);
4541 }
4542 Ops.push_back(ShuffleMask);
4543
4544 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004545 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4546 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004547 }
4548 }
Chris Lattnerce872152006-03-19 06:31:19 +00004549
4550 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4551 // aligned object on the stack, store each element into it, then load
4552 // the result as a vector.
4553 MVT::ValueType VT = Node->getValueType(0);
4554 // Create the stack frame object.
4555 SDOperand FIPtr = CreateStackTemporary(VT);
4556
4557 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004558 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004559 unsigned TypeByteSize =
4560 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004561 // Store (in the right endianness) the elements to memory.
4562 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4563 // Ignore undef elements.
4564 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4565
Chris Lattner841c8822006-03-22 01:46:54 +00004566 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004567
4568 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4569 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4570
Evan Cheng786225a2006-10-05 23:01:46 +00004571 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004572 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004573 }
4574
4575 SDOperand StoreChain;
4576 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004577 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4578 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004579 else
4580 StoreChain = DAG.getEntryNode();
4581
4582 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004583 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004584}
4585
4586/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4587/// specified value type.
4588SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4589 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4590 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004591 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004592 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004593 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004594 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4595}
4596
Chris Lattner5b359c62005-04-02 04:00:59 +00004597void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4598 SDOperand Op, SDOperand Amt,
4599 SDOperand &Lo, SDOperand &Hi) {
4600 // Expand the subcomponents.
4601 SDOperand LHSL, LHSH;
4602 ExpandOp(Op, LHSL, LHSH);
4603
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004604 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004605 MVT::ValueType VT = LHSL.getValueType();
4606 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004607 Hi = Lo.getValue(1);
4608}
4609
4610
Chris Lattnere34b3962005-01-19 04:19:40 +00004611/// ExpandShift - Try to find a clever way to expand this shift operation out to
4612/// smaller elements. If we can't find a way that is more efficient than a
4613/// libcall on this target, return false. Otherwise, return true with the
4614/// low-parts expanded into Lo and Hi.
4615bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4616 SDOperand &Lo, SDOperand &Hi) {
4617 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4618 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004619
Chris Lattnere34b3962005-01-19 04:19:40 +00004620 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004621 SDOperand ShAmt = LegalizeOp(Amt);
4622 MVT::ValueType ShTy = ShAmt.getValueType();
4623 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4624 unsigned NVTBits = MVT::getSizeInBits(NVT);
4625
4626 // Handle the case when Amt is an immediate. Other cases are currently broken
4627 // and are disabled.
4628 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4629 unsigned Cst = CN->getValue();
4630 // Expand the incoming operand to be shifted, so that we have its parts
4631 SDOperand InL, InH;
4632 ExpandOp(Op, InL, InH);
4633 switch(Opc) {
4634 case ISD::SHL:
4635 if (Cst > VTBits) {
4636 Lo = DAG.getConstant(0, NVT);
4637 Hi = DAG.getConstant(0, NVT);
4638 } else if (Cst > NVTBits) {
4639 Lo = DAG.getConstant(0, NVT);
4640 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004641 } else if (Cst == NVTBits) {
4642 Lo = DAG.getConstant(0, NVT);
4643 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004644 } else {
4645 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4646 Hi = DAG.getNode(ISD::OR, NVT,
4647 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4648 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4649 }
4650 return true;
4651 case ISD::SRL:
4652 if (Cst > VTBits) {
4653 Lo = DAG.getConstant(0, NVT);
4654 Hi = DAG.getConstant(0, NVT);
4655 } else if (Cst > NVTBits) {
4656 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4657 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004658 } else if (Cst == NVTBits) {
4659 Lo = InH;
4660 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004661 } else {
4662 Lo = DAG.getNode(ISD::OR, NVT,
4663 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4664 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4665 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4666 }
4667 return true;
4668 case ISD::SRA:
4669 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004670 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004671 DAG.getConstant(NVTBits-1, ShTy));
4672 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004673 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004674 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004675 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004676 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004677 } else if (Cst == NVTBits) {
4678 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004679 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004680 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004681 } else {
4682 Lo = DAG.getNode(ISD::OR, NVT,
4683 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4684 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4685 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4686 }
4687 return true;
4688 }
4689 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004690
4691 // Okay, the shift amount isn't constant. However, if we can tell that it is
4692 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4693 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004694 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004695
4696 // If we know that the high bit of the shift amount is one, then we can do
4697 // this as a couple of simple shifts.
4698 if (KnownOne & Mask) {
4699 // Mask out the high bit, which we know is set.
4700 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4701 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4702
4703 // Expand the incoming operand to be shifted, so that we have its parts
4704 SDOperand InL, InH;
4705 ExpandOp(Op, InL, InH);
4706 switch(Opc) {
4707 case ISD::SHL:
4708 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4709 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4710 return true;
4711 case ISD::SRL:
4712 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4713 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4714 return true;
4715 case ISD::SRA:
4716 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4717 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4718 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4719 return true;
4720 }
4721 }
4722
4723 // If we know that the high bit of the shift amount is zero, then we can do
4724 // this as a couple of simple shifts.
4725 if (KnownZero & Mask) {
4726 // Compute 32-amt.
4727 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4728 DAG.getConstant(NVTBits, Amt.getValueType()),
4729 Amt);
4730
4731 // Expand the incoming operand to be shifted, so that we have its parts
4732 SDOperand InL, InH;
4733 ExpandOp(Op, InL, InH);
4734 switch(Opc) {
4735 case ISD::SHL:
4736 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4737 Hi = DAG.getNode(ISD::OR, NVT,
4738 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4739 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4740 return true;
4741 case ISD::SRL:
4742 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4743 Lo = DAG.getNode(ISD::OR, NVT,
4744 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4745 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4746 return true;
4747 case ISD::SRA:
4748 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4749 Lo = DAG.getNode(ISD::OR, NVT,
4750 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4751 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4752 return true;
4753 }
4754 }
4755
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004756 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004757}
Chris Lattner77e77a62005-01-21 06:05:23 +00004758
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004759
Chris Lattner77e77a62005-01-21 06:05:23 +00004760// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4761// does not fit into a register, return the lo part and set the hi part to the
4762// by-reg argument. If it does fit into a single register, return the result
4763// and leave the Hi part unset.
4764SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004765 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004766 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4767 // The input chain to this libcall is the entry node of the function.
4768 // Legalizing the call will automatically add the previous call to the
4769 // dependence.
4770 SDOperand InChain = DAG.getEntryNode();
4771
Chris Lattner77e77a62005-01-21 06:05:23 +00004772 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004773 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004774 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4775 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4776 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004777 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004778 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004779 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004780 }
4781 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004782
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004783 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004784 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004785 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004786 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004787 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004788
Chris Lattner6831a812006-02-13 09:18:02 +00004789 // Legalize the call sequence, starting with the chain. This will advance
4790 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4791 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4792 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004793 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004794 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004795 default: assert(0 && "Unknown thing");
4796 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004797 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004798 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004799 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004800 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004801 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004802 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004803 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004804}
4805
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004806
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004807/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4808///
Chris Lattner77e77a62005-01-21 06:05:23 +00004809SDOperand SelectionDAGLegalize::
4810ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004811 assert(getTypeAction(Source.getValueType()) == Expand &&
4812 "This is not an expansion!");
4813 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4814
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004815 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004816 assert(Source.getValueType() == MVT::i64 &&
4817 "This only works for 64-bit -> FP");
4818 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4819 // incoming integer is set. To handle this, we dynamically test to see if
4820 // it is set, and, if so, add a fudge factor.
4821 SDOperand Lo, Hi;
4822 ExpandOp(Source, Lo, Hi);
4823
Chris Lattner66de05b2005-05-13 04:45:13 +00004824 // If this is unsigned, and not supported, first perform the conversion to
4825 // signed, then adjust the result if the sign bit is set.
4826 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4827 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4828
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004829 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4830 DAG.getConstant(0, Hi.getValueType()),
4831 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004832 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4833 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4834 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004835 uint64_t FF = 0x5f800000ULL;
4836 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004837 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004838
Chris Lattner5839bf22005-08-26 17:15:30 +00004839 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004840 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4841 SDOperand FudgeInReg;
4842 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004843 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004844 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004845 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004846 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004847 CPIdx, NULL, 0, MVT::f32);
4848 else
4849 assert(0 && "Unexpected conversion");
4850
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004851 MVT::ValueType SCVT = SignedConv.getValueType();
4852 if (SCVT != DestTy) {
4853 // Destination type needs to be expanded as well. The FADD now we are
4854 // constructing will be expanded into a libcall.
4855 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4856 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4857 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4858 SignedConv, SignedConv.getValue(1));
4859 }
4860 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4861 }
Chris Lattner473a9902005-09-29 06:44:39 +00004862 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004863 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004864
Chris Lattnera88a2602005-05-14 05:33:54 +00004865 // Check to see if the target has a custom way to lower this. If so, use it.
4866 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4867 default: assert(0 && "This action not implemented for this operation!");
4868 case TargetLowering::Legal:
4869 case TargetLowering::Expand:
4870 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004871 case TargetLowering::Custom: {
4872 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4873 Source), DAG);
4874 if (NV.Val)
4875 return LegalizeOp(NV);
4876 break; // The target decided this was legal after all
4877 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004878 }
4879
Chris Lattner13689e22005-05-12 07:00:44 +00004880 // Expand the source, then glue it back together for the call. We must expand
4881 // the source in case it is shared (this pass of legalize must traverse it).
4882 SDOperand SrcLo, SrcHi;
4883 ExpandOp(Source, SrcLo, SrcHi);
4884 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4885
Evan Cheng56966222007-01-12 02:11:51 +00004886 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004887 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004888 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004889 else {
4890 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004891 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004892 }
Chris Lattner6831a812006-02-13 09:18:02 +00004893
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004894 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004895 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4896 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004897 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4898 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004899}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004900
Chris Lattner22cde6a2006-01-28 08:25:58 +00004901/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4902/// INT_TO_FP operation of the specified operand when the target requests that
4903/// we expand it. At this point, we know that the result and operand types are
4904/// legal for the target.
4905SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4906 SDOperand Op0,
4907 MVT::ValueType DestVT) {
4908 if (Op0.getValueType() == MVT::i32) {
4909 // simple 32-bit [signed|unsigned] integer to float/double expansion
4910
Chris Lattner58092e32007-01-20 22:35:55 +00004911 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004912 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004913 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4914 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004915 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004916 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004917 // get address of 8 byte buffer
4918 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4919 // word offset constant for Hi/Lo address computation
4920 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4921 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004922 SDOperand Hi = StackSlot;
4923 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4924 if (TLI.isLittleEndian())
4925 std::swap(Hi, Lo);
4926
Chris Lattner22cde6a2006-01-28 08:25:58 +00004927 // if signed map to unsigned space
4928 SDOperand Op0Mapped;
4929 if (isSigned) {
4930 // constant used to invert sign bit (signed to unsigned mapping)
4931 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4932 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4933 } else {
4934 Op0Mapped = Op0;
4935 }
4936 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004937 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004938 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004939 // initial hi portion of constructed double
4940 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4941 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004942 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004943 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004944 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004945 // FP constant to bias correct the final result
4946 SDOperand Bias = DAG.getConstantFP(isSigned ?
4947 BitsToDouble(0x4330000080000000ULL)
4948 : BitsToDouble(0x4330000000000000ULL),
4949 MVT::f64);
4950 // subtract the bias
4951 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4952 // final result
4953 SDOperand Result;
4954 // handle final rounding
4955 if (DestVT == MVT::f64) {
4956 // do nothing
4957 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004958 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4959 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4960 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4961 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004962 }
4963 return Result;
4964 }
4965 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4966 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4967
4968 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4969 DAG.getConstant(0, Op0.getValueType()),
4970 ISD::SETLT);
4971 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4972 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4973 SignSet, Four, Zero);
4974
4975 // If the sign bit of the integer is set, the large number will be treated
4976 // as a negative number. To counteract this, the dynamic code adds an
4977 // offset depending on the data type.
4978 uint64_t FF;
4979 switch (Op0.getValueType()) {
4980 default: assert(0 && "Unsupported integer type!");
4981 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4982 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4983 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4984 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4985 }
4986 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004987 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004988
4989 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4990 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4991 SDOperand FudgeInReg;
4992 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004993 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004994 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00004995 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00004996 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004997 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004998 }
4999
5000 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5001}
5002
5003/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5004/// *INT_TO_FP operation of the specified operand when the target requests that
5005/// we promote it. At this point, we know that the result and operand types are
5006/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5007/// operation that takes a larger input.
5008SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5009 MVT::ValueType DestVT,
5010 bool isSigned) {
5011 // First step, figure out the appropriate *INT_TO_FP operation to use.
5012 MVT::ValueType NewInTy = LegalOp.getValueType();
5013
5014 unsigned OpToUse = 0;
5015
5016 // Scan for the appropriate larger type to use.
5017 while (1) {
5018 NewInTy = (MVT::ValueType)(NewInTy+1);
5019 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5020
5021 // If the target supports SINT_TO_FP of this type, use it.
5022 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5023 default: break;
5024 case TargetLowering::Legal:
5025 if (!TLI.isTypeLegal(NewInTy))
5026 break; // Can't use this datatype.
5027 // FALL THROUGH.
5028 case TargetLowering::Custom:
5029 OpToUse = ISD::SINT_TO_FP;
5030 break;
5031 }
5032 if (OpToUse) break;
5033 if (isSigned) continue;
5034
5035 // If the target supports UINT_TO_FP of this type, use it.
5036 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5037 default: break;
5038 case TargetLowering::Legal:
5039 if (!TLI.isTypeLegal(NewInTy))
5040 break; // Can't use this datatype.
5041 // FALL THROUGH.
5042 case TargetLowering::Custom:
5043 OpToUse = ISD::UINT_TO_FP;
5044 break;
5045 }
5046 if (OpToUse) break;
5047
5048 // Otherwise, try a larger type.
5049 }
5050
5051 // Okay, we found the operation and type to use. Zero extend our input to the
5052 // desired type then run the operation on it.
5053 return DAG.getNode(OpToUse, DestVT,
5054 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5055 NewInTy, LegalOp));
5056}
5057
5058/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5059/// FP_TO_*INT operation of the specified operand when the target requests that
5060/// we promote it. At this point, we know that the result and operand types are
5061/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5062/// operation that returns a larger result.
5063SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5064 MVT::ValueType DestVT,
5065 bool isSigned) {
5066 // First step, figure out the appropriate FP_TO*INT operation to use.
5067 MVT::ValueType NewOutTy = DestVT;
5068
5069 unsigned OpToUse = 0;
5070
5071 // Scan for the appropriate larger type to use.
5072 while (1) {
5073 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5074 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5075
5076 // If the target supports FP_TO_SINT returning this type, use it.
5077 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5078 default: break;
5079 case TargetLowering::Legal:
5080 if (!TLI.isTypeLegal(NewOutTy))
5081 break; // Can't use this datatype.
5082 // FALL THROUGH.
5083 case TargetLowering::Custom:
5084 OpToUse = ISD::FP_TO_SINT;
5085 break;
5086 }
5087 if (OpToUse) break;
5088
5089 // If the target supports FP_TO_UINT of this type, use it.
5090 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5091 default: break;
5092 case TargetLowering::Legal:
5093 if (!TLI.isTypeLegal(NewOutTy))
5094 break; // Can't use this datatype.
5095 // FALL THROUGH.
5096 case TargetLowering::Custom:
5097 OpToUse = ISD::FP_TO_UINT;
5098 break;
5099 }
5100 if (OpToUse) break;
5101
5102 // Otherwise, try a larger type.
5103 }
5104
5105 // Okay, we found the operation and type to use. Truncate the result of the
5106 // extended FP_TO_*INT operation to the desired size.
5107 return DAG.getNode(ISD::TRUNCATE, DestVT,
5108 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5109}
5110
5111/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5112///
5113SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5114 MVT::ValueType VT = Op.getValueType();
5115 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5116 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5117 switch (VT) {
5118 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5119 case MVT::i16:
5120 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5121 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5122 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5123 case MVT::i32:
5124 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5125 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5126 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5127 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5128 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5129 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5130 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5131 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5132 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5133 case MVT::i64:
5134 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5135 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5136 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5137 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5138 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5139 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5140 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5141 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5142 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5143 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5144 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5145 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5146 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5147 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5148 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5149 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5150 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5151 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5152 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5153 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5154 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5155 }
5156}
5157
5158/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5159///
5160SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5161 switch (Opc) {
5162 default: assert(0 && "Cannot expand this yet!");
5163 case ISD::CTPOP: {
5164 static const uint64_t mask[6] = {
5165 0x5555555555555555ULL, 0x3333333333333333ULL,
5166 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5167 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5168 };
5169 MVT::ValueType VT = Op.getValueType();
5170 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005171 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005172 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5173 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5174 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5175 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5176 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5177 DAG.getNode(ISD::AND, VT,
5178 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5179 }
5180 return Op;
5181 }
5182 case ISD::CTLZ: {
5183 // for now, we do this:
5184 // x = x | (x >> 1);
5185 // x = x | (x >> 2);
5186 // ...
5187 // x = x | (x >>16);
5188 // x = x | (x >>32); // for 64-bit input
5189 // return popcount(~x);
5190 //
5191 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5192 MVT::ValueType VT = Op.getValueType();
5193 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005194 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005195 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5196 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5197 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5198 }
5199 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5200 return DAG.getNode(ISD::CTPOP, VT, Op);
5201 }
5202 case ISD::CTTZ: {
5203 // for now, we use: { return popcount(~x & (x - 1)); }
5204 // unless the target has ctlz but not ctpop, in which case we use:
5205 // { return 32 - nlz(~x & (x-1)); }
5206 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5207 MVT::ValueType VT = Op.getValueType();
5208 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5209 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5210 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5211 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5212 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5213 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5214 TLI.isOperationLegal(ISD::CTLZ, VT))
5215 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005216 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005217 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5218 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5219 }
5220 }
5221}
Chris Lattnere34b3962005-01-19 04:19:40 +00005222
Chris Lattner3e928bb2005-01-07 07:47:09 +00005223/// ExpandOp - Expand the specified SDOperand into its two component pieces
5224/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5225/// LegalizeNodes map is filled in for any results that are not expanded, the
5226/// ExpandedNodes map is filled in for any results that are expanded, and the
5227/// Lo/Hi values are returned.
5228void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5229 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005230 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005231 SDNode *Node = Op.Val;
5232 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005233 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005234 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005235 "Cannot expand to FP value or to larger int value!");
5236
Chris Lattner6fdcb252005-09-02 20:32:45 +00005237 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005238 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005239 = ExpandedNodes.find(Op);
5240 if (I != ExpandedNodes.end()) {
5241 Lo = I->second.first;
5242 Hi = I->second.second;
5243 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005244 }
5245
Chris Lattner3e928bb2005-01-07 07:47:09 +00005246 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005247 case ISD::CopyFromReg:
5248 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005249 case ISD::FP_ROUND_INREG:
5250 if (VT == MVT::ppcf128 &&
5251 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5252 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005253 SDOperand SrcLo, SrcHi, Src;
5254 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5255 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5256 SDOperand Result = TLI.LowerOperation(
5257 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005258 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5259 Lo = Result.Val->getOperand(0);
5260 Hi = Result.Val->getOperand(1);
5261 break;
5262 }
5263 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005264 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005265#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005266 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005267#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005268 assert(0 && "Do not know how to expand this operator!");
5269 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005270 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005271 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005272 Lo = DAG.getNode(ISD::UNDEF, NVT);
5273 Hi = DAG.getNode(ISD::UNDEF, NVT);
5274 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005275 case ISD::Constant: {
5276 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5277 Lo = DAG.getConstant(Cst, NVT);
5278 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5279 break;
5280 }
Evan Cheng00495212006-12-12 21:32:44 +00005281 case ISD::ConstantFP: {
5282 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005283 if (CFP->getValueType(0) == MVT::ppcf128) {
5284 APInt api = CFP->getValueAPF().convertToAPInt();
5285 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5286 MVT::f64);
5287 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5288 MVT::f64);
5289 break;
5290 }
Evan Cheng279101e2006-12-12 22:19:28 +00005291 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005292 if (getTypeAction(Lo.getValueType()) == Expand)
5293 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005294 break;
5295 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005296 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005297 // Return the operands.
5298 Lo = Node->getOperand(0);
5299 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005300 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005301
5302 case ISD::SIGN_EXTEND_INREG:
5303 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005304 // sext_inreg the low part if needed.
5305 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5306
5307 // The high part gets the sign extension from the lo-part. This handles
5308 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005309 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5310 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5311 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005312 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005313
Nate Begemand88fc032006-01-14 03:14:10 +00005314 case ISD::BSWAP: {
5315 ExpandOp(Node->getOperand(0), Lo, Hi);
5316 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5317 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5318 Lo = TempLo;
5319 break;
5320 }
5321
Chris Lattneredb1add2005-05-11 04:51:16 +00005322 case ISD::CTPOP:
5323 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005324 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5325 DAG.getNode(ISD::CTPOP, NVT, Lo),
5326 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005327 Hi = DAG.getConstant(0, NVT);
5328 break;
5329
Chris Lattner39a8f332005-05-12 19:05:01 +00005330 case ISD::CTLZ: {
5331 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005332 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005333 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5334 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005335 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5336 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005337 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5338 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5339
5340 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5341 Hi = DAG.getConstant(0, NVT);
5342 break;
5343 }
5344
5345 case ISD::CTTZ: {
5346 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005347 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005348 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5349 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005350 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5351 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005352 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5353 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5354
5355 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5356 Hi = DAG.getConstant(0, NVT);
5357 break;
5358 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005359
Nate Begemanacc398c2006-01-25 18:21:52 +00005360 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005361 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5362 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005363 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5364 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5365
5366 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005367 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005368 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5369 if (!TLI.isLittleEndian())
5370 std::swap(Lo, Hi);
5371 break;
5372 }
5373
Chris Lattner3e928bb2005-01-07 07:47:09 +00005374 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005375 LoadSDNode *LD = cast<LoadSDNode>(Node);
5376 SDOperand Ch = LD->getChain(); // Legalize the chain.
5377 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5378 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005379 int SVOffset = LD->getSrcValueOffset();
5380 unsigned Alignment = LD->getAlignment();
5381 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005382
Evan Cheng466685d2006-10-09 20:57:25 +00005383 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005384 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5385 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005386 if (VT == MVT::f32 || VT == MVT::f64) {
5387 // f32->i32 or f64->i64 one to one expansion.
5388 // Remember that we legalized the chain.
5389 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005390 // Recursively expand the new load.
5391 if (getTypeAction(NVT) == Expand)
5392 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005393 break;
5394 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005395
Evan Cheng466685d2006-10-09 20:57:25 +00005396 // Increment the pointer to the other half.
5397 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5398 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5399 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005400 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005401 if (Alignment > IncrementSize)
5402 Alignment = IncrementSize;
5403 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5404 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005405
Evan Cheng466685d2006-10-09 20:57:25 +00005406 // Build a factor node to remember that this load is independent of the
5407 // other one.
5408 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5409 Hi.getValue(1));
5410
5411 // Remember that we legalized the chain.
5412 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5413 if (!TLI.isLittleEndian())
5414 std::swap(Lo, Hi);
5415 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005416 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005417
5418 if (VT == MVT::f64 && EVT == MVT::f32) {
5419 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5420 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005421 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005422 // Remember that we legalized the chain.
5423 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5424 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5425 break;
5426 }
Evan Cheng466685d2006-10-09 20:57:25 +00005427
5428 if (EVT == NVT)
5429 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005430 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005431 else
5432 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005433 SVOffset, EVT, isVolatile,
5434 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005435
5436 // Remember that we legalized the chain.
5437 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5438
5439 if (ExtType == ISD::SEXTLOAD) {
5440 // The high part is obtained by SRA'ing all but one of the bits of the
5441 // lo part.
5442 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5443 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5444 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5445 } else if (ExtType == ISD::ZEXTLOAD) {
5446 // The high part is just a zero.
5447 Hi = DAG.getConstant(0, NVT);
5448 } else /* if (ExtType == ISD::EXTLOAD) */ {
5449 // The high part is undefined.
5450 Hi = DAG.getNode(ISD::UNDEF, NVT);
5451 }
5452 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005453 break;
5454 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005455 case ISD::AND:
5456 case ISD::OR:
5457 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5458 SDOperand LL, LH, RL, RH;
5459 ExpandOp(Node->getOperand(0), LL, LH);
5460 ExpandOp(Node->getOperand(1), RL, RH);
5461 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5462 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5463 break;
5464 }
5465 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005466 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005467 ExpandOp(Node->getOperand(1), LL, LH);
5468 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005469 if (getTypeAction(NVT) == Expand)
5470 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005471 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005472 if (VT != MVT::f32)
5473 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005474 break;
5475 }
Nate Begeman9373a812005-08-10 20:51:12 +00005476 case ISD::SELECT_CC: {
5477 SDOperand TL, TH, FL, FH;
5478 ExpandOp(Node->getOperand(2), TL, TH);
5479 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005480 if (getTypeAction(NVT) == Expand)
5481 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005482 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5483 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005484 if (VT != MVT::f32)
5485 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5486 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005487 break;
5488 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005489 case ISD::ANY_EXTEND:
5490 // The low part is any extension of the input (which degenerates to a copy).
5491 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5492 // The high part is undefined.
5493 Hi = DAG.getNode(ISD::UNDEF, NVT);
5494 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005495 case ISD::SIGN_EXTEND: {
5496 // The low part is just a sign extension of the input (which degenerates to
5497 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005498 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005499
Chris Lattner3e928bb2005-01-07 07:47:09 +00005500 // The high part is obtained by SRA'ing all but one of the bits of the lo
5501 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005502 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005503 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5504 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005505 break;
5506 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005507 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005508 // The low part is just a zero extension of the input (which degenerates to
5509 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005510 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005511
Chris Lattner3e928bb2005-01-07 07:47:09 +00005512 // The high part is just a zero.
5513 Hi = DAG.getConstant(0, NVT);
5514 break;
Chris Lattner35481892005-12-23 00:16:34 +00005515
Chris Lattner4c948eb2007-02-13 23:55:16 +00005516 case ISD::TRUNCATE: {
5517 // The input value must be larger than this value. Expand *it*.
5518 SDOperand NewLo;
5519 ExpandOp(Node->getOperand(0), NewLo, Hi);
5520
5521 // The low part is now either the right size, or it is closer. If not the
5522 // right size, make an illegal truncate so we recursively expand it.
5523 if (NewLo.getValueType() != Node->getValueType(0))
5524 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5525 ExpandOp(NewLo, Lo, Hi);
5526 break;
5527 }
5528
Chris Lattner35481892005-12-23 00:16:34 +00005529 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005530 SDOperand Tmp;
5531 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5532 // If the target wants to, allow it to lower this itself.
5533 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5534 case Expand: assert(0 && "cannot expand FP!");
5535 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5536 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5537 }
5538 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5539 }
5540
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005541 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005542 if (VT == MVT::f32 || VT == MVT::f64) {
5543 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005544 if (getTypeAction(NVT) == Expand)
5545 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005546 break;
5547 }
5548
5549 // If source operand will be expanded to the same type as VT, i.e.
5550 // i64 <- f64, i32 <- f32, expand the source operand instead.
5551 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5552 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5553 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005554 break;
5555 }
5556
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005557 // Turn this into a load/store pair by default.
5558 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005559 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005560
Chris Lattner35481892005-12-23 00:16:34 +00005561 ExpandOp(Tmp, Lo, Hi);
5562 break;
5563 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005564
Chris Lattner8137c9e2006-01-28 05:07:51 +00005565 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005566 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5567 TargetLowering::Custom &&
5568 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005569 Lo = TLI.LowerOperation(Op, DAG);
5570 assert(Lo.Val && "Node must be custom expanded!");
5571 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005572 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005573 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005574 break;
5575
Chris Lattner4e6c7462005-01-08 19:27:05 +00005576 // These operators cannot be expanded directly, emit them as calls to
5577 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005578 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005579 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005580 SDOperand Op;
5581 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5582 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005583 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5584 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005585 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005586
Chris Lattnerf20d1832005-07-30 01:40:57 +00005587 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5588
Chris Lattner80a3e942005-07-29 00:33:32 +00005589 // Now that the custom expander is done, expand the result, which is still
5590 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005591 if (Op.Val) {
5592 ExpandOp(Op, Lo, Hi);
5593 break;
5594 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005595 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005596
Dale Johannesen161e8972007-10-05 20:04:43 +00005597 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005598 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005599 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005600 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005601 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005602 else if (Node->getOperand(0).getValueType() == MVT::f80)
5603 LC = RTLIB::FPTOSINT_F80_I64;
5604 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5605 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005606 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5607 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005608 break;
Evan Cheng56966222007-01-12 02:11:51 +00005609 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005610
Evan Cheng56966222007-01-12 02:11:51 +00005611 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005612 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005613 SDOperand Op;
5614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5615 case Expand: assert(0 && "cannot expand FP!");
5616 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5617 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5618 }
5619
5620 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5621
5622 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005623 if (Op.Val) {
5624 ExpandOp(Op, Lo, Hi);
5625 break;
5626 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005627 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005628
Evan Chengdaccea182007-10-05 01:09:32 +00005629 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005630 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005631 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005632 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005633 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005634 else if (Node->getOperand(0).getValueType() == MVT::f80)
5635 LC = RTLIB::FPTOUINT_F80_I64;
5636 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5637 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005638 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5639 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005640 break;
Evan Cheng56966222007-01-12 02:11:51 +00005641 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005642
Evan Cheng05a2d562006-01-09 18:31:59 +00005643 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005644 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005645 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005646 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005647 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005648 Op = TLI.LowerOperation(Op, DAG);
5649 if (Op.Val) {
5650 // Now that the custom expander is done, expand the result, which is
5651 // still VT.
5652 ExpandOp(Op, Lo, Hi);
5653 break;
5654 }
5655 }
5656
Chris Lattner79980b02006-09-13 03:50:39 +00005657 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5658 // this X << 1 as X+X.
5659 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5660 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5661 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5662 SDOperand LoOps[2], HiOps[3];
5663 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5664 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5665 LoOps[1] = LoOps[0];
5666 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5667
5668 HiOps[1] = HiOps[0];
5669 HiOps[2] = Lo.getValue(1);
5670 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5671 break;
5672 }
5673 }
5674
Chris Lattnere34b3962005-01-19 04:19:40 +00005675 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005676 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005677 break;
Chris Lattner47599822005-04-02 03:38:53 +00005678
5679 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005680 TargetLowering::LegalizeAction Action =
5681 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5682 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5683 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005684 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005685 break;
5686 }
5687
Chris Lattnere34b3962005-01-19 04:19:40 +00005688 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005689 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5690 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005691 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005692 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005693
Evan Cheng05a2d562006-01-09 18:31:59 +00005694 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005695 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005696 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005697 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005698 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005699 Op = TLI.LowerOperation(Op, DAG);
5700 if (Op.Val) {
5701 // Now that the custom expander is done, expand the result, which is
5702 // still VT.
5703 ExpandOp(Op, Lo, Hi);
5704 break;
5705 }
5706 }
5707
Chris Lattnere34b3962005-01-19 04:19:40 +00005708 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005709 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005710 break;
Chris Lattner47599822005-04-02 03:38:53 +00005711
5712 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005713 TargetLowering::LegalizeAction Action =
5714 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5715 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5716 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005717 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005718 break;
5719 }
5720
Chris Lattnere34b3962005-01-19 04:19:40 +00005721 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005722 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5723 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005724 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005725 }
5726
5727 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005728 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005729 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005730 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005731 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005732 Op = TLI.LowerOperation(Op, DAG);
5733 if (Op.Val) {
5734 // Now that the custom expander is done, expand the result, which is
5735 // still VT.
5736 ExpandOp(Op, Lo, Hi);
5737 break;
5738 }
5739 }
5740
Chris Lattnere34b3962005-01-19 04:19:40 +00005741 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005742 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005743 break;
Chris Lattner47599822005-04-02 03:38:53 +00005744
5745 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005746 TargetLowering::LegalizeAction Action =
5747 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5748 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5749 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005750 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005751 break;
5752 }
5753
Chris Lattnere34b3962005-01-19 04:19:40 +00005754 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005755 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5756 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005757 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005758 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005759
Misha Brukmanedf128a2005-04-21 22:36:52 +00005760 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005761 case ISD::SUB: {
5762 // If the target wants to custom expand this, let them.
5763 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5764 TargetLowering::Custom) {
5765 Op = TLI.LowerOperation(Op, DAG);
5766 if (Op.Val) {
5767 ExpandOp(Op, Lo, Hi);
5768 break;
5769 }
5770 }
5771
5772 // Expand the subcomponents.
5773 SDOperand LHSL, LHSH, RHSL, RHSH;
5774 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5775 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005776 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5777 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005778 LoOps[0] = LHSL;
5779 LoOps[1] = RHSL;
5780 HiOps[0] = LHSH;
5781 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005782 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005783 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005784 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005785 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005786 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005787 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005788 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005789 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005790 }
Chris Lattner84f67882005-01-20 18:52:28 +00005791 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005792 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005793
5794 case ISD::ADDC:
5795 case ISD::SUBC: {
5796 // Expand the subcomponents.
5797 SDOperand LHSL, LHSH, RHSL, RHSH;
5798 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5799 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5800 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5801 SDOperand LoOps[2] = { LHSL, RHSL };
5802 SDOperand HiOps[3] = { LHSH, RHSH };
5803
5804 if (Node->getOpcode() == ISD::ADDC) {
5805 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5806 HiOps[2] = Lo.getValue(1);
5807 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5808 } else {
5809 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5810 HiOps[2] = Lo.getValue(1);
5811 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5812 }
5813 // Remember that we legalized the flag.
5814 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5815 break;
5816 }
5817 case ISD::ADDE:
5818 case ISD::SUBE: {
5819 // Expand the subcomponents.
5820 SDOperand LHSL, LHSH, RHSL, RHSH;
5821 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5822 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5823 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5824 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5825 SDOperand HiOps[3] = { LHSH, RHSH };
5826
5827 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5828 HiOps[2] = Lo.getValue(1);
5829 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5830
5831 // Remember that we legalized the flag.
5832 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5833 break;
5834 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005835 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005836 // If the target wants to custom expand this, let them.
5837 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005838 SDOperand New = TLI.LowerOperation(Op, DAG);
5839 if (New.Val) {
5840 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005841 break;
5842 }
5843 }
5844
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005845 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5846 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00005847 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5848 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5849 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005850 SDOperand LL, LH, RL, RH;
5851 ExpandOp(Node->getOperand(0), LL, LH);
5852 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00005853 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5854 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5855 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5856 // FIXME: generalize this to handle other bit sizes
5857 if (LHSSB == 32 && RHSSB == 32 &&
5858 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5859 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5860 // The inputs are both zero-extended.
5861 if (HasUMUL_LOHI) {
5862 // We can emit a umul_lohi.
5863 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5864 Hi = SDOperand(Lo.Val, 1);
5865 break;
5866 }
5867 if (HasMULHU) {
5868 // We can emit a mulhu+mul.
5869 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5870 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5871 break;
5872 }
Chris Lattnera89654b2006-09-16 00:21:44 +00005873 break;
Dan Gohman525178c2007-10-08 18:33:35 +00005874 }
5875 if (LHSSB > BitSize && RHSSB > BitSize) {
5876 // The input values are both sign-extended.
5877 if (HasSMUL_LOHI) {
5878 // We can emit a smul_lohi.
5879 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5880 Hi = SDOperand(Lo.Val, 1);
5881 break;
5882 }
5883 if (HasMULHS) {
5884 // We can emit a mulhs+mul.
5885 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5886 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5887 break;
5888 }
5889 }
5890 if (HasUMUL_LOHI) {
5891 // Lo,Hi = umul LHS, RHS.
5892 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5893 DAG.getVTList(NVT, NVT), LL, RL);
5894 Lo = UMulLOHI;
5895 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00005896 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5897 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5898 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5899 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005900 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005901 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005902 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005903
Dan Gohman525178c2007-10-08 18:33:35 +00005904 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005905 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5906 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005907 break;
5908 }
Evan Cheng56966222007-01-12 02:11:51 +00005909 case ISD::SDIV:
5910 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5911 break;
5912 case ISD::UDIV:
5913 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5914 break;
5915 case ISD::SREM:
5916 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5917 break;
5918 case ISD::UREM:
5919 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5920 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005921
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005922 case ISD::FADD:
Dale Johannesen161e8972007-10-05 20:04:43 +00005923 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5924 VT == MVT::f64 ? RTLIB::ADD_F64 :
5925 VT == MVT::ppcf128 ?
5926 RTLIB::ADD_PPCF128 :
5927 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005928 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005929 break;
5930 case ISD::FSUB:
Dale Johannesen161e8972007-10-05 20:04:43 +00005931 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5932 VT == MVT::f64 ? RTLIB::SUB_F64 :
5933 VT == MVT::ppcf128 ?
5934 RTLIB::SUB_PPCF128 :
5935 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005936 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005937 break;
5938 case ISD::FMUL:
Dale Johannesen161e8972007-10-05 20:04:43 +00005939 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5940 VT == MVT::f64 ? RTLIB::MUL_F64 :
5941 VT == MVT::ppcf128 ?
5942 RTLIB::MUL_PPCF128 :
5943 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005944 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005945 break;
5946 case ISD::FDIV:
Dale Johannesen161e8972007-10-05 20:04:43 +00005947 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5948 VT == MVT::f64 ? RTLIB::DIV_F64 :
5949 VT == MVT::ppcf128 ?
5950 RTLIB::DIV_PPCF128 :
5951 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005952 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005953 break;
5954 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005955 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005956 break;
5957 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005958 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005959 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005960 case ISD::FPOWI:
Dale Johannesen317096a2007-09-28 01:08:20 +00005961 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
5962 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesen161e8972007-10-05 20:04:43 +00005963 (VT == MVT::f80) ? RTLIB::POWI_F80 :
5964 (VT == MVT::ppcf128) ?
5965 RTLIB::POWI_PPCF128 :
5966 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005967 Node, false, Hi);
5968 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005969 case ISD::FSQRT:
5970 case ISD::FSIN:
5971 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005972 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005973 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005974 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00005975 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00005976 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
5977 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
5978 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
5979 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00005980 break;
5981 case ISD::FSIN:
5982 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5983 break;
5984 case ISD::FCOS:
5985 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5986 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005987 default: assert(0 && "Unreachable!");
5988 }
Evan Cheng56966222007-01-12 02:11:51 +00005989 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005990 break;
5991 }
Evan Cheng966bf242006-12-16 00:52:40 +00005992 case ISD::FABS: {
5993 SDOperand Mask = (VT == MVT::f64)
5994 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5995 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5996 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5997 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5998 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5999 if (getTypeAction(NVT) == Expand)
6000 ExpandOp(Lo, Lo, Hi);
6001 break;
6002 }
6003 case ISD::FNEG: {
6004 SDOperand Mask = (VT == MVT::f64)
6005 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6006 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6007 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6008 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6009 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6010 if (getTypeAction(NVT) == Expand)
6011 ExpandOp(Lo, Lo, Hi);
6012 break;
6013 }
Evan Cheng912095b2007-01-04 21:56:39 +00006014 case ISD::FCOPYSIGN: {
6015 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6016 if (getTypeAction(NVT) == Expand)
6017 ExpandOp(Lo, Lo, Hi);
6018 break;
6019 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006020 case ISD::SINT_TO_FP:
6021 case ISD::UINT_TO_FP: {
6022 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6023 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng64f638d2007-09-27 07:35:39 +00006024 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006025 if (Node->getOperand(0).getValueType() == MVT::i64) {
6026 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006027 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006028 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006029 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006030 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006031 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006032 LC = RTLIB::SINTTOFP_I64_F80;
6033 }
6034 else if (VT == MVT::ppcf128) {
6035 assert(isSigned);
6036 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006037 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006038 } else {
6039 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006040 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006041 else
Evan Cheng56966222007-01-12 02:11:51 +00006042 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006043 }
6044
6045 // Promote the operand if needed.
6046 if (getTypeAction(SrcVT) == Promote) {
6047 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6048 Tmp = isSigned
6049 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6050 DAG.getValueType(SrcVT))
6051 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6052 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6053 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006054
6055 const char *LibCall = TLI.getLibcallName(LC);
6056 if (LibCall)
6057 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6058 else {
6059 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6060 Node->getOperand(0));
6061 if (getTypeAction(Lo.getValueType()) == Expand)
6062 ExpandOp(Lo, Lo, Hi);
6063 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006064 break;
6065 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006066 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006067
Chris Lattner83397362005-12-21 18:02:52 +00006068 // Make sure the resultant values have been legalized themselves, unless this
6069 // is a type that requires multi-step expansion.
6070 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6071 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006072 if (Hi.Val)
6073 // Don't legalize the high part if it is expanded to a single node.
6074 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006075 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006076
6077 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006078 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006079 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006080}
6081
Dan Gohman7f321562007-06-25 16:23:39 +00006082/// SplitVectorOp - Given an operand of vector type, break it down into
6083/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006084void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6085 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006086 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006087 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006088 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006089 assert(NumElements > 1 && "Cannot split a single element vector!");
6090 unsigned NewNumElts = NumElements/2;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006091 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00006092 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00006093
6094 // See if we already split it.
6095 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6096 = SplitNodes.find(Op);
6097 if (I != SplitNodes.end()) {
6098 Lo = I->second.first;
6099 Hi = I->second.second;
6100 return;
6101 }
6102
6103 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006104 default:
6105#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006106 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006107#endif
6108 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00006109 case ISD::BUILD_PAIR:
6110 Lo = Node->getOperand(0);
6111 Hi = Node->getOperand(1);
6112 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006113 case ISD::INSERT_VECTOR_ELT: {
6114 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6115 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6116 SDOperand ScalarOp = Node->getOperand(1);
6117 if (Index < NewNumElts)
6118 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6119 DAG.getConstant(Index, TLI.getPointerTy()));
6120 else
6121 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6122 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6123 break;
6124 }
Dan Gohman7f321562007-06-25 16:23:39 +00006125 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006126 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6127 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00006128 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006129
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006130 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00006131 Node->op_end());
6132 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006133 break;
6134 }
Dan Gohman7f321562007-06-25 16:23:39 +00006135 case ISD::CONCAT_VECTORS: {
6136 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6137 if (NewNumSubvectors == 1) {
6138 Lo = Node->getOperand(0);
6139 Hi = Node->getOperand(1);
6140 } else {
6141 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6142 Node->op_begin()+NewNumSubvectors);
6143 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006144
Dan Gohman7f321562007-06-25 16:23:39 +00006145 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6146 Node->op_end());
6147 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6148 }
Dan Gohman65956352007-06-13 15:12:02 +00006149 break;
6150 }
Dan Gohman7f321562007-06-25 16:23:39 +00006151 case ISD::ADD:
6152 case ISD::SUB:
6153 case ISD::MUL:
6154 case ISD::FADD:
6155 case ISD::FSUB:
6156 case ISD::FMUL:
6157 case ISD::SDIV:
6158 case ISD::UDIV:
6159 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006160 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006161 case ISD::AND:
6162 case ISD::OR:
6163 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006164 SDOperand LL, LH, RL, RH;
6165 SplitVectorOp(Node->getOperand(0), LL, LH);
6166 SplitVectorOp(Node->getOperand(1), RL, RH);
6167
Dan Gohman7f321562007-06-25 16:23:39 +00006168 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6169 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006170 break;
6171 }
Dan Gohman82669522007-10-11 23:57:53 +00006172 case ISD::FPOWI: {
6173 SDOperand L, H;
6174 SplitVectorOp(Node->getOperand(0), L, H);
6175
6176 Lo = DAG.getNode(Node->getOpcode(), NewVT, L, Node->getOperand(1));
6177 Hi = DAG.getNode(Node->getOpcode(), NewVT, H, Node->getOperand(1));
6178 break;
6179 }
6180 case ISD::CTTZ:
6181 case ISD::CTLZ:
6182 case ISD::CTPOP:
6183 case ISD::FNEG:
6184 case ISD::FABS:
6185 case ISD::FSQRT:
6186 case ISD::FSIN:
6187 case ISD::FCOS: {
6188 SDOperand L, H;
6189 SplitVectorOp(Node->getOperand(0), L, H);
6190
6191 Lo = DAG.getNode(Node->getOpcode(), NewVT, L);
6192 Hi = DAG.getNode(Node->getOpcode(), NewVT, H);
6193 break;
6194 }
Dan Gohman7f321562007-06-25 16:23:39 +00006195 case ISD::LOAD: {
6196 LoadSDNode *LD = cast<LoadSDNode>(Node);
6197 SDOperand Ch = LD->getChain();
6198 SDOperand Ptr = LD->getBasePtr();
6199 const Value *SV = LD->getSrcValue();
6200 int SVOffset = LD->getSrcValueOffset();
6201 unsigned Alignment = LD->getAlignment();
6202 bool isVolatile = LD->isVolatile();
6203
6204 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6205 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006206 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6207 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006208 SVOffset += IncrementSize;
6209 if (Alignment > IncrementSize)
6210 Alignment = IncrementSize;
6211 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006212
6213 // Build a factor node to remember that this load is independent of the
6214 // other one.
6215 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6216 Hi.getValue(1));
6217
6218 // Remember that we legalized the chain.
6219 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006220 break;
6221 }
Dan Gohman7f321562007-06-25 16:23:39 +00006222 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006223 // We know the result is a vector. The input may be either a vector or a
6224 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006225 SDOperand InOp = Node->getOperand(0);
6226 if (!MVT::isVector(InOp.getValueType()) ||
6227 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6228 // The input is a scalar or single-element vector.
6229 // Lower to a store/load so that it can be split.
6230 // FIXME: this could be improved probably.
6231 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006232
Evan Cheng786225a2006-10-05 23:01:46 +00006233 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006234 InOp, Ptr, NULL, 0);
6235 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006236 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006237 // Split the vector and convert each of the pieces now.
6238 SplitVectorOp(InOp, Lo, Hi);
6239 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6240 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6241 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006242 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006243 }
6244
6245 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006246 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006247 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006248 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006249}
6250
6251
Dan Gohman89b20c02007-06-27 14:06:22 +00006252/// ScalarizeVectorOp - Given an operand of single-element vector type
6253/// (e.g. v1f32), convert it into the equivalent operation that returns a
6254/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006255SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6256 assert(MVT::isVector(Op.getValueType()) &&
6257 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006258 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006259 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6260 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006261
Dan Gohman7f321562007-06-25 16:23:39 +00006262 // See if we already scalarized it.
6263 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6264 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006265
6266 SDOperand Result;
6267 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006268 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006269#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006270 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006271#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006272 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6273 case ISD::ADD:
6274 case ISD::FADD:
6275 case ISD::SUB:
6276 case ISD::FSUB:
6277 case ISD::MUL:
6278 case ISD::FMUL:
6279 case ISD::SDIV:
6280 case ISD::UDIV:
6281 case ISD::FDIV:
6282 case ISD::SREM:
6283 case ISD::UREM:
6284 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006285 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006286 case ISD::AND:
6287 case ISD::OR:
6288 case ISD::XOR:
6289 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006290 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006291 ScalarizeVectorOp(Node->getOperand(0)),
6292 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006293 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006294 case ISD::FNEG:
6295 case ISD::FABS:
6296 case ISD::FSQRT:
6297 case ISD::FSIN:
6298 case ISD::FCOS:
6299 Result = DAG.getNode(Node->getOpcode(),
6300 NewVT,
6301 ScalarizeVectorOp(Node->getOperand(0)));
6302 break;
6303 case ISD::LOAD: {
6304 LoadSDNode *LD = cast<LoadSDNode>(Node);
6305 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6306 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006307
Dan Gohman7f321562007-06-25 16:23:39 +00006308 const Value *SV = LD->getSrcValue();
6309 int SVOffset = LD->getSrcValueOffset();
6310 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6311 LD->isVolatile(), LD->getAlignment());
6312
Chris Lattnerc7029802006-03-18 01:44:44 +00006313 // Remember that we legalized the chain.
6314 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6315 break;
6316 }
Dan Gohman7f321562007-06-25 16:23:39 +00006317 case ISD::BUILD_VECTOR:
6318 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006319 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006320 case ISD::INSERT_VECTOR_ELT:
6321 // Returning the inserted scalar element.
6322 Result = Node->getOperand(1);
6323 break;
6324 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006325 assert(Node->getOperand(0).getValueType() == NewVT &&
6326 "Concat of non-legal vectors not yet supported!");
6327 Result = Node->getOperand(0);
6328 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006329 case ISD::VECTOR_SHUFFLE: {
6330 // Figure out if the scalar is the LHS or RHS and return it.
6331 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6332 if (cast<ConstantSDNode>(EltNum)->getValue())
6333 Result = ScalarizeVectorOp(Node->getOperand(1));
6334 else
6335 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006336 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006337 }
6338 case ISD::EXTRACT_SUBVECTOR:
6339 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006340 assert(Result.getValueType() == NewVT);
6341 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006342 case ISD::BIT_CONVERT:
6343 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006344 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006345 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006346 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006347 ScalarizeVectorOp(Op.getOperand(1)),
6348 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006349 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006350 }
6351
6352 if (TLI.isTypeLegal(NewVT))
6353 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006354 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6355 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006356 return Result;
6357}
6358
Chris Lattner3e928bb2005-01-07 07:47:09 +00006359
6360// SelectionDAG::Legalize - This is the entry point for the file.
6361//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006362void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006363 if (ViewLegalizeDAGs) viewGraph();
6364
Chris Lattner3e928bb2005-01-07 07:47:09 +00006365 /// run - This is the main entry point to this class.
6366 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006367 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006368}
6369