blob: 5f41452055c17e6e14002b9a81dbd955a160e399 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000031#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000032using namespace llvm;
33
Chris Lattneref598052006-04-02 03:07:27 +000034#ifndef NDEBUG
35static cl::opt<bool>
36ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
37 cl::desc("Pop up a window to show dags before legalize"));
38#else
39static const bool ViewLegalizeDAGs = 0;
40#endif
41
Chris Lattnered39c862007-02-04 00:50:02 +000042namespace llvm {
43template<>
44struct DenseMapKeyInfo<SDOperand> {
45 static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); }
46 static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);}
47 static unsigned getHashValue(const SDOperand &Val) {
48 return DenseMapKeyInfo<void*>::getHashValue(Val.Val) + Val.ResNo;
49 }
50 static bool isPod() { return true; }
51};
52}
53
Chris Lattnerdc750592005-01-07 07:47:09 +000054//===----------------------------------------------------------------------===//
55/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
56/// hacks on it until the target machine can handle it. This involves
57/// eliminating value sizes the machine cannot handle (promoting small sizes to
58/// large sizes or splitting up large values into small values) as well as
59/// eliminating operations the machine cannot handle.
60///
61/// This code also does a small amount of optimization and recognition of idioms
62/// as part of its processing. For example, if a target does not support a
63/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
64/// will attempt merge setcc and brc instructions into brcc's.
65///
66namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000067class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000068 TargetLowering &TLI;
69 SelectionDAG &DAG;
70
Chris Lattner462505f2006-02-13 09:18:02 +000071 // Libcall insertion helpers.
72
73 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
74 /// legalized. We use this to ensure that calls are properly serialized
75 /// against each other, including inserted libcalls.
76 SDOperand LastCALLSEQ_END;
77
78 /// IsLegalizingCall - This member is used *only* for purposes of providing
79 /// helpful assertions that a libcall isn't created while another call is
80 /// being legalized (which could lead to non-serialized call sequences).
81 bool IsLegalizingCall;
82
Chris Lattnerdc750592005-01-07 07:47:09 +000083 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000084 Legal, // The target natively supports this operation.
85 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000086 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000087 };
Chris Lattner462505f2006-02-13 09:18:02 +000088
Chris Lattnerdc750592005-01-07 07:47:09 +000089 /// ValueTypeActions - This is a bitvector that contains two bits for each
90 /// value type, where the two bits correspond to the LegalizeAction enum.
91 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000092 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000093
Chris Lattnerdc750592005-01-07 07:47:09 +000094 /// LegalizedNodes - For nodes that are of legal width, and that have more
95 /// than one use, this map indicates what regularized operand to use. This
96 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000097 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000098
Chris Lattner1f2c9d82005-01-15 05:21:40 +000099 /// PromotedNodes - For nodes that are below legal width, and that have more
100 /// than one use, this map indicates what promoted value to use. This allows
101 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000102 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000103
Chris Lattner32206f52006-03-18 01:44:44 +0000104 /// ExpandedNodes - For nodes that need to be expanded this map indicates
105 /// which which operands are the expanded version of the input. This allows
106 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000107 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +0000108
Chris Lattner32206f52006-03-18 01:44:44 +0000109 /// SplitNodes - For vector nodes that need to be split, this map indicates
110 /// which which operands are the split version of the input. This allows us
111 /// to avoid splitting the same node more than once.
112 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
113
Dan Gohmana8665142007-06-25 16:23:39 +0000114 /// ScalarizedNodes - For nodes that need to be converted from vector types to
115 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000116 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000117 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000118
Chris Lattnerea4ca942005-01-07 22:28:47 +0000119 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000120 LegalizedNodes.insert(std::make_pair(From, To));
121 // If someone requests legalization of the new node, return itself.
122 if (From != To)
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000124 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000125 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000126 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000127 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000128 // If someone requests legalization of the new node, return itself.
129 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000130 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000131
Chris Lattnerdc750592005-01-07 07:47:09 +0000132public:
133
Chris Lattner4add7e32005-01-23 04:42:50 +0000134 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000135
Chris Lattnerdc750592005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
139 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
145 bool isTypeLegal(MVT::ValueType VT) const {
146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattnerdc750592005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000151private:
Dan Gohmana8665142007-06-25 16:23:39 +0000152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000153 /// appropriate for its type.
154 void HandleOp(SDOperand Op);
155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000159 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000160
161 /// PromoteOp - Given an operation that produces a value in an invalid type,
162 /// promote it to compute the value into a larger type. The produced value
163 /// will have the correct bits for the low portion of the register, but no
164 /// guarantee is made about the top bits: it may be zero, sign-extended, or
165 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000166 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000167
Chris Lattner32206f52006-03-18 01:44:44 +0000168 /// ExpandOp - Expand the specified SDOperand into its two component pieces
169 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
170 /// the LegalizeNodes map is filled in for any results that are not expanded,
171 /// the ExpandedNodes map is filled in for any results that are expanded, and
172 /// the Lo/Hi values are returned. This applies to integer types and Vector
173 /// types.
174 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
Dan Gohmana8665142007-06-25 16:23:39 +0000176 /// SplitVectorOp - Given an operand of vector type, break it down into
177 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000178 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
179
Dan Gohmana8665142007-06-25 16:23:39 +0000180 /// ScalarizeVectorOp - Given an operand of vector type, convert it into the
181 /// equivalent operation that returns a scalar value.
182 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000183
Chris Lattner6be79822006-04-04 17:23:26 +0000184 /// isShuffleLegal - Return true if a vector shuffle is legal with the
185 /// specified mask and type. Targets can specify exactly which masks they
186 /// support and the code generator is tasked with not creating illegal masks.
187 ///
188 /// Note that this will also return true for shuffles that are promoted to a
189 /// different type.
190 ///
191 /// If this is a legal shuffle, this method returns the (possibly promoted)
192 /// build_vector Mask. If it's not a legal shuffle, it returns null.
193 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
194
Chris Lattner4488f0c2006-07-26 23:55:56 +0000195 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000196 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000197
Nate Begeman7e7f4392006-02-01 07:19:44 +0000198 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
199
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000200 SDOperand CreateStackTemporary(MVT::ValueType VT);
201
Reid Spencere63b6512006-12-31 05:55:36 +0000202 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000203 SDOperand &Hi);
204 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
205 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000206
Chris Lattner36e663d2005-12-23 00:16:34 +0000207 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000208 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000209 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000210 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
211 SDOperand LegalOp,
212 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000213 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
214 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000215 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
216 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000217
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000218 SDOperand ExpandBSWAP(SDOperand Op);
219 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000220 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
221 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000222 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
223 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000224
Dan Gohmana8665142007-06-25 16:23:39 +0000225 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000226 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000227
Chris Lattnerdc750592005-01-07 07:47:09 +0000228 SDOperand getIntPtrConstant(uint64_t Val) {
229 return DAG.getConstant(Val, TLI.getPointerTy());
230 }
231};
232}
233
Chris Lattner6be79822006-04-04 17:23:26 +0000234/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
235/// specified mask and type. Targets can specify exactly which masks they
236/// support and the code generator is tasked with not creating illegal masks.
237///
238/// Note that this will also return true for shuffles that are promoted to a
239/// different type.
240SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
241 SDOperand Mask) const {
242 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
243 default: return 0;
244 case TargetLowering::Legal:
245 case TargetLowering::Custom:
246 break;
247 case TargetLowering::Promote: {
248 // If this is promoted to a different type, convert the shuffle mask and
249 // ask if it is legal in the promoted type!
250 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
251
252 // If we changed # elements, change the shuffle mask.
253 unsigned NumEltsGrowth =
254 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
255 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
256 if (NumEltsGrowth > 1) {
257 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000258 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000259 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
260 SDOperand InOp = Mask.getOperand(i);
261 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
262 if (InOp.getOpcode() == ISD::UNDEF)
263 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
264 else {
265 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
266 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
267 }
268 }
269 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000270 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000271 }
272 VT = NVT;
273 break;
274 }
275 }
276 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
277}
278
Chris Lattner4add7e32005-01-23 04:42:50 +0000279SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
280 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
281 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000282 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000283 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000284}
285
Chris Lattnere31adc82007-06-18 21:28:10 +0000286/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
287/// contains all of a nodes operands before it contains the node.
288static void ComputeTopDownOrdering(SelectionDAG &DAG,
289 SmallVector<SDNode*, 64> &Order) {
290
291 DenseMap<SDNode*, unsigned> Visited;
292 std::vector<SDNode*> Worklist;
293 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000294
Chris Lattnere31adc82007-06-18 21:28:10 +0000295 // Compute ordering from all of the leaves in the graphs, those (like the
296 // entry node) that have no operands.
297 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
298 E = DAG.allnodes_end(); I != E; ++I) {
299 if (I->getNumOperands() == 0) {
300 Visited[I] = 0 - 1U;
301 Worklist.push_back(I);
302 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000303 }
304
Chris Lattnere31adc82007-06-18 21:28:10 +0000305 while (!Worklist.empty()) {
306 SDNode *N = Worklist.back();
307 Worklist.pop_back();
308
309 if (++Visited[N] != N->getNumOperands())
310 continue; // Haven't visited all operands yet
311
312 Order.push_back(N);
313
314 // Now that we have N in, add anything that uses it if all of their operands
315 // are now done.
316 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
317 UI != E; ++UI)
318 Worklist.push_back(*UI);
319 }
320
321 assert(Order.size() == Visited.size() &&
322 Order.size() ==
323 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
324 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000325}
326
Chris Lattner44fe26f2005-07-29 00:11:56 +0000327
Chris Lattnerdc750592005-01-07 07:47:09 +0000328void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000329 LastCALLSEQ_END = DAG.getEntryNode();
330 IsLegalizingCall = false;
331
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000332 // The legalize process is inherently a bottom-up recursive process (users
333 // legalize their uses before themselves). Given infinite stack space, we
334 // could just start legalizing on the root and traverse the whole graph. In
335 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000336 // blocks. To avoid this problem, compute an ordering of the nodes where each
337 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000338 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000339 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000340
Chris Lattner32206f52006-03-18 01:44:44 +0000341 for (unsigned i = 0, e = Order.size(); i != e; ++i)
342 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000343
344 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000345 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000346 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
347 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000348
349 ExpandedNodes.clear();
350 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000351 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000352 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000353 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000354
355 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000356 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000357}
358
Chris Lattner462505f2006-02-13 09:18:02 +0000359
360/// FindCallEndFromCallStart - Given a chained node that is part of a call
361/// sequence, find the CALLSEQ_END node that terminates the call sequence.
362static SDNode *FindCallEndFromCallStart(SDNode *Node) {
363 if (Node->getOpcode() == ISD::CALLSEQ_END)
364 return Node;
365 if (Node->use_empty())
366 return 0; // No CallSeqEnd
367
368 // The chain is usually at the end.
369 SDOperand TheChain(Node, Node->getNumValues()-1);
370 if (TheChain.getValueType() != MVT::Other) {
371 // Sometimes it's at the beginning.
372 TheChain = SDOperand(Node, 0);
373 if (TheChain.getValueType() != MVT::Other) {
374 // Otherwise, hunt for it.
375 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
376 if (Node->getValueType(i) == MVT::Other) {
377 TheChain = SDOperand(Node, i);
378 break;
379 }
380
381 // Otherwise, we walked into a node without a chain.
382 if (TheChain.getValueType() != MVT::Other)
383 return 0;
384 }
385 }
386
387 for (SDNode::use_iterator UI = Node->use_begin(),
388 E = Node->use_end(); UI != E; ++UI) {
389
390 // Make sure to only follow users of our token chain.
391 SDNode *User = *UI;
392 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
393 if (User->getOperand(i) == TheChain)
394 if (SDNode *Result = FindCallEndFromCallStart(User))
395 return Result;
396 }
397 return 0;
398}
399
400/// FindCallStartFromCallEnd - Given a chained node that is part of a call
401/// sequence, find the CALLSEQ_START node that initiates the call sequence.
402static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
403 assert(Node && "Didn't find callseq_start for a call??");
404 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
405
406 assert(Node->getOperand(0).getValueType() == MVT::Other &&
407 "Node doesn't have a token chain argument!");
408 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
409}
410
411/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
412/// see if any uses can reach Dest. If no dest operands can get to dest,
413/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000414///
415/// Keep track of the nodes we fine that actually do lead to Dest in
416/// NodesLeadingTo. This avoids retraversing them exponential number of times.
417///
418bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000419 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000420 if (N == Dest) return true; // N certainly leads to Dest :)
421
Chris Lattner4488f0c2006-07-26 23:55:56 +0000422 // If we've already processed this node and it does lead to Dest, there is no
423 // need to reprocess it.
424 if (NodesLeadingTo.count(N)) return true;
425
Chris Lattner462505f2006-02-13 09:18:02 +0000426 // If the first result of this node has been already legalized, then it cannot
427 // reach N.
428 switch (getTypeAction(N->getValueType(0))) {
429 case Legal:
430 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 case Promote:
433 if (PromotedNodes.count(SDOperand(N, 0))) return false;
434 break;
435 case Expand:
436 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
437 break;
438 }
439
440 // Okay, this node has not already been legalized. Check and legalize all
441 // operands. If none lead to Dest, then we can legalize this node.
442 bool OperandsLeadToDest = false;
443 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
444 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000445 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000446
Chris Lattner4488f0c2006-07-26 23:55:56 +0000447 if (OperandsLeadToDest) {
448 NodesLeadingTo.insert(N);
449 return true;
450 }
Chris Lattner462505f2006-02-13 09:18:02 +0000451
452 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000453 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000454 return false;
455}
456
Dan Gohmana8665142007-06-25 16:23:39 +0000457/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000458/// appropriate for its type.
459void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000460 MVT::ValueType VT = Op.getValueType();
461 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000462 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000463 case Legal: (void)LegalizeOp(Op); break;
464 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000465 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000466 if (!MVT::isVector(VT)) {
467 // If this is an illegal scalar, expand it into its two component
468 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000469 SDOperand X, Y;
470 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000471 } else if (MVT::getVectorNumElements(VT) == 1) {
472 // If this is an illegal single element vector, convert it to a
473 // scalar operation.
474 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000475 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000476 // Otherwise, this is an illegal multiple element vector.
477 // Split it in half and legalize both parts.
478 SDOperand X, Y;
479 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000480 }
481 break;
482 }
483}
Chris Lattner462505f2006-02-13 09:18:02 +0000484
Evan Cheng22cf8992006-12-13 20:57:08 +0000485/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
486/// a load from the constant pool.
487static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000488 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000489 bool Extend = false;
490
491 // If a FP immediate is precise when represented as a float and if the
492 // target can do an extending load from float to double, we put it into
493 // the constant pool as a float, even if it's is statically typed as a
494 // double.
495 MVT::ValueType VT = CFP->getValueType(0);
496 bool isDouble = VT == MVT::f64;
497 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
498 Type::FloatTy, CFP->getValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000499 if (!UseCP) {
Evan Cheng3766fc602006-12-12 22:19:28 +0000500 double Val = LLVMC->getValue();
501 return isDouble
502 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
503 : DAG.getConstant(FloatToBits(Val), MVT::i32);
504 }
505
Evan Cheng47833a12006-12-12 21:32:44 +0000506 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
507 // Only do this if the target has a native EXTLOAD instruction from f32.
508 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
509 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
510 VT = MVT::f32;
511 Extend = true;
512 }
513
514 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
515 if (Extend) {
516 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
517 CPIdx, NULL, 0, MVT::f32);
518 } else {
519 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
520 }
521}
522
Chris Lattner462505f2006-02-13 09:18:02 +0000523
Evan Cheng003feb02007-01-04 21:56:39 +0000524/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
525/// operations.
526static
527SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
528 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000529 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000530 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000531 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
532 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000533 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534
Evan Cheng003feb02007-01-04 21:56:39 +0000535 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000536 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000537 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
538 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000540 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000541 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000542 // Shift right or sign-extend it if the two operands have different types.
543 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
544 if (SizeDiff > 0) {
545 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
546 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
547 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
548 } else if (SizeDiff < 0)
549 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000550
551 // Clear the sign bit of first operand.
552 SDOperand Mask2 = (VT == MVT::f64)
553 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
554 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
555 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000556 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000557 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
558
559 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000560 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
561 return Result;
562}
563
564
Chris Lattner32206f52006-03-18 01:44:44 +0000565/// LegalizeOp - We know that the specified value has a legal type.
566/// Recursively ensure that the operands have legal types, then return the
567/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000568SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000569 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000570 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000571 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000572
Chris Lattnerdc750592005-01-07 07:47:09 +0000573 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000574 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000575 if (Node->getNumValues() > 1) {
576 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000577 if (getTypeAction(Node->getValueType(i)) != Legal) {
578 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000579 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000580 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000581 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000582 }
583 }
584
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000585 // Note that LegalizeOp may be reentered even from single-use nodes, which
586 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000587 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000588 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000589
Nate Begemane5b86d72005-08-10 20:51:12 +0000590 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000591 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000592 bool isCustom = false;
593
Chris Lattnerdc750592005-01-07 07:47:09 +0000594 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000595 case ISD::FrameIndex:
596 case ISD::EntryToken:
597 case ISD::Register:
598 case ISD::BasicBlock:
599 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000600 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000601 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000602 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000603 case ISD::TargetConstantPool:
604 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000605 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000606 case ISD::TargetExternalSymbol:
607 case ISD::VALUETYPE:
608 case ISD::SRCVALUE:
609 case ISD::STRING:
610 case ISD::CONDCODE:
611 // Primitives must all be legal.
612 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
613 "This must be legal!");
614 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000615 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000616 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
617 // If this is a target node, legalize it by legalizing the operands then
618 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000619 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000620 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000621 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000622
Chris Lattner97af9d52006-08-08 01:09:31 +0000623 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000624
625 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
626 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
627 return Result.getValue(Op.ResNo);
628 }
629 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000630#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000631 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000632#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000633 assert(0 && "Do not know how to legalize this operator!");
634 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000635 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000636 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000637 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000638 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000639 case ISD::ConstantPool:
640 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000641 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
642 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000643 case TargetLowering::Custom:
644 Tmp1 = TLI.LowerOperation(Op, DAG);
645 if (Tmp1.Val) Result = Tmp1;
646 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000647 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000648 break;
649 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000650 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000651 case ISD::FRAMEADDR:
652 case ISD::RETURNADDR:
653 // The only option for these nodes is to custom lower them. If the target
654 // does not custom lower them, then return zero.
655 Tmp1 = TLI.LowerOperation(Op, DAG);
656 if (Tmp1.Val)
657 Result = Tmp1;
658 else
659 Result = DAG.getConstant(0, TLI.getPointerTy());
660 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000661 case ISD::EXCEPTIONADDR: {
662 Tmp1 = LegalizeOp(Node->getOperand(0));
663 MVT::ValueType VT = Node->getValueType(0);
664 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
665 default: assert(0 && "This action is not supported yet!");
666 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000667 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000668 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
669 }
670 break;
671 case TargetLowering::Custom:
672 Result = TLI.LowerOperation(Op, DAG);
673 if (Result.Val) break;
674 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000675 case TargetLowering::Legal: {
676 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
677 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
678 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000679 break;
680 }
681 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000682 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000683 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000684 case ISD::EHSELECTION: {
685 Tmp1 = LegalizeOp(Node->getOperand(0));
686 Tmp2 = LegalizeOp(Node->getOperand(1));
687 MVT::ValueType VT = Node->getValueType(0);
688 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
689 default: assert(0 && "This action is not supported yet!");
690 case TargetLowering::Expand: {
691 unsigned Reg = TLI.getExceptionSelectorRegister();
692 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
693 }
694 break;
695 case TargetLowering::Custom:
696 Result = TLI.LowerOperation(Op, DAG);
697 if (Result.Val) break;
698 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000699 case TargetLowering::Legal: {
700 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
701 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
702 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000703 break;
704 }
705 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000706 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000707 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000708 case ISD::AssertSext:
709 case ISD::AssertZext:
710 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000712 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000713 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000714 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000715 Result = Node->getOperand(Op.ResNo);
716 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000717 case ISD::CopyFromReg:
718 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000719 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000720 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000722 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000723 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000724 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000725 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
727 } else {
728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
729 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000730 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
731 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000732 // Since CopyFromReg produces two values, make sure to remember that we
733 // legalized both of them.
734 AddLegalizedOperand(Op.getValue(0), Result);
735 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
736 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000737 case ISD::UNDEF: {
738 MVT::ValueType VT = Op.getValueType();
739 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000740 default: assert(0 && "This action is not supported yet!");
741 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000742 if (MVT::isInteger(VT))
743 Result = DAG.getConstant(0, VT);
744 else if (MVT::isFloatingPoint(VT))
745 Result = DAG.getConstantFP(0, VT);
746 else
747 assert(0 && "Unknown value type!");
748 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000749 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000750 break;
751 }
752 break;
753 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000754
Chris Lattnere55d1712006-03-28 00:40:33 +0000755 case ISD::INTRINSIC_W_CHAIN:
756 case ISD::INTRINSIC_WO_CHAIN:
757 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000758 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000759 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
760 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000761 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000762
763 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000764 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000765 TargetLowering::Custom) {
766 Tmp3 = TLI.LowerOperation(Result, DAG);
767 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000768 }
769
770 if (Result.Val->getNumValues() == 1) break;
771
772 // Must have return value and chain result.
773 assert(Result.Val->getNumValues() == 2 &&
774 "Cannot return more than two values!");
775
776 // Since loads produce two values, make sure to remember that we
777 // legalized both of them.
778 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
779 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
780 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000781 }
Chris Lattner435b4022005-11-29 06:21:05 +0000782
783 case ISD::LOCATION:
784 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
785 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
786
787 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
788 case TargetLowering::Promote:
789 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000790 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000791 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000792 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000793 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000794
Jim Laskeyc56315c2007-01-26 21:22:28 +0000795 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000796 const std::string &FName =
797 cast<StringSDNode>(Node->getOperand(3))->getValue();
798 const std::string &DirName =
799 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000800 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000801
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000802 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000803 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000804 SDOperand LineOp = Node->getOperand(1);
805 SDOperand ColOp = Node->getOperand(2);
806
807 if (useDEBUG_LOC) {
808 Ops.push_back(LineOp); // line #
809 Ops.push_back(ColOp); // col #
810 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000811 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000812 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000813 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
814 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000815 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000816 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000817 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000818 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000819 } else {
820 Result = Tmp1; // chain
821 }
Chris Lattner435b4022005-11-29 06:21:05 +0000822 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000823 }
Chris Lattner435b4022005-11-29 06:21:05 +0000824 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000825 if (Tmp1 != Node->getOperand(0) ||
826 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000827 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000828 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000829 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
830 Ops.push_back(Node->getOperand(1)); // line # must be legal.
831 Ops.push_back(Node->getOperand(2)); // col # must be legal.
832 } else {
833 // Otherwise promote them.
834 Ops.push_back(PromoteOp(Node->getOperand(1)));
835 Ops.push_back(PromoteOp(Node->getOperand(2)));
836 }
Chris Lattner435b4022005-11-29 06:21:05 +0000837 Ops.push_back(Node->getOperand(3)); // filename must be legal.
838 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000839 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000840 }
841 break;
842 }
843 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000844
845 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000846 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000847 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000848 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000849 case TargetLowering::Legal:
850 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
851 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
852 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
853 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000854 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000855 break;
856 }
857 break;
858
Jim Laskeyf9e54452007-01-26 14:34:52 +0000859 case ISD::LABEL:
860 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
861 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000862 default: assert(0 && "This action is not supported yet!");
863 case TargetLowering::Legal:
864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000867 break;
Chris Lattner567b9252007-03-03 19:21:38 +0000868 case TargetLowering::Expand:
869 Result = LegalizeOp(Node->getOperand(0));
870 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000871 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000872 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000873
Chris Lattnerdc750592005-01-07 07:47:09 +0000874 case ISD::Constant:
875 // We know we don't need to expand constants here, constants only have one
876 // value and we check that it is fine above.
877
878 // FIXME: Maybe we should handle things like targets that don't support full
879 // 32-bit immediates?
880 break;
881 case ISD::ConstantFP: {
882 // Spill FP immediates to the constant pool if the target cannot directly
883 // codegen them. Targets often have some immediate values that can be
884 // efficiently generated into an FP register without a load. We explicitly
885 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000886 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
887
888 // Check to see if this FP immediate is already legal.
889 bool isLegal = false;
890 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
891 E = TLI.legal_fpimm_end(); I != E; ++I)
892 if (CFP->isExactlyValue(*I)) {
893 isLegal = true;
894 break;
895 }
896
Chris Lattner758b0ac2006-01-29 06:26:56 +0000897 // If this is a legal constant, turn it into a TargetConstantFP node.
898 if (isLegal) {
899 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
900 break;
901 }
902
903 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
904 default: assert(0 && "This action is not supported yet!");
905 case TargetLowering::Custom:
906 Tmp3 = TLI.LowerOperation(Result, DAG);
907 if (Tmp3.Val) {
908 Result = Tmp3;
909 break;
910 }
911 // FALLTHROUGH
912 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000913 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000914 }
915 break;
916 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000917 case ISD::TokenFactor:
918 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000919 Tmp1 = LegalizeOp(Node->getOperand(0));
920 Tmp2 = LegalizeOp(Node->getOperand(1));
921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
922 } else if (Node->getNumOperands() == 3) {
923 Tmp1 = LegalizeOp(Node->getOperand(0));
924 Tmp2 = LegalizeOp(Node->getOperand(1));
925 Tmp3 = LegalizeOp(Node->getOperand(2));
926 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000927 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000928 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000929 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000930 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
931 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000932 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000933 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000934 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000935
936 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000937 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000938 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000939 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
940 assert(Tmp3.Val && "Target didn't custom lower this node!");
941 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
942 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000943
Chris Lattneraaa23d92006-05-16 22:53:20 +0000944 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000945 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000946 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
947 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000948 if (Op.ResNo == i)
949 Tmp2 = Tmp1;
950 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
951 }
952 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000953
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000954 case ISD::BUILD_VECTOR:
955 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000956 default: assert(0 && "This action is not supported yet!");
957 case TargetLowering::Custom:
958 Tmp3 = TLI.LowerOperation(Result, DAG);
959 if (Tmp3.Val) {
960 Result = Tmp3;
961 break;
962 }
963 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000964 case TargetLowering::Expand:
965 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000966 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000967 }
Chris Lattner29b23012006-03-19 01:17:20 +0000968 break;
969 case ISD::INSERT_VECTOR_ELT:
970 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
971 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
972 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
974
975 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
976 Node->getValueType(0))) {
977 default: assert(0 && "This action is not supported yet!");
978 case TargetLowering::Legal:
979 break;
980 case TargetLowering::Custom:
981 Tmp3 = TLI.LowerOperation(Result, DAG);
982 if (Tmp3.Val) {
983 Result = Tmp3;
984 break;
985 }
986 // FALLTHROUGH
987 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +0000988 // If the insert index is a constant, codegen this as a scalar_to_vector,
989 // then a shuffle that inserts it into the right position in the vector.
990 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
991 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
992 Tmp1.getValueType(), Tmp2);
993
994 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
995 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +0000996 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +0000997
998 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
999 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1000 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001001 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001002 for (unsigned i = 0; i != NumElts; ++i) {
1003 if (i != InsertPos->getValue())
1004 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1005 else
1006 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1007 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001008 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1009 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001010
1011 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1012 Tmp1, ScVec, ShufMask);
1013 Result = LegalizeOp(Result);
1014 break;
1015 }
1016
Chris Lattner29b23012006-03-19 01:17:20 +00001017 // If the target doesn't support this, we have to spill the input vector
1018 // to a temporary stack slot, update the element, then reload it. This is
1019 // badness. We could also load the value into a vector register (either
1020 // with a "move to register" or "extload into register" instruction, then
1021 // permute it into place, if the idx is a constant and if the idx is
1022 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001023 MVT::ValueType VT = Tmp1.getValueType();
1024 MVT::ValueType EltVT = Tmp2.getValueType();
1025 MVT::ValueType IdxVT = Tmp3.getValueType();
1026 MVT::ValueType PtrVT = TLI.getPointerTy();
1027 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001028 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001029 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001030
1031 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001032 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1033 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001034 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001035 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1036 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1037 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001038 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001039 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001040 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001041 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001042 break;
1043 }
1044 }
1045 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001046 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001047 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1048 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1049 break;
1050 }
1051
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001052 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1053 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1054 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1055 Node->getValueType(0))) {
1056 default: assert(0 && "This action is not supported yet!");
1057 case TargetLowering::Legal:
1058 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001059 case TargetLowering::Custom:
1060 Tmp3 = TLI.LowerOperation(Result, DAG);
1061 if (Tmp3.Val) {
1062 Result = Tmp3;
1063 break;
1064 }
1065 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001066 case TargetLowering::Expand:
1067 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001068 break;
1069 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001070 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001071 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001072 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1073 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1075
1076 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001077 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1078 default: assert(0 && "Unknown operation action!");
1079 case TargetLowering::Legal:
1080 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1081 "vector shuffle should not be created if not legal!");
1082 break;
1083 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001084 Tmp3 = TLI.LowerOperation(Result, DAG);
1085 if (Tmp3.Val) {
1086 Result = Tmp3;
1087 break;
1088 }
1089 // FALLTHROUGH
1090 case TargetLowering::Expand: {
1091 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001092 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001093 MVT::ValueType PtrVT = TLI.getPointerTy();
1094 SDOperand Mask = Node->getOperand(2);
1095 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001096 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001097 for (unsigned i = 0; i != NumElems; ++i) {
1098 SDOperand Arg = Mask.getOperand(i);
1099 if (Arg.getOpcode() == ISD::UNDEF) {
1100 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1101 } else {
1102 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1103 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1104 if (Idx < NumElems)
1105 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1106 DAG.getConstant(Idx, PtrVT)));
1107 else
1108 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1109 DAG.getConstant(Idx - NumElems, PtrVT)));
1110 }
1111 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001112 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001113 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001114 }
Chris Lattner6be79822006-04-04 17:23:26 +00001115 case TargetLowering::Promote: {
1116 // Change base type to a different vector type.
1117 MVT::ValueType OVT = Node->getValueType(0);
1118 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1119
1120 // Cast the two input vectors.
1121 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1122 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1123
1124 // Convert the shuffle mask to the right # elements.
1125 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1126 assert(Tmp3.Val && "Shuffle not legal?");
1127 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1128 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1129 break;
1130 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001131 }
1132 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001133
1134 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001135 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001136 Tmp2 = LegalizeOp(Node->getOperand(1));
1137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001138 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001139 break;
1140
Dan Gohmana8665142007-06-25 16:23:39 +00001141 case ISD::EXTRACT_SUBVECTOR:
1142 Tmp1 = Node->getOperand(0);
1143 Tmp2 = LegalizeOp(Node->getOperand(1));
1144 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1145 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001146 break;
1147
Chris Lattner462505f2006-02-13 09:18:02 +00001148 case ISD::CALLSEQ_START: {
1149 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1150
1151 // Recursively Legalize all of the inputs of the call end that do not lead
1152 // to this call start. This ensures that any libcalls that need be inserted
1153 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001154 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001155 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001156 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1157 NodesLeadingTo);
1158 }
Chris Lattner462505f2006-02-13 09:18:02 +00001159
1160 // Now that we legalized all of the inputs (which may have inserted
1161 // libcalls) create the new CALLSEQ_START node.
1162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1163
1164 // Merge in the last call, to ensure that this call start after the last
1165 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001166 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001167 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1168 Tmp1 = LegalizeOp(Tmp1);
1169 }
Chris Lattner462505f2006-02-13 09:18:02 +00001170
1171 // Do not try to legalize the target-specific arguments (#1+).
1172 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001173 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001174 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001175 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001176 }
1177
1178 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001179 AddLegalizedOperand(Op.getValue(0), Result);
1180 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1181 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1182
Chris Lattner462505f2006-02-13 09:18:02 +00001183 // Now that the callseq_start and all of the non-call nodes above this call
1184 // sequence have been legalized, legalize the call itself. During this
1185 // process, no libcalls can/will be inserted, guaranteeing that no calls
1186 // can overlap.
1187 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1188 SDOperand InCallSEQ = LastCALLSEQ_END;
1189 // Note that we are selecting this call!
1190 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1191 IsLegalizingCall = true;
1192
1193 // Legalize the call, starting from the CALLSEQ_END.
1194 LegalizeOp(LastCALLSEQ_END);
1195 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1196 return Result;
1197 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001198 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001199 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1200 // will cause this node to be legalized as well as handling libcalls right.
1201 if (LastCALLSEQ_END.Val != Node) {
1202 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001203 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001204 assert(I != LegalizedNodes.end() &&
1205 "Legalizing the call start should have legalized this node!");
1206 return I->second;
1207 }
1208
1209 // Otherwise, the call start has been legalized and everything is going
1210 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001211 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001212 // Do not try to legalize the target-specific arguments (#1+), except for
1213 // an optional flag input.
1214 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1215 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001216 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001217 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001218 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001219 }
1220 } else {
1221 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1222 if (Tmp1 != Node->getOperand(0) ||
1223 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001224 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001225 Ops[0] = Tmp1;
1226 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001227 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001228 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001229 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001230 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001231 // This finishes up call legalization.
1232 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001233
1234 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1235 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1236 if (Node->getNumValues() == 2)
1237 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1238 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001239 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001240 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1241 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1242 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001244
Chris Lattnerd02b0542006-01-28 10:58:55 +00001245 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001246 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001247 switch (TLI.getOperationAction(Node->getOpcode(),
1248 Node->getValueType(0))) {
1249 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001250 case TargetLowering::Expand: {
1251 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1252 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1253 " not tell us which reg is the stack pointer!");
1254 SDOperand Chain = Tmp1.getOperand(0);
1255 SDOperand Size = Tmp2.getOperand(1);
1256 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1257 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1258 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001259 Tmp1 = LegalizeOp(Tmp1);
1260 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001261 break;
1262 }
1263 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001264 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1265 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001266 Tmp1 = LegalizeOp(Tmp3);
1267 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001268 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001269 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001270 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001271 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001272 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001273 // Since this op produce two values, make sure to remember that we
1274 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001275 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1276 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001277 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001278 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001279 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001280 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001281 bool Changed = false;
1282 // Legalize all of the operands of the inline asm, in case they are nodes
1283 // that need to be expanded or something. Note we skip the asm string and
1284 // all of the TargetConstant flags.
1285 SDOperand Op = LegalizeOp(Ops[0]);
1286 Changed = Op != Ops[0];
1287 Ops[0] = Op;
1288
1289 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1290 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1291 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1292 for (++i; NumVals; ++i, --NumVals) {
1293 SDOperand Op = LegalizeOp(Ops[i]);
1294 if (Op != Ops[i]) {
1295 Changed = true;
1296 Ops[i] = Op;
1297 }
1298 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001299 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001300
1301 if (HasInFlag) {
1302 Op = LegalizeOp(Ops.back());
1303 Changed |= Op != Ops.back();
1304 Ops.back() = Op;
1305 }
1306
1307 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001308 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001309
1310 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001311 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001312 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1313 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001314 }
Chris Lattner68a12142005-01-07 22:12:08 +00001315 case ISD::BR:
1316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001317 // Ensure that libcalls are emitted before a branch.
1318 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1319 Tmp1 = LegalizeOp(Tmp1);
1320 LastCALLSEQ_END = DAG.getEntryNode();
1321
Chris Lattnerd02b0542006-01-28 10:58:55 +00001322 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001323 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001324 case ISD::BRIND:
1325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1326 // Ensure that libcalls are emitted before a branch.
1327 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1328 Tmp1 = LegalizeOp(Tmp1);
1329 LastCALLSEQ_END = DAG.getEntryNode();
1330
1331 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1332 default: assert(0 && "Indirect target must be legal type (pointer)!");
1333 case Legal:
1334 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1335 break;
1336 }
1337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1338 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001339 case ISD::BR_JT:
1340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1341 // Ensure that libcalls are emitted before a branch.
1342 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1343 Tmp1 = LegalizeOp(Tmp1);
1344 LastCALLSEQ_END = DAG.getEntryNode();
1345
1346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1348
1349 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1350 default: assert(0 && "This action is not supported yet!");
1351 case TargetLowering::Legal: break;
1352 case TargetLowering::Custom:
1353 Tmp1 = TLI.LowerOperation(Result, DAG);
1354 if (Tmp1.Val) Result = Tmp1;
1355 break;
1356 case TargetLowering::Expand: {
1357 SDOperand Chain = Result.getOperand(0);
1358 SDOperand Table = Result.getOperand(1);
1359 SDOperand Index = Result.getOperand(2);
1360
1361 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001362 MachineFunction &MF = DAG.getMachineFunction();
1363 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001364 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1365 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001366
1367 SDOperand LD;
1368 switch (EntrySize) {
1369 default: assert(0 && "Size of jump table not supported yet."); break;
1370 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1371 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1372 }
1373
1374 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001375 // For PIC, the sequence is:
1376 // BRIND(load(Jumptable + index) + RelocBase)
1377 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1378 SDOperand Reloc;
1379 if (TLI.usesGlobalOffsetTable())
1380 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1381 else
1382 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001383 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001384 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1385 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1386 } else {
1387 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1388 }
1389 }
1390 }
1391 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001392 case ISD::BRCOND:
1393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001394 // Ensure that libcalls are emitted before a return.
1395 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1396 Tmp1 = LegalizeOp(Tmp1);
1397 LastCALLSEQ_END = DAG.getEntryNode();
1398
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001399 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1400 case Expand: assert(0 && "It's impossible to expand bools");
1401 case Legal:
1402 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1403 break;
1404 case Promote:
1405 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001406
1407 // The top bits of the promoted condition are not necessarily zero, ensure
1408 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001409 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001410 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1411 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001412 break;
1413 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001414
1415 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001417
1418 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1419 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001420 case TargetLowering::Legal: break;
1421 case TargetLowering::Custom:
1422 Tmp1 = TLI.LowerOperation(Result, DAG);
1423 if (Tmp1.Val) Result = Tmp1;
1424 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001425 case TargetLowering::Expand:
1426 // Expand brcond's setcc into its constituent parts and create a BR_CC
1427 // Node.
1428 if (Tmp2.getOpcode() == ISD::SETCC) {
1429 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1430 Tmp2.getOperand(0), Tmp2.getOperand(1),
1431 Node->getOperand(2));
1432 } else {
1433 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1434 DAG.getCondCode(ISD::SETNE), Tmp2,
1435 DAG.getConstant(0, Tmp2.getValueType()),
1436 Node->getOperand(2));
1437 }
1438 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001439 }
1440 break;
1441 case ISD::BR_CC:
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001443 // Ensure that libcalls are emitted before a branch.
1444 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1445 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001446 Tmp2 = Node->getOperand(2); // LHS
1447 Tmp3 = Node->getOperand(3); // RHS
1448 Tmp4 = Node->getOperand(1); // CC
1449
1450 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001451 LastCALLSEQ_END = DAG.getEntryNode();
1452
Nate Begeman7e7f4392006-02-01 07:19:44 +00001453 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1454 // the LHS is a legal SETCC itself. In this case, we need to compare
1455 // the result against zero to select between true and false values.
1456 if (Tmp3.Val == 0) {
1457 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1458 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001459 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001460
1461 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1462 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001463
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001464 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1465 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001466 case TargetLowering::Legal: break;
1467 case TargetLowering::Custom:
1468 Tmp4 = TLI.LowerOperation(Result, DAG);
1469 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001470 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001471 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001472 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001473 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001474 LoadSDNode *LD = cast<LoadSDNode>(Node);
1475 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1476 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001477
Evan Chenge71fe34d2006-10-09 20:57:25 +00001478 ISD::LoadExtType ExtType = LD->getExtensionType();
1479 if (ExtType == ISD::NON_EXTLOAD) {
1480 MVT::ValueType VT = Node->getValueType(0);
1481 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1482 Tmp3 = Result.getValue(0);
1483 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001484
Evan Chenge71fe34d2006-10-09 20:57:25 +00001485 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1486 default: assert(0 && "This action is not supported yet!");
1487 case TargetLowering::Legal: break;
1488 case TargetLowering::Custom:
1489 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1490 if (Tmp1.Val) {
1491 Tmp3 = LegalizeOp(Tmp1);
1492 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001493 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001494 break;
1495 case TargetLowering::Promote: {
1496 // Only promote a load of vector type to another.
1497 assert(MVT::isVector(VT) && "Cannot promote this load!");
1498 // Change base type to a different vector type.
1499 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1500
1501 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1502 LD->getSrcValueOffset());
1503 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1504 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001505 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001506 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001507 }
1508 // Since loads produce two values, make sure to remember that we
1509 // legalized both of them.
1510 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1511 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1512 return Op.ResNo ? Tmp4 : Tmp3;
1513 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001514 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001515 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1516 default: assert(0 && "This action is not supported yet!");
1517 case TargetLowering::Promote:
1518 assert(SrcVT == MVT::i1 &&
1519 "Can only promote extending LOAD from i1 -> i8!");
1520 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1521 LD->getSrcValue(), LD->getSrcValueOffset(),
1522 MVT::i8);
1523 Tmp1 = Result.getValue(0);
1524 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001525 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001526 case TargetLowering::Custom:
1527 isCustom = true;
1528 // FALLTHROUGH
1529 case TargetLowering::Legal:
1530 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1531 Tmp1 = Result.getValue(0);
1532 Tmp2 = Result.getValue(1);
1533
1534 if (isCustom) {
1535 Tmp3 = TLI.LowerOperation(Result, DAG);
1536 if (Tmp3.Val) {
1537 Tmp1 = LegalizeOp(Tmp3);
1538 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1539 }
1540 }
1541 break;
1542 case TargetLowering::Expand:
1543 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1544 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1545 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1546 LD->getSrcValueOffset());
1547 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1548 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1549 Tmp2 = LegalizeOp(Load.getValue(1));
1550 break;
1551 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001552 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001553 // Turn the unsupported load into an EXTLOAD followed by an explicit
1554 // zero/sign extend inreg.
1555 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1556 Tmp1, Tmp2, LD->getSrcValue(),
1557 LD->getSrcValueOffset(), SrcVT);
1558 SDOperand ValRes;
1559 if (ExtType == ISD::SEXTLOAD)
1560 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1561 Result, DAG.getValueType(SrcVT));
1562 else
1563 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1564 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1565 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1566 break;
1567 }
1568 // Since loads produce two values, make sure to remember that we legalized
1569 // both of them.
1570 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1571 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1572 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001573 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001574 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001575 case ISD::EXTRACT_ELEMENT: {
1576 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1577 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001578 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001579 case Legal:
1580 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1581 // 1 -> Hi
1582 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1583 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1584 TLI.getShiftAmountTy()));
1585 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1586 } else {
1587 // 0 -> Lo
1588 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1589 Node->getOperand(0));
1590 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001591 break;
1592 case Expand:
1593 // Get both the low and high parts.
1594 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1595 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1596 Result = Tmp2; // 1 -> Hi
1597 else
1598 Result = Tmp1; // 0 -> Lo
1599 break;
1600 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001601 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001602 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001603
1604 case ISD::CopyToReg:
1605 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001606
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001607 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001608 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001609 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001610 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001611 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001612 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001613 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001614 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001615 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001616 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001617 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1618 Tmp3);
1619 } else {
1620 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001621 }
1622
1623 // Since this produces two values, make sure to remember that we legalized
1624 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001625 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001626 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001627 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001628 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001629 break;
1630
1631 case ISD::RET:
1632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001633
1634 // Ensure that libcalls are emitted before a return.
1635 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1636 Tmp1 = LegalizeOp(Tmp1);
1637 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001638
Chris Lattnerdc750592005-01-07 07:47:09 +00001639 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001640 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001641 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001642 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001643 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001644 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001645 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001646 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001647 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001648 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001649 SDOperand Lo, Hi;
1650 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001651
1652 // Big endian systems want the hi reg first.
1653 if (!TLI.isLittleEndian())
1654 std::swap(Lo, Hi);
1655
Evan Cheng3432ab92006-12-11 19:27:14 +00001656 if (Hi.Val)
1657 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1658 else
1659 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001660 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001661 } else {
1662 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001663 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1664 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001665
Dan Gohmana8665142007-06-25 16:23:39 +00001666 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001667 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001668 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001669 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001670 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001671 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001673 } else if (NumElems == 1) {
1674 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001675 Tmp2 = ScalarizeVectorOp(Tmp2);
1676 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001677 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001678
1679 // FIXME: Returns of gcc generic vectors smaller than a legal type
1680 // should be returned in integer registers!
1681
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001682 // The scalarized value type may not be legal, e.g. it might require
1683 // promotion or expansion. Relegalize the return.
1684 Result = LegalizeOp(Result);
1685 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001686 // FIXME: Returns of gcc generic vectors larger than a legal vector
1687 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001688 SDOperand Lo, Hi;
1689 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001690 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001691 Result = LegalizeOp(Result);
1692 }
1693 }
Misha Brukman835702a2005-04-21 22:36:52 +00001694 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001695 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001696 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001698 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001699 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001700 }
1701 break;
1702 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001703 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001704 break;
1705 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001706 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001707 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001708 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001709 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1710 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001711 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001712 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001713 break;
1714 case Expand: {
1715 SDOperand Lo, Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00001716 assert(!MVT::isExtendedValueType(Node->getOperand(i).getValueType())&&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001717 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001718 ExpandOp(Node->getOperand(i), Lo, Hi);
1719 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001720 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001721 if (Hi.Val) {
1722 NewValues.push_back(Hi);
1723 NewValues.push_back(Node->getOperand(i+1));
1724 }
Misha Brukman835702a2005-04-21 22:36:52 +00001725 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001726 }
1727 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001728 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001729 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001730
1731 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001732 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001733 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001734 Result = DAG.getNode(ISD::RET, MVT::Other,
1735 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001736 break;
1737 }
1738 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001739
Chris Lattner4d1ea712006-01-29 21:02:23 +00001740 if (Result.getOpcode() == ISD::RET) {
1741 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1742 default: assert(0 && "This action is not supported yet!");
1743 case TargetLowering::Legal: break;
1744 case TargetLowering::Custom:
1745 Tmp1 = TLI.LowerOperation(Result, DAG);
1746 if (Tmp1.Val) Result = Tmp1;
1747 break;
1748 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001749 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001750 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001751 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001752 StoreSDNode *ST = cast<StoreSDNode>(Node);
1753 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1754 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattnerdc750592005-01-07 07:47:09 +00001755
Evan Chengab51cf22006-10-13 21:14:26 +00001756 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001757 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1758 // FIXME: We shouldn't do this for TargetConstantFP's.
1759 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1760 // to phase ordering between legalized code and the dag combiner. This
1761 // probably means that we need to integrate dag combiner and legalizer
1762 // together.
1763 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1764 if (CFP->getValueType(0) == MVT::f32) {
1765 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1766 } else {
1767 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1768 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1769 }
1770 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1771 ST->getSrcValueOffset());
1772 break;
1773 }
1774
Evan Chengab51cf22006-10-13 21:14:26 +00001775 switch (getTypeAction(ST->getStoredVT())) {
1776 case Legal: {
1777 Tmp3 = LegalizeOp(ST->getValue());
1778 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1779 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001780
Evan Chengab51cf22006-10-13 21:14:26 +00001781 MVT::ValueType VT = Tmp3.getValueType();
1782 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1783 default: assert(0 && "This action is not supported yet!");
1784 case TargetLowering::Legal: break;
1785 case TargetLowering::Custom:
1786 Tmp1 = TLI.LowerOperation(Result, DAG);
1787 if (Tmp1.Val) Result = Tmp1;
1788 break;
1789 case TargetLowering::Promote:
1790 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1791 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1792 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1793 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1794 ST->getSrcValue(), ST->getSrcValueOffset());
1795 break;
1796 }
1797 break;
1798 }
1799 case Promote:
1800 // Truncate the value and store the result.
1801 Tmp3 = PromoteOp(ST->getValue());
1802 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1803 ST->getSrcValueOffset(), ST->getStoredVT());
1804 break;
1805
1806 case Expand:
1807 unsigned IncrementSize = 0;
1808 SDOperand Lo, Hi;
1809
1810 // If this is a vector type, then we have to calculate the increment as
1811 // the product of the element size in bytes, and the number of elements
1812 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00001813 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00001814 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001815 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1816 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00001817
Dan Gohmana8665142007-06-25 16:23:39 +00001818 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001819 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001820 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001821 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001822 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001823 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001824 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmana8665142007-06-25 16:23:39 +00001825 ST->getSrcValueOffset(),
1826 ST->isVolatile(),
1827 ST->getAlignment());
Evan Chengab51cf22006-10-13 21:14:26 +00001828 Result = LegalizeOp(Result);
1829 break;
1830 } else if (NumElems == 1) {
1831 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001832 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001833 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmana8665142007-06-25 16:23:39 +00001834 ST->getSrcValueOffset(),
1835 ST->isVolatile(),
1836 ST->getAlignment());
Evan Chengab51cf22006-10-13 21:14:26 +00001837 // The scalarized value type may not be legal, e.g. it might require
1838 // promotion or expansion. Relegalize the scalar store.
1839 Result = LegalizeOp(Result);
1840 break;
1841 } else {
1842 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1843 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1844 }
1845 } else {
1846 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001847 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001848
1849 if (!TLI.isLittleEndian())
1850 std::swap(Lo, Hi);
1851 }
1852
1853 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner07e6f322007-05-05 19:39:05 +00001854 ST->getSrcValueOffset(), ST->isVolatile(),
1855 ST->getAlignment());
Evan Cheng0076ca02006-12-12 19:53:13 +00001856
1857 if (Hi.Val == NULL) {
1858 // Must be int <-> float one-to-one expansion.
1859 Result = Lo;
1860 break;
1861 }
1862
Evan Chengab51cf22006-10-13 21:14:26 +00001863 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1864 getIntPtrConstant(IncrementSize));
1865 assert(isTypeLegal(Tmp2.getValueType()) &&
1866 "Pointers must be legal!");
1867 // FIXME: This sets the srcvalue of both halves to be the same, which is
1868 // wrong.
1869 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Chris Lattner07e6f322007-05-05 19:39:05 +00001870 ST->getSrcValueOffset(), ST->isVolatile(),
1871 std::min(ST->getAlignment(), IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00001872 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1873 break;
1874 }
1875 } else {
1876 // Truncating store
1877 assert(isTypeLegal(ST->getValue().getValueType()) &&
1878 "Cannot handle illegal TRUNCSTORE yet!");
1879 Tmp3 = LegalizeOp(ST->getValue());
1880
1881 // The only promote case we handle is TRUNCSTORE:i1 X into
1882 // -> TRUNCSTORE:i8 (and X, 1)
1883 if (ST->getStoredVT() == MVT::i1 &&
1884 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1885 // Promote the bool to a mask then store.
1886 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1887 DAG.getConstant(1, Tmp3.getValueType()));
1888 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1889 ST->getSrcValueOffset(), MVT::i8);
1890 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1891 Tmp2 != ST->getBasePtr()) {
1892 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1893 ST->getOffset());
1894 }
1895
1896 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1897 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001898 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001899 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001900 case TargetLowering::Custom:
1901 Tmp1 = TLI.LowerOperation(Result, DAG);
1902 if (Tmp1.Val) Result = Tmp1;
1903 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001904 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001905 }
1906 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001907 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001908 case ISD::PCMARKER:
1909 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001911 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001912 case ISD::STACKSAVE:
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001914 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1915 Tmp1 = Result.getValue(0);
1916 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001917
Chris Lattnerb3266452006-01-13 02:50:02 +00001918 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1919 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001920 case TargetLowering::Legal: break;
1921 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001922 Tmp3 = TLI.LowerOperation(Result, DAG);
1923 if (Tmp3.Val) {
1924 Tmp1 = LegalizeOp(Tmp3);
1925 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001926 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001927 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001928 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001929 // Expand to CopyFromReg if the target set
1930 // StackPointerRegisterToSaveRestore.
1931 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001932 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001933 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001934 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001935 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001936 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1937 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001938 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001939 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001940 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001941
1942 // Since stacksave produce two values, make sure to remember that we
1943 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001944 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1945 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1946 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001947
Chris Lattnerb3266452006-01-13 02:50:02 +00001948 case ISD::STACKRESTORE:
1949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1950 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001951 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001952
1953 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1954 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001955 case TargetLowering::Legal: break;
1956 case TargetLowering::Custom:
1957 Tmp1 = TLI.LowerOperation(Result, DAG);
1958 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001959 break;
1960 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001961 // Expand to CopyToReg if the target set
1962 // StackPointerRegisterToSaveRestore.
1963 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1964 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1965 } else {
1966 Result = Tmp1;
1967 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001968 break;
1969 }
1970 break;
1971
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001972 case ISD::READCYCLECOUNTER:
1973 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001974 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001975 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1976 Node->getValueType(0))) {
1977 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001978 case TargetLowering::Legal:
1979 Tmp1 = Result.getValue(0);
1980 Tmp2 = Result.getValue(1);
1981 break;
Evan Cheng69739932006-11-29 08:26:18 +00001982 case TargetLowering::Custom:
1983 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00001984 Tmp1 = LegalizeOp(Result.getValue(0));
1985 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00001986 break;
1987 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001988
1989 // Since rdcc produce two values, make sure to remember that we legalized
1990 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00001991 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1992 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001993 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001994
Chris Lattner39c67442005-01-14 22:08:15 +00001995 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001996 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1997 case Expand: assert(0 && "It's impossible to expand bools");
1998 case Legal:
1999 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2000 break;
2001 case Promote:
2002 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002003 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002004 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002005 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2006 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002007 break;
2008 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002009 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002010 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002011
Chris Lattnerd02b0542006-01-28 10:58:55 +00002012 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002013
Nate Begeman987121a2005-08-23 04:29:48 +00002014 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002015 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002016 case TargetLowering::Legal: break;
2017 case TargetLowering::Custom: {
2018 Tmp1 = TLI.LowerOperation(Result, DAG);
2019 if (Tmp1.Val) Result = Tmp1;
2020 break;
2021 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002022 case TargetLowering::Expand:
2023 if (Tmp1.getOpcode() == ISD::SETCC) {
2024 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2025 Tmp2, Tmp3,
2026 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2027 } else {
2028 Result = DAG.getSelectCC(Tmp1,
2029 DAG.getConstant(0, Tmp1.getValueType()),
2030 Tmp2, Tmp3, ISD::SETNE);
2031 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002032 break;
2033 case TargetLowering::Promote: {
2034 MVT::ValueType NVT =
2035 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2036 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002037 if (MVT::isVector(Tmp2.getValueType())) {
2038 ExtOp = ISD::BIT_CONVERT;
2039 TruncOp = ISD::BIT_CONVERT;
2040 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002041 ExtOp = ISD::ANY_EXTEND;
2042 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002043 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002044 ExtOp = ISD::FP_EXTEND;
2045 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002046 }
2047 // Promote each of the values to the new type.
2048 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2049 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2050 // Perform the larger operation, then round down.
2051 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2052 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2053 break;
2054 }
2055 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002056 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002057 case ISD::SELECT_CC: {
2058 Tmp1 = Node->getOperand(0); // LHS
2059 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002060 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2061 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002062 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002063
Nate Begeman7e7f4392006-02-01 07:19:44 +00002064 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2065
2066 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2067 // the LHS is a legal SETCC itself. In this case, we need to compare
2068 // the result against zero to select between true and false values.
2069 if (Tmp2.Val == 0) {
2070 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2071 CC = DAG.getCondCode(ISD::SETNE);
2072 }
2073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2074
2075 // Everything is legal, see if we should expand this op or something.
2076 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2077 default: assert(0 && "This action is not supported yet!");
2078 case TargetLowering::Legal: break;
2079 case TargetLowering::Custom:
2080 Tmp1 = TLI.LowerOperation(Result, DAG);
2081 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002082 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002083 }
2084 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002085 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002086 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002087 Tmp1 = Node->getOperand(0);
2088 Tmp2 = Node->getOperand(1);
2089 Tmp3 = Node->getOperand(2);
2090 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2091
2092 // If we had to Expand the SetCC operands into a SELECT node, then it may
2093 // not always be possible to return a true LHS & RHS. In this case, just
2094 // return the value we legalized, returned in the LHS
2095 if (Tmp2.Val == 0) {
2096 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002097 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002098 }
Nate Begeman987121a2005-08-23 04:29:48 +00002099
Chris Lattnerf263a232006-01-30 22:43:50 +00002100 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002101 default: assert(0 && "Cannot handle this action for SETCC yet!");
2102 case TargetLowering::Custom:
2103 isCustom = true;
2104 // FALLTHROUGH.
2105 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002107 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002108 Tmp4 = TLI.LowerOperation(Result, DAG);
2109 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002110 }
Nate Begeman987121a2005-08-23 04:29:48 +00002111 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002112 case TargetLowering::Promote: {
2113 // First step, figure out the appropriate operation to use.
2114 // Allow SETCC to not be supported for all legal data types
2115 // Mostly this targets FP
2116 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002117 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002118
2119 // Scan for the appropriate larger type to use.
2120 while (1) {
2121 NewInTy = (MVT::ValueType)(NewInTy+1);
2122
2123 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2124 "Fell off of the edge of the integer world");
2125 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2126 "Fell off of the edge of the floating point world");
2127
2128 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002129 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002130 break;
2131 }
2132 if (MVT::isInteger(NewInTy))
2133 assert(0 && "Cannot promote Legal Integer SETCC yet");
2134 else {
2135 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2136 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2137 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002138 Tmp1 = LegalizeOp(Tmp1);
2139 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002141 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002142 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002143 }
Nate Begeman987121a2005-08-23 04:29:48 +00002144 case TargetLowering::Expand:
2145 // Expand a setcc node into a select_cc of the same condition, lhs, and
2146 // rhs that selects between const 1 (true) and const 0 (false).
2147 MVT::ValueType VT = Node->getValueType(0);
2148 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2149 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002150 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002151 break;
2152 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002153 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002154 case ISD::MEMSET:
2155 case ISD::MEMCPY:
2156 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002158 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2159
2160 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2161 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2162 case Expand: assert(0 && "Cannot expand a byte!");
2163 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002164 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002165 break;
2166 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002167 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002168 break;
2169 }
2170 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002171 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002172 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002173
2174 SDOperand Tmp4;
2175 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002176 case Expand: {
2177 // Length is too big, just take the lo-part of the length.
2178 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002179 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002180 break;
2181 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002182 case Legal:
2183 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002184 break;
2185 case Promote:
2186 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002187 break;
2188 }
2189
2190 SDOperand Tmp5;
2191 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2192 case Expand: assert(0 && "Cannot expand this yet!");
2193 case Legal:
2194 Tmp5 = LegalizeOp(Node->getOperand(4));
2195 break;
2196 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002197 Tmp5 = PromoteOp(Node->getOperand(4));
2198 break;
2199 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002200
2201 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2202 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002203 case TargetLowering::Custom:
2204 isCustom = true;
2205 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002206 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002208 if (isCustom) {
2209 Tmp1 = TLI.LowerOperation(Result, DAG);
2210 if (Tmp1.Val) Result = Tmp1;
2211 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002212 break;
2213 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002214 // Otherwise, the target does not support this operation. Lower the
2215 // operation to an explicit libcall as appropriate.
2216 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002217 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002218 TargetLowering::ArgListTy Args;
2219 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002220
Reid Spencer6dced922005-01-12 14:53:45 +00002221 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002222 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002223 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002224 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002225 // Extend the (previously legalized) ubyte argument to be an int value
2226 // for the call.
2227 if (Tmp3.getValueType() > MVT::i32)
2228 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2229 else
2230 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002231 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002232 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002233 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002234 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002235
2236 FnName = "memset";
2237 } else if (Node->getOpcode() == ISD::MEMCPY ||
2238 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002239 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002240 Entry.Node = Tmp2; Args.push_back(Entry);
2241 Entry.Node = Tmp3; Args.push_back(Entry);
2242 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002243 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2244 } else {
2245 assert(0 && "Unknown op!");
2246 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002247
Chris Lattner85d70c62005-01-11 05:57:22 +00002248 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002249 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002250 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002251 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002252 break;
2253 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002254 }
2255 break;
2256 }
Chris Lattner5385db52005-05-09 20:23:03 +00002257
Chris Lattner4157c412005-04-02 04:00:59 +00002258 case ISD::SHL_PARTS:
2259 case ISD::SRA_PARTS:
2260 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002261 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002262 bool Changed = false;
2263 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2264 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2265 Changed |= Ops.back() != Node->getOperand(i);
2266 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002267 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002268 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002269
Evan Cheng870e4f82006-01-09 18:31:59 +00002270 switch (TLI.getOperationAction(Node->getOpcode(),
2271 Node->getValueType(0))) {
2272 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002273 case TargetLowering::Legal: break;
2274 case TargetLowering::Custom:
2275 Tmp1 = TLI.LowerOperation(Result, DAG);
2276 if (Tmp1.Val) {
2277 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002278 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002279 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002280 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2281 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002282 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002283 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002284 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002285 return RetVal;
2286 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002287 break;
2288 }
2289
Chris Lattner13fe99c2005-04-02 05:00:07 +00002290 // Since these produce multiple values, make sure to remember that we
2291 // legalized all of them.
2292 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2293 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2294 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002295 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002296
2297 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002298 case ISD::ADD:
2299 case ISD::SUB:
2300 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002301 case ISD::MULHS:
2302 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002303 case ISD::UDIV:
2304 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002305 case ISD::AND:
2306 case ISD::OR:
2307 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002308 case ISD::SHL:
2309 case ISD::SRL:
2310 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002311 case ISD::FADD:
2312 case ISD::FSUB:
2313 case ISD::FMUL:
2314 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002316 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2317 case Expand: assert(0 && "Not possible");
2318 case Legal:
2319 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2320 break;
2321 case Promote:
2322 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2323 break;
2324 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002325
2326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002327
Andrew Lenharth72594262005-12-24 23:42:32 +00002328 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002329 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002330 case TargetLowering::Legal: break;
2331 case TargetLowering::Custom:
2332 Tmp1 = TLI.LowerOperation(Result, DAG);
2333 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002334 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002335 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002336 if (Node->getValueType(0) == MVT::i32) {
2337 switch (Node->getOpcode()) {
2338 default: assert(0 && "Do not know how to expand this integer BinOp!");
2339 case ISD::UDIV:
2340 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002341 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2342 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002343 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002344 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002345 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002346 };
2347 break;
2348 }
2349
Chris Lattner87f08092006-04-02 03:57:31 +00002350 assert(MVT::isVector(Node->getValueType(0)) &&
2351 "Cannot expand this binary operator!");
2352 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002353 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002354 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002355 MVT::ValueType PtrVT = TLI.getPointerTy();
2356 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2357 i != e; ++i) {
2358 SDOperand Idx = DAG.getConstant(i, PtrVT);
2359 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2360 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2361 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2362 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002363 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2364 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002365 break;
2366 }
Evan Cheng119266e2006-04-12 21:20:24 +00002367 case TargetLowering::Promote: {
2368 switch (Node->getOpcode()) {
2369 default: assert(0 && "Do not know how to promote this BinOp!");
2370 case ISD::AND:
2371 case ISD::OR:
2372 case ISD::XOR: {
2373 MVT::ValueType OVT = Node->getValueType(0);
2374 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2375 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2376 // Bit convert each of the values to the new type.
2377 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2378 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2379 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2380 // Bit convert the result back the original type.
2381 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2382 break;
2383 }
2384 }
2385 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002386 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002387 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002388
2389 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2390 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2391 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2392 case Expand: assert(0 && "Not possible");
2393 case Legal:
2394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2395 break;
2396 case Promote:
2397 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2398 break;
2399 }
2400
2401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2402
2403 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2404 default: assert(0 && "Operation not supported");
2405 case TargetLowering::Custom:
2406 Tmp1 = TLI.LowerOperation(Result, DAG);
2407 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002408 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002409 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002410 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002411 // If this target supports fabs/fneg natively and select is cheap,
2412 // do this efficiently.
2413 if (!TLI.isSelectExpensive() &&
2414 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2415 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002416 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002417 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002418 // Get the sign bit of the RHS.
2419 MVT::ValueType IVT =
2420 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2421 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2422 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2423 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2424 // Get the absolute value of the result.
2425 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2426 // Select between the nabs and abs value based on the sign bit of
2427 // the input.
2428 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2429 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2430 AbsVal),
2431 AbsVal);
2432 Result = LegalizeOp(Result);
2433 break;
2434 }
2435
2436 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002437 MVT::ValueType NVT =
2438 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2439 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2440 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2441 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002442 break;
2443 }
Evan Cheng003feb02007-01-04 21:56:39 +00002444 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002445 break;
2446
Nate Begeman5965bd12006-02-17 05:43:56 +00002447 case ISD::ADDC:
2448 case ISD::SUBC:
2449 Tmp1 = LegalizeOp(Node->getOperand(0));
2450 Tmp2 = LegalizeOp(Node->getOperand(1));
2451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2452 // Since this produces two values, make sure to remember that we legalized
2453 // both of them.
2454 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2455 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2456 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002457
Nate Begeman5965bd12006-02-17 05:43:56 +00002458 case ISD::ADDE:
2459 case ISD::SUBE:
2460 Tmp1 = LegalizeOp(Node->getOperand(0));
2461 Tmp2 = LegalizeOp(Node->getOperand(1));
2462 Tmp3 = LegalizeOp(Node->getOperand(2));
2463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2464 // Since this produces two values, make sure to remember that we legalized
2465 // both of them.
2466 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2467 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2468 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002469
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002470 case ISD::BUILD_PAIR: {
2471 MVT::ValueType PairTy = Node->getValueType(0);
2472 // TODO: handle the case where the Lo and Hi operands are not of legal type
2473 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2474 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2475 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002476 case TargetLowering::Promote:
2477 case TargetLowering::Custom:
2478 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002479 case TargetLowering::Legal:
2480 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2481 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2482 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002483 case TargetLowering::Expand:
2484 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2485 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2486 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2487 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2488 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002489 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002490 break;
2491 }
2492 break;
2493 }
2494
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002495 case ISD::UREM:
2496 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002497 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002498 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2499 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002500
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002501 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002502 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2503 case TargetLowering::Custom:
2504 isCustom = true;
2505 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002506 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002508 if (isCustom) {
2509 Tmp1 = TLI.LowerOperation(Result, DAG);
2510 if (Tmp1.Val) Result = Tmp1;
2511 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002512 break;
Chris Lattner81914422005-08-03 20:31:37 +00002513 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002514 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002515 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002516 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002517 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2518 TargetLowering::Legal) {
2519 // X % Y -> X-X/Y*Y
2520 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002521 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002522 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2523 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2524 } else {
2525 assert(Node->getValueType(0) == MVT::i32 &&
2526 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002527 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2528 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002529 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002530 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002531 }
Chris Lattner81914422005-08-03 20:31:37 +00002532 } else {
2533 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002534 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2535 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002536 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002537 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2538 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002539 }
2540 break;
2541 }
2542 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002543 case ISD::VAARG: {
2544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2545 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2546
Chris Lattner364b89a2006-01-28 07:42:08 +00002547 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002548 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2549 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002550 case TargetLowering::Custom:
2551 isCustom = true;
2552 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002553 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002554 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2555 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002556 Tmp1 = Result.getValue(1);
2557
2558 if (isCustom) {
2559 Tmp2 = TLI.LowerOperation(Result, DAG);
2560 if (Tmp2.Val) {
2561 Result = LegalizeOp(Tmp2);
2562 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2563 }
2564 }
Nate Begemane74795c2006-01-25 18:21:52 +00002565 break;
2566 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002567 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002568 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002569 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002570 // Increment the pointer, VAList, to the next vaarg
2571 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2572 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2573 TLI.getPointerTy()));
2574 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002575 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2576 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002577 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002578 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002579 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002580 Result = LegalizeOp(Result);
2581 break;
2582 }
2583 }
2584 // Since VAARG produces two values, make sure to remember that we
2585 // legalized both of them.
2586 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002587 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2588 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002589 }
2590
2591 case ISD::VACOPY:
2592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2593 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2594 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2595
2596 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2597 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002598 case TargetLowering::Custom:
2599 isCustom = true;
2600 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002601 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002602 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2603 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002604 if (isCustom) {
2605 Tmp1 = TLI.LowerOperation(Result, DAG);
2606 if (Tmp1.Val) Result = Tmp1;
2607 }
Nate Begemane74795c2006-01-25 18:21:52 +00002608 break;
2609 case TargetLowering::Expand:
2610 // This defaults to loading a pointer from the input and storing it to the
2611 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002612 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002613 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002614 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2615 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002616 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2617 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002618 break;
2619 }
2620 break;
2621
2622 case ISD::VAEND:
2623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2625
2626 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2627 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002628 case TargetLowering::Custom:
2629 isCustom = true;
2630 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002631 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002632 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002633 if (isCustom) {
2634 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2635 if (Tmp1.Val) Result = Tmp1;
2636 }
Nate Begemane74795c2006-01-25 18:21:52 +00002637 break;
2638 case TargetLowering::Expand:
2639 Result = Tmp1; // Default to a no-op, return the chain
2640 break;
2641 }
2642 break;
2643
2644 case ISD::VASTART:
2645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2647
Chris Lattnerd02b0542006-01-28 10:58:55 +00002648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2649
Nate Begemane74795c2006-01-25 18:21:52 +00002650 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2651 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002652 case TargetLowering::Legal: break;
2653 case TargetLowering::Custom:
2654 Tmp1 = TLI.LowerOperation(Result, DAG);
2655 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002656 break;
2657 }
2658 break;
2659
Nate Begeman1b8121b2006-01-11 21:21:00 +00002660 case ISD::ROTL:
2661 case ISD::ROTR:
2662 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2663 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002665 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2666 default:
2667 assert(0 && "ROTL/ROTR legalize operation not supported");
2668 break;
2669 case TargetLowering::Legal:
2670 break;
2671 case TargetLowering::Custom:
2672 Tmp1 = TLI.LowerOperation(Result, DAG);
2673 if (Tmp1.Val) Result = Tmp1;
2674 break;
2675 case TargetLowering::Promote:
2676 assert(0 && "Do not know how to promote ROTL/ROTR");
2677 break;
2678 case TargetLowering::Expand:
2679 assert(0 && "Do not know how to expand ROTL/ROTR");
2680 break;
2681 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00002682 break;
2683
Nate Begeman2fba8a32006-01-14 03:14:10 +00002684 case ISD::BSWAP:
2685 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2686 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002687 case TargetLowering::Custom:
2688 assert(0 && "Cannot custom legalize this yet!");
2689 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002690 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002691 break;
2692 case TargetLowering::Promote: {
2693 MVT::ValueType OVT = Tmp1.getValueType();
2694 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00002695 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002696
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002697 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2698 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2699 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2700 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2701 break;
2702 }
2703 case TargetLowering::Expand:
2704 Result = ExpandBSWAP(Tmp1);
2705 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002706 }
2707 break;
2708
Andrew Lenharth5e177822005-05-03 17:19:30 +00002709 case ISD::CTPOP:
2710 case ISD::CTTZ:
2711 case ISD::CTLZ:
2712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2713 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002714 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002715 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002716 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002717 break;
2718 case TargetLowering::Promote: {
2719 MVT::ValueType OVT = Tmp1.getValueType();
2720 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002721
2722 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002723 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2724 // Perform the larger operation, then subtract if needed.
2725 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002726 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002727 case ISD::CTPOP:
2728 Result = Tmp1;
2729 break;
2730 case ISD::CTTZ:
2731 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002732 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002733 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00002734 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002735 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002736 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002737 break;
2738 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002739 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002740 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002741 DAG.getConstant(MVT::getSizeInBits(NVT) -
2742 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00002743 break;
2744 }
2745 break;
2746 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002747 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002748 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002749 break;
2750 }
2751 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002752
Chris Lattner13fe99c2005-04-02 05:00:07 +00002753 // Unary operators
2754 case ISD::FABS:
2755 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002756 case ISD::FSQRT:
2757 case ISD::FSIN:
2758 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002759 Tmp1 = LegalizeOp(Node->getOperand(0));
2760 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002761 case TargetLowering::Promote:
2762 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002763 isCustom = true;
2764 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002765 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002766 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002767 if (isCustom) {
2768 Tmp1 = TLI.LowerOperation(Result, DAG);
2769 if (Tmp1.Val) Result = Tmp1;
2770 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002771 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002772 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002773 switch (Node->getOpcode()) {
2774 default: assert(0 && "Unreachable!");
2775 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002776 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2777 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002778 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002779 break;
Chris Lattner80026402005-04-30 04:43:14 +00002780 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002781 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2782 MVT::ValueType VT = Node->getValueType(0);
2783 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002784 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002785 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2786 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002787 break;
2788 }
2789 case ISD::FSQRT:
2790 case ISD::FSIN:
2791 case ISD::FCOS: {
2792 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002793 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002794 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002795 case ISD::FSQRT:
2796 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2797 break;
2798 case ISD::FSIN:
2799 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2800 break;
2801 case ISD::FCOS:
2802 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2803 break;
Chris Lattner80026402005-04-30 04:43:14 +00002804 default: assert(0 && "Unreachable!");
2805 }
Nate Begeman77558da2005-08-04 21:43:28 +00002806 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002807 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2808 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002809 break;
2810 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002811 }
2812 break;
2813 }
2814 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002815 case ISD::FPOWI: {
2816 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002817 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2818 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002819 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002820 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2821 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002822 break;
2823 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002824 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002825 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002826 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00002827 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2828 // The input has to be a vector type, we have to either scalarize it, pack
2829 // it, or convert it based on whether the input vector type is legal.
2830 SDNode *InVal = Node->getOperand(0).Val;
2831 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2832 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
2833
2834 // Figure out if there is a simple type corresponding to this Vector
2835 // type. If so, convert to the vector type.
2836 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2837 if (TLI.isTypeLegal(TVT)) {
2838 // Turn this into a bit convert of the packed input.
2839 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2840 LegalizeOp(Node->getOperand(0)));
2841 break;
2842 } else if (NumElems == 1) {
2843 // Turn this into a bit convert of the scalar input.
2844 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2845 ScalarizeVectorOp(Node->getOperand(0)));
2846 break;
2847 } else {
2848 // FIXME: UNIMP! Store then reload
2849 assert(0 && "Cast from unsupported vector type not implemented yet!");
2850 }
Chris Lattner763dfd72006-01-23 07:30:46 +00002851 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002852 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2853 Node->getOperand(0).getValueType())) {
2854 default: assert(0 && "Unknown operation action!");
2855 case TargetLowering::Expand:
2856 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2857 break;
2858 case TargetLowering::Legal:
2859 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002861 break;
2862 }
2863 }
2864 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002865
Chris Lattner13fe99c2005-04-02 05:00:07 +00002866 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002867 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002868 case ISD::UINT_TO_FP: {
2869 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2871 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002872 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002873 Node->getOperand(0).getValueType())) {
2874 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002875 case TargetLowering::Custom:
2876 isCustom = true;
2877 // FALLTHROUGH
2878 case TargetLowering::Legal:
2879 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002880 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002881 if (isCustom) {
2882 Tmp1 = TLI.LowerOperation(Result, DAG);
2883 if (Tmp1.Val) Result = Tmp1;
2884 }
2885 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002886 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002887 Result = ExpandLegalINT_TO_FP(isSigned,
2888 LegalizeOp(Node->getOperand(0)),
2889 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002890 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002891 case TargetLowering::Promote:
2892 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2893 Node->getValueType(0),
2894 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002895 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002896 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002897 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002898 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002899 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2900 Node->getValueType(0), Node->getOperand(0));
2901 break;
2902 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002903 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002904 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002905 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2906 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002907 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002908 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2909 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002910 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002911 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2912 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002913 break;
2914 }
2915 break;
2916 }
2917 case ISD::TRUNCATE:
2918 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2919 case Legal:
2920 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002921 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002922 break;
2923 case Expand:
2924 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2925
2926 // Since the result is legal, we should just be able to truncate the low
2927 // part of the source.
2928 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2929 break;
2930 case Promote:
2931 Result = PromoteOp(Node->getOperand(0));
2932 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2933 break;
2934 }
2935 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002936
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002937 case ISD::FP_TO_SINT:
2938 case ISD::FP_TO_UINT:
2939 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2940 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002941 Tmp1 = LegalizeOp(Node->getOperand(0));
2942
Chris Lattner44fe26f2005-07-29 00:11:56 +00002943 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2944 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002945 case TargetLowering::Custom:
2946 isCustom = true;
2947 // FALLTHROUGH
2948 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002949 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002950 if (isCustom) {
2951 Tmp1 = TLI.LowerOperation(Result, DAG);
2952 if (Tmp1.Val) Result = Tmp1;
2953 }
2954 break;
2955 case TargetLowering::Promote:
2956 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2957 Node->getOpcode() == ISD::FP_TO_SINT);
2958 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002959 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002960 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2961 SDOperand True, False;
2962 MVT::ValueType VT = Node->getOperand(0).getValueType();
2963 MVT::ValueType NVT = Node->getValueType(0);
2964 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2965 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2966 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2967 Node->getOperand(0), Tmp2, ISD::SETLT);
2968 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2969 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002970 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002971 Tmp2));
2972 False = DAG.getNode(ISD::XOR, NVT, False,
2973 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002974 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2975 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002976 } else {
2977 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2978 }
2979 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002980 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002981 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002982 case Expand: {
2983 // Convert f32 / f64 to i32 / i64.
2984 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00002985 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002986 switch (Node->getOpcode()) {
2987 case ISD::FP_TO_SINT:
2988 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002989 LC = (VT == MVT::i32)
2990 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002991 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00002992 LC = (VT == MVT::i32)
2993 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002994 break;
2995 case ISD::FP_TO_UINT:
2996 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002997 LC = (VT == MVT::i32)
2998 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002999 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003000 LC = (VT == MVT::i32)
3001 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003002 break;
3003 default: assert(0 && "Unreachable!");
3004 }
3005 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003006 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3007 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003008 break;
3009 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003010 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003011 Tmp1 = PromoteOp(Node->getOperand(0));
3012 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3013 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003014 break;
3015 }
3016 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003017
Chris Lattner7753f172005-09-02 00:18:10 +00003018 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003019 case ISD::ZERO_EXTEND:
3020 case ISD::SIGN_EXTEND:
3021 case ISD::FP_EXTEND:
3022 case ISD::FP_ROUND:
3023 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003024 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003025 case Legal:
3026 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003027 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003028 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003029 case Promote:
3030 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003031 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003032 Tmp1 = PromoteOp(Node->getOperand(0));
3033 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003034 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003035 case ISD::ZERO_EXTEND:
3036 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003037 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003038 Result = DAG.getZeroExtendInReg(Result,
3039 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003040 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003041 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003042 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003043 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003044 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003045 Result,
3046 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003047 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003048 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003049 Result = PromoteOp(Node->getOperand(0));
3050 if (Result.getValueType() != Op.getValueType())
3051 // Dynamically dead while we have only 2 FP types.
3052 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3053 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003054 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003055 Result = PromoteOp(Node->getOperand(0));
3056 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3057 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003058 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003059 }
3060 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003061 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003062 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003063 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003064 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003065
3066 // If this operation is not supported, convert it to a shl/shr or load/store
3067 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003068 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3069 default: assert(0 && "This action not supported for this op yet!");
3070 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003072 break;
3073 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003074 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003075 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003076 // NOTE: we could fall back on load/store here too for targets without
3077 // SAR. However, it is doubtful that any exist.
3078 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3079 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003080 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003081 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3082 Node->getOperand(0), ShiftCst);
3083 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3084 Result, ShiftCst);
3085 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003086 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003087 // EXTLOAD pair, targetting a temporary location (a stack slot).
3088
3089 // NOTE: there is a choice here between constantly creating new stack
3090 // slots and always reusing the same one. We currently always create
3091 // new ones, as reuse may inhibit scheduling.
3092 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003093 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003094 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003095 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003096 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003097 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003098 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003099 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3100 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003101 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003102 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003103 } else {
3104 assert(0 && "Unknown op");
3105 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003106 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003107 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003108 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003109 }
Chris Lattner99222f72005-01-15 07:15:18 +00003110 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003111
Chris Lattner101ea662006-04-08 04:13:17 +00003112 assert(Result.getValueType() == Op.getValueType() &&
3113 "Bad legalization!");
3114
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003115 // Make sure that the generated code is itself legal.
3116 if (Result != Op)
3117 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003118
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003119 // Note that LegalizeOp may be reentered even from single-use nodes, which
3120 // means that we always must cache transformed nodes.
3121 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003122 return Result;
3123}
3124
Chris Lattner4d978642005-01-15 22:16:26 +00003125/// PromoteOp - Given an operation that produces a value in an invalid type,
3126/// promote it to compute the value into a larger type. The produced value will
3127/// have the correct bits for the low portion of the register, but no guarantee
3128/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003129SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3130 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003131 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003132 assert(getTypeAction(VT) == Promote &&
3133 "Caller should expand or legalize operands that are not promotable!");
3134 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3135 "Cannot promote to smaller type!");
3136
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003137 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003138 SDOperand Result;
3139 SDNode *Node = Op.Val;
3140
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003141 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003142 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003143
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003144 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003145 case ISD::CopyFromReg:
3146 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003147 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003148#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003149 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003150#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003151 assert(0 && "Do not know how to promote this operator!");
3152 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003153 case ISD::UNDEF:
3154 Result = DAG.getNode(ISD::UNDEF, NVT);
3155 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003156 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003157 if (VT != MVT::i1)
3158 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3159 else
3160 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003161 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3162 break;
3163 case ISD::ConstantFP:
3164 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3165 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3166 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003167
Chris Lattner2cb338d2005-01-18 02:59:52 +00003168 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003169 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003170 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3171 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003172 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003173
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003174 case ISD::TRUNCATE:
3175 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3176 case Legal:
3177 Result = LegalizeOp(Node->getOperand(0));
3178 assert(Result.getValueType() >= NVT &&
3179 "This truncation doesn't make sense!");
3180 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3181 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3182 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003183 case Promote:
3184 // The truncation is not required, because we don't guarantee anything
3185 // about high bits anyway.
3186 Result = PromoteOp(Node->getOperand(0));
3187 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003188 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003189 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3190 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003191 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003192 }
3193 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003194 case ISD::SIGN_EXTEND:
3195 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003196 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003197 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3198 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3199 case Legal:
3200 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003201 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003202 break;
3203 case Promote:
3204 // Promote the reg if it's smaller.
3205 Result = PromoteOp(Node->getOperand(0));
3206 // The high bits are not guaranteed to be anything. Insert an extend.
3207 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003208 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003209 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003210 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003211 Result = DAG.getZeroExtendInReg(Result,
3212 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003213 break;
3214 }
3215 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003216 case ISD::BIT_CONVERT:
3217 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3218 Result = PromoteOp(Result);
3219 break;
3220
Chris Lattner4d978642005-01-15 22:16:26 +00003221 case ISD::FP_EXTEND:
3222 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3223 case ISD::FP_ROUND:
3224 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3225 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3226 case Promote: assert(0 && "Unreachable with 2 FP types!");
3227 case Legal:
3228 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003229 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003230 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003231 break;
3232 }
3233 break;
3234
3235 case ISD::SINT_TO_FP:
3236 case ISD::UINT_TO_FP:
3237 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3238 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003239 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003240 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003241 break;
3242
3243 case Promote:
3244 Result = PromoteOp(Node->getOperand(0));
3245 if (Node->getOpcode() == ISD::SINT_TO_FP)
3246 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003247 Result,
3248 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003249 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003250 Result = DAG.getZeroExtendInReg(Result,
3251 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003252 // No extra round required here.
3253 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003254 break;
3255 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003256 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3257 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003258 // Round if we cannot tolerate excess precision.
3259 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003260 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3261 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003262 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003263 }
Chris Lattner4d978642005-01-15 22:16:26 +00003264 break;
3265
Chris Lattner268d4572005-12-09 17:32:47 +00003266 case ISD::SIGN_EXTEND_INREG:
3267 Result = PromoteOp(Node->getOperand(0));
3268 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3269 Node->getOperand(1));
3270 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003271 case ISD::FP_TO_SINT:
3272 case ISD::FP_TO_UINT:
3273 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3274 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003275 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003276 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003277 break;
3278 case Promote:
3279 // The input result is prerounded, so we don't have to do anything
3280 // special.
3281 Tmp1 = PromoteOp(Node->getOperand(0));
3282 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003283 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003284 // If we're promoting a UINT to a larger size, check to see if the new node
3285 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3286 // we can use that instead. This allows us to generate better code for
3287 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3288 // legal, such as PowerPC.
3289 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003290 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003291 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3292 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003293 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3294 } else {
3295 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3296 }
Chris Lattner4d978642005-01-15 22:16:26 +00003297 break;
3298
Chris Lattner13fe99c2005-04-02 05:00:07 +00003299 case ISD::FABS:
3300 case ISD::FNEG:
3301 Tmp1 = PromoteOp(Node->getOperand(0));
3302 assert(Tmp1.getValueType() == NVT);
3303 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3304 // NOTE: we do not have to do any extra rounding here for
3305 // NoExcessFPPrecision, because we know the input will have the appropriate
3306 // precision, and these operations don't modify precision at all.
3307 break;
3308
Chris Lattner9d6fa982005-04-28 21:44:33 +00003309 case ISD::FSQRT:
3310 case ISD::FSIN:
3311 case ISD::FCOS:
3312 Tmp1 = PromoteOp(Node->getOperand(0));
3313 assert(Tmp1.getValueType() == NVT);
3314 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003315 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003316 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3317 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003318 break;
3319
Chris Lattnerca401aa2007-03-03 23:43:21 +00003320 case ISD::FPOWI: {
3321 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3322 // directly as well, which may be better.
3323 Tmp1 = PromoteOp(Node->getOperand(0));
3324 assert(Tmp1.getValueType() == NVT);
3325 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3326 if (NoExcessFPPrecision)
3327 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3328 DAG.getValueType(VT));
3329 break;
3330 }
3331
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003332 case ISD::AND:
3333 case ISD::OR:
3334 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003335 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003336 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003337 case ISD::MUL:
3338 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003339 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003340 // that too is okay if they are integer operations.
3341 Tmp1 = PromoteOp(Node->getOperand(0));
3342 Tmp2 = PromoteOp(Node->getOperand(1));
3343 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3344 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003345 break;
3346 case ISD::FADD:
3347 case ISD::FSUB:
3348 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003349 Tmp1 = PromoteOp(Node->getOperand(0));
3350 Tmp2 = PromoteOp(Node->getOperand(1));
3351 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3352 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3353
3354 // Floating point operations will give excess precision that we may not be
3355 // able to tolerate. If we DO allow excess precision, just leave it,
3356 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003357 // FIXME: Why would we need to round FP ops more than integer ones?
3358 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003359 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003360 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3361 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003362 break;
3363
Chris Lattner4d978642005-01-15 22:16:26 +00003364 case ISD::SDIV:
3365 case ISD::SREM:
3366 // These operators require that their input be sign extended.
3367 Tmp1 = PromoteOp(Node->getOperand(0));
3368 Tmp2 = PromoteOp(Node->getOperand(1));
3369 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003370 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3371 DAG.getValueType(VT));
3372 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3373 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003374 }
3375 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3376
3377 // Perform FP_ROUND: this is probably overly pessimistic.
3378 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003379 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3380 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003381 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003382 case ISD::FDIV:
3383 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003384 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003385 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003386 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3387 case Legal:
3388 Tmp1 = LegalizeOp(Node->getOperand(0));
3389 break;
3390 case Promote:
3391 Tmp1 = PromoteOp(Node->getOperand(0));
3392 break;
3393 case Expand:
3394 assert(0 && "not implemented");
3395 }
3396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3397 case Legal:
3398 Tmp2 = LegalizeOp(Node->getOperand(1));
3399 break;
3400 case Promote:
3401 Tmp2 = PromoteOp(Node->getOperand(1));
3402 break;
3403 case Expand:
3404 assert(0 && "not implemented");
3405 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003406 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3407
3408 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003409 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003410 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3411 DAG.getValueType(VT));
3412 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003413
3414 case ISD::UDIV:
3415 case ISD::UREM:
3416 // These operators require that their input be zero extended.
3417 Tmp1 = PromoteOp(Node->getOperand(0));
3418 Tmp2 = PromoteOp(Node->getOperand(1));
3419 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003420 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3421 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003422 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3423 break;
3424
3425 case ISD::SHL:
3426 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003427 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003428 break;
3429 case ISD::SRA:
3430 // The input value must be properly sign extended.
3431 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003432 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3433 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003434 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003435 break;
3436 case ISD::SRL:
3437 // The input value must be properly zero extended.
3438 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003439 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003440 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003441 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003442
3443 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003444 Tmp1 = Node->getOperand(0); // Get the chain.
3445 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003446 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3447 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3448 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3449 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003450 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003451 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003452 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003453 // Increment the pointer, VAList, to the next vaarg
3454 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3455 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3456 TLI.getPointerTy()));
3457 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003458 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3459 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003460 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003461 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003462 }
3463 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003464 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003465 break;
3466
Evan Chenge71fe34d2006-10-09 20:57:25 +00003467 case ISD::LOAD: {
3468 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003469 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3470 ? ISD::EXTLOAD : LD->getExtensionType();
3471 Result = DAG.getExtLoad(ExtType, NVT,
3472 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003473 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengd35734b2006-10-11 07:10:22 +00003474 LD->getLoadedVT());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003475 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003476 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003477 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003478 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003479 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003480 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3481 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003482 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003483 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003484 case ISD::SELECT_CC:
3485 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3486 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3487 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003488 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003489 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003490 case ISD::BSWAP:
3491 Tmp1 = Node->getOperand(0);
3492 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3493 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3494 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003495 DAG.getConstant(MVT::getSizeInBits(NVT) -
3496 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003497 TLI.getShiftAmountTy()));
3498 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003499 case ISD::CTPOP:
3500 case ISD::CTTZ:
3501 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003502 // Zero extend the argument
3503 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003504 // Perform the larger operation, then subtract if needed.
3505 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003506 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003507 case ISD::CTPOP:
3508 Result = Tmp1;
3509 break;
3510 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003511 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003512 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003513 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3514 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003515 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003516 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003517 break;
3518 case ISD::CTLZ:
3519 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003520 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003521 DAG.getConstant(MVT::getSizeInBits(NVT) -
3522 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003523 break;
3524 }
3525 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003526 case ISD::EXTRACT_SUBVECTOR:
3527 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003528 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003529 case ISD::EXTRACT_VECTOR_ELT:
3530 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3531 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003532 }
3533
3534 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003535
3536 // Make sure the result is itself legal.
3537 Result = LegalizeOp(Result);
3538
3539 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003540 AddPromotedOperand(Op, Result);
3541 return Result;
3542}
Chris Lattnerdc750592005-01-07 07:47:09 +00003543
Dan Gohmana8665142007-06-25 16:23:39 +00003544/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3545/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3546/// based on the vector type. The return type of this matches the element type
3547/// of the vector, which may not be legal for the target.
3548SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003549 // We know that operand #0 is the Vec vector. If the index is a constant
3550 // or if the invec is a supported hardware type, we can use it. Otherwise,
3551 // lower to a store then an indexed load.
3552 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003553 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003554
3555 SDNode *InVal = Vec.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00003556 MVT::ValueType TVT = InVal->getValueType(0);
3557 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003558
Dan Gohmana8665142007-06-25 16:23:39 +00003559 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3560 default: assert(0 && "This action is not supported yet!");
3561 case TargetLowering::Custom: {
3562 Vec = LegalizeOp(Vec);
3563 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3564 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3565 if (Tmp3.Val)
3566 return Tmp3;
3567 break;
3568 }
3569 case TargetLowering::Legal:
3570 if (isTypeLegal(TVT)) {
3571 Vec = LegalizeOp(Vec);
3572 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3573 Op = LegalizeOp(Op);
3574 }
3575 break;
3576 case TargetLowering::Expand:
3577 break;
3578 }
3579
3580 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00003581 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003582 Op = ScalarizeVectorOp(Vec);
3583 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3584 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00003585 SDOperand Lo, Hi;
3586 SplitVectorOp(Vec, Lo, Hi);
3587 if (CIdx->getValue() < NumElems/2) {
3588 Vec = Lo;
3589 } else {
3590 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00003591 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3592 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00003593 }
Dan Gohmana8665142007-06-25 16:23:39 +00003594
Chris Lattner6f423252006-03-31 17:55:51 +00003595 // It's now an extract from the appropriate high or low part. Recurse.
3596 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003597 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00003598 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00003599 // Store the value to a temporary stack slot, then LOAD the scalar
3600 // element back out.
3601 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3602 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3603
3604 // Add the offset to the index.
3605 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3606 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3607 DAG.getConstant(EltSize, Idx.getValueType()));
3608 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3609
3610 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00003611 }
Dan Gohmana8665142007-06-25 16:23:39 +00003612 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00003613}
3614
Dan Gohmana8665142007-06-25 16:23:39 +00003615/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00003616/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00003617SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00003618 // We know that operand #0 is the Vec vector. For now we assume the index
3619 // is a constant and that the extracted result is a supported hardware type.
3620 SDOperand Vec = Op.getOperand(0);
3621 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3622
Dan Gohmana8665142007-06-25 16:23:39 +00003623 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00003624
3625 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3626 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003627 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00003628 }
3629
3630 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3631 SDOperand Lo, Hi;
3632 SplitVectorOp(Vec, Lo, Hi);
3633 if (CIdx->getValue() < NumElems/2) {
3634 Vec = Lo;
3635 } else {
3636 Vec = Hi;
3637 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3638 }
3639
3640 // It's now an extract from the appropriate high or low part. Recurse.
3641 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003642 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00003643}
3644
Nate Begeman7e7f4392006-02-01 07:19:44 +00003645/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3646/// with condition CC on the current target. This usually involves legalizing
3647/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3648/// there may be no choice but to create a new SetCC node to represent the
3649/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3650/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3651void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3652 SDOperand &RHS,
3653 SDOperand &CC) {
3654 SDOperand Tmp1, Tmp2, Result;
3655
3656 switch (getTypeAction(LHS.getValueType())) {
3657 case Legal:
3658 Tmp1 = LegalizeOp(LHS); // LHS
3659 Tmp2 = LegalizeOp(RHS); // RHS
3660 break;
3661 case Promote:
3662 Tmp1 = PromoteOp(LHS); // LHS
3663 Tmp2 = PromoteOp(RHS); // RHS
3664
3665 // If this is an FP compare, the operands have already been extended.
3666 if (MVT::isInteger(LHS.getValueType())) {
3667 MVT::ValueType VT = LHS.getValueType();
3668 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3669
3670 // Otherwise, we have to insert explicit sign or zero extends. Note
3671 // that we could insert sign extends for ALL conditions, but zero extend
3672 // is cheaper on many machines (an AND instead of two shifts), so prefer
3673 // it.
3674 switch (cast<CondCodeSDNode>(CC)->get()) {
3675 default: assert(0 && "Unknown integer comparison!");
3676 case ISD::SETEQ:
3677 case ISD::SETNE:
3678 case ISD::SETUGE:
3679 case ISD::SETUGT:
3680 case ISD::SETULE:
3681 case ISD::SETULT:
3682 // ALL of these operations will work if we either sign or zero extend
3683 // the operands (including the unsigned comparisons!). Zero extend is
3684 // usually a simpler/cheaper operation, so prefer it.
3685 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3686 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3687 break;
3688 case ISD::SETGE:
3689 case ISD::SETGT:
3690 case ISD::SETLT:
3691 case ISD::SETLE:
3692 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3693 DAG.getValueType(VT));
3694 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3695 DAG.getValueType(VT));
3696 break;
3697 }
3698 }
3699 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003700 case Expand: {
3701 MVT::ValueType VT = LHS.getValueType();
3702 if (VT == MVT::f32 || VT == MVT::f64) {
3703 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003704 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003705 switch (cast<CondCodeSDNode>(CC)->get()) {
3706 case ISD::SETEQ:
3707 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003708 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003709 break;
3710 case ISD::SETNE:
3711 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003712 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003713 break;
3714 case ISD::SETGE:
3715 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003716 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003717 break;
3718 case ISD::SETLT:
3719 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003720 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003721 break;
3722 case ISD::SETLE:
3723 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003724 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003725 break;
3726 case ISD::SETGT:
3727 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003728 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003729 break;
3730 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003731 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3732 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003733 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003734 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003735 break;
3736 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003737 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003738 switch (cast<CondCodeSDNode>(CC)->get()) {
3739 case ISD::SETONE:
3740 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003741 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003742 // Fallthrough
3743 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003744 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003745 break;
3746 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003747 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003748 break;
3749 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003750 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003751 break;
3752 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003753 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003754 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003755 case ISD::SETUEQ:
3756 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003757 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003758 default: assert(0 && "Unsupported FP setcc!");
3759 }
3760 }
3761
3762 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003763 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00003764 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003765 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003766 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00003767 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00003768 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003769 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003770 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00003771 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003772 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003773 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00003774 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003775 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3776 Tmp2 = SDOperand();
3777 }
3778 LHS = Tmp1;
3779 RHS = Tmp2;
3780 return;
3781 }
3782
Nate Begeman7e7f4392006-02-01 07:19:44 +00003783 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3784 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003785 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003786 switch (cast<CondCodeSDNode>(CC)->get()) {
3787 case ISD::SETEQ:
3788 case ISD::SETNE:
3789 if (RHSLo == RHSHi)
3790 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3791 if (RHSCST->isAllOnesValue()) {
3792 // Comparison to -1.
3793 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3794 Tmp2 = RHSLo;
3795 break;
3796 }
3797
3798 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3799 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3800 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3801 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3802 break;
3803 default:
3804 // If this is a comparison of the sign bit, just look at the top part.
3805 // X > -1, x < 0
3806 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3807 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3808 CST->getValue() == 0) || // X < 0
3809 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3810 CST->isAllOnesValue())) { // X > -1
3811 Tmp1 = LHSHi;
3812 Tmp2 = RHSHi;
3813 break;
3814 }
3815
3816 // FIXME: This generated code sucks.
3817 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00003818 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3819 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003820 default: assert(0 && "Unknown integer setcc!");
3821 case ISD::SETLT:
3822 case ISD::SETULT: LowCC = ISD::SETULT; break;
3823 case ISD::SETGT:
3824 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3825 case ISD::SETLE:
3826 case ISD::SETULE: LowCC = ISD::SETULE; break;
3827 case ISD::SETGE:
3828 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3829 }
3830
3831 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3832 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3833 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3834
3835 // NOTE: on targets without efficient SELECT of bools, we can always use
3836 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00003837 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3838 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3839 false, DagCombineInfo);
3840 if (!Tmp1.Val)
3841 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3842 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3843 CCCode, false, DagCombineInfo);
3844 if (!Tmp2.Val)
3845 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3846
3847 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3848 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3849 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3850 (Tmp2C && Tmp2C->getValue() == 0 &&
3851 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3852 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3853 (Tmp2C && Tmp2C->getValue() == 1 &&
3854 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3855 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3856 // low part is known false, returns high part.
3857 // For LE / GE, if high part is known false, ignore the low part.
3858 // For LT / GT, if high part is known true, ignore the low part.
3859 Tmp1 = Tmp2;
3860 Tmp2 = SDOperand();
3861 } else {
3862 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3863 ISD::SETEQ, false, DagCombineInfo);
3864 if (!Result.Val)
3865 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3866 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3867 Result, Tmp1, Tmp2));
3868 Tmp1 = Result;
3869 Tmp2 = SDOperand();
3870 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003871 }
3872 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003873 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003874 LHS = Tmp1;
3875 RHS = Tmp2;
3876}
3877
Chris Lattner36e663d2005-12-23 00:16:34 +00003878/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003879/// The resultant code need not be legal. Note that SrcOp is the input operand
3880/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003881SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3882 SDOperand SrcOp) {
3883 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003884 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003885
3886 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003887 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003888 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003889 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003890}
3891
Chris Lattner6be79822006-04-04 17:23:26 +00003892SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3893 // Create a vector sized/aligned stack slot, store the value to element #0,
3894 // then load the whole vector back out.
3895 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003896 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003897 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003898 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003899}
3900
3901
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003902/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3903/// support the operation, but do support the resultant packed vector type.
3904SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3905
3906 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003907 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003908 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003909 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003910 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003911 std::map<SDOperand, std::vector<unsigned> > Values;
3912 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003913 bool isConstant = true;
3914 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3915 SplatValue.getOpcode() != ISD::UNDEF)
3916 isConstant = false;
3917
Evan Cheng1d2e9952006-03-24 01:17:21 +00003918 for (unsigned i = 1; i < NumElems; ++i) {
3919 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003920 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003921 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003922 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003923 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003924 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003925
3926 // If this isn't a constant element or an undef, we can't use a constant
3927 // pool load.
3928 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3929 V.getOpcode() != ISD::UNDEF)
3930 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003931 }
3932
3933 if (isOnlyLowElement) {
3934 // If the low element is an undef too, then this whole things is an undef.
3935 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3936 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3937 // Otherwise, turn this into a scalar_to_vector node.
3938 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3939 Node->getOperand(0));
3940 }
3941
Chris Lattner77e271c2006-03-24 07:29:17 +00003942 // If all elements are constants, create a load from the constant pool.
3943 if (isConstant) {
3944 MVT::ValueType VT = Node->getValueType(0);
3945 const Type *OpNTy =
3946 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3947 std::vector<Constant*> CV;
3948 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3949 if (ConstantFPSDNode *V =
3950 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3951 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3952 } else if (ConstantSDNode *V =
3953 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003954 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003955 } else {
3956 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3957 CV.push_back(UndefValue::get(OpNTy));
3958 }
3959 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00003960 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00003961 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003962 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003963 }
3964
Chris Lattner21e68c82006-03-20 01:52:29 +00003965 if (SplatValue.Val) { // Splat of one value?
3966 // Build the shuffle constant vector: <0, 0, 0, 0>
3967 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003968 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00003969 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003970 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003971 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3972 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00003973
3974 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003975 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003976 // Get the splatted value into the low element of a vector register.
3977 SDOperand LowValVec =
3978 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3979
3980 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3981 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3982 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3983 SplatMask);
3984 }
3985 }
3986
Evan Cheng1d2e9952006-03-24 01:17:21 +00003987 // If there are only two unique elements, we may be able to turn this into a
3988 // vector shuffle.
3989 if (Values.size() == 2) {
3990 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3991 MVT::ValueType MaskVT =
3992 MVT::getIntVectorWithNumElements(NumElems);
3993 std::vector<SDOperand> MaskVec(NumElems);
3994 unsigned i = 0;
3995 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3996 E = Values.end(); I != E; ++I) {
3997 for (std::vector<unsigned>::iterator II = I->second.begin(),
3998 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00003999 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004000 i += NumElems;
4001 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004002 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4003 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004004
4005 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004006 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4007 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004008 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004009 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4010 E = Values.end(); I != E; ++I) {
4011 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4012 I->first);
4013 Ops.push_back(Op);
4014 }
4015 Ops.push_back(ShuffleMask);
4016
4017 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004018 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4019 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004020 }
4021 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004022
4023 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4024 // aligned object on the stack, store each element into it, then load
4025 // the result as a vector.
4026 MVT::ValueType VT = Node->getValueType(0);
4027 // Create the stack frame object.
4028 SDOperand FIPtr = CreateStackTemporary(VT);
4029
4030 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004031 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004032 unsigned TypeByteSize =
4033 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004034 // Store (in the right endianness) the elements to memory.
4035 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4036 // Ignore undef elements.
4037 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4038
Chris Lattner8fa445a2006-03-22 01:46:54 +00004039 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004040
4041 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4042 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4043
Evan Chengdf9ac472006-10-05 23:01:46 +00004044 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004045 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004046 }
4047
4048 SDOperand StoreChain;
4049 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004050 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4051 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004052 else
4053 StoreChain = DAG.getEntryNode();
4054
4055 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004056 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004057}
4058
4059/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4060/// specified value type.
4061SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4062 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4063 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004064 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004065 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004066 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004067 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4068}
4069
Chris Lattner4157c412005-04-02 04:00:59 +00004070void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4071 SDOperand Op, SDOperand Amt,
4072 SDOperand &Lo, SDOperand &Hi) {
4073 // Expand the subcomponents.
4074 SDOperand LHSL, LHSH;
4075 ExpandOp(Op, LHSL, LHSH);
4076
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004077 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004078 MVT::ValueType VT = LHSL.getValueType();
4079 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004080 Hi = Lo.getValue(1);
4081}
4082
4083
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004084/// ExpandShift - Try to find a clever way to expand this shift operation out to
4085/// smaller elements. If we can't find a way that is more efficient than a
4086/// libcall on this target, return false. Otherwise, return true with the
4087/// low-parts expanded into Lo and Hi.
4088bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4089 SDOperand &Lo, SDOperand &Hi) {
4090 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4091 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004092
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004093 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004094 SDOperand ShAmt = LegalizeOp(Amt);
4095 MVT::ValueType ShTy = ShAmt.getValueType();
4096 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4097 unsigned NVTBits = MVT::getSizeInBits(NVT);
4098
4099 // Handle the case when Amt is an immediate. Other cases are currently broken
4100 // and are disabled.
4101 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4102 unsigned Cst = CN->getValue();
4103 // Expand the incoming operand to be shifted, so that we have its parts
4104 SDOperand InL, InH;
4105 ExpandOp(Op, InL, InH);
4106 switch(Opc) {
4107 case ISD::SHL:
4108 if (Cst > VTBits) {
4109 Lo = DAG.getConstant(0, NVT);
4110 Hi = DAG.getConstant(0, NVT);
4111 } else if (Cst > NVTBits) {
4112 Lo = DAG.getConstant(0, NVT);
4113 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004114 } else if (Cst == NVTBits) {
4115 Lo = DAG.getConstant(0, NVT);
4116 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004117 } else {
4118 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4119 Hi = DAG.getNode(ISD::OR, NVT,
4120 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4121 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4122 }
4123 return true;
4124 case ISD::SRL:
4125 if (Cst > VTBits) {
4126 Lo = DAG.getConstant(0, NVT);
4127 Hi = DAG.getConstant(0, NVT);
4128 } else if (Cst > NVTBits) {
4129 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4130 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004131 } else if (Cst == NVTBits) {
4132 Lo = InH;
4133 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004134 } else {
4135 Lo = DAG.getNode(ISD::OR, NVT,
4136 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4137 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4138 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4139 }
4140 return true;
4141 case ISD::SRA:
4142 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004143 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004144 DAG.getConstant(NVTBits-1, ShTy));
4145 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004146 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004147 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004148 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004149 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004150 } else if (Cst == NVTBits) {
4151 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004152 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004153 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004154 } else {
4155 Lo = DAG.getNode(ISD::OR, NVT,
4156 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4157 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4158 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4159 }
4160 return true;
4161 }
4162 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004163
4164 // Okay, the shift amount isn't constant. However, if we can tell that it is
4165 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4166 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004167 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004168
4169 // If we know that the high bit of the shift amount is one, then we can do
4170 // this as a couple of simple shifts.
4171 if (KnownOne & Mask) {
4172 // Mask out the high bit, which we know is set.
4173 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4174 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4175
4176 // Expand the incoming operand to be shifted, so that we have its parts
4177 SDOperand InL, InH;
4178 ExpandOp(Op, InL, InH);
4179 switch(Opc) {
4180 case ISD::SHL:
4181 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4182 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4183 return true;
4184 case ISD::SRL:
4185 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4186 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4187 return true;
4188 case ISD::SRA:
4189 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4190 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4191 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4192 return true;
4193 }
4194 }
4195
4196 // If we know that the high bit of the shift amount is zero, then we can do
4197 // this as a couple of simple shifts.
4198 if (KnownZero & Mask) {
4199 // Compute 32-amt.
4200 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4201 DAG.getConstant(NVTBits, Amt.getValueType()),
4202 Amt);
4203
4204 // Expand the incoming operand to be shifted, so that we have its parts
4205 SDOperand InL, InH;
4206 ExpandOp(Op, InL, InH);
4207 switch(Opc) {
4208 case ISD::SHL:
4209 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4210 Hi = DAG.getNode(ISD::OR, NVT,
4211 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4212 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4213 return true;
4214 case ISD::SRL:
4215 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4216 Lo = DAG.getNode(ISD::OR, NVT,
4217 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4218 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4219 return true;
4220 case ISD::SRA:
4221 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4222 Lo = DAG.getNode(ISD::OR, NVT,
4223 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4224 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4225 return true;
4226 }
4227 }
4228
Nate Begemanb0674922005-04-06 21:13:14 +00004229 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004230}
Chris Lattneraac464e2005-01-21 06:05:23 +00004231
Chris Lattner4add7e32005-01-23 04:42:50 +00004232
Chris Lattneraac464e2005-01-21 06:05:23 +00004233// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4234// does not fit into a register, return the lo part and set the hi part to the
4235// by-reg argument. If it does fit into a single register, return the result
4236// and leave the Hi part unset.
4237SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004238 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004239 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4240 // The input chain to this libcall is the entry node of the function.
4241 // Legalizing the call will automatically add the previous call to the
4242 // dependence.
4243 SDOperand InChain = DAG.getEntryNode();
4244
Chris Lattneraac464e2005-01-21 06:05:23 +00004245 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004246 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004247 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4248 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4249 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004250 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004251 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004252 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004253 }
4254 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004255
Chris Lattner06bbeb62005-05-11 19:02:11 +00004256 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004257 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004258 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004259 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004260 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004261
Chris Lattner462505f2006-02-13 09:18:02 +00004262 // Legalize the call sequence, starting with the chain. This will advance
4263 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4264 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4265 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004266 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004267 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004268 default: assert(0 && "Unknown thing");
4269 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004270 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004271 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004272 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004273 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004274 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004275 }
Chris Lattner63022662005-09-02 20:26:58 +00004276 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004277}
4278
Chris Lattner4add7e32005-01-23 04:42:50 +00004279
Evan Chengbf535fc2007-04-27 07:33:31 +00004280/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4281///
Chris Lattneraac464e2005-01-21 06:05:23 +00004282SDOperand SelectionDAGLegalize::
4283ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004284 assert(getTypeAction(Source.getValueType()) == Expand &&
4285 "This is not an expansion!");
4286 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4287
Chris Lattner06bbeb62005-05-11 19:02:11 +00004288 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004289 assert(Source.getValueType() == MVT::i64 &&
4290 "This only works for 64-bit -> FP");
4291 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4292 // incoming integer is set. To handle this, we dynamically test to see if
4293 // it is set, and, if so, add a fudge factor.
4294 SDOperand Lo, Hi;
4295 ExpandOp(Source, Lo, Hi);
4296
Chris Lattner2a4f7312005-05-13 04:45:13 +00004297 // If this is unsigned, and not supported, first perform the conversion to
4298 // signed, then adjust the result if the sign bit is set.
4299 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4300 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4301
Chris Lattnerd47675e2005-08-09 20:20:18 +00004302 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4303 DAG.getConstant(0, Hi.getValueType()),
4304 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004305 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4306 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4307 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004308 uint64_t FF = 0x5f800000ULL;
4309 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004310 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004311
Chris Lattnerc30405e2005-08-26 17:15:30 +00004312 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004313 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4314 SDOperand FudgeInReg;
4315 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004316 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004317 else {
4318 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Chengbf535fc2007-04-27 07:33:31 +00004319 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004320 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004321 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004322 }
Evan Chengbf535fc2007-04-27 07:33:31 +00004323 MVT::ValueType SCVT = SignedConv.getValueType();
4324 if (SCVT != DestTy) {
4325 // Destination type needs to be expanded as well. The FADD now we are
4326 // constructing will be expanded into a libcall.
4327 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4328 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4329 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4330 SignedConv, SignedConv.getValue(1));
4331 }
4332 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4333 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004334 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004335 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004336
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004337 // Check to see if the target has a custom way to lower this. If so, use it.
4338 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4339 default: assert(0 && "This action not implemented for this operation!");
4340 case TargetLowering::Legal:
4341 case TargetLowering::Expand:
4342 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004343 case TargetLowering::Custom: {
4344 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4345 Source), DAG);
4346 if (NV.Val)
4347 return LegalizeOp(NV);
4348 break; // The target decided this was legal after all
4349 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004350 }
4351
Chris Lattner153587e2005-05-12 07:00:44 +00004352 // Expand the source, then glue it back together for the call. We must expand
4353 // the source in case it is shared (this pass of legalize must traverse it).
4354 SDOperand SrcLo, SrcHi;
4355 ExpandOp(Source, SrcLo, SrcHi);
4356 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4357
Evan Cheng31cbddf2007-01-12 02:11:51 +00004358 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004359 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004360 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004361 else {
4362 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004363 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004364 }
Chris Lattner462505f2006-02-13 09:18:02 +00004365
Evan Chengbf535fc2007-04-27 07:33:31 +00004366 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004367 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4368 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004369 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4370 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004371}
Misha Brukman835702a2005-04-21 22:36:52 +00004372
Chris Lattner689bdcc2006-01-28 08:25:58 +00004373/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4374/// INT_TO_FP operation of the specified operand when the target requests that
4375/// we expand it. At this point, we know that the result and operand types are
4376/// legal for the target.
4377SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4378 SDOperand Op0,
4379 MVT::ValueType DestVT) {
4380 if (Op0.getValueType() == MVT::i32) {
4381 // simple 32-bit [signed|unsigned] integer to float/double expansion
4382
Chris Lattner50ee0e42007-01-20 22:35:55 +00004383 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004384 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004385 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4386 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004387 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004388 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004389 // get address of 8 byte buffer
4390 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4391 // word offset constant for Hi/Lo address computation
4392 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4393 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004394 SDOperand Hi = StackSlot;
4395 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4396 if (TLI.isLittleEndian())
4397 std::swap(Hi, Lo);
4398
Chris Lattner689bdcc2006-01-28 08:25:58 +00004399 // if signed map to unsigned space
4400 SDOperand Op0Mapped;
4401 if (isSigned) {
4402 // constant used to invert sign bit (signed to unsigned mapping)
4403 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4404 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4405 } else {
4406 Op0Mapped = Op0;
4407 }
4408 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004409 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004410 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004411 // initial hi portion of constructed double
4412 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4413 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004414 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004415 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004416 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004417 // FP constant to bias correct the final result
4418 SDOperand Bias = DAG.getConstantFP(isSigned ?
4419 BitsToDouble(0x4330000080000000ULL)
4420 : BitsToDouble(0x4330000000000000ULL),
4421 MVT::f64);
4422 // subtract the bias
4423 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4424 // final result
4425 SDOperand Result;
4426 // handle final rounding
4427 if (DestVT == MVT::f64) {
4428 // do nothing
4429 Result = Sub;
4430 } else {
4431 // if f32 then cast to f32
4432 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4433 }
4434 return Result;
4435 }
4436 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4437 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4438
4439 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4440 DAG.getConstant(0, Op0.getValueType()),
4441 ISD::SETLT);
4442 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4443 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4444 SignSet, Four, Zero);
4445
4446 // If the sign bit of the integer is set, the large number will be treated
4447 // as a negative number. To counteract this, the dynamic code adds an
4448 // offset depending on the data type.
4449 uint64_t FF;
4450 switch (Op0.getValueType()) {
4451 default: assert(0 && "Unsupported integer type!");
4452 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4453 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4454 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4455 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4456 }
4457 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004458 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004459
4460 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4461 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4462 SDOperand FudgeInReg;
4463 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004464 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004465 else {
4466 assert(DestVT == MVT::f64 && "Unexpected conversion");
4467 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4468 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004469 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004470 }
4471
4472 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4473}
4474
4475/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4476/// *INT_TO_FP operation of the specified operand when the target requests that
4477/// we promote it. At this point, we know that the result and operand types are
4478/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4479/// operation that takes a larger input.
4480SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4481 MVT::ValueType DestVT,
4482 bool isSigned) {
4483 // First step, figure out the appropriate *INT_TO_FP operation to use.
4484 MVT::ValueType NewInTy = LegalOp.getValueType();
4485
4486 unsigned OpToUse = 0;
4487
4488 // Scan for the appropriate larger type to use.
4489 while (1) {
4490 NewInTy = (MVT::ValueType)(NewInTy+1);
4491 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4492
4493 // If the target supports SINT_TO_FP of this type, use it.
4494 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4495 default: break;
4496 case TargetLowering::Legal:
4497 if (!TLI.isTypeLegal(NewInTy))
4498 break; // Can't use this datatype.
4499 // FALL THROUGH.
4500 case TargetLowering::Custom:
4501 OpToUse = ISD::SINT_TO_FP;
4502 break;
4503 }
4504 if (OpToUse) break;
4505 if (isSigned) continue;
4506
4507 // If the target supports UINT_TO_FP of this type, use it.
4508 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4509 default: break;
4510 case TargetLowering::Legal:
4511 if (!TLI.isTypeLegal(NewInTy))
4512 break; // Can't use this datatype.
4513 // FALL THROUGH.
4514 case TargetLowering::Custom:
4515 OpToUse = ISD::UINT_TO_FP;
4516 break;
4517 }
4518 if (OpToUse) break;
4519
4520 // Otherwise, try a larger type.
4521 }
4522
4523 // Okay, we found the operation and type to use. Zero extend our input to the
4524 // desired type then run the operation on it.
4525 return DAG.getNode(OpToUse, DestVT,
4526 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4527 NewInTy, LegalOp));
4528}
4529
4530/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4531/// FP_TO_*INT operation of the specified operand when the target requests that
4532/// we promote it. At this point, we know that the result and operand types are
4533/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4534/// operation that returns a larger result.
4535SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4536 MVT::ValueType DestVT,
4537 bool isSigned) {
4538 // First step, figure out the appropriate FP_TO*INT operation to use.
4539 MVT::ValueType NewOutTy = DestVT;
4540
4541 unsigned OpToUse = 0;
4542
4543 // Scan for the appropriate larger type to use.
4544 while (1) {
4545 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4546 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4547
4548 // If the target supports FP_TO_SINT returning this type, use it.
4549 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4550 default: break;
4551 case TargetLowering::Legal:
4552 if (!TLI.isTypeLegal(NewOutTy))
4553 break; // Can't use this datatype.
4554 // FALL THROUGH.
4555 case TargetLowering::Custom:
4556 OpToUse = ISD::FP_TO_SINT;
4557 break;
4558 }
4559 if (OpToUse) break;
4560
4561 // If the target supports FP_TO_UINT of this type, use it.
4562 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4563 default: break;
4564 case TargetLowering::Legal:
4565 if (!TLI.isTypeLegal(NewOutTy))
4566 break; // Can't use this datatype.
4567 // FALL THROUGH.
4568 case TargetLowering::Custom:
4569 OpToUse = ISD::FP_TO_UINT;
4570 break;
4571 }
4572 if (OpToUse) break;
4573
4574 // Otherwise, try a larger type.
4575 }
4576
4577 // Okay, we found the operation and type to use. Truncate the result of the
4578 // extended FP_TO_*INT operation to the desired size.
4579 return DAG.getNode(ISD::TRUNCATE, DestVT,
4580 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4581}
4582
4583/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4584///
4585SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4586 MVT::ValueType VT = Op.getValueType();
4587 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4588 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4589 switch (VT) {
4590 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4591 case MVT::i16:
4592 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4593 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4594 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4595 case MVT::i32:
4596 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4597 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4598 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4599 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4600 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4601 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4602 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4603 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4604 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4605 case MVT::i64:
4606 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4607 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4608 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4609 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4610 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4611 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4612 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4613 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4614 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4615 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4616 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4617 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4618 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4619 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4620 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4621 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4622 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4623 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4624 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4625 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4626 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4627 }
4628}
4629
4630/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4631///
4632SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4633 switch (Opc) {
4634 default: assert(0 && "Cannot expand this yet!");
4635 case ISD::CTPOP: {
4636 static const uint64_t mask[6] = {
4637 0x5555555555555555ULL, 0x3333333333333333ULL,
4638 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4639 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4640 };
4641 MVT::ValueType VT = Op.getValueType();
4642 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004643 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004644 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4645 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4646 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4647 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4648 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4649 DAG.getNode(ISD::AND, VT,
4650 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4651 }
4652 return Op;
4653 }
4654 case ISD::CTLZ: {
4655 // for now, we do this:
4656 // x = x | (x >> 1);
4657 // x = x | (x >> 2);
4658 // ...
4659 // x = x | (x >>16);
4660 // x = x | (x >>32); // for 64-bit input
4661 // return popcount(~x);
4662 //
4663 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4664 MVT::ValueType VT = Op.getValueType();
4665 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004666 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004667 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4668 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4669 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4670 }
4671 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4672 return DAG.getNode(ISD::CTPOP, VT, Op);
4673 }
4674 case ISD::CTTZ: {
4675 // for now, we use: { return popcount(~x & (x - 1)); }
4676 // unless the target has ctlz but not ctpop, in which case we use:
4677 // { return 32 - nlz(~x & (x-1)); }
4678 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4679 MVT::ValueType VT = Op.getValueType();
4680 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4681 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4682 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4683 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4684 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4685 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4686 TLI.isOperationLegal(ISD::CTLZ, VT))
4687 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004688 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00004689 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4690 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4691 }
4692 }
4693}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004694
Chris Lattnerdc750592005-01-07 07:47:09 +00004695/// ExpandOp - Expand the specified SDOperand into its two component pieces
4696/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4697/// LegalizeNodes map is filled in for any results that are not expanded, the
4698/// ExpandedNodes map is filled in for any results that are expanded, and the
4699/// Lo/Hi values are returned.
4700void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4701 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004702 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004703 SDNode *Node = Op.Val;
4704 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004705 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00004706 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004707 "Cannot expand to FP value or to larger int value!");
4708
Chris Lattner1a570f12005-09-02 20:32:45 +00004709 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004710 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004711 = ExpandedNodes.find(Op);
4712 if (I != ExpandedNodes.end()) {
4713 Lo = I->second.first;
4714 Hi = I->second.second;
4715 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004716 }
4717
Chris Lattnerdc750592005-01-07 07:47:09 +00004718 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004719 case ISD::CopyFromReg:
4720 assert(0 && "CopyFromReg must be legal!");
4721 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004722#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004723 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004724#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004725 assert(0 && "Do not know how to expand this operator!");
4726 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004727 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004728 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004729 Lo = DAG.getNode(ISD::UNDEF, NVT);
4730 Hi = DAG.getNode(ISD::UNDEF, NVT);
4731 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004732 case ISD::Constant: {
4733 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4734 Lo = DAG.getConstant(Cst, NVT);
4735 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4736 break;
4737 }
Evan Cheng47833a12006-12-12 21:32:44 +00004738 case ISD::ConstantFP: {
4739 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004740 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004741 if (getTypeAction(Lo.getValueType()) == Expand)
4742 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004743 break;
4744 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004745 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004746 // Return the operands.
4747 Lo = Node->getOperand(0);
4748 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004749 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004750
4751 case ISD::SIGN_EXTEND_INREG:
4752 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004753 // sext_inreg the low part if needed.
4754 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4755
4756 // The high part gets the sign extension from the lo-part. This handles
4757 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004758 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4759 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4760 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004761 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004762
Nate Begeman2fba8a32006-01-14 03:14:10 +00004763 case ISD::BSWAP: {
4764 ExpandOp(Node->getOperand(0), Lo, Hi);
4765 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4766 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4767 Lo = TempLo;
4768 break;
4769 }
4770
Chris Lattner55e9cde2005-05-11 04:51:16 +00004771 case ISD::CTPOP:
4772 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004773 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4774 DAG.getNode(ISD::CTPOP, NVT, Lo),
4775 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004776 Hi = DAG.getConstant(0, NVT);
4777 break;
4778
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004779 case ISD::CTLZ: {
4780 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004781 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004782 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4783 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004784 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4785 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004786 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4787 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4788
4789 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4790 Hi = DAG.getConstant(0, NVT);
4791 break;
4792 }
4793
4794 case ISD::CTTZ: {
4795 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004796 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004797 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4798 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004799 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4800 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004801 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4802 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4803
4804 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4805 Hi = DAG.getConstant(0, NVT);
4806 break;
4807 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004808
Nate Begemane74795c2006-01-25 18:21:52 +00004809 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004810 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4811 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004812 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4813 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4814
4815 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004816 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004817 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4818 if (!TLI.isLittleEndian())
4819 std::swap(Lo, Hi);
4820 break;
4821 }
4822
Chris Lattnerdc750592005-01-07 07:47:09 +00004823 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004824 LoadSDNode *LD = cast<LoadSDNode>(Node);
4825 SDOperand Ch = LD->getChain(); // Legalize the chain.
4826 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4827 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmana8665142007-06-25 16:23:39 +00004828 unsigned SVOffset = LD->getSrcValueOffset();
Chris Lattnerdc750592005-01-07 07:47:09 +00004829
Evan Chenge71fe34d2006-10-09 20:57:25 +00004830 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmana8665142007-06-25 16:23:39 +00004831 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset);
Evan Cheng47833a12006-12-12 21:32:44 +00004832 if (VT == MVT::f32 || VT == MVT::f64) {
4833 // f32->i32 or f64->i64 one to one expansion.
4834 // Remember that we legalized the chain.
4835 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004836 // Recursively expand the new load.
4837 if (getTypeAction(NVT) == Expand)
4838 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004839 break;
4840 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004841
Evan Chenge71fe34d2006-10-09 20:57:25 +00004842 // Increment the pointer to the other half.
4843 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4844 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4845 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00004846 SVOffset += IncrementSize;
4847 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset);
Misha Brukman835702a2005-04-21 22:36:52 +00004848
Evan Chenge71fe34d2006-10-09 20:57:25 +00004849 // Build a factor node to remember that this load is independent of the
4850 // other one.
4851 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4852 Hi.getValue(1));
4853
4854 // Remember that we legalized the chain.
4855 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4856 if (!TLI.isLittleEndian())
4857 std::swap(Lo, Hi);
4858 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004859 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004860
4861 if (VT == MVT::f64 && EVT == MVT::f32) {
4862 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4863 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmana8665142007-06-25 16:23:39 +00004864 SVOffset);
Evan Chenge370e0e2006-12-13 03:19:57 +00004865 // Remember that we legalized the chain.
4866 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4867 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4868 break;
4869 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004870
4871 if (EVT == NVT)
4872 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmana8665142007-06-25 16:23:39 +00004873 SVOffset);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004874 else
4875 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmana8665142007-06-25 16:23:39 +00004876 SVOffset, EVT);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004877
4878 // Remember that we legalized the chain.
4879 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4880
4881 if (ExtType == ISD::SEXTLOAD) {
4882 // The high part is obtained by SRA'ing all but one of the bits of the
4883 // lo part.
4884 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4885 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4886 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4887 } else if (ExtType == ISD::ZEXTLOAD) {
4888 // The high part is just a zero.
4889 Hi = DAG.getConstant(0, NVT);
4890 } else /* if (ExtType == ISD::EXTLOAD) */ {
4891 // The high part is undefined.
4892 Hi = DAG.getNode(ISD::UNDEF, NVT);
4893 }
4894 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004895 break;
4896 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004897 case ISD::AND:
4898 case ISD::OR:
4899 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4900 SDOperand LL, LH, RL, RH;
4901 ExpandOp(Node->getOperand(0), LL, LH);
4902 ExpandOp(Node->getOperand(1), RL, RH);
4903 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4904 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4905 break;
4906 }
4907 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004908 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004909 ExpandOp(Node->getOperand(1), LL, LH);
4910 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004911 if (getTypeAction(NVT) == Expand)
4912 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004913 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004914 if (VT != MVT::f32)
4915 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004916 break;
4917 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004918 case ISD::SELECT_CC: {
4919 SDOperand TL, TH, FL, FH;
4920 ExpandOp(Node->getOperand(2), TL, TH);
4921 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004922 if (getTypeAction(NVT) == Expand)
4923 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004924 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4925 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004926 if (VT != MVT::f32)
4927 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4928 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004929 break;
4930 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004931 case ISD::ANY_EXTEND:
4932 // The low part is any extension of the input (which degenerates to a copy).
4933 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4934 // The high part is undefined.
4935 Hi = DAG.getNode(ISD::UNDEF, NVT);
4936 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004937 case ISD::SIGN_EXTEND: {
4938 // The low part is just a sign extension of the input (which degenerates to
4939 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004940 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004941
Chris Lattnerdc750592005-01-07 07:47:09 +00004942 // The high part is obtained by SRA'ing all but one of the bits of the lo
4943 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004944 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004945 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4946 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004947 break;
4948 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004949 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004950 // The low part is just a zero extension of the input (which degenerates to
4951 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004952 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004953
Chris Lattnerdc750592005-01-07 07:47:09 +00004954 // The high part is just a zero.
4955 Hi = DAG.getConstant(0, NVT);
4956 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004957
Chris Lattner59b27fa372007-02-13 23:55:16 +00004958 case ISD::TRUNCATE: {
4959 // The input value must be larger than this value. Expand *it*.
4960 SDOperand NewLo;
4961 ExpandOp(Node->getOperand(0), NewLo, Hi);
4962
4963 // The low part is now either the right size, or it is closer. If not the
4964 // right size, make an illegal truncate so we recursively expand it.
4965 if (NewLo.getValueType() != Node->getValueType(0))
4966 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
4967 ExpandOp(NewLo, Lo, Hi);
4968 break;
4969 }
4970
Chris Lattner36e663d2005-12-23 00:16:34 +00004971 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004972 SDOperand Tmp;
4973 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4974 // If the target wants to, allow it to lower this itself.
4975 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4976 case Expand: assert(0 && "cannot expand FP!");
4977 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4978 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4979 }
4980 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4981 }
4982
Evan Cheng4eee7242006-12-09 02:42:38 +00004983 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00004984 if (VT == MVT::f32 || VT == MVT::f64) {
4985 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00004986 if (getTypeAction(NVT) == Expand)
4987 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00004988 break;
4989 }
4990
4991 // If source operand will be expanded to the same type as VT, i.e.
4992 // i64 <- f64, i32 <- f32, expand the source operand instead.
4993 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4994 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4995 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00004996 break;
4997 }
4998
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004999 // Turn this into a load/store pair by default.
5000 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005001 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005002
Chris Lattner36e663d2005-12-23 00:16:34 +00005003 ExpandOp(Tmp, Lo, Hi);
5004 break;
5005 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005006
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005007 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005008 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5009 TargetLowering::Custom &&
5010 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005011 Lo = TLI.LowerOperation(Op, DAG);
5012 assert(Lo.Val && "Node must be custom expanded!");
5013 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005014 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005015 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005016 break;
5017
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005018 // These operators cannot be expanded directly, emit them as calls to
5019 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005020 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005021 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005022 SDOperand Op;
5023 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5024 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005025 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5026 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005027 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005028
Chris Lattner941d84a2005-07-30 01:40:57 +00005029 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5030
Chris Lattnerfe68d752005-07-29 00:33:32 +00005031 // Now that the custom expander is done, expand the result, which is still
5032 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005033 if (Op.Val) {
5034 ExpandOp(Op, Lo, Hi);
5035 break;
5036 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005037 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005038
Evan Cheng31cbddf2007-01-12 02:11:51 +00005039 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005040 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005041 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005042 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005043 LC = RTLIB::FPTOSINT_F64_I64;
5044 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5045 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005046 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005047 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005048
Evan Cheng31cbddf2007-01-12 02:11:51 +00005049 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005050 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005051 SDOperand Op;
5052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5053 case Expand: assert(0 && "cannot expand FP!");
5054 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5055 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5056 }
5057
5058 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5059
5060 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005061 if (Op.Val) {
5062 ExpandOp(Op, Lo, Hi);
5063 break;
5064 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005065 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005066
Evan Cheng31cbddf2007-01-12 02:11:51 +00005067 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005068 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005069 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005070 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005071 LC = RTLIB::FPTOUINT_F64_I64;
5072 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5073 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005074 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005075 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005076
Evan Cheng870e4f82006-01-09 18:31:59 +00005077 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005078 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005079 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005080 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005081 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005082 Op = TLI.LowerOperation(Op, DAG);
5083 if (Op.Val) {
5084 // Now that the custom expander is done, expand the result, which is
5085 // still VT.
5086 ExpandOp(Op, Lo, Hi);
5087 break;
5088 }
5089 }
5090
Chris Lattner72b503b2006-09-13 03:50:39 +00005091 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5092 // this X << 1 as X+X.
5093 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5094 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5095 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5096 SDOperand LoOps[2], HiOps[3];
5097 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5098 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5099 LoOps[1] = LoOps[0];
5100 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5101
5102 HiOps[1] = HiOps[0];
5103 HiOps[2] = Lo.getValue(1);
5104 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5105 break;
5106 }
5107 }
5108
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005109 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005110 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005111 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005112
5113 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005114 TargetLowering::LegalizeAction Action =
5115 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5116 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5117 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005118 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005119 break;
5120 }
5121
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005122 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005123 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5124 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005125 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005126 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005127
Evan Cheng870e4f82006-01-09 18:31:59 +00005128 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005129 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005130 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005131 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005132 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005133 Op = TLI.LowerOperation(Op, DAG);
5134 if (Op.Val) {
5135 // Now that the custom expander is done, expand the result, which is
5136 // still VT.
5137 ExpandOp(Op, Lo, Hi);
5138 break;
5139 }
5140 }
5141
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005142 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005143 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005144 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005145
5146 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005147 TargetLowering::LegalizeAction Action =
5148 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5149 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5150 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005151 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005152 break;
5153 }
5154
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005155 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005156 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5157 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005158 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005159 }
5160
5161 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005162 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005163 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005164 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005165 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005166 Op = TLI.LowerOperation(Op, DAG);
5167 if (Op.Val) {
5168 // Now that the custom expander is done, expand the result, which is
5169 // still VT.
5170 ExpandOp(Op, Lo, Hi);
5171 break;
5172 }
5173 }
5174
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005175 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005176 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005177 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005178
5179 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005180 TargetLowering::LegalizeAction Action =
5181 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5182 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5183 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005184 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005185 break;
5186 }
5187
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005188 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005189 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5190 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005191 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005192 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005193
Misha Brukman835702a2005-04-21 22:36:52 +00005194 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005195 case ISD::SUB: {
5196 // If the target wants to custom expand this, let them.
5197 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5198 TargetLowering::Custom) {
5199 Op = TLI.LowerOperation(Op, DAG);
5200 if (Op.Val) {
5201 ExpandOp(Op, Lo, Hi);
5202 break;
5203 }
5204 }
5205
5206 // Expand the subcomponents.
5207 SDOperand LHSL, LHSH, RHSL, RHSH;
5208 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5209 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005210 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5211 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005212 LoOps[0] = LHSL;
5213 LoOps[1] = RHSL;
5214 HiOps[0] = LHSH;
5215 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005216 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005217 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005218 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005219 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005220 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005221 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005222 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005223 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005224 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005225 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005226 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005227
5228 case ISD::ADDC:
5229 case ISD::SUBC: {
5230 // Expand the subcomponents.
5231 SDOperand LHSL, LHSH, RHSL, RHSH;
5232 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5233 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5234 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5235 SDOperand LoOps[2] = { LHSL, RHSL };
5236 SDOperand HiOps[3] = { LHSH, RHSH };
5237
5238 if (Node->getOpcode() == ISD::ADDC) {
5239 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5240 HiOps[2] = Lo.getValue(1);
5241 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5242 } else {
5243 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5244 HiOps[2] = Lo.getValue(1);
5245 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5246 }
5247 // Remember that we legalized the flag.
5248 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5249 break;
5250 }
5251 case ISD::ADDE:
5252 case ISD::SUBE: {
5253 // Expand the subcomponents.
5254 SDOperand LHSL, LHSH, RHSL, RHSH;
5255 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5256 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5257 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5258 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5259 SDOperand HiOps[3] = { LHSH, RHSH };
5260
5261 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5262 HiOps[2] = Lo.getValue(1);
5263 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5264
5265 // Remember that we legalized the flag.
5266 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5267 break;
5268 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005269 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005270 // If the target wants to custom expand this, let them.
5271 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005272 SDOperand New = TLI.LowerOperation(Op, DAG);
5273 if (New.Val) {
5274 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005275 break;
5276 }
5277 }
5278
Evan Chenge93762d2006-09-01 18:17:58 +00005279 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5280 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005281 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005282 SDOperand LL, LH, RL, RH;
5283 ExpandOp(Node->getOperand(0), LL, LH);
5284 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005285 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005286 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005287 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5288 // extended the sign bit of the low half through the upper half, and if so
5289 // emit a MULHS instead of the alternate sequence that is valid for any
5290 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005291 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005292 // is RH an extension of the sign bit of RL?
5293 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5294 RH.getOperand(1).getOpcode() == ISD::Constant &&
5295 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5296 // is LH an extension of the sign bit of LL?
5297 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5298 LH.getOperand(1).getOpcode() == ISD::Constant &&
5299 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005300 // Low part:
5301 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5302 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005303 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005304 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005305 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005306 // Low part:
5307 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5308
5309 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005310 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5311 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5312 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5313 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5314 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005315 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005316 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005317 }
Evan Chenge93762d2006-09-01 18:17:58 +00005318
Evan Cheng31cbddf2007-01-12 02:11:51 +00005319 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5320 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005321 break;
5322 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005323 case ISD::SDIV:
5324 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5325 break;
5326 case ISD::UDIV:
5327 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5328 break;
5329 case ISD::SREM:
5330 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5331 break;
5332 case ISD::UREM:
5333 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5334 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005335
Evan Cheng4eee7242006-12-09 02:42:38 +00005336 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005337 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5338 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5339 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005340 break;
5341 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005342 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5343 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5344 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005345 break;
5346 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005347 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5348 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5349 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005350 break;
5351 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005352 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5353 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5354 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005355 break;
5356 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005357 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005358 break;
5359 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005360 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005361 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005362 case ISD::FSQRT:
5363 case ISD::FSIN:
5364 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005365 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005366 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005367 case ISD::FSQRT:
5368 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5369 break;
5370 case ISD::FSIN:
5371 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5372 break;
5373 case ISD::FCOS:
5374 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5375 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005376 default: assert(0 && "Unreachable!");
5377 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005378 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005379 break;
5380 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005381 case ISD::FABS: {
5382 SDOperand Mask = (VT == MVT::f64)
5383 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5384 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5385 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5386 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5387 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5388 if (getTypeAction(NVT) == Expand)
5389 ExpandOp(Lo, Lo, Hi);
5390 break;
5391 }
5392 case ISD::FNEG: {
5393 SDOperand Mask = (VT == MVT::f64)
5394 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5395 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5396 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5397 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5398 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5399 if (getTypeAction(NVT) == Expand)
5400 ExpandOp(Lo, Lo, Hi);
5401 break;
5402 }
Evan Cheng003feb02007-01-04 21:56:39 +00005403 case ISD::FCOPYSIGN: {
5404 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5405 if (getTypeAction(NVT) == Expand)
5406 ExpandOp(Lo, Lo, Hi);
5407 break;
5408 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005409 case ISD::SINT_TO_FP:
5410 case ISD::UINT_TO_FP: {
5411 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5412 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005413 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005414 if (Node->getOperand(0).getValueType() == MVT::i64) {
5415 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005416 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005417 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005418 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005419 } else {
5420 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005421 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005422 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005423 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005424 }
5425
5426 // Promote the operand if needed.
5427 if (getTypeAction(SrcVT) == Promote) {
5428 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5429 Tmp = isSigned
5430 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5431 DAG.getValueType(SrcVT))
5432 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5433 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5434 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005435
5436 const char *LibCall = TLI.getLibcallName(LC);
5437 if (LibCall)
5438 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5439 else {
5440 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5441 Node->getOperand(0));
5442 if (getTypeAction(Lo.getValueType()) == Expand)
5443 ExpandOp(Lo, Lo, Hi);
5444 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005445 break;
5446 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005447 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005448
Chris Lattnerac12f682005-12-21 18:02:52 +00005449 // Make sure the resultant values have been legalized themselves, unless this
5450 // is a type that requires multi-step expansion.
5451 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5452 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005453 if (Hi.Val)
5454 // Don't legalize the high part if it is expanded to a single node.
5455 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005456 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005457
5458 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005459 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005460 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005461}
5462
Dan Gohmana8665142007-06-25 16:23:39 +00005463/// SplitVectorOp - Given an operand of vector type, break it down into
5464/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005465void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5466 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005467 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005468 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005469 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattner32206f52006-03-18 01:44:44 +00005470 assert(NumElements > 1 && "Cannot split a single element vector!");
5471 unsigned NewNumElts = NumElements/2;
Dan Gohmana8665142007-06-25 16:23:39 +00005472 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5473 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005474
5475 // See if we already split it.
5476 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5477 = SplitNodes.find(Op);
5478 if (I != SplitNodes.end()) {
5479 Lo = I->second.first;
5480 Hi = I->second.second;
5481 return;
5482 }
5483
5484 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005485 default:
5486#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005487 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005488#endif
5489 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00005490 case ISD::BUILD_PAIR:
5491 Lo = Node->getOperand(0);
5492 Hi = Node->getOperand(1);
5493 break;
5494 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005495 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5496 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00005497 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005498
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005499 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00005500 Node->op_end());
5501 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005502 break;
5503 }
Dan Gohmana8665142007-06-25 16:23:39 +00005504 case ISD::CONCAT_VECTORS: {
5505 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5506 if (NewNumSubvectors == 1) {
5507 Lo = Node->getOperand(0);
5508 Hi = Node->getOperand(1);
5509 } else {
5510 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5511 Node->op_begin()+NewNumSubvectors);
5512 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00005513
Dan Gohmana8665142007-06-25 16:23:39 +00005514 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5515 Node->op_end());
5516 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5517 }
Dan Gohman26455c42007-06-13 15:12:02 +00005518 break;
5519 }
Dan Gohmana8665142007-06-25 16:23:39 +00005520 case ISD::ADD:
5521 case ISD::SUB:
5522 case ISD::MUL:
5523 case ISD::FADD:
5524 case ISD::FSUB:
5525 case ISD::FMUL:
5526 case ISD::SDIV:
5527 case ISD::UDIV:
5528 case ISD::FDIV:
5529 case ISD::AND:
5530 case ISD::OR:
5531 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00005532 SDOperand LL, LH, RL, RH;
5533 SplitVectorOp(Node->getOperand(0), LL, LH);
5534 SplitVectorOp(Node->getOperand(1), RL, RH);
5535
Dan Gohmana8665142007-06-25 16:23:39 +00005536 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5537 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00005538 break;
5539 }
Dan Gohmana8665142007-06-25 16:23:39 +00005540 case ISD::LOAD: {
5541 LoadSDNode *LD = cast<LoadSDNode>(Node);
5542 SDOperand Ch = LD->getChain();
5543 SDOperand Ptr = LD->getBasePtr();
5544 const Value *SV = LD->getSrcValue();
5545 int SVOffset = LD->getSrcValueOffset();
5546 unsigned Alignment = LD->getAlignment();
5547 bool isVolatile = LD->isVolatile();
5548
5549 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5550 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00005551 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5552 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005553 SVOffset += IncrementSize;
5554 if (Alignment > IncrementSize)
5555 Alignment = IncrementSize;
5556 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00005557
5558 // Build a factor node to remember that this load is independent of the
5559 // other one.
5560 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5561 Hi.getValue(1));
5562
5563 // Remember that we legalized the chain.
5564 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005565 break;
5566 }
Dan Gohmana8665142007-06-25 16:23:39 +00005567 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005568 // We know the result is a vector. The input may be either a vector or a
5569 // scalar value.
Dan Gohmana8665142007-06-25 16:23:39 +00005570 if (!MVT::isVector(Op.getOperand(0).getValueType())) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005571 // Lower to a store/load. FIXME: this could be improved probably.
5572 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5573
Evan Chengdf9ac472006-10-05 23:01:46 +00005574 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005575 Op.getOperand(0), Ptr, NULL, 0);
Dan Gohmana8665142007-06-25 16:23:39 +00005576 St = DAG.getLoad(NewVT, St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005577 SplitVectorOp(St, Lo, Hi);
5578 } else {
5579 // If the input is a vector type, we have to either scalarize it, pack it
5580 // or convert it based on whether the input vector type is legal.
5581 SDNode *InVal = Node->getOperand(0).Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005582 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005583
Dan Gohmana8665142007-06-25 16:23:39 +00005584 assert(NumElems > 1);
5585 {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005586 // Split the input vector.
5587 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5588
5589 // Convert each of the pieces now.
Dan Gohmana8665142007-06-25 16:23:39 +00005590 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5591 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005592 }
5593 break;
5594 }
5595 }
Chris Lattner32206f52006-03-18 01:44:44 +00005596 }
5597
5598 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005599 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005600 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5601 assert(isNew && "Value already expanded?!?");
5602}
5603
5604
Dan Gohmana8665142007-06-25 16:23:39 +00005605/// ScalarizeVectorOp - Given an operand of vector type, convert it into the
5606/// equivalent operation that returns a scalar (e.g. F32) value.
5607SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5608 assert(MVT::isVector(Op.getValueType()) &&
5609 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00005610 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005611 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5612 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00005613
Dan Gohmana8665142007-06-25 16:23:39 +00005614 // See if we already scalarized it.
5615 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5616 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00005617
5618 SDOperand Result;
5619 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005620 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005621#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005622 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005623#endif
Dan Gohmana8665142007-06-25 16:23:39 +00005624 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5625 case ISD::ADD:
5626 case ISD::FADD:
5627 case ISD::SUB:
5628 case ISD::FSUB:
5629 case ISD::MUL:
5630 case ISD::FMUL:
5631 case ISD::SDIV:
5632 case ISD::UDIV:
5633 case ISD::FDIV:
5634 case ISD::SREM:
5635 case ISD::UREM:
5636 case ISD::FREM:
5637 case ISD::AND:
5638 case ISD::OR:
5639 case ISD::XOR:
5640 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00005641 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00005642 ScalarizeVectorOp(Node->getOperand(0)),
5643 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00005644 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005645 case ISD::FNEG:
5646 case ISD::FABS:
5647 case ISD::FSQRT:
5648 case ISD::FSIN:
5649 case ISD::FCOS:
5650 Result = DAG.getNode(Node->getOpcode(),
5651 NewVT,
5652 ScalarizeVectorOp(Node->getOperand(0)));
5653 break;
5654 case ISD::LOAD: {
5655 LoadSDNode *LD = cast<LoadSDNode>(Node);
5656 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5657 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005658
Dan Gohmana8665142007-06-25 16:23:39 +00005659 const Value *SV = LD->getSrcValue();
5660 int SVOffset = LD->getSrcValueOffset();
5661 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5662 LD->isVolatile(), LD->getAlignment());
5663
Chris Lattner32206f52006-03-18 01:44:44 +00005664 // Remember that we legalized the chain.
5665 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5666 break;
5667 }
Dan Gohmana8665142007-06-25 16:23:39 +00005668 case ISD::BUILD_VECTOR:
5669 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00005670 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005671 case ISD::INSERT_VECTOR_ELT:
5672 // Returning the inserted scalar element.
5673 Result = Node->getOperand(1);
5674 break;
5675 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00005676 assert(Node->getOperand(0).getValueType() == NewVT &&
5677 "Concat of non-legal vectors not yet supported!");
5678 Result = Node->getOperand(0);
5679 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005680 case ISD::VECTOR_SHUFFLE: {
5681 // Figure out if the scalar is the LHS or RHS and return it.
5682 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5683 if (cast<ConstantSDNode>(EltNum)->getValue())
5684 Result = ScalarizeVectorOp(Node->getOperand(1));
5685 else
5686 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00005687 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005688 }
5689 case ISD::EXTRACT_SUBVECTOR:
5690 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00005691 assert(Result.getValueType() == NewVT);
5692 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005693 case ISD::BIT_CONVERT:
5694 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005695 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005696 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00005697 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00005698 ScalarizeVectorOp(Op.getOperand(1)),
5699 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00005700 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005701 }
5702
5703 if (TLI.isTypeLegal(NewVT))
5704 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00005705 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5706 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00005707 return Result;
5708}
5709
Chris Lattnerdc750592005-01-07 07:47:09 +00005710
5711// SelectionDAG::Legalize - This is the entry point for the file.
5712//
Chris Lattner4add7e32005-01-23 04:42:50 +00005713void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005714 if (ViewLegalizeDAGs) viewGraph();
5715
Chris Lattnerdc750592005-01-07 07:47:09 +00005716 /// run - This is the main entry point to this class.
5717 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005718 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005719}
5720