blob: aa8ffaae071146bdc74eb5a11bdf67214421e81c [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/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000028#include "llvm/Support/MathExtras.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()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000605 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
607 ST->getSrcValue(), SVOffset + IncrementSize,
608 NewStoredVT, ST->isVolatile(), Alignment);
609
610 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
611}
612
613/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
614static
615SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
616 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617 int SVOffset = LD->getSrcValueOffset();
618 SDOperand Chain = LD->getChain();
619 SDOperand Ptr = LD->getBasePtr();
620 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000621 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000622 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000623 // Expand to a (misaligned) integer load of the same size,
624 // then bitconvert to floating point.
625 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000626 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000627 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000628 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 intVT = MVT::i32;
630 else
631 assert(0 && "Unaligned load of unsupported floating point type");
632
633 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
634 SVOffset, LD->isVolatile(),
635 LD->getAlignment());
636 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
637 if (LoadedVT != VT)
638 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
639
640 SDOperand Ops[] = { Result, Chain };
641 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
642 Ops, 2);
643 }
Chris Lattnere400af82007-11-19 21:38:03 +0000644 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
645 "Unaligned load of unsupported type.");
646
647 // Compute the new VT that is half the size of the old one. We either have an
648 // integer MVT or we have a vector MVT.
649 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
650 MVT::ValueType NewLoadedVT;
651 if (!MVT::isVector(LoadedVT)) {
652 NewLoadedVT = MVT::getIntegerType(NumBits/2);
653 } else {
654 // FIXME: This is not right for <1 x anything> it is also not right for
655 // non-power-of-two vectors.
656 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
657 MVT::getVectorNumElements(LoadedVT)/2);
658 }
659 NumBits >>= 1;
660
661 unsigned Alignment = LD->getAlignment();
662 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000663 ISD::LoadExtType HiExtType = LD->getExtensionType();
664
665 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
666 if (HiExtType == ISD::NON_EXTLOAD)
667 HiExtType = ISD::ZEXTLOAD;
668
669 // Load the value in two parts
670 SDOperand Lo, Hi;
671 if (TLI.isLittleEndian()) {
672 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
673 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
674 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
675 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
676 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
677 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000678 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000679 } else {
680 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
681 NewLoadedVT,LD->isVolatile(), Alignment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
683 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
684 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
685 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000686 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000687 }
688
689 // aggregate the two parts
690 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
691 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
692 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
693
694 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
695 Hi.getValue(1));
696
697 SDOperand Ops[] = { Result, TF };
698 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
699}
Evan Cheng912095b2007-01-04 21:56:39 +0000700
Dan Gohman82669522007-10-11 23:57:53 +0000701/// UnrollVectorOp - We know that the given vector has a legal type, however
702/// the operation it performs is not legal and is an operation that we have
703/// no way of lowering. "Unroll" the vector, splitting out the scalars and
704/// operating on each element individually.
705SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
706 MVT::ValueType VT = Op.getValueType();
707 assert(isTypeLegal(VT) &&
708 "Caller should expand or promote operands that are not legal!");
709 assert(Op.Val->getNumValues() == 1 &&
710 "Can't unroll a vector with multiple results!");
711 unsigned NE = MVT::getVectorNumElements(VT);
712 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
713
714 SmallVector<SDOperand, 8> Scalars;
715 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
716 for (unsigned i = 0; i != NE; ++i) {
717 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
718 SDOperand Operand = Op.getOperand(j);
719 MVT::ValueType OperandVT = Operand.getValueType();
720 if (MVT::isVector(OperandVT)) {
721 // A vector operand; extract a single element.
722 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
723 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
724 OperandEltVT,
725 Operand,
726 DAG.getConstant(i, MVT::i32));
727 } else {
728 // A scalar operand; just use it as is.
729 Operands[j] = Operand;
730 }
731 }
732 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
733 &Operands[0], Operands.size()));
734 }
735
736 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
737}
738
Dan Gohmana3466152007-07-13 20:14:11 +0000739/// LegalizeOp - We know that the specified value has a legal type, and
740/// that its operands are legal. Now ensure that the operation itself
741/// is legal, recursively ensuring that the operands' operations remain
742/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000743SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000744 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
745 return Op;
746
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000747 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000748 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000749 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000750
Chris Lattner3e928bb2005-01-07 07:47:09 +0000751 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000752 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000753 if (Node->getNumValues() > 1) {
754 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000755 if (getTypeAction(Node->getValueType(i)) != Legal) {
756 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000757 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000758 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000759 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000760 }
761 }
762
Chris Lattner45982da2005-05-12 16:53:42 +0000763 // Note that LegalizeOp may be reentered even from single-use nodes, which
764 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000765 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000766 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000767
Nate Begeman9373a812005-08-10 20:51:12 +0000768 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000770 bool isCustom = false;
771
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000773 case ISD::FrameIndex:
774 case ISD::EntryToken:
775 case ISD::Register:
776 case ISD::BasicBlock:
777 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000778 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000779 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000780 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000781 case ISD::TargetConstantPool:
782 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000783 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000784 case ISD::TargetExternalSymbol:
785 case ISD::VALUETYPE:
786 case ISD::SRCVALUE:
787 case ISD::STRING:
788 case ISD::CONDCODE:
789 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000790 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000791 "This must be legal!");
792 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000793 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000794 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
795 // If this is a target node, legalize it by legalizing the operands then
796 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000797 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000798 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000799 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000800
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000801 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000802
803 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
804 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
805 return Result.getValue(Op.ResNo);
806 }
807 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000808#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000809 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000810#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811 assert(0 && "Do not know how to legalize this operator!");
812 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000813 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000814 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000815 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000816 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000817 case ISD::ConstantPool:
818 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000819 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
820 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000821 case TargetLowering::Custom:
822 Tmp1 = TLI.LowerOperation(Op, DAG);
823 if (Tmp1.Val) Result = Tmp1;
824 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000825 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000826 break;
827 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000828 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000829 case ISD::FRAMEADDR:
830 case ISD::RETURNADDR:
831 // The only option for these nodes is to custom lower them. If the target
832 // does not custom lower them, then return zero.
833 Tmp1 = TLI.LowerOperation(Op, DAG);
834 if (Tmp1.Val)
835 Result = Tmp1;
836 else
837 Result = DAG.getConstant(0, TLI.getPointerTy());
838 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000839 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000840 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::Custom:
844 Result = TLI.LowerOperation(Op, DAG);
845 if (Result.Val) break;
846 // Fall Thru
847 case TargetLowering::Legal:
848 Result = DAG.getConstant(0, VT);
849 break;
850 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000851 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000852 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000853 case ISD::EXCEPTIONADDR: {
854 Tmp1 = LegalizeOp(Node->getOperand(0));
855 MVT::ValueType VT = Node->getValueType(0);
856 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
857 default: assert(0 && "This action is not supported yet!");
858 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000859 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000860 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
861 }
862 break;
863 case TargetLowering::Custom:
864 Result = TLI.LowerOperation(Op, DAG);
865 if (Result.Val) break;
866 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000867 case TargetLowering::Legal: {
868 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
869 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
870 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000871 break;
872 }
873 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000874 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000875 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000876 case ISD::EHSELECTION: {
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 Tmp2 = LegalizeOp(Node->getOperand(1));
879 MVT::ValueType VT = Node->getValueType(0);
880 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
881 default: assert(0 && "This action is not supported yet!");
882 case TargetLowering::Expand: {
883 unsigned Reg = TLI.getExceptionSelectorRegister();
884 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
885 }
886 break;
887 case TargetLowering::Custom:
888 Result = TLI.LowerOperation(Op, DAG);
889 if (Result.Val) break;
890 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000891 case TargetLowering::Legal: {
892 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
893 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
894 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000895 break;
896 }
897 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000898 }
Jim Laskey8782d482007-02-28 20:43:58 +0000899 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000900 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000901 MVT::ValueType VT = Node->getValueType(0);
902 // The only "good" option for this node is to custom lower it.
903 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
904 default: assert(0 && "This action is not supported at all!");
905 case TargetLowering::Custom:
906 Result = TLI.LowerOperation(Op, DAG);
907 if (Result.Val) break;
908 // Fall Thru
909 case TargetLowering::Legal:
910 // Target does not know, how to lower this, lower to noop
911 Result = LegalizeOp(Node->getOperand(0));
912 break;
913 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000914 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000915 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000916 case ISD::AssertSext:
917 case ISD::AssertZext:
918 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000920 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000921 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000922 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000923 Result = Node->getOperand(Op.ResNo);
924 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000925 case ISD::CopyFromReg:
926 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000927 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000928 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000930 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000931 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000932 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000933 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000934 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
935 } else {
936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
937 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000938 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
939 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000940 // Since CopyFromReg produces two values, make sure to remember that we
941 // legalized both of them.
942 AddLegalizedOperand(Op.getValue(0), Result);
943 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
944 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000945 case ISD::UNDEF: {
946 MVT::ValueType VT = Op.getValueType();
947 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000948 default: assert(0 && "This action is not supported yet!");
949 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000950 if (MVT::isInteger(VT))
951 Result = DAG.getConstant(0, VT);
952 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000953 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
954 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000955 else
956 assert(0 && "Unknown value type!");
957 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000958 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000959 break;
960 }
961 break;
962 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000963
Chris Lattner48b61a72006-03-28 00:40:33 +0000964 case ISD::INTRINSIC_W_CHAIN:
965 case ISD::INTRINSIC_WO_CHAIN:
966 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000967 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000968 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
969 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000970 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000971
972 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000973 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000974 TargetLowering::Custom) {
975 Tmp3 = TLI.LowerOperation(Result, DAG);
976 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000977 }
978
979 if (Result.Val->getNumValues() == 1) break;
980
981 // Must have return value and chain result.
982 assert(Result.Val->getNumValues() == 2 &&
983 "Cannot return more than two values!");
984
985 // Since loads produce two values, make sure to remember that we
986 // legalized both of them.
987 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
988 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
989 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000990 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000991
992 case ISD::LOCATION:
993 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
995
996 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
997 case TargetLowering::Promote:
998 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000999 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001000 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001001 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001002 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001003
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001004 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001005 const std::string &FName =
1006 cast<StringSDNode>(Node->getOperand(3))->getValue();
1007 const std::string &DirName =
1008 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001009 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001010
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001011 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001012 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001013 SDOperand LineOp = Node->getOperand(1);
1014 SDOperand ColOp = Node->getOperand(2);
1015
1016 if (useDEBUG_LOC) {
1017 Ops.push_back(LineOp); // line #
1018 Ops.push_back(ColOp); // col #
1019 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001020 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001021 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001022 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1023 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001024 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001025 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001026 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001027 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001028 } else {
1029 Result = Tmp1; // chain
1030 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001031 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001032 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001033 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001034 if (Tmp1 != Node->getOperand(0) ||
1035 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001036 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001037 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001038 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1039 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1040 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1041 } else {
1042 // Otherwise promote them.
1043 Ops.push_back(PromoteOp(Node->getOperand(1)));
1044 Ops.push_back(PromoteOp(Node->getOperand(2)));
1045 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001046 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1047 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001048 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001049 }
1050 break;
1051 }
1052 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001053
1054 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001055 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001056 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001057 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001058 case TargetLowering::Legal:
1059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1061 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1062 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001063 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001064 break;
1065 }
1066 break;
1067
Jim Laskey1ee29252007-01-26 14:34:52 +00001068 case ISD::LABEL:
1069 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1070 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001071 default: assert(0 && "This action is not supported yet!");
1072 case TargetLowering::Legal:
1073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1074 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001076 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001077 case TargetLowering::Expand:
1078 Result = LegalizeOp(Node->getOperand(0));
1079 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001080 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001081 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001082
Scott Michelc1513d22007-08-08 23:23:31 +00001083 case ISD::Constant: {
1084 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1085 unsigned opAction =
1086 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1087
Chris Lattner3e928bb2005-01-07 07:47:09 +00001088 // We know we don't need to expand constants here, constants only have one
1089 // value and we check that it is fine above.
1090
Scott Michelc1513d22007-08-08 23:23:31 +00001091 if (opAction == TargetLowering::Custom) {
1092 Tmp1 = TLI.LowerOperation(Result, DAG);
1093 if (Tmp1.Val)
1094 Result = Tmp1;
1095 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001096 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001097 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001098 case ISD::ConstantFP: {
1099 // Spill FP immediates to the constant pool if the target cannot directly
1100 // codegen them. Targets often have some immediate values that can be
1101 // efficiently generated into an FP register without a load. We explicitly
1102 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001103 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1104
1105 // Check to see if this FP immediate is already legal.
1106 bool isLegal = false;
1107 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1108 E = TLI.legal_fpimm_end(); I != E; ++I)
1109 if (CFP->isExactlyValue(*I)) {
1110 isLegal = true;
1111 break;
1112 }
1113
Chris Lattner3181a772006-01-29 06:26:56 +00001114 // If this is a legal constant, turn it into a TargetConstantFP node.
1115 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001116 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1117 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001118 break;
1119 }
1120
1121 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1122 default: assert(0 && "This action is not supported yet!");
1123 case TargetLowering::Custom:
1124 Tmp3 = TLI.LowerOperation(Result, DAG);
1125 if (Tmp3.Val) {
1126 Result = Tmp3;
1127 break;
1128 }
1129 // FALLTHROUGH
1130 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001131 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001132 }
1133 break;
1134 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001135 case ISD::TokenFactor:
1136 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001137 Tmp1 = LegalizeOp(Node->getOperand(0));
1138 Tmp2 = LegalizeOp(Node->getOperand(1));
1139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1140 } else if (Node->getNumOperands() == 3) {
1141 Tmp1 = LegalizeOp(Node->getOperand(0));
1142 Tmp2 = LegalizeOp(Node->getOperand(1));
1143 Tmp3 = LegalizeOp(Node->getOperand(2));
1144 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001145 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001146 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001147 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001148 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1149 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001150 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001151 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001152 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001153
1154 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001155 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001156 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001157 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1158 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001159
1160 // The number of incoming and outgoing values should match; unless the final
1161 // outgoing value is a flag.
1162 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1163 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1164 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1165 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001166 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001167
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001168 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001169 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001170 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001171 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1172 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001173 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001174 if (Op.ResNo == i)
1175 Tmp2 = Tmp1;
1176 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1177 }
1178 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001179 case ISD::EXTRACT_SUBREG: {
1180 Tmp1 = LegalizeOp(Node->getOperand(0));
1181 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1182 assert(idx && "Operand must be a constant");
1183 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1185 }
1186 break;
1187 case ISD::INSERT_SUBREG: {
1188 Tmp1 = LegalizeOp(Node->getOperand(0));
1189 Tmp2 = LegalizeOp(Node->getOperand(1));
1190 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1191 assert(idx && "Operand must be a constant");
1192 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1194 }
1195 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001196 case ISD::BUILD_VECTOR:
1197 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001198 default: assert(0 && "This action is not supported yet!");
1199 case TargetLowering::Custom:
1200 Tmp3 = TLI.LowerOperation(Result, DAG);
1201 if (Tmp3.Val) {
1202 Result = Tmp3;
1203 break;
1204 }
1205 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001206 case TargetLowering::Expand:
1207 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001208 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001209 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001210 break;
1211 case ISD::INSERT_VECTOR_ELT:
1212 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1213 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1214 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1216
1217 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1218 Node->getValueType(0))) {
1219 default: assert(0 && "This action is not supported yet!");
1220 case TargetLowering::Legal:
1221 break;
1222 case TargetLowering::Custom:
1223 Tmp3 = TLI.LowerOperation(Result, DAG);
1224 if (Tmp3.Val) {
1225 Result = Tmp3;
1226 break;
1227 }
1228 // FALLTHROUGH
1229 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001230 // If the insert index is a constant, codegen this as a scalar_to_vector,
1231 // then a shuffle that inserts it into the right position in the vector.
1232 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1233 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1234 Tmp1.getValueType(), Tmp2);
1235
1236 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1237 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001238 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001239
1240 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1241 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1242 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001243 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001244 for (unsigned i = 0; i != NumElts; ++i) {
1245 if (i != InsertPos->getValue())
1246 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1247 else
1248 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1249 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001250 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1251 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001252
1253 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1254 Tmp1, ScVec, ShufMask);
1255 Result = LegalizeOp(Result);
1256 break;
1257 }
1258
Chris Lattner2332b9f2006-03-19 01:17:20 +00001259 // If the target doesn't support this, we have to spill the input vector
1260 // to a temporary stack slot, update the element, then reload it. This is
1261 // badness. We could also load the value into a vector register (either
1262 // with a "move to register" or "extload into register" instruction, then
1263 // permute it into place, if the idx is a constant and if the idx is
1264 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001265 MVT::ValueType VT = Tmp1.getValueType();
1266 MVT::ValueType EltVT = Tmp2.getValueType();
1267 MVT::ValueType IdxVT = Tmp3.getValueType();
1268 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001269 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001270 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001271 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001272
1273 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001274 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1275 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001276 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001277 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1278 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1279 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001280 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001281 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001282 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001283 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001284 break;
1285 }
1286 }
1287 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001288 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001289 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1290 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1291 break;
1292 }
1293
Chris Lattnerce872152006-03-19 06:31:19 +00001294 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1295 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1296 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1297 Node->getValueType(0))) {
1298 default: assert(0 && "This action is not supported yet!");
1299 case TargetLowering::Legal:
1300 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001301 case TargetLowering::Custom:
1302 Tmp3 = TLI.LowerOperation(Result, DAG);
1303 if (Tmp3.Val) {
1304 Result = Tmp3;
1305 break;
1306 }
1307 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001308 case TargetLowering::Expand:
1309 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001310 break;
1311 }
Chris Lattnerce872152006-03-19 06:31:19 +00001312 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001313 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1315 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1316 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1317
1318 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001319 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1320 default: assert(0 && "Unknown operation action!");
1321 case TargetLowering::Legal:
1322 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1323 "vector shuffle should not be created if not legal!");
1324 break;
1325 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001326 Tmp3 = TLI.LowerOperation(Result, DAG);
1327 if (Tmp3.Val) {
1328 Result = Tmp3;
1329 break;
1330 }
1331 // FALLTHROUGH
1332 case TargetLowering::Expand: {
1333 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001334 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001335 MVT::ValueType PtrVT = TLI.getPointerTy();
1336 SDOperand Mask = Node->getOperand(2);
1337 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001338 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001339 for (unsigned i = 0; i != NumElems; ++i) {
1340 SDOperand Arg = Mask.getOperand(i);
1341 if (Arg.getOpcode() == ISD::UNDEF) {
1342 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1343 } else {
1344 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1345 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1346 if (Idx < NumElems)
1347 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1348 DAG.getConstant(Idx, PtrVT)));
1349 else
1350 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1351 DAG.getConstant(Idx - NumElems, PtrVT)));
1352 }
1353 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001354 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001355 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001356 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001357 case TargetLowering::Promote: {
1358 // Change base type to a different vector type.
1359 MVT::ValueType OVT = Node->getValueType(0);
1360 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1361
1362 // Cast the two input vectors.
1363 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1364 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1365
1366 // Convert the shuffle mask to the right # elements.
1367 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1368 assert(Tmp3.Val && "Shuffle not legal?");
1369 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1370 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1371 break;
1372 }
Chris Lattner87100e02006-03-20 01:52:29 +00001373 }
1374 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001375
1376 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001377 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001378 Tmp2 = LegalizeOp(Node->getOperand(1));
1379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001380 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001381 break;
1382
Dan Gohman7f321562007-06-25 16:23:39 +00001383 case ISD::EXTRACT_SUBVECTOR:
1384 Tmp1 = Node->getOperand(0);
1385 Tmp2 = LegalizeOp(Node->getOperand(1));
1386 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1387 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001388 break;
1389
Chris Lattner6831a812006-02-13 09:18:02 +00001390 case ISD::CALLSEQ_START: {
1391 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1392
1393 // Recursively Legalize all of the inputs of the call end that do not lead
1394 // to this call start. This ensures that any libcalls that need be inserted
1395 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001396 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001397 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001398 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1399 NodesLeadingTo);
1400 }
Chris Lattner6831a812006-02-13 09:18:02 +00001401
1402 // Now that we legalized all of the inputs (which may have inserted
1403 // libcalls) create the new CALLSEQ_START node.
1404 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1405
1406 // Merge in the last call, to ensure that this call start after the last
1407 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001408 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001409 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1410 Tmp1 = LegalizeOp(Tmp1);
1411 }
Chris Lattner6831a812006-02-13 09:18:02 +00001412
1413 // Do not try to legalize the target-specific arguments (#1+).
1414 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001415 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001416 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001417 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001418 }
1419
1420 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001421 AddLegalizedOperand(Op.getValue(0), Result);
1422 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1423 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1424
Chris Lattner6831a812006-02-13 09:18:02 +00001425 // Now that the callseq_start and all of the non-call nodes above this call
1426 // sequence have been legalized, legalize the call itself. During this
1427 // process, no libcalls can/will be inserted, guaranteeing that no calls
1428 // can overlap.
1429 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1430 SDOperand InCallSEQ = LastCALLSEQ_END;
1431 // Note that we are selecting this call!
1432 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1433 IsLegalizingCall = true;
1434
1435 // Legalize the call, starting from the CALLSEQ_END.
1436 LegalizeOp(LastCALLSEQ_END);
1437 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1438 return Result;
1439 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001440 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001441 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1442 // will cause this node to be legalized as well as handling libcalls right.
1443 if (LastCALLSEQ_END.Val != Node) {
1444 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001445 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001446 assert(I != LegalizedNodes.end() &&
1447 "Legalizing the call start should have legalized this node!");
1448 return I->second;
1449 }
1450
1451 // Otherwise, the call start has been legalized and everything is going
1452 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001453 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001454 // Do not try to legalize the target-specific arguments (#1+), except for
1455 // an optional flag input.
1456 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1457 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001458 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001459 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001460 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001461 }
1462 } else {
1463 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1464 if (Tmp1 != Node->getOperand(0) ||
1465 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001466 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001467 Ops[0] = Tmp1;
1468 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001469 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001470 }
Chris Lattner6a542892006-01-24 05:48:21 +00001471 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001472 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001473 // This finishes up call legalization.
1474 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001475
1476 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1477 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1478 if (Node->getNumValues() == 2)
1479 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1480 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001481 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001482 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001483 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1484 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1485 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001487
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001488 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001489 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001490 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001491 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001492 case TargetLowering::Expand: {
1493 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1494 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1495 " not tell us which reg is the stack pointer!");
1496 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001497
1498 // Chain the dynamic stack allocation so that it doesn't modify the stack
1499 // pointer when other instructions are using the stack.
1500 Chain = DAG.getCALLSEQ_START(Chain,
1501 DAG.getConstant(0, TLI.getPointerTy()));
1502
Chris Lattner903d2782006-01-15 08:54:32 +00001503 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001504 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1505 Chain = SP.getValue(1);
1506 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1507 unsigned StackAlign =
1508 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1509 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001510 SP = DAG.getNode(ISD::AND, VT, SP,
1511 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001512 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001513 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1514
1515 Tmp2 =
1516 DAG.getCALLSEQ_END(Chain,
1517 DAG.getConstant(0, TLI.getPointerTy()),
1518 DAG.getConstant(0, TLI.getPointerTy()),
1519 SDOperand());
1520
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001521 Tmp1 = LegalizeOp(Tmp1);
1522 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001523 break;
1524 }
1525 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001526 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1527 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001528 Tmp1 = LegalizeOp(Tmp3);
1529 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001530 }
Chris Lattner903d2782006-01-15 08:54:32 +00001531 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001532 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001533 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001534 }
Chris Lattner903d2782006-01-15 08:54:32 +00001535 // Since this op produce two values, make sure to remember that we
1536 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001537 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1538 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001539 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001540 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001541 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001542 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001543 bool Changed = false;
1544 // Legalize all of the operands of the inline asm, in case they are nodes
1545 // that need to be expanded or something. Note we skip the asm string and
1546 // all of the TargetConstant flags.
1547 SDOperand Op = LegalizeOp(Ops[0]);
1548 Changed = Op != Ops[0];
1549 Ops[0] = Op;
1550
1551 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1552 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1553 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1554 for (++i; NumVals; ++i, --NumVals) {
1555 SDOperand Op = LegalizeOp(Ops[i]);
1556 if (Op != Ops[i]) {
1557 Changed = true;
1558 Ops[i] = Op;
1559 }
1560 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001561 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001562
1563 if (HasInFlag) {
1564 Op = LegalizeOp(Ops.back());
1565 Changed |= Op != Ops.back();
1566 Ops.back() = Op;
1567 }
1568
1569 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001570 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001571
1572 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001573 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001574 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1575 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001576 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001577 case ISD::BR:
1578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001579 // Ensure that libcalls are emitted before a branch.
1580 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1581 Tmp1 = LegalizeOp(Tmp1);
1582 LastCALLSEQ_END = DAG.getEntryNode();
1583
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001585 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001586 case ISD::BRIND:
1587 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1588 // Ensure that libcalls are emitted before a branch.
1589 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1590 Tmp1 = LegalizeOp(Tmp1);
1591 LastCALLSEQ_END = DAG.getEntryNode();
1592
1593 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1594 default: assert(0 && "Indirect target must be legal type (pointer)!");
1595 case Legal:
1596 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1597 break;
1598 }
1599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1600 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001601 case ISD::BR_JT:
1602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1603 // Ensure that libcalls are emitted before a branch.
1604 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1605 Tmp1 = LegalizeOp(Tmp1);
1606 LastCALLSEQ_END = DAG.getEntryNode();
1607
1608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1609 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1610
1611 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1612 default: assert(0 && "This action is not supported yet!");
1613 case TargetLowering::Legal: break;
1614 case TargetLowering::Custom:
1615 Tmp1 = TLI.LowerOperation(Result, DAG);
1616 if (Tmp1.Val) Result = Tmp1;
1617 break;
1618 case TargetLowering::Expand: {
1619 SDOperand Chain = Result.getOperand(0);
1620 SDOperand Table = Result.getOperand(1);
1621 SDOperand Index = Result.getOperand(2);
1622
1623 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001624 MachineFunction &MF = DAG.getMachineFunction();
1625 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001626 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1627 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001628
1629 SDOperand LD;
1630 switch (EntrySize) {
1631 default: assert(0 && "Size of jump table not supported yet."); break;
1632 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1633 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1634 }
1635
Evan Chengcc415862007-11-09 01:32:10 +00001636 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001637 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001638 // For PIC, the sequence is:
1639 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001640 // RelocBase can be JumpTable, GOT or some sort of global base.
1641 if (PTy != MVT::i32)
1642 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1643 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1644 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001645 }
Evan Chengcc415862007-11-09 01:32:10 +00001646 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001647 }
1648 }
1649 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001650 case ISD::BRCOND:
1651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001652 // Ensure that libcalls are emitted before a return.
1653 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1654 Tmp1 = LegalizeOp(Tmp1);
1655 LastCALLSEQ_END = DAG.getEntryNode();
1656
Chris Lattner47e92232005-01-18 19:27:06 +00001657 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1658 case Expand: assert(0 && "It's impossible to expand bools");
1659 case Legal:
1660 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1661 break;
1662 case Promote:
1663 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001664
1665 // The top bits of the promoted condition are not necessarily zero, ensure
1666 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001667 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001668 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1669 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001670 break;
1671 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001672
1673 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001675
1676 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1677 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001678 case TargetLowering::Legal: break;
1679 case TargetLowering::Custom:
1680 Tmp1 = TLI.LowerOperation(Result, DAG);
1681 if (Tmp1.Val) Result = Tmp1;
1682 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001683 case TargetLowering::Expand:
1684 // Expand brcond's setcc into its constituent parts and create a BR_CC
1685 // Node.
1686 if (Tmp2.getOpcode() == ISD::SETCC) {
1687 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1688 Tmp2.getOperand(0), Tmp2.getOperand(1),
1689 Node->getOperand(2));
1690 } else {
1691 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1692 DAG.getCondCode(ISD::SETNE), Tmp2,
1693 DAG.getConstant(0, Tmp2.getValueType()),
1694 Node->getOperand(2));
1695 }
1696 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001697 }
1698 break;
1699 case ISD::BR_CC:
1700 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001701 // Ensure that libcalls are emitted before a branch.
1702 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1703 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001704 Tmp2 = Node->getOperand(2); // LHS
1705 Tmp3 = Node->getOperand(3); // RHS
1706 Tmp4 = Node->getOperand(1); // CC
1707
1708 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001709 LastCALLSEQ_END = DAG.getEntryNode();
1710
Nate Begeman750ac1b2006-02-01 07:19:44 +00001711 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1712 // the LHS is a legal SETCC itself. In this case, we need to compare
1713 // the result against zero to select between true and false values.
1714 if (Tmp3.Val == 0) {
1715 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1716 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001717 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001718
1719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1720 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001721
Chris Lattner181b7a32005-12-17 23:46:46 +00001722 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1723 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001724 case TargetLowering::Legal: break;
1725 case TargetLowering::Custom:
1726 Tmp4 = TLI.LowerOperation(Result, DAG);
1727 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001728 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001729 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001730 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001731 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001732 LoadSDNode *LD = cast<LoadSDNode>(Node);
1733 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1734 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001735
Evan Cheng466685d2006-10-09 20:57:25 +00001736 ISD::LoadExtType ExtType = LD->getExtensionType();
1737 if (ExtType == ISD::NON_EXTLOAD) {
1738 MVT::ValueType VT = Node->getValueType(0);
1739 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1740 Tmp3 = Result.getValue(0);
1741 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001742
Evan Cheng466685d2006-10-09 20:57:25 +00001743 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1744 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001745 case TargetLowering::Legal:
1746 // If this is an unaligned load and the target doesn't support it,
1747 // expand it.
1748 if (!TLI.allowsUnalignedMemoryAccesses()) {
1749 unsigned ABIAlignment = TLI.getTargetData()->
1750 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1751 if (LD->getAlignment() < ABIAlignment){
1752 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1753 TLI);
1754 Tmp3 = Result.getOperand(0);
1755 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001756 Tmp3 = LegalizeOp(Tmp3);
1757 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001758 }
1759 }
1760 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001761 case TargetLowering::Custom:
1762 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1763 if (Tmp1.Val) {
1764 Tmp3 = LegalizeOp(Tmp1);
1765 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001766 }
Evan Cheng466685d2006-10-09 20:57:25 +00001767 break;
1768 case TargetLowering::Promote: {
1769 // Only promote a load of vector type to another.
1770 assert(MVT::isVector(VT) && "Cannot promote this load!");
1771 // Change base type to a different vector type.
1772 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1773
1774 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001775 LD->getSrcValueOffset(),
1776 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001777 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1778 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001779 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001780 }
Evan Cheng466685d2006-10-09 20:57:25 +00001781 }
1782 // Since loads produce two values, make sure to remember that we
1783 // legalized both of them.
1784 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1785 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1786 return Op.ResNo ? Tmp4 : Tmp3;
1787 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001788 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001789 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1790 default: assert(0 && "This action is not supported yet!");
1791 case TargetLowering::Promote:
1792 assert(SrcVT == MVT::i1 &&
1793 "Can only promote extending LOAD from i1 -> i8!");
1794 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1795 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001796 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001797 Tmp1 = Result.getValue(0);
1798 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001799 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001800 case TargetLowering::Custom:
1801 isCustom = true;
1802 // FALLTHROUGH
1803 case TargetLowering::Legal:
1804 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1805 Tmp1 = Result.getValue(0);
1806 Tmp2 = Result.getValue(1);
1807
1808 if (isCustom) {
1809 Tmp3 = TLI.LowerOperation(Result, DAG);
1810 if (Tmp3.Val) {
1811 Tmp1 = LegalizeOp(Tmp3);
1812 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1813 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001814 } else {
1815 // If this is an unaligned load and the target doesn't support it,
1816 // expand it.
1817 if (!TLI.allowsUnalignedMemoryAccesses()) {
1818 unsigned ABIAlignment = TLI.getTargetData()->
1819 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1820 if (LD->getAlignment() < ABIAlignment){
1821 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1822 TLI);
1823 Tmp1 = Result.getOperand(0);
1824 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001825 Tmp1 = LegalizeOp(Tmp1);
1826 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001827 }
1828 }
Evan Cheng466685d2006-10-09 20:57:25 +00001829 }
1830 break;
1831 case TargetLowering::Expand:
1832 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1833 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1834 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001835 LD->getSrcValueOffset(),
1836 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001837 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1838 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1839 Tmp2 = LegalizeOp(Load.getValue(1));
1840 break;
1841 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001842 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001843 // Turn the unsupported load into an EXTLOAD followed by an explicit
1844 // zero/sign extend inreg.
1845 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1846 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001847 LD->getSrcValueOffset(), SrcVT,
1848 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001849 SDOperand ValRes;
1850 if (ExtType == ISD::SEXTLOAD)
1851 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1852 Result, DAG.getValueType(SrcVT));
1853 else
1854 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1855 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1856 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1857 break;
1858 }
1859 // Since loads produce two values, make sure to remember that we legalized
1860 // both of them.
1861 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1862 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1863 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001864 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001865 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001866 case ISD::EXTRACT_ELEMENT: {
1867 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1868 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001869 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001870 case Legal:
1871 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1872 // 1 -> Hi
1873 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1874 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1875 TLI.getShiftAmountTy()));
1876 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1877 } else {
1878 // 0 -> Lo
1879 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1880 Node->getOperand(0));
1881 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001882 break;
1883 case Expand:
1884 // Get both the low and high parts.
1885 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1886 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1887 Result = Tmp2; // 1 -> Hi
1888 else
1889 Result = Tmp1; // 0 -> Lo
1890 break;
1891 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001892 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001893 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001894
1895 case ISD::CopyToReg:
1896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001897
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001898 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001899 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001900 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001901 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001902 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001903 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001904 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001905 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001906 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001907 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1909 Tmp3);
1910 } else {
1911 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001912 }
1913
1914 // Since this produces two values, make sure to remember that we legalized
1915 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001916 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001917 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001918 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001919 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001920 break;
1921
1922 case ISD::RET:
1923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001924
1925 // Ensure that libcalls are emitted before a return.
1926 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1927 Tmp1 = LegalizeOp(Tmp1);
1928 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001929
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001931 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001932 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001933 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001934 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001935 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001936 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001937 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001938 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001939 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001940 SDOperand Lo, Hi;
1941 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001942
1943 // Big endian systems want the hi reg first.
1944 if (!TLI.isLittleEndian())
1945 std::swap(Lo, Hi);
1946
Evan Cheng13acce32006-12-11 19:27:14 +00001947 if (Hi.Val)
1948 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1949 else
1950 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001951 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001952 } else {
1953 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00001954 int InIx = Tmp2.ResNo;
1955 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1956 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001957
Dan Gohman7f321562007-06-25 16:23:39 +00001958 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001959 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001960 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001961 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001962 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001963 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001964 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001965 } else if (NumElems == 1) {
1966 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001967 Tmp2 = ScalarizeVectorOp(Tmp2);
1968 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001969 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001970
1971 // FIXME: Returns of gcc generic vectors smaller than a legal type
1972 // should be returned in integer registers!
1973
Chris Lattnerf87324e2006-04-11 01:31:51 +00001974 // The scalarized value type may not be legal, e.g. it might require
1975 // promotion or expansion. Relegalize the return.
1976 Result = LegalizeOp(Result);
1977 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001978 // FIXME: Returns of gcc generic vectors larger than a legal vector
1979 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001980 SDOperand Lo, Hi;
1981 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001982 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001983 Result = LegalizeOp(Result);
1984 }
1985 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001986 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001987 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001988 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001989 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001990 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001991 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001992 }
1993 break;
1994 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001995 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001996 break;
1997 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001998 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001999 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002000 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002001 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2002 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002003 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002004 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002005 break;
2006 case Expand: {
2007 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002008 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002009 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002010 ExpandOp(Node->getOperand(i), Lo, Hi);
2011 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002012 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002013 if (Hi.Val) {
2014 NewValues.push_back(Hi);
2015 NewValues.push_back(Node->getOperand(i+1));
2016 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002017 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002018 }
2019 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002020 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002021 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002022
2023 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002024 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002025 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002026 Result = DAG.getNode(ISD::RET, MVT::Other,
2027 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002028 break;
2029 }
2030 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002031
Chris Lattner6862dbc2006-01-29 21:02:23 +00002032 if (Result.getOpcode() == ISD::RET) {
2033 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2034 default: assert(0 && "This action is not supported yet!");
2035 case TargetLowering::Legal: break;
2036 case TargetLowering::Custom:
2037 Tmp1 = TLI.LowerOperation(Result, DAG);
2038 if (Tmp1.Val) Result = Tmp1;
2039 break;
2040 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002041 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002042 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002043 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002044 StoreSDNode *ST = cast<StoreSDNode>(Node);
2045 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2046 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002047 int SVOffset = ST->getSrcValueOffset();
2048 unsigned Alignment = ST->getAlignment();
2049 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002050
Evan Cheng8b2794a2006-10-13 21:14:26 +00002051 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002052 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2053 // FIXME: We shouldn't do this for TargetConstantFP's.
2054 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2055 // to phase ordering between legalized code and the dag combiner. This
2056 // probably means that we need to integrate dag combiner and legalizer
2057 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002058 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002059 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002060 if (CFP->getValueType(0) == MVT::f32 &&
2061 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002062 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2063 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002064 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002065 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2066 SVOffset, isVolatile, Alignment);
2067 break;
2068 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002069 // If this target supports 64-bit registers, do a single 64-bit store.
2070 if (getTypeAction(MVT::i64) == Legal) {
2071 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2072 getZExtValue(), MVT::i64);
2073 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2074 SVOffset, isVolatile, Alignment);
2075 break;
2076 } else if (getTypeAction(MVT::i32) == Legal) {
2077 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2078 // stores. If the target supports neither 32- nor 64-bits, this
2079 // xform is certainly not worth it.
2080 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2081 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2082 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2083 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2084
2085 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2086 SVOffset, isVolatile, Alignment);
2087 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2088 getIntPtrConstant(4));
2089 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002090 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002091
2092 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2093 break;
2094 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002095 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002096 }
2097
Evan Cheng8b2794a2006-10-13 21:14:26 +00002098 switch (getTypeAction(ST->getStoredVT())) {
2099 case Legal: {
2100 Tmp3 = LegalizeOp(ST->getValue());
2101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2102 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002103
Evan Cheng8b2794a2006-10-13 21:14:26 +00002104 MVT::ValueType VT = Tmp3.getValueType();
2105 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2106 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002107 case TargetLowering::Legal:
2108 // If this is an unaligned store and the target doesn't support it,
2109 // expand it.
2110 if (!TLI.allowsUnalignedMemoryAccesses()) {
2111 unsigned ABIAlignment = TLI.getTargetData()->
2112 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2113 if (ST->getAlignment() < ABIAlignment)
2114 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2115 TLI);
2116 }
2117 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002118 case TargetLowering::Custom:
2119 Tmp1 = TLI.LowerOperation(Result, DAG);
2120 if (Tmp1.Val) Result = Tmp1;
2121 break;
2122 case TargetLowering::Promote:
2123 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2124 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2125 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2126 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002127 ST->getSrcValue(), SVOffset, isVolatile,
2128 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002129 break;
2130 }
2131 break;
2132 }
2133 case Promote:
2134 // Truncate the value and store the result.
2135 Tmp3 = PromoteOp(ST->getValue());
2136 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002137 SVOffset, ST->getStoredVT(),
2138 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002139 break;
2140
2141 case Expand:
2142 unsigned IncrementSize = 0;
2143 SDOperand Lo, Hi;
2144
2145 // If this is a vector type, then we have to calculate the increment as
2146 // the product of the element size in bytes, and the number of elements
2147 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002148 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002149 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002150 int InIx = ST->getValue().ResNo;
2151 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2152 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002153
Dan Gohman7f321562007-06-25 16:23:39 +00002154 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002155 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002156 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002157 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002158 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002159 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002160 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002161 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002162 Result = LegalizeOp(Result);
2163 break;
2164 } else if (NumElems == 1) {
2165 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002166 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002167 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002168 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002169 // The scalarized value type may not be legal, e.g. it might require
2170 // promotion or expansion. Relegalize the scalar store.
2171 Result = LegalizeOp(Result);
2172 break;
2173 } else {
2174 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002175 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2176 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002177 }
2178 } else {
2179 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002180 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002181
2182 if (!TLI.isLittleEndian())
2183 std::swap(Lo, Hi);
2184 }
2185
2186 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002187 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002188
2189 if (Hi.Val == NULL) {
2190 // Must be int <-> float one-to-one expansion.
2191 Result = Lo;
2192 break;
2193 }
2194
Evan Cheng8b2794a2006-10-13 21:14:26 +00002195 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2196 getIntPtrConstant(IncrementSize));
2197 assert(isTypeLegal(Tmp2.getValueType()) &&
2198 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002199 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002200 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002201 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002202 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002203 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2204 break;
2205 }
2206 } else {
2207 // Truncating store
2208 assert(isTypeLegal(ST->getValue().getValueType()) &&
2209 "Cannot handle illegal TRUNCSTORE yet!");
2210 Tmp3 = LegalizeOp(ST->getValue());
2211
2212 // The only promote case we handle is TRUNCSTORE:i1 X into
2213 // -> TRUNCSTORE:i8 (and X, 1)
2214 if (ST->getStoredVT() == MVT::i1 &&
2215 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2216 // Promote the bool to a mask then store.
2217 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2218 DAG.getConstant(1, Tmp3.getValueType()));
2219 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002220 SVOffset, MVT::i8,
2221 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002222 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2223 Tmp2 != ST->getBasePtr()) {
2224 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2225 ST->getOffset());
2226 }
2227
2228 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2229 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002230 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002231 case TargetLowering::Legal:
2232 // If this is an unaligned store and the target doesn't support it,
2233 // expand it.
2234 if (!TLI.allowsUnalignedMemoryAccesses()) {
2235 unsigned ABIAlignment = TLI.getTargetData()->
2236 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2237 if (ST->getAlignment() < ABIAlignment)
2238 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2239 TLI);
2240 }
2241 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002242 case TargetLowering::Custom:
2243 Tmp1 = TLI.LowerOperation(Result, DAG);
2244 if (Tmp1.Val) Result = Tmp1;
2245 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002246 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002247 }
2248 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002249 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002250 case ISD::PCMARKER:
2251 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002252 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002253 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002254 case ISD::STACKSAVE:
2255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002256 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2257 Tmp1 = Result.getValue(0);
2258 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002259
Chris Lattner140d53c2006-01-13 02:50:02 +00002260 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2261 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002262 case TargetLowering::Legal: break;
2263 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002264 Tmp3 = TLI.LowerOperation(Result, DAG);
2265 if (Tmp3.Val) {
2266 Tmp1 = LegalizeOp(Tmp3);
2267 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002268 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002269 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002270 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002271 // Expand to CopyFromReg if the target set
2272 // StackPointerRegisterToSaveRestore.
2273 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002274 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002275 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002276 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002277 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002278 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2279 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002280 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002281 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002282 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002283
2284 // Since stacksave produce two values, make sure to remember that we
2285 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002286 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2287 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2288 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002289
Chris Lattner140d53c2006-01-13 02:50:02 +00002290 case ISD::STACKRESTORE:
2291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2292 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002294
2295 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2296 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002297 case TargetLowering::Legal: break;
2298 case TargetLowering::Custom:
2299 Tmp1 = TLI.LowerOperation(Result, DAG);
2300 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002301 break;
2302 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002303 // Expand to CopyToReg if the target set
2304 // StackPointerRegisterToSaveRestore.
2305 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2306 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2307 } else {
2308 Result = Tmp1;
2309 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002310 break;
2311 }
2312 break;
2313
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002314 case ISD::READCYCLECOUNTER:
2315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002316 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002317 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2318 Node->getValueType(0))) {
2319 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002320 case TargetLowering::Legal:
2321 Tmp1 = Result.getValue(0);
2322 Tmp2 = Result.getValue(1);
2323 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002324 case TargetLowering::Custom:
2325 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002326 Tmp1 = LegalizeOp(Result.getValue(0));
2327 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002328 break;
2329 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002330
2331 // Since rdcc produce two values, make sure to remember that we legalized
2332 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002333 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2334 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002335 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002336
Chris Lattner2ee743f2005-01-14 22:08:15 +00002337 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002338 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2339 case Expand: assert(0 && "It's impossible to expand bools");
2340 case Legal:
2341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2342 break;
2343 case Promote:
2344 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002345 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002346 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002347 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2348 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002349 break;
2350 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002351 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002352 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002353
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002355
Nate Begemanb942a3d2005-08-23 04:29:48 +00002356 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002357 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002358 case TargetLowering::Legal: break;
2359 case TargetLowering::Custom: {
2360 Tmp1 = TLI.LowerOperation(Result, DAG);
2361 if (Tmp1.Val) Result = Tmp1;
2362 break;
2363 }
Nate Begeman9373a812005-08-10 20:51:12 +00002364 case TargetLowering::Expand:
2365 if (Tmp1.getOpcode() == ISD::SETCC) {
2366 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2367 Tmp2, Tmp3,
2368 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2369 } else {
2370 Result = DAG.getSelectCC(Tmp1,
2371 DAG.getConstant(0, Tmp1.getValueType()),
2372 Tmp2, Tmp3, ISD::SETNE);
2373 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002374 break;
2375 case TargetLowering::Promote: {
2376 MVT::ValueType NVT =
2377 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2378 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002379 if (MVT::isVector(Tmp2.getValueType())) {
2380 ExtOp = ISD::BIT_CONVERT;
2381 TruncOp = ISD::BIT_CONVERT;
2382 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002383 ExtOp = ISD::ANY_EXTEND;
2384 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002385 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002386 ExtOp = ISD::FP_EXTEND;
2387 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002388 }
2389 // Promote each of the values to the new type.
2390 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2391 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2392 // Perform the larger operation, then round down.
2393 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2394 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2395 break;
2396 }
2397 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002398 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002399 case ISD::SELECT_CC: {
2400 Tmp1 = Node->getOperand(0); // LHS
2401 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002402 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2403 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002404 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002405
Nate Begeman750ac1b2006-02-01 07:19:44 +00002406 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2407
2408 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2409 // the LHS is a legal SETCC itself. In this case, we need to compare
2410 // the result against zero to select between true and false values.
2411 if (Tmp2.Val == 0) {
2412 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2413 CC = DAG.getCondCode(ISD::SETNE);
2414 }
2415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2416
2417 // Everything is legal, see if we should expand this op or something.
2418 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2419 default: assert(0 && "This action is not supported yet!");
2420 case TargetLowering::Legal: break;
2421 case TargetLowering::Custom:
2422 Tmp1 = TLI.LowerOperation(Result, DAG);
2423 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002424 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002425 }
2426 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002427 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002428 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002429 Tmp1 = Node->getOperand(0);
2430 Tmp2 = Node->getOperand(1);
2431 Tmp3 = Node->getOperand(2);
2432 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2433
2434 // If we had to Expand the SetCC operands into a SELECT node, then it may
2435 // not always be possible to return a true LHS & RHS. In this case, just
2436 // return the value we legalized, returned in the LHS
2437 if (Tmp2.Val == 0) {
2438 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002439 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002440 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002441
Chris Lattner73e142f2006-01-30 22:43:50 +00002442 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002443 default: assert(0 && "Cannot handle this action for SETCC yet!");
2444 case TargetLowering::Custom:
2445 isCustom = true;
2446 // FALLTHROUGH.
2447 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002449 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002450 Tmp4 = TLI.LowerOperation(Result, DAG);
2451 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002452 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002453 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002454 case TargetLowering::Promote: {
2455 // First step, figure out the appropriate operation to use.
2456 // Allow SETCC to not be supported for all legal data types
2457 // Mostly this targets FP
2458 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002459 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002460
2461 // Scan for the appropriate larger type to use.
2462 while (1) {
2463 NewInTy = (MVT::ValueType)(NewInTy+1);
2464
2465 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2466 "Fell off of the edge of the integer world");
2467 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2468 "Fell off of the edge of the floating point world");
2469
2470 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002471 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002472 break;
2473 }
2474 if (MVT::isInteger(NewInTy))
2475 assert(0 && "Cannot promote Legal Integer SETCC yet");
2476 else {
2477 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2478 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2479 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002480 Tmp1 = LegalizeOp(Tmp1);
2481 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002483 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002484 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002485 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002486 case TargetLowering::Expand:
2487 // Expand a setcc node into a select_cc of the same condition, lhs, and
2488 // rhs that selects between const 1 (true) and const 0 (false).
2489 MVT::ValueType VT = Node->getValueType(0);
2490 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2491 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002492 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002493 break;
2494 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002495 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002496 case ISD::MEMSET:
2497 case ISD::MEMCPY:
2498 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002499 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002500 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2501
2502 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2503 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2504 case Expand: assert(0 && "Cannot expand a byte!");
2505 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002506 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002507 break;
2508 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002509 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002510 break;
2511 }
2512 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002513 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002514 }
Chris Lattner272455b2005-02-02 03:44:41 +00002515
2516 SDOperand Tmp4;
2517 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002518 case Expand: {
2519 // Length is too big, just take the lo-part of the length.
2520 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002521 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002522 break;
2523 }
Chris Lattnere5605212005-01-28 22:29:18 +00002524 case Legal:
2525 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002526 break;
2527 case Promote:
2528 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002529 break;
2530 }
2531
2532 SDOperand Tmp5;
2533 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2534 case Expand: assert(0 && "Cannot expand this yet!");
2535 case Legal:
2536 Tmp5 = LegalizeOp(Node->getOperand(4));
2537 break;
2538 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002539 Tmp5 = PromoteOp(Node->getOperand(4));
2540 break;
2541 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002542
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002543 SDOperand Tmp6;
2544 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2545 case Expand: assert(0 && "Cannot expand this yet!");
2546 case Legal:
2547 Tmp6 = LegalizeOp(Node->getOperand(5));
2548 break;
2549 case Promote:
2550 Tmp6 = PromoteOp(Node->getOperand(5));
2551 break;
2552 }
2553
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002554 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2555 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002556 case TargetLowering::Custom:
2557 isCustom = true;
2558 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002559 case TargetLowering::Legal: {
2560 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2561 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002562 if (isCustom) {
2563 Tmp1 = TLI.LowerOperation(Result, DAG);
2564 if (Tmp1.Val) Result = Tmp1;
2565 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002566 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002567 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002568 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002569 // Otherwise, the target does not support this operation. Lower the
2570 // operation to an explicit libcall as appropriate.
2571 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002572 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002573 TargetLowering::ArgListTy Args;
2574 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002575
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002576 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002577 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002578 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002579 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002580 // Extend the (previously legalized) ubyte argument to be an int value
2581 // for the call.
2582 if (Tmp3.getValueType() > MVT::i32)
2583 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2584 else
2585 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002586 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002587 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002588 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002589 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002590
2591 FnName = "memset";
2592 } else if (Node->getOpcode() == ISD::MEMCPY ||
2593 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002594 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002595 Entry.Node = Tmp2; Args.push_back(Entry);
2596 Entry.Node = Tmp3; Args.push_back(Entry);
2597 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002598 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2599 } else {
2600 assert(0 && "Unknown op!");
2601 }
Chris Lattner45982da2005-05-12 16:53:42 +00002602
Chris Lattnere1bd8222005-01-11 05:57:22 +00002603 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002604 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002605 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002606 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002607 break;
2608 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002609 }
2610 break;
2611 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002612
Chris Lattner5b359c62005-04-02 04:00:59 +00002613 case ISD::SHL_PARTS:
2614 case ISD::SRA_PARTS:
2615 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002616 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002617 bool Changed = false;
2618 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2619 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2620 Changed |= Ops.back() != Node->getOperand(i);
2621 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002622 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002623 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002624
Evan Cheng05a2d562006-01-09 18:31:59 +00002625 switch (TLI.getOperationAction(Node->getOpcode(),
2626 Node->getValueType(0))) {
2627 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002628 case TargetLowering::Legal: break;
2629 case TargetLowering::Custom:
2630 Tmp1 = TLI.LowerOperation(Result, DAG);
2631 if (Tmp1.Val) {
2632 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002633 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002634 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002635 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2636 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002637 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002638 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002639 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002640 return RetVal;
2641 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002642 break;
2643 }
2644
Chris Lattner2c8086f2005-04-02 05:00:07 +00002645 // Since these produce multiple values, make sure to remember that we
2646 // legalized all of them.
2647 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2648 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2649 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002650 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002651
2652 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002653 case ISD::ADD:
2654 case ISD::SUB:
2655 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002656 case ISD::MULHS:
2657 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002658 case ISD::UDIV:
2659 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002660 case ISD::AND:
2661 case ISD::OR:
2662 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002663 case ISD::SHL:
2664 case ISD::SRL:
2665 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002666 case ISD::FADD:
2667 case ISD::FSUB:
2668 case ISD::FMUL:
2669 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002670 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002671 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002672 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2673 case Expand: assert(0 && "Not possible");
2674 case Legal:
2675 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2676 break;
2677 case Promote:
2678 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2679 break;
2680 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002681
2682 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002683
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002684 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002685 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002686 case TargetLowering::Legal: break;
2687 case TargetLowering::Custom:
2688 Tmp1 = TLI.LowerOperation(Result, DAG);
2689 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002690 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002691 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002692 MVT::ValueType VT = Op.getValueType();
2693
2694 // See if multiply or divide can be lowered using two-result operations.
2695 SDVTList VTs = DAG.getVTList(VT, VT);
2696 if (Node->getOpcode() == ISD::MUL) {
2697 // We just need the low half of the multiply; try both the signed
2698 // and unsigned forms. If the target supports both SMUL_LOHI and
2699 // UMUL_LOHI, form a preference by checking which forms of plain
2700 // MULH it supports.
2701 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2702 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2703 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2704 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2705 unsigned OpToUse = 0;
2706 if (HasSMUL_LOHI && !HasMULHS) {
2707 OpToUse = ISD::SMUL_LOHI;
2708 } else if (HasUMUL_LOHI && !HasMULHU) {
2709 OpToUse = ISD::UMUL_LOHI;
2710 } else if (HasSMUL_LOHI) {
2711 OpToUse = ISD::SMUL_LOHI;
2712 } else if (HasUMUL_LOHI) {
2713 OpToUse = ISD::UMUL_LOHI;
2714 }
2715 if (OpToUse) {
2716 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2717 break;
2718 }
2719 }
2720 if (Node->getOpcode() == ISD::MULHS &&
2721 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2722 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2723 break;
2724 }
2725 if (Node->getOpcode() == ISD::MULHU &&
2726 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2727 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2728 break;
2729 }
2730 if (Node->getOpcode() == ISD::SDIV &&
2731 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2732 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2733 break;
2734 }
2735 if (Node->getOpcode() == ISD::UDIV &&
2736 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2737 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2738 break;
2739 }
2740
Dan Gohman82669522007-10-11 23:57:53 +00002741 // Check to see if we have a libcall for this operator.
2742 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2743 bool isSigned = false;
2744 switch (Node->getOpcode()) {
2745 case ISD::UDIV:
2746 case ISD::SDIV:
2747 if (VT == MVT::i32) {
2748 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002749 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002750 isSigned = Node->getOpcode() == ISD::SDIV;
2751 }
2752 break;
2753 case ISD::FPOW:
2754 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2755 VT == MVT::f64 ? RTLIB::POW_F64 :
2756 VT == MVT::f80 ? RTLIB::POW_F80 :
2757 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2758 RTLIB::UNKNOWN_LIBCALL;
2759 break;
2760 default: break;
2761 }
2762 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2763 SDOperand Dummy;
2764 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002765 break;
2766 }
2767
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002768 assert(MVT::isVector(Node->getValueType(0)) &&
2769 "Cannot expand this binary operator!");
2770 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002771 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002772 break;
2773 }
Evan Chengcc987612006-04-12 21:20:24 +00002774 case TargetLowering::Promote: {
2775 switch (Node->getOpcode()) {
2776 default: assert(0 && "Do not know how to promote this BinOp!");
2777 case ISD::AND:
2778 case ISD::OR:
2779 case ISD::XOR: {
2780 MVT::ValueType OVT = Node->getValueType(0);
2781 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2782 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2783 // Bit convert each of the values to the new type.
2784 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2785 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2786 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2787 // Bit convert the result back the original type.
2788 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2789 break;
2790 }
2791 }
2792 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002793 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002794 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002795
Dan Gohmane14ea862007-10-05 14:17:22 +00002796 case ISD::SMUL_LOHI:
2797 case ISD::UMUL_LOHI:
2798 case ISD::SDIVREM:
2799 case ISD::UDIVREM:
2800 // These nodes will only be produced by target-specific lowering, so
2801 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002802 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002803 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002804
2805 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2806 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2807 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002808 break;
2809
Chris Lattnera09f8482006-03-05 05:09:38 +00002810 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2811 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2812 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2813 case Expand: assert(0 && "Not possible");
2814 case Legal:
2815 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2816 break;
2817 case Promote:
2818 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2819 break;
2820 }
2821
2822 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2823
2824 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2825 default: assert(0 && "Operation not supported");
2826 case TargetLowering::Custom:
2827 Tmp1 = TLI.LowerOperation(Result, DAG);
2828 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002829 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002830 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002831 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002832 // If this target supports fabs/fneg natively and select is cheap,
2833 // do this efficiently.
2834 if (!TLI.isSelectExpensive() &&
2835 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2836 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002837 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002838 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002839 // Get the sign bit of the RHS.
2840 MVT::ValueType IVT =
2841 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2842 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2843 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2844 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2845 // Get the absolute value of the result.
2846 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2847 // Select between the nabs and abs value based on the sign bit of
2848 // the input.
2849 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2850 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2851 AbsVal),
2852 AbsVal);
2853 Result = LegalizeOp(Result);
2854 break;
2855 }
2856
2857 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002858 MVT::ValueType NVT =
2859 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2860 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2861 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2862 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002863 break;
2864 }
Evan Cheng912095b2007-01-04 21:56:39 +00002865 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002866 break;
2867
Nate Begeman551bf3f2006-02-17 05:43:56 +00002868 case ISD::ADDC:
2869 case ISD::SUBC:
2870 Tmp1 = LegalizeOp(Node->getOperand(0));
2871 Tmp2 = LegalizeOp(Node->getOperand(1));
2872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2873 // Since this produces two values, make sure to remember that we legalized
2874 // both of them.
2875 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2876 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2877 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002878
Nate Begeman551bf3f2006-02-17 05:43:56 +00002879 case ISD::ADDE:
2880 case ISD::SUBE:
2881 Tmp1 = LegalizeOp(Node->getOperand(0));
2882 Tmp2 = LegalizeOp(Node->getOperand(1));
2883 Tmp3 = LegalizeOp(Node->getOperand(2));
2884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2885 // Since this produces two values, make sure to remember that we legalized
2886 // both of them.
2887 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2888 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2889 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002890
Nate Begeman419f8b62005-10-18 00:27:41 +00002891 case ISD::BUILD_PAIR: {
2892 MVT::ValueType PairTy = Node->getValueType(0);
2893 // TODO: handle the case where the Lo and Hi operands are not of legal type
2894 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2895 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2896 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002897 case TargetLowering::Promote:
2898 case TargetLowering::Custom:
2899 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002900 case TargetLowering::Legal:
2901 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2902 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2903 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002904 case TargetLowering::Expand:
2905 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2906 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2907 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2908 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2909 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002910 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002911 break;
2912 }
2913 break;
2914 }
2915
Nate Begemanc105e192005-04-06 00:23:54 +00002916 case ISD::UREM:
2917 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002918 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002919 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2920 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002921
Nate Begemanc105e192005-04-06 00:23:54 +00002922 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002923 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2924 case TargetLowering::Custom:
2925 isCustom = true;
2926 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002927 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002928 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002929 if (isCustom) {
2930 Tmp1 = TLI.LowerOperation(Result, DAG);
2931 if (Tmp1.Val) Result = Tmp1;
2932 }
Nate Begemanc105e192005-04-06 00:23:54 +00002933 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002934 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002935 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002936 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002937 MVT::ValueType VT = Node->getValueType(0);
2938
2939 // See if remainder can be lowered using two-result operations.
2940 SDVTList VTs = DAG.getVTList(VT, VT);
2941 if (Node->getOpcode() == ISD::SREM &&
2942 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2943 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2944 break;
2945 }
2946 if (Node->getOpcode() == ISD::UREM &&
2947 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2948 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2949 break;
2950 }
2951
2952 if (MVT::isInteger(VT)) {
2953 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002954 TargetLowering::Legal) {
2955 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002956 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002957 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2958 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00002959 } else if (MVT::isVector(VT)) {
2960 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002961 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002962 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002963 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002964 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2965 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002966 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002967 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002968 }
Dan Gohman0d974262007-11-06 22:11:54 +00002969 } else {
2970 assert(MVT::isFloatingPoint(VT) &&
2971 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00002972 if (MVT::isVector(VT)) {
2973 Result = LegalizeOp(UnrollVectorOp(Op));
2974 } else {
2975 // Floating point mod -> fmod libcall.
2976 RTLIB::Libcall LC = VT == MVT::f32
2977 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2978 SDOperand Dummy;
2979 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2980 false/*sign irrelevant*/, Dummy);
2981 }
Nate Begemanc105e192005-04-06 00:23:54 +00002982 }
2983 break;
2984 }
Dan Gohman525178c2007-10-08 18:33:35 +00002985 }
Nate Begemanc105e192005-04-06 00:23:54 +00002986 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002987 case ISD::VAARG: {
2988 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2989 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2990
Chris Lattner5c62f332006-01-28 07:42:08 +00002991 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002992 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2993 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002994 case TargetLowering::Custom:
2995 isCustom = true;
2996 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002997 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002998 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2999 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003000 Tmp1 = Result.getValue(1);
3001
3002 if (isCustom) {
3003 Tmp2 = TLI.LowerOperation(Result, DAG);
3004 if (Tmp2.Val) {
3005 Result = LegalizeOp(Tmp2);
3006 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3007 }
3008 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003009 break;
3010 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003011 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003012 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003013 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003014 // Increment the pointer, VAList, to the next vaarg
3015 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3016 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3017 TLI.getPointerTy()));
3018 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003019 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3020 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003021 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003022 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003023 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003024 Result = LegalizeOp(Result);
3025 break;
3026 }
3027 }
3028 // Since VAARG produces two values, make sure to remember that we
3029 // legalized both of them.
3030 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003031 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3032 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003033 }
3034
3035 case ISD::VACOPY:
3036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3037 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3038 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3039
3040 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3041 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003042 case TargetLowering::Custom:
3043 isCustom = true;
3044 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003045 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3047 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003048 if (isCustom) {
3049 Tmp1 = TLI.LowerOperation(Result, DAG);
3050 if (Tmp1.Val) Result = Tmp1;
3051 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003052 break;
3053 case TargetLowering::Expand:
3054 // This defaults to loading a pointer from the input and storing it to the
3055 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003056 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003057 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003058 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3059 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003060 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3061 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003062 break;
3063 }
3064 break;
3065
3066 case ISD::VAEND:
3067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3068 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3069
3070 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3071 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003072 case TargetLowering::Custom:
3073 isCustom = true;
3074 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003075 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003077 if (isCustom) {
3078 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3079 if (Tmp1.Val) Result = Tmp1;
3080 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003081 break;
3082 case TargetLowering::Expand:
3083 Result = Tmp1; // Default to a no-op, return the chain
3084 break;
3085 }
3086 break;
3087
3088 case ISD::VASTART:
3089 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3090 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3091
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3093
Nate Begemanacc398c2006-01-25 18:21:52 +00003094 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3095 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003096 case TargetLowering::Legal: break;
3097 case TargetLowering::Custom:
3098 Tmp1 = TLI.LowerOperation(Result, DAG);
3099 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003100 break;
3101 }
3102 break;
3103
Nate Begeman35ef9132006-01-11 21:21:00 +00003104 case ISD::ROTL:
3105 case ISD::ROTR:
3106 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3107 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003108 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003109 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3110 default:
3111 assert(0 && "ROTL/ROTR legalize operation not supported");
3112 break;
3113 case TargetLowering::Legal:
3114 break;
3115 case TargetLowering::Custom:
3116 Tmp1 = TLI.LowerOperation(Result, DAG);
3117 if (Tmp1.Val) Result = Tmp1;
3118 break;
3119 case TargetLowering::Promote:
3120 assert(0 && "Do not know how to promote ROTL/ROTR");
3121 break;
3122 case TargetLowering::Expand:
3123 assert(0 && "Do not know how to expand ROTL/ROTR");
3124 break;
3125 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003126 break;
3127
Nate Begemand88fc032006-01-14 03:14:10 +00003128 case ISD::BSWAP:
3129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3130 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003131 case TargetLowering::Custom:
3132 assert(0 && "Cannot custom legalize this yet!");
3133 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003134 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003135 break;
3136 case TargetLowering::Promote: {
3137 MVT::ValueType OVT = Tmp1.getValueType();
3138 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003139 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003140
Chris Lattner456a93a2006-01-28 07:39:30 +00003141 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3142 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3143 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3144 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3145 break;
3146 }
3147 case TargetLowering::Expand:
3148 Result = ExpandBSWAP(Tmp1);
3149 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003150 }
3151 break;
3152
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003153 case ISD::CTPOP:
3154 case ISD::CTTZ:
3155 case ISD::CTLZ:
3156 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3157 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003158 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003159 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003160 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003161 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003162 TargetLowering::Custom) {
3163 Tmp1 = TLI.LowerOperation(Result, DAG);
3164 if (Tmp1.Val) {
3165 Result = Tmp1;
3166 }
Scott Michel910b66d2007-07-30 21:00:31 +00003167 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003168 break;
3169 case TargetLowering::Promote: {
3170 MVT::ValueType OVT = Tmp1.getValueType();
3171 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003172
3173 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003174 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3175 // Perform the larger operation, then subtract if needed.
3176 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003177 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003178 case ISD::CTPOP:
3179 Result = Tmp1;
3180 break;
3181 case ISD::CTTZ:
3182 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003183 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003184 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003185 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003186 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003187 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003188 break;
3189 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003190 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003191 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003192 DAG.getConstant(MVT::getSizeInBits(NVT) -
3193 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003194 break;
3195 }
3196 break;
3197 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003198 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003199 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003200 break;
3201 }
3202 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003203
Chris Lattner2c8086f2005-04-02 05:00:07 +00003204 // Unary operators
3205 case ISD::FABS:
3206 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003207 case ISD::FSQRT:
3208 case ISD::FSIN:
3209 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003210 Tmp1 = LegalizeOp(Node->getOperand(0));
3211 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003212 case TargetLowering::Promote:
3213 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003214 isCustom = true;
3215 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003216 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003217 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003218 if (isCustom) {
3219 Tmp1 = TLI.LowerOperation(Result, DAG);
3220 if (Tmp1.Val) Result = Tmp1;
3221 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003222 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003223 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003224 switch (Node->getOpcode()) {
3225 default: assert(0 && "Unreachable!");
3226 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003227 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3228 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003229 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003230 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003231 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003232 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3233 MVT::ValueType VT = Node->getValueType(0);
3234 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003235 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003236 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3237 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003238 break;
3239 }
3240 case ISD::FSQRT:
3241 case ISD::FSIN:
3242 case ISD::FCOS: {
3243 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003244
3245 // Expand unsupported unary vector operators by unrolling them.
3246 if (MVT::isVector(VT)) {
3247 Result = LegalizeOp(UnrollVectorOp(Op));
3248 break;
3249 }
3250
Evan Cheng56966222007-01-12 02:11:51 +00003251 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003252 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003253 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00003254 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003255 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3256 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3257 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3258 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00003259 break;
3260 case ISD::FSIN:
3261 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3262 break;
3263 case ISD::FCOS:
3264 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3265 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003266 default: assert(0 && "Unreachable!");
3267 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003268 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003269 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3270 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003271 break;
3272 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003273 }
3274 break;
3275 }
3276 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003277 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003278 MVT::ValueType VT = Node->getValueType(0);
3279
3280 // Expand unsupported unary vector operators by unrolling them.
3281 if (MVT::isVector(VT)) {
3282 Result = LegalizeOp(UnrollVectorOp(Op));
3283 break;
3284 }
3285
3286 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen317096a2007-09-28 01:08:20 +00003287 RTLIB::Libcall LC =
Dan Gohman82669522007-10-11 23:57:53 +00003288 VT == MVT::f32 ? RTLIB::POWI_F32 :
3289 VT == MVT::f64 ? RTLIB::POWI_F64 :
3290 VT == MVT::f80 ? RTLIB::POWI_F80 :
3291 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003292 RTLIB::UNKNOWN_LIBCALL;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003293 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003294 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3295 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003296 break;
3297 }
Chris Lattner35481892005-12-23 00:16:34 +00003298 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003299 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003300 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003301 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3302 // The input has to be a vector type, we have to either scalarize it, pack
3303 // it, or convert it based on whether the input vector type is legal.
3304 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003305 int InIx = Node->getOperand(0).ResNo;
3306 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3307 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003308
3309 // Figure out if there is a simple type corresponding to this Vector
3310 // type. If so, convert to the vector type.
3311 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3312 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003313 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003314 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3315 LegalizeOp(Node->getOperand(0)));
3316 break;
3317 } else if (NumElems == 1) {
3318 // Turn this into a bit convert of the scalar input.
3319 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3320 ScalarizeVectorOp(Node->getOperand(0)));
3321 break;
3322 } else {
3323 // FIXME: UNIMP! Store then reload
3324 assert(0 && "Cast from unsupported vector type not implemented yet!");
3325 }
Chris Lattner67993f72006-01-23 07:30:46 +00003326 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003327 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3328 Node->getOperand(0).getValueType())) {
3329 default: assert(0 && "Unknown operation action!");
3330 case TargetLowering::Expand:
3331 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3332 break;
3333 case TargetLowering::Legal:
3334 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003335 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003336 break;
3337 }
3338 }
3339 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003340
Chris Lattner2c8086f2005-04-02 05:00:07 +00003341 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003342 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003343 case ISD::UINT_TO_FP: {
3344 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003345 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3346 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003347 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003348 Node->getOperand(0).getValueType())) {
3349 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003350 case TargetLowering::Custom:
3351 isCustom = true;
3352 // FALLTHROUGH
3353 case TargetLowering::Legal:
3354 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003355 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003356 if (isCustom) {
3357 Tmp1 = TLI.LowerOperation(Result, DAG);
3358 if (Tmp1.Val) Result = Tmp1;
3359 }
3360 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003361 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003362 Result = ExpandLegalINT_TO_FP(isSigned,
3363 LegalizeOp(Node->getOperand(0)),
3364 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003365 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003366 case TargetLowering::Promote:
3367 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3368 Node->getValueType(0),
3369 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003370 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003371 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003372 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003373 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003374 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3375 Node->getValueType(0), Node->getOperand(0));
3376 break;
3377 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003378 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003379 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003380 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3381 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003382 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003383 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3384 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003385 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003386 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3387 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003388 break;
3389 }
3390 break;
3391 }
3392 case ISD::TRUNCATE:
3393 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3394 case Legal:
3395 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003396 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003397 break;
3398 case Expand:
3399 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3400
3401 // Since the result is legal, we should just be able to truncate the low
3402 // part of the source.
3403 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3404 break;
3405 case Promote:
3406 Result = PromoteOp(Node->getOperand(0));
3407 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3408 break;
3409 }
3410 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003411
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003412 case ISD::FP_TO_SINT:
3413 case ISD::FP_TO_UINT:
3414 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3415 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003416 Tmp1 = LegalizeOp(Node->getOperand(0));
3417
Chris Lattner1618beb2005-07-29 00:11:56 +00003418 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3419 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003420 case TargetLowering::Custom:
3421 isCustom = true;
3422 // FALLTHROUGH
3423 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003424 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003425 if (isCustom) {
3426 Tmp1 = TLI.LowerOperation(Result, DAG);
3427 if (Tmp1.Val) Result = Tmp1;
3428 }
3429 break;
3430 case TargetLowering::Promote:
3431 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3432 Node->getOpcode() == ISD::FP_TO_SINT);
3433 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003434 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003435 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3436 SDOperand True, False;
3437 MVT::ValueType VT = Node->getOperand(0).getValueType();
3438 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003439 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003440 const uint64_t zero[] = {0, 0};
3441 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3442 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003443 (void)apf.convertFromZeroExtendedInteger
3444 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003445 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003446 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3447 Node->getOperand(0), Tmp2, ISD::SETLT);
3448 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3449 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003450 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003451 Tmp2));
3452 False = DAG.getNode(ISD::XOR, NVT, False,
3453 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003454 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3455 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003456 } else {
3457 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3458 }
3459 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003460 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003461 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003462 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003463 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003464 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003465 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003466 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003467 if (Node->getOpcode()==ISD::FP_TO_SINT)
3468 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003469 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3470 (DAG.getNode(ISD::FP_ROUND_INREG,
3471 MVT::ppcf128, Node->getOperand(0),
3472 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003473 else {
3474 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3475 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3476 Tmp2 = DAG.getConstantFP(apf, OVT);
3477 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3478 // FIXME: generated code sucks.
3479 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3480 DAG.getNode(ISD::ADD, MVT::i32,
3481 DAG.getNode(ISD::FP_TO_SINT, VT,
3482 DAG.getNode(ISD::FSUB, OVT,
3483 Node->getOperand(0), Tmp2)),
3484 DAG.getConstant(0x80000000, MVT::i32)),
3485 DAG.getNode(ISD::FP_TO_SINT, VT,
3486 Node->getOperand(0)),
3487 DAG.getCondCode(ISD::SETGE));
3488 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003489 break;
3490 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003491 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003492 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003493 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003494 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003495 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003496 LC = (VT == MVT::i32)
3497 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003498 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003499 LC = (VT == MVT::i32)
3500 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003501 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003502 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003503 LC = RTLIB::FPTOSINT_F80_I64;
3504 }
3505 else if (OVT == MVT::ppcf128) {
3506 assert(VT == MVT::i64);
3507 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003508 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003509 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003510 }
3511 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003512 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003513 LC = (VT == MVT::i32)
3514 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003515 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003516 LC = (VT == MVT::i32)
3517 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003518 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003519 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003520 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3521 }
3522 else if (OVT == MVT::ppcf128) {
3523 assert(VT == MVT::i64);
3524 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003525 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003526 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003527 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003528 default: assert(0 && "Unreachable!");
3529 }
3530 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003531 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3532 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003533 break;
3534 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003535 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003536 Tmp1 = PromoteOp(Node->getOperand(0));
3537 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3538 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003539 break;
3540 }
3541 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003542
Dale Johannesenab081c72007-08-09 17:27:48 +00003543 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003544 case ISD::FP_ROUND: {
3545 MVT::ValueType newVT = Op.getValueType();
3546 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3547 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003548 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3549 SDOperand Lo, Hi;
3550 ExpandOp(Node->getOperand(0), Lo, Hi);
3551 if (newVT == MVT::f64)
3552 Result = Hi;
3553 else
3554 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3555 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003556 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003557 // The only other way we can lower this is to turn it into a STORE,
3558 // LOAD pair, targetting a temporary location (a stack slot).
3559
3560 // NOTE: there is a choice here between constantly creating new stack
3561 // slots and always reusing the same one. We currently always create
3562 // new ones, as reuse may inhibit scheduling.
3563 MVT::ValueType slotVT =
3564 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3565 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003566 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003567 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3568 MachineFunction &MF = DAG.getMachineFunction();
3569 int SSFI =
3570 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3571 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3572 if (Node->getOpcode() == ISD::FP_EXTEND) {
3573 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3574 StackSlot, NULL, 0);
3575 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3576 Result, StackSlot, NULL, 0, oldVT);
3577 } else {
3578 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3579 StackSlot, NULL, 0, newVT);
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003580 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003581 }
3582 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003583 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003584 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003585 }
3586 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003587 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003588 case ISD::ZERO_EXTEND:
3589 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003591 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003592 case Legal:
3593 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003594 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003595 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003596 case Promote:
3597 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003598 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003599 Tmp1 = PromoteOp(Node->getOperand(0));
3600 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003601 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003602 case ISD::ZERO_EXTEND:
3603 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003604 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003605 Result = DAG.getZeroExtendInReg(Result,
3606 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003607 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003608 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003609 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003610 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003611 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003612 Result,
3613 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003614 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003615 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003616 Result = PromoteOp(Node->getOperand(0));
3617 if (Result.getValueType() != Op.getValueType())
3618 // Dynamically dead while we have only 2 FP types.
3619 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3620 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003621 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003622 Result = PromoteOp(Node->getOperand(0));
3623 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3624 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003625 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003626 }
3627 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003628 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003629 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003630 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003631 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003632
3633 // If this operation is not supported, convert it to a shl/shr or load/store
3634 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003635 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3636 default: assert(0 && "This action not supported for this op yet!");
3637 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003639 break;
3640 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003641 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003642 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003643 // NOTE: we could fall back on load/store here too for targets without
3644 // SAR. However, it is doubtful that any exist.
3645 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3646 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003647 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003648 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3649 Node->getOperand(0), ShiftCst);
3650 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3651 Result, ShiftCst);
3652 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003653 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003654 // EXTLOAD pair, targetting a temporary location (a stack slot).
3655
3656 // NOTE: there is a choice here between constantly creating new stack
3657 // slots and always reusing the same one. We currently always create
3658 // new ones, as reuse may inhibit scheduling.
3659 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003660 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003661 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003662 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003663 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003664 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003665 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003666 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3667 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003668 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003669 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003670 } else {
3671 assert(0 && "Unknown op");
3672 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003673 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003674 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003675 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003676 }
Duncan Sands36397f52007-07-27 12:58:54 +00003677 case ISD::TRAMPOLINE: {
3678 SDOperand Ops[6];
3679 for (unsigned i = 0; i != 6; ++i)
3680 Ops[i] = LegalizeOp(Node->getOperand(i));
3681 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3682 // The only option for this node is to custom lower it.
3683 Result = TLI.LowerOperation(Result, DAG);
3684 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003685
3686 // Since trampoline produces two values, make sure to remember that we
3687 // legalized both of them.
3688 Tmp1 = LegalizeOp(Result.getValue(1));
3689 Result = LegalizeOp(Result);
3690 AddLegalizedOperand(SDOperand(Node, 0), Result);
3691 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3692 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003693 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003694 case ISD::FLT_ROUNDS: {
3695 MVT::ValueType VT = Node->getValueType(0);
3696 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3697 default: assert(0 && "This action not supported for this op yet!");
3698 case TargetLowering::Custom:
3699 Result = TLI.LowerOperation(Op, DAG);
3700 if (Result.Val) break;
3701 // Fall Thru
3702 case TargetLowering::Legal:
3703 // If this operation is not supported, lower it to constant 1
3704 Result = DAG.getConstant(1, VT);
3705 break;
3706 }
3707 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003708 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003709
Chris Lattner4ddd2832006-04-08 04:13:17 +00003710 assert(Result.getValueType() == Op.getValueType() &&
3711 "Bad legalization!");
3712
Chris Lattner456a93a2006-01-28 07:39:30 +00003713 // Make sure that the generated code is itself legal.
3714 if (Result != Op)
3715 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003716
Chris Lattner45982da2005-05-12 16:53:42 +00003717 // Note that LegalizeOp may be reentered even from single-use nodes, which
3718 // means that we always must cache transformed nodes.
3719 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003720 return Result;
3721}
3722
Chris Lattner8b6fa222005-01-15 22:16:26 +00003723/// PromoteOp - Given an operation that produces a value in an invalid type,
3724/// promote it to compute the value into a larger type. The produced value will
3725/// have the correct bits for the low portion of the register, but no guarantee
3726/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003727SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3728 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003729 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003730 assert(getTypeAction(VT) == Promote &&
3731 "Caller should expand or legalize operands that are not promotable!");
3732 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3733 "Cannot promote to smaller type!");
3734
Chris Lattner03c85462005-01-15 05:21:40 +00003735 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003736 SDOperand Result;
3737 SDNode *Node = Op.Val;
3738
Chris Lattner40030bf2007-02-04 01:17:38 +00003739 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003740 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003741
Chris Lattner03c85462005-01-15 05:21:40 +00003742 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003743 case ISD::CopyFromReg:
3744 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003745 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003746#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003747 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003748#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003749 assert(0 && "Do not know how to promote this operator!");
3750 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003751 case ISD::UNDEF:
3752 Result = DAG.getNode(ISD::UNDEF, NVT);
3753 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003754 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003755 if (VT != MVT::i1)
3756 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3757 else
3758 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003759 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3760 break;
3761 case ISD::ConstantFP:
3762 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3763 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3764 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003765
Chris Lattner82fbfb62005-01-18 02:59:52 +00003766 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003767 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003768 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3769 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003770 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003771
Chris Lattner03c85462005-01-15 05:21:40 +00003772 case ISD::TRUNCATE:
3773 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3774 case Legal:
3775 Result = LegalizeOp(Node->getOperand(0));
3776 assert(Result.getValueType() >= NVT &&
3777 "This truncation doesn't make sense!");
3778 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3779 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3780 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003781 case Promote:
3782 // The truncation is not required, because we don't guarantee anything
3783 // about high bits anyway.
3784 Result = PromoteOp(Node->getOperand(0));
3785 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003786 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003787 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3788 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003789 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003790 }
3791 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003792 case ISD::SIGN_EXTEND:
3793 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003794 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003795 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3796 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3797 case Legal:
3798 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003799 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003800 break;
3801 case Promote:
3802 // Promote the reg if it's smaller.
3803 Result = PromoteOp(Node->getOperand(0));
3804 // The high bits are not guaranteed to be anything. Insert an extend.
3805 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003806 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003807 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003808 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003809 Result = DAG.getZeroExtendInReg(Result,
3810 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003811 break;
3812 }
3813 break;
Chris Lattner35481892005-12-23 00:16:34 +00003814 case ISD::BIT_CONVERT:
3815 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3816 Result = PromoteOp(Result);
3817 break;
3818
Chris Lattner8b6fa222005-01-15 22:16:26 +00003819 case ISD::FP_EXTEND:
3820 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3821 case ISD::FP_ROUND:
3822 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3823 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3824 case Promote: assert(0 && "Unreachable with 2 FP types!");
3825 case Legal:
3826 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003827 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003828 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003829 break;
3830 }
3831 break;
3832
3833 case ISD::SINT_TO_FP:
3834 case ISD::UINT_TO_FP:
3835 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3836 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003837 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003838 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003839 break;
3840
3841 case Promote:
3842 Result = PromoteOp(Node->getOperand(0));
3843 if (Node->getOpcode() == ISD::SINT_TO_FP)
3844 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003845 Result,
3846 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003847 else
Chris Lattner23993562005-04-13 02:38:47 +00003848 Result = DAG.getZeroExtendInReg(Result,
3849 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003850 // No extra round required here.
3851 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003852 break;
3853 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003854 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3855 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003856 // Round if we cannot tolerate excess precision.
3857 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003858 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3859 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003860 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003861 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003862 break;
3863
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003864 case ISD::SIGN_EXTEND_INREG:
3865 Result = PromoteOp(Node->getOperand(0));
3866 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3867 Node->getOperand(1));
3868 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003869 case ISD::FP_TO_SINT:
3870 case ISD::FP_TO_UINT:
3871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3872 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003873 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003874 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003875 break;
3876 case Promote:
3877 // The input result is prerounded, so we don't have to do anything
3878 // special.
3879 Tmp1 = PromoteOp(Node->getOperand(0));
3880 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003881 }
Nate Begemand2558e32005-08-14 01:20:53 +00003882 // If we're promoting a UINT to a larger size, check to see if the new node
3883 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3884 // we can use that instead. This allows us to generate better code for
3885 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3886 // legal, such as PowerPC.
3887 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003888 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003889 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3890 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003891 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3892 } else {
3893 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3894 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003895 break;
3896
Chris Lattner2c8086f2005-04-02 05:00:07 +00003897 case ISD::FABS:
3898 case ISD::FNEG:
3899 Tmp1 = PromoteOp(Node->getOperand(0));
3900 assert(Tmp1.getValueType() == NVT);
3901 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3902 // NOTE: we do not have to do any extra rounding here for
3903 // NoExcessFPPrecision, because we know the input will have the appropriate
3904 // precision, and these operations don't modify precision at all.
3905 break;
3906
Chris Lattnerda6ba872005-04-28 21:44:33 +00003907 case ISD::FSQRT:
3908 case ISD::FSIN:
3909 case ISD::FCOS:
3910 Tmp1 = PromoteOp(Node->getOperand(0));
3911 assert(Tmp1.getValueType() == NVT);
3912 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003913 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003914 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3915 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003916 break;
3917
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003918 case ISD::FPOWI: {
3919 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3920 // directly as well, which may be better.
3921 Tmp1 = PromoteOp(Node->getOperand(0));
3922 assert(Tmp1.getValueType() == NVT);
3923 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3924 if (NoExcessFPPrecision)
3925 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3926 DAG.getValueType(VT));
3927 break;
3928 }
3929
Chris Lattner03c85462005-01-15 05:21:40 +00003930 case ISD::AND:
3931 case ISD::OR:
3932 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003933 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003934 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003935 case ISD::MUL:
3936 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003937 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003938 // that too is okay if they are integer operations.
3939 Tmp1 = PromoteOp(Node->getOperand(0));
3940 Tmp2 = PromoteOp(Node->getOperand(1));
3941 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3942 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003943 break;
3944 case ISD::FADD:
3945 case ISD::FSUB:
3946 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003947 Tmp1 = PromoteOp(Node->getOperand(0));
3948 Tmp2 = PromoteOp(Node->getOperand(1));
3949 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3950 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3951
3952 // Floating point operations will give excess precision that we may not be
3953 // able to tolerate. If we DO allow excess precision, just leave it,
3954 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003955 // FIXME: Why would we need to round FP ops more than integer ones?
3956 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003957 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003958 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3959 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003960 break;
3961
Chris Lattner8b6fa222005-01-15 22:16:26 +00003962 case ISD::SDIV:
3963 case ISD::SREM:
3964 // These operators require that their input be sign extended.
3965 Tmp1 = PromoteOp(Node->getOperand(0));
3966 Tmp2 = PromoteOp(Node->getOperand(1));
3967 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003968 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3969 DAG.getValueType(VT));
3970 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3971 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003972 }
3973 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3974
3975 // Perform FP_ROUND: this is probably overly pessimistic.
3976 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003977 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3978 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003979 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003980 case ISD::FDIV:
3981 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003982 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003983 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003984 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3985 case Legal:
3986 Tmp1 = LegalizeOp(Node->getOperand(0));
3987 break;
3988 case Promote:
3989 Tmp1 = PromoteOp(Node->getOperand(0));
3990 break;
3991 case Expand:
3992 assert(0 && "not implemented");
3993 }
3994 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3995 case Legal:
3996 Tmp2 = LegalizeOp(Node->getOperand(1));
3997 break;
3998 case Promote:
3999 Tmp2 = PromoteOp(Node->getOperand(1));
4000 break;
4001 case Expand:
4002 assert(0 && "not implemented");
4003 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004004 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4005
4006 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004007 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004008 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4009 DAG.getValueType(VT));
4010 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004011
4012 case ISD::UDIV:
4013 case ISD::UREM:
4014 // These operators require that their input be zero extended.
4015 Tmp1 = PromoteOp(Node->getOperand(0));
4016 Tmp2 = PromoteOp(Node->getOperand(1));
4017 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004018 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4019 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004020 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4021 break;
4022
4023 case ISD::SHL:
4024 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004025 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004026 break;
4027 case ISD::SRA:
4028 // The input value must be properly sign extended.
4029 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004030 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4031 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004032 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004033 break;
4034 case ISD::SRL:
4035 // The input value must be properly zero extended.
4036 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004037 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004038 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004039 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004040
4041 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004042 Tmp1 = Node->getOperand(0); // Get the chain.
4043 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004044 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4045 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4046 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4047 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004048 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004049 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004050 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004051 // Increment the pointer, VAList, to the next vaarg
4052 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4053 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4054 TLI.getPointerTy()));
4055 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004056 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4057 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004058 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004059 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004060 }
4061 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004062 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004063 break;
4064
Evan Cheng466685d2006-10-09 20:57:25 +00004065 case ISD::LOAD: {
4066 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004067 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4068 ? ISD::EXTLOAD : LD->getExtensionType();
4069 Result = DAG.getExtLoad(ExtType, NVT,
4070 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004071 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004072 LD->getLoadedVT(),
4073 LD->isVolatile(),
4074 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004075 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004076 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004077 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004078 }
Chris Lattner03c85462005-01-15 05:21:40 +00004079 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004080 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4081 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004082 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004083 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004084 case ISD::SELECT_CC:
4085 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4086 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4087 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004088 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004089 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004090 case ISD::BSWAP:
4091 Tmp1 = Node->getOperand(0);
4092 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4093 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4094 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004095 DAG.getConstant(MVT::getSizeInBits(NVT) -
4096 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004097 TLI.getShiftAmountTy()));
4098 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004099 case ISD::CTPOP:
4100 case ISD::CTTZ:
4101 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004102 // Zero extend the argument
4103 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004104 // Perform the larger operation, then subtract if needed.
4105 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004106 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004107 case ISD::CTPOP:
4108 Result = Tmp1;
4109 break;
4110 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004111 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004112 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004113 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4114 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004115 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004116 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004117 break;
4118 case ISD::CTLZ:
4119 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004120 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004121 DAG.getConstant(MVT::getSizeInBits(NVT) -
4122 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004123 break;
4124 }
4125 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004126 case ISD::EXTRACT_SUBVECTOR:
4127 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004128 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004129 case ISD::EXTRACT_VECTOR_ELT:
4130 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4131 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004132 }
4133
4134 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004135
4136 // Make sure the result is itself legal.
4137 Result = LegalizeOp(Result);
4138
4139 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004140 AddPromotedOperand(Op, Result);
4141 return Result;
4142}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004143
Dan Gohman7f321562007-06-25 16:23:39 +00004144/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4145/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4146/// based on the vector type. The return type of this matches the element type
4147/// of the vector, which may not be legal for the target.
4148SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004149 // We know that operand #0 is the Vec vector. If the index is a constant
4150 // or if the invec is a supported hardware type, we can use it. Otherwise,
4151 // lower to a store then an indexed load.
4152 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004153 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004154
Dan Gohmane40c7b02007-09-24 15:54:53 +00004155 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004156 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004157
Dan Gohman7f321562007-06-25 16:23:39 +00004158 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4159 default: assert(0 && "This action is not supported yet!");
4160 case TargetLowering::Custom: {
4161 Vec = LegalizeOp(Vec);
4162 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4163 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4164 if (Tmp3.Val)
4165 return Tmp3;
4166 break;
4167 }
4168 case TargetLowering::Legal:
4169 if (isTypeLegal(TVT)) {
4170 Vec = LegalizeOp(Vec);
4171 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004172 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004173 }
4174 break;
4175 case TargetLowering::Expand:
4176 break;
4177 }
4178
4179 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004180 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004181 Op = ScalarizeVectorOp(Vec);
4182 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4183 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004184 SDOperand Lo, Hi;
4185 SplitVectorOp(Vec, Lo, Hi);
4186 if (CIdx->getValue() < NumElems/2) {
4187 Vec = Lo;
4188 } else {
4189 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004190 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4191 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004192 }
Dan Gohman7f321562007-06-25 16:23:39 +00004193
Chris Lattner15972212006-03-31 17:55:51 +00004194 // It's now an extract from the appropriate high or low part. Recurse.
4195 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004196 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004197 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004198 // Store the value to a temporary stack slot, then LOAD the scalar
4199 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004200 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004201 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4202
4203 // Add the offset to the index.
4204 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4205 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4206 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004207
4208 if (MVT::getSizeInBits(Idx.getValueType()) >
4209 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004210 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004211 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004212 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004213
Dan Gohman7f321562007-06-25 16:23:39 +00004214 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4215
4216 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004217 }
Dan Gohman7f321562007-06-25 16:23:39 +00004218 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004219}
4220
Dan Gohman7f321562007-06-25 16:23:39 +00004221/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004222/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004223SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004224 // We know that operand #0 is the Vec vector. For now we assume the index
4225 // is a constant and that the extracted result is a supported hardware type.
4226 SDOperand Vec = Op.getOperand(0);
4227 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4228
Dan Gohman7f321562007-06-25 16:23:39 +00004229 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004230
4231 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4232 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004233 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004234 }
4235
4236 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4237 SDOperand Lo, Hi;
4238 SplitVectorOp(Vec, Lo, Hi);
4239 if (CIdx->getValue() < NumElems/2) {
4240 Vec = Lo;
4241 } else {
4242 Vec = Hi;
4243 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4244 }
4245
4246 // It's now an extract from the appropriate high or low part. Recurse.
4247 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004248 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004249}
4250
Nate Begeman750ac1b2006-02-01 07:19:44 +00004251/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4252/// with condition CC on the current target. This usually involves legalizing
4253/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4254/// there may be no choice but to create a new SetCC node to represent the
4255/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4256/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4257void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4258 SDOperand &RHS,
4259 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004260 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004261
4262 switch (getTypeAction(LHS.getValueType())) {
4263 case Legal:
4264 Tmp1 = LegalizeOp(LHS); // LHS
4265 Tmp2 = LegalizeOp(RHS); // RHS
4266 break;
4267 case Promote:
4268 Tmp1 = PromoteOp(LHS); // LHS
4269 Tmp2 = PromoteOp(RHS); // RHS
4270
4271 // If this is an FP compare, the operands have already been extended.
4272 if (MVT::isInteger(LHS.getValueType())) {
4273 MVT::ValueType VT = LHS.getValueType();
4274 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4275
4276 // Otherwise, we have to insert explicit sign or zero extends. Note
4277 // that we could insert sign extends for ALL conditions, but zero extend
4278 // is cheaper on many machines (an AND instead of two shifts), so prefer
4279 // it.
4280 switch (cast<CondCodeSDNode>(CC)->get()) {
4281 default: assert(0 && "Unknown integer comparison!");
4282 case ISD::SETEQ:
4283 case ISD::SETNE:
4284 case ISD::SETUGE:
4285 case ISD::SETUGT:
4286 case ISD::SETULE:
4287 case ISD::SETULT:
4288 // ALL of these operations will work if we either sign or zero extend
4289 // the operands (including the unsigned comparisons!). Zero extend is
4290 // usually a simpler/cheaper operation, so prefer it.
4291 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4292 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4293 break;
4294 case ISD::SETGE:
4295 case ISD::SETGT:
4296 case ISD::SETLT:
4297 case ISD::SETLE:
4298 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4299 DAG.getValueType(VT));
4300 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4301 DAG.getValueType(VT));
4302 break;
4303 }
4304 }
4305 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004306 case Expand: {
4307 MVT::ValueType VT = LHS.getValueType();
4308 if (VT == MVT::f32 || VT == MVT::f64) {
4309 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004310 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004311 switch (cast<CondCodeSDNode>(CC)->get()) {
4312 case ISD::SETEQ:
4313 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004314 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004315 break;
4316 case ISD::SETNE:
4317 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004318 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004319 break;
4320 case ISD::SETGE:
4321 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004322 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004323 break;
4324 case ISD::SETLT:
4325 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004326 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004327 break;
4328 case ISD::SETLE:
4329 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004330 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004331 break;
4332 case ISD::SETGT:
4333 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004334 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004335 break;
4336 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004337 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4338 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004339 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004340 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004341 break;
4342 default:
Evan Cheng56966222007-01-12 02:11:51 +00004343 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004344 switch (cast<CondCodeSDNode>(CC)->get()) {
4345 case ISD::SETONE:
4346 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004347 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004348 // Fallthrough
4349 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004350 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004351 break;
4352 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004353 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004354 break;
4355 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004356 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004357 break;
4358 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004359 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004360 break;
Evan Cheng56966222007-01-12 02:11:51 +00004361 case ISD::SETUEQ:
4362 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004363 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004364 default: assert(0 && "Unsupported FP setcc!");
4365 }
4366 }
4367
4368 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004369 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004370 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004371 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004372 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004373 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004374 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004375 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004376 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004377 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004378 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004379 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004380 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004381 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4382 Tmp2 = SDOperand();
4383 }
4384 LHS = Tmp1;
4385 RHS = Tmp2;
4386 return;
4387 }
4388
Nate Begeman750ac1b2006-02-01 07:19:44 +00004389 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4390 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004391 ExpandOp(RHS, RHSLo, RHSHi);
4392 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4393
4394 if (VT==MVT::ppcf128) {
4395 // FIXME: This generated code sucks. We want to generate
4396 // FCMP crN, hi1, hi2
4397 // BNE crN, L:
4398 // FCMP crN, lo1, lo2
4399 // The following can be improved, but not that much.
4400 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4401 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4402 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4403 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4404 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4405 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4406 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4407 Tmp2 = SDOperand();
4408 break;
4409 }
4410
4411 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004412 case ISD::SETEQ:
4413 case ISD::SETNE:
4414 if (RHSLo == RHSHi)
4415 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4416 if (RHSCST->isAllOnesValue()) {
4417 // Comparison to -1.
4418 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4419 Tmp2 = RHSLo;
4420 break;
4421 }
4422
4423 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4424 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4425 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4426 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4427 break;
4428 default:
4429 // If this is a comparison of the sign bit, just look at the top part.
4430 // X > -1, x < 0
4431 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4432 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4433 CST->getValue() == 0) || // X < 0
4434 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4435 CST->isAllOnesValue())) { // X > -1
4436 Tmp1 = LHSHi;
4437 Tmp2 = RHSHi;
4438 break;
4439 }
4440
4441 // FIXME: This generated code sucks.
4442 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004443 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004444 default: assert(0 && "Unknown integer setcc!");
4445 case ISD::SETLT:
4446 case ISD::SETULT: LowCC = ISD::SETULT; break;
4447 case ISD::SETGT:
4448 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4449 case ISD::SETLE:
4450 case ISD::SETULE: LowCC = ISD::SETULE; break;
4451 case ISD::SETGE:
4452 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4453 }
4454
4455 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4456 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4457 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4458
4459 // NOTE: on targets without efficient SELECT of bools, we can always use
4460 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004461 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4462 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4463 false, DagCombineInfo);
4464 if (!Tmp1.Val)
4465 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4466 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4467 CCCode, false, DagCombineInfo);
4468 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004469 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004470
4471 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4472 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4473 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4474 (Tmp2C && Tmp2C->getValue() == 0 &&
4475 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4476 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4477 (Tmp2C && Tmp2C->getValue() == 1 &&
4478 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4479 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4480 // low part is known false, returns high part.
4481 // For LE / GE, if high part is known false, ignore the low part.
4482 // For LT / GT, if high part is known true, ignore the low part.
4483 Tmp1 = Tmp2;
4484 Tmp2 = SDOperand();
4485 } else {
4486 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4487 ISD::SETEQ, false, DagCombineInfo);
4488 if (!Result.Val)
4489 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4490 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4491 Result, Tmp1, Tmp2));
4492 Tmp1 = Result;
4493 Tmp2 = SDOperand();
4494 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004495 }
4496 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004497 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004498 LHS = Tmp1;
4499 RHS = Tmp2;
4500}
4501
Chris Lattner35481892005-12-23 00:16:34 +00004502/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004503/// The resultant code need not be legal. Note that SrcOp is the input operand
4504/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004505SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4506 SDOperand SrcOp) {
4507 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004508 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004509
4510 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004511 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004512 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004513 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004514}
4515
Chris Lattner4352cc92006-04-04 17:23:26 +00004516SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4517 // Create a vector sized/aligned stack slot, store the value to element #0,
4518 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004519 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004520 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004521 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004522 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004523}
4524
4525
Chris Lattnerce872152006-03-19 06:31:19 +00004526/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004527/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004528SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4529
4530 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004531 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004532 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004533 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004534 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004535 std::map<SDOperand, std::vector<unsigned> > Values;
4536 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004537 bool isConstant = true;
4538 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4539 SplatValue.getOpcode() != ISD::UNDEF)
4540 isConstant = false;
4541
Evan Cheng033e6812006-03-24 01:17:21 +00004542 for (unsigned i = 1; i < NumElems; ++i) {
4543 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004544 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004545 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004546 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004547 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004548 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004549
4550 // If this isn't a constant element or an undef, we can't use a constant
4551 // pool load.
4552 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4553 V.getOpcode() != ISD::UNDEF)
4554 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004555 }
4556
4557 if (isOnlyLowElement) {
4558 // If the low element is an undef too, then this whole things is an undef.
4559 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4560 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4561 // Otherwise, turn this into a scalar_to_vector node.
4562 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4563 Node->getOperand(0));
4564 }
4565
Chris Lattner2eb86532006-03-24 07:29:17 +00004566 // If all elements are constants, create a load from the constant pool.
4567 if (isConstant) {
4568 MVT::ValueType VT = Node->getValueType(0);
4569 const Type *OpNTy =
4570 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4571 std::vector<Constant*> CV;
4572 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4573 if (ConstantFPSDNode *V =
4574 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004575 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004576 } else if (ConstantSDNode *V =
4577 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004578 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004579 } else {
4580 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4581 CV.push_back(UndefValue::get(OpNTy));
4582 }
4583 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004584 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004585 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004586 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004587 }
4588
Chris Lattner87100e02006-03-20 01:52:29 +00004589 if (SplatValue.Val) { // Splat of one value?
4590 // Build the shuffle constant vector: <0, 0, 0, 0>
4591 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004592 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004593 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004594 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004595 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4596 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004597
4598 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004599 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004600 // Get the splatted value into the low element of a vector register.
4601 SDOperand LowValVec =
4602 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4603
4604 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4605 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4606 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4607 SplatMask);
4608 }
4609 }
4610
Evan Cheng033e6812006-03-24 01:17:21 +00004611 // If there are only two unique elements, we may be able to turn this into a
4612 // vector shuffle.
4613 if (Values.size() == 2) {
4614 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4615 MVT::ValueType MaskVT =
4616 MVT::getIntVectorWithNumElements(NumElems);
4617 std::vector<SDOperand> MaskVec(NumElems);
4618 unsigned i = 0;
4619 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4620 E = Values.end(); I != E; ++I) {
4621 for (std::vector<unsigned>::iterator II = I->second.begin(),
4622 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004623 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004624 i += NumElems;
4625 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004626 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4627 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004628
4629 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004630 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4631 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004632 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004633 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4634 E = Values.end(); I != E; ++I) {
4635 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4636 I->first);
4637 Ops.push_back(Op);
4638 }
4639 Ops.push_back(ShuffleMask);
4640
4641 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004642 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4643 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004644 }
4645 }
Chris Lattnerce872152006-03-19 06:31:19 +00004646
4647 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4648 // aligned object on the stack, store each element into it, then load
4649 // the result as a vector.
4650 MVT::ValueType VT = Node->getValueType(0);
4651 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004652 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004653
4654 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004655 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004656 unsigned TypeByteSize =
4657 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004658 // Store (in the right endianness) the elements to memory.
4659 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4660 // Ignore undef elements.
4661 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4662
Chris Lattner841c8822006-03-22 01:46:54 +00004663 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004664
4665 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4666 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4667
Evan Cheng786225a2006-10-05 23:01:46 +00004668 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004669 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004670 }
4671
4672 SDOperand StoreChain;
4673 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004674 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4675 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004676 else
4677 StoreChain = DAG.getEntryNode();
4678
4679 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004680 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004681}
4682
Chris Lattner5b359c62005-04-02 04:00:59 +00004683void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4684 SDOperand Op, SDOperand Amt,
4685 SDOperand &Lo, SDOperand &Hi) {
4686 // Expand the subcomponents.
4687 SDOperand LHSL, LHSH;
4688 ExpandOp(Op, LHSL, LHSH);
4689
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004690 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004691 MVT::ValueType VT = LHSL.getValueType();
4692 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004693 Hi = Lo.getValue(1);
4694}
4695
4696
Chris Lattnere34b3962005-01-19 04:19:40 +00004697/// ExpandShift - Try to find a clever way to expand this shift operation out to
4698/// smaller elements. If we can't find a way that is more efficient than a
4699/// libcall on this target, return false. Otherwise, return true with the
4700/// low-parts expanded into Lo and Hi.
4701bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4702 SDOperand &Lo, SDOperand &Hi) {
4703 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4704 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004705
Chris Lattnere34b3962005-01-19 04:19:40 +00004706 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004707 SDOperand ShAmt = LegalizeOp(Amt);
4708 MVT::ValueType ShTy = ShAmt.getValueType();
4709 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4710 unsigned NVTBits = MVT::getSizeInBits(NVT);
4711
Chris Lattner7a3c8552007-10-14 20:35:12 +00004712 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004713 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4714 unsigned Cst = CN->getValue();
4715 // Expand the incoming operand to be shifted, so that we have its parts
4716 SDOperand InL, InH;
4717 ExpandOp(Op, InL, InH);
4718 switch(Opc) {
4719 case ISD::SHL:
4720 if (Cst > VTBits) {
4721 Lo = DAG.getConstant(0, NVT);
4722 Hi = DAG.getConstant(0, NVT);
4723 } else if (Cst > NVTBits) {
4724 Lo = DAG.getConstant(0, NVT);
4725 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004726 } else if (Cst == NVTBits) {
4727 Lo = DAG.getConstant(0, NVT);
4728 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004729 } else {
4730 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4731 Hi = DAG.getNode(ISD::OR, NVT,
4732 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4733 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4734 }
4735 return true;
4736 case ISD::SRL:
4737 if (Cst > VTBits) {
4738 Lo = DAG.getConstant(0, NVT);
4739 Hi = DAG.getConstant(0, NVT);
4740 } else if (Cst > NVTBits) {
4741 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4742 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004743 } else if (Cst == NVTBits) {
4744 Lo = InH;
4745 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004746 } else {
4747 Lo = DAG.getNode(ISD::OR, NVT,
4748 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4749 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4750 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4751 }
4752 return true;
4753 case ISD::SRA:
4754 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004755 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004756 DAG.getConstant(NVTBits-1, ShTy));
4757 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004758 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004759 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004760 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004761 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004762 } else if (Cst == NVTBits) {
4763 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004764 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004765 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004766 } else {
4767 Lo = DAG.getNode(ISD::OR, NVT,
4768 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4769 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4770 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4771 }
4772 return true;
4773 }
4774 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004775
4776 // Okay, the shift amount isn't constant. However, if we can tell that it is
4777 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4778 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004779 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004780
4781 // If we know that the high bit of the shift amount is one, then we can do
4782 // this as a couple of simple shifts.
4783 if (KnownOne & Mask) {
4784 // Mask out the high bit, which we know is set.
4785 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4786 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4787
4788 // Expand the incoming operand to be shifted, so that we have its parts
4789 SDOperand InL, InH;
4790 ExpandOp(Op, InL, InH);
4791 switch(Opc) {
4792 case ISD::SHL:
4793 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4794 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4795 return true;
4796 case ISD::SRL:
4797 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4798 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4799 return true;
4800 case ISD::SRA:
4801 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4802 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4803 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4804 return true;
4805 }
4806 }
4807
4808 // If we know that the high bit of the shift amount is zero, then we can do
4809 // this as a couple of simple shifts.
4810 if (KnownZero & Mask) {
4811 // Compute 32-amt.
4812 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4813 DAG.getConstant(NVTBits, Amt.getValueType()),
4814 Amt);
4815
4816 // Expand the incoming operand to be shifted, so that we have its parts
4817 SDOperand InL, InH;
4818 ExpandOp(Op, InL, InH);
4819 switch(Opc) {
4820 case ISD::SHL:
4821 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4822 Hi = DAG.getNode(ISD::OR, NVT,
4823 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4824 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4825 return true;
4826 case ISD::SRL:
4827 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4828 Lo = DAG.getNode(ISD::OR, NVT,
4829 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4830 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4831 return true;
4832 case ISD::SRA:
4833 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4834 Lo = DAG.getNode(ISD::OR, NVT,
4835 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4836 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4837 return true;
4838 }
4839 }
4840
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004841 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004842}
Chris Lattner77e77a62005-01-21 06:05:23 +00004843
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004844
Chris Lattner77e77a62005-01-21 06:05:23 +00004845// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4846// does not fit into a register, return the lo part and set the hi part to the
4847// by-reg argument. If it does fit into a single register, return the result
4848// and leave the Hi part unset.
4849SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004850 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004851 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4852 // The input chain to this libcall is the entry node of the function.
4853 // Legalizing the call will automatically add the previous call to the
4854 // dependence.
4855 SDOperand InChain = DAG.getEntryNode();
4856
Chris Lattner77e77a62005-01-21 06:05:23 +00004857 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004858 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004859 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4860 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4861 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004862 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004863 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004864 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004865 }
4866 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004867
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004868 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004869 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004870 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004871 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004872 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004873
Chris Lattner6831a812006-02-13 09:18:02 +00004874 // Legalize the call sequence, starting with the chain. This will advance
4875 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4876 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4877 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004878 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004879 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004880 default: assert(0 && "Unknown thing");
4881 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004882 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004883 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004884 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004885 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004886 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004887 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004888 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004889}
4890
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004891
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004892/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4893///
Chris Lattner77e77a62005-01-21 06:05:23 +00004894SDOperand SelectionDAGLegalize::
4895ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004896 assert(getTypeAction(Source.getValueType()) == Expand &&
4897 "This is not an expansion!");
4898 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4899
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004900 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004901 assert(Source.getValueType() == MVT::i64 &&
4902 "This only works for 64-bit -> FP");
4903 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4904 // incoming integer is set. To handle this, we dynamically test to see if
4905 // it is set, and, if so, add a fudge factor.
4906 SDOperand Lo, Hi;
4907 ExpandOp(Source, Lo, Hi);
4908
Chris Lattner66de05b2005-05-13 04:45:13 +00004909 // If this is unsigned, and not supported, first perform the conversion to
4910 // signed, then adjust the result if the sign bit is set.
4911 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4912 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4913
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004914 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4915 DAG.getConstant(0, Hi.getValueType()),
4916 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004917 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4918 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4919 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004920 uint64_t FF = 0x5f800000ULL;
4921 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004922 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004923
Chris Lattner5839bf22005-08-26 17:15:30 +00004924 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004925 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4926 SDOperand FudgeInReg;
4927 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004928 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004929 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004930 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004931 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004932 CPIdx, NULL, 0, MVT::f32);
4933 else
4934 assert(0 && "Unexpected conversion");
4935
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004936 MVT::ValueType SCVT = SignedConv.getValueType();
4937 if (SCVT != DestTy) {
4938 // Destination type needs to be expanded as well. The FADD now we are
4939 // constructing will be expanded into a libcall.
4940 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4941 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4942 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4943 SignedConv, SignedConv.getValue(1));
4944 }
4945 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4946 }
Chris Lattner473a9902005-09-29 06:44:39 +00004947 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004948 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004949
Chris Lattnera88a2602005-05-14 05:33:54 +00004950 // Check to see if the target has a custom way to lower this. If so, use it.
4951 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4952 default: assert(0 && "This action not implemented for this operation!");
4953 case TargetLowering::Legal:
4954 case TargetLowering::Expand:
4955 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004956 case TargetLowering::Custom: {
4957 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4958 Source), DAG);
4959 if (NV.Val)
4960 return LegalizeOp(NV);
4961 break; // The target decided this was legal after all
4962 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004963 }
4964
Chris Lattner13689e22005-05-12 07:00:44 +00004965 // Expand the source, then glue it back together for the call. We must expand
4966 // the source in case it is shared (this pass of legalize must traverse it).
4967 SDOperand SrcLo, SrcHi;
4968 ExpandOp(Source, SrcLo, SrcHi);
4969 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4970
Evan Cheng56966222007-01-12 02:11:51 +00004971 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004972 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004973 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004974 else {
4975 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004976 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004977 }
Chris Lattner6831a812006-02-13 09:18:02 +00004978
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004979 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004980 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4981 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004982 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4983 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004984}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004985
Chris Lattner22cde6a2006-01-28 08:25:58 +00004986/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4987/// INT_TO_FP operation of the specified operand when the target requests that
4988/// we expand it. At this point, we know that the result and operand types are
4989/// legal for the target.
4990SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4991 SDOperand Op0,
4992 MVT::ValueType DestVT) {
4993 if (Op0.getValueType() == MVT::i32) {
4994 // simple 32-bit [signed|unsigned] integer to float/double expansion
4995
Chris Lattner58092e32007-01-20 22:35:55 +00004996 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004997 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004998 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4999 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00005000 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00005001 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005002 // get address of 8 byte buffer
5003 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5004 // word offset constant for Hi/Lo address computation
5005 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5006 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005007 SDOperand Hi = StackSlot;
5008 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5009 if (TLI.isLittleEndian())
5010 std::swap(Hi, Lo);
5011
Chris Lattner22cde6a2006-01-28 08:25:58 +00005012 // if signed map to unsigned space
5013 SDOperand Op0Mapped;
5014 if (isSigned) {
5015 // constant used to invert sign bit (signed to unsigned mapping)
5016 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5017 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5018 } else {
5019 Op0Mapped = Op0;
5020 }
5021 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005022 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005023 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005024 // initial hi portion of constructed double
5025 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5026 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005027 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005028 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005029 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005030 // FP constant to bias correct the final result
5031 SDOperand Bias = DAG.getConstantFP(isSigned ?
5032 BitsToDouble(0x4330000080000000ULL)
5033 : BitsToDouble(0x4330000000000000ULL),
5034 MVT::f64);
5035 // subtract the bias
5036 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5037 // final result
5038 SDOperand Result;
5039 // handle final rounding
5040 if (DestVT == MVT::f64) {
5041 // do nothing
5042 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005043 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5044 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5045 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5046 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005047 }
5048 return Result;
5049 }
5050 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5051 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5052
5053 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5054 DAG.getConstant(0, Op0.getValueType()),
5055 ISD::SETLT);
5056 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5057 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5058 SignSet, Four, Zero);
5059
5060 // If the sign bit of the integer is set, the large number will be treated
5061 // as a negative number. To counteract this, the dynamic code adds an
5062 // offset depending on the data type.
5063 uint64_t FF;
5064 switch (Op0.getValueType()) {
5065 default: assert(0 && "Unsupported integer type!");
5066 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5067 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5068 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5069 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5070 }
5071 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005072 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005073
5074 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5075 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5076 SDOperand FudgeInReg;
5077 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005078 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005079 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005080 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005081 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005082 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005083 }
5084
5085 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5086}
5087
5088/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5089/// *INT_TO_FP operation of the specified operand when the target requests that
5090/// we promote it. At this point, we know that the result and operand types are
5091/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5092/// operation that takes a larger input.
5093SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5094 MVT::ValueType DestVT,
5095 bool isSigned) {
5096 // First step, figure out the appropriate *INT_TO_FP operation to use.
5097 MVT::ValueType NewInTy = LegalOp.getValueType();
5098
5099 unsigned OpToUse = 0;
5100
5101 // Scan for the appropriate larger type to use.
5102 while (1) {
5103 NewInTy = (MVT::ValueType)(NewInTy+1);
5104 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5105
5106 // If the target supports SINT_TO_FP of this type, use it.
5107 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5108 default: break;
5109 case TargetLowering::Legal:
5110 if (!TLI.isTypeLegal(NewInTy))
5111 break; // Can't use this datatype.
5112 // FALL THROUGH.
5113 case TargetLowering::Custom:
5114 OpToUse = ISD::SINT_TO_FP;
5115 break;
5116 }
5117 if (OpToUse) break;
5118 if (isSigned) continue;
5119
5120 // If the target supports UINT_TO_FP of this type, use it.
5121 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5122 default: break;
5123 case TargetLowering::Legal:
5124 if (!TLI.isTypeLegal(NewInTy))
5125 break; // Can't use this datatype.
5126 // FALL THROUGH.
5127 case TargetLowering::Custom:
5128 OpToUse = ISD::UINT_TO_FP;
5129 break;
5130 }
5131 if (OpToUse) break;
5132
5133 // Otherwise, try a larger type.
5134 }
5135
5136 // Okay, we found the operation and type to use. Zero extend our input to the
5137 // desired type then run the operation on it.
5138 return DAG.getNode(OpToUse, DestVT,
5139 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5140 NewInTy, LegalOp));
5141}
5142
5143/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5144/// FP_TO_*INT operation of the specified operand when the target requests that
5145/// we promote it. At this point, we know that the result and operand types are
5146/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5147/// operation that returns a larger result.
5148SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5149 MVT::ValueType DestVT,
5150 bool isSigned) {
5151 // First step, figure out the appropriate FP_TO*INT operation to use.
5152 MVT::ValueType NewOutTy = DestVT;
5153
5154 unsigned OpToUse = 0;
5155
5156 // Scan for the appropriate larger type to use.
5157 while (1) {
5158 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5159 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5160
5161 // If the target supports FP_TO_SINT returning this type, use it.
5162 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5163 default: break;
5164 case TargetLowering::Legal:
5165 if (!TLI.isTypeLegal(NewOutTy))
5166 break; // Can't use this datatype.
5167 // FALL THROUGH.
5168 case TargetLowering::Custom:
5169 OpToUse = ISD::FP_TO_SINT;
5170 break;
5171 }
5172 if (OpToUse) break;
5173
5174 // If the target supports FP_TO_UINT of this type, use it.
5175 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5176 default: break;
5177 case TargetLowering::Legal:
5178 if (!TLI.isTypeLegal(NewOutTy))
5179 break; // Can't use this datatype.
5180 // FALL THROUGH.
5181 case TargetLowering::Custom:
5182 OpToUse = ISD::FP_TO_UINT;
5183 break;
5184 }
5185 if (OpToUse) break;
5186
5187 // Otherwise, try a larger type.
5188 }
5189
Chris Lattner27a6c732007-11-24 07:07:01 +00005190
5191 // Okay, we found the operation and type to use.
5192 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5193
5194 // If the operation produces an invalid type, it must be custom lowered. Use
5195 // the target lowering hooks to expand it. Just keep the low part of the
5196 // expanded operation, we know that we're truncating anyway.
5197 if (getTypeAction(NewOutTy) == Expand) {
5198 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5199 assert(Operation.Val && "Didn't return anything");
5200 }
5201
5202 // Truncate the result of the extended FP_TO_*INT operation to the desired
5203 // size.
5204 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005205}
5206
5207/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5208///
5209SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5210 MVT::ValueType VT = Op.getValueType();
5211 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5212 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5213 switch (VT) {
5214 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5215 case MVT::i16:
5216 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5217 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5218 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5219 case MVT::i32:
5220 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5221 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5222 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5223 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5224 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5225 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5226 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5227 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5228 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5229 case MVT::i64:
5230 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5231 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5232 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5233 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5234 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5235 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5236 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5237 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5238 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5239 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5240 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5241 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5242 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5243 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5244 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5245 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5246 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5247 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5248 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5249 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5250 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5251 }
5252}
5253
5254/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5255///
5256SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5257 switch (Opc) {
5258 default: assert(0 && "Cannot expand this yet!");
5259 case ISD::CTPOP: {
5260 static const uint64_t mask[6] = {
5261 0x5555555555555555ULL, 0x3333333333333333ULL,
5262 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5263 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5264 };
5265 MVT::ValueType VT = Op.getValueType();
5266 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005267 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005268 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5269 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5270 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5271 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5272 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5273 DAG.getNode(ISD::AND, VT,
5274 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5275 }
5276 return Op;
5277 }
5278 case ISD::CTLZ: {
5279 // for now, we do this:
5280 // x = x | (x >> 1);
5281 // x = x | (x >> 2);
5282 // ...
5283 // x = x | (x >>16);
5284 // x = x | (x >>32); // for 64-bit input
5285 // return popcount(~x);
5286 //
5287 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5288 MVT::ValueType VT = Op.getValueType();
5289 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005290 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005291 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5292 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5293 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5294 }
5295 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5296 return DAG.getNode(ISD::CTPOP, VT, Op);
5297 }
5298 case ISD::CTTZ: {
5299 // for now, we use: { return popcount(~x & (x - 1)); }
5300 // unless the target has ctlz but not ctpop, in which case we use:
5301 // { return 32 - nlz(~x & (x-1)); }
5302 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5303 MVT::ValueType VT = Op.getValueType();
5304 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5305 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5306 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5307 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5308 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5309 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5310 TLI.isOperationLegal(ISD::CTLZ, VT))
5311 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005312 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005313 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5314 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5315 }
5316 }
5317}
Chris Lattnere34b3962005-01-19 04:19:40 +00005318
Chris Lattner3e928bb2005-01-07 07:47:09 +00005319/// ExpandOp - Expand the specified SDOperand into its two component pieces
5320/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5321/// LegalizeNodes map is filled in for any results that are not expanded, the
5322/// ExpandedNodes map is filled in for any results that are expanded, and the
5323/// Lo/Hi values are returned.
5324void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5325 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005326 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005327 SDNode *Node = Op.Val;
5328 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005329 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005330 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005331 "Cannot expand to FP value or to larger int value!");
5332
Chris Lattner6fdcb252005-09-02 20:32:45 +00005333 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005334 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005335 = ExpandedNodes.find(Op);
5336 if (I != ExpandedNodes.end()) {
5337 Lo = I->second.first;
5338 Hi = I->second.second;
5339 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005340 }
5341
Chris Lattner3e928bb2005-01-07 07:47:09 +00005342 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005343 case ISD::CopyFromReg:
5344 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005345 case ISD::FP_ROUND_INREG:
5346 if (VT == MVT::ppcf128 &&
5347 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5348 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005349 SDOperand SrcLo, SrcHi, Src;
5350 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5351 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5352 SDOperand Result = TLI.LowerOperation(
5353 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005354 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5355 Lo = Result.Val->getOperand(0);
5356 Hi = Result.Val->getOperand(1);
5357 break;
5358 }
5359 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005360 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005361#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005362 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005363#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005364 assert(0 && "Do not know how to expand this operator!");
5365 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005366 case ISD::EXTRACT_VECTOR_ELT:
5367 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5368 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5369 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5370 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005371 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005372 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005373 Lo = DAG.getNode(ISD::UNDEF, NVT);
5374 Hi = DAG.getNode(ISD::UNDEF, NVT);
5375 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005376 case ISD::Constant: {
5377 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5378 Lo = DAG.getConstant(Cst, NVT);
5379 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5380 break;
5381 }
Evan Cheng00495212006-12-12 21:32:44 +00005382 case ISD::ConstantFP: {
5383 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005384 if (CFP->getValueType(0) == MVT::ppcf128) {
5385 APInt api = CFP->getValueAPF().convertToAPInt();
5386 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5387 MVT::f64);
5388 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5389 MVT::f64);
5390 break;
5391 }
Evan Cheng279101e2006-12-12 22:19:28 +00005392 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005393 if (getTypeAction(Lo.getValueType()) == Expand)
5394 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005395 break;
5396 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005397 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005398 // Return the operands.
5399 Lo = Node->getOperand(0);
5400 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005401 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005402
5403 case ISD::MERGE_VALUES:
5404 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5405 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5406 Op.getValue(1).getValueType() == MVT::Other &&
5407 "unhandled MERGE_VALUES");
5408 ExpandOp(Op.getOperand(0), Lo, Hi);
5409 // Remember that we legalized the chain.
5410 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5411 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005412
5413 case ISD::SIGN_EXTEND_INREG:
5414 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005415 // sext_inreg the low part if needed.
5416 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5417
5418 // The high part gets the sign extension from the lo-part. This handles
5419 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005420 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5421 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5422 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005423 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005424
Nate Begemand88fc032006-01-14 03:14:10 +00005425 case ISD::BSWAP: {
5426 ExpandOp(Node->getOperand(0), Lo, Hi);
5427 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5428 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5429 Lo = TempLo;
5430 break;
5431 }
5432
Chris Lattneredb1add2005-05-11 04:51:16 +00005433 case ISD::CTPOP:
5434 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005435 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5436 DAG.getNode(ISD::CTPOP, NVT, Lo),
5437 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005438 Hi = DAG.getConstant(0, NVT);
5439 break;
5440
Chris Lattner39a8f332005-05-12 19:05:01 +00005441 case ISD::CTLZ: {
5442 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005443 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005444 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5445 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005446 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5447 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005448 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5449 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5450
5451 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5452 Hi = DAG.getConstant(0, NVT);
5453 break;
5454 }
5455
5456 case ISD::CTTZ: {
5457 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005458 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005459 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5460 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005461 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5462 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005463 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5464 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5465
5466 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5467 Hi = DAG.getConstant(0, NVT);
5468 break;
5469 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005470
Nate Begemanacc398c2006-01-25 18:21:52 +00005471 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005472 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5473 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005474 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5475 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5476
5477 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005478 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005479 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5480 if (!TLI.isLittleEndian())
5481 std::swap(Lo, Hi);
5482 break;
5483 }
5484
Chris Lattner3e928bb2005-01-07 07:47:09 +00005485 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005486 LoadSDNode *LD = cast<LoadSDNode>(Node);
5487 SDOperand Ch = LD->getChain(); // Legalize the chain.
5488 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5489 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005490 int SVOffset = LD->getSrcValueOffset();
5491 unsigned Alignment = LD->getAlignment();
5492 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005493
Evan Cheng466685d2006-10-09 20:57:25 +00005494 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005495 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5496 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005497 if (VT == MVT::f32 || VT == MVT::f64) {
5498 // f32->i32 or f64->i64 one to one expansion.
5499 // Remember that we legalized the chain.
5500 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005501 // Recursively expand the new load.
5502 if (getTypeAction(NVT) == Expand)
5503 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005504 break;
5505 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005506
Evan Cheng466685d2006-10-09 20:57:25 +00005507 // Increment the pointer to the other half.
5508 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5509 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5510 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005511 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005512 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005513 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5514 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005515
Evan Cheng466685d2006-10-09 20:57:25 +00005516 // Build a factor node to remember that this load is independent of the
5517 // other one.
5518 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5519 Hi.getValue(1));
5520
5521 // Remember that we legalized the chain.
5522 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5523 if (!TLI.isLittleEndian())
5524 std::swap(Lo, Hi);
5525 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005526 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005527
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005528 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5529 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005530 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5531 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005532 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005533 // Remember that we legalized the chain.
5534 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5535 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5536 break;
5537 }
Evan Cheng466685d2006-10-09 20:57:25 +00005538
5539 if (EVT == NVT)
5540 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005541 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005542 else
5543 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005544 SVOffset, EVT, isVolatile,
5545 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005546
5547 // Remember that we legalized the chain.
5548 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5549
5550 if (ExtType == ISD::SEXTLOAD) {
5551 // The high part is obtained by SRA'ing all but one of the bits of the
5552 // lo part.
5553 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5554 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5555 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5556 } else if (ExtType == ISD::ZEXTLOAD) {
5557 // The high part is just a zero.
5558 Hi = DAG.getConstant(0, NVT);
5559 } else /* if (ExtType == ISD::EXTLOAD) */ {
5560 // The high part is undefined.
5561 Hi = DAG.getNode(ISD::UNDEF, NVT);
5562 }
5563 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005564 break;
5565 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005566 case ISD::AND:
5567 case ISD::OR:
5568 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5569 SDOperand LL, LH, RL, RH;
5570 ExpandOp(Node->getOperand(0), LL, LH);
5571 ExpandOp(Node->getOperand(1), RL, RH);
5572 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5573 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5574 break;
5575 }
5576 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005577 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005578 ExpandOp(Node->getOperand(1), LL, LH);
5579 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005580 if (getTypeAction(NVT) == Expand)
5581 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005582 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005583 if (VT != MVT::f32)
5584 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005585 break;
5586 }
Nate Begeman9373a812005-08-10 20:51:12 +00005587 case ISD::SELECT_CC: {
5588 SDOperand TL, TH, FL, FH;
5589 ExpandOp(Node->getOperand(2), TL, TH);
5590 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005591 if (getTypeAction(NVT) == Expand)
5592 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005593 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5594 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005595 if (VT != MVT::f32)
5596 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5597 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005598 break;
5599 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005600 case ISD::ANY_EXTEND:
5601 // The low part is any extension of the input (which degenerates to a copy).
5602 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5603 // The high part is undefined.
5604 Hi = DAG.getNode(ISD::UNDEF, NVT);
5605 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005606 case ISD::SIGN_EXTEND: {
5607 // The low part is just a sign extension of the input (which degenerates to
5608 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005609 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005610
Chris Lattner3e928bb2005-01-07 07:47:09 +00005611 // The high part is obtained by SRA'ing all but one of the bits of the lo
5612 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005613 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005614 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5615 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005616 break;
5617 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005618 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005619 // The low part is just a zero extension of the input (which degenerates to
5620 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005621 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005622
Chris Lattner3e928bb2005-01-07 07:47:09 +00005623 // The high part is just a zero.
5624 Hi = DAG.getConstant(0, NVT);
5625 break;
Chris Lattner35481892005-12-23 00:16:34 +00005626
Chris Lattner4c948eb2007-02-13 23:55:16 +00005627 case ISD::TRUNCATE: {
5628 // The input value must be larger than this value. Expand *it*.
5629 SDOperand NewLo;
5630 ExpandOp(Node->getOperand(0), NewLo, Hi);
5631
5632 // The low part is now either the right size, or it is closer. If not the
5633 // right size, make an illegal truncate so we recursively expand it.
5634 if (NewLo.getValueType() != Node->getValueType(0))
5635 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5636 ExpandOp(NewLo, Lo, Hi);
5637 break;
5638 }
5639
Chris Lattner35481892005-12-23 00:16:34 +00005640 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005641 SDOperand Tmp;
5642 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5643 // If the target wants to, allow it to lower this itself.
5644 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5645 case Expand: assert(0 && "cannot expand FP!");
5646 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5647 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5648 }
5649 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5650 }
5651
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005652 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005653 if (VT == MVT::f32 || VT == MVT::f64) {
5654 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005655 if (getTypeAction(NVT) == Expand)
5656 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005657 break;
5658 }
5659
5660 // If source operand will be expanded to the same type as VT, i.e.
5661 // i64 <- f64, i32 <- f32, expand the source operand instead.
5662 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5663 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5664 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005665 break;
5666 }
5667
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005668 // Turn this into a load/store pair by default.
5669 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005670 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005671
Chris Lattner35481892005-12-23 00:16:34 +00005672 ExpandOp(Tmp, Lo, Hi);
5673 break;
5674 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005675
Chris Lattner27a6c732007-11-24 07:07:01 +00005676 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005677 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5678 TargetLowering::Custom &&
5679 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005680 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5681 assert(Tmp.Val && "Node must be custom expanded!");
5682 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005683 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005684 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005685 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005686 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005687
Chris Lattner4e6c7462005-01-08 19:27:05 +00005688 // These operators cannot be expanded directly, emit them as calls to
5689 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005690 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005691 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005692 SDOperand Op;
5693 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5694 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005695 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5696 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005697 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005698
Chris Lattnerf20d1832005-07-30 01:40:57 +00005699 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5700
Chris Lattner80a3e942005-07-29 00:33:32 +00005701 // Now that the custom expander is done, expand the result, which is still
5702 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005703 if (Op.Val) {
5704 ExpandOp(Op, Lo, Hi);
5705 break;
5706 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005707 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005708
Dale Johannesen161e8972007-10-05 20:04:43 +00005709 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005710 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005711 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005712 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005713 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005714 else if (Node->getOperand(0).getValueType() == MVT::f80)
5715 LC = RTLIB::FPTOSINT_F80_I64;
5716 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5717 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005718 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5719 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005720 break;
Evan Cheng56966222007-01-12 02:11:51 +00005721 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005722
Evan Cheng56966222007-01-12 02:11:51 +00005723 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005724 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005725 SDOperand Op;
5726 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5727 case Expand: assert(0 && "cannot expand FP!");
5728 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5729 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5730 }
5731
5732 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5733
5734 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005735 if (Op.Val) {
5736 ExpandOp(Op, Lo, Hi);
5737 break;
5738 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005739 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005740
Evan Chengdaccea182007-10-05 01:09:32 +00005741 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005742 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005743 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005744 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005745 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005746 else if (Node->getOperand(0).getValueType() == MVT::f80)
5747 LC = RTLIB::FPTOUINT_F80_I64;
5748 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5749 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005750 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5751 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005752 break;
Evan Cheng56966222007-01-12 02:11:51 +00005753 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005754
Evan Cheng05a2d562006-01-09 18:31:59 +00005755 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005756 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005757 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005758 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005759 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005760 Op = TLI.LowerOperation(Op, DAG);
5761 if (Op.Val) {
5762 // Now that the custom expander is done, expand the result, which is
5763 // still VT.
5764 ExpandOp(Op, Lo, Hi);
5765 break;
5766 }
5767 }
5768
Chris Lattner79980b02006-09-13 03:50:39 +00005769 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5770 // this X << 1 as X+X.
5771 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5772 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5773 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5774 SDOperand LoOps[2], HiOps[3];
5775 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5776 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5777 LoOps[1] = LoOps[0];
5778 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5779
5780 HiOps[1] = HiOps[0];
5781 HiOps[2] = Lo.getValue(1);
5782 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5783 break;
5784 }
5785 }
5786
Chris Lattnere34b3962005-01-19 04:19:40 +00005787 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005788 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005789 break;
Chris Lattner47599822005-04-02 03:38:53 +00005790
5791 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005792 TargetLowering::LegalizeAction Action =
5793 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5794 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5795 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005796 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005797 break;
5798 }
5799
Chris Lattnere34b3962005-01-19 04:19:40 +00005800 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005801 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5802 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005803 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005804 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005805
Evan Cheng05a2d562006-01-09 18:31:59 +00005806 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005807 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005808 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005809 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005810 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005811 Op = TLI.LowerOperation(Op, DAG);
5812 if (Op.Val) {
5813 // Now that the custom expander is done, expand the result, which is
5814 // still VT.
5815 ExpandOp(Op, Lo, Hi);
5816 break;
5817 }
5818 }
5819
Chris Lattnere34b3962005-01-19 04:19:40 +00005820 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005821 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005822 break;
Chris Lattner47599822005-04-02 03:38:53 +00005823
5824 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005825 TargetLowering::LegalizeAction Action =
5826 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5827 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5828 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005829 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005830 break;
5831 }
5832
Chris Lattnere34b3962005-01-19 04:19:40 +00005833 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005834 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5835 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005836 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005837 }
5838
5839 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005840 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005841 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005842 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005843 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005844 Op = TLI.LowerOperation(Op, DAG);
5845 if (Op.Val) {
5846 // Now that the custom expander is done, expand the result, which is
5847 // still VT.
5848 ExpandOp(Op, Lo, Hi);
5849 break;
5850 }
5851 }
5852
Chris Lattnere34b3962005-01-19 04:19:40 +00005853 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005854 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005855 break;
Chris Lattner47599822005-04-02 03:38:53 +00005856
5857 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005858 TargetLowering::LegalizeAction Action =
5859 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5860 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5861 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005862 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005863 break;
5864 }
5865
Chris Lattnere34b3962005-01-19 04:19:40 +00005866 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005867 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5868 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005869 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005870 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005871
Misha Brukmanedf128a2005-04-21 22:36:52 +00005872 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005873 case ISD::SUB: {
5874 // If the target wants to custom expand this, let them.
5875 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5876 TargetLowering::Custom) {
5877 Op = TLI.LowerOperation(Op, DAG);
5878 if (Op.Val) {
5879 ExpandOp(Op, Lo, Hi);
5880 break;
5881 }
5882 }
5883
5884 // Expand the subcomponents.
5885 SDOperand LHSL, LHSH, RHSL, RHSH;
5886 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5887 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005888 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5889 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005890 LoOps[0] = LHSL;
5891 LoOps[1] = RHSL;
5892 HiOps[0] = LHSH;
5893 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005894 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005895 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005896 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005897 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005898 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005899 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005900 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005901 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005902 }
Chris Lattner84f67882005-01-20 18:52:28 +00005903 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005904 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005905
5906 case ISD::ADDC:
5907 case ISD::SUBC: {
5908 // Expand the subcomponents.
5909 SDOperand LHSL, LHSH, RHSL, RHSH;
5910 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5911 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5912 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5913 SDOperand LoOps[2] = { LHSL, RHSL };
5914 SDOperand HiOps[3] = { LHSH, RHSH };
5915
5916 if (Node->getOpcode() == ISD::ADDC) {
5917 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5918 HiOps[2] = Lo.getValue(1);
5919 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5920 } else {
5921 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5922 HiOps[2] = Lo.getValue(1);
5923 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5924 }
5925 // Remember that we legalized the flag.
5926 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5927 break;
5928 }
5929 case ISD::ADDE:
5930 case ISD::SUBE: {
5931 // Expand the subcomponents.
5932 SDOperand LHSL, LHSH, RHSL, RHSH;
5933 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5934 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5935 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5936 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5937 SDOperand HiOps[3] = { LHSH, RHSH };
5938
5939 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5940 HiOps[2] = Lo.getValue(1);
5941 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5942
5943 // Remember that we legalized the flag.
5944 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5945 break;
5946 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005947 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005948 // If the target wants to custom expand this, let them.
5949 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005950 SDOperand New = TLI.LowerOperation(Op, DAG);
5951 if (New.Val) {
5952 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005953 break;
5954 }
5955 }
5956
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005957 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5958 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00005959 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5960 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5961 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005962 SDOperand LL, LH, RL, RH;
5963 ExpandOp(Node->getOperand(0), LL, LH);
5964 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00005965 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5966 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5967 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5968 // FIXME: generalize this to handle other bit sizes
5969 if (LHSSB == 32 && RHSSB == 32 &&
5970 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5971 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5972 // The inputs are both zero-extended.
5973 if (HasUMUL_LOHI) {
5974 // We can emit a umul_lohi.
5975 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5976 Hi = SDOperand(Lo.Val, 1);
5977 break;
5978 }
5979 if (HasMULHU) {
5980 // We can emit a mulhu+mul.
5981 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5982 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5983 break;
5984 }
Dan Gohman525178c2007-10-08 18:33:35 +00005985 }
5986 if (LHSSB > BitSize && RHSSB > BitSize) {
5987 // The input values are both sign-extended.
5988 if (HasSMUL_LOHI) {
5989 // We can emit a smul_lohi.
5990 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5991 Hi = SDOperand(Lo.Val, 1);
5992 break;
5993 }
5994 if (HasMULHS) {
5995 // We can emit a mulhs+mul.
5996 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5997 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5998 break;
5999 }
6000 }
6001 if (HasUMUL_LOHI) {
6002 // Lo,Hi = umul LHS, RHS.
6003 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6004 DAG.getVTList(NVT, NVT), LL, RL);
6005 Lo = UMulLOHI;
6006 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006007 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6008 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6009 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6010 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006011 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006012 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006013 if (HasMULHU) {
6014 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6015 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6016 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6017 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6018 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6019 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6020 break;
6021 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006022 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006023
Dan Gohman525178c2007-10-08 18:33:35 +00006024 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006025 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6026 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006027 break;
6028 }
Evan Cheng56966222007-01-12 02:11:51 +00006029 case ISD::SDIV:
6030 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6031 break;
6032 case ISD::UDIV:
6033 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6034 break;
6035 case ISD::SREM:
6036 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6037 break;
6038 case ISD::UREM:
6039 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6040 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006041
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006042 case ISD::FADD:
Dale Johannesen161e8972007-10-05 20:04:43 +00006043 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
6044 VT == MVT::f64 ? RTLIB::ADD_F64 :
6045 VT == MVT::ppcf128 ?
6046 RTLIB::ADD_PPCF128 :
6047 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006048 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006049 break;
6050 case ISD::FSUB:
Dale Johannesen161e8972007-10-05 20:04:43 +00006051 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
6052 VT == MVT::f64 ? RTLIB::SUB_F64 :
6053 VT == MVT::ppcf128 ?
6054 RTLIB::SUB_PPCF128 :
6055 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006056 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006057 break;
6058 case ISD::FMUL:
Dale Johannesen161e8972007-10-05 20:04:43 +00006059 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
6060 VT == MVT::f64 ? RTLIB::MUL_F64 :
6061 VT == MVT::ppcf128 ?
6062 RTLIB::MUL_PPCF128 :
6063 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006064 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006065 break;
6066 case ISD::FDIV:
Dale Johannesen161e8972007-10-05 20:04:43 +00006067 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
6068 VT == MVT::f64 ? RTLIB::DIV_F64 :
6069 VT == MVT::ppcf128 ?
6070 RTLIB::DIV_PPCF128 :
6071 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006072 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006073 break;
6074 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006075 if (VT == MVT::ppcf128) {
6076 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6077 Node->getOperand(0).getValueType()==MVT::f64);
6078 const uint64_t zero = 0;
6079 if (Node->getOperand(0).getValueType()==MVT::f32)
6080 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6081 else
6082 Hi = Node->getOperand(0);
6083 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6084 break;
6085 }
Evan Cheng56966222007-01-12 02:11:51 +00006086 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006087 break;
6088 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006089 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006090 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006091 case ISD::FPOWI:
Dale Johannesen317096a2007-09-28 01:08:20 +00006092 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6093 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006094 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6095 (VT == MVT::ppcf128) ?
6096 RTLIB::POWI_PPCF128 :
6097 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006098 Node, false, Hi);
6099 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006100 case ISD::FSQRT:
6101 case ISD::FSIN:
6102 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006103 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006104 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006105 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00006106 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006107 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6108 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6109 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6110 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00006111 break;
6112 case ISD::FSIN:
6113 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6114 break;
6115 case ISD::FCOS:
6116 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6117 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006118 default: assert(0 && "Unreachable!");
6119 }
Evan Cheng56966222007-01-12 02:11:51 +00006120 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006121 break;
6122 }
Evan Cheng966bf242006-12-16 00:52:40 +00006123 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006124 if (VT == MVT::ppcf128) {
6125 SDOperand Tmp;
6126 ExpandOp(Node->getOperand(0), Lo, Tmp);
6127 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6128 // lo = hi==fabs(hi) ? lo : -lo;
6129 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6130 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6131 DAG.getCondCode(ISD::SETEQ));
6132 break;
6133 }
Evan Cheng966bf242006-12-16 00:52:40 +00006134 SDOperand Mask = (VT == MVT::f64)
6135 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6136 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6137 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6138 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6139 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6140 if (getTypeAction(NVT) == Expand)
6141 ExpandOp(Lo, Lo, Hi);
6142 break;
6143 }
6144 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006145 if (VT == MVT::ppcf128) {
6146 ExpandOp(Node->getOperand(0), Lo, Hi);
6147 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6148 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6149 break;
6150 }
Evan Cheng966bf242006-12-16 00:52:40 +00006151 SDOperand Mask = (VT == MVT::f64)
6152 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6153 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6154 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6155 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6156 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6157 if (getTypeAction(NVT) == Expand)
6158 ExpandOp(Lo, Lo, Hi);
6159 break;
6160 }
Evan Cheng912095b2007-01-04 21:56:39 +00006161 case ISD::FCOPYSIGN: {
6162 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6163 if (getTypeAction(NVT) == Expand)
6164 ExpandOp(Lo, Lo, Hi);
6165 break;
6166 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006167 case ISD::SINT_TO_FP:
6168 case ISD::UINT_TO_FP: {
6169 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6170 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006171 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006172 static uint64_t zero = 0;
6173 if (isSigned) {
6174 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6175 Node->getOperand(0)));
6176 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6177 } else {
6178 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6179 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6180 Node->getOperand(0)));
6181 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6182 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006183 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006184 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6185 DAG.getConstant(0, MVT::i32),
6186 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6187 DAG.getConstantFP(
6188 APFloat(APInt(128, 2, TwoE32)),
6189 MVT::ppcf128)),
6190 Hi,
6191 DAG.getCondCode(ISD::SETLT)),
6192 Lo, Hi);
6193 }
6194 break;
6195 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006196 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6197 // si64->ppcf128 done by libcall, below
6198 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6199 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6200 Lo, Hi);
6201 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6202 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6203 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6204 DAG.getConstant(0, MVT::i64),
6205 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6206 DAG.getConstantFP(
6207 APFloat(APInt(128, 2, TwoE64)),
6208 MVT::ppcf128)),
6209 Hi,
6210 DAG.getCondCode(ISD::SETLT)),
6211 Lo, Hi);
6212 break;
6213 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006214 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006215 if (Node->getOperand(0).getValueType() == MVT::i64) {
6216 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006217 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006218 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006219 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006220 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006221 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006222 LC = RTLIB::SINTTOFP_I64_F80;
6223 }
6224 else if (VT == MVT::ppcf128) {
6225 assert(isSigned);
6226 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006227 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006228 } else {
6229 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006230 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006231 else
Evan Cheng56966222007-01-12 02:11:51 +00006232 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006233 }
6234
6235 // Promote the operand if needed.
6236 if (getTypeAction(SrcVT) == Promote) {
6237 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6238 Tmp = isSigned
6239 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6240 DAG.getValueType(SrcVT))
6241 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6242 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6243 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006244
6245 const char *LibCall = TLI.getLibcallName(LC);
6246 if (LibCall)
6247 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6248 else {
6249 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6250 Node->getOperand(0));
6251 if (getTypeAction(Lo.getValueType()) == Expand)
6252 ExpandOp(Lo, Lo, Hi);
6253 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006254 break;
6255 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006256 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006257
Chris Lattner83397362005-12-21 18:02:52 +00006258 // Make sure the resultant values have been legalized themselves, unless this
6259 // is a type that requires multi-step expansion.
6260 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6261 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006262 if (Hi.Val)
6263 // Don't legalize the high part if it is expanded to a single node.
6264 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006265 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006266
6267 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006268 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006269 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006270}
6271
Dan Gohman7f321562007-06-25 16:23:39 +00006272/// SplitVectorOp - Given an operand of vector type, break it down into
6273/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006274void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6275 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006276 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006277 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006278 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006279 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006280
Dan Gohmane40c7b02007-09-24 15:54:53 +00006281 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006282
6283 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6284 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6285
6286 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6287 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6288
Chris Lattnerc7029802006-03-18 01:44:44 +00006289 // See if we already split it.
6290 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6291 = SplitNodes.find(Op);
6292 if (I != SplitNodes.end()) {
6293 Lo = I->second.first;
6294 Hi = I->second.second;
6295 return;
6296 }
6297
6298 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006299 default:
6300#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006301 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006302#endif
6303 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006304 case ISD::UNDEF:
6305 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6306 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6307 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006308 case ISD::BUILD_PAIR:
6309 Lo = Node->getOperand(0);
6310 Hi = Node->getOperand(1);
6311 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006312 case ISD::INSERT_VECTOR_ELT: {
6313 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6314 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6315 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006316 if (Index < NewNumElts_Lo)
6317 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006318 DAG.getConstant(Index, TLI.getPointerTy()));
6319 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006320 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6321 DAG.getConstant(Index - NewNumElts_Lo,
6322 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006323 break;
6324 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006325 case ISD::VECTOR_SHUFFLE: {
6326 // Build the low part.
6327 SDOperand Mask = Node->getOperand(2);
6328 SmallVector<SDOperand, 8> Ops;
6329 MVT::ValueType PtrVT = TLI.getPointerTy();
6330
6331 // Insert all of the elements from the input that are needed. We use
6332 // buildvector of extractelement here because the input vectors will have
6333 // to be legalized, so this makes the code simpler.
6334 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6335 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6336 SDOperand InVec = Node->getOperand(0);
6337 if (Idx >= NumElements) {
6338 InVec = Node->getOperand(1);
6339 Idx -= NumElements;
6340 }
6341 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6342 DAG.getConstant(Idx, PtrVT)));
6343 }
6344 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6345 Ops.clear();
6346
6347 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6348 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6349 SDOperand InVec = Node->getOperand(0);
6350 if (Idx >= NumElements) {
6351 InVec = Node->getOperand(1);
6352 Idx -= NumElements;
6353 }
6354 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6355 DAG.getConstant(Idx, PtrVT)));
6356 }
6357 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6358 break;
6359 }
Dan Gohman7f321562007-06-25 16:23:39 +00006360 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006361 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006362 Node->op_begin()+NewNumElts_Lo);
6363 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006364
Nate Begeman5db1afb2007-11-15 21:15:26 +00006365 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006366 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006367 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006368 break;
6369 }
Dan Gohman7f321562007-06-25 16:23:39 +00006370 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006371 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006372 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6373 if (NewNumSubvectors == 1) {
6374 Lo = Node->getOperand(0);
6375 Hi = Node->getOperand(1);
6376 } else {
6377 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6378 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006379 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006380
Dan Gohman7f321562007-06-25 16:23:39 +00006381 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6382 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006383 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006384 }
Dan Gohman65956352007-06-13 15:12:02 +00006385 break;
6386 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006387 case ISD::SELECT: {
6388 SDOperand Cond = Node->getOperand(0);
6389
6390 SDOperand LL, LH, RL, RH;
6391 SplitVectorOp(Node->getOperand(1), LL, LH);
6392 SplitVectorOp(Node->getOperand(2), RL, RH);
6393
6394 if (MVT::isVector(Cond.getValueType())) {
6395 // Handle a vector merge.
6396 SDOperand CL, CH;
6397 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006398 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6399 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006400 } else {
6401 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006402 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6403 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006404 }
6405 break;
6406 }
Dan Gohman7f321562007-06-25 16:23:39 +00006407 case ISD::ADD:
6408 case ISD::SUB:
6409 case ISD::MUL:
6410 case ISD::FADD:
6411 case ISD::FSUB:
6412 case ISD::FMUL:
6413 case ISD::SDIV:
6414 case ISD::UDIV:
6415 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006416 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006417 case ISD::AND:
6418 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006419 case ISD::XOR:
6420 case ISD::UREM:
6421 case ISD::SREM:
6422 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006423 SDOperand LL, LH, RL, RH;
6424 SplitVectorOp(Node->getOperand(0), LL, LH);
6425 SplitVectorOp(Node->getOperand(1), RL, RH);
6426
Nate Begeman5db1afb2007-11-15 21:15:26 +00006427 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6428 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006429 break;
6430 }
Dan Gohman82669522007-10-11 23:57:53 +00006431 case ISD::FPOWI: {
6432 SDOperand L, H;
6433 SplitVectorOp(Node->getOperand(0), L, H);
6434
Nate Begeman5db1afb2007-11-15 21:15:26 +00006435 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6436 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006437 break;
6438 }
6439 case ISD::CTTZ:
6440 case ISD::CTLZ:
6441 case ISD::CTPOP:
6442 case ISD::FNEG:
6443 case ISD::FABS:
6444 case ISD::FSQRT:
6445 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006446 case ISD::FCOS:
6447 case ISD::FP_TO_SINT:
6448 case ISD::FP_TO_UINT:
6449 case ISD::SINT_TO_FP:
6450 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006451 SDOperand L, H;
6452 SplitVectorOp(Node->getOperand(0), L, H);
6453
Nate Begeman5db1afb2007-11-15 21:15:26 +00006454 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6455 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006456 break;
6457 }
Dan Gohman7f321562007-06-25 16:23:39 +00006458 case ISD::LOAD: {
6459 LoadSDNode *LD = cast<LoadSDNode>(Node);
6460 SDOperand Ch = LD->getChain();
6461 SDOperand Ptr = LD->getBasePtr();
6462 const Value *SV = LD->getSrcValue();
6463 int SVOffset = LD->getSrcValueOffset();
6464 unsigned Alignment = LD->getAlignment();
6465 bool isVolatile = LD->isVolatile();
6466
Nate Begeman5db1afb2007-11-15 21:15:26 +00006467 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6468 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006469 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6470 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006471 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006472 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006473 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006474
6475 // Build a factor node to remember that this load is independent of the
6476 // other one.
6477 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6478 Hi.getValue(1));
6479
6480 // Remember that we legalized the chain.
6481 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006482 break;
6483 }
Dan Gohman7f321562007-06-25 16:23:39 +00006484 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006485 // We know the result is a vector. The input may be either a vector or a
6486 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006487 SDOperand InOp = Node->getOperand(0);
6488 if (!MVT::isVector(InOp.getValueType()) ||
6489 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6490 // The input is a scalar or single-element vector.
6491 // Lower to a store/load so that it can be split.
6492 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006493 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006494
Evan Cheng786225a2006-10-05 23:01:46 +00006495 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006496 InOp, Ptr, NULL, 0);
6497 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006498 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006499 // Split the vector and convert each of the pieces now.
6500 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006501 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6502 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006503 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006504 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006505 }
6506
6507 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006508 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006509 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006510 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006511}
6512
6513
Dan Gohman89b20c02007-06-27 14:06:22 +00006514/// ScalarizeVectorOp - Given an operand of single-element vector type
6515/// (e.g. v1f32), convert it into the equivalent operation that returns a
6516/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006517SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6518 assert(MVT::isVector(Op.getValueType()) &&
6519 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006520 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006521 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6522 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006523
Dan Gohman7f321562007-06-25 16:23:39 +00006524 // See if we already scalarized it.
6525 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6526 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006527
6528 SDOperand Result;
6529 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006530 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006531#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006532 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006533#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006534 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6535 case ISD::ADD:
6536 case ISD::FADD:
6537 case ISD::SUB:
6538 case ISD::FSUB:
6539 case ISD::MUL:
6540 case ISD::FMUL:
6541 case ISD::SDIV:
6542 case ISD::UDIV:
6543 case ISD::FDIV:
6544 case ISD::SREM:
6545 case ISD::UREM:
6546 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006547 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006548 case ISD::AND:
6549 case ISD::OR:
6550 case ISD::XOR:
6551 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006552 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006553 ScalarizeVectorOp(Node->getOperand(0)),
6554 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006555 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006556 case ISD::FNEG:
6557 case ISD::FABS:
6558 case ISD::FSQRT:
6559 case ISD::FSIN:
6560 case ISD::FCOS:
6561 Result = DAG.getNode(Node->getOpcode(),
6562 NewVT,
6563 ScalarizeVectorOp(Node->getOperand(0)));
6564 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006565 case ISD::FPOWI:
6566 Result = DAG.getNode(Node->getOpcode(),
6567 NewVT,
6568 ScalarizeVectorOp(Node->getOperand(0)),
6569 Node->getOperand(1));
6570 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006571 case ISD::LOAD: {
6572 LoadSDNode *LD = cast<LoadSDNode>(Node);
6573 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6574 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006575
Dan Gohman7f321562007-06-25 16:23:39 +00006576 const Value *SV = LD->getSrcValue();
6577 int SVOffset = LD->getSrcValueOffset();
6578 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6579 LD->isVolatile(), LD->getAlignment());
6580
Chris Lattnerc7029802006-03-18 01:44:44 +00006581 // Remember that we legalized the chain.
6582 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6583 break;
6584 }
Dan Gohman7f321562007-06-25 16:23:39 +00006585 case ISD::BUILD_VECTOR:
6586 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006587 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006588 case ISD::INSERT_VECTOR_ELT:
6589 // Returning the inserted scalar element.
6590 Result = Node->getOperand(1);
6591 break;
6592 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006593 assert(Node->getOperand(0).getValueType() == NewVT &&
6594 "Concat of non-legal vectors not yet supported!");
6595 Result = Node->getOperand(0);
6596 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006597 case ISD::VECTOR_SHUFFLE: {
6598 // Figure out if the scalar is the LHS or RHS and return it.
6599 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6600 if (cast<ConstantSDNode>(EltNum)->getValue())
6601 Result = ScalarizeVectorOp(Node->getOperand(1));
6602 else
6603 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006604 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006605 }
6606 case ISD::EXTRACT_SUBVECTOR:
6607 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006608 assert(Result.getValueType() == NewVT);
6609 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006610 case ISD::BIT_CONVERT:
6611 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006612 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006613 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006614 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006615 ScalarizeVectorOp(Op.getOperand(1)),
6616 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006617 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006618 }
6619
6620 if (TLI.isTypeLegal(NewVT))
6621 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006622 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6623 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006624 return Result;
6625}
6626
Chris Lattner3e928bb2005-01-07 07:47:09 +00006627
6628// SelectionDAG::Legalize - This is the entry point for the file.
6629//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006630void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006631 if (ViewLegalizeDAGs) viewGraph();
6632
Chris Lattner3e928bb2005-01-07 07:47:09 +00006633 /// run - This is the main entry point to this class.
6634 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006635 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006636}
6637