blob: da2c1dccb5968beb430d99f66b816a493c71cb2a [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
Reid Spencer47857812006-12-31 05:55:36 +0000196 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000197 SDOperand &Hi);
198 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
199 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000200
Chris Lattner35481892005-12-23 00:16:34 +0000201 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000202 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000203 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000204 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
205 SDOperand LegalOp,
206 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000207 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
208 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000209 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000211
Chris Lattner456a93a2006-01-28 07:39:30 +0000212 SDOperand ExpandBSWAP(SDOperand Op);
213 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000214 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
215 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000216 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000218
Dan Gohman7f321562007-06-25 16:23:39 +0000219 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000220 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000221
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222 SDOperand getIntPtrConstant(uint64_t Val) {
223 return DAG.getConstant(Val, TLI.getPointerTy());
224 }
225};
226}
227
Chris Lattner4352cc92006-04-04 17:23:26 +0000228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000252 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000278}
279
Chris Lattnere10e6f72007-06-18 21:28:10 +0000280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000288
Chris Lattnere10e6f72007-06-18 21:28:10 +0000289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
Chris Lattner32fca002005-10-06 01:20:27 +0000297 }
298
Chris Lattnere10e6f72007-06-18 21:28:10 +0000299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
312 Worklist.push_back(*UI);
313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000319}
320
Chris Lattner1618beb2005-07-29 00:11:56 +0000321
Chris Lattner3e928bb2005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
Chris Lattnerab510a72005-10-02 17:49:46 +0000326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000332 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000333 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000334
Chris Lattnerc7029802006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000346 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000347 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000350 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351}
352
Chris Lattner6831a812006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000414 if (N == Dest) return true; // N certainly leads to Dest :)
415
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
Chris Lattner6831a812006-02-13 09:18:02 +0000420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000440
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
Chris Lattner6831a812006-02-13 09:18:02 +0000445
446 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000447 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000448 return false;
449}
450
Dan Gohman7f321562007-06-25 16:23:39 +0000451/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000456 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000459 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000463 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000466 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000471 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000476 }
477 break;
478 }
479}
Chris Lattner6831a812006-02-13 09:18:02 +0000480
Evan Cheng9f877882006-12-13 20:57:08 +0000481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000484 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
490 // double.
491 MVT::ValueType VT = CFP->getValueType(0);
492 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
499 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000500 }
501
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000502 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000503 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000504 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000505 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
506 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
507 VT = MVT::f32;
508 Extend = true;
509 }
510
511 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
512 if (Extend) {
513 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
514 CPIdx, NULL, 0, MVT::f32);
515 } else {
516 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
517 }
518}
519
Chris Lattner6831a812006-02-13 09:18:02 +0000520
Evan Cheng912095b2007-01-04 21:56:39 +0000521/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
522/// operations.
523static
524SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
525 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000526 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000527 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000528 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
529 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000530 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000531
Evan Cheng912095b2007-01-04 21:56:39 +0000532 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000534 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
535 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000536 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000537 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000538 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000539 // Shift right or sign-extend it if the two operands have different types.
540 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
541 if (SizeDiff > 0) {
542 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
543 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
544 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
545 } else if (SizeDiff < 0)
546 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000547
548 // Clear the sign bit of first operand.
549 SDOperand Mask2 = (VT == MVT::f64)
550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
552 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000553 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000554 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
555
556 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000557 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
558 return Result;
559}
560
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000561/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
562static
563SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
564 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000565 SDOperand Chain = ST->getChain();
566 SDOperand Ptr = ST->getBasePtr();
567 SDOperand Val = ST->getValue();
568 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000569 int Alignment = ST->getAlignment();
570 int SVOffset = ST->getSrcValueOffset();
571 if (MVT::isFloatingPoint(ST->getStoredVT())) {
572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
574 MVT::ValueType intVT;
575 if (VT==MVT::f64)
576 intVT = MVT::i64;
577 else if (VT==MVT::f32)
578 intVT = MVT::i32;
579 else
580 assert(0 && "Unaligned load of unsupported floating point type");
581
582 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
583 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
584 SVOffset, ST->isVolatile(), Alignment);
585 }
586 assert(MVT::isInteger(ST->getStoredVT()) &&
587 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000588 // Get the half-size VT
589 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
590 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000591 int IncrementSize = NumBits / 8;
592
593 // Divide the stored value in two parts.
594 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
595 SDOperand Lo = Val;
596 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
597
598 // Store the two parts
599 SDOperand Store1, Store2;
600 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
601 ST->getSrcValue(), SVOffset, NewStoredVT,
602 ST->isVolatile(), Alignment);
603 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
604 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
605 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
606 ST->getSrcValue(), SVOffset + IncrementSize,
607 NewStoredVT, ST->isVolatile(), Alignment);
608
609 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
610}
611
612/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
613static
614SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
615 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616 int SVOffset = LD->getSrcValueOffset();
617 SDOperand Chain = LD->getChain();
618 SDOperand Ptr = LD->getBasePtr();
619 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000620 MVT::ValueType LoadedVT = LD->getLoadedVT();
621 if (MVT::isFloatingPoint(VT)) {
622 // Expand to a (misaligned) integer load of the same size,
623 // then bitconvert to floating point.
624 MVT::ValueType intVT;
625 if (LoadedVT==MVT::f64)
626 intVT = MVT::i64;
627 else if (LoadedVT==MVT::f32)
628 intVT = MVT::i32;
629 else
630 assert(0 && "Unaligned load of unsupported floating point type");
631
632 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
633 SVOffset, LD->isVolatile(),
634 LD->getAlignment());
635 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
636 if (LoadedVT != VT)
637 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
638
639 SDOperand Ops[] = { Result, Chain };
640 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
641 Ops, 2);
642 }
643 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
644 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000645 int NumBits = MVT::getSizeInBits(NewLoadedVT);
646 int Alignment = LD->getAlignment();
647 int IncrementSize = NumBits / 8;
648 ISD::LoadExtType HiExtType = LD->getExtensionType();
649
650 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
651 if (HiExtType == ISD::NON_EXTLOAD)
652 HiExtType = ISD::ZEXTLOAD;
653
654 // Load the value in two parts
655 SDOperand Lo, Hi;
656 if (TLI.isLittleEndian()) {
657 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
658 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
659 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
660 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
661 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
662 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
663 Alignment);
664 } else {
665 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
666 NewLoadedVT,LD->isVolatile(), Alignment);
667 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
668 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
669 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
670 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
671 Alignment);
672 }
673
674 // aggregate the two parts
675 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
676 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
677 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
678
679 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
680 Hi.getValue(1));
681
682 SDOperand Ops[] = { Result, TF };
683 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
684}
Evan Cheng912095b2007-01-04 21:56:39 +0000685
Dan Gohman82669522007-10-11 23:57:53 +0000686/// UnrollVectorOp - We know that the given vector has a legal type, however
687/// the operation it performs is not legal and is an operation that we have
688/// no way of lowering. "Unroll" the vector, splitting out the scalars and
689/// operating on each element individually.
690SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
691 MVT::ValueType VT = Op.getValueType();
692 assert(isTypeLegal(VT) &&
693 "Caller should expand or promote operands that are not legal!");
694 assert(Op.Val->getNumValues() == 1 &&
695 "Can't unroll a vector with multiple results!");
696 unsigned NE = MVT::getVectorNumElements(VT);
697 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
698
699 SmallVector<SDOperand, 8> Scalars;
700 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
701 for (unsigned i = 0; i != NE; ++i) {
702 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
703 SDOperand Operand = Op.getOperand(j);
704 MVT::ValueType OperandVT = Operand.getValueType();
705 if (MVT::isVector(OperandVT)) {
706 // A vector operand; extract a single element.
707 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
708 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
709 OperandEltVT,
710 Operand,
711 DAG.getConstant(i, MVT::i32));
712 } else {
713 // A scalar operand; just use it as is.
714 Operands[j] = Operand;
715 }
716 }
717 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
718 &Operands[0], Operands.size()));
719 }
720
721 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
722}
723
Dan Gohmana3466152007-07-13 20:14:11 +0000724/// LegalizeOp - We know that the specified value has a legal type, and
725/// that its operands are legal. Now ensure that the operation itself
726/// is legal, recursively ensuring that the operands' operations remain
727/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000728SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000729 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
730 return Op;
731
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000732 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000733 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000734 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000735
Chris Lattner3e928bb2005-01-07 07:47:09 +0000736 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000737 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000738 if (Node->getNumValues() > 1) {
739 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000740 if (getTypeAction(Node->getValueType(i)) != Legal) {
741 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000742 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000743 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000744 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000745 }
746 }
747
Chris Lattner45982da2005-05-12 16:53:42 +0000748 // Note that LegalizeOp may be reentered even from single-use nodes, which
749 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000750 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000751 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000752
Nate Begeman9373a812005-08-10 20:51:12 +0000753 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000754 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000755 bool isCustom = false;
756
Chris Lattner3e928bb2005-01-07 07:47:09 +0000757 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000758 case ISD::FrameIndex:
759 case ISD::EntryToken:
760 case ISD::Register:
761 case ISD::BasicBlock:
762 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000763 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000764 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000765 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000766 case ISD::TargetConstantPool:
767 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000768 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000769 case ISD::TargetExternalSymbol:
770 case ISD::VALUETYPE:
771 case ISD::SRCVALUE:
772 case ISD::STRING:
773 case ISD::CONDCODE:
774 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000775 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000776 "This must be legal!");
777 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000779 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
780 // If this is a target node, legalize it by legalizing the operands then
781 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000782 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000783 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000784 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000785
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000786 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000787
788 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
789 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
790 return Result.getValue(Op.ResNo);
791 }
792 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000793#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000794 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000795#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000796 assert(0 && "Do not know how to legalize this operator!");
797 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000798 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000799 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000800 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000801 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000802 case ISD::ConstantPool:
803 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000804 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
805 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 case TargetLowering::Custom:
807 Tmp1 = TLI.LowerOperation(Op, DAG);
808 if (Tmp1.Val) Result = Tmp1;
809 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000810 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000811 break;
812 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000813 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000814 case ISD::FRAMEADDR:
815 case ISD::RETURNADDR:
816 // The only option for these nodes is to custom lower them. If the target
817 // does not custom lower them, then return zero.
818 Tmp1 = TLI.LowerOperation(Op, DAG);
819 if (Tmp1.Val)
820 Result = Tmp1;
821 else
822 Result = DAG.getConstant(0, TLI.getPointerTy());
823 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000824 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000825 MVT::ValueType VT = Node->getValueType(0);
826 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
827 default: assert(0 && "This action is not supported yet!");
828 case TargetLowering::Custom:
829 Result = TLI.LowerOperation(Op, DAG);
830 if (Result.Val) break;
831 // Fall Thru
832 case TargetLowering::Legal:
833 Result = DAG.getConstant(0, VT);
834 break;
835 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000836 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000837 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000838 case ISD::EXCEPTIONADDR: {
839 Tmp1 = LegalizeOp(Node->getOperand(0));
840 MVT::ValueType VT = Node->getValueType(0);
841 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
842 default: assert(0 && "This action is not supported yet!");
843 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000844 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000845 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
846 }
847 break;
848 case TargetLowering::Custom:
849 Result = TLI.LowerOperation(Op, DAG);
850 if (Result.Val) break;
851 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000852 case TargetLowering::Legal: {
853 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
854 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
855 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000856 break;
857 }
858 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000859 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000860 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000861 case ISD::EHSELECTION: {
862 Tmp1 = LegalizeOp(Node->getOperand(0));
863 Tmp2 = LegalizeOp(Node->getOperand(1));
864 MVT::ValueType VT = Node->getValueType(0);
865 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Expand: {
868 unsigned Reg = TLI.getExceptionSelectorRegister();
869 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
870 }
871 break;
872 case TargetLowering::Custom:
873 Result = TLI.LowerOperation(Op, DAG);
874 if (Result.Val) break;
875 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000876 case TargetLowering::Legal: {
877 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
878 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
879 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000880 break;
881 }
882 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000883 }
Jim Laskey8782d482007-02-28 20:43:58 +0000884 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000885 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000886 MVT::ValueType VT = Node->getValueType(0);
887 // The only "good" option for this node is to custom lower it.
888 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
889 default: assert(0 && "This action is not supported at all!");
890 case TargetLowering::Custom:
891 Result = TLI.LowerOperation(Op, DAG);
892 if (Result.Val) break;
893 // Fall Thru
894 case TargetLowering::Legal:
895 // Target does not know, how to lower this, lower to noop
896 Result = LegalizeOp(Node->getOperand(0));
897 break;
898 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000899 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000900 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000901 case ISD::AssertSext:
902 case ISD::AssertZext:
903 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000905 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000906 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000907 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000908 Result = Node->getOperand(Op.ResNo);
909 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000910 case ISD::CopyFromReg:
911 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000912 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000913 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000915 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000916 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000917 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000918 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
920 } else {
921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
922 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000923 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
924 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000925 // Since CopyFromReg produces two values, make sure to remember that we
926 // legalized both of them.
927 AddLegalizedOperand(Op.getValue(0), Result);
928 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
929 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000930 case ISD::UNDEF: {
931 MVT::ValueType VT = Op.getValueType();
932 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000933 default: assert(0 && "This action is not supported yet!");
934 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000935 if (MVT::isInteger(VT))
936 Result = DAG.getConstant(0, VT);
937 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000938 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
939 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000940 else
941 assert(0 && "Unknown value type!");
942 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000943 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000944 break;
945 }
946 break;
947 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000948
Chris Lattner48b61a72006-03-28 00:40:33 +0000949 case ISD::INTRINSIC_W_CHAIN:
950 case ISD::INTRINSIC_WO_CHAIN:
951 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000952 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000953 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
954 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000955 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000956
957 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000958 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000959 TargetLowering::Custom) {
960 Tmp3 = TLI.LowerOperation(Result, DAG);
961 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000962 }
963
964 if (Result.Val->getNumValues() == 1) break;
965
966 // Must have return value and chain result.
967 assert(Result.Val->getNumValues() == 2 &&
968 "Cannot return more than two values!");
969
970 // Since loads produce two values, make sure to remember that we
971 // legalized both of them.
972 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
973 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
974 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000975 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000976
977 case ISD::LOCATION:
978 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
980
981 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
982 case TargetLowering::Promote:
983 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000984 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000985 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000986 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000987 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000988
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000989 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000990 const std::string &FName =
991 cast<StringSDNode>(Node->getOperand(3))->getValue();
992 const std::string &DirName =
993 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000994 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000995
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000996 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000997 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000998 SDOperand LineOp = Node->getOperand(1);
999 SDOperand ColOp = Node->getOperand(2);
1000
1001 if (useDEBUG_LOC) {
1002 Ops.push_back(LineOp); // line #
1003 Ops.push_back(ColOp); // col #
1004 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001005 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001006 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001007 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1008 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001009 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001010 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001011 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001012 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001013 } else {
1014 Result = Tmp1; // chain
1015 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001016 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001017 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001018 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001019 if (Tmp1 != Node->getOperand(0) ||
1020 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001021 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001022 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001023 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1024 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1025 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1026 } else {
1027 // Otherwise promote them.
1028 Ops.push_back(PromoteOp(Node->getOperand(1)));
1029 Ops.push_back(PromoteOp(Node->getOperand(2)));
1030 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001031 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1032 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001033 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001034 }
1035 break;
1036 }
1037 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001038
1039 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001040 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001041 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001042 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001043 case TargetLowering::Legal:
1044 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1046 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1047 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001048 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001049 break;
1050 }
1051 break;
1052
Jim Laskey1ee29252007-01-26 14:34:52 +00001053 case ISD::LABEL:
1054 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1055 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001056 default: assert(0 && "This action is not supported yet!");
1057 case TargetLowering::Legal:
1058 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1059 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001061 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001062 case TargetLowering::Expand:
1063 Result = LegalizeOp(Node->getOperand(0));
1064 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001065 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001066 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001067
Scott Michelc1513d22007-08-08 23:23:31 +00001068 case ISD::Constant: {
1069 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1070 unsigned opAction =
1071 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1072
Chris Lattner3e928bb2005-01-07 07:47:09 +00001073 // We know we don't need to expand constants here, constants only have one
1074 // value and we check that it is fine above.
1075
Scott Michelc1513d22007-08-08 23:23:31 +00001076 if (opAction == TargetLowering::Custom) {
1077 Tmp1 = TLI.LowerOperation(Result, DAG);
1078 if (Tmp1.Val)
1079 Result = Tmp1;
1080 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001081 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001082 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001083 case ISD::ConstantFP: {
1084 // Spill FP immediates to the constant pool if the target cannot directly
1085 // codegen them. Targets often have some immediate values that can be
1086 // efficiently generated into an FP register without a load. We explicitly
1087 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001088 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1089
1090 // Check to see if this FP immediate is already legal.
1091 bool isLegal = false;
1092 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1093 E = TLI.legal_fpimm_end(); I != E; ++I)
1094 if (CFP->isExactlyValue(*I)) {
1095 isLegal = true;
1096 break;
1097 }
1098
Chris Lattner3181a772006-01-29 06:26:56 +00001099 // If this is a legal constant, turn it into a TargetConstantFP node.
1100 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001101 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1102 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001103 break;
1104 }
1105
1106 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1107 default: assert(0 && "This action is not supported yet!");
1108 case TargetLowering::Custom:
1109 Tmp3 = TLI.LowerOperation(Result, DAG);
1110 if (Tmp3.Val) {
1111 Result = Tmp3;
1112 break;
1113 }
1114 // FALLTHROUGH
1115 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001116 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001117 }
1118 break;
1119 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001120 case ISD::TokenFactor:
1121 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001122 Tmp1 = LegalizeOp(Node->getOperand(0));
1123 Tmp2 = LegalizeOp(Node->getOperand(1));
1124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1125 } else if (Node->getNumOperands() == 3) {
1126 Tmp1 = LegalizeOp(Node->getOperand(0));
1127 Tmp2 = LegalizeOp(Node->getOperand(1));
1128 Tmp3 = LegalizeOp(Node->getOperand(2));
1129 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001130 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001131 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001132 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001133 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1134 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001135 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001136 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001137 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001138
1139 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001140 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001141 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001142 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1143 assert(Tmp3.Val && "Target didn't custom lower this node!");
1144 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1145 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001146
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001147 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001148 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001149 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1150 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001151 if (Op.ResNo == i)
1152 Tmp2 = Tmp1;
1153 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1154 }
1155 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001156 case ISD::EXTRACT_SUBREG: {
1157 Tmp1 = LegalizeOp(Node->getOperand(0));
1158 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1159 assert(idx && "Operand must be a constant");
1160 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1161 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1162 }
1163 break;
1164 case ISD::INSERT_SUBREG: {
1165 Tmp1 = LegalizeOp(Node->getOperand(0));
1166 Tmp2 = LegalizeOp(Node->getOperand(1));
1167 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1168 assert(idx && "Operand must be a constant");
1169 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1171 }
1172 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001173 case ISD::BUILD_VECTOR:
1174 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001175 default: assert(0 && "This action is not supported yet!");
1176 case TargetLowering::Custom:
1177 Tmp3 = TLI.LowerOperation(Result, DAG);
1178 if (Tmp3.Val) {
1179 Result = Tmp3;
1180 break;
1181 }
1182 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001183 case TargetLowering::Expand:
1184 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001185 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001186 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001187 break;
1188 case ISD::INSERT_VECTOR_ELT:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1193
1194 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1195 Node->getValueType(0))) {
1196 default: assert(0 && "This action is not supported yet!");
1197 case TargetLowering::Legal:
1198 break;
1199 case TargetLowering::Custom:
1200 Tmp3 = TLI.LowerOperation(Result, DAG);
1201 if (Tmp3.Val) {
1202 Result = Tmp3;
1203 break;
1204 }
1205 // FALLTHROUGH
1206 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001207 // If the insert index is a constant, codegen this as a scalar_to_vector,
1208 // then a shuffle that inserts it into the right position in the vector.
1209 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1210 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1211 Tmp1.getValueType(), Tmp2);
1212
1213 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1214 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001215 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001216
1217 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1218 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1219 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001220 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001221 for (unsigned i = 0; i != NumElts; ++i) {
1222 if (i != InsertPos->getValue())
1223 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1224 else
1225 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1226 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001227 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1228 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001229
1230 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1231 Tmp1, ScVec, ShufMask);
1232 Result = LegalizeOp(Result);
1233 break;
1234 }
1235
Chris Lattner2332b9f2006-03-19 01:17:20 +00001236 // If the target doesn't support this, we have to spill the input vector
1237 // to a temporary stack slot, update the element, then reload it. This is
1238 // badness. We could also load the value into a vector register (either
1239 // with a "move to register" or "extload into register" instruction, then
1240 // permute it into place, if the idx is a constant and if the idx is
1241 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001242 MVT::ValueType VT = Tmp1.getValueType();
1243 MVT::ValueType EltVT = Tmp2.getValueType();
1244 MVT::ValueType IdxVT = Tmp3.getValueType();
1245 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001246 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001247 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001248 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001249
1250 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001251 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1252 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001253 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001254 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1255 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1256 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001257 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001258 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001259 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001260 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001261 break;
1262 }
1263 }
1264 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001265 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001266 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1267 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1268 break;
1269 }
1270
Chris Lattnerce872152006-03-19 06:31:19 +00001271 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1272 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1273 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1274 Node->getValueType(0))) {
1275 default: assert(0 && "This action is not supported yet!");
1276 case TargetLowering::Legal:
1277 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001278 case TargetLowering::Custom:
1279 Tmp3 = TLI.LowerOperation(Result, DAG);
1280 if (Tmp3.Val) {
1281 Result = Tmp3;
1282 break;
1283 }
1284 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001285 case TargetLowering::Expand:
1286 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001287 break;
1288 }
Chris Lattnerce872152006-03-19 06:31:19 +00001289 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001290 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1292 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1294
1295 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001296 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1297 default: assert(0 && "Unknown operation action!");
1298 case TargetLowering::Legal:
1299 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1300 "vector shuffle should not be created if not legal!");
1301 break;
1302 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001303 Tmp3 = TLI.LowerOperation(Result, DAG);
1304 if (Tmp3.Val) {
1305 Result = Tmp3;
1306 break;
1307 }
1308 // FALLTHROUGH
1309 case TargetLowering::Expand: {
1310 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001311 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001312 MVT::ValueType PtrVT = TLI.getPointerTy();
1313 SDOperand Mask = Node->getOperand(2);
1314 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001315 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001316 for (unsigned i = 0; i != NumElems; ++i) {
1317 SDOperand Arg = Mask.getOperand(i);
1318 if (Arg.getOpcode() == ISD::UNDEF) {
1319 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1320 } else {
1321 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1322 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1323 if (Idx < NumElems)
1324 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1325 DAG.getConstant(Idx, PtrVT)));
1326 else
1327 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1328 DAG.getConstant(Idx - NumElems, PtrVT)));
1329 }
1330 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001331 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001332 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001333 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001334 case TargetLowering::Promote: {
1335 // Change base type to a different vector type.
1336 MVT::ValueType OVT = Node->getValueType(0);
1337 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1338
1339 // Cast the two input vectors.
1340 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1341 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1342
1343 // Convert the shuffle mask to the right # elements.
1344 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1345 assert(Tmp3.Val && "Shuffle not legal?");
1346 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1347 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1348 break;
1349 }
Chris Lattner87100e02006-03-20 01:52:29 +00001350 }
1351 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001352
1353 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001354 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001355 Tmp2 = LegalizeOp(Node->getOperand(1));
1356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001357 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001358 break;
1359
Dan Gohman7f321562007-06-25 16:23:39 +00001360 case ISD::EXTRACT_SUBVECTOR:
1361 Tmp1 = Node->getOperand(0);
1362 Tmp2 = LegalizeOp(Node->getOperand(1));
1363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1364 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001365 break;
1366
Chris Lattner6831a812006-02-13 09:18:02 +00001367 case ISD::CALLSEQ_START: {
1368 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1369
1370 // Recursively Legalize all of the inputs of the call end that do not lead
1371 // to this call start. This ensures that any libcalls that need be inserted
1372 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001373 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001374 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001375 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1376 NodesLeadingTo);
1377 }
Chris Lattner6831a812006-02-13 09:18:02 +00001378
1379 // Now that we legalized all of the inputs (which may have inserted
1380 // libcalls) create the new CALLSEQ_START node.
1381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1382
1383 // Merge in the last call, to ensure that this call start after the last
1384 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001385 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001386 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1387 Tmp1 = LegalizeOp(Tmp1);
1388 }
Chris Lattner6831a812006-02-13 09:18:02 +00001389
1390 // Do not try to legalize the target-specific arguments (#1+).
1391 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001392 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001393 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001394 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001395 }
1396
1397 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001398 AddLegalizedOperand(Op.getValue(0), Result);
1399 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1400 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1401
Chris Lattner6831a812006-02-13 09:18:02 +00001402 // Now that the callseq_start and all of the non-call nodes above this call
1403 // sequence have been legalized, legalize the call itself. During this
1404 // process, no libcalls can/will be inserted, guaranteeing that no calls
1405 // can overlap.
1406 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1407 SDOperand InCallSEQ = LastCALLSEQ_END;
1408 // Note that we are selecting this call!
1409 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1410 IsLegalizingCall = true;
1411
1412 // Legalize the call, starting from the CALLSEQ_END.
1413 LegalizeOp(LastCALLSEQ_END);
1414 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1415 return Result;
1416 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001417 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001418 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1419 // will cause this node to be legalized as well as handling libcalls right.
1420 if (LastCALLSEQ_END.Val != Node) {
1421 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001422 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001423 assert(I != LegalizedNodes.end() &&
1424 "Legalizing the call start should have legalized this node!");
1425 return I->second;
1426 }
1427
1428 // Otherwise, the call start has been legalized and everything is going
1429 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001431 // Do not try to legalize the target-specific arguments (#1+), except for
1432 // an optional flag input.
1433 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1434 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001435 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001436 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001437 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001438 }
1439 } else {
1440 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1441 if (Tmp1 != Node->getOperand(0) ||
1442 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001443 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001444 Ops[0] = Tmp1;
1445 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001446 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001447 }
Chris Lattner6a542892006-01-24 05:48:21 +00001448 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001449 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001450 // This finishes up call legalization.
1451 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001452
1453 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1454 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1455 if (Node->getNumValues() == 2)
1456 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1457 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001458 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001459 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1462 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001464
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001465 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001466 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001467 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001468 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001469 case TargetLowering::Expand: {
1470 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1471 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1472 " not tell us which reg is the stack pointer!");
1473 SDOperand Chain = Tmp1.getOperand(0);
1474 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001475 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1476 Chain = SP.getValue(1);
1477 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1478 unsigned StackAlign =
1479 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1480 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001481 SP = DAG.getNode(ISD::AND, VT, SP,
1482 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001483 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1484 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001485 Tmp1 = LegalizeOp(Tmp1);
1486 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001487 break;
1488 }
1489 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001490 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1491 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001492 Tmp1 = LegalizeOp(Tmp3);
1493 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001494 }
Chris Lattner903d2782006-01-15 08:54:32 +00001495 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001496 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001497 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001498 }
Chris Lattner903d2782006-01-15 08:54:32 +00001499 // Since this op produce two values, make sure to remember that we
1500 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001501 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1502 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001503 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001504 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001505 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001506 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001507 bool Changed = false;
1508 // Legalize all of the operands of the inline asm, in case they are nodes
1509 // that need to be expanded or something. Note we skip the asm string and
1510 // all of the TargetConstant flags.
1511 SDOperand Op = LegalizeOp(Ops[0]);
1512 Changed = Op != Ops[0];
1513 Ops[0] = Op;
1514
1515 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1516 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1517 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1518 for (++i; NumVals; ++i, --NumVals) {
1519 SDOperand Op = LegalizeOp(Ops[i]);
1520 if (Op != Ops[i]) {
1521 Changed = true;
1522 Ops[i] = Op;
1523 }
1524 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001525 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001526
1527 if (HasInFlag) {
1528 Op = LegalizeOp(Ops.back());
1529 Changed |= Op != Ops.back();
1530 Ops.back() = Op;
1531 }
1532
1533 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001534 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001535
1536 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001537 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001538 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1539 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001540 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001541 case ISD::BR:
1542 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001543 // Ensure that libcalls are emitted before a branch.
1544 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1545 Tmp1 = LegalizeOp(Tmp1);
1546 LastCALLSEQ_END = DAG.getEntryNode();
1547
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001548 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001549 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001550 case ISD::BRIND:
1551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1552 // Ensure that libcalls are emitted before a branch.
1553 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1554 Tmp1 = LegalizeOp(Tmp1);
1555 LastCALLSEQ_END = DAG.getEntryNode();
1556
1557 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1558 default: assert(0 && "Indirect target must be legal type (pointer)!");
1559 case Legal:
1560 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1561 break;
1562 }
1563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1564 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001565 case ISD::BR_JT:
1566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1567 // Ensure that libcalls are emitted before a branch.
1568 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1569 Tmp1 = LegalizeOp(Tmp1);
1570 LastCALLSEQ_END = DAG.getEntryNode();
1571
1572 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1574
1575 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1576 default: assert(0 && "This action is not supported yet!");
1577 case TargetLowering::Legal: break;
1578 case TargetLowering::Custom:
1579 Tmp1 = TLI.LowerOperation(Result, DAG);
1580 if (Tmp1.Val) Result = Tmp1;
1581 break;
1582 case TargetLowering::Expand: {
1583 SDOperand Chain = Result.getOperand(0);
1584 SDOperand Table = Result.getOperand(1);
1585 SDOperand Index = Result.getOperand(2);
1586
1587 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001588 MachineFunction &MF = DAG.getMachineFunction();
1589 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001590 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1591 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001592
1593 SDOperand LD;
1594 switch (EntrySize) {
1595 default: assert(0 && "Size of jump table not supported yet."); break;
1596 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1597 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1598 }
1599
1600 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001601 // For PIC, the sequence is:
1602 // BRIND(load(Jumptable + index) + RelocBase)
1603 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1604 SDOperand Reloc;
1605 if (TLI.usesGlobalOffsetTable())
1606 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1607 else
1608 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001609 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001610 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1611 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1612 } else {
1613 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1614 }
1615 }
1616 }
1617 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001618 case ISD::BRCOND:
1619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001620 // Ensure that libcalls are emitted before a return.
1621 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1622 Tmp1 = LegalizeOp(Tmp1);
1623 LastCALLSEQ_END = DAG.getEntryNode();
1624
Chris Lattner47e92232005-01-18 19:27:06 +00001625 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1626 case Expand: assert(0 && "It's impossible to expand bools");
1627 case Legal:
1628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1629 break;
1630 case Promote:
1631 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001632
1633 // The top bits of the promoted condition are not necessarily zero, ensure
1634 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001636 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1637 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001638 break;
1639 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001640
1641 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001643
1644 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1645 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001646 case TargetLowering::Legal: break;
1647 case TargetLowering::Custom:
1648 Tmp1 = TLI.LowerOperation(Result, DAG);
1649 if (Tmp1.Val) Result = Tmp1;
1650 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001651 case TargetLowering::Expand:
1652 // Expand brcond's setcc into its constituent parts and create a BR_CC
1653 // Node.
1654 if (Tmp2.getOpcode() == ISD::SETCC) {
1655 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1656 Tmp2.getOperand(0), Tmp2.getOperand(1),
1657 Node->getOperand(2));
1658 } else {
1659 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1660 DAG.getCondCode(ISD::SETNE), Tmp2,
1661 DAG.getConstant(0, Tmp2.getValueType()),
1662 Node->getOperand(2));
1663 }
1664 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001665 }
1666 break;
1667 case ISD::BR_CC:
1668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001669 // Ensure that libcalls are emitted before a branch.
1670 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1671 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001672 Tmp2 = Node->getOperand(2); // LHS
1673 Tmp3 = Node->getOperand(3); // RHS
1674 Tmp4 = Node->getOperand(1); // CC
1675
1676 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001677 LastCALLSEQ_END = DAG.getEntryNode();
1678
Nate Begeman750ac1b2006-02-01 07:19:44 +00001679 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1680 // the LHS is a legal SETCC itself. In this case, we need to compare
1681 // the result against zero to select between true and false values.
1682 if (Tmp3.Val == 0) {
1683 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1684 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001685 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001686
1687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1688 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001689
Chris Lattner181b7a32005-12-17 23:46:46 +00001690 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1691 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001692 case TargetLowering::Legal: break;
1693 case TargetLowering::Custom:
1694 Tmp4 = TLI.LowerOperation(Result, DAG);
1695 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001696 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001697 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001698 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001699 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001700 LoadSDNode *LD = cast<LoadSDNode>(Node);
1701 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1702 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001703
Evan Cheng466685d2006-10-09 20:57:25 +00001704 ISD::LoadExtType ExtType = LD->getExtensionType();
1705 if (ExtType == ISD::NON_EXTLOAD) {
1706 MVT::ValueType VT = Node->getValueType(0);
1707 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1708 Tmp3 = Result.getValue(0);
1709 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001710
Evan Cheng466685d2006-10-09 20:57:25 +00001711 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1712 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001713 case TargetLowering::Legal:
1714 // If this is an unaligned load and the target doesn't support it,
1715 // expand it.
1716 if (!TLI.allowsUnalignedMemoryAccesses()) {
1717 unsigned ABIAlignment = TLI.getTargetData()->
1718 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1719 if (LD->getAlignment() < ABIAlignment){
1720 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1721 TLI);
1722 Tmp3 = Result.getOperand(0);
1723 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001724 Tmp3 = LegalizeOp(Tmp3);
1725 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001726 }
1727 }
1728 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001729 case TargetLowering::Custom:
1730 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1731 if (Tmp1.Val) {
1732 Tmp3 = LegalizeOp(Tmp1);
1733 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001734 }
Evan Cheng466685d2006-10-09 20:57:25 +00001735 break;
1736 case TargetLowering::Promote: {
1737 // Only promote a load of vector type to another.
1738 assert(MVT::isVector(VT) && "Cannot promote this load!");
1739 // Change base type to a different vector type.
1740 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1741
1742 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001743 LD->getSrcValueOffset(),
1744 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001745 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1746 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001747 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001748 }
Evan Cheng466685d2006-10-09 20:57:25 +00001749 }
1750 // Since loads produce two values, make sure to remember that we
1751 // legalized both of them.
1752 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1753 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1754 return Op.ResNo ? Tmp4 : Tmp3;
1755 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001756 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001757 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1758 default: assert(0 && "This action is not supported yet!");
1759 case TargetLowering::Promote:
1760 assert(SrcVT == MVT::i1 &&
1761 "Can only promote extending LOAD from i1 -> i8!");
1762 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1763 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001764 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001765 Tmp1 = Result.getValue(0);
1766 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001767 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001768 case TargetLowering::Custom:
1769 isCustom = true;
1770 // FALLTHROUGH
1771 case TargetLowering::Legal:
1772 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1773 Tmp1 = Result.getValue(0);
1774 Tmp2 = Result.getValue(1);
1775
1776 if (isCustom) {
1777 Tmp3 = TLI.LowerOperation(Result, DAG);
1778 if (Tmp3.Val) {
1779 Tmp1 = LegalizeOp(Tmp3);
1780 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1781 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001782 } else {
1783 // If this is an unaligned load and the target doesn't support it,
1784 // expand it.
1785 if (!TLI.allowsUnalignedMemoryAccesses()) {
1786 unsigned ABIAlignment = TLI.getTargetData()->
1787 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1788 if (LD->getAlignment() < ABIAlignment){
1789 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1790 TLI);
1791 Tmp1 = Result.getOperand(0);
1792 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001793 Tmp1 = LegalizeOp(Tmp1);
1794 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001795 }
1796 }
Evan Cheng466685d2006-10-09 20:57:25 +00001797 }
1798 break;
1799 case TargetLowering::Expand:
1800 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1801 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1802 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001803 LD->getSrcValueOffset(),
1804 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001805 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1806 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1807 Tmp2 = LegalizeOp(Load.getValue(1));
1808 break;
1809 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001810 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001811 // Turn the unsupported load into an EXTLOAD followed by an explicit
1812 // zero/sign extend inreg.
1813 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1814 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001815 LD->getSrcValueOffset(), SrcVT,
1816 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001817 SDOperand ValRes;
1818 if (ExtType == ISD::SEXTLOAD)
1819 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1820 Result, DAG.getValueType(SrcVT));
1821 else
1822 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1823 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1824 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1825 break;
1826 }
1827 // Since loads produce two values, make sure to remember that we legalized
1828 // both of them.
1829 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1830 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1831 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001832 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001833 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001834 case ISD::EXTRACT_ELEMENT: {
1835 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1836 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001837 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001838 case Legal:
1839 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1840 // 1 -> Hi
1841 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1842 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1843 TLI.getShiftAmountTy()));
1844 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1845 } else {
1846 // 0 -> Lo
1847 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1848 Node->getOperand(0));
1849 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001850 break;
1851 case Expand:
1852 // Get both the low and high parts.
1853 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1854 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1855 Result = Tmp2; // 1 -> Hi
1856 else
1857 Result = Tmp1; // 0 -> Lo
1858 break;
1859 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001860 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001861 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001862
1863 case ISD::CopyToReg:
1864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001865
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001866 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001867 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001868 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001869 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001870 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001871 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001872 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001873 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001874 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001875 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1877 Tmp3);
1878 } else {
1879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001880 }
1881
1882 // Since this produces two values, make sure to remember that we legalized
1883 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001884 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001885 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001886 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001887 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001888 break;
1889
1890 case ISD::RET:
1891 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001892
1893 // Ensure that libcalls are emitted before a return.
1894 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1895 Tmp1 = LegalizeOp(Tmp1);
1896 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001897
Chris Lattner3e928bb2005-01-07 07:47:09 +00001898 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001899 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001900 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001901 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001902 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001903 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001904 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001905 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001906 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001907 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001908 SDOperand Lo, Hi;
1909 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001910
1911 // Big endian systems want the hi reg first.
1912 if (!TLI.isLittleEndian())
1913 std::swap(Lo, Hi);
1914
Evan Cheng13acce32006-12-11 19:27:14 +00001915 if (Hi.Val)
1916 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1917 else
1918 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001919 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001920 } else {
1921 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001922 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1923 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001924
Dan Gohman7f321562007-06-25 16:23:39 +00001925 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001926 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001927 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001928 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001929 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001930 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001932 } else if (NumElems == 1) {
1933 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001934 Tmp2 = ScalarizeVectorOp(Tmp2);
1935 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001937
1938 // FIXME: Returns of gcc generic vectors smaller than a legal type
1939 // should be returned in integer registers!
1940
Chris Lattnerf87324e2006-04-11 01:31:51 +00001941 // The scalarized value type may not be legal, e.g. it might require
1942 // promotion or expansion. Relegalize the return.
1943 Result = LegalizeOp(Result);
1944 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001945 // FIXME: Returns of gcc generic vectors larger than a legal vector
1946 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001947 SDOperand Lo, Hi;
1948 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001949 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001950 Result = LegalizeOp(Result);
1951 }
1952 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001953 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001954 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001955 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001957 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001958 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001959 }
1960 break;
1961 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001962 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001963 break;
1964 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001965 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001966 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001967 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001968 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1969 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001970 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001971 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001972 break;
1973 case Expand: {
1974 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001975 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001976 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001977 ExpandOp(Node->getOperand(i), Lo, Hi);
1978 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001979 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001980 if (Hi.Val) {
1981 NewValues.push_back(Hi);
1982 NewValues.push_back(Node->getOperand(i+1));
1983 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001984 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001985 }
1986 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001987 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001988 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001989
1990 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001991 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001992 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001993 Result = DAG.getNode(ISD::RET, MVT::Other,
1994 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001995 break;
1996 }
1997 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001998
Chris Lattner6862dbc2006-01-29 21:02:23 +00001999 if (Result.getOpcode() == ISD::RET) {
2000 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2001 default: assert(0 && "This action is not supported yet!");
2002 case TargetLowering::Legal: break;
2003 case TargetLowering::Custom:
2004 Tmp1 = TLI.LowerOperation(Result, DAG);
2005 if (Tmp1.Val) Result = Tmp1;
2006 break;
2007 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002008 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002009 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002010 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002011 StoreSDNode *ST = cast<StoreSDNode>(Node);
2012 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2013 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002014 int SVOffset = ST->getSrcValueOffset();
2015 unsigned Alignment = ST->getAlignment();
2016 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002017
Evan Cheng8b2794a2006-10-13 21:14:26 +00002018 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002019 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2020 // FIXME: We shouldn't do this for TargetConstantFP's.
2021 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2022 // to phase ordering between legalized code and the dag combiner. This
2023 // probably means that we need to integrate dag combiner and legalizer
2024 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002025 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002026 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002027 if (CFP->getValueType(0) == MVT::f32 &&
2028 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002029 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2030 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002031 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002032 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2033 SVOffset, isVolatile, Alignment);
2034 break;
2035 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002036 // If this target supports 64-bit registers, do a single 64-bit store.
2037 if (getTypeAction(MVT::i64) == Legal) {
2038 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2039 getZExtValue(), MVT::i64);
2040 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2041 SVOffset, isVolatile, Alignment);
2042 break;
2043 } else if (getTypeAction(MVT::i32) == Legal) {
2044 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2045 // stores. If the target supports neither 32- nor 64-bits, this
2046 // xform is certainly not worth it.
2047 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2048 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2049 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2050 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2051
2052 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2053 SVOffset, isVolatile, Alignment);
2054 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2055 getIntPtrConstant(4));
2056 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
2057 isVolatile, std::max(Alignment, 4U));
2058
2059 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2060 break;
2061 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002062 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002063 }
2064
Evan Cheng8b2794a2006-10-13 21:14:26 +00002065 switch (getTypeAction(ST->getStoredVT())) {
2066 case Legal: {
2067 Tmp3 = LegalizeOp(ST->getValue());
2068 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2069 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002070
Evan Cheng8b2794a2006-10-13 21:14:26 +00002071 MVT::ValueType VT = Tmp3.getValueType();
2072 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2073 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002074 case TargetLowering::Legal:
2075 // If this is an unaligned store and the target doesn't support it,
2076 // expand it.
2077 if (!TLI.allowsUnalignedMemoryAccesses()) {
2078 unsigned ABIAlignment = TLI.getTargetData()->
2079 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2080 if (ST->getAlignment() < ABIAlignment)
2081 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2082 TLI);
2083 }
2084 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002085 case TargetLowering::Custom:
2086 Tmp1 = TLI.LowerOperation(Result, DAG);
2087 if (Tmp1.Val) Result = Tmp1;
2088 break;
2089 case TargetLowering::Promote:
2090 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2091 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2092 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2093 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002094 ST->getSrcValue(), SVOffset, isVolatile,
2095 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002096 break;
2097 }
2098 break;
2099 }
2100 case Promote:
2101 // Truncate the value and store the result.
2102 Tmp3 = PromoteOp(ST->getValue());
2103 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002104 SVOffset, ST->getStoredVT(),
2105 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002106 break;
2107
2108 case Expand:
2109 unsigned IncrementSize = 0;
2110 SDOperand Lo, Hi;
2111
2112 // If this is a vector type, then we have to calculate the increment as
2113 // the product of the element size in bytes, and the number of elements
2114 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002115 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002116 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00002117 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2118 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002119
Dan Gohman7f321562007-06-25 16:23:39 +00002120 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002121 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002122 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002123 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002124 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002125 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002126 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002127 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002128 Result = LegalizeOp(Result);
2129 break;
2130 } else if (NumElems == 1) {
2131 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002132 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002133 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002134 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002135 // The scalarized value type may not be legal, e.g. it might require
2136 // promotion or expansion. Relegalize the scalar store.
2137 Result = LegalizeOp(Result);
2138 break;
2139 } else {
2140 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2141 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2142 }
2143 } else {
2144 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002145 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002146
2147 if (!TLI.isLittleEndian())
2148 std::swap(Lo, Hi);
2149 }
2150
2151 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002152 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002153
2154 if (Hi.Val == NULL) {
2155 // Must be int <-> float one-to-one expansion.
2156 Result = Lo;
2157 break;
2158 }
2159
Evan Cheng8b2794a2006-10-13 21:14:26 +00002160 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2161 getIntPtrConstant(IncrementSize));
2162 assert(isTypeLegal(Tmp2.getValueType()) &&
2163 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002164 SVOffset += IncrementSize;
2165 if (Alignment > IncrementSize)
2166 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002167 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002168 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002169 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2170 break;
2171 }
2172 } else {
2173 // Truncating store
2174 assert(isTypeLegal(ST->getValue().getValueType()) &&
2175 "Cannot handle illegal TRUNCSTORE yet!");
2176 Tmp3 = LegalizeOp(ST->getValue());
2177
2178 // The only promote case we handle is TRUNCSTORE:i1 X into
2179 // -> TRUNCSTORE:i8 (and X, 1)
2180 if (ST->getStoredVT() == MVT::i1 &&
2181 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2182 // Promote the bool to a mask then store.
2183 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2184 DAG.getConstant(1, Tmp3.getValueType()));
2185 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002186 SVOffset, MVT::i8,
2187 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002188 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2189 Tmp2 != ST->getBasePtr()) {
2190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2191 ST->getOffset());
2192 }
2193
2194 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2195 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002196 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002197 case TargetLowering::Legal:
2198 // If this is an unaligned store and the target doesn't support it,
2199 // expand it.
2200 if (!TLI.allowsUnalignedMemoryAccesses()) {
2201 unsigned ABIAlignment = TLI.getTargetData()->
2202 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2203 if (ST->getAlignment() < ABIAlignment)
2204 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2205 TLI);
2206 }
2207 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002208 case TargetLowering::Custom:
2209 Tmp1 = TLI.LowerOperation(Result, DAG);
2210 if (Tmp1.Val) Result = Tmp1;
2211 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002212 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002213 }
2214 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002215 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002216 case ISD::PCMARKER:
2217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002219 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002220 case ISD::STACKSAVE:
2221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2223 Tmp1 = Result.getValue(0);
2224 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002225
Chris Lattner140d53c2006-01-13 02:50:02 +00002226 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2227 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002228 case TargetLowering::Legal: break;
2229 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002230 Tmp3 = TLI.LowerOperation(Result, DAG);
2231 if (Tmp3.Val) {
2232 Tmp1 = LegalizeOp(Tmp3);
2233 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002234 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002235 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002236 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002237 // Expand to CopyFromReg if the target set
2238 // StackPointerRegisterToSaveRestore.
2239 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002240 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002241 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002242 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002243 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002244 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2245 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002246 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002247 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002248 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002249
2250 // Since stacksave produce two values, make sure to remember that we
2251 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002252 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2253 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2254 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002255
Chris Lattner140d53c2006-01-13 02:50:02 +00002256 case ISD::STACKRESTORE:
2257 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2258 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002259 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002260
2261 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2262 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002263 case TargetLowering::Legal: break;
2264 case TargetLowering::Custom:
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002267 break;
2268 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002269 // Expand to CopyToReg if the target set
2270 // StackPointerRegisterToSaveRestore.
2271 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2272 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2273 } else {
2274 Result = Tmp1;
2275 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002276 break;
2277 }
2278 break;
2279
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002280 case ISD::READCYCLECOUNTER:
2281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002282 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002283 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2284 Node->getValueType(0))) {
2285 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002286 case TargetLowering::Legal:
2287 Tmp1 = Result.getValue(0);
2288 Tmp2 = Result.getValue(1);
2289 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002290 case TargetLowering::Custom:
2291 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002292 Tmp1 = LegalizeOp(Result.getValue(0));
2293 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002294 break;
2295 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002296
2297 // Since rdcc produce two values, make sure to remember that we legalized
2298 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002299 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2300 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002301 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002302
Chris Lattner2ee743f2005-01-14 22:08:15 +00002303 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002304 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2305 case Expand: assert(0 && "It's impossible to expand bools");
2306 case Legal:
2307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2308 break;
2309 case Promote:
2310 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002311 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002312 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002313 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2314 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002315 break;
2316 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002317 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002318 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002319
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002321
Nate Begemanb942a3d2005-08-23 04:29:48 +00002322 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002323 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002324 case TargetLowering::Legal: break;
2325 case TargetLowering::Custom: {
2326 Tmp1 = TLI.LowerOperation(Result, DAG);
2327 if (Tmp1.Val) Result = Tmp1;
2328 break;
2329 }
Nate Begeman9373a812005-08-10 20:51:12 +00002330 case TargetLowering::Expand:
2331 if (Tmp1.getOpcode() == ISD::SETCC) {
2332 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2333 Tmp2, Tmp3,
2334 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2335 } else {
2336 Result = DAG.getSelectCC(Tmp1,
2337 DAG.getConstant(0, Tmp1.getValueType()),
2338 Tmp2, Tmp3, ISD::SETNE);
2339 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002340 break;
2341 case TargetLowering::Promote: {
2342 MVT::ValueType NVT =
2343 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2344 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002345 if (MVT::isVector(Tmp2.getValueType())) {
2346 ExtOp = ISD::BIT_CONVERT;
2347 TruncOp = ISD::BIT_CONVERT;
2348 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002349 ExtOp = ISD::ANY_EXTEND;
2350 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002351 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002352 ExtOp = ISD::FP_EXTEND;
2353 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002354 }
2355 // Promote each of the values to the new type.
2356 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2357 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2358 // Perform the larger operation, then round down.
2359 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2360 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2361 break;
2362 }
2363 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002364 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002365 case ISD::SELECT_CC: {
2366 Tmp1 = Node->getOperand(0); // LHS
2367 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002368 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2369 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002370 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002371
Nate Begeman750ac1b2006-02-01 07:19:44 +00002372 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2373
2374 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2375 // the LHS is a legal SETCC itself. In this case, we need to compare
2376 // the result against zero to select between true and false values.
2377 if (Tmp2.Val == 0) {
2378 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2379 CC = DAG.getCondCode(ISD::SETNE);
2380 }
2381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2382
2383 // Everything is legal, see if we should expand this op or something.
2384 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2385 default: assert(0 && "This action is not supported yet!");
2386 case TargetLowering::Legal: break;
2387 case TargetLowering::Custom:
2388 Tmp1 = TLI.LowerOperation(Result, DAG);
2389 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002390 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002391 }
2392 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002393 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002394 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002395 Tmp1 = Node->getOperand(0);
2396 Tmp2 = Node->getOperand(1);
2397 Tmp3 = Node->getOperand(2);
2398 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2399
2400 // If we had to Expand the SetCC operands into a SELECT node, then it may
2401 // not always be possible to return a true LHS & RHS. In this case, just
2402 // return the value we legalized, returned in the LHS
2403 if (Tmp2.Val == 0) {
2404 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002405 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002406 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002407
Chris Lattner73e142f2006-01-30 22:43:50 +00002408 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002409 default: assert(0 && "Cannot handle this action for SETCC yet!");
2410 case TargetLowering::Custom:
2411 isCustom = true;
2412 // FALLTHROUGH.
2413 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002415 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002416 Tmp4 = TLI.LowerOperation(Result, DAG);
2417 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002418 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002419 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002420 case TargetLowering::Promote: {
2421 // First step, figure out the appropriate operation to use.
2422 // Allow SETCC to not be supported for all legal data types
2423 // Mostly this targets FP
2424 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002425 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002426
2427 // Scan for the appropriate larger type to use.
2428 while (1) {
2429 NewInTy = (MVT::ValueType)(NewInTy+1);
2430
2431 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2432 "Fell off of the edge of the integer world");
2433 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2434 "Fell off of the edge of the floating point world");
2435
2436 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002437 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002438 break;
2439 }
2440 if (MVT::isInteger(NewInTy))
2441 assert(0 && "Cannot promote Legal Integer SETCC yet");
2442 else {
2443 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2444 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2445 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002446 Tmp1 = LegalizeOp(Tmp1);
2447 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002449 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002450 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002451 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002452 case TargetLowering::Expand:
2453 // Expand a setcc node into a select_cc of the same condition, lhs, and
2454 // rhs that selects between const 1 (true) and const 0 (false).
2455 MVT::ValueType VT = Node->getValueType(0);
2456 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2457 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002458 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002459 break;
2460 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002461 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002462 case ISD::MEMSET:
2463 case ISD::MEMCPY:
2464 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002465 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002466 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2467
2468 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2469 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2470 case Expand: assert(0 && "Cannot expand a byte!");
2471 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002472 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002473 break;
2474 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002475 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002476 break;
2477 }
2478 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002479 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002480 }
Chris Lattner272455b2005-02-02 03:44:41 +00002481
2482 SDOperand Tmp4;
2483 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002484 case Expand: {
2485 // Length is too big, just take the lo-part of the length.
2486 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002487 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002488 break;
2489 }
Chris Lattnere5605212005-01-28 22:29:18 +00002490 case Legal:
2491 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002492 break;
2493 case Promote:
2494 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002495 break;
2496 }
2497
2498 SDOperand Tmp5;
2499 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2500 case Expand: assert(0 && "Cannot expand this yet!");
2501 case Legal:
2502 Tmp5 = LegalizeOp(Node->getOperand(4));
2503 break;
2504 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002505 Tmp5 = PromoteOp(Node->getOperand(4));
2506 break;
2507 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002508
2509 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2510 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002511 case TargetLowering::Custom:
2512 isCustom = true;
2513 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002514 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002515 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002516 if (isCustom) {
2517 Tmp1 = TLI.LowerOperation(Result, DAG);
2518 if (Tmp1.Val) Result = Tmp1;
2519 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002520 break;
2521 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002522 // Otherwise, the target does not support this operation. Lower the
2523 // operation to an explicit libcall as appropriate.
2524 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002525 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002526 TargetLowering::ArgListTy Args;
2527 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002528
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002529 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002530 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002531 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002532 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002533 // Extend the (previously legalized) ubyte argument to be an int value
2534 // for the call.
2535 if (Tmp3.getValueType() > MVT::i32)
2536 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2537 else
2538 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002539 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002540 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002541 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002542 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002543
2544 FnName = "memset";
2545 } else if (Node->getOpcode() == ISD::MEMCPY ||
2546 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002547 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002548 Entry.Node = Tmp2; Args.push_back(Entry);
2549 Entry.Node = Tmp3; Args.push_back(Entry);
2550 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002551 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2552 } else {
2553 assert(0 && "Unknown op!");
2554 }
Chris Lattner45982da2005-05-12 16:53:42 +00002555
Chris Lattnere1bd8222005-01-11 05:57:22 +00002556 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002557 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002558 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002559 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002560 break;
2561 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002562 }
2563 break;
2564 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002565
Chris Lattner5b359c62005-04-02 04:00:59 +00002566 case ISD::SHL_PARTS:
2567 case ISD::SRA_PARTS:
2568 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002569 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002570 bool Changed = false;
2571 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2572 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2573 Changed |= Ops.back() != Node->getOperand(i);
2574 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002575 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002576 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002577
Evan Cheng05a2d562006-01-09 18:31:59 +00002578 switch (TLI.getOperationAction(Node->getOpcode(),
2579 Node->getValueType(0))) {
2580 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002581 case TargetLowering::Legal: break;
2582 case TargetLowering::Custom:
2583 Tmp1 = TLI.LowerOperation(Result, DAG);
2584 if (Tmp1.Val) {
2585 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002586 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002587 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002588 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2589 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002590 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002591 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002592 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002593 return RetVal;
2594 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002595 break;
2596 }
2597
Chris Lattner2c8086f2005-04-02 05:00:07 +00002598 // Since these produce multiple values, make sure to remember that we
2599 // legalized all of them.
2600 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2601 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2602 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002603 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002604
2605 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002606 case ISD::ADD:
2607 case ISD::SUB:
2608 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002609 case ISD::MULHS:
2610 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002611 case ISD::UDIV:
2612 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002613 case ISD::AND:
2614 case ISD::OR:
2615 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002616 case ISD::SHL:
2617 case ISD::SRL:
2618 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002619 case ISD::FADD:
2620 case ISD::FSUB:
2621 case ISD::FMUL:
2622 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002623 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002624 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002625 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2626 case Expand: assert(0 && "Not possible");
2627 case Legal:
2628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2629 break;
2630 case Promote:
2631 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2632 break;
2633 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002634
2635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002636
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002637 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002638 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002639 case TargetLowering::Legal: break;
2640 case TargetLowering::Custom:
2641 Tmp1 = TLI.LowerOperation(Result, DAG);
2642 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002643 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002644 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002645 MVT::ValueType VT = Op.getValueType();
2646
2647 // See if multiply or divide can be lowered using two-result operations.
2648 SDVTList VTs = DAG.getVTList(VT, VT);
2649 if (Node->getOpcode() == ISD::MUL) {
2650 // We just need the low half of the multiply; try both the signed
2651 // and unsigned forms. If the target supports both SMUL_LOHI and
2652 // UMUL_LOHI, form a preference by checking which forms of plain
2653 // MULH it supports.
2654 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2655 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2656 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2657 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2658 unsigned OpToUse = 0;
2659 if (HasSMUL_LOHI && !HasMULHS) {
2660 OpToUse = ISD::SMUL_LOHI;
2661 } else if (HasUMUL_LOHI && !HasMULHU) {
2662 OpToUse = ISD::UMUL_LOHI;
2663 } else if (HasSMUL_LOHI) {
2664 OpToUse = ISD::SMUL_LOHI;
2665 } else if (HasUMUL_LOHI) {
2666 OpToUse = ISD::UMUL_LOHI;
2667 }
2668 if (OpToUse) {
2669 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2670 break;
2671 }
2672 }
2673 if (Node->getOpcode() == ISD::MULHS &&
2674 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2675 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2676 break;
2677 }
2678 if (Node->getOpcode() == ISD::MULHU &&
2679 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2680 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2681 break;
2682 }
2683 if (Node->getOpcode() == ISD::SDIV &&
2684 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2685 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2686 break;
2687 }
2688 if (Node->getOpcode() == ISD::UDIV &&
2689 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2690 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2691 break;
2692 }
2693
Dan Gohman82669522007-10-11 23:57:53 +00002694 // Check to see if we have a libcall for this operator.
2695 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2696 bool isSigned = false;
2697 switch (Node->getOpcode()) {
2698 case ISD::UDIV:
2699 case ISD::SDIV:
2700 if (VT == MVT::i32) {
2701 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002702 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002703 isSigned = Node->getOpcode() == ISD::SDIV;
2704 }
2705 break;
2706 case ISD::FPOW:
2707 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2708 VT == MVT::f64 ? RTLIB::POW_F64 :
2709 VT == MVT::f80 ? RTLIB::POW_F80 :
2710 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2711 RTLIB::UNKNOWN_LIBCALL;
2712 break;
2713 default: break;
2714 }
2715 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2716 SDOperand Dummy;
2717 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002718 break;
2719 }
2720
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002721 assert(MVT::isVector(Node->getValueType(0)) &&
2722 "Cannot expand this binary operator!");
2723 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002724 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002725 break;
2726 }
Evan Chengcc987612006-04-12 21:20:24 +00002727 case TargetLowering::Promote: {
2728 switch (Node->getOpcode()) {
2729 default: assert(0 && "Do not know how to promote this BinOp!");
2730 case ISD::AND:
2731 case ISD::OR:
2732 case ISD::XOR: {
2733 MVT::ValueType OVT = Node->getValueType(0);
2734 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2735 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2736 // Bit convert each of the values to the new type.
2737 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2738 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2739 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2740 // Bit convert the result back the original type.
2741 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2742 break;
2743 }
2744 }
2745 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002746 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002747 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002748
Dan Gohmane14ea862007-10-05 14:17:22 +00002749 case ISD::SMUL_LOHI:
2750 case ISD::UMUL_LOHI:
2751 case ISD::SDIVREM:
2752 case ISD::UDIVREM:
2753 // These nodes will only be produced by target-specific lowering, so
2754 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002755 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002756 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002757
2758 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2759 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002761 break;
2762
Chris Lattnera09f8482006-03-05 05:09:38 +00002763 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2764 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2765 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2766 case Expand: assert(0 && "Not possible");
2767 case Legal:
2768 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2769 break;
2770 case Promote:
2771 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2772 break;
2773 }
2774
2775 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2776
2777 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2778 default: assert(0 && "Operation not supported");
2779 case TargetLowering::Custom:
2780 Tmp1 = TLI.LowerOperation(Result, DAG);
2781 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002782 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002783 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002784 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002785 // If this target supports fabs/fneg natively and select is cheap,
2786 // do this efficiently.
2787 if (!TLI.isSelectExpensive() &&
2788 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2789 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002790 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002791 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002792 // Get the sign bit of the RHS.
2793 MVT::ValueType IVT =
2794 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2795 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2796 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2797 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2798 // Get the absolute value of the result.
2799 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2800 // Select between the nabs and abs value based on the sign bit of
2801 // the input.
2802 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2803 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2804 AbsVal),
2805 AbsVal);
2806 Result = LegalizeOp(Result);
2807 break;
2808 }
2809
2810 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002811 MVT::ValueType NVT =
2812 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2813 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2814 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2815 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002816 break;
2817 }
Evan Cheng912095b2007-01-04 21:56:39 +00002818 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002819 break;
2820
Nate Begeman551bf3f2006-02-17 05:43:56 +00002821 case ISD::ADDC:
2822 case ISD::SUBC:
2823 Tmp1 = LegalizeOp(Node->getOperand(0));
2824 Tmp2 = LegalizeOp(Node->getOperand(1));
2825 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2826 // Since this produces two values, make sure to remember that we legalized
2827 // both of them.
2828 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2829 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2830 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002831
Nate Begeman551bf3f2006-02-17 05:43:56 +00002832 case ISD::ADDE:
2833 case ISD::SUBE:
2834 Tmp1 = LegalizeOp(Node->getOperand(0));
2835 Tmp2 = LegalizeOp(Node->getOperand(1));
2836 Tmp3 = LegalizeOp(Node->getOperand(2));
2837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2838 // Since this produces two values, make sure to remember that we legalized
2839 // both of them.
2840 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2842 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002843
Nate Begeman419f8b62005-10-18 00:27:41 +00002844 case ISD::BUILD_PAIR: {
2845 MVT::ValueType PairTy = Node->getValueType(0);
2846 // TODO: handle the case where the Lo and Hi operands are not of legal type
2847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2849 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002850 case TargetLowering::Promote:
2851 case TargetLowering::Custom:
2852 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002853 case TargetLowering::Legal:
2854 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2855 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2856 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002857 case TargetLowering::Expand:
2858 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2859 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2860 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2861 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2862 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002863 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002864 break;
2865 }
2866 break;
2867 }
2868
Nate Begemanc105e192005-04-06 00:23:54 +00002869 case ISD::UREM:
2870 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002871 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002872 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2873 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002874
Nate Begemanc105e192005-04-06 00:23:54 +00002875 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002876 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2877 case TargetLowering::Custom:
2878 isCustom = true;
2879 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002880 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002882 if (isCustom) {
2883 Tmp1 = TLI.LowerOperation(Result, DAG);
2884 if (Tmp1.Val) Result = Tmp1;
2885 }
Nate Begemanc105e192005-04-06 00:23:54 +00002886 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002887 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002888 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002889 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002890 MVT::ValueType VT = Node->getValueType(0);
2891
2892 // See if remainder can be lowered using two-result operations.
2893 SDVTList VTs = DAG.getVTList(VT, VT);
2894 if (Node->getOpcode() == ISD::SREM &&
2895 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2896 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2897 break;
2898 }
2899 if (Node->getOpcode() == ISD::UREM &&
2900 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2901 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2902 break;
2903 }
2904
2905 if (MVT::isInteger(VT)) {
2906 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002907 TargetLowering::Legal) {
2908 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002909 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002910 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2911 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2912 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002913 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002914 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002915 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2916 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002917 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002918 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002919 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002920 } else {
2921 // Floating point mod -> fmod libcall.
Dan Gohman525178c2007-10-08 18:33:35 +00002922 RTLIB::Libcall LC = VT == MVT::f32
Evan Cheng56966222007-01-12 02:11:51 +00002923 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002924 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002925 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2926 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002927 }
2928 break;
2929 }
Dan Gohman525178c2007-10-08 18:33:35 +00002930 }
Nate Begemanc105e192005-04-06 00:23:54 +00002931 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002932 case ISD::VAARG: {
2933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2935
Chris Lattner5c62f332006-01-28 07:42:08 +00002936 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002937 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2938 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002939 case TargetLowering::Custom:
2940 isCustom = true;
2941 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002942 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002943 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2944 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002945 Tmp1 = Result.getValue(1);
2946
2947 if (isCustom) {
2948 Tmp2 = TLI.LowerOperation(Result, DAG);
2949 if (Tmp2.Val) {
2950 Result = LegalizeOp(Tmp2);
2951 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2952 }
2953 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002954 break;
2955 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002956 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002957 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002958 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002959 // Increment the pointer, VAList, to the next vaarg
2960 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2961 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2962 TLI.getPointerTy()));
2963 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002964 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2965 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002966 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002967 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002968 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002969 Result = LegalizeOp(Result);
2970 break;
2971 }
2972 }
2973 // Since VAARG produces two values, make sure to remember that we
2974 // legalized both of them.
2975 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002976 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2977 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002978 }
2979
2980 case ISD::VACOPY:
2981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2982 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2983 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2984
2985 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2986 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002987 case TargetLowering::Custom:
2988 isCustom = true;
2989 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002990 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2992 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002993 if (isCustom) {
2994 Tmp1 = TLI.LowerOperation(Result, DAG);
2995 if (Tmp1.Val) Result = Tmp1;
2996 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002997 break;
2998 case TargetLowering::Expand:
2999 // This defaults to loading a pointer from the input and storing it to the
3000 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003001 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003002 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003003 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3004 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003005 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3006 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003007 break;
3008 }
3009 break;
3010
3011 case ISD::VAEND:
3012 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3013 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3014
3015 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3016 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003017 case TargetLowering::Custom:
3018 isCustom = true;
3019 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003020 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003021 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003022 if (isCustom) {
3023 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3024 if (Tmp1.Val) Result = Tmp1;
3025 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003026 break;
3027 case TargetLowering::Expand:
3028 Result = Tmp1; // Default to a no-op, return the chain
3029 break;
3030 }
3031 break;
3032
3033 case ISD::VASTART:
3034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3036
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3038
Nate Begemanacc398c2006-01-25 18:21:52 +00003039 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3040 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003041 case TargetLowering::Legal: break;
3042 case TargetLowering::Custom:
3043 Tmp1 = TLI.LowerOperation(Result, DAG);
3044 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003045 break;
3046 }
3047 break;
3048
Nate Begeman35ef9132006-01-11 21:21:00 +00003049 case ISD::ROTL:
3050 case ISD::ROTR:
3051 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3052 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3055 default:
3056 assert(0 && "ROTL/ROTR legalize operation not supported");
3057 break;
3058 case TargetLowering::Legal:
3059 break;
3060 case TargetLowering::Custom:
3061 Tmp1 = TLI.LowerOperation(Result, DAG);
3062 if (Tmp1.Val) Result = Tmp1;
3063 break;
3064 case TargetLowering::Promote:
3065 assert(0 && "Do not know how to promote ROTL/ROTR");
3066 break;
3067 case TargetLowering::Expand:
3068 assert(0 && "Do not know how to expand ROTL/ROTR");
3069 break;
3070 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003071 break;
3072
Nate Begemand88fc032006-01-14 03:14:10 +00003073 case ISD::BSWAP:
3074 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3075 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003076 case TargetLowering::Custom:
3077 assert(0 && "Cannot custom legalize this yet!");
3078 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003079 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003080 break;
3081 case TargetLowering::Promote: {
3082 MVT::ValueType OVT = Tmp1.getValueType();
3083 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003084 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003085
Chris Lattner456a93a2006-01-28 07:39:30 +00003086 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3087 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3088 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3089 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3090 break;
3091 }
3092 case TargetLowering::Expand:
3093 Result = ExpandBSWAP(Tmp1);
3094 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003095 }
3096 break;
3097
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003098 case ISD::CTPOP:
3099 case ISD::CTTZ:
3100 case ISD::CTLZ:
3101 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3102 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003103 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003104 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003105 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003106 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003107 TargetLowering::Custom) {
3108 Tmp1 = TLI.LowerOperation(Result, DAG);
3109 if (Tmp1.Val) {
3110 Result = Tmp1;
3111 }
Scott Michel910b66d2007-07-30 21:00:31 +00003112 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003113 break;
3114 case TargetLowering::Promote: {
3115 MVT::ValueType OVT = Tmp1.getValueType();
3116 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003117
3118 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003119 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3120 // Perform the larger operation, then subtract if needed.
3121 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003122 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003123 case ISD::CTPOP:
3124 Result = Tmp1;
3125 break;
3126 case ISD::CTTZ:
3127 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003128 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003129 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003130 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003131 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003132 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003133 break;
3134 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003135 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003136 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003137 DAG.getConstant(MVT::getSizeInBits(NVT) -
3138 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003139 break;
3140 }
3141 break;
3142 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003143 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003144 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003145 break;
3146 }
3147 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003148
Chris Lattner2c8086f2005-04-02 05:00:07 +00003149 // Unary operators
3150 case ISD::FABS:
3151 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003152 case ISD::FSQRT:
3153 case ISD::FSIN:
3154 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003155 Tmp1 = LegalizeOp(Node->getOperand(0));
3156 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003157 case TargetLowering::Promote:
3158 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003159 isCustom = true;
3160 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003161 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003162 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003163 if (isCustom) {
3164 Tmp1 = TLI.LowerOperation(Result, DAG);
3165 if (Tmp1.Val) Result = Tmp1;
3166 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003167 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003168 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003169 switch (Node->getOpcode()) {
3170 default: assert(0 && "Unreachable!");
3171 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003172 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3173 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003174 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003175 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003176 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003177 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3178 MVT::ValueType VT = Node->getValueType(0);
3179 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003180 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003181 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3182 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003183 break;
3184 }
3185 case ISD::FSQRT:
3186 case ISD::FSIN:
3187 case ISD::FCOS: {
3188 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003189
3190 // Expand unsupported unary vector operators by unrolling them.
3191 if (MVT::isVector(VT)) {
3192 Result = LegalizeOp(UnrollVectorOp(Op));
3193 break;
3194 }
3195
Evan Cheng56966222007-01-12 02:11:51 +00003196 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003197 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003198 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00003199 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003200 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3201 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3202 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3203 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00003204 break;
3205 case ISD::FSIN:
3206 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3207 break;
3208 case ISD::FCOS:
3209 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3210 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003211 default: assert(0 && "Unreachable!");
3212 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003213 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003214 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3215 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003216 break;
3217 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003218 }
3219 break;
3220 }
3221 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003222 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003223 MVT::ValueType VT = Node->getValueType(0);
3224
3225 // Expand unsupported unary vector operators by unrolling them.
3226 if (MVT::isVector(VT)) {
3227 Result = LegalizeOp(UnrollVectorOp(Op));
3228 break;
3229 }
3230
3231 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen317096a2007-09-28 01:08:20 +00003232 RTLIB::Libcall LC =
Dan Gohman82669522007-10-11 23:57:53 +00003233 VT == MVT::f32 ? RTLIB::POWI_F32 :
3234 VT == MVT::f64 ? RTLIB::POWI_F64 :
3235 VT == MVT::f80 ? RTLIB::POWI_F80 :
3236 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003237 RTLIB::UNKNOWN_LIBCALL;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003238 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003239 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3240 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003241 break;
3242 }
Chris Lattner35481892005-12-23 00:16:34 +00003243 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003244 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003245 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003246 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3247 // The input has to be a vector type, we have to either scalarize it, pack
3248 // it, or convert it based on whether the input vector type is legal.
3249 SDNode *InVal = Node->getOperand(0).Val;
3250 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3251 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3252
3253 // Figure out if there is a simple type corresponding to this Vector
3254 // type. If so, convert to the vector type.
3255 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3256 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003257 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003258 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3259 LegalizeOp(Node->getOperand(0)));
3260 break;
3261 } else if (NumElems == 1) {
3262 // Turn this into a bit convert of the scalar input.
3263 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3264 ScalarizeVectorOp(Node->getOperand(0)));
3265 break;
3266 } else {
3267 // FIXME: UNIMP! Store then reload
3268 assert(0 && "Cast from unsupported vector type not implemented yet!");
3269 }
Chris Lattner67993f72006-01-23 07:30:46 +00003270 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003271 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3272 Node->getOperand(0).getValueType())) {
3273 default: assert(0 && "Unknown operation action!");
3274 case TargetLowering::Expand:
3275 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3276 break;
3277 case TargetLowering::Legal:
3278 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003279 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003280 break;
3281 }
3282 }
3283 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003284
Chris Lattner2c8086f2005-04-02 05:00:07 +00003285 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003286 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003287 case ISD::UINT_TO_FP: {
3288 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003289 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3290 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003291 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003292 Node->getOperand(0).getValueType())) {
3293 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003294 case TargetLowering::Custom:
3295 isCustom = true;
3296 // FALLTHROUGH
3297 case TargetLowering::Legal:
3298 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003299 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003300 if (isCustom) {
3301 Tmp1 = TLI.LowerOperation(Result, DAG);
3302 if (Tmp1.Val) Result = Tmp1;
3303 }
3304 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003305 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003306 Result = ExpandLegalINT_TO_FP(isSigned,
3307 LegalizeOp(Node->getOperand(0)),
3308 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003309 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003310 case TargetLowering::Promote:
3311 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3312 Node->getValueType(0),
3313 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003314 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003315 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003316 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003317 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003318 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3319 Node->getValueType(0), Node->getOperand(0));
3320 break;
3321 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003322 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003323 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003324 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3325 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003326 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003327 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3328 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003329 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003330 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3331 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003332 break;
3333 }
3334 break;
3335 }
3336 case ISD::TRUNCATE:
3337 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3338 case Legal:
3339 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003340 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003341 break;
3342 case Expand:
3343 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3344
3345 // Since the result is legal, we should just be able to truncate the low
3346 // part of the source.
3347 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3348 break;
3349 case Promote:
3350 Result = PromoteOp(Node->getOperand(0));
3351 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3352 break;
3353 }
3354 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003355
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003356 case ISD::FP_TO_SINT:
3357 case ISD::FP_TO_UINT:
3358 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3359 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003360 Tmp1 = LegalizeOp(Node->getOperand(0));
3361
Chris Lattner1618beb2005-07-29 00:11:56 +00003362 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3363 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003364 case TargetLowering::Custom:
3365 isCustom = true;
3366 // FALLTHROUGH
3367 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003368 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003369 if (isCustom) {
3370 Tmp1 = TLI.LowerOperation(Result, DAG);
3371 if (Tmp1.Val) Result = Tmp1;
3372 }
3373 break;
3374 case TargetLowering::Promote:
3375 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3376 Node->getOpcode() == ISD::FP_TO_SINT);
3377 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003378 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003379 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3380 SDOperand True, False;
3381 MVT::ValueType VT = Node->getOperand(0).getValueType();
3382 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003383 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003384 const uint64_t zero[] = {0, 0};
3385 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3386 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003387 (void)apf.convertFromZeroExtendedInteger
3388 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003389 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003390 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3391 Node->getOperand(0), Tmp2, ISD::SETLT);
3392 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3393 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003394 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003395 Tmp2));
3396 False = DAG.getNode(ISD::XOR, NVT, False,
3397 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003398 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3399 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003400 } else {
3401 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3402 }
3403 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003404 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003405 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003406 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003407 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003408 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003409 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003410 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003411 if (Node->getOpcode()==ISD::FP_TO_SINT)
3412 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003413 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3414 (DAG.getNode(ISD::FP_ROUND_INREG,
3415 MVT::ppcf128, Node->getOperand(0),
3416 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003417 else {
3418 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3419 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3420 Tmp2 = DAG.getConstantFP(apf, OVT);
3421 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3422 // FIXME: generated code sucks.
3423 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3424 DAG.getNode(ISD::ADD, MVT::i32,
3425 DAG.getNode(ISD::FP_TO_SINT, VT,
3426 DAG.getNode(ISD::FSUB, OVT,
3427 Node->getOperand(0), Tmp2)),
3428 DAG.getConstant(0x80000000, MVT::i32)),
3429 DAG.getNode(ISD::FP_TO_SINT, VT,
3430 Node->getOperand(0)),
3431 DAG.getCondCode(ISD::SETGE));
3432 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003433 break;
3434 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003435 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003436 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003437 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003438 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003439 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003440 LC = (VT == MVT::i32)
3441 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003442 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003443 LC = (VT == MVT::i32)
3444 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003445 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003446 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003447 LC = RTLIB::FPTOSINT_F80_I64;
3448 }
3449 else if (OVT == MVT::ppcf128) {
3450 assert(VT == MVT::i64);
3451 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003452 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003453 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003454 }
3455 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003456 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003457 LC = (VT == MVT::i32)
3458 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003459 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003460 LC = (VT == MVT::i32)
3461 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003462 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003463 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003464 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3465 }
3466 else if (OVT == MVT::ppcf128) {
3467 assert(VT == MVT::i64);
3468 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003469 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003470 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003471 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003472 default: assert(0 && "Unreachable!");
3473 }
3474 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003475 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3476 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003477 break;
3478 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003479 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003480 Tmp1 = PromoteOp(Node->getOperand(0));
3481 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3482 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003483 break;
3484 }
3485 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003486
Dale Johannesenab081c72007-08-09 17:27:48 +00003487 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003488 case ISD::FP_ROUND: {
3489 MVT::ValueType newVT = Op.getValueType();
3490 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3491 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003492 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3493 SDOperand Lo, Hi;
3494 ExpandOp(Node->getOperand(0), Lo, Hi);
3495 if (newVT == MVT::f64)
3496 Result = Hi;
3497 else
3498 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3499 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003500 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003501 // The only other way we can lower this is to turn it into a STORE,
3502 // LOAD pair, targetting a temporary location (a stack slot).
3503
3504 // NOTE: there is a choice here between constantly creating new stack
3505 // slots and always reusing the same one. We currently always create
3506 // new ones, as reuse may inhibit scheduling.
3507 MVT::ValueType slotVT =
3508 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3509 const Type *Ty = MVT::getTypeForValueType(slotVT);
3510 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3511 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3512 MachineFunction &MF = DAG.getMachineFunction();
3513 int SSFI =
3514 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3515 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3516 if (Node->getOpcode() == ISD::FP_EXTEND) {
3517 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3518 StackSlot, NULL, 0);
3519 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3520 Result, StackSlot, NULL, 0, oldVT);
3521 } else {
3522 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3523 StackSlot, NULL, 0, newVT);
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003524 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003525 }
3526 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003527 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003528 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003529 }
3530 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003531 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003532 case ISD::ZERO_EXTEND:
3533 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003534 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003535 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003536 case Legal:
3537 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003538 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003539 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003540 case Promote:
3541 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003542 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003543 Tmp1 = PromoteOp(Node->getOperand(0));
3544 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003545 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003546 case ISD::ZERO_EXTEND:
3547 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003548 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003549 Result = DAG.getZeroExtendInReg(Result,
3550 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003551 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003552 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003553 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003554 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003555 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003556 Result,
3557 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003558 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003559 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003560 Result = PromoteOp(Node->getOperand(0));
3561 if (Result.getValueType() != Op.getValueType())
3562 // Dynamically dead while we have only 2 FP types.
3563 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3564 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003565 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003566 Result = PromoteOp(Node->getOperand(0));
3567 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3568 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003569 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003570 }
3571 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003572 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003573 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003574 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003575 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003576
3577 // If this operation is not supported, convert it to a shl/shr or load/store
3578 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003579 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3580 default: assert(0 && "This action not supported for this op yet!");
3581 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003582 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003583 break;
3584 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003585 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003586 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003587 // NOTE: we could fall back on load/store here too for targets without
3588 // SAR. However, it is doubtful that any exist.
3589 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3590 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003591 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003592 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3593 Node->getOperand(0), ShiftCst);
3594 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3595 Result, ShiftCst);
3596 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003597 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003598 // EXTLOAD pair, targetting a temporary location (a stack slot).
3599
3600 // NOTE: there is a choice here between constantly creating new stack
3601 // slots and always reusing the same one. We currently always create
3602 // new ones, as reuse may inhibit scheduling.
3603 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003604 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003605 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003606 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003607 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003608 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003609 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003610 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3611 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003612 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003613 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003614 } else {
3615 assert(0 && "Unknown op");
3616 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003617 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003618 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003619 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003620 }
Duncan Sands36397f52007-07-27 12:58:54 +00003621 case ISD::TRAMPOLINE: {
3622 SDOperand Ops[6];
3623 for (unsigned i = 0; i != 6; ++i)
3624 Ops[i] = LegalizeOp(Node->getOperand(i));
3625 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3626 // The only option for this node is to custom lower it.
3627 Result = TLI.LowerOperation(Result, DAG);
3628 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003629
3630 // Since trampoline produces two values, make sure to remember that we
3631 // legalized both of them.
3632 Tmp1 = LegalizeOp(Result.getValue(1));
3633 Result = LegalizeOp(Result);
3634 AddLegalizedOperand(SDOperand(Node, 0), Result);
3635 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3636 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003637 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003638 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003639
Chris Lattner4ddd2832006-04-08 04:13:17 +00003640 assert(Result.getValueType() == Op.getValueType() &&
3641 "Bad legalization!");
3642
Chris Lattner456a93a2006-01-28 07:39:30 +00003643 // Make sure that the generated code is itself legal.
3644 if (Result != Op)
3645 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003646
Chris Lattner45982da2005-05-12 16:53:42 +00003647 // Note that LegalizeOp may be reentered even from single-use nodes, which
3648 // means that we always must cache transformed nodes.
3649 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003650 return Result;
3651}
3652
Chris Lattner8b6fa222005-01-15 22:16:26 +00003653/// PromoteOp - Given an operation that produces a value in an invalid type,
3654/// promote it to compute the value into a larger type. The produced value will
3655/// have the correct bits for the low portion of the register, but no guarantee
3656/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003657SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3658 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003659 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003660 assert(getTypeAction(VT) == Promote &&
3661 "Caller should expand or legalize operands that are not promotable!");
3662 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3663 "Cannot promote to smaller type!");
3664
Chris Lattner03c85462005-01-15 05:21:40 +00003665 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003666 SDOperand Result;
3667 SDNode *Node = Op.Val;
3668
Chris Lattner40030bf2007-02-04 01:17:38 +00003669 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003670 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003671
Chris Lattner03c85462005-01-15 05:21:40 +00003672 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003673 case ISD::CopyFromReg:
3674 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003675 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003676#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003677 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003678#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003679 assert(0 && "Do not know how to promote this operator!");
3680 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003681 case ISD::UNDEF:
3682 Result = DAG.getNode(ISD::UNDEF, NVT);
3683 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003684 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003685 if (VT != MVT::i1)
3686 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3687 else
3688 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003689 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3690 break;
3691 case ISD::ConstantFP:
3692 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3693 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3694 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003695
Chris Lattner82fbfb62005-01-18 02:59:52 +00003696 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003697 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003698 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3699 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003700 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003701
Chris Lattner03c85462005-01-15 05:21:40 +00003702 case ISD::TRUNCATE:
3703 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3704 case Legal:
3705 Result = LegalizeOp(Node->getOperand(0));
3706 assert(Result.getValueType() >= NVT &&
3707 "This truncation doesn't make sense!");
3708 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3709 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3710 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003711 case Promote:
3712 // The truncation is not required, because we don't guarantee anything
3713 // about high bits anyway.
3714 Result = PromoteOp(Node->getOperand(0));
3715 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003716 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003717 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3718 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003719 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003720 }
3721 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003722 case ISD::SIGN_EXTEND:
3723 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003724 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003725 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3726 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3727 case Legal:
3728 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003729 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003730 break;
3731 case Promote:
3732 // Promote the reg if it's smaller.
3733 Result = PromoteOp(Node->getOperand(0));
3734 // The high bits are not guaranteed to be anything. Insert an extend.
3735 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003736 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003737 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003738 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003739 Result = DAG.getZeroExtendInReg(Result,
3740 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003741 break;
3742 }
3743 break;
Chris Lattner35481892005-12-23 00:16:34 +00003744 case ISD::BIT_CONVERT:
3745 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3746 Result = PromoteOp(Result);
3747 break;
3748
Chris Lattner8b6fa222005-01-15 22:16:26 +00003749 case ISD::FP_EXTEND:
3750 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3751 case ISD::FP_ROUND:
3752 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3753 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3754 case Promote: assert(0 && "Unreachable with 2 FP types!");
3755 case Legal:
3756 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003757 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003758 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003759 break;
3760 }
3761 break;
3762
3763 case ISD::SINT_TO_FP:
3764 case ISD::UINT_TO_FP:
3765 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3766 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003767 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003768 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003769 break;
3770
3771 case Promote:
3772 Result = PromoteOp(Node->getOperand(0));
3773 if (Node->getOpcode() == ISD::SINT_TO_FP)
3774 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003775 Result,
3776 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003777 else
Chris Lattner23993562005-04-13 02:38:47 +00003778 Result = DAG.getZeroExtendInReg(Result,
3779 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003780 // No extra round required here.
3781 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003782 break;
3783 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003784 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3785 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003786 // Round if we cannot tolerate excess precision.
3787 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003788 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3789 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003790 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003791 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003792 break;
3793
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003794 case ISD::SIGN_EXTEND_INREG:
3795 Result = PromoteOp(Node->getOperand(0));
3796 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3797 Node->getOperand(1));
3798 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003799 case ISD::FP_TO_SINT:
3800 case ISD::FP_TO_UINT:
3801 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3802 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003803 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003804 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003805 break;
3806 case Promote:
3807 // The input result is prerounded, so we don't have to do anything
3808 // special.
3809 Tmp1 = PromoteOp(Node->getOperand(0));
3810 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003811 }
Nate Begemand2558e32005-08-14 01:20:53 +00003812 // If we're promoting a UINT to a larger size, check to see if the new node
3813 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3814 // we can use that instead. This allows us to generate better code for
3815 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3816 // legal, such as PowerPC.
3817 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003818 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003819 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3820 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003821 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3822 } else {
3823 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3824 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003825 break;
3826
Chris Lattner2c8086f2005-04-02 05:00:07 +00003827 case ISD::FABS:
3828 case ISD::FNEG:
3829 Tmp1 = PromoteOp(Node->getOperand(0));
3830 assert(Tmp1.getValueType() == NVT);
3831 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3832 // NOTE: we do not have to do any extra rounding here for
3833 // NoExcessFPPrecision, because we know the input will have the appropriate
3834 // precision, and these operations don't modify precision at all.
3835 break;
3836
Chris Lattnerda6ba872005-04-28 21:44:33 +00003837 case ISD::FSQRT:
3838 case ISD::FSIN:
3839 case ISD::FCOS:
3840 Tmp1 = PromoteOp(Node->getOperand(0));
3841 assert(Tmp1.getValueType() == NVT);
3842 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003843 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003844 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3845 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003846 break;
3847
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003848 case ISD::FPOWI: {
3849 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3850 // directly as well, which may be better.
3851 Tmp1 = PromoteOp(Node->getOperand(0));
3852 assert(Tmp1.getValueType() == NVT);
3853 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3854 if (NoExcessFPPrecision)
3855 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3856 DAG.getValueType(VT));
3857 break;
3858 }
3859
Chris Lattner03c85462005-01-15 05:21:40 +00003860 case ISD::AND:
3861 case ISD::OR:
3862 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003863 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003864 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003865 case ISD::MUL:
3866 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003867 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003868 // that too is okay if they are integer operations.
3869 Tmp1 = PromoteOp(Node->getOperand(0));
3870 Tmp2 = PromoteOp(Node->getOperand(1));
3871 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3872 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003873 break;
3874 case ISD::FADD:
3875 case ISD::FSUB:
3876 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003877 Tmp1 = PromoteOp(Node->getOperand(0));
3878 Tmp2 = PromoteOp(Node->getOperand(1));
3879 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3880 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3881
3882 // Floating point operations will give excess precision that we may not be
3883 // able to tolerate. If we DO allow excess precision, just leave it,
3884 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003885 // FIXME: Why would we need to round FP ops more than integer ones?
3886 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003887 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003888 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3889 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003890 break;
3891
Chris Lattner8b6fa222005-01-15 22:16:26 +00003892 case ISD::SDIV:
3893 case ISD::SREM:
3894 // These operators require that their input be sign extended.
3895 Tmp1 = PromoteOp(Node->getOperand(0));
3896 Tmp2 = PromoteOp(Node->getOperand(1));
3897 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003898 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3899 DAG.getValueType(VT));
3900 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3901 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003902 }
3903 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3904
3905 // Perform FP_ROUND: this is probably overly pessimistic.
3906 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003907 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3908 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003909 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003910 case ISD::FDIV:
3911 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003912 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003913 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003914 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3915 case Legal:
3916 Tmp1 = LegalizeOp(Node->getOperand(0));
3917 break;
3918 case Promote:
3919 Tmp1 = PromoteOp(Node->getOperand(0));
3920 break;
3921 case Expand:
3922 assert(0 && "not implemented");
3923 }
3924 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3925 case Legal:
3926 Tmp2 = LegalizeOp(Node->getOperand(1));
3927 break;
3928 case Promote:
3929 Tmp2 = PromoteOp(Node->getOperand(1));
3930 break;
3931 case Expand:
3932 assert(0 && "not implemented");
3933 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003934 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3935
3936 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003937 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003938 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3939 DAG.getValueType(VT));
3940 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003941
3942 case ISD::UDIV:
3943 case ISD::UREM:
3944 // These operators require that their input be zero extended.
3945 Tmp1 = PromoteOp(Node->getOperand(0));
3946 Tmp2 = PromoteOp(Node->getOperand(1));
3947 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003948 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3949 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003950 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3951 break;
3952
3953 case ISD::SHL:
3954 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003955 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003956 break;
3957 case ISD::SRA:
3958 // The input value must be properly sign extended.
3959 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003960 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3961 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003962 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003963 break;
3964 case ISD::SRL:
3965 // The input value must be properly zero extended.
3966 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003967 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003968 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003969 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003970
3971 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003972 Tmp1 = Node->getOperand(0); // Get the chain.
3973 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003974 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3975 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3976 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3977 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003978 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003979 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003980 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003981 // Increment the pointer, VAList, to the next vaarg
3982 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3983 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3984 TLI.getPointerTy()));
3985 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003986 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3987 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003988 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003989 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003990 }
3991 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003992 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003993 break;
3994
Evan Cheng466685d2006-10-09 20:57:25 +00003995 case ISD::LOAD: {
3996 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003997 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3998 ? ISD::EXTLOAD : LD->getExtensionType();
3999 Result = DAG.getExtLoad(ExtType, NVT,
4000 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004001 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004002 LD->getLoadedVT(),
4003 LD->isVolatile(),
4004 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004005 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004006 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004007 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004008 }
Chris Lattner03c85462005-01-15 05:21:40 +00004009 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004010 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4011 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004012 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004013 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004014 case ISD::SELECT_CC:
4015 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4016 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4017 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004018 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004019 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004020 case ISD::BSWAP:
4021 Tmp1 = Node->getOperand(0);
4022 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4023 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4024 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004025 DAG.getConstant(MVT::getSizeInBits(NVT) -
4026 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004027 TLI.getShiftAmountTy()));
4028 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004029 case ISD::CTPOP:
4030 case ISD::CTTZ:
4031 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004032 // Zero extend the argument
4033 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004034 // Perform the larger operation, then subtract if needed.
4035 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004036 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004037 case ISD::CTPOP:
4038 Result = Tmp1;
4039 break;
4040 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004041 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004042 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004043 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4044 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004045 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004046 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004047 break;
4048 case ISD::CTLZ:
4049 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004050 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004051 DAG.getConstant(MVT::getSizeInBits(NVT) -
4052 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004053 break;
4054 }
4055 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004056 case ISD::EXTRACT_SUBVECTOR:
4057 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004058 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004059 case ISD::EXTRACT_VECTOR_ELT:
4060 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4061 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004062 }
4063
4064 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004065
4066 // Make sure the result is itself legal.
4067 Result = LegalizeOp(Result);
4068
4069 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004070 AddPromotedOperand(Op, Result);
4071 return Result;
4072}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004073
Dan Gohman7f321562007-06-25 16:23:39 +00004074/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4075/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4076/// based on the vector type. The return type of this matches the element type
4077/// of the vector, which may not be legal for the target.
4078SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004079 // We know that operand #0 is the Vec vector. If the index is a constant
4080 // or if the invec is a supported hardware type, we can use it. Otherwise,
4081 // lower to a store then an indexed load.
4082 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004083 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004084
Dan Gohmane40c7b02007-09-24 15:54:53 +00004085 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004086 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004087
Dan Gohman7f321562007-06-25 16:23:39 +00004088 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4089 default: assert(0 && "This action is not supported yet!");
4090 case TargetLowering::Custom: {
4091 Vec = LegalizeOp(Vec);
4092 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4093 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4094 if (Tmp3.Val)
4095 return Tmp3;
4096 break;
4097 }
4098 case TargetLowering::Legal:
4099 if (isTypeLegal(TVT)) {
4100 Vec = LegalizeOp(Vec);
4101 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004102 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004103 }
4104 break;
4105 case TargetLowering::Expand:
4106 break;
4107 }
4108
4109 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004110 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004111 Op = ScalarizeVectorOp(Vec);
4112 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4113 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004114 SDOperand Lo, Hi;
4115 SplitVectorOp(Vec, Lo, Hi);
4116 if (CIdx->getValue() < NumElems/2) {
4117 Vec = Lo;
4118 } else {
4119 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004120 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4121 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004122 }
Dan Gohman7f321562007-06-25 16:23:39 +00004123
Chris Lattner15972212006-03-31 17:55:51 +00004124 // It's now an extract from the appropriate high or low part. Recurse.
4125 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004126 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004127 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004128 // Store the value to a temporary stack slot, then LOAD the scalar
4129 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004130 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004131 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4132
4133 // Add the offset to the index.
4134 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4135 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4136 DAG.getConstant(EltSize, Idx.getValueType()));
4137 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4138
4139 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004140 }
Dan Gohman7f321562007-06-25 16:23:39 +00004141 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004142}
4143
Dan Gohman7f321562007-06-25 16:23:39 +00004144/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004145/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004146SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004147 // We know that operand #0 is the Vec vector. For now we assume the index
4148 // is a constant and that the extracted result is a supported hardware type.
4149 SDOperand Vec = Op.getOperand(0);
4150 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4151
Dan Gohman7f321562007-06-25 16:23:39 +00004152 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004153
4154 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4155 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004156 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004157 }
4158
4159 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4160 SDOperand Lo, Hi;
4161 SplitVectorOp(Vec, Lo, Hi);
4162 if (CIdx->getValue() < NumElems/2) {
4163 Vec = Lo;
4164 } else {
4165 Vec = Hi;
4166 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4167 }
4168
4169 // It's now an extract from the appropriate high or low part. Recurse.
4170 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004171 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004172}
4173
Nate Begeman750ac1b2006-02-01 07:19:44 +00004174/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4175/// with condition CC on the current target. This usually involves legalizing
4176/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4177/// there may be no choice but to create a new SetCC node to represent the
4178/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4179/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4180void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4181 SDOperand &RHS,
4182 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004183 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004184
4185 switch (getTypeAction(LHS.getValueType())) {
4186 case Legal:
4187 Tmp1 = LegalizeOp(LHS); // LHS
4188 Tmp2 = LegalizeOp(RHS); // RHS
4189 break;
4190 case Promote:
4191 Tmp1 = PromoteOp(LHS); // LHS
4192 Tmp2 = PromoteOp(RHS); // RHS
4193
4194 // If this is an FP compare, the operands have already been extended.
4195 if (MVT::isInteger(LHS.getValueType())) {
4196 MVT::ValueType VT = LHS.getValueType();
4197 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4198
4199 // Otherwise, we have to insert explicit sign or zero extends. Note
4200 // that we could insert sign extends for ALL conditions, but zero extend
4201 // is cheaper on many machines (an AND instead of two shifts), so prefer
4202 // it.
4203 switch (cast<CondCodeSDNode>(CC)->get()) {
4204 default: assert(0 && "Unknown integer comparison!");
4205 case ISD::SETEQ:
4206 case ISD::SETNE:
4207 case ISD::SETUGE:
4208 case ISD::SETUGT:
4209 case ISD::SETULE:
4210 case ISD::SETULT:
4211 // ALL of these operations will work if we either sign or zero extend
4212 // the operands (including the unsigned comparisons!). Zero extend is
4213 // usually a simpler/cheaper operation, so prefer it.
4214 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4215 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4216 break;
4217 case ISD::SETGE:
4218 case ISD::SETGT:
4219 case ISD::SETLT:
4220 case ISD::SETLE:
4221 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4222 DAG.getValueType(VT));
4223 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4224 DAG.getValueType(VT));
4225 break;
4226 }
4227 }
4228 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004229 case Expand: {
4230 MVT::ValueType VT = LHS.getValueType();
4231 if (VT == MVT::f32 || VT == MVT::f64) {
4232 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004233 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004234 switch (cast<CondCodeSDNode>(CC)->get()) {
4235 case ISD::SETEQ:
4236 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004237 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004238 break;
4239 case ISD::SETNE:
4240 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004241 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004242 break;
4243 case ISD::SETGE:
4244 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004245 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004246 break;
4247 case ISD::SETLT:
4248 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004249 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004250 break;
4251 case ISD::SETLE:
4252 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004253 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004254 break;
4255 case ISD::SETGT:
4256 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004257 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004258 break;
4259 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004260 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4261 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004262 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004263 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004264 break;
4265 default:
Evan Cheng56966222007-01-12 02:11:51 +00004266 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004267 switch (cast<CondCodeSDNode>(CC)->get()) {
4268 case ISD::SETONE:
4269 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004270 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004271 // Fallthrough
4272 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004273 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004274 break;
4275 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004276 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004277 break;
4278 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004279 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004280 break;
4281 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004282 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004283 break;
Evan Cheng56966222007-01-12 02:11:51 +00004284 case ISD::SETUEQ:
4285 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004286 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004287 default: assert(0 && "Unsupported FP setcc!");
4288 }
4289 }
4290
4291 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004292 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004293 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004294 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004295 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004296 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004297 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004298 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004299 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004300 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004301 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004302 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004303 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004304 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4305 Tmp2 = SDOperand();
4306 }
4307 LHS = Tmp1;
4308 RHS = Tmp2;
4309 return;
4310 }
4311
Nate Begeman750ac1b2006-02-01 07:19:44 +00004312 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4313 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004314 ExpandOp(RHS, RHSLo, RHSHi);
4315 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4316
4317 if (VT==MVT::ppcf128) {
4318 // FIXME: This generated code sucks. We want to generate
4319 // FCMP crN, hi1, hi2
4320 // BNE crN, L:
4321 // FCMP crN, lo1, lo2
4322 // The following can be improved, but not that much.
4323 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4324 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4325 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4326 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4327 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4328 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4329 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4330 Tmp2 = SDOperand();
4331 break;
4332 }
4333
4334 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004335 case ISD::SETEQ:
4336 case ISD::SETNE:
4337 if (RHSLo == RHSHi)
4338 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4339 if (RHSCST->isAllOnesValue()) {
4340 // Comparison to -1.
4341 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4342 Tmp2 = RHSLo;
4343 break;
4344 }
4345
4346 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4347 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4348 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4349 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4350 break;
4351 default:
4352 // If this is a comparison of the sign bit, just look at the top part.
4353 // X > -1, x < 0
4354 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4355 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4356 CST->getValue() == 0) || // X < 0
4357 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4358 CST->isAllOnesValue())) { // X > -1
4359 Tmp1 = LHSHi;
4360 Tmp2 = RHSHi;
4361 break;
4362 }
4363
4364 // FIXME: This generated code sucks.
4365 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004366 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004367 default: assert(0 && "Unknown integer setcc!");
4368 case ISD::SETLT:
4369 case ISD::SETULT: LowCC = ISD::SETULT; break;
4370 case ISD::SETGT:
4371 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4372 case ISD::SETLE:
4373 case ISD::SETULE: LowCC = ISD::SETULE; break;
4374 case ISD::SETGE:
4375 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4376 }
4377
4378 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4379 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4380 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4381
4382 // NOTE: on targets without efficient SELECT of bools, we can always use
4383 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004384 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4385 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4386 false, DagCombineInfo);
4387 if (!Tmp1.Val)
4388 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4389 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4390 CCCode, false, DagCombineInfo);
4391 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004392 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004393
4394 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4395 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4396 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4397 (Tmp2C && Tmp2C->getValue() == 0 &&
4398 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4399 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4400 (Tmp2C && Tmp2C->getValue() == 1 &&
4401 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4402 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4403 // low part is known false, returns high part.
4404 // For LE / GE, if high part is known false, ignore the low part.
4405 // For LT / GT, if high part is known true, ignore the low part.
4406 Tmp1 = Tmp2;
4407 Tmp2 = SDOperand();
4408 } else {
4409 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4410 ISD::SETEQ, false, DagCombineInfo);
4411 if (!Result.Val)
4412 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4413 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4414 Result, Tmp1, Tmp2));
4415 Tmp1 = Result;
4416 Tmp2 = SDOperand();
4417 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004418 }
4419 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004420 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004421 LHS = Tmp1;
4422 RHS = Tmp2;
4423}
4424
Chris Lattner35481892005-12-23 00:16:34 +00004425/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004426/// The resultant code need not be legal. Note that SrcOp is the input operand
4427/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004428SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4429 SDOperand SrcOp) {
4430 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004431 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004432
4433 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004434 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004435 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004436 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004437}
4438
Chris Lattner4352cc92006-04-04 17:23:26 +00004439SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4440 // Create a vector sized/aligned stack slot, store the value to element #0,
4441 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004442 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004443 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004444 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004445 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004446}
4447
4448
Chris Lattnerce872152006-03-19 06:31:19 +00004449/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004450/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004451SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4452
4453 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004454 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004455 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004456 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004457 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004458 std::map<SDOperand, std::vector<unsigned> > Values;
4459 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004460 bool isConstant = true;
4461 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4462 SplatValue.getOpcode() != ISD::UNDEF)
4463 isConstant = false;
4464
Evan Cheng033e6812006-03-24 01:17:21 +00004465 for (unsigned i = 1; i < NumElems; ++i) {
4466 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004467 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004468 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004469 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004470 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004471 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004472
4473 // If this isn't a constant element or an undef, we can't use a constant
4474 // pool load.
4475 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4476 V.getOpcode() != ISD::UNDEF)
4477 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004478 }
4479
4480 if (isOnlyLowElement) {
4481 // If the low element is an undef too, then this whole things is an undef.
4482 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4483 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4484 // Otherwise, turn this into a scalar_to_vector node.
4485 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4486 Node->getOperand(0));
4487 }
4488
Chris Lattner2eb86532006-03-24 07:29:17 +00004489 // If all elements are constants, create a load from the constant pool.
4490 if (isConstant) {
4491 MVT::ValueType VT = Node->getValueType(0);
4492 const Type *OpNTy =
4493 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4494 std::vector<Constant*> CV;
4495 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4496 if (ConstantFPSDNode *V =
4497 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004498 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004499 } else if (ConstantSDNode *V =
4500 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004501 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004502 } else {
4503 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4504 CV.push_back(UndefValue::get(OpNTy));
4505 }
4506 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004507 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004508 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004509 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004510 }
4511
Chris Lattner87100e02006-03-20 01:52:29 +00004512 if (SplatValue.Val) { // Splat of one value?
4513 // Build the shuffle constant vector: <0, 0, 0, 0>
4514 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004515 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004516 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004517 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004518 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4519 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004520
4521 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004522 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004523 // Get the splatted value into the low element of a vector register.
4524 SDOperand LowValVec =
4525 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4526
4527 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4528 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4529 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4530 SplatMask);
4531 }
4532 }
4533
Evan Cheng033e6812006-03-24 01:17:21 +00004534 // If there are only two unique elements, we may be able to turn this into a
4535 // vector shuffle.
4536 if (Values.size() == 2) {
4537 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4538 MVT::ValueType MaskVT =
4539 MVT::getIntVectorWithNumElements(NumElems);
4540 std::vector<SDOperand> MaskVec(NumElems);
4541 unsigned i = 0;
4542 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4543 E = Values.end(); I != E; ++I) {
4544 for (std::vector<unsigned>::iterator II = I->second.begin(),
4545 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004546 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004547 i += NumElems;
4548 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004549 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4550 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004551
4552 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004553 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4554 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004555 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004556 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4557 E = Values.end(); I != E; ++I) {
4558 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4559 I->first);
4560 Ops.push_back(Op);
4561 }
4562 Ops.push_back(ShuffleMask);
4563
4564 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004565 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4566 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004567 }
4568 }
Chris Lattnerce872152006-03-19 06:31:19 +00004569
4570 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4571 // aligned object on the stack, store each element into it, then load
4572 // the result as a vector.
4573 MVT::ValueType VT = Node->getValueType(0);
4574 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004575 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004576
4577 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004578 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004579 unsigned TypeByteSize =
4580 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004581 // Store (in the right endianness) the elements to memory.
4582 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4583 // Ignore undef elements.
4584 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4585
Chris Lattner841c8822006-03-22 01:46:54 +00004586 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004587
4588 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4589 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4590
Evan Cheng786225a2006-10-05 23:01:46 +00004591 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004592 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004593 }
4594
4595 SDOperand StoreChain;
4596 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004597 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4598 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004599 else
4600 StoreChain = DAG.getEntryNode();
4601
4602 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004603 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004604}
4605
Chris Lattner5b359c62005-04-02 04:00:59 +00004606void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4607 SDOperand Op, SDOperand Amt,
4608 SDOperand &Lo, SDOperand &Hi) {
4609 // Expand the subcomponents.
4610 SDOperand LHSL, LHSH;
4611 ExpandOp(Op, LHSL, LHSH);
4612
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004613 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004614 MVT::ValueType VT = LHSL.getValueType();
4615 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004616 Hi = Lo.getValue(1);
4617}
4618
4619
Chris Lattnere34b3962005-01-19 04:19:40 +00004620/// ExpandShift - Try to find a clever way to expand this shift operation out to
4621/// smaller elements. If we can't find a way that is more efficient than a
4622/// libcall on this target, return false. Otherwise, return true with the
4623/// low-parts expanded into Lo and Hi.
4624bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4625 SDOperand &Lo, SDOperand &Hi) {
4626 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4627 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004628
Chris Lattnere34b3962005-01-19 04:19:40 +00004629 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004630 SDOperand ShAmt = LegalizeOp(Amt);
4631 MVT::ValueType ShTy = ShAmt.getValueType();
4632 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4633 unsigned NVTBits = MVT::getSizeInBits(NVT);
4634
Chris Lattner7a3c8552007-10-14 20:35:12 +00004635 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004636 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4637 unsigned Cst = CN->getValue();
4638 // Expand the incoming operand to be shifted, so that we have its parts
4639 SDOperand InL, InH;
4640 ExpandOp(Op, InL, InH);
4641 switch(Opc) {
4642 case ISD::SHL:
4643 if (Cst > VTBits) {
4644 Lo = DAG.getConstant(0, NVT);
4645 Hi = DAG.getConstant(0, NVT);
4646 } else if (Cst > NVTBits) {
4647 Lo = DAG.getConstant(0, NVT);
4648 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004649 } else if (Cst == NVTBits) {
4650 Lo = DAG.getConstant(0, NVT);
4651 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004652 } else {
4653 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4654 Hi = DAG.getNode(ISD::OR, NVT,
4655 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4656 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4657 }
4658 return true;
4659 case ISD::SRL:
4660 if (Cst > VTBits) {
4661 Lo = DAG.getConstant(0, NVT);
4662 Hi = DAG.getConstant(0, NVT);
4663 } else if (Cst > NVTBits) {
4664 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4665 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004666 } else if (Cst == NVTBits) {
4667 Lo = InH;
4668 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004669 } else {
4670 Lo = DAG.getNode(ISD::OR, NVT,
4671 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4672 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4673 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4674 }
4675 return true;
4676 case ISD::SRA:
4677 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004678 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004679 DAG.getConstant(NVTBits-1, ShTy));
4680 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004681 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004682 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004683 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004684 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004685 } else if (Cst == NVTBits) {
4686 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004687 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004688 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004689 } else {
4690 Lo = DAG.getNode(ISD::OR, NVT,
4691 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4692 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4693 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4694 }
4695 return true;
4696 }
4697 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004698
4699 // Okay, the shift amount isn't constant. However, if we can tell that it is
4700 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4701 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004702 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004703
4704 // If we know that the high bit of the shift amount is one, then we can do
4705 // this as a couple of simple shifts.
4706 if (KnownOne & Mask) {
4707 // Mask out the high bit, which we know is set.
4708 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4709 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4710
4711 // Expand the incoming operand to be shifted, so that we have its parts
4712 SDOperand InL, InH;
4713 ExpandOp(Op, InL, InH);
4714 switch(Opc) {
4715 case ISD::SHL:
4716 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4717 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4718 return true;
4719 case ISD::SRL:
4720 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4721 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4722 return true;
4723 case ISD::SRA:
4724 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4725 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4726 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4727 return true;
4728 }
4729 }
4730
4731 // If we know that the high bit of the shift amount is zero, then we can do
4732 // this as a couple of simple shifts.
4733 if (KnownZero & Mask) {
4734 // Compute 32-amt.
4735 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4736 DAG.getConstant(NVTBits, Amt.getValueType()),
4737 Amt);
4738
4739 // Expand the incoming operand to be shifted, so that we have its parts
4740 SDOperand InL, InH;
4741 ExpandOp(Op, InL, InH);
4742 switch(Opc) {
4743 case ISD::SHL:
4744 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4745 Hi = DAG.getNode(ISD::OR, NVT,
4746 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4747 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4748 return true;
4749 case ISD::SRL:
4750 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4751 Lo = DAG.getNode(ISD::OR, NVT,
4752 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4753 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4754 return true;
4755 case ISD::SRA:
4756 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4757 Lo = DAG.getNode(ISD::OR, NVT,
4758 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4759 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4760 return true;
4761 }
4762 }
4763
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004764 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004765}
Chris Lattner77e77a62005-01-21 06:05:23 +00004766
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004767
Chris Lattner77e77a62005-01-21 06:05:23 +00004768// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4769// does not fit into a register, return the lo part and set the hi part to the
4770// by-reg argument. If it does fit into a single register, return the result
4771// and leave the Hi part unset.
4772SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004773 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004774 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4775 // The input chain to this libcall is the entry node of the function.
4776 // Legalizing the call will automatically add the previous call to the
4777 // dependence.
4778 SDOperand InChain = DAG.getEntryNode();
4779
Chris Lattner77e77a62005-01-21 06:05:23 +00004780 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004781 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004782 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4783 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4784 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004785 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004786 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004787 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004788 }
4789 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004790
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004791 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004792 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004793 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004794 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004795 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004796
Chris Lattner6831a812006-02-13 09:18:02 +00004797 // Legalize the call sequence, starting with the chain. This will advance
4798 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4799 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4800 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004801 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004802 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004803 default: assert(0 && "Unknown thing");
4804 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004805 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004806 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004807 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004808 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004809 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004810 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004811 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004812}
4813
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004814
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004815/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4816///
Chris Lattner77e77a62005-01-21 06:05:23 +00004817SDOperand SelectionDAGLegalize::
4818ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004819 assert(getTypeAction(Source.getValueType()) == Expand &&
4820 "This is not an expansion!");
4821 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4822
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004823 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004824 assert(Source.getValueType() == MVT::i64 &&
4825 "This only works for 64-bit -> FP");
4826 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4827 // incoming integer is set. To handle this, we dynamically test to see if
4828 // it is set, and, if so, add a fudge factor.
4829 SDOperand Lo, Hi;
4830 ExpandOp(Source, Lo, Hi);
4831
Chris Lattner66de05b2005-05-13 04:45:13 +00004832 // If this is unsigned, and not supported, first perform the conversion to
4833 // signed, then adjust the result if the sign bit is set.
4834 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4835 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4836
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004837 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4838 DAG.getConstant(0, Hi.getValueType()),
4839 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004840 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4841 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4842 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004843 uint64_t FF = 0x5f800000ULL;
4844 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004845 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004846
Chris Lattner5839bf22005-08-26 17:15:30 +00004847 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004848 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4849 SDOperand FudgeInReg;
4850 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004851 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004852 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004853 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004854 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004855 CPIdx, NULL, 0, MVT::f32);
4856 else
4857 assert(0 && "Unexpected conversion");
4858
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004859 MVT::ValueType SCVT = SignedConv.getValueType();
4860 if (SCVT != DestTy) {
4861 // Destination type needs to be expanded as well. The FADD now we are
4862 // constructing will be expanded into a libcall.
4863 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4864 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4865 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4866 SignedConv, SignedConv.getValue(1));
4867 }
4868 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4869 }
Chris Lattner473a9902005-09-29 06:44:39 +00004870 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004871 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004872
Chris Lattnera88a2602005-05-14 05:33:54 +00004873 // Check to see if the target has a custom way to lower this. If so, use it.
4874 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4875 default: assert(0 && "This action not implemented for this operation!");
4876 case TargetLowering::Legal:
4877 case TargetLowering::Expand:
4878 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004879 case TargetLowering::Custom: {
4880 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4881 Source), DAG);
4882 if (NV.Val)
4883 return LegalizeOp(NV);
4884 break; // The target decided this was legal after all
4885 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004886 }
4887
Chris Lattner13689e22005-05-12 07:00:44 +00004888 // Expand the source, then glue it back together for the call. We must expand
4889 // the source in case it is shared (this pass of legalize must traverse it).
4890 SDOperand SrcLo, SrcHi;
4891 ExpandOp(Source, SrcLo, SrcHi);
4892 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4893
Evan Cheng56966222007-01-12 02:11:51 +00004894 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004895 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004896 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004897 else {
4898 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004899 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004900 }
Chris Lattner6831a812006-02-13 09:18:02 +00004901
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004902 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004903 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4904 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004905 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4906 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004907}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004908
Chris Lattner22cde6a2006-01-28 08:25:58 +00004909/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4910/// INT_TO_FP operation of the specified operand when the target requests that
4911/// we expand it. At this point, we know that the result and operand types are
4912/// legal for the target.
4913SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4914 SDOperand Op0,
4915 MVT::ValueType DestVT) {
4916 if (Op0.getValueType() == MVT::i32) {
4917 // simple 32-bit [signed|unsigned] integer to float/double expansion
4918
Chris Lattner58092e32007-01-20 22:35:55 +00004919 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004920 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004921 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4922 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004923 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004924 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004925 // get address of 8 byte buffer
4926 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4927 // word offset constant for Hi/Lo address computation
4928 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4929 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004930 SDOperand Hi = StackSlot;
4931 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4932 if (TLI.isLittleEndian())
4933 std::swap(Hi, Lo);
4934
Chris Lattner22cde6a2006-01-28 08:25:58 +00004935 // if signed map to unsigned space
4936 SDOperand Op0Mapped;
4937 if (isSigned) {
4938 // constant used to invert sign bit (signed to unsigned mapping)
4939 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4940 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4941 } else {
4942 Op0Mapped = Op0;
4943 }
4944 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004945 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004946 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004947 // initial hi portion of constructed double
4948 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4949 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004950 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004951 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004952 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004953 // FP constant to bias correct the final result
4954 SDOperand Bias = DAG.getConstantFP(isSigned ?
4955 BitsToDouble(0x4330000080000000ULL)
4956 : BitsToDouble(0x4330000000000000ULL),
4957 MVT::f64);
4958 // subtract the bias
4959 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4960 // final result
4961 SDOperand Result;
4962 // handle final rounding
4963 if (DestVT == MVT::f64) {
4964 // do nothing
4965 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004966 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4967 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4968 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4969 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004970 }
4971 return Result;
4972 }
4973 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4974 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4975
4976 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4977 DAG.getConstant(0, Op0.getValueType()),
4978 ISD::SETLT);
4979 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4980 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4981 SignSet, Four, Zero);
4982
4983 // If the sign bit of the integer is set, the large number will be treated
4984 // as a negative number. To counteract this, the dynamic code adds an
4985 // offset depending on the data type.
4986 uint64_t FF;
4987 switch (Op0.getValueType()) {
4988 default: assert(0 && "Unsupported integer type!");
4989 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4990 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4991 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4992 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4993 }
4994 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004995 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004996
4997 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4998 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4999 SDOperand FudgeInReg;
5000 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005001 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005002 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005003 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005004 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005005 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005006 }
5007
5008 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5009}
5010
5011/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5012/// *INT_TO_FP operation of the specified operand when the target requests that
5013/// we promote it. At this point, we know that the result and operand types are
5014/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5015/// operation that takes a larger input.
5016SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5017 MVT::ValueType DestVT,
5018 bool isSigned) {
5019 // First step, figure out the appropriate *INT_TO_FP operation to use.
5020 MVT::ValueType NewInTy = LegalOp.getValueType();
5021
5022 unsigned OpToUse = 0;
5023
5024 // Scan for the appropriate larger type to use.
5025 while (1) {
5026 NewInTy = (MVT::ValueType)(NewInTy+1);
5027 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5028
5029 // If the target supports SINT_TO_FP of this type, use it.
5030 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5031 default: break;
5032 case TargetLowering::Legal:
5033 if (!TLI.isTypeLegal(NewInTy))
5034 break; // Can't use this datatype.
5035 // FALL THROUGH.
5036 case TargetLowering::Custom:
5037 OpToUse = ISD::SINT_TO_FP;
5038 break;
5039 }
5040 if (OpToUse) break;
5041 if (isSigned) continue;
5042
5043 // If the target supports UINT_TO_FP of this type, use it.
5044 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5045 default: break;
5046 case TargetLowering::Legal:
5047 if (!TLI.isTypeLegal(NewInTy))
5048 break; // Can't use this datatype.
5049 // FALL THROUGH.
5050 case TargetLowering::Custom:
5051 OpToUse = ISD::UINT_TO_FP;
5052 break;
5053 }
5054 if (OpToUse) break;
5055
5056 // Otherwise, try a larger type.
5057 }
5058
5059 // Okay, we found the operation and type to use. Zero extend our input to the
5060 // desired type then run the operation on it.
5061 return DAG.getNode(OpToUse, DestVT,
5062 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5063 NewInTy, LegalOp));
5064}
5065
5066/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5067/// FP_TO_*INT operation of the specified operand when the target requests that
5068/// we promote it. At this point, we know that the result and operand types are
5069/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5070/// operation that returns a larger result.
5071SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5072 MVT::ValueType DestVT,
5073 bool isSigned) {
5074 // First step, figure out the appropriate FP_TO*INT operation to use.
5075 MVT::ValueType NewOutTy = DestVT;
5076
5077 unsigned OpToUse = 0;
5078
5079 // Scan for the appropriate larger type to use.
5080 while (1) {
5081 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5082 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5083
5084 // If the target supports FP_TO_SINT returning this type, use it.
5085 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5086 default: break;
5087 case TargetLowering::Legal:
5088 if (!TLI.isTypeLegal(NewOutTy))
5089 break; // Can't use this datatype.
5090 // FALL THROUGH.
5091 case TargetLowering::Custom:
5092 OpToUse = ISD::FP_TO_SINT;
5093 break;
5094 }
5095 if (OpToUse) break;
5096
5097 // If the target supports FP_TO_UINT of this type, use it.
5098 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5099 default: break;
5100 case TargetLowering::Legal:
5101 if (!TLI.isTypeLegal(NewOutTy))
5102 break; // Can't use this datatype.
5103 // FALL THROUGH.
5104 case TargetLowering::Custom:
5105 OpToUse = ISD::FP_TO_UINT;
5106 break;
5107 }
5108 if (OpToUse) break;
5109
5110 // Otherwise, try a larger type.
5111 }
5112
5113 // Okay, we found the operation and type to use. Truncate the result of the
5114 // extended FP_TO_*INT operation to the desired size.
5115 return DAG.getNode(ISD::TRUNCATE, DestVT,
5116 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5117}
5118
5119/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5120///
5121SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5122 MVT::ValueType VT = Op.getValueType();
5123 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5124 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5125 switch (VT) {
5126 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5127 case MVT::i16:
5128 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5129 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5130 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5131 case MVT::i32:
5132 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5133 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5134 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5135 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5136 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5137 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5138 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5139 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5140 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5141 case MVT::i64:
5142 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5143 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5144 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5145 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5146 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5147 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5148 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5149 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5150 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5151 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5152 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5153 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5154 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5155 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5156 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5157 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5158 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5159 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5160 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5161 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5162 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5163 }
5164}
5165
5166/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5167///
5168SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5169 switch (Opc) {
5170 default: assert(0 && "Cannot expand this yet!");
5171 case ISD::CTPOP: {
5172 static const uint64_t mask[6] = {
5173 0x5555555555555555ULL, 0x3333333333333333ULL,
5174 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5175 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5176 };
5177 MVT::ValueType VT = Op.getValueType();
5178 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005179 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005180 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5181 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5182 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5183 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5184 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5185 DAG.getNode(ISD::AND, VT,
5186 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5187 }
5188 return Op;
5189 }
5190 case ISD::CTLZ: {
5191 // for now, we do this:
5192 // x = x | (x >> 1);
5193 // x = x | (x >> 2);
5194 // ...
5195 // x = x | (x >>16);
5196 // x = x | (x >>32); // for 64-bit input
5197 // return popcount(~x);
5198 //
5199 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5200 MVT::ValueType VT = Op.getValueType();
5201 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005202 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005203 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5204 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5205 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5206 }
5207 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5208 return DAG.getNode(ISD::CTPOP, VT, Op);
5209 }
5210 case ISD::CTTZ: {
5211 // for now, we use: { return popcount(~x & (x - 1)); }
5212 // unless the target has ctlz but not ctpop, in which case we use:
5213 // { return 32 - nlz(~x & (x-1)); }
5214 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5215 MVT::ValueType VT = Op.getValueType();
5216 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5217 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5218 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5219 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5220 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5221 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5222 TLI.isOperationLegal(ISD::CTLZ, VT))
5223 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005224 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005225 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5226 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5227 }
5228 }
5229}
Chris Lattnere34b3962005-01-19 04:19:40 +00005230
Chris Lattner3e928bb2005-01-07 07:47:09 +00005231/// ExpandOp - Expand the specified SDOperand into its two component pieces
5232/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5233/// LegalizeNodes map is filled in for any results that are not expanded, the
5234/// ExpandedNodes map is filled in for any results that are expanded, and the
5235/// Lo/Hi values are returned.
5236void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5237 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005238 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005239 SDNode *Node = Op.Val;
5240 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005241 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005242 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005243 "Cannot expand to FP value or to larger int value!");
5244
Chris Lattner6fdcb252005-09-02 20:32:45 +00005245 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005246 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005247 = ExpandedNodes.find(Op);
5248 if (I != ExpandedNodes.end()) {
5249 Lo = I->second.first;
5250 Hi = I->second.second;
5251 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005252 }
5253
Chris Lattner3e928bb2005-01-07 07:47:09 +00005254 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005255 case ISD::CopyFromReg:
5256 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005257 case ISD::FP_ROUND_INREG:
5258 if (VT == MVT::ppcf128 &&
5259 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5260 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005261 SDOperand SrcLo, SrcHi, Src;
5262 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5263 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5264 SDOperand Result = TLI.LowerOperation(
5265 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005266 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5267 Lo = Result.Val->getOperand(0);
5268 Hi = Result.Val->getOperand(1);
5269 break;
5270 }
5271 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005272 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005273#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005274 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005275#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005276 assert(0 && "Do not know how to expand this operator!");
5277 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005278 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005279 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005280 Lo = DAG.getNode(ISD::UNDEF, NVT);
5281 Hi = DAG.getNode(ISD::UNDEF, NVT);
5282 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005283 case ISD::Constant: {
5284 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5285 Lo = DAG.getConstant(Cst, NVT);
5286 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5287 break;
5288 }
Evan Cheng00495212006-12-12 21:32:44 +00005289 case ISD::ConstantFP: {
5290 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005291 if (CFP->getValueType(0) == MVT::ppcf128) {
5292 APInt api = CFP->getValueAPF().convertToAPInt();
5293 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5294 MVT::f64);
5295 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5296 MVT::f64);
5297 break;
5298 }
Evan Cheng279101e2006-12-12 22:19:28 +00005299 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005300 if (getTypeAction(Lo.getValueType()) == Expand)
5301 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005302 break;
5303 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005304 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005305 // Return the operands.
5306 Lo = Node->getOperand(0);
5307 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005308 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005309
5310 case ISD::SIGN_EXTEND_INREG:
5311 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005312 // sext_inreg the low part if needed.
5313 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5314
5315 // The high part gets the sign extension from the lo-part. This handles
5316 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005317 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5318 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5319 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005320 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005321
Nate Begemand88fc032006-01-14 03:14:10 +00005322 case ISD::BSWAP: {
5323 ExpandOp(Node->getOperand(0), Lo, Hi);
5324 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5325 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5326 Lo = TempLo;
5327 break;
5328 }
5329
Chris Lattneredb1add2005-05-11 04:51:16 +00005330 case ISD::CTPOP:
5331 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005332 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5333 DAG.getNode(ISD::CTPOP, NVT, Lo),
5334 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005335 Hi = DAG.getConstant(0, NVT);
5336 break;
5337
Chris Lattner39a8f332005-05-12 19:05:01 +00005338 case ISD::CTLZ: {
5339 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005340 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005341 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5342 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005343 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5344 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005345 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5346 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5347
5348 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5349 Hi = DAG.getConstant(0, NVT);
5350 break;
5351 }
5352
5353 case ISD::CTTZ: {
5354 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005355 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005356 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5357 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005358 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5359 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005360 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5361 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5362
5363 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5364 Hi = DAG.getConstant(0, NVT);
5365 break;
5366 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005367
Nate Begemanacc398c2006-01-25 18:21:52 +00005368 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005369 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5370 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005371 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5372 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5373
5374 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005375 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005376 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5377 if (!TLI.isLittleEndian())
5378 std::swap(Lo, Hi);
5379 break;
5380 }
5381
Chris Lattner3e928bb2005-01-07 07:47:09 +00005382 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005383 LoadSDNode *LD = cast<LoadSDNode>(Node);
5384 SDOperand Ch = LD->getChain(); // Legalize the chain.
5385 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5386 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005387 int SVOffset = LD->getSrcValueOffset();
5388 unsigned Alignment = LD->getAlignment();
5389 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005390
Evan Cheng466685d2006-10-09 20:57:25 +00005391 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005392 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5393 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005394 if (VT == MVT::f32 || VT == MVT::f64) {
5395 // f32->i32 or f64->i64 one to one expansion.
5396 // Remember that we legalized the chain.
5397 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005398 // Recursively expand the new load.
5399 if (getTypeAction(NVT) == Expand)
5400 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005401 break;
5402 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005403
Evan Cheng466685d2006-10-09 20:57:25 +00005404 // Increment the pointer to the other half.
5405 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5406 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5407 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005408 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005409 if (Alignment > IncrementSize)
5410 Alignment = IncrementSize;
5411 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5412 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005413
Evan Cheng466685d2006-10-09 20:57:25 +00005414 // Build a factor node to remember that this load is independent of the
5415 // other one.
5416 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5417 Hi.getValue(1));
5418
5419 // Remember that we legalized the chain.
5420 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5421 if (!TLI.isLittleEndian())
5422 std::swap(Lo, Hi);
5423 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005424 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005425
5426 if (VT == MVT::f64 && EVT == MVT::f32) {
5427 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5428 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005429 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005430 // Remember that we legalized the chain.
5431 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5432 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5433 break;
5434 }
Evan Cheng466685d2006-10-09 20:57:25 +00005435
5436 if (EVT == NVT)
5437 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005438 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005439 else
5440 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005441 SVOffset, EVT, isVolatile,
5442 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005443
5444 // Remember that we legalized the chain.
5445 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5446
5447 if (ExtType == ISD::SEXTLOAD) {
5448 // The high part is obtained by SRA'ing all but one of the bits of the
5449 // lo part.
5450 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5451 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5452 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5453 } else if (ExtType == ISD::ZEXTLOAD) {
5454 // The high part is just a zero.
5455 Hi = DAG.getConstant(0, NVT);
5456 } else /* if (ExtType == ISD::EXTLOAD) */ {
5457 // The high part is undefined.
5458 Hi = DAG.getNode(ISD::UNDEF, NVT);
5459 }
5460 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005461 break;
5462 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005463 case ISD::AND:
5464 case ISD::OR:
5465 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5466 SDOperand LL, LH, RL, RH;
5467 ExpandOp(Node->getOperand(0), LL, LH);
5468 ExpandOp(Node->getOperand(1), RL, RH);
5469 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5470 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5471 break;
5472 }
5473 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005474 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005475 ExpandOp(Node->getOperand(1), LL, LH);
5476 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005477 if (getTypeAction(NVT) == Expand)
5478 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005479 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005480 if (VT != MVT::f32)
5481 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005482 break;
5483 }
Nate Begeman9373a812005-08-10 20:51:12 +00005484 case ISD::SELECT_CC: {
5485 SDOperand TL, TH, FL, FH;
5486 ExpandOp(Node->getOperand(2), TL, TH);
5487 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005488 if (getTypeAction(NVT) == Expand)
5489 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005490 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5491 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005492 if (VT != MVT::f32)
5493 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5494 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005495 break;
5496 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005497 case ISD::ANY_EXTEND:
5498 // The low part is any extension of the input (which degenerates to a copy).
5499 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5500 // The high part is undefined.
5501 Hi = DAG.getNode(ISD::UNDEF, NVT);
5502 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005503 case ISD::SIGN_EXTEND: {
5504 // The low part is just a sign extension of the input (which degenerates to
5505 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005506 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005507
Chris Lattner3e928bb2005-01-07 07:47:09 +00005508 // The high part is obtained by SRA'ing all but one of the bits of the lo
5509 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005510 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005511 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5512 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005513 break;
5514 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005515 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005516 // The low part is just a zero extension of the input (which degenerates to
5517 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005518 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005519
Chris Lattner3e928bb2005-01-07 07:47:09 +00005520 // The high part is just a zero.
5521 Hi = DAG.getConstant(0, NVT);
5522 break;
Chris Lattner35481892005-12-23 00:16:34 +00005523
Chris Lattner4c948eb2007-02-13 23:55:16 +00005524 case ISD::TRUNCATE: {
5525 // The input value must be larger than this value. Expand *it*.
5526 SDOperand NewLo;
5527 ExpandOp(Node->getOperand(0), NewLo, Hi);
5528
5529 // The low part is now either the right size, or it is closer. If not the
5530 // right size, make an illegal truncate so we recursively expand it.
5531 if (NewLo.getValueType() != Node->getValueType(0))
5532 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5533 ExpandOp(NewLo, Lo, Hi);
5534 break;
5535 }
5536
Chris Lattner35481892005-12-23 00:16:34 +00005537 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005538 SDOperand Tmp;
5539 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5540 // If the target wants to, allow it to lower this itself.
5541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5542 case Expand: assert(0 && "cannot expand FP!");
5543 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5544 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5545 }
5546 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5547 }
5548
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005549 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005550 if (VT == MVT::f32 || VT == MVT::f64) {
5551 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005552 if (getTypeAction(NVT) == Expand)
5553 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005554 break;
5555 }
5556
5557 // If source operand will be expanded to the same type as VT, i.e.
5558 // i64 <- f64, i32 <- f32, expand the source operand instead.
5559 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5560 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5561 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005562 break;
5563 }
5564
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005565 // Turn this into a load/store pair by default.
5566 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005567 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005568
Chris Lattner35481892005-12-23 00:16:34 +00005569 ExpandOp(Tmp, Lo, Hi);
5570 break;
5571 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005572
Chris Lattner8137c9e2006-01-28 05:07:51 +00005573 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005574 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5575 TargetLowering::Custom &&
5576 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005577 Lo = TLI.LowerOperation(Op, DAG);
5578 assert(Lo.Val && "Node must be custom expanded!");
5579 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005580 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005581 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005582 break;
5583
Chris Lattner4e6c7462005-01-08 19:27:05 +00005584 // These operators cannot be expanded directly, emit them as calls to
5585 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005586 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005587 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005588 SDOperand Op;
5589 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5590 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005591 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5592 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005593 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005594
Chris Lattnerf20d1832005-07-30 01:40:57 +00005595 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5596
Chris Lattner80a3e942005-07-29 00:33:32 +00005597 // Now that the custom expander is done, expand the result, which is still
5598 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005599 if (Op.Val) {
5600 ExpandOp(Op, Lo, Hi);
5601 break;
5602 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005603 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005604
Dale Johannesen161e8972007-10-05 20:04:43 +00005605 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005606 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005607 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005608 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005609 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005610 else if (Node->getOperand(0).getValueType() == MVT::f80)
5611 LC = RTLIB::FPTOSINT_F80_I64;
5612 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5613 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005614 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5615 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005616 break;
Evan Cheng56966222007-01-12 02:11:51 +00005617 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005618
Evan Cheng56966222007-01-12 02:11:51 +00005619 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005620 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005621 SDOperand Op;
5622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5623 case Expand: assert(0 && "cannot expand FP!");
5624 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5625 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5626 }
5627
5628 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5629
5630 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005631 if (Op.Val) {
5632 ExpandOp(Op, Lo, Hi);
5633 break;
5634 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005635 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005636
Evan Chengdaccea182007-10-05 01:09:32 +00005637 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005638 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005639 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005640 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005641 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005642 else if (Node->getOperand(0).getValueType() == MVT::f80)
5643 LC = RTLIB::FPTOUINT_F80_I64;
5644 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5645 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005646 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5647 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005648 break;
Evan Cheng56966222007-01-12 02:11:51 +00005649 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005650
Evan Cheng05a2d562006-01-09 18:31:59 +00005651 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005652 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005653 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005654 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005655 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005656 Op = TLI.LowerOperation(Op, DAG);
5657 if (Op.Val) {
5658 // Now that the custom expander is done, expand the result, which is
5659 // still VT.
5660 ExpandOp(Op, Lo, Hi);
5661 break;
5662 }
5663 }
5664
Chris Lattner79980b02006-09-13 03:50:39 +00005665 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5666 // this X << 1 as X+X.
5667 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5668 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5669 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5670 SDOperand LoOps[2], HiOps[3];
5671 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5672 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5673 LoOps[1] = LoOps[0];
5674 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5675
5676 HiOps[1] = HiOps[0];
5677 HiOps[2] = Lo.getValue(1);
5678 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5679 break;
5680 }
5681 }
5682
Chris Lattnere34b3962005-01-19 04:19:40 +00005683 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005684 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005685 break;
Chris Lattner47599822005-04-02 03:38:53 +00005686
5687 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005688 TargetLowering::LegalizeAction Action =
5689 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5690 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5691 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005692 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005693 break;
5694 }
5695
Chris Lattnere34b3962005-01-19 04:19:40 +00005696 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005697 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5698 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005699 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005700 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005701
Evan Cheng05a2d562006-01-09 18:31:59 +00005702 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005703 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005704 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005705 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005706 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005707 Op = TLI.LowerOperation(Op, DAG);
5708 if (Op.Val) {
5709 // Now that the custom expander is done, expand the result, which is
5710 // still VT.
5711 ExpandOp(Op, Lo, Hi);
5712 break;
5713 }
5714 }
5715
Chris Lattnere34b3962005-01-19 04:19:40 +00005716 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005717 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005718 break;
Chris Lattner47599822005-04-02 03:38:53 +00005719
5720 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005721 TargetLowering::LegalizeAction Action =
5722 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5723 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5724 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005725 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005726 break;
5727 }
5728
Chris Lattnere34b3962005-01-19 04:19:40 +00005729 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005730 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5731 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005732 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005733 }
5734
5735 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005736 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005737 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005738 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005739 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005740 Op = TLI.LowerOperation(Op, DAG);
5741 if (Op.Val) {
5742 // Now that the custom expander is done, expand the result, which is
5743 // still VT.
5744 ExpandOp(Op, Lo, Hi);
5745 break;
5746 }
5747 }
5748
Chris Lattnere34b3962005-01-19 04:19:40 +00005749 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005750 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005751 break;
Chris Lattner47599822005-04-02 03:38:53 +00005752
5753 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005754 TargetLowering::LegalizeAction Action =
5755 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5756 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5757 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005758 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005759 break;
5760 }
5761
Chris Lattnere34b3962005-01-19 04:19:40 +00005762 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005763 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5764 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005765 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005766 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005767
Misha Brukmanedf128a2005-04-21 22:36:52 +00005768 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005769 case ISD::SUB: {
5770 // If the target wants to custom expand this, let them.
5771 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5772 TargetLowering::Custom) {
5773 Op = TLI.LowerOperation(Op, DAG);
5774 if (Op.Val) {
5775 ExpandOp(Op, Lo, Hi);
5776 break;
5777 }
5778 }
5779
5780 // Expand the subcomponents.
5781 SDOperand LHSL, LHSH, RHSL, RHSH;
5782 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5783 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005784 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5785 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005786 LoOps[0] = LHSL;
5787 LoOps[1] = RHSL;
5788 HiOps[0] = LHSH;
5789 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005790 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005791 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005792 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005793 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005794 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005795 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005796 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005797 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005798 }
Chris Lattner84f67882005-01-20 18:52:28 +00005799 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005800 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005801
5802 case ISD::ADDC:
5803 case ISD::SUBC: {
5804 // Expand the subcomponents.
5805 SDOperand LHSL, LHSH, RHSL, RHSH;
5806 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5807 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5808 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5809 SDOperand LoOps[2] = { LHSL, RHSL };
5810 SDOperand HiOps[3] = { LHSH, RHSH };
5811
5812 if (Node->getOpcode() == ISD::ADDC) {
5813 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5814 HiOps[2] = Lo.getValue(1);
5815 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5816 } else {
5817 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5818 HiOps[2] = Lo.getValue(1);
5819 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5820 }
5821 // Remember that we legalized the flag.
5822 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5823 break;
5824 }
5825 case ISD::ADDE:
5826 case ISD::SUBE: {
5827 // Expand the subcomponents.
5828 SDOperand LHSL, LHSH, RHSL, RHSH;
5829 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5830 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5831 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5832 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5833 SDOperand HiOps[3] = { LHSH, RHSH };
5834
5835 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5836 HiOps[2] = Lo.getValue(1);
5837 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5838
5839 // Remember that we legalized the flag.
5840 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5841 break;
5842 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005843 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005844 // If the target wants to custom expand this, let them.
5845 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005846 SDOperand New = TLI.LowerOperation(Op, DAG);
5847 if (New.Val) {
5848 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005849 break;
5850 }
5851 }
5852
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005853 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5854 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00005855 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5856 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5857 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005858 SDOperand LL, LH, RL, RH;
5859 ExpandOp(Node->getOperand(0), LL, LH);
5860 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00005861 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5862 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5863 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5864 // FIXME: generalize this to handle other bit sizes
5865 if (LHSSB == 32 && RHSSB == 32 &&
5866 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5867 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5868 // The inputs are both zero-extended.
5869 if (HasUMUL_LOHI) {
5870 // We can emit a umul_lohi.
5871 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5872 Hi = SDOperand(Lo.Val, 1);
5873 break;
5874 }
5875 if (HasMULHU) {
5876 // We can emit a mulhu+mul.
5877 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5878 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5879 break;
5880 }
Dan Gohman525178c2007-10-08 18:33:35 +00005881 }
5882 if (LHSSB > BitSize && RHSSB > BitSize) {
5883 // The input values are both sign-extended.
5884 if (HasSMUL_LOHI) {
5885 // We can emit a smul_lohi.
5886 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5887 Hi = SDOperand(Lo.Val, 1);
5888 break;
5889 }
5890 if (HasMULHS) {
5891 // We can emit a mulhs+mul.
5892 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5893 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5894 break;
5895 }
5896 }
5897 if (HasUMUL_LOHI) {
5898 // Lo,Hi = umul LHS, RHS.
5899 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5900 DAG.getVTList(NVT, NVT), LL, RL);
5901 Lo = UMulLOHI;
5902 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00005903 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5904 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5905 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5906 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005907 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005908 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005909 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005910
Dan Gohman525178c2007-10-08 18:33:35 +00005911 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005912 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5913 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005914 break;
5915 }
Evan Cheng56966222007-01-12 02:11:51 +00005916 case ISD::SDIV:
5917 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5918 break;
5919 case ISD::UDIV:
5920 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5921 break;
5922 case ISD::SREM:
5923 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5924 break;
5925 case ISD::UREM:
5926 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5927 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005928
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005929 case ISD::FADD:
Dale Johannesen161e8972007-10-05 20:04:43 +00005930 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5931 VT == MVT::f64 ? RTLIB::ADD_F64 :
5932 VT == MVT::ppcf128 ?
5933 RTLIB::ADD_PPCF128 :
5934 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005935 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005936 break;
5937 case ISD::FSUB:
Dale Johannesen161e8972007-10-05 20:04:43 +00005938 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5939 VT == MVT::f64 ? RTLIB::SUB_F64 :
5940 VT == MVT::ppcf128 ?
5941 RTLIB::SUB_PPCF128 :
5942 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005943 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005944 break;
5945 case ISD::FMUL:
Dale Johannesen161e8972007-10-05 20:04:43 +00005946 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5947 VT == MVT::f64 ? RTLIB::MUL_F64 :
5948 VT == MVT::ppcf128 ?
5949 RTLIB::MUL_PPCF128 :
5950 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005951 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005952 break;
5953 case ISD::FDIV:
Dale Johannesen161e8972007-10-05 20:04:43 +00005954 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5955 VT == MVT::f64 ? RTLIB::DIV_F64 :
5956 VT == MVT::ppcf128 ?
5957 RTLIB::DIV_PPCF128 :
5958 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00005959 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005960 break;
5961 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00005962 if (VT == MVT::ppcf128) {
5963 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
5964 Node->getOperand(0).getValueType()==MVT::f64);
5965 const uint64_t zero = 0;
5966 if (Node->getOperand(0).getValueType()==MVT::f32)
5967 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
5968 else
5969 Hi = Node->getOperand(0);
5970 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
5971 break;
5972 }
Evan Cheng56966222007-01-12 02:11:51 +00005973 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005974 break;
5975 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005976 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005977 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005978 case ISD::FPOWI:
Dale Johannesen317096a2007-09-28 01:08:20 +00005979 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
5980 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesen161e8972007-10-05 20:04:43 +00005981 (VT == MVT::f80) ? RTLIB::POWI_F80 :
5982 (VT == MVT::ppcf128) ?
5983 RTLIB::POWI_PPCF128 :
5984 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005985 Node, false, Hi);
5986 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005987 case ISD::FSQRT:
5988 case ISD::FSIN:
5989 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005990 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005991 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005992 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00005993 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00005994 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
5995 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
5996 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
5997 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00005998 break;
5999 case ISD::FSIN:
6000 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6001 break;
6002 case ISD::FCOS:
6003 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6004 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006005 default: assert(0 && "Unreachable!");
6006 }
Evan Cheng56966222007-01-12 02:11:51 +00006007 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006008 break;
6009 }
Evan Cheng966bf242006-12-16 00:52:40 +00006010 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006011 if (VT == MVT::ppcf128) {
6012 SDOperand Tmp;
6013 ExpandOp(Node->getOperand(0), Lo, Tmp);
6014 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6015 // lo = hi==fabs(hi) ? lo : -lo;
6016 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6017 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6018 DAG.getCondCode(ISD::SETEQ));
6019 break;
6020 }
Evan Cheng966bf242006-12-16 00:52:40 +00006021 SDOperand Mask = (VT == MVT::f64)
6022 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6023 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6024 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6025 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6026 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6027 if (getTypeAction(NVT) == Expand)
6028 ExpandOp(Lo, Lo, Hi);
6029 break;
6030 }
6031 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006032 if (VT == MVT::ppcf128) {
6033 ExpandOp(Node->getOperand(0), Lo, Hi);
6034 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6035 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6036 break;
6037 }
Evan Cheng966bf242006-12-16 00:52:40 +00006038 SDOperand Mask = (VT == MVT::f64)
6039 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6040 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6041 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6042 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6043 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6044 if (getTypeAction(NVT) == Expand)
6045 ExpandOp(Lo, Lo, Hi);
6046 break;
6047 }
Evan Cheng912095b2007-01-04 21:56:39 +00006048 case ISD::FCOPYSIGN: {
6049 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6050 if (getTypeAction(NVT) == Expand)
6051 ExpandOp(Lo, Lo, Hi);
6052 break;
6053 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006054 case ISD::SINT_TO_FP:
6055 case ISD::UINT_TO_FP: {
6056 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6057 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006058 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006059 static uint64_t zero = 0;
6060 if (isSigned) {
6061 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6062 Node->getOperand(0)));
6063 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6064 } else {
6065 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6066 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6067 Node->getOperand(0)));
6068 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6069 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006070 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006071 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6072 DAG.getConstant(0, MVT::i32),
6073 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6074 DAG.getConstantFP(
6075 APFloat(APInt(128, 2, TwoE32)),
6076 MVT::ppcf128)),
6077 Hi,
6078 DAG.getCondCode(ISD::SETLT)),
6079 Lo, Hi);
6080 }
6081 break;
6082 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006083 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6084 // si64->ppcf128 done by libcall, below
6085 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6086 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6087 Lo, Hi);
6088 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6089 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6090 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6091 DAG.getConstant(0, MVT::i64),
6092 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6093 DAG.getConstantFP(
6094 APFloat(APInt(128, 2, TwoE64)),
6095 MVT::ppcf128)),
6096 Hi,
6097 DAG.getCondCode(ISD::SETLT)),
6098 Lo, Hi);
6099 break;
6100 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006101 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006102 if (Node->getOperand(0).getValueType() == MVT::i64) {
6103 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006104 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006105 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006106 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006107 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006108 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006109 LC = RTLIB::SINTTOFP_I64_F80;
6110 }
6111 else if (VT == MVT::ppcf128) {
6112 assert(isSigned);
6113 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006114 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006115 } else {
6116 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006117 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006118 else
Evan Cheng56966222007-01-12 02:11:51 +00006119 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006120 }
6121
6122 // Promote the operand if needed.
6123 if (getTypeAction(SrcVT) == Promote) {
6124 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6125 Tmp = isSigned
6126 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6127 DAG.getValueType(SrcVT))
6128 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6129 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6130 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006131
6132 const char *LibCall = TLI.getLibcallName(LC);
6133 if (LibCall)
6134 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6135 else {
6136 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6137 Node->getOperand(0));
6138 if (getTypeAction(Lo.getValueType()) == Expand)
6139 ExpandOp(Lo, Lo, Hi);
6140 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006141 break;
6142 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006143 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006144
Chris Lattner83397362005-12-21 18:02:52 +00006145 // Make sure the resultant values have been legalized themselves, unless this
6146 // is a type that requires multi-step expansion.
6147 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6148 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006149 if (Hi.Val)
6150 // Don't legalize the high part if it is expanded to a single node.
6151 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006152 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006153
6154 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006155 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006156 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006157}
6158
Dan Gohman7f321562007-06-25 16:23:39 +00006159/// SplitVectorOp - Given an operand of vector type, break it down into
6160/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006161void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6162 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006163 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006164 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006165 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006166 assert(NumElements > 1 && "Cannot split a single element vector!");
6167 unsigned NewNumElts = NumElements/2;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006168 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00006169 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00006170
6171 // See if we already split it.
6172 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6173 = SplitNodes.find(Op);
6174 if (I != SplitNodes.end()) {
6175 Lo = I->second.first;
6176 Hi = I->second.second;
6177 return;
6178 }
6179
6180 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006181 default:
6182#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006183 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006184#endif
6185 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00006186 case ISD::BUILD_PAIR:
6187 Lo = Node->getOperand(0);
6188 Hi = Node->getOperand(1);
6189 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006190 case ISD::INSERT_VECTOR_ELT: {
6191 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6192 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6193 SDOperand ScalarOp = Node->getOperand(1);
6194 if (Index < NewNumElts)
6195 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6196 DAG.getConstant(Index, TLI.getPointerTy()));
6197 else
6198 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6199 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6200 break;
6201 }
Dan Gohman7f321562007-06-25 16:23:39 +00006202 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006203 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6204 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00006205 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006206
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006207 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00006208 Node->op_end());
6209 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006210 break;
6211 }
Dan Gohman7f321562007-06-25 16:23:39 +00006212 case ISD::CONCAT_VECTORS: {
6213 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6214 if (NewNumSubvectors == 1) {
6215 Lo = Node->getOperand(0);
6216 Hi = Node->getOperand(1);
6217 } else {
6218 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6219 Node->op_begin()+NewNumSubvectors);
6220 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006221
Dan Gohman7f321562007-06-25 16:23:39 +00006222 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6223 Node->op_end());
6224 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6225 }
Dan Gohman65956352007-06-13 15:12:02 +00006226 break;
6227 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006228 case ISD::SELECT: {
6229 SDOperand Cond = Node->getOperand(0);
6230
6231 SDOperand LL, LH, RL, RH;
6232 SplitVectorOp(Node->getOperand(1), LL, LH);
6233 SplitVectorOp(Node->getOperand(2), RL, RH);
6234
6235 if (MVT::isVector(Cond.getValueType())) {
6236 // Handle a vector merge.
6237 SDOperand CL, CH;
6238 SplitVectorOp(Cond, CL, CH);
6239 Lo = DAG.getNode(Node->getOpcode(), NewVT, CL, LL, RL);
6240 Hi = DAG.getNode(Node->getOpcode(), NewVT, CH, LH, RH);
6241 } else {
6242 // Handle a simple select with vector operands.
6243 Lo = DAG.getNode(Node->getOpcode(), NewVT, Cond, LL, RL);
6244 Hi = DAG.getNode(Node->getOpcode(), NewVT, Cond, LH, RH);
6245 }
6246 break;
6247 }
Dan Gohman7f321562007-06-25 16:23:39 +00006248 case ISD::ADD:
6249 case ISD::SUB:
6250 case ISD::MUL:
6251 case ISD::FADD:
6252 case ISD::FSUB:
6253 case ISD::FMUL:
6254 case ISD::SDIV:
6255 case ISD::UDIV:
6256 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006257 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006258 case ISD::AND:
6259 case ISD::OR:
6260 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006261 SDOperand LL, LH, RL, RH;
6262 SplitVectorOp(Node->getOperand(0), LL, LH);
6263 SplitVectorOp(Node->getOperand(1), RL, RH);
6264
Dan Gohman7f321562007-06-25 16:23:39 +00006265 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6266 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006267 break;
6268 }
Dan Gohman82669522007-10-11 23:57:53 +00006269 case ISD::FPOWI: {
6270 SDOperand L, H;
6271 SplitVectorOp(Node->getOperand(0), L, H);
6272
6273 Lo = DAG.getNode(Node->getOpcode(), NewVT, L, Node->getOperand(1));
6274 Hi = DAG.getNode(Node->getOpcode(), NewVT, H, Node->getOperand(1));
6275 break;
6276 }
6277 case ISD::CTTZ:
6278 case ISD::CTLZ:
6279 case ISD::CTPOP:
6280 case ISD::FNEG:
6281 case ISD::FABS:
6282 case ISD::FSQRT:
6283 case ISD::FSIN:
6284 case ISD::FCOS: {
6285 SDOperand L, H;
6286 SplitVectorOp(Node->getOperand(0), L, H);
6287
6288 Lo = DAG.getNode(Node->getOpcode(), NewVT, L);
6289 Hi = DAG.getNode(Node->getOpcode(), NewVT, H);
6290 break;
6291 }
Dan Gohman7f321562007-06-25 16:23:39 +00006292 case ISD::LOAD: {
6293 LoadSDNode *LD = cast<LoadSDNode>(Node);
6294 SDOperand Ch = LD->getChain();
6295 SDOperand Ptr = LD->getBasePtr();
6296 const Value *SV = LD->getSrcValue();
6297 int SVOffset = LD->getSrcValueOffset();
6298 unsigned Alignment = LD->getAlignment();
6299 bool isVolatile = LD->isVolatile();
6300
6301 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6302 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006303 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6304 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006305 SVOffset += IncrementSize;
6306 if (Alignment > IncrementSize)
6307 Alignment = IncrementSize;
6308 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006309
6310 // Build a factor node to remember that this load is independent of the
6311 // other one.
6312 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6313 Hi.getValue(1));
6314
6315 // Remember that we legalized the chain.
6316 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006317 break;
6318 }
Dan Gohman7f321562007-06-25 16:23:39 +00006319 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006320 // We know the result is a vector. The input may be either a vector or a
6321 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006322 SDOperand InOp = Node->getOperand(0);
6323 if (!MVT::isVector(InOp.getValueType()) ||
6324 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6325 // The input is a scalar or single-element vector.
6326 // Lower to a store/load so that it can be split.
6327 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006328 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006329
Evan Cheng786225a2006-10-05 23:01:46 +00006330 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006331 InOp, Ptr, NULL, 0);
6332 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006333 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006334 // Split the vector and convert each of the pieces now.
6335 SplitVectorOp(InOp, Lo, Hi);
6336 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6337 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6338 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006339 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006340 }
6341
6342 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006343 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006344 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006345 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006346}
6347
6348
Dan Gohman89b20c02007-06-27 14:06:22 +00006349/// ScalarizeVectorOp - Given an operand of single-element vector type
6350/// (e.g. v1f32), convert it into the equivalent operation that returns a
6351/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006352SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6353 assert(MVT::isVector(Op.getValueType()) &&
6354 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006355 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006356 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6357 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006358
Dan Gohman7f321562007-06-25 16:23:39 +00006359 // See if we already scalarized it.
6360 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6361 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006362
6363 SDOperand Result;
6364 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006365 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006366#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006367 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006368#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006369 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6370 case ISD::ADD:
6371 case ISD::FADD:
6372 case ISD::SUB:
6373 case ISD::FSUB:
6374 case ISD::MUL:
6375 case ISD::FMUL:
6376 case ISD::SDIV:
6377 case ISD::UDIV:
6378 case ISD::FDIV:
6379 case ISD::SREM:
6380 case ISD::UREM:
6381 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006382 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006383 case ISD::AND:
6384 case ISD::OR:
6385 case ISD::XOR:
6386 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006387 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006388 ScalarizeVectorOp(Node->getOperand(0)),
6389 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006390 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006391 case ISD::FNEG:
6392 case ISD::FABS:
6393 case ISD::FSQRT:
6394 case ISD::FSIN:
6395 case ISD::FCOS:
6396 Result = DAG.getNode(Node->getOpcode(),
6397 NewVT,
6398 ScalarizeVectorOp(Node->getOperand(0)));
6399 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006400 case ISD::FPOWI:
6401 Result = DAG.getNode(Node->getOpcode(),
6402 NewVT,
6403 ScalarizeVectorOp(Node->getOperand(0)),
6404 Node->getOperand(1));
6405 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006406 case ISD::LOAD: {
6407 LoadSDNode *LD = cast<LoadSDNode>(Node);
6408 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6409 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006410
Dan Gohman7f321562007-06-25 16:23:39 +00006411 const Value *SV = LD->getSrcValue();
6412 int SVOffset = LD->getSrcValueOffset();
6413 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6414 LD->isVolatile(), LD->getAlignment());
6415
Chris Lattnerc7029802006-03-18 01:44:44 +00006416 // Remember that we legalized the chain.
6417 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6418 break;
6419 }
Dan Gohman7f321562007-06-25 16:23:39 +00006420 case ISD::BUILD_VECTOR:
6421 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006422 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006423 case ISD::INSERT_VECTOR_ELT:
6424 // Returning the inserted scalar element.
6425 Result = Node->getOperand(1);
6426 break;
6427 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006428 assert(Node->getOperand(0).getValueType() == NewVT &&
6429 "Concat of non-legal vectors not yet supported!");
6430 Result = Node->getOperand(0);
6431 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006432 case ISD::VECTOR_SHUFFLE: {
6433 // Figure out if the scalar is the LHS or RHS and return it.
6434 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6435 if (cast<ConstantSDNode>(EltNum)->getValue())
6436 Result = ScalarizeVectorOp(Node->getOperand(1));
6437 else
6438 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006439 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006440 }
6441 case ISD::EXTRACT_SUBVECTOR:
6442 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006443 assert(Result.getValueType() == NewVT);
6444 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006445 case ISD::BIT_CONVERT:
6446 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006447 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006448 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006449 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006450 ScalarizeVectorOp(Op.getOperand(1)),
6451 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006452 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006453 }
6454
6455 if (TLI.isTypeLegal(NewVT))
6456 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006457 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6458 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006459 return Result;
6460}
6461
Chris Lattner3e928bb2005-01-07 07:47:09 +00006462
6463// SelectionDAG::Legalize - This is the entry point for the file.
6464//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006465void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006466 if (ViewLegalizeDAGs) viewGraph();
6467
Chris Lattner3e928bb2005-01-07 07:47:09 +00006468 /// run - This is the main entry point to this class.
6469 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006470 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006471}
6472