blob: 8b82b62b65210d9a20a064d181e92855d6181d4d [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 Lattnerdc750592005-01-07 07:47:09 +000042//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
58
Chris Lattner462505f2006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
64 SDOperand LastCALLSEQ_END;
65
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattnerdc750592005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000075 };
Chris Lattner462505f2006-02-13 09:18:02 +000076
Chris Lattnerdc750592005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000081
Chris Lattnerdc750592005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000085 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000086
Chris Lattner1f2c9d82005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000090 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000091
Chris Lattner32206f52006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
93 /// which which operands are the expanded version of the input. This allows
94 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000095 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000096
Chris Lattner32206f52006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
98 /// which which operands are the split version of the input. This allows us
99 /// to avoid splitting the same node more than once.
100 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
101
Dan Gohmana8665142007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000105 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000106
Chris Lattnerea4ca942005-01-07 22:28:47 +0000107 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000108 LegalizedNodes.insert(std::make_pair(From, To));
109 // If someone requests legalization of the new node, return itself.
110 if (From != To)
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000112 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000113 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000114 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000115 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000116 // If someone requests legalization of the new node, return itself.
117 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000118 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000119
Chris Lattnerdc750592005-01-07 07:47:09 +0000120public:
121
Chris Lattner4add7e32005-01-23 04:42:50 +0000122 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattnerdc750592005-01-07 07:47:09 +0000124 /// getTypeAction - Return how we should legalize values of this type, either
125 /// it is already legal or we need to expand it into multiple registers of
126 /// smaller integer type, or we need to promote it to a larger type.
127 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000128 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000129 }
130
131 /// isTypeLegal - Return true if this type is legal on this target.
132 ///
133 bool isTypeLegal(MVT::ValueType VT) const {
134 return getTypeAction(VT) == Legal;
135 }
136
Chris Lattnerdc750592005-01-07 07:47:09 +0000137 void LegalizeDAG();
138
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000139private:
Dan Gohmana8665142007-06-25 16:23:39 +0000140 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000141 /// appropriate for its type.
142 void HandleOp(SDOperand Op);
143
144 /// LegalizeOp - We know that the specified value has a legal type.
145 /// Recursively ensure that the operands have legal types, then return the
146 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000147 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000148
149 /// PromoteOp - Given an operation that produces a value in an invalid type,
150 /// promote it to compute the value into a larger type. The produced value
151 /// will have the correct bits for the low portion of the register, but no
152 /// guarantee is made about the top bits: it may be zero, sign-extended, or
153 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000154 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000155
Chris Lattner32206f52006-03-18 01:44:44 +0000156 /// ExpandOp - Expand the specified SDOperand into its two component pieces
157 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
158 /// the LegalizeNodes map is filled in for any results that are not expanded,
159 /// the ExpandedNodes map is filled in for any results that are expanded, and
160 /// the Lo/Hi values are returned. This applies to integer types and Vector
161 /// types.
162 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
163
Dan Gohmana8665142007-06-25 16:23:39 +0000164 /// SplitVectorOp - Given an operand of vector type, break it down into
165 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000166 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
167
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000168 /// ScalarizeVectorOp - Given an operand of single-element vector type
169 /// (e.g. v1f32), convert it into the equivalent operation that returns a
170 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000171 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000172
Chris Lattner6be79822006-04-04 17:23:26 +0000173 /// isShuffleLegal - Return true if a vector shuffle is legal with the
174 /// specified mask and type. Targets can specify exactly which masks they
175 /// support and the code generator is tasked with not creating illegal masks.
176 ///
177 /// Note that this will also return true for shuffles that are promoted to a
178 /// different type.
179 ///
180 /// If this is a legal shuffle, this method returns the (possibly promoted)
181 /// build_vector Mask. If it's not a legal shuffle, it returns null.
182 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
183
Chris Lattner4488f0c2006-07-26 23:55:56 +0000184 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000185 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000186
Nate Begeman7e7f4392006-02-01 07:19:44 +0000187 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
188
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000189 SDOperand CreateStackTemporary(MVT::ValueType VT);
190
Reid Spencere63b6512006-12-31 05:55:36 +0000191 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000192 SDOperand &Hi);
193 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
194 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000195
Chris Lattner36e663d2005-12-23 00:16:34 +0000196 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000197 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000198 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000199 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
200 SDOperand LegalOp,
201 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000202 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000204 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
205 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000206
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000207 SDOperand ExpandBSWAP(SDOperand Op);
208 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000209 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000211 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
212 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000213
Dan Gohmana8665142007-06-25 16:23:39 +0000214 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000215 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000216
Chris Lattnerdc750592005-01-07 07:47:09 +0000217 SDOperand getIntPtrConstant(uint64_t Val) {
218 return DAG.getConstant(Val, TLI.getPointerTy());
219 }
220};
221}
222
Chris Lattner6be79822006-04-04 17:23:26 +0000223/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
224/// specified mask and type. Targets can specify exactly which masks they
225/// support and the code generator is tasked with not creating illegal masks.
226///
227/// Note that this will also return true for shuffles that are promoted to a
228/// different type.
229SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
230 SDOperand Mask) const {
231 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
232 default: return 0;
233 case TargetLowering::Legal:
234 case TargetLowering::Custom:
235 break;
236 case TargetLowering::Promote: {
237 // If this is promoted to a different type, convert the shuffle mask and
238 // ask if it is legal in the promoted type!
239 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
240
241 // If we changed # elements, change the shuffle mask.
242 unsigned NumEltsGrowth =
243 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
244 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
245 if (NumEltsGrowth > 1) {
246 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000247 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000248 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
249 SDOperand InOp = Mask.getOperand(i);
250 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
251 if (InOp.getOpcode() == ISD::UNDEF)
252 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
253 else {
254 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
255 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
256 }
257 }
258 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000259 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000260 }
261 VT = NVT;
262 break;
263 }
264 }
265 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
266}
267
Chris Lattner4add7e32005-01-23 04:42:50 +0000268SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
269 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
270 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000271 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000272 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000273}
274
Chris Lattnere31adc82007-06-18 21:28:10 +0000275/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
276/// contains all of a nodes operands before it contains the node.
277static void ComputeTopDownOrdering(SelectionDAG &DAG,
278 SmallVector<SDNode*, 64> &Order) {
279
280 DenseMap<SDNode*, unsigned> Visited;
281 std::vector<SDNode*> Worklist;
282 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000283
Chris Lattnere31adc82007-06-18 21:28:10 +0000284 // Compute ordering from all of the leaves in the graphs, those (like the
285 // entry node) that have no operands.
286 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
287 E = DAG.allnodes_end(); I != E; ++I) {
288 if (I->getNumOperands() == 0) {
289 Visited[I] = 0 - 1U;
290 Worklist.push_back(I);
291 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000292 }
293
Chris Lattnere31adc82007-06-18 21:28:10 +0000294 while (!Worklist.empty()) {
295 SDNode *N = Worklist.back();
296 Worklist.pop_back();
297
298 if (++Visited[N] != N->getNumOperands())
299 continue; // Haven't visited all operands yet
300
301 Order.push_back(N);
302
303 // Now that we have N in, add anything that uses it if all of their operands
304 // are now done.
305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
306 UI != E; ++UI)
307 Worklist.push_back(*UI);
308 }
309
310 assert(Order.size() == Visited.size() &&
311 Order.size() ==
312 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
313 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000314}
315
Chris Lattner44fe26f2005-07-29 00:11:56 +0000316
Chris Lattnerdc750592005-01-07 07:47:09 +0000317void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000318 LastCALLSEQ_END = DAG.getEntryNode();
319 IsLegalizingCall = false;
320
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000321 // The legalize process is inherently a bottom-up recursive process (users
322 // legalize their uses before themselves). Given infinite stack space, we
323 // could just start legalizing on the root and traverse the whole graph. In
324 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000325 // blocks. To avoid this problem, compute an ordering of the nodes where each
326 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000327 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000328 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000329
Chris Lattner32206f52006-03-18 01:44:44 +0000330 for (unsigned i = 0, e = Order.size(); i != e; ++i)
331 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000332
333 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000334 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000335 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
336 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000337
338 ExpandedNodes.clear();
339 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000340 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000341 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000342 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000343
344 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000345 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000346}
347
Chris Lattner462505f2006-02-13 09:18:02 +0000348
349/// FindCallEndFromCallStart - Given a chained node that is part of a call
350/// sequence, find the CALLSEQ_END node that terminates the call sequence.
351static SDNode *FindCallEndFromCallStart(SDNode *Node) {
352 if (Node->getOpcode() == ISD::CALLSEQ_END)
353 return Node;
354 if (Node->use_empty())
355 return 0; // No CallSeqEnd
356
357 // The chain is usually at the end.
358 SDOperand TheChain(Node, Node->getNumValues()-1);
359 if (TheChain.getValueType() != MVT::Other) {
360 // Sometimes it's at the beginning.
361 TheChain = SDOperand(Node, 0);
362 if (TheChain.getValueType() != MVT::Other) {
363 // Otherwise, hunt for it.
364 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
365 if (Node->getValueType(i) == MVT::Other) {
366 TheChain = SDOperand(Node, i);
367 break;
368 }
369
370 // Otherwise, we walked into a node without a chain.
371 if (TheChain.getValueType() != MVT::Other)
372 return 0;
373 }
374 }
375
376 for (SDNode::use_iterator UI = Node->use_begin(),
377 E = Node->use_end(); UI != E; ++UI) {
378
379 // Make sure to only follow users of our token chain.
380 SDNode *User = *UI;
381 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
382 if (User->getOperand(i) == TheChain)
383 if (SDNode *Result = FindCallEndFromCallStart(User))
384 return Result;
385 }
386 return 0;
387}
388
389/// FindCallStartFromCallEnd - Given a chained node that is part of a call
390/// sequence, find the CALLSEQ_START node that initiates the call sequence.
391static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
392 assert(Node && "Didn't find callseq_start for a call??");
393 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
394
395 assert(Node->getOperand(0).getValueType() == MVT::Other &&
396 "Node doesn't have a token chain argument!");
397 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
398}
399
400/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
401/// see if any uses can reach Dest. If no dest operands can get to dest,
402/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000403///
404/// Keep track of the nodes we fine that actually do lead to Dest in
405/// NodesLeadingTo. This avoids retraversing them exponential number of times.
406///
407bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000408 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000409 if (N == Dest) return true; // N certainly leads to Dest :)
410
Chris Lattner4488f0c2006-07-26 23:55:56 +0000411 // If we've already processed this node and it does lead to Dest, there is no
412 // need to reprocess it.
413 if (NodesLeadingTo.count(N)) return true;
414
Chris Lattner462505f2006-02-13 09:18:02 +0000415 // If the first result of this node has been already legalized, then it cannot
416 // reach N.
417 switch (getTypeAction(N->getValueType(0))) {
418 case Legal:
419 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Promote:
422 if (PromotedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 case Expand:
425 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 }
428
429 // Okay, this node has not already been legalized. Check and legalize all
430 // operands. If none lead to Dest, then we can legalize this node.
431 bool OperandsLeadToDest = false;
432 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
433 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000434 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000435
Chris Lattner4488f0c2006-07-26 23:55:56 +0000436 if (OperandsLeadToDest) {
437 NodesLeadingTo.insert(N);
438 return true;
439 }
Chris Lattner462505f2006-02-13 09:18:02 +0000440
441 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000442 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000443 return false;
444}
445
Dan Gohmana8665142007-06-25 16:23:39 +0000446/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000447/// appropriate for its type.
448void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000449 MVT::ValueType VT = Op.getValueType();
450 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000451 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000452 case Legal: (void)LegalizeOp(Op); break;
453 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000454 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000455 if (!MVT::isVector(VT)) {
456 // If this is an illegal scalar, expand it into its two component
457 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000458 SDOperand X, Y;
459 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000460 } else if (MVT::getVectorNumElements(VT) == 1) {
461 // If this is an illegal single element vector, convert it to a
462 // scalar operation.
463 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000464 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000465 // Otherwise, this is an illegal multiple element vector.
466 // Split it in half and legalize both parts.
467 SDOperand X, Y;
468 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000469 }
470 break;
471 }
472}
Chris Lattner462505f2006-02-13 09:18:02 +0000473
Evan Cheng22cf8992006-12-13 20:57:08 +0000474/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
475/// a load from the constant pool.
476static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000477 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000478 bool Extend = false;
479
480 // If a FP immediate is precise when represented as a float and if the
481 // target can do an extending load from float to double, we put it into
482 // the constant pool as a float, even if it's is statically typed as a
483 // double.
484 MVT::ValueType VT = CFP->getValueType(0);
485 bool isDouble = VT == MVT::f64;
486 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
487 Type::FloatTy, CFP->getValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000488 if (!UseCP) {
Evan Cheng3766fc602006-12-12 22:19:28 +0000489 double Val = LLVMC->getValue();
490 return isDouble
491 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
492 : DAG.getConstant(FloatToBits(Val), MVT::i32);
493 }
494
Evan Cheng47833a12006-12-12 21:32:44 +0000495 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
496 // Only do this if the target has a native EXTLOAD instruction from f32.
497 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
498 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
499 VT = MVT::f32;
500 Extend = true;
501 }
502
503 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
504 if (Extend) {
505 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
506 CPIdx, NULL, 0, MVT::f32);
507 } else {
508 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
509 }
510}
511
Chris Lattner462505f2006-02-13 09:18:02 +0000512
Evan Cheng003feb02007-01-04 21:56:39 +0000513/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
514/// operations.
515static
516SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
517 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000518 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000519 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000520 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
521 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000522 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000523
Evan Cheng003feb02007-01-04 21:56:39 +0000524 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000525 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000526 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
527 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000528 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000529 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000530 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000531 // Shift right or sign-extend it if the two operands have different types.
532 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
533 if (SizeDiff > 0) {
534 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
535 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
536 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
537 } else if (SizeDiff < 0)
538 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539
540 // Clear the sign bit of first operand.
541 SDOperand Mask2 = (VT == MVT::f64)
542 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
543 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
544 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000545 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000546 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
547
548 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000549 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
550 return Result;
551}
552
553
Dan Gohmanff727882007-07-13 20:14:11 +0000554/// LegalizeOp - We know that the specified value has a legal type, and
555/// that its operands are legal. Now ensure that the operation itself
556/// is legal, recursively ensuring that the operands' operations remain
557/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000558SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000559 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000560 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000561 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000562
Chris Lattnerdc750592005-01-07 07:47:09 +0000563 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000564 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000565 if (Node->getNumValues() > 1) {
566 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000567 if (getTypeAction(Node->getValueType(i)) != Legal) {
568 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000569 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000570 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000571 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000572 }
573 }
574
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000575 // Note that LegalizeOp may be reentered even from single-use nodes, which
576 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000577 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000578 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000579
Nate Begemane5b86d72005-08-10 20:51:12 +0000580 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000581 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000582 bool isCustom = false;
583
Chris Lattnerdc750592005-01-07 07:47:09 +0000584 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000585 case ISD::FrameIndex:
586 case ISD::EntryToken:
587 case ISD::Register:
588 case ISD::BasicBlock:
589 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000590 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000591 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000592 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000593 case ISD::TargetConstantPool:
594 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000595 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000596 case ISD::TargetExternalSymbol:
597 case ISD::VALUETYPE:
598 case ISD::SRCVALUE:
599 case ISD::STRING:
600 case ISD::CONDCODE:
601 // Primitives must all be legal.
602 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
603 "This must be legal!");
604 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000605 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000606 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
607 // If this is a target node, legalize it by legalizing the operands then
608 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000609 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000611 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000612
Chris Lattner97af9d52006-08-08 01:09:31 +0000613 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000614
615 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
616 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
617 return Result.getValue(Op.ResNo);
618 }
619 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000620#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000621 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000622#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000623 assert(0 && "Do not know how to legalize this operator!");
624 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000625 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000626 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000627 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000628 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000629 case ISD::ConstantPool:
630 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000631 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
632 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000633 case TargetLowering::Custom:
634 Tmp1 = TLI.LowerOperation(Op, DAG);
635 if (Tmp1.Val) Result = Tmp1;
636 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000637 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000638 break;
639 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000640 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000641 case ISD::FRAMEADDR:
642 case ISD::RETURNADDR:
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000643 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemaneda59972007-01-29 22:58:52 +0000644 // The only option for these nodes is to custom lower them. If the target
645 // does not custom lower them, then return zero.
646 Tmp1 = TLI.LowerOperation(Op, DAG);
647 if (Tmp1.Val)
648 Result = Tmp1;
649 else
650 Result = DAG.getConstant(0, TLI.getPointerTy());
651 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000652 case ISD::EXCEPTIONADDR: {
653 Tmp1 = LegalizeOp(Node->getOperand(0));
654 MVT::ValueType VT = Node->getValueType(0);
655 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
656 default: assert(0 && "This action is not supported yet!");
657 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000658 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000659 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
660 }
661 break;
662 case TargetLowering::Custom:
663 Result = TLI.LowerOperation(Op, DAG);
664 if (Result.Val) break;
665 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000666 case TargetLowering::Legal: {
667 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
668 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
669 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000670 break;
671 }
672 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000673 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000674 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000675 case ISD::EHSELECTION: {
676 Tmp1 = LegalizeOp(Node->getOperand(0));
677 Tmp2 = LegalizeOp(Node->getOperand(1));
678 MVT::ValueType VT = Node->getValueType(0);
679 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
680 default: assert(0 && "This action is not supported yet!");
681 case TargetLowering::Expand: {
682 unsigned Reg = TLI.getExceptionSelectorRegister();
683 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
684 }
685 break;
686 case TargetLowering::Custom:
687 Result = TLI.LowerOperation(Op, DAG);
688 if (Result.Val) break;
689 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000690 case TargetLowering::Legal: {
691 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
692 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
693 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000694 break;
695 }
696 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000697 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000698 break;
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000699 case ISD::EH_RETURN:
700 MVT::ValueType VT = Node->getValueType(0);
701 // The only "good" option for this node is to custom lower it.
702 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
703 default: assert(0 && "This action is not supported at all!");
704 case TargetLowering::Custom:
705 Result = TLI.LowerOperation(Op, DAG);
706 if (Result.Val) break;
707 // Fall Thru
708 case TargetLowering::Legal:
709 // Target does not know, how to lower this, lower to noop
710 Result = LegalizeOp(Node->getOperand(0));
711 break;
712 }
713 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000714 case ISD::AssertSext:
715 case ISD::AssertZext:
716 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000717 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000718 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000719 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000720 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000721 Result = Node->getOperand(Op.ResNo);
722 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000723 case ISD::CopyFromReg:
724 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000725 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000726 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000727 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000728 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000729 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000730 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000731 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000732 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
733 } else {
734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
735 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000736 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
737 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000738 // Since CopyFromReg produces two values, make sure to remember that we
739 // legalized both of them.
740 AddLegalizedOperand(Op.getValue(0), Result);
741 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
742 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000743 case ISD::UNDEF: {
744 MVT::ValueType VT = Op.getValueType();
745 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000746 default: assert(0 && "This action is not supported yet!");
747 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000748 if (MVT::isInteger(VT))
749 Result = DAG.getConstant(0, VT);
750 else if (MVT::isFloatingPoint(VT))
751 Result = DAG.getConstantFP(0, VT);
752 else
753 assert(0 && "Unknown value type!");
754 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000755 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000756 break;
757 }
758 break;
759 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000760
Chris Lattnere55d1712006-03-28 00:40:33 +0000761 case ISD::INTRINSIC_W_CHAIN:
762 case ISD::INTRINSIC_WO_CHAIN:
763 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000764 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000765 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
766 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000767 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000768
769 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000770 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000771 TargetLowering::Custom) {
772 Tmp3 = TLI.LowerOperation(Result, DAG);
773 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000774 }
775
776 if (Result.Val->getNumValues() == 1) break;
777
778 // Must have return value and chain result.
779 assert(Result.Val->getNumValues() == 2 &&
780 "Cannot return more than two values!");
781
782 // Since loads produce two values, make sure to remember that we
783 // legalized both of them.
784 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
785 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
786 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000787 }
Chris Lattner435b4022005-11-29 06:21:05 +0000788
789 case ISD::LOCATION:
790 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
792
793 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
794 case TargetLowering::Promote:
795 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000796 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000797 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000798 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000799 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000800
Jim Laskeyc56315c2007-01-26 21:22:28 +0000801 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000802 const std::string &FName =
803 cast<StringSDNode>(Node->getOperand(3))->getValue();
804 const std::string &DirName =
805 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000806 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000807
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000808 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000809 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000810 SDOperand LineOp = Node->getOperand(1);
811 SDOperand ColOp = Node->getOperand(2);
812
813 if (useDEBUG_LOC) {
814 Ops.push_back(LineOp); // line #
815 Ops.push_back(ColOp); // col #
816 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000817 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000818 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000819 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
820 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000821 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000822 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000823 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000824 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000825 } else {
826 Result = Tmp1; // chain
827 }
Chris Lattner435b4022005-11-29 06:21:05 +0000828 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000829 }
Chris Lattner435b4022005-11-29 06:21:05 +0000830 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000831 if (Tmp1 != Node->getOperand(0) ||
832 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000833 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000834 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000835 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
836 Ops.push_back(Node->getOperand(1)); // line # must be legal.
837 Ops.push_back(Node->getOperand(2)); // col # must be legal.
838 } else {
839 // Otherwise promote them.
840 Ops.push_back(PromoteOp(Node->getOperand(1)));
841 Ops.push_back(PromoteOp(Node->getOperand(2)));
842 }
Chris Lattner435b4022005-11-29 06:21:05 +0000843 Ops.push_back(Node->getOperand(3)); // filename must be legal.
844 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000845 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000846 }
847 break;
848 }
849 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000850
851 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000852 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000853 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000854 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000855 case TargetLowering::Legal:
856 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
857 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
858 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
859 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000861 break;
862 }
863 break;
864
Jim Laskeyf9e54452007-01-26 14:34:52 +0000865 case ISD::LABEL:
866 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
867 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000868 default: assert(0 && "This action is not supported yet!");
869 case TargetLowering::Legal:
870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
871 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000873 break;
Chris Lattner567b9252007-03-03 19:21:38 +0000874 case TargetLowering::Expand:
875 Result = LegalizeOp(Node->getOperand(0));
876 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000877 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000878 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000879
Chris Lattnerdc750592005-01-07 07:47:09 +0000880 case ISD::Constant:
881 // We know we don't need to expand constants here, constants only have one
882 // value and we check that it is fine above.
883
884 // FIXME: Maybe we should handle things like targets that don't support full
885 // 32-bit immediates?
886 break;
887 case ISD::ConstantFP: {
888 // Spill FP immediates to the constant pool if the target cannot directly
889 // codegen them. Targets often have some immediate values that can be
890 // efficiently generated into an FP register without a load. We explicitly
891 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000892 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
893
894 // Check to see if this FP immediate is already legal.
895 bool isLegal = false;
896 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
897 E = TLI.legal_fpimm_end(); I != E; ++I)
898 if (CFP->isExactlyValue(*I)) {
899 isLegal = true;
900 break;
901 }
902
Chris Lattner758b0ac2006-01-29 06:26:56 +0000903 // If this is a legal constant, turn it into a TargetConstantFP node.
904 if (isLegal) {
905 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
906 break;
907 }
908
909 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
910 default: assert(0 && "This action is not supported yet!");
911 case TargetLowering::Custom:
912 Tmp3 = TLI.LowerOperation(Result, DAG);
913 if (Tmp3.Val) {
914 Result = Tmp3;
915 break;
916 }
917 // FALLTHROUGH
918 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000919 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000920 }
921 break;
922 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000923 case ISD::TokenFactor:
924 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000925 Tmp1 = LegalizeOp(Node->getOperand(0));
926 Tmp2 = LegalizeOp(Node->getOperand(1));
927 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
928 } else if (Node->getNumOperands() == 3) {
929 Tmp1 = LegalizeOp(Node->getOperand(0));
930 Tmp2 = LegalizeOp(Node->getOperand(1));
931 Tmp3 = LegalizeOp(Node->getOperand(2));
932 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000933 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000934 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000935 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000936 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
937 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000938 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000939 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000940 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000941
942 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000943 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000944 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000945 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
946 assert(Tmp3.Val && "Target didn't custom lower this node!");
947 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
948 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000949
Chris Lattneraaa23d92006-05-16 22:53:20 +0000950 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000951 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000952 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
953 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000954 if (Op.ResNo == i)
955 Tmp2 = Tmp1;
956 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
957 }
958 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000959
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000960 case ISD::BUILD_VECTOR:
961 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000962 default: assert(0 && "This action is not supported yet!");
963 case TargetLowering::Custom:
964 Tmp3 = TLI.LowerOperation(Result, DAG);
965 if (Tmp3.Val) {
966 Result = Tmp3;
967 break;
968 }
969 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000970 case TargetLowering::Expand:
971 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000972 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000973 }
Chris Lattner29b23012006-03-19 01:17:20 +0000974 break;
975 case ISD::INSERT_VECTOR_ELT:
976 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
977 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
978 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
980
981 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
982 Node->getValueType(0))) {
983 default: assert(0 && "This action is not supported yet!");
984 case TargetLowering::Legal:
985 break;
986 case TargetLowering::Custom:
987 Tmp3 = TLI.LowerOperation(Result, DAG);
988 if (Tmp3.Val) {
989 Result = Tmp3;
990 break;
991 }
992 // FALLTHROUGH
993 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +0000994 // If the insert index is a constant, codegen this as a scalar_to_vector,
995 // then a shuffle that inserts it into the right position in the vector.
996 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
997 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
998 Tmp1.getValueType(), Tmp2);
999
1000 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1001 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001002 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001003
1004 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1005 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1006 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001007 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001008 for (unsigned i = 0; i != NumElts; ++i) {
1009 if (i != InsertPos->getValue())
1010 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1011 else
1012 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1013 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001014 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1015 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001016
1017 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1018 Tmp1, ScVec, ShufMask);
1019 Result = LegalizeOp(Result);
1020 break;
1021 }
1022
Chris Lattner29b23012006-03-19 01:17:20 +00001023 // If the target doesn't support this, we have to spill the input vector
1024 // to a temporary stack slot, update the element, then reload it. This is
1025 // badness. We could also load the value into a vector register (either
1026 // with a "move to register" or "extload into register" instruction, then
1027 // permute it into place, if the idx is a constant and if the idx is
1028 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001029 MVT::ValueType VT = Tmp1.getValueType();
1030 MVT::ValueType EltVT = Tmp2.getValueType();
1031 MVT::ValueType IdxVT = Tmp3.getValueType();
1032 MVT::ValueType PtrVT = TLI.getPointerTy();
1033 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001034 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001035 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001036
1037 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001038 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1039 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001040 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001041 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1042 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1043 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001044 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001045 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001046 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001047 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001048 break;
1049 }
1050 }
1051 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001052 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001053 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1054 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1055 break;
1056 }
1057
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001058 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1060 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1061 Node->getValueType(0))) {
1062 default: assert(0 && "This action is not supported yet!");
1063 case TargetLowering::Legal:
1064 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001065 case TargetLowering::Custom:
1066 Tmp3 = TLI.LowerOperation(Result, DAG);
1067 if (Tmp3.Val) {
1068 Result = Tmp3;
1069 break;
1070 }
1071 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001072 case TargetLowering::Expand:
1073 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001074 break;
1075 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001076 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001077 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001078 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1079 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1081
1082 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001083 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1084 default: assert(0 && "Unknown operation action!");
1085 case TargetLowering::Legal:
1086 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1087 "vector shuffle should not be created if not legal!");
1088 break;
1089 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001090 Tmp3 = TLI.LowerOperation(Result, DAG);
1091 if (Tmp3.Val) {
1092 Result = Tmp3;
1093 break;
1094 }
1095 // FALLTHROUGH
1096 case TargetLowering::Expand: {
1097 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001098 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001099 MVT::ValueType PtrVT = TLI.getPointerTy();
1100 SDOperand Mask = Node->getOperand(2);
1101 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001102 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001103 for (unsigned i = 0; i != NumElems; ++i) {
1104 SDOperand Arg = Mask.getOperand(i);
1105 if (Arg.getOpcode() == ISD::UNDEF) {
1106 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1107 } else {
1108 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1109 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1110 if (Idx < NumElems)
1111 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1112 DAG.getConstant(Idx, PtrVT)));
1113 else
1114 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1115 DAG.getConstant(Idx - NumElems, PtrVT)));
1116 }
1117 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001118 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001119 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001120 }
Chris Lattner6be79822006-04-04 17:23:26 +00001121 case TargetLowering::Promote: {
1122 // Change base type to a different vector type.
1123 MVT::ValueType OVT = Node->getValueType(0);
1124 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1125
1126 // Cast the two input vectors.
1127 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1128 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1129
1130 // Convert the shuffle mask to the right # elements.
1131 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1132 assert(Tmp3.Val && "Shuffle not legal?");
1133 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1134 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1135 break;
1136 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001137 }
1138 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001139
1140 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001141 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001142 Tmp2 = LegalizeOp(Node->getOperand(1));
1143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001144 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001145 break;
1146
Dan Gohmana8665142007-06-25 16:23:39 +00001147 case ISD::EXTRACT_SUBVECTOR:
1148 Tmp1 = Node->getOperand(0);
1149 Tmp2 = LegalizeOp(Node->getOperand(1));
1150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1151 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001152 break;
1153
Chris Lattner462505f2006-02-13 09:18:02 +00001154 case ISD::CALLSEQ_START: {
1155 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1156
1157 // Recursively Legalize all of the inputs of the call end that do not lead
1158 // to this call start. This ensures that any libcalls that need be inserted
1159 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001160 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001161 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001162 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1163 NodesLeadingTo);
1164 }
Chris Lattner462505f2006-02-13 09:18:02 +00001165
1166 // Now that we legalized all of the inputs (which may have inserted
1167 // libcalls) create the new CALLSEQ_START node.
1168 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1169
1170 // Merge in the last call, to ensure that this call start after the last
1171 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001172 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001173 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1174 Tmp1 = LegalizeOp(Tmp1);
1175 }
Chris Lattner462505f2006-02-13 09:18:02 +00001176
1177 // Do not try to legalize the target-specific arguments (#1+).
1178 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001179 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001180 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001181 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001182 }
1183
1184 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001185 AddLegalizedOperand(Op.getValue(0), Result);
1186 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1187 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1188
Chris Lattner462505f2006-02-13 09:18:02 +00001189 // Now that the callseq_start and all of the non-call nodes above this call
1190 // sequence have been legalized, legalize the call itself. During this
1191 // process, no libcalls can/will be inserted, guaranteeing that no calls
1192 // can overlap.
1193 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1194 SDOperand InCallSEQ = LastCALLSEQ_END;
1195 // Note that we are selecting this call!
1196 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1197 IsLegalizingCall = true;
1198
1199 // Legalize the call, starting from the CALLSEQ_END.
1200 LegalizeOp(LastCALLSEQ_END);
1201 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1202 return Result;
1203 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001204 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001205 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1206 // will cause this node to be legalized as well as handling libcalls right.
1207 if (LastCALLSEQ_END.Val != Node) {
1208 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001209 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001210 assert(I != LegalizedNodes.end() &&
1211 "Legalizing the call start should have legalized this node!");
1212 return I->second;
1213 }
1214
1215 // Otherwise, the call start has been legalized and everything is going
1216 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001218 // Do not try to legalize the target-specific arguments (#1+), except for
1219 // an optional flag input.
1220 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1221 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001222 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001223 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001224 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001225 }
1226 } else {
1227 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1228 if (Tmp1 != Node->getOperand(0) ||
1229 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001230 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001231 Ops[0] = Tmp1;
1232 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001233 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001234 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001235 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001236 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001237 // This finishes up call legalization.
1238 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001239
1240 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1241 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1242 if (Node->getNumValues() == 2)
1243 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1244 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001245 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1247 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1248 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001250
Chris Lattnerd02b0542006-01-28 10:58:55 +00001251 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001252 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001253 switch (TLI.getOperationAction(Node->getOpcode(),
1254 Node->getValueType(0))) {
1255 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001256 case TargetLowering::Expand: {
1257 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1258 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1259 " not tell us which reg is the stack pointer!");
1260 SDOperand Chain = Tmp1.getOperand(0);
1261 SDOperand Size = Tmp2.getOperand(1);
1262 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1263 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1264 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001265 Tmp1 = LegalizeOp(Tmp1);
1266 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001267 break;
1268 }
1269 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001270 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1271 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001272 Tmp1 = LegalizeOp(Tmp3);
1273 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001274 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001275 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001276 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001277 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001278 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001279 // Since this op produce two values, make sure to remember that we
1280 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001281 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1282 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001283 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001284 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001285 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001286 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001287 bool Changed = false;
1288 // Legalize all of the operands of the inline asm, in case they are nodes
1289 // that need to be expanded or something. Note we skip the asm string and
1290 // all of the TargetConstant flags.
1291 SDOperand Op = LegalizeOp(Ops[0]);
1292 Changed = Op != Ops[0];
1293 Ops[0] = Op;
1294
1295 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1296 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1297 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1298 for (++i; NumVals; ++i, --NumVals) {
1299 SDOperand Op = LegalizeOp(Ops[i]);
1300 if (Op != Ops[i]) {
1301 Changed = true;
1302 Ops[i] = Op;
1303 }
1304 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001305 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001306
1307 if (HasInFlag) {
1308 Op = LegalizeOp(Ops.back());
1309 Changed |= Op != Ops.back();
1310 Ops.back() = Op;
1311 }
1312
1313 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001314 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001315
1316 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001317 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001318 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1319 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001320 }
Chris Lattner68a12142005-01-07 22:12:08 +00001321 case ISD::BR:
1322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001323 // Ensure that libcalls are emitted before a branch.
1324 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1325 Tmp1 = LegalizeOp(Tmp1);
1326 LastCALLSEQ_END = DAG.getEntryNode();
1327
Chris Lattnerd02b0542006-01-28 10:58:55 +00001328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001329 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001330 case ISD::BRIND:
1331 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1332 // Ensure that libcalls are emitted before a branch.
1333 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1334 Tmp1 = LegalizeOp(Tmp1);
1335 LastCALLSEQ_END = DAG.getEntryNode();
1336
1337 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1338 default: assert(0 && "Indirect target must be legal type (pointer)!");
1339 case Legal:
1340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1341 break;
1342 }
1343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1344 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001345 case ISD::BR_JT:
1346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1347 // Ensure that libcalls are emitted before a branch.
1348 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1349 Tmp1 = LegalizeOp(Tmp1);
1350 LastCALLSEQ_END = DAG.getEntryNode();
1351
1352 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1354
1355 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1356 default: assert(0 && "This action is not supported yet!");
1357 case TargetLowering::Legal: break;
1358 case TargetLowering::Custom:
1359 Tmp1 = TLI.LowerOperation(Result, DAG);
1360 if (Tmp1.Val) Result = Tmp1;
1361 break;
1362 case TargetLowering::Expand: {
1363 SDOperand Chain = Result.getOperand(0);
1364 SDOperand Table = Result.getOperand(1);
1365 SDOperand Index = Result.getOperand(2);
1366
1367 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001368 MachineFunction &MF = DAG.getMachineFunction();
1369 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001370 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1371 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001372
1373 SDOperand LD;
1374 switch (EntrySize) {
1375 default: assert(0 && "Size of jump table not supported yet."); break;
1376 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1377 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1378 }
1379
1380 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001381 // For PIC, the sequence is:
1382 // BRIND(load(Jumptable + index) + RelocBase)
1383 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1384 SDOperand Reloc;
1385 if (TLI.usesGlobalOffsetTable())
1386 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1387 else
1388 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001389 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001390 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1391 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1392 } else {
1393 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1394 }
1395 }
1396 }
1397 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001398 case ISD::BRCOND:
1399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001400 // Ensure that libcalls are emitted before a return.
1401 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1402 Tmp1 = LegalizeOp(Tmp1);
1403 LastCALLSEQ_END = DAG.getEntryNode();
1404
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001405 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1406 case Expand: assert(0 && "It's impossible to expand bools");
1407 case Legal:
1408 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1409 break;
1410 case Promote:
1411 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001412
1413 // The top bits of the promoted condition are not necessarily zero, ensure
1414 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001415 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001416 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1417 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001418 break;
1419 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001420
1421 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001422 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001423
1424 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1425 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001426 case TargetLowering::Legal: break;
1427 case TargetLowering::Custom:
1428 Tmp1 = TLI.LowerOperation(Result, DAG);
1429 if (Tmp1.Val) Result = Tmp1;
1430 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001431 case TargetLowering::Expand:
1432 // Expand brcond's setcc into its constituent parts and create a BR_CC
1433 // Node.
1434 if (Tmp2.getOpcode() == ISD::SETCC) {
1435 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1436 Tmp2.getOperand(0), Tmp2.getOperand(1),
1437 Node->getOperand(2));
1438 } else {
1439 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1440 DAG.getCondCode(ISD::SETNE), Tmp2,
1441 DAG.getConstant(0, Tmp2.getValueType()),
1442 Node->getOperand(2));
1443 }
1444 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001445 }
1446 break;
1447 case ISD::BR_CC:
1448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001449 // Ensure that libcalls are emitted before a branch.
1450 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1451 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001452 Tmp2 = Node->getOperand(2); // LHS
1453 Tmp3 = Node->getOperand(3); // RHS
1454 Tmp4 = Node->getOperand(1); // CC
1455
1456 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001457 LastCALLSEQ_END = DAG.getEntryNode();
1458
Nate Begeman7e7f4392006-02-01 07:19:44 +00001459 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1460 // the LHS is a legal SETCC itself. In this case, we need to compare
1461 // the result against zero to select between true and false values.
1462 if (Tmp3.Val == 0) {
1463 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1464 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001465 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001466
1467 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1468 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001469
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001470 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1471 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001472 case TargetLowering::Legal: break;
1473 case TargetLowering::Custom:
1474 Tmp4 = TLI.LowerOperation(Result, DAG);
1475 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001476 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001477 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001478 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001479 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001480 LoadSDNode *LD = cast<LoadSDNode>(Node);
1481 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1482 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001483
Evan Chenge71fe34d2006-10-09 20:57:25 +00001484 ISD::LoadExtType ExtType = LD->getExtensionType();
1485 if (ExtType == ISD::NON_EXTLOAD) {
1486 MVT::ValueType VT = Node->getValueType(0);
1487 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1488 Tmp3 = Result.getValue(0);
1489 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001490
Evan Chenge71fe34d2006-10-09 20:57:25 +00001491 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1492 default: assert(0 && "This action is not supported yet!");
1493 case TargetLowering::Legal: break;
1494 case TargetLowering::Custom:
1495 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1496 if (Tmp1.Val) {
1497 Tmp3 = LegalizeOp(Tmp1);
1498 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001499 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001500 break;
1501 case TargetLowering::Promote: {
1502 // Only promote a load of vector type to another.
1503 assert(MVT::isVector(VT) && "Cannot promote this load!");
1504 // Change base type to a different vector type.
1505 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1506
1507 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001508 LD->getSrcValueOffset(),
1509 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001510 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1511 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001512 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001513 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001514 }
1515 // Since loads produce two values, make sure to remember that we
1516 // legalized both of them.
1517 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1518 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1519 return Op.ResNo ? Tmp4 : Tmp3;
1520 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001521 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001522 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1523 default: assert(0 && "This action is not supported yet!");
1524 case TargetLowering::Promote:
1525 assert(SrcVT == MVT::i1 &&
1526 "Can only promote extending LOAD from i1 -> i8!");
1527 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1528 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001529 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001530 Tmp1 = Result.getValue(0);
1531 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001532 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001533 case TargetLowering::Custom:
1534 isCustom = true;
1535 // FALLTHROUGH
1536 case TargetLowering::Legal:
1537 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1538 Tmp1 = Result.getValue(0);
1539 Tmp2 = Result.getValue(1);
1540
1541 if (isCustom) {
1542 Tmp3 = TLI.LowerOperation(Result, DAG);
1543 if (Tmp3.Val) {
1544 Tmp1 = LegalizeOp(Tmp3);
1545 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1546 }
1547 }
1548 break;
1549 case TargetLowering::Expand:
1550 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1551 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1552 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001553 LD->getSrcValueOffset(),
1554 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001555 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1556 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1557 Tmp2 = LegalizeOp(Load.getValue(1));
1558 break;
1559 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001560 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001561 // Turn the unsupported load into an EXTLOAD followed by an explicit
1562 // zero/sign extend inreg.
1563 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1564 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001565 LD->getSrcValueOffset(), SrcVT,
1566 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001567 SDOperand ValRes;
1568 if (ExtType == ISD::SEXTLOAD)
1569 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1570 Result, DAG.getValueType(SrcVT));
1571 else
1572 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1573 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1574 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1575 break;
1576 }
1577 // Since loads produce two values, make sure to remember that we legalized
1578 // both of them.
1579 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1580 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1581 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001582 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001583 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001584 case ISD::EXTRACT_ELEMENT: {
1585 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1586 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001587 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001588 case Legal:
1589 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1590 // 1 -> Hi
1591 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1592 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1593 TLI.getShiftAmountTy()));
1594 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1595 } else {
1596 // 0 -> Lo
1597 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1598 Node->getOperand(0));
1599 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001600 break;
1601 case Expand:
1602 // Get both the low and high parts.
1603 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1604 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1605 Result = Tmp2; // 1 -> Hi
1606 else
1607 Result = Tmp1; // 0 -> Lo
1608 break;
1609 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001610 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001611 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001612
1613 case ISD::CopyToReg:
1614 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001615
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001616 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001617 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001618 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001619 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001620 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001622 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001623 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001624 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001625 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001626 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1627 Tmp3);
1628 } else {
1629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001630 }
1631
1632 // Since this produces two values, make sure to remember that we legalized
1633 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001634 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001635 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001636 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001637 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001638 break;
1639
1640 case ISD::RET:
1641 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001642
1643 // Ensure that libcalls are emitted before a return.
1644 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1645 Tmp1 = LegalizeOp(Tmp1);
1646 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001647
Chris Lattnerdc750592005-01-07 07:47:09 +00001648 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001649 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001650 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001651 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001652 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001653 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001654 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001655 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001656 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001657 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001658 SDOperand Lo, Hi;
1659 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001660
1661 // Big endian systems want the hi reg first.
1662 if (!TLI.isLittleEndian())
1663 std::swap(Lo, Hi);
1664
Evan Cheng3432ab92006-12-11 19:27:14 +00001665 if (Hi.Val)
1666 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1667 else
1668 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001669 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001670 } else {
1671 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001672 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1673 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001674
Dan Gohmana8665142007-06-25 16:23:39 +00001675 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001676 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001677 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001678 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001679 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001680 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001681 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001682 } else if (NumElems == 1) {
1683 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001684 Tmp2 = ScalarizeVectorOp(Tmp2);
1685 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001686 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001687
1688 // FIXME: Returns of gcc generic vectors smaller than a legal type
1689 // should be returned in integer registers!
1690
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001691 // The scalarized value type may not be legal, e.g. it might require
1692 // promotion or expansion. Relegalize the return.
1693 Result = LegalizeOp(Result);
1694 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001695 // FIXME: Returns of gcc generic vectors larger than a legal vector
1696 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001697 SDOperand Lo, Hi;
1698 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001699 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001700 Result = LegalizeOp(Result);
1701 }
1702 }
Misha Brukman835702a2005-04-21 22:36:52 +00001703 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001704 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001705 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001706 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001707 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001708 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001709 }
1710 break;
1711 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001712 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001713 break;
1714 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001715 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001716 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001717 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001718 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1719 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001720 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001721 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001722 break;
1723 case Expand: {
1724 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001725 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001726 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001727 ExpandOp(Node->getOperand(i), Lo, Hi);
1728 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001729 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001730 if (Hi.Val) {
1731 NewValues.push_back(Hi);
1732 NewValues.push_back(Node->getOperand(i+1));
1733 }
Misha Brukman835702a2005-04-21 22:36:52 +00001734 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001735 }
1736 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001737 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001738 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001739
1740 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001741 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001742 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001743 Result = DAG.getNode(ISD::RET, MVT::Other,
1744 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001745 break;
1746 }
1747 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001748
Chris Lattner4d1ea712006-01-29 21:02:23 +00001749 if (Result.getOpcode() == ISD::RET) {
1750 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1751 default: assert(0 && "This action is not supported yet!");
1752 case TargetLowering::Legal: break;
1753 case TargetLowering::Custom:
1754 Tmp1 = TLI.LowerOperation(Result, DAG);
1755 if (Tmp1.Val) Result = Tmp1;
1756 break;
1757 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001758 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001759 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001760 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001761 StoreSDNode *ST = cast<StoreSDNode>(Node);
1762 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1763 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00001764 int SVOffset = ST->getSrcValueOffset();
1765 unsigned Alignment = ST->getAlignment();
1766 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00001767
Evan Chengab51cf22006-10-13 21:14:26 +00001768 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001769 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1770 // FIXME: We shouldn't do this for TargetConstantFP's.
1771 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1772 // to phase ordering between legalized code and the dag combiner. This
1773 // probably means that we need to integrate dag combiner and legalizer
1774 // together.
1775 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1776 if (CFP->getValueType(0) == MVT::f32) {
1777 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1778 } else {
1779 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1780 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1781 }
1782 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001783 SVOffset, isVolatile, Alignment);
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001784 break;
1785 }
1786
Evan Chengab51cf22006-10-13 21:14:26 +00001787 switch (getTypeAction(ST->getStoredVT())) {
1788 case Legal: {
1789 Tmp3 = LegalizeOp(ST->getValue());
1790 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1791 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001792
Evan Chengab51cf22006-10-13 21:14:26 +00001793 MVT::ValueType VT = Tmp3.getValueType();
1794 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1795 default: assert(0 && "This action is not supported yet!");
1796 case TargetLowering::Legal: break;
1797 case TargetLowering::Custom:
1798 Tmp1 = TLI.LowerOperation(Result, DAG);
1799 if (Tmp1.Val) Result = Tmp1;
1800 break;
1801 case TargetLowering::Promote:
1802 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1803 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1804 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1805 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00001806 ST->getSrcValue(), SVOffset, isVolatile,
1807 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001808 break;
1809 }
1810 break;
1811 }
1812 case Promote:
1813 // Truncate the value and store the result.
1814 Tmp3 = PromoteOp(ST->getValue());
1815 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001816 SVOffset, ST->getStoredVT(),
1817 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001818 break;
1819
1820 case Expand:
1821 unsigned IncrementSize = 0;
1822 SDOperand Lo, Hi;
1823
1824 // If this is a vector type, then we have to calculate the increment as
1825 // the product of the element size in bytes, and the number of elements
1826 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00001827 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00001828 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001829 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1830 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00001831
Dan Gohmana8665142007-06-25 16:23:39 +00001832 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001833 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001834 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001835 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001836 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001837 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001838 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001839 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001840 Result = LegalizeOp(Result);
1841 break;
1842 } else if (NumElems == 1) {
1843 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001844 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001845 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001846 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001847 // The scalarized value type may not be legal, e.g. it might require
1848 // promotion or expansion. Relegalize the scalar store.
1849 Result = LegalizeOp(Result);
1850 break;
1851 } else {
1852 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1853 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1854 }
1855 } else {
1856 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001857 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001858
1859 if (!TLI.isLittleEndian())
1860 std::swap(Lo, Hi);
1861 }
1862
1863 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001864 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00001865
1866 if (Hi.Val == NULL) {
1867 // Must be int <-> float one-to-one expansion.
1868 Result = Lo;
1869 break;
1870 }
1871
Evan Chengab51cf22006-10-13 21:14:26 +00001872 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1873 getIntPtrConstant(IncrementSize));
1874 assert(isTypeLegal(Tmp2.getValueType()) &&
1875 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00001876 SVOffset += IncrementSize;
1877 if (Alignment > IncrementSize)
1878 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00001879 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001880 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001881 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1882 break;
1883 }
1884 } else {
1885 // Truncating store
1886 assert(isTypeLegal(ST->getValue().getValueType()) &&
1887 "Cannot handle illegal TRUNCSTORE yet!");
1888 Tmp3 = LegalizeOp(ST->getValue());
1889
1890 // The only promote case we handle is TRUNCSTORE:i1 X into
1891 // -> TRUNCSTORE:i8 (and X, 1)
1892 if (ST->getStoredVT() == MVT::i1 &&
1893 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1894 // Promote the bool to a mask then store.
1895 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1896 DAG.getConstant(1, Tmp3.getValueType()));
1897 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001898 SVOffset, MVT::i8,
1899 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001900 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1901 Tmp2 != ST->getBasePtr()) {
1902 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1903 ST->getOffset());
1904 }
1905
1906 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1907 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001908 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001909 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001910 case TargetLowering::Custom:
1911 Tmp1 = TLI.LowerOperation(Result, DAG);
1912 if (Tmp1.Val) Result = Tmp1;
1913 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001914 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001915 }
1916 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001917 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001918 case ISD::PCMARKER:
1919 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001921 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001922 case ISD::STACKSAVE:
1923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001924 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1925 Tmp1 = Result.getValue(0);
1926 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001927
Chris Lattnerb3266452006-01-13 02:50:02 +00001928 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1929 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001930 case TargetLowering::Legal: break;
1931 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001932 Tmp3 = TLI.LowerOperation(Result, DAG);
1933 if (Tmp3.Val) {
1934 Tmp1 = LegalizeOp(Tmp3);
1935 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001936 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001937 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001938 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001939 // Expand to CopyFromReg if the target set
1940 // StackPointerRegisterToSaveRestore.
1941 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001942 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001943 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001944 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001945 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001946 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1947 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001948 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001949 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001950 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001951
1952 // Since stacksave produce two values, make sure to remember that we
1953 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001954 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1955 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1956 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001957
Chris Lattnerb3266452006-01-13 02:50:02 +00001958 case ISD::STACKRESTORE:
1959 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1960 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001962
1963 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1964 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001965 case TargetLowering::Legal: break;
1966 case TargetLowering::Custom:
1967 Tmp1 = TLI.LowerOperation(Result, DAG);
1968 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001969 break;
1970 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001971 // Expand to CopyToReg if the target set
1972 // StackPointerRegisterToSaveRestore.
1973 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1974 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1975 } else {
1976 Result = Tmp1;
1977 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001978 break;
1979 }
1980 break;
1981
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001982 case ISD::READCYCLECOUNTER:
1983 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001984 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001985 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1986 Node->getValueType(0))) {
1987 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001988 case TargetLowering::Legal:
1989 Tmp1 = Result.getValue(0);
1990 Tmp2 = Result.getValue(1);
1991 break;
Evan Cheng69739932006-11-29 08:26:18 +00001992 case TargetLowering::Custom:
1993 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00001994 Tmp1 = LegalizeOp(Result.getValue(0));
1995 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00001996 break;
1997 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001998
1999 // Since rdcc produce two values, make sure to remember that we legalized
2000 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002001 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2002 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002003 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002004
Chris Lattner39c67442005-01-14 22:08:15 +00002005 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002006 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2007 case Expand: assert(0 && "It's impossible to expand bools");
2008 case Legal:
2009 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2010 break;
2011 case Promote:
2012 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002013 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002014 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002015 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2016 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002017 break;
2018 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002019 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002020 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002021
Chris Lattnerd02b0542006-01-28 10:58:55 +00002022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002023
Nate Begeman987121a2005-08-23 04:29:48 +00002024 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002025 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002026 case TargetLowering::Legal: break;
2027 case TargetLowering::Custom: {
2028 Tmp1 = TLI.LowerOperation(Result, DAG);
2029 if (Tmp1.Val) Result = Tmp1;
2030 break;
2031 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002032 case TargetLowering::Expand:
2033 if (Tmp1.getOpcode() == ISD::SETCC) {
2034 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2035 Tmp2, Tmp3,
2036 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2037 } else {
2038 Result = DAG.getSelectCC(Tmp1,
2039 DAG.getConstant(0, Tmp1.getValueType()),
2040 Tmp2, Tmp3, ISD::SETNE);
2041 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002042 break;
2043 case TargetLowering::Promote: {
2044 MVT::ValueType NVT =
2045 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2046 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002047 if (MVT::isVector(Tmp2.getValueType())) {
2048 ExtOp = ISD::BIT_CONVERT;
2049 TruncOp = ISD::BIT_CONVERT;
2050 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002051 ExtOp = ISD::ANY_EXTEND;
2052 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002053 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002054 ExtOp = ISD::FP_EXTEND;
2055 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002056 }
2057 // Promote each of the values to the new type.
2058 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2059 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2060 // Perform the larger operation, then round down.
2061 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2062 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2063 break;
2064 }
2065 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002066 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002067 case ISD::SELECT_CC: {
2068 Tmp1 = Node->getOperand(0); // LHS
2069 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002070 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2071 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002072 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002073
Nate Begeman7e7f4392006-02-01 07:19:44 +00002074 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2075
2076 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2077 // the LHS is a legal SETCC itself. In this case, we need to compare
2078 // the result against zero to select between true and false values.
2079 if (Tmp2.Val == 0) {
2080 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2081 CC = DAG.getCondCode(ISD::SETNE);
2082 }
2083 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2084
2085 // Everything is legal, see if we should expand this op or something.
2086 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2087 default: assert(0 && "This action is not supported yet!");
2088 case TargetLowering::Legal: break;
2089 case TargetLowering::Custom:
2090 Tmp1 = TLI.LowerOperation(Result, DAG);
2091 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002092 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002093 }
2094 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002095 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002096 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002097 Tmp1 = Node->getOperand(0);
2098 Tmp2 = Node->getOperand(1);
2099 Tmp3 = Node->getOperand(2);
2100 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2101
2102 // If we had to Expand the SetCC operands into a SELECT node, then it may
2103 // not always be possible to return a true LHS & RHS. In this case, just
2104 // return the value we legalized, returned in the LHS
2105 if (Tmp2.Val == 0) {
2106 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002107 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002108 }
Nate Begeman987121a2005-08-23 04:29:48 +00002109
Chris Lattnerf263a232006-01-30 22:43:50 +00002110 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002111 default: assert(0 && "Cannot handle this action for SETCC yet!");
2112 case TargetLowering::Custom:
2113 isCustom = true;
2114 // FALLTHROUGH.
2115 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002116 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002117 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002118 Tmp4 = TLI.LowerOperation(Result, DAG);
2119 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002120 }
Nate Begeman987121a2005-08-23 04:29:48 +00002121 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002122 case TargetLowering::Promote: {
2123 // First step, figure out the appropriate operation to use.
2124 // Allow SETCC to not be supported for all legal data types
2125 // Mostly this targets FP
2126 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002127 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002128
2129 // Scan for the appropriate larger type to use.
2130 while (1) {
2131 NewInTy = (MVT::ValueType)(NewInTy+1);
2132
2133 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2134 "Fell off of the edge of the integer world");
2135 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2136 "Fell off of the edge of the floating point world");
2137
2138 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002139 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002140 break;
2141 }
2142 if (MVT::isInteger(NewInTy))
2143 assert(0 && "Cannot promote Legal Integer SETCC yet");
2144 else {
2145 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2146 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2147 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002148 Tmp1 = LegalizeOp(Tmp1);
2149 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002151 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002152 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002153 }
Nate Begeman987121a2005-08-23 04:29:48 +00002154 case TargetLowering::Expand:
2155 // Expand a setcc node into a select_cc of the same condition, lhs, and
2156 // rhs that selects between const 1 (true) and const 0 (false).
2157 MVT::ValueType VT = Node->getValueType(0);
2158 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2159 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002160 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002161 break;
2162 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002163 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002164 case ISD::MEMSET:
2165 case ISD::MEMCPY:
2166 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002167 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002168 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2169
2170 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2171 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2172 case Expand: assert(0 && "Cannot expand a byte!");
2173 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002174 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002175 break;
2176 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002177 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002178 break;
2179 }
2180 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002181 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002182 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002183
2184 SDOperand Tmp4;
2185 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002186 case Expand: {
2187 // Length is too big, just take the lo-part of the length.
2188 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002189 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002190 break;
2191 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002192 case Legal:
2193 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002194 break;
2195 case Promote:
2196 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002197 break;
2198 }
2199
2200 SDOperand Tmp5;
2201 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2202 case Expand: assert(0 && "Cannot expand this yet!");
2203 case Legal:
2204 Tmp5 = LegalizeOp(Node->getOperand(4));
2205 break;
2206 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002207 Tmp5 = PromoteOp(Node->getOperand(4));
2208 break;
2209 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002210
2211 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2212 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002213 case TargetLowering::Custom:
2214 isCustom = true;
2215 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002216 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002218 if (isCustom) {
2219 Tmp1 = TLI.LowerOperation(Result, DAG);
2220 if (Tmp1.Val) Result = Tmp1;
2221 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002222 break;
2223 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002224 // Otherwise, the target does not support this operation. Lower the
2225 // operation to an explicit libcall as appropriate.
2226 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002227 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002228 TargetLowering::ArgListTy Args;
2229 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002230
Reid Spencer6dced922005-01-12 14:53:45 +00002231 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002232 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002233 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002234 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002235 // Extend the (previously legalized) ubyte argument to be an int value
2236 // for the call.
2237 if (Tmp3.getValueType() > MVT::i32)
2238 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2239 else
2240 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002241 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002242 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002243 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002244 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002245
2246 FnName = "memset";
2247 } else if (Node->getOpcode() == ISD::MEMCPY ||
2248 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002249 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002250 Entry.Node = Tmp2; Args.push_back(Entry);
2251 Entry.Node = Tmp3; Args.push_back(Entry);
2252 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002253 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2254 } else {
2255 assert(0 && "Unknown op!");
2256 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002257
Chris Lattner85d70c62005-01-11 05:57:22 +00002258 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002259 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002260 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002262 break;
2263 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002264 }
2265 break;
2266 }
Chris Lattner5385db52005-05-09 20:23:03 +00002267
Chris Lattner4157c412005-04-02 04:00:59 +00002268 case ISD::SHL_PARTS:
2269 case ISD::SRA_PARTS:
2270 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002271 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002272 bool Changed = false;
2273 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2274 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2275 Changed |= Ops.back() != Node->getOperand(i);
2276 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002277 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002278 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002279
Evan Cheng870e4f82006-01-09 18:31:59 +00002280 switch (TLI.getOperationAction(Node->getOpcode(),
2281 Node->getValueType(0))) {
2282 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002283 case TargetLowering::Legal: break;
2284 case TargetLowering::Custom:
2285 Tmp1 = TLI.LowerOperation(Result, DAG);
2286 if (Tmp1.Val) {
2287 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002288 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002289 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002290 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2291 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002292 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002293 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002294 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002295 return RetVal;
2296 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002297 break;
2298 }
2299
Chris Lattner13fe99c2005-04-02 05:00:07 +00002300 // Since these produce multiple values, make sure to remember that we
2301 // legalized all of them.
2302 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2303 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2304 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002305 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002306
2307 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002308 case ISD::ADD:
2309 case ISD::SUB:
2310 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002311 case ISD::MULHS:
2312 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002313 case ISD::UDIV:
2314 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 case ISD::AND:
2316 case ISD::OR:
2317 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002318 case ISD::SHL:
2319 case ISD::SRL:
2320 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002321 case ISD::FADD:
2322 case ISD::FSUB:
2323 case ISD::FMUL:
2324 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002325 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002326 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2327 case Expand: assert(0 && "Not possible");
2328 case Legal:
2329 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2330 break;
2331 case Promote:
2332 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2333 break;
2334 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002335
2336 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002337
Andrew Lenharth72594262005-12-24 23:42:32 +00002338 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002339 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002340 case TargetLowering::Legal: break;
2341 case TargetLowering::Custom:
2342 Tmp1 = TLI.LowerOperation(Result, DAG);
2343 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002344 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002345 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002346 if (Node->getValueType(0) == MVT::i32) {
2347 switch (Node->getOpcode()) {
2348 default: assert(0 && "Do not know how to expand this integer BinOp!");
2349 case ISD::UDIV:
2350 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002351 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2352 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002353 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002354 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002355 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002356 };
2357 break;
2358 }
2359
Chris Lattner87f08092006-04-02 03:57:31 +00002360 assert(MVT::isVector(Node->getValueType(0)) &&
2361 "Cannot expand this binary operator!");
2362 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002363 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002364 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002365 MVT::ValueType PtrVT = TLI.getPointerTy();
2366 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2367 i != e; ++i) {
2368 SDOperand Idx = DAG.getConstant(i, PtrVT);
2369 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2370 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2371 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2372 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002373 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2374 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002375 break;
2376 }
Evan Cheng119266e2006-04-12 21:20:24 +00002377 case TargetLowering::Promote: {
2378 switch (Node->getOpcode()) {
2379 default: assert(0 && "Do not know how to promote this BinOp!");
2380 case ISD::AND:
2381 case ISD::OR:
2382 case ISD::XOR: {
2383 MVT::ValueType OVT = Node->getValueType(0);
2384 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2385 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2386 // Bit convert each of the values to the new type.
2387 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2388 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2389 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2390 // Bit convert the result back the original type.
2391 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2392 break;
2393 }
2394 }
2395 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002396 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002397 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002398
2399 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2400 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2401 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2402 case Expand: assert(0 && "Not possible");
2403 case Legal:
2404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2405 break;
2406 case Promote:
2407 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2408 break;
2409 }
2410
2411 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2412
2413 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2414 default: assert(0 && "Operation not supported");
2415 case TargetLowering::Custom:
2416 Tmp1 = TLI.LowerOperation(Result, DAG);
2417 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002418 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002419 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002420 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002421 // If this target supports fabs/fneg natively and select is cheap,
2422 // do this efficiently.
2423 if (!TLI.isSelectExpensive() &&
2424 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2425 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002426 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002427 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002428 // Get the sign bit of the RHS.
2429 MVT::ValueType IVT =
2430 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2431 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2432 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2433 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2434 // Get the absolute value of the result.
2435 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2436 // Select between the nabs and abs value based on the sign bit of
2437 // the input.
2438 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2439 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2440 AbsVal),
2441 AbsVal);
2442 Result = LegalizeOp(Result);
2443 break;
2444 }
2445
2446 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002447 MVT::ValueType NVT =
2448 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2449 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2450 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2451 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002452 break;
2453 }
Evan Cheng003feb02007-01-04 21:56:39 +00002454 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002455 break;
2456
Nate Begeman5965bd12006-02-17 05:43:56 +00002457 case ISD::ADDC:
2458 case ISD::SUBC:
2459 Tmp1 = LegalizeOp(Node->getOperand(0));
2460 Tmp2 = LegalizeOp(Node->getOperand(1));
2461 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2462 // Since this produces two values, make sure to remember that we legalized
2463 // both of them.
2464 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2465 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2466 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002467
Nate Begeman5965bd12006-02-17 05:43:56 +00002468 case ISD::ADDE:
2469 case ISD::SUBE:
2470 Tmp1 = LegalizeOp(Node->getOperand(0));
2471 Tmp2 = LegalizeOp(Node->getOperand(1));
2472 Tmp3 = LegalizeOp(Node->getOperand(2));
2473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2474 // Since this produces two values, make sure to remember that we legalized
2475 // both of them.
2476 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2477 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2478 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002479
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002480 case ISD::BUILD_PAIR: {
2481 MVT::ValueType PairTy = Node->getValueType(0);
2482 // TODO: handle the case where the Lo and Hi operands are not of legal type
2483 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2484 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2485 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002486 case TargetLowering::Promote:
2487 case TargetLowering::Custom:
2488 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002489 case TargetLowering::Legal:
2490 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2491 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2492 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002493 case TargetLowering::Expand:
2494 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2495 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2496 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2497 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2498 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002499 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002500 break;
2501 }
2502 break;
2503 }
2504
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002505 case ISD::UREM:
2506 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002507 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002508 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2509 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002510
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002511 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002512 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2513 case TargetLowering::Custom:
2514 isCustom = true;
2515 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002516 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002518 if (isCustom) {
2519 Tmp1 = TLI.LowerOperation(Result, DAG);
2520 if (Tmp1.Val) Result = Tmp1;
2521 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002522 break;
Chris Lattner81914422005-08-03 20:31:37 +00002523 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002524 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002525 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002526 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002527 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2528 TargetLowering::Legal) {
2529 // X % Y -> X-X/Y*Y
2530 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002531 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002532 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2533 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2534 } else {
2535 assert(Node->getValueType(0) == MVT::i32 &&
2536 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002537 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2538 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002539 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002540 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002541 }
Chris Lattner81914422005-08-03 20:31:37 +00002542 } else {
2543 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002544 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2545 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002546 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002547 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2548 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002549 }
2550 break;
2551 }
2552 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002553 case ISD::VAARG: {
2554 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2555 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2556
Chris Lattner364b89a2006-01-28 07:42:08 +00002557 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002558 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2559 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002560 case TargetLowering::Custom:
2561 isCustom = true;
2562 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002563 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2565 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002566 Tmp1 = Result.getValue(1);
2567
2568 if (isCustom) {
2569 Tmp2 = TLI.LowerOperation(Result, DAG);
2570 if (Tmp2.Val) {
2571 Result = LegalizeOp(Tmp2);
2572 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2573 }
2574 }
Nate Begemane74795c2006-01-25 18:21:52 +00002575 break;
2576 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002577 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002578 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002579 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002580 // Increment the pointer, VAList, to the next vaarg
2581 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2582 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2583 TLI.getPointerTy()));
2584 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002585 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2586 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002587 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002588 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002589 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002590 Result = LegalizeOp(Result);
2591 break;
2592 }
2593 }
2594 // Since VAARG produces two values, make sure to remember that we
2595 // legalized both of them.
2596 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002597 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2598 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002599 }
2600
2601 case ISD::VACOPY:
2602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2604 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2605
2606 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2607 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002608 case TargetLowering::Custom:
2609 isCustom = true;
2610 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002611 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002612 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2613 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002614 if (isCustom) {
2615 Tmp1 = TLI.LowerOperation(Result, DAG);
2616 if (Tmp1.Val) Result = Tmp1;
2617 }
Nate Begemane74795c2006-01-25 18:21:52 +00002618 break;
2619 case TargetLowering::Expand:
2620 // This defaults to loading a pointer from the input and storing it to the
2621 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002622 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002623 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002624 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2625 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002626 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2627 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002628 break;
2629 }
2630 break;
2631
2632 case ISD::VAEND:
2633 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2635
2636 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2637 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002638 case TargetLowering::Custom:
2639 isCustom = true;
2640 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002641 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002643 if (isCustom) {
2644 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2645 if (Tmp1.Val) Result = Tmp1;
2646 }
Nate Begemane74795c2006-01-25 18:21:52 +00002647 break;
2648 case TargetLowering::Expand:
2649 Result = Tmp1; // Default to a no-op, return the chain
2650 break;
2651 }
2652 break;
2653
2654 case ISD::VASTART:
2655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2656 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2657
Chris Lattnerd02b0542006-01-28 10:58:55 +00002658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2659
Nate Begemane74795c2006-01-25 18:21:52 +00002660 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2661 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002662 case TargetLowering::Legal: break;
2663 case TargetLowering::Custom:
2664 Tmp1 = TLI.LowerOperation(Result, DAG);
2665 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002666 break;
2667 }
2668 break;
2669
Nate Begeman1b8121b2006-01-11 21:21:00 +00002670 case ISD::ROTL:
2671 case ISD::ROTR:
2672 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2673 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002675 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2676 default:
2677 assert(0 && "ROTL/ROTR legalize operation not supported");
2678 break;
2679 case TargetLowering::Legal:
2680 break;
2681 case TargetLowering::Custom:
2682 Tmp1 = TLI.LowerOperation(Result, DAG);
2683 if (Tmp1.Val) Result = Tmp1;
2684 break;
2685 case TargetLowering::Promote:
2686 assert(0 && "Do not know how to promote ROTL/ROTR");
2687 break;
2688 case TargetLowering::Expand:
2689 assert(0 && "Do not know how to expand ROTL/ROTR");
2690 break;
2691 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00002692 break;
2693
Nate Begeman2fba8a32006-01-14 03:14:10 +00002694 case ISD::BSWAP:
2695 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2696 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002697 case TargetLowering::Custom:
2698 assert(0 && "Cannot custom legalize this yet!");
2699 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002700 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002701 break;
2702 case TargetLowering::Promote: {
2703 MVT::ValueType OVT = Tmp1.getValueType();
2704 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00002705 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002706
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002707 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2708 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2709 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2710 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2711 break;
2712 }
2713 case TargetLowering::Expand:
2714 Result = ExpandBSWAP(Tmp1);
2715 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002716 }
2717 break;
2718
Andrew Lenharth5e177822005-05-03 17:19:30 +00002719 case ISD::CTPOP:
2720 case ISD::CTTZ:
2721 case ISD::CTLZ:
2722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2723 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002724 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002725 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002726 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002727 break;
2728 case TargetLowering::Promote: {
2729 MVT::ValueType OVT = Tmp1.getValueType();
2730 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002731
2732 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002733 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2734 // Perform the larger operation, then subtract if needed.
2735 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002736 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002737 case ISD::CTPOP:
2738 Result = Tmp1;
2739 break;
2740 case ISD::CTTZ:
2741 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002742 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002743 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00002744 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002745 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002746 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002747 break;
2748 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002749 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002750 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002751 DAG.getConstant(MVT::getSizeInBits(NVT) -
2752 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00002753 break;
2754 }
2755 break;
2756 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002757 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002758 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002759 break;
2760 }
2761 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002762
Chris Lattner13fe99c2005-04-02 05:00:07 +00002763 // Unary operators
2764 case ISD::FABS:
2765 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002766 case ISD::FSQRT:
2767 case ISD::FSIN:
2768 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002769 Tmp1 = LegalizeOp(Node->getOperand(0));
2770 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002771 case TargetLowering::Promote:
2772 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002773 isCustom = true;
2774 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002775 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002776 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002777 if (isCustom) {
2778 Tmp1 = TLI.LowerOperation(Result, DAG);
2779 if (Tmp1.Val) Result = Tmp1;
2780 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002781 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002782 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002783 switch (Node->getOpcode()) {
2784 default: assert(0 && "Unreachable!");
2785 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002786 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2787 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002788 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002789 break;
Chris Lattner80026402005-04-30 04:43:14 +00002790 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002791 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2792 MVT::ValueType VT = Node->getValueType(0);
2793 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002794 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002795 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2796 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002797 break;
2798 }
2799 case ISD::FSQRT:
2800 case ISD::FSIN:
2801 case ISD::FCOS: {
2802 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002803 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002804 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002805 case ISD::FSQRT:
2806 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2807 break;
2808 case ISD::FSIN:
2809 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2810 break;
2811 case ISD::FCOS:
2812 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2813 break;
Chris Lattner80026402005-04-30 04:43:14 +00002814 default: assert(0 && "Unreachable!");
2815 }
Nate Begeman77558da2005-08-04 21:43:28 +00002816 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002817 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2818 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002819 break;
2820 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002821 }
2822 break;
2823 }
2824 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002825 case ISD::FPOWI: {
2826 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002827 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2828 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002829 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002830 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2831 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002832 break;
2833 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002834 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002835 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002836 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00002837 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
2838 // The input has to be a vector type, we have to either scalarize it, pack
2839 // it, or convert it based on whether the input vector type is legal.
2840 SDNode *InVal = Node->getOperand(0).Val;
2841 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2842 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
2843
2844 // Figure out if there is a simple type corresponding to this Vector
2845 // type. If so, convert to the vector type.
2846 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2847 if (TLI.isTypeLegal(TVT)) {
2848 // Turn this into a bit convert of the packed input.
2849 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2850 LegalizeOp(Node->getOperand(0)));
2851 break;
2852 } else if (NumElems == 1) {
2853 // Turn this into a bit convert of the scalar input.
2854 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2855 ScalarizeVectorOp(Node->getOperand(0)));
2856 break;
2857 } else {
2858 // FIXME: UNIMP! Store then reload
2859 assert(0 && "Cast from unsupported vector type not implemented yet!");
2860 }
Chris Lattner763dfd72006-01-23 07:30:46 +00002861 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002862 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2863 Node->getOperand(0).getValueType())) {
2864 default: assert(0 && "Unknown operation action!");
2865 case TargetLowering::Expand:
2866 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2867 break;
2868 case TargetLowering::Legal:
2869 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002870 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002871 break;
2872 }
2873 }
2874 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002875
Chris Lattner13fe99c2005-04-02 05:00:07 +00002876 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002877 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002878 case ISD::UINT_TO_FP: {
2879 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002880 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2881 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002882 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002883 Node->getOperand(0).getValueType())) {
2884 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002885 case TargetLowering::Custom:
2886 isCustom = true;
2887 // FALLTHROUGH
2888 case TargetLowering::Legal:
2889 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002890 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002891 if (isCustom) {
2892 Tmp1 = TLI.LowerOperation(Result, DAG);
2893 if (Tmp1.Val) Result = Tmp1;
2894 }
2895 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002896 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002897 Result = ExpandLegalINT_TO_FP(isSigned,
2898 LegalizeOp(Node->getOperand(0)),
2899 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002900 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002901 case TargetLowering::Promote:
2902 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2903 Node->getValueType(0),
2904 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002905 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002906 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002907 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002908 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002909 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2910 Node->getValueType(0), Node->getOperand(0));
2911 break;
2912 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002913 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002914 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002915 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2916 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002917 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002918 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2919 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002920 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002921 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2922 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002923 break;
2924 }
2925 break;
2926 }
2927 case ISD::TRUNCATE:
2928 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2929 case Legal:
2930 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002931 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002932 break;
2933 case Expand:
2934 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2935
2936 // Since the result is legal, we should just be able to truncate the low
2937 // part of the source.
2938 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2939 break;
2940 case Promote:
2941 Result = PromoteOp(Node->getOperand(0));
2942 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2943 break;
2944 }
2945 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002946
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002947 case ISD::FP_TO_SINT:
2948 case ISD::FP_TO_UINT:
2949 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2950 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002951 Tmp1 = LegalizeOp(Node->getOperand(0));
2952
Chris Lattner44fe26f2005-07-29 00:11:56 +00002953 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2954 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002955 case TargetLowering::Custom:
2956 isCustom = true;
2957 // FALLTHROUGH
2958 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002959 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002960 if (isCustom) {
2961 Tmp1 = TLI.LowerOperation(Result, DAG);
2962 if (Tmp1.Val) Result = Tmp1;
2963 }
2964 break;
2965 case TargetLowering::Promote:
2966 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2967 Node->getOpcode() == ISD::FP_TO_SINT);
2968 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002969 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002970 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2971 SDOperand True, False;
2972 MVT::ValueType VT = Node->getOperand(0).getValueType();
2973 MVT::ValueType NVT = Node->getValueType(0);
2974 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2975 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2976 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2977 Node->getOperand(0), Tmp2, ISD::SETLT);
2978 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2979 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002980 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002981 Tmp2));
2982 False = DAG.getNode(ISD::XOR, NVT, False,
2983 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002984 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2985 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002986 } else {
2987 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2988 }
2989 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002990 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002991 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002992 case Expand: {
2993 // Convert f32 / f64 to i32 / i64.
2994 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00002995 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002996 switch (Node->getOpcode()) {
2997 case ISD::FP_TO_SINT:
2998 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002999 LC = (VT == MVT::i32)
3000 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003001 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003002 LC = (VT == MVT::i32)
3003 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003004 break;
3005 case ISD::FP_TO_UINT:
3006 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003007 LC = (VT == MVT::i32)
3008 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003009 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003010 LC = (VT == MVT::i32)
3011 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003012 break;
3013 default: assert(0 && "Unreachable!");
3014 }
3015 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003016 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3017 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003018 break;
3019 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003020 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003021 Tmp1 = PromoteOp(Node->getOperand(0));
3022 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3023 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003024 break;
3025 }
3026 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003027
Dale Johannesena2b3c172007-07-03 00:53:03 +00003028 case ISD::FP_ROUND:
3029 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3030 TargetLowering::Expand) {
3031 // The only way we can lower this is to turn it into a TRUNCSTORE,
3032 // EXTLOAD pair, targetting a temporary location (a stack slot).
3033
3034 // NOTE: there is a choice here between constantly creating new stack
3035 // slots and always reusing the same one. We currently always create
3036 // new ones, as reuse may inhibit scheduling.
3037 MVT::ValueType VT = Op.getValueType(); // 32
3038 const Type *Ty = MVT::getTypeForValueType(VT);
3039 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3040 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3041 MachineFunction &MF = DAG.getMachineFunction();
3042 int SSFI =
3043 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3044 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3045 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3046 StackSlot, NULL, 0, VT);
3047 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3048 break;
3049 }
3050 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003051 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003052 case ISD::ZERO_EXTEND:
3053 case ISD::SIGN_EXTEND:
3054 case ISD::FP_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003056 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003057 case Legal:
3058 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003060 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003061 case Promote:
3062 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003063 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003064 Tmp1 = PromoteOp(Node->getOperand(0));
3065 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003066 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003067 case ISD::ZERO_EXTEND:
3068 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003069 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003070 Result = DAG.getZeroExtendInReg(Result,
3071 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003072 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003073 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003074 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003075 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003076 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003077 Result,
3078 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003079 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003080 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003081 Result = PromoteOp(Node->getOperand(0));
3082 if (Result.getValueType() != Op.getValueType())
3083 // Dynamically dead while we have only 2 FP types.
3084 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3085 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003086 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003087 Result = PromoteOp(Node->getOperand(0));
3088 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3089 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003090 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003091 }
3092 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003093 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003094 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003095 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003096 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003097
3098 // If this operation is not supported, convert it to a shl/shr or load/store
3099 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003100 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3101 default: assert(0 && "This action not supported for this op yet!");
3102 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003104 break;
3105 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003106 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003107 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003108 // NOTE: we could fall back on load/store here too for targets without
3109 // SAR. However, it is doubtful that any exist.
3110 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3111 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003112 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003113 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3114 Node->getOperand(0), ShiftCst);
3115 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3116 Result, ShiftCst);
3117 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003118 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003119 // EXTLOAD pair, targetting a temporary location (a stack slot).
3120
3121 // NOTE: there is a choice here between constantly creating new stack
3122 // slots and always reusing the same one. We currently always create
3123 // new ones, as reuse may inhibit scheduling.
3124 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003125 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003126 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003127 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003128 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003129 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003130 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003131 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3132 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003133 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003134 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003135 } else {
3136 assert(0 && "Unknown op");
3137 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003138 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003139 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003140 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003141 }
Chris Lattner99222f72005-01-15 07:15:18 +00003142 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003143
Chris Lattner101ea662006-04-08 04:13:17 +00003144 assert(Result.getValueType() == Op.getValueType() &&
3145 "Bad legalization!");
3146
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003147 // Make sure that the generated code is itself legal.
3148 if (Result != Op)
3149 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003150
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003151 // Note that LegalizeOp may be reentered even from single-use nodes, which
3152 // means that we always must cache transformed nodes.
3153 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003154 return Result;
3155}
3156
Chris Lattner4d978642005-01-15 22:16:26 +00003157/// PromoteOp - Given an operation that produces a value in an invalid type,
3158/// promote it to compute the value into a larger type. The produced value will
3159/// have the correct bits for the low portion of the register, but no guarantee
3160/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003161SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3162 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003163 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003164 assert(getTypeAction(VT) == Promote &&
3165 "Caller should expand or legalize operands that are not promotable!");
3166 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3167 "Cannot promote to smaller type!");
3168
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003169 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003170 SDOperand Result;
3171 SDNode *Node = Op.Val;
3172
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003173 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003174 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003175
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003176 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003177 case ISD::CopyFromReg:
3178 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003179 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003180#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003181 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003182#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003183 assert(0 && "Do not know how to promote this operator!");
3184 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003185 case ISD::UNDEF:
3186 Result = DAG.getNode(ISD::UNDEF, NVT);
3187 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003188 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003189 if (VT != MVT::i1)
3190 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3191 else
3192 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003193 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3194 break;
3195 case ISD::ConstantFP:
3196 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3197 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3198 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003199
Chris Lattner2cb338d2005-01-18 02:59:52 +00003200 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003201 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003202 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3203 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003204 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003205
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003206 case ISD::TRUNCATE:
3207 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3208 case Legal:
3209 Result = LegalizeOp(Node->getOperand(0));
3210 assert(Result.getValueType() >= NVT &&
3211 "This truncation doesn't make sense!");
3212 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3213 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3214 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003215 case Promote:
3216 // The truncation is not required, because we don't guarantee anything
3217 // about high bits anyway.
3218 Result = PromoteOp(Node->getOperand(0));
3219 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003220 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003221 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3222 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003223 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003224 }
3225 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003226 case ISD::SIGN_EXTEND:
3227 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003228 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003229 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3230 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3231 case Legal:
3232 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003233 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003234 break;
3235 case Promote:
3236 // Promote the reg if it's smaller.
3237 Result = PromoteOp(Node->getOperand(0));
3238 // The high bits are not guaranteed to be anything. Insert an extend.
3239 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003240 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003241 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003242 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003243 Result = DAG.getZeroExtendInReg(Result,
3244 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003245 break;
3246 }
3247 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003248 case ISD::BIT_CONVERT:
3249 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3250 Result = PromoteOp(Result);
3251 break;
3252
Chris Lattner4d978642005-01-15 22:16:26 +00003253 case ISD::FP_EXTEND:
3254 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3255 case ISD::FP_ROUND:
3256 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3257 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3258 case Promote: assert(0 && "Unreachable with 2 FP types!");
3259 case Legal:
3260 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003261 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003262 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003263 break;
3264 }
3265 break;
3266
3267 case ISD::SINT_TO_FP:
3268 case ISD::UINT_TO_FP:
3269 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3270 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003271 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003272 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003273 break;
3274
3275 case Promote:
3276 Result = PromoteOp(Node->getOperand(0));
3277 if (Node->getOpcode() == ISD::SINT_TO_FP)
3278 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003279 Result,
3280 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003281 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003282 Result = DAG.getZeroExtendInReg(Result,
3283 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003284 // No extra round required here.
3285 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003286 break;
3287 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003288 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3289 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003290 // Round if we cannot tolerate excess precision.
3291 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003292 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3293 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003294 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003295 }
Chris Lattner4d978642005-01-15 22:16:26 +00003296 break;
3297
Chris Lattner268d4572005-12-09 17:32:47 +00003298 case ISD::SIGN_EXTEND_INREG:
3299 Result = PromoteOp(Node->getOperand(0));
3300 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3301 Node->getOperand(1));
3302 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003303 case ISD::FP_TO_SINT:
3304 case ISD::FP_TO_UINT:
3305 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3306 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003307 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003308 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003309 break;
3310 case Promote:
3311 // The input result is prerounded, so we don't have to do anything
3312 // special.
3313 Tmp1 = PromoteOp(Node->getOperand(0));
3314 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003315 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003316 // If we're promoting a UINT to a larger size, check to see if the new node
3317 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3318 // we can use that instead. This allows us to generate better code for
3319 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3320 // legal, such as PowerPC.
3321 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003322 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003323 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3324 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003325 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3326 } else {
3327 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3328 }
Chris Lattner4d978642005-01-15 22:16:26 +00003329 break;
3330
Chris Lattner13fe99c2005-04-02 05:00:07 +00003331 case ISD::FABS:
3332 case ISD::FNEG:
3333 Tmp1 = PromoteOp(Node->getOperand(0));
3334 assert(Tmp1.getValueType() == NVT);
3335 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3336 // NOTE: we do not have to do any extra rounding here for
3337 // NoExcessFPPrecision, because we know the input will have the appropriate
3338 // precision, and these operations don't modify precision at all.
3339 break;
3340
Chris Lattner9d6fa982005-04-28 21:44:33 +00003341 case ISD::FSQRT:
3342 case ISD::FSIN:
3343 case ISD::FCOS:
3344 Tmp1 = PromoteOp(Node->getOperand(0));
3345 assert(Tmp1.getValueType() == NVT);
3346 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003347 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003348 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3349 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003350 break;
3351
Chris Lattnerca401aa2007-03-03 23:43:21 +00003352 case ISD::FPOWI: {
3353 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3354 // directly as well, which may be better.
3355 Tmp1 = PromoteOp(Node->getOperand(0));
3356 assert(Tmp1.getValueType() == NVT);
3357 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3358 if (NoExcessFPPrecision)
3359 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3360 DAG.getValueType(VT));
3361 break;
3362 }
3363
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003364 case ISD::AND:
3365 case ISD::OR:
3366 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003367 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003368 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003369 case ISD::MUL:
3370 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003371 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003372 // that too is okay if they are integer operations.
3373 Tmp1 = PromoteOp(Node->getOperand(0));
3374 Tmp2 = PromoteOp(Node->getOperand(1));
3375 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3376 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003377 break;
3378 case ISD::FADD:
3379 case ISD::FSUB:
3380 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003381 Tmp1 = PromoteOp(Node->getOperand(0));
3382 Tmp2 = PromoteOp(Node->getOperand(1));
3383 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3384 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3385
3386 // Floating point operations will give excess precision that we may not be
3387 // able to tolerate. If we DO allow excess precision, just leave it,
3388 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003389 // FIXME: Why would we need to round FP ops more than integer ones?
3390 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003391 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003392 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3393 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003394 break;
3395
Chris Lattner4d978642005-01-15 22:16:26 +00003396 case ISD::SDIV:
3397 case ISD::SREM:
3398 // These operators require that their input be sign extended.
3399 Tmp1 = PromoteOp(Node->getOperand(0));
3400 Tmp2 = PromoteOp(Node->getOperand(1));
3401 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003402 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3403 DAG.getValueType(VT));
3404 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3405 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003406 }
3407 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3408
3409 // Perform FP_ROUND: this is probably overly pessimistic.
3410 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003411 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3412 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003413 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003414 case ISD::FDIV:
3415 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003416 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003417 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003418 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3419 case Legal:
3420 Tmp1 = LegalizeOp(Node->getOperand(0));
3421 break;
3422 case Promote:
3423 Tmp1 = PromoteOp(Node->getOperand(0));
3424 break;
3425 case Expand:
3426 assert(0 && "not implemented");
3427 }
3428 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3429 case Legal:
3430 Tmp2 = LegalizeOp(Node->getOperand(1));
3431 break;
3432 case Promote:
3433 Tmp2 = PromoteOp(Node->getOperand(1));
3434 break;
3435 case Expand:
3436 assert(0 && "not implemented");
3437 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003438 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3439
3440 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003441 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003442 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3443 DAG.getValueType(VT));
3444 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003445
3446 case ISD::UDIV:
3447 case ISD::UREM:
3448 // These operators require that their input be zero extended.
3449 Tmp1 = PromoteOp(Node->getOperand(0));
3450 Tmp2 = PromoteOp(Node->getOperand(1));
3451 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003452 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3453 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003454 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3455 break;
3456
3457 case ISD::SHL:
3458 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003459 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003460 break;
3461 case ISD::SRA:
3462 // The input value must be properly sign extended.
3463 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003464 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3465 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003466 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003467 break;
3468 case ISD::SRL:
3469 // The input value must be properly zero extended.
3470 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003471 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003472 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003473 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003474
3475 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003476 Tmp1 = Node->getOperand(0); // Get the chain.
3477 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003478 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3479 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3480 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3481 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003482 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003483 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003484 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003485 // Increment the pointer, VAList, to the next vaarg
3486 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3487 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3488 TLI.getPointerTy()));
3489 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003490 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3491 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003492 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003493 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003494 }
3495 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003496 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003497 break;
3498
Evan Chenge71fe34d2006-10-09 20:57:25 +00003499 case ISD::LOAD: {
3500 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003501 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3502 ? ISD::EXTLOAD : LD->getExtensionType();
3503 Result = DAG.getExtLoad(ExtType, NVT,
3504 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003505 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00003506 LD->getLoadedVT(),
3507 LD->isVolatile(),
3508 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003509 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003510 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003511 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003512 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003513 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003514 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3515 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003516 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003517 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003518 case ISD::SELECT_CC:
3519 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3520 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3521 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003522 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003523 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003524 case ISD::BSWAP:
3525 Tmp1 = Node->getOperand(0);
3526 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3527 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3528 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003529 DAG.getConstant(MVT::getSizeInBits(NVT) -
3530 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003531 TLI.getShiftAmountTy()));
3532 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003533 case ISD::CTPOP:
3534 case ISD::CTTZ:
3535 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003536 // Zero extend the argument
3537 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003538 // Perform the larger operation, then subtract if needed.
3539 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003540 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003541 case ISD::CTPOP:
3542 Result = Tmp1;
3543 break;
3544 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003545 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003546 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003547 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3548 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003549 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003550 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003551 break;
3552 case ISD::CTLZ:
3553 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003554 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003555 DAG.getConstant(MVT::getSizeInBits(NVT) -
3556 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003557 break;
3558 }
3559 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003560 case ISD::EXTRACT_SUBVECTOR:
3561 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003562 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003563 case ISD::EXTRACT_VECTOR_ELT:
3564 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3565 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003566 }
3567
3568 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003569
3570 // Make sure the result is itself legal.
3571 Result = LegalizeOp(Result);
3572
3573 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003574 AddPromotedOperand(Op, Result);
3575 return Result;
3576}
Chris Lattnerdc750592005-01-07 07:47:09 +00003577
Dan Gohmana8665142007-06-25 16:23:39 +00003578/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3579/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3580/// based on the vector type. The return type of this matches the element type
3581/// of the vector, which may not be legal for the target.
3582SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003583 // We know that operand #0 is the Vec vector. If the index is a constant
3584 // or if the invec is a supported hardware type, we can use it. Otherwise,
3585 // lower to a store then an indexed load.
3586 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003587 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003588
3589 SDNode *InVal = Vec.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00003590 MVT::ValueType TVT = InVal->getValueType(0);
3591 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003592
Dan Gohmana8665142007-06-25 16:23:39 +00003593 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3594 default: assert(0 && "This action is not supported yet!");
3595 case TargetLowering::Custom: {
3596 Vec = LegalizeOp(Vec);
3597 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3598 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3599 if (Tmp3.Val)
3600 return Tmp3;
3601 break;
3602 }
3603 case TargetLowering::Legal:
3604 if (isTypeLegal(TVT)) {
3605 Vec = LegalizeOp(Vec);
3606 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3607 Op = LegalizeOp(Op);
3608 }
3609 break;
3610 case TargetLowering::Expand:
3611 break;
3612 }
3613
3614 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00003615 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003616 Op = ScalarizeVectorOp(Vec);
3617 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3618 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00003619 SDOperand Lo, Hi;
3620 SplitVectorOp(Vec, Lo, Hi);
3621 if (CIdx->getValue() < NumElems/2) {
3622 Vec = Lo;
3623 } else {
3624 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00003625 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3626 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00003627 }
Dan Gohmana8665142007-06-25 16:23:39 +00003628
Chris Lattner6f423252006-03-31 17:55:51 +00003629 // It's now an extract from the appropriate high or low part. Recurse.
3630 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003631 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00003632 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00003633 // Store the value to a temporary stack slot, then LOAD the scalar
3634 // element back out.
3635 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3636 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3637
3638 // Add the offset to the index.
3639 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3640 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3641 DAG.getConstant(EltSize, Idx.getValueType()));
3642 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3643
3644 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00003645 }
Dan Gohmana8665142007-06-25 16:23:39 +00003646 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00003647}
3648
Dan Gohmana8665142007-06-25 16:23:39 +00003649/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00003650/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00003651SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00003652 // We know that operand #0 is the Vec vector. For now we assume the index
3653 // is a constant and that the extracted result is a supported hardware type.
3654 SDOperand Vec = Op.getOperand(0);
3655 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3656
Dan Gohmana8665142007-06-25 16:23:39 +00003657 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00003658
3659 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3660 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003661 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00003662 }
3663
3664 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3665 SDOperand Lo, Hi;
3666 SplitVectorOp(Vec, Lo, Hi);
3667 if (CIdx->getValue() < NumElems/2) {
3668 Vec = Lo;
3669 } else {
3670 Vec = Hi;
3671 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3672 }
3673
3674 // It's now an extract from the appropriate high or low part. Recurse.
3675 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003676 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00003677}
3678
Nate Begeman7e7f4392006-02-01 07:19:44 +00003679/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3680/// with condition CC on the current target. This usually involves legalizing
3681/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3682/// there may be no choice but to create a new SetCC node to represent the
3683/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3684/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3685void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3686 SDOperand &RHS,
3687 SDOperand &CC) {
3688 SDOperand Tmp1, Tmp2, Result;
3689
3690 switch (getTypeAction(LHS.getValueType())) {
3691 case Legal:
3692 Tmp1 = LegalizeOp(LHS); // LHS
3693 Tmp2 = LegalizeOp(RHS); // RHS
3694 break;
3695 case Promote:
3696 Tmp1 = PromoteOp(LHS); // LHS
3697 Tmp2 = PromoteOp(RHS); // RHS
3698
3699 // If this is an FP compare, the operands have already been extended.
3700 if (MVT::isInteger(LHS.getValueType())) {
3701 MVT::ValueType VT = LHS.getValueType();
3702 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3703
3704 // Otherwise, we have to insert explicit sign or zero extends. Note
3705 // that we could insert sign extends for ALL conditions, but zero extend
3706 // is cheaper on many machines (an AND instead of two shifts), so prefer
3707 // it.
3708 switch (cast<CondCodeSDNode>(CC)->get()) {
3709 default: assert(0 && "Unknown integer comparison!");
3710 case ISD::SETEQ:
3711 case ISD::SETNE:
3712 case ISD::SETUGE:
3713 case ISD::SETUGT:
3714 case ISD::SETULE:
3715 case ISD::SETULT:
3716 // ALL of these operations will work if we either sign or zero extend
3717 // the operands (including the unsigned comparisons!). Zero extend is
3718 // usually a simpler/cheaper operation, so prefer it.
3719 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3720 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3721 break;
3722 case ISD::SETGE:
3723 case ISD::SETGT:
3724 case ISD::SETLT:
3725 case ISD::SETLE:
3726 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3727 DAG.getValueType(VT));
3728 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3729 DAG.getValueType(VT));
3730 break;
3731 }
3732 }
3733 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003734 case Expand: {
3735 MVT::ValueType VT = LHS.getValueType();
3736 if (VT == MVT::f32 || VT == MVT::f64) {
3737 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003738 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003739 switch (cast<CondCodeSDNode>(CC)->get()) {
3740 case ISD::SETEQ:
3741 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003742 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003743 break;
3744 case ISD::SETNE:
3745 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003746 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003747 break;
3748 case ISD::SETGE:
3749 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003750 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003751 break;
3752 case ISD::SETLT:
3753 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003754 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003755 break;
3756 case ISD::SETLE:
3757 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003758 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003759 break;
3760 case ISD::SETGT:
3761 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003762 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003763 break;
3764 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003765 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3766 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003767 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003768 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003769 break;
3770 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003771 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003772 switch (cast<CondCodeSDNode>(CC)->get()) {
3773 case ISD::SETONE:
3774 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003775 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003776 // Fallthrough
3777 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003778 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003779 break;
3780 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003781 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003782 break;
3783 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003784 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003785 break;
3786 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003787 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003788 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003789 case ISD::SETUEQ:
3790 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003791 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003792 default: assert(0 && "Unsupported FP setcc!");
3793 }
3794 }
3795
3796 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003797 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00003798 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003799 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003800 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00003801 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00003802 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003803 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003804 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00003805 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003806 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003807 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00003808 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003809 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3810 Tmp2 = SDOperand();
3811 }
3812 LHS = Tmp1;
3813 RHS = Tmp2;
3814 return;
3815 }
3816
Nate Begeman7e7f4392006-02-01 07:19:44 +00003817 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3818 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003819 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003820 switch (cast<CondCodeSDNode>(CC)->get()) {
3821 case ISD::SETEQ:
3822 case ISD::SETNE:
3823 if (RHSLo == RHSHi)
3824 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3825 if (RHSCST->isAllOnesValue()) {
3826 // Comparison to -1.
3827 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3828 Tmp2 = RHSLo;
3829 break;
3830 }
3831
3832 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3833 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3834 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3835 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3836 break;
3837 default:
3838 // If this is a comparison of the sign bit, just look at the top part.
3839 // X > -1, x < 0
3840 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3841 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3842 CST->getValue() == 0) || // X < 0
3843 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3844 CST->isAllOnesValue())) { // X > -1
3845 Tmp1 = LHSHi;
3846 Tmp2 = RHSHi;
3847 break;
3848 }
3849
3850 // FIXME: This generated code sucks.
3851 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00003852 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3853 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003854 default: assert(0 && "Unknown integer setcc!");
3855 case ISD::SETLT:
3856 case ISD::SETULT: LowCC = ISD::SETULT; break;
3857 case ISD::SETGT:
3858 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3859 case ISD::SETLE:
3860 case ISD::SETULE: LowCC = ISD::SETULE; break;
3861 case ISD::SETGE:
3862 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3863 }
3864
3865 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3866 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3867 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3868
3869 // NOTE: on targets without efficient SELECT of bools, we can always use
3870 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00003871 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3872 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3873 false, DagCombineInfo);
3874 if (!Tmp1.Val)
3875 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3876 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3877 CCCode, false, DagCombineInfo);
3878 if (!Tmp2.Val)
3879 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3880
3881 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3882 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3883 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3884 (Tmp2C && Tmp2C->getValue() == 0 &&
3885 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3886 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3887 (Tmp2C && Tmp2C->getValue() == 1 &&
3888 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3889 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3890 // low part is known false, returns high part.
3891 // For LE / GE, if high part is known false, ignore the low part.
3892 // For LT / GT, if high part is known true, ignore the low part.
3893 Tmp1 = Tmp2;
3894 Tmp2 = SDOperand();
3895 } else {
3896 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3897 ISD::SETEQ, false, DagCombineInfo);
3898 if (!Result.Val)
3899 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3900 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3901 Result, Tmp1, Tmp2));
3902 Tmp1 = Result;
3903 Tmp2 = SDOperand();
3904 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003905 }
3906 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003907 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003908 LHS = Tmp1;
3909 RHS = Tmp2;
3910}
3911
Chris Lattner36e663d2005-12-23 00:16:34 +00003912/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003913/// The resultant code need not be legal. Note that SrcOp is the input operand
3914/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003915SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3916 SDOperand SrcOp) {
3917 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003918 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003919
3920 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003921 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003922 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003923 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003924}
3925
Chris Lattner6be79822006-04-04 17:23:26 +00003926SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3927 // Create a vector sized/aligned stack slot, store the value to element #0,
3928 // then load the whole vector back out.
3929 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003930 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003931 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003932 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003933}
3934
3935
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003936/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3937/// support the operation, but do support the resultant packed vector type.
3938SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3939
3940 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003941 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003942 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003943 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003944 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003945 std::map<SDOperand, std::vector<unsigned> > Values;
3946 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003947 bool isConstant = true;
3948 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3949 SplatValue.getOpcode() != ISD::UNDEF)
3950 isConstant = false;
3951
Evan Cheng1d2e9952006-03-24 01:17:21 +00003952 for (unsigned i = 1; i < NumElems; ++i) {
3953 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003954 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003955 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003956 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003957 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003958 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003959
3960 // If this isn't a constant element or an undef, we can't use a constant
3961 // pool load.
3962 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3963 V.getOpcode() != ISD::UNDEF)
3964 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003965 }
3966
3967 if (isOnlyLowElement) {
3968 // If the low element is an undef too, then this whole things is an undef.
3969 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3970 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3971 // Otherwise, turn this into a scalar_to_vector node.
3972 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3973 Node->getOperand(0));
3974 }
3975
Chris Lattner77e271c2006-03-24 07:29:17 +00003976 // If all elements are constants, create a load from the constant pool.
3977 if (isConstant) {
3978 MVT::ValueType VT = Node->getValueType(0);
3979 const Type *OpNTy =
3980 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3981 std::vector<Constant*> CV;
3982 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3983 if (ConstantFPSDNode *V =
3984 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3985 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3986 } else if (ConstantSDNode *V =
3987 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003988 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003989 } else {
3990 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3991 CV.push_back(UndefValue::get(OpNTy));
3992 }
3993 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00003994 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00003995 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003996 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003997 }
3998
Chris Lattner21e68c82006-03-20 01:52:29 +00003999 if (SplatValue.Val) { // Splat of one value?
4000 // Build the shuffle constant vector: <0, 0, 0, 0>
4001 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004002 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004003 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004004 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004005 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4006 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004007
4008 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004009 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004010 // Get the splatted value into the low element of a vector register.
4011 SDOperand LowValVec =
4012 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4013
4014 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4015 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4016 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4017 SplatMask);
4018 }
4019 }
4020
Evan Cheng1d2e9952006-03-24 01:17:21 +00004021 // If there are only two unique elements, we may be able to turn this into a
4022 // vector shuffle.
4023 if (Values.size() == 2) {
4024 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4025 MVT::ValueType MaskVT =
4026 MVT::getIntVectorWithNumElements(NumElems);
4027 std::vector<SDOperand> MaskVec(NumElems);
4028 unsigned i = 0;
4029 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4030 E = Values.end(); I != E; ++I) {
4031 for (std::vector<unsigned>::iterator II = I->second.begin(),
4032 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004033 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004034 i += NumElems;
4035 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004036 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4037 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004038
4039 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004040 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4041 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004042 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004043 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4044 E = Values.end(); I != E; ++I) {
4045 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4046 I->first);
4047 Ops.push_back(Op);
4048 }
4049 Ops.push_back(ShuffleMask);
4050
4051 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004052 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4053 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004054 }
4055 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004056
4057 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4058 // aligned object on the stack, store each element into it, then load
4059 // the result as a vector.
4060 MVT::ValueType VT = Node->getValueType(0);
4061 // Create the stack frame object.
4062 SDOperand FIPtr = CreateStackTemporary(VT);
4063
4064 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004065 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004066 unsigned TypeByteSize =
4067 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004068 // Store (in the right endianness) the elements to memory.
4069 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4070 // Ignore undef elements.
4071 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4072
Chris Lattner8fa445a2006-03-22 01:46:54 +00004073 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004074
4075 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4076 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4077
Evan Chengdf9ac472006-10-05 23:01:46 +00004078 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004079 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004080 }
4081
4082 SDOperand StoreChain;
4083 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004084 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4085 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004086 else
4087 StoreChain = DAG.getEntryNode();
4088
4089 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004090 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004091}
4092
4093/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4094/// specified value type.
4095SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4096 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4097 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004098 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004099 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004100 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004101 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4102}
4103
Chris Lattner4157c412005-04-02 04:00:59 +00004104void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4105 SDOperand Op, SDOperand Amt,
4106 SDOperand &Lo, SDOperand &Hi) {
4107 // Expand the subcomponents.
4108 SDOperand LHSL, LHSH;
4109 ExpandOp(Op, LHSL, LHSH);
4110
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004111 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004112 MVT::ValueType VT = LHSL.getValueType();
4113 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004114 Hi = Lo.getValue(1);
4115}
4116
4117
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004118/// ExpandShift - Try to find a clever way to expand this shift operation out to
4119/// smaller elements. If we can't find a way that is more efficient than a
4120/// libcall on this target, return false. Otherwise, return true with the
4121/// low-parts expanded into Lo and Hi.
4122bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4123 SDOperand &Lo, SDOperand &Hi) {
4124 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4125 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004126
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004127 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004128 SDOperand ShAmt = LegalizeOp(Amt);
4129 MVT::ValueType ShTy = ShAmt.getValueType();
4130 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4131 unsigned NVTBits = MVT::getSizeInBits(NVT);
4132
4133 // Handle the case when Amt is an immediate. Other cases are currently broken
4134 // and are disabled.
4135 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4136 unsigned Cst = CN->getValue();
4137 // Expand the incoming operand to be shifted, so that we have its parts
4138 SDOperand InL, InH;
4139 ExpandOp(Op, InL, InH);
4140 switch(Opc) {
4141 case ISD::SHL:
4142 if (Cst > VTBits) {
4143 Lo = DAG.getConstant(0, NVT);
4144 Hi = DAG.getConstant(0, NVT);
4145 } else if (Cst > NVTBits) {
4146 Lo = DAG.getConstant(0, NVT);
4147 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004148 } else if (Cst == NVTBits) {
4149 Lo = DAG.getConstant(0, NVT);
4150 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004151 } else {
4152 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4153 Hi = DAG.getNode(ISD::OR, NVT,
4154 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4155 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4156 }
4157 return true;
4158 case ISD::SRL:
4159 if (Cst > VTBits) {
4160 Lo = DAG.getConstant(0, NVT);
4161 Hi = DAG.getConstant(0, NVT);
4162 } else if (Cst > NVTBits) {
4163 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4164 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004165 } else if (Cst == NVTBits) {
4166 Lo = InH;
4167 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004168 } else {
4169 Lo = DAG.getNode(ISD::OR, NVT,
4170 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4171 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4172 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4173 }
4174 return true;
4175 case ISD::SRA:
4176 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004177 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004178 DAG.getConstant(NVTBits-1, ShTy));
4179 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004180 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004181 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004182 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004183 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004184 } else if (Cst == NVTBits) {
4185 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004186 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004187 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004188 } else {
4189 Lo = DAG.getNode(ISD::OR, NVT,
4190 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4191 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4192 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4193 }
4194 return true;
4195 }
4196 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004197
4198 // Okay, the shift amount isn't constant. However, if we can tell that it is
4199 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4200 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004201 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004202
4203 // If we know that the high bit of the shift amount is one, then we can do
4204 // this as a couple of simple shifts.
4205 if (KnownOne & Mask) {
4206 // Mask out the high bit, which we know is set.
4207 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4208 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4209
4210 // Expand the incoming operand to be shifted, so that we have its parts
4211 SDOperand InL, InH;
4212 ExpandOp(Op, InL, InH);
4213 switch(Opc) {
4214 case ISD::SHL:
4215 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4216 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4217 return true;
4218 case ISD::SRL:
4219 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4220 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4221 return true;
4222 case ISD::SRA:
4223 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4224 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4225 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4226 return true;
4227 }
4228 }
4229
4230 // If we know that the high bit of the shift amount is zero, then we can do
4231 // this as a couple of simple shifts.
4232 if (KnownZero & Mask) {
4233 // Compute 32-amt.
4234 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4235 DAG.getConstant(NVTBits, Amt.getValueType()),
4236 Amt);
4237
4238 // Expand the incoming operand to be shifted, so that we have its parts
4239 SDOperand InL, InH;
4240 ExpandOp(Op, InL, InH);
4241 switch(Opc) {
4242 case ISD::SHL:
4243 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4244 Hi = DAG.getNode(ISD::OR, NVT,
4245 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4246 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4247 return true;
4248 case ISD::SRL:
4249 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4250 Lo = DAG.getNode(ISD::OR, NVT,
4251 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4252 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4253 return true;
4254 case ISD::SRA:
4255 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4256 Lo = DAG.getNode(ISD::OR, NVT,
4257 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4258 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4259 return true;
4260 }
4261 }
4262
Nate Begemanb0674922005-04-06 21:13:14 +00004263 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004264}
Chris Lattneraac464e2005-01-21 06:05:23 +00004265
Chris Lattner4add7e32005-01-23 04:42:50 +00004266
Chris Lattneraac464e2005-01-21 06:05:23 +00004267// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4268// does not fit into a register, return the lo part and set the hi part to the
4269// by-reg argument. If it does fit into a single register, return the result
4270// and leave the Hi part unset.
4271SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004272 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004273 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4274 // The input chain to this libcall is the entry node of the function.
4275 // Legalizing the call will automatically add the previous call to the
4276 // dependence.
4277 SDOperand InChain = DAG.getEntryNode();
4278
Chris Lattneraac464e2005-01-21 06:05:23 +00004279 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004280 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004281 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4282 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4283 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004284 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004285 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004286 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004287 }
4288 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004289
Chris Lattner06bbeb62005-05-11 19:02:11 +00004290 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004291 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004292 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004293 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004294 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004295
Chris Lattner462505f2006-02-13 09:18:02 +00004296 // Legalize the call sequence, starting with the chain. This will advance
4297 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4298 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4299 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004300 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004301 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004302 default: assert(0 && "Unknown thing");
4303 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004304 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004305 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004306 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004307 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004308 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004309 }
Chris Lattner63022662005-09-02 20:26:58 +00004310 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004311}
4312
Chris Lattner4add7e32005-01-23 04:42:50 +00004313
Evan Chengbf535fc2007-04-27 07:33:31 +00004314/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4315///
Chris Lattneraac464e2005-01-21 06:05:23 +00004316SDOperand SelectionDAGLegalize::
4317ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004318 assert(getTypeAction(Source.getValueType()) == Expand &&
4319 "This is not an expansion!");
4320 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4321
Chris Lattner06bbeb62005-05-11 19:02:11 +00004322 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004323 assert(Source.getValueType() == MVT::i64 &&
4324 "This only works for 64-bit -> FP");
4325 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4326 // incoming integer is set. To handle this, we dynamically test to see if
4327 // it is set, and, if so, add a fudge factor.
4328 SDOperand Lo, Hi;
4329 ExpandOp(Source, Lo, Hi);
4330
Chris Lattner2a4f7312005-05-13 04:45:13 +00004331 // If this is unsigned, and not supported, first perform the conversion to
4332 // signed, then adjust the result if the sign bit is set.
4333 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4334 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4335
Chris Lattnerd47675e2005-08-09 20:20:18 +00004336 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4337 DAG.getConstant(0, Hi.getValueType()),
4338 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004339 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4340 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4341 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004342 uint64_t FF = 0x5f800000ULL;
4343 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004344 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004345
Chris Lattnerc30405e2005-08-26 17:15:30 +00004346 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004347 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4348 SDOperand FudgeInReg;
4349 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004350 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004351 else {
4352 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Chengbf535fc2007-04-27 07:33:31 +00004353 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004354 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004355 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004356 }
Evan Chengbf535fc2007-04-27 07:33:31 +00004357 MVT::ValueType SCVT = SignedConv.getValueType();
4358 if (SCVT != DestTy) {
4359 // Destination type needs to be expanded as well. The FADD now we are
4360 // constructing will be expanded into a libcall.
4361 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4362 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4363 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4364 SignedConv, SignedConv.getValue(1));
4365 }
4366 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4367 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004368 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004369 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004370
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004371 // Check to see if the target has a custom way to lower this. If so, use it.
4372 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4373 default: assert(0 && "This action not implemented for this operation!");
4374 case TargetLowering::Legal:
4375 case TargetLowering::Expand:
4376 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004377 case TargetLowering::Custom: {
4378 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4379 Source), DAG);
4380 if (NV.Val)
4381 return LegalizeOp(NV);
4382 break; // The target decided this was legal after all
4383 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004384 }
4385
Chris Lattner153587e2005-05-12 07:00:44 +00004386 // Expand the source, then glue it back together for the call. We must expand
4387 // the source in case it is shared (this pass of legalize must traverse it).
4388 SDOperand SrcLo, SrcHi;
4389 ExpandOp(Source, SrcLo, SrcHi);
4390 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4391
Evan Cheng31cbddf2007-01-12 02:11:51 +00004392 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004393 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004394 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004395 else {
4396 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004397 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004398 }
Chris Lattner462505f2006-02-13 09:18:02 +00004399
Evan Chengbf535fc2007-04-27 07:33:31 +00004400 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004401 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4402 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004403 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4404 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004405}
Misha Brukman835702a2005-04-21 22:36:52 +00004406
Chris Lattner689bdcc2006-01-28 08:25:58 +00004407/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4408/// INT_TO_FP operation of the specified operand when the target requests that
4409/// we expand it. At this point, we know that the result and operand types are
4410/// legal for the target.
4411SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4412 SDOperand Op0,
4413 MVT::ValueType DestVT) {
4414 if (Op0.getValueType() == MVT::i32) {
4415 // simple 32-bit [signed|unsigned] integer to float/double expansion
4416
Chris Lattner50ee0e42007-01-20 22:35:55 +00004417 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004418 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004419 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4420 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004421 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004422 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004423 // get address of 8 byte buffer
4424 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4425 // word offset constant for Hi/Lo address computation
4426 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4427 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004428 SDOperand Hi = StackSlot;
4429 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4430 if (TLI.isLittleEndian())
4431 std::swap(Hi, Lo);
4432
Chris Lattner689bdcc2006-01-28 08:25:58 +00004433 // if signed map to unsigned space
4434 SDOperand Op0Mapped;
4435 if (isSigned) {
4436 // constant used to invert sign bit (signed to unsigned mapping)
4437 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4438 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4439 } else {
4440 Op0Mapped = Op0;
4441 }
4442 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004443 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004444 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004445 // initial hi portion of constructed double
4446 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4447 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004448 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004449 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004450 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004451 // FP constant to bias correct the final result
4452 SDOperand Bias = DAG.getConstantFP(isSigned ?
4453 BitsToDouble(0x4330000080000000ULL)
4454 : BitsToDouble(0x4330000000000000ULL),
4455 MVT::f64);
4456 // subtract the bias
4457 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4458 // final result
4459 SDOperand Result;
4460 // handle final rounding
4461 if (DestVT == MVT::f64) {
4462 // do nothing
4463 Result = Sub;
4464 } else {
4465 // if f32 then cast to f32
4466 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4467 }
4468 return Result;
4469 }
4470 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4471 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4472
4473 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4474 DAG.getConstant(0, Op0.getValueType()),
4475 ISD::SETLT);
4476 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4477 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4478 SignSet, Four, Zero);
4479
4480 // If the sign bit of the integer is set, the large number will be treated
4481 // as a negative number. To counteract this, the dynamic code adds an
4482 // offset depending on the data type.
4483 uint64_t FF;
4484 switch (Op0.getValueType()) {
4485 default: assert(0 && "Unsupported integer type!");
4486 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4487 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4488 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4489 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4490 }
4491 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004492 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004493
4494 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4495 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4496 SDOperand FudgeInReg;
4497 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004498 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004499 else {
4500 assert(DestVT == MVT::f64 && "Unexpected conversion");
4501 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4502 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004503 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004504 }
4505
4506 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4507}
4508
4509/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4510/// *INT_TO_FP operation of the specified operand when the target requests that
4511/// we promote it. At this point, we know that the result and operand types are
4512/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4513/// operation that takes a larger input.
4514SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4515 MVT::ValueType DestVT,
4516 bool isSigned) {
4517 // First step, figure out the appropriate *INT_TO_FP operation to use.
4518 MVT::ValueType NewInTy = LegalOp.getValueType();
4519
4520 unsigned OpToUse = 0;
4521
4522 // Scan for the appropriate larger type to use.
4523 while (1) {
4524 NewInTy = (MVT::ValueType)(NewInTy+1);
4525 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4526
4527 // If the target supports SINT_TO_FP of this type, use it.
4528 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4529 default: break;
4530 case TargetLowering::Legal:
4531 if (!TLI.isTypeLegal(NewInTy))
4532 break; // Can't use this datatype.
4533 // FALL THROUGH.
4534 case TargetLowering::Custom:
4535 OpToUse = ISD::SINT_TO_FP;
4536 break;
4537 }
4538 if (OpToUse) break;
4539 if (isSigned) continue;
4540
4541 // If the target supports UINT_TO_FP of this type, use it.
4542 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4543 default: break;
4544 case TargetLowering::Legal:
4545 if (!TLI.isTypeLegal(NewInTy))
4546 break; // Can't use this datatype.
4547 // FALL THROUGH.
4548 case TargetLowering::Custom:
4549 OpToUse = ISD::UINT_TO_FP;
4550 break;
4551 }
4552 if (OpToUse) break;
4553
4554 // Otherwise, try a larger type.
4555 }
4556
4557 // Okay, we found the operation and type to use. Zero extend our input to the
4558 // desired type then run the operation on it.
4559 return DAG.getNode(OpToUse, DestVT,
4560 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4561 NewInTy, LegalOp));
4562}
4563
4564/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4565/// FP_TO_*INT operation of the specified operand when the target requests that
4566/// we promote it. At this point, we know that the result and operand types are
4567/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4568/// operation that returns a larger result.
4569SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4570 MVT::ValueType DestVT,
4571 bool isSigned) {
4572 // First step, figure out the appropriate FP_TO*INT operation to use.
4573 MVT::ValueType NewOutTy = DestVT;
4574
4575 unsigned OpToUse = 0;
4576
4577 // Scan for the appropriate larger type to use.
4578 while (1) {
4579 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4580 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4581
4582 // If the target supports FP_TO_SINT returning this type, use it.
4583 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4584 default: break;
4585 case TargetLowering::Legal:
4586 if (!TLI.isTypeLegal(NewOutTy))
4587 break; // Can't use this datatype.
4588 // FALL THROUGH.
4589 case TargetLowering::Custom:
4590 OpToUse = ISD::FP_TO_SINT;
4591 break;
4592 }
4593 if (OpToUse) break;
4594
4595 // If the target supports FP_TO_UINT of this type, use it.
4596 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4597 default: break;
4598 case TargetLowering::Legal:
4599 if (!TLI.isTypeLegal(NewOutTy))
4600 break; // Can't use this datatype.
4601 // FALL THROUGH.
4602 case TargetLowering::Custom:
4603 OpToUse = ISD::FP_TO_UINT;
4604 break;
4605 }
4606 if (OpToUse) break;
4607
4608 // Otherwise, try a larger type.
4609 }
4610
4611 // Okay, we found the operation and type to use. Truncate the result of the
4612 // extended FP_TO_*INT operation to the desired size.
4613 return DAG.getNode(ISD::TRUNCATE, DestVT,
4614 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4615}
4616
4617/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4618///
4619SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4620 MVT::ValueType VT = Op.getValueType();
4621 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4622 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4623 switch (VT) {
4624 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4625 case MVT::i16:
4626 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4627 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4628 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4629 case MVT::i32:
4630 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4631 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4632 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4633 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4634 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4635 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4636 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4637 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4638 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4639 case MVT::i64:
4640 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4641 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4642 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4643 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4644 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4645 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4646 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4647 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4648 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4649 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4650 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4651 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4652 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4653 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4654 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4655 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4656 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4657 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4658 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4659 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4660 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4661 }
4662}
4663
4664/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4665///
4666SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4667 switch (Opc) {
4668 default: assert(0 && "Cannot expand this yet!");
4669 case ISD::CTPOP: {
4670 static const uint64_t mask[6] = {
4671 0x5555555555555555ULL, 0x3333333333333333ULL,
4672 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4673 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4674 };
4675 MVT::ValueType VT = Op.getValueType();
4676 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004677 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004678 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4679 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4680 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4681 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4682 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4683 DAG.getNode(ISD::AND, VT,
4684 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4685 }
4686 return Op;
4687 }
4688 case ISD::CTLZ: {
4689 // for now, we do this:
4690 // x = x | (x >> 1);
4691 // x = x | (x >> 2);
4692 // ...
4693 // x = x | (x >>16);
4694 // x = x | (x >>32); // for 64-bit input
4695 // return popcount(~x);
4696 //
4697 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4698 MVT::ValueType VT = Op.getValueType();
4699 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004700 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004701 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4702 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4703 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4704 }
4705 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4706 return DAG.getNode(ISD::CTPOP, VT, Op);
4707 }
4708 case ISD::CTTZ: {
4709 // for now, we use: { return popcount(~x & (x - 1)); }
4710 // unless the target has ctlz but not ctpop, in which case we use:
4711 // { return 32 - nlz(~x & (x-1)); }
4712 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4713 MVT::ValueType VT = Op.getValueType();
4714 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4715 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4716 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4717 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4718 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4719 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4720 TLI.isOperationLegal(ISD::CTLZ, VT))
4721 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004722 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00004723 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4724 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4725 }
4726 }
4727}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004728
Chris Lattnerdc750592005-01-07 07:47:09 +00004729/// ExpandOp - Expand the specified SDOperand into its two component pieces
4730/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4731/// LegalizeNodes map is filled in for any results that are not expanded, the
4732/// ExpandedNodes map is filled in for any results that are expanded, and the
4733/// Lo/Hi values are returned.
4734void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4735 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004736 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004737 SDNode *Node = Op.Val;
4738 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004739 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00004740 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004741 "Cannot expand to FP value or to larger int value!");
4742
Chris Lattner1a570f12005-09-02 20:32:45 +00004743 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004744 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004745 = ExpandedNodes.find(Op);
4746 if (I != ExpandedNodes.end()) {
4747 Lo = I->second.first;
4748 Hi = I->second.second;
4749 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004750 }
4751
Chris Lattnerdc750592005-01-07 07:47:09 +00004752 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004753 case ISD::CopyFromReg:
4754 assert(0 && "CopyFromReg must be legal!");
4755 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004756#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004757 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004758#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004759 assert(0 && "Do not know how to expand this operator!");
4760 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004761 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004762 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004763 Lo = DAG.getNode(ISD::UNDEF, NVT);
4764 Hi = DAG.getNode(ISD::UNDEF, NVT);
4765 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004766 case ISD::Constant: {
4767 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4768 Lo = DAG.getConstant(Cst, NVT);
4769 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4770 break;
4771 }
Evan Cheng47833a12006-12-12 21:32:44 +00004772 case ISD::ConstantFP: {
4773 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004774 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004775 if (getTypeAction(Lo.getValueType()) == Expand)
4776 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004777 break;
4778 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004779 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004780 // Return the operands.
4781 Lo = Node->getOperand(0);
4782 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004783 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004784
4785 case ISD::SIGN_EXTEND_INREG:
4786 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004787 // sext_inreg the low part if needed.
4788 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4789
4790 // The high part gets the sign extension from the lo-part. This handles
4791 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004792 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4793 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4794 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004795 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004796
Nate Begeman2fba8a32006-01-14 03:14:10 +00004797 case ISD::BSWAP: {
4798 ExpandOp(Node->getOperand(0), Lo, Hi);
4799 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4800 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4801 Lo = TempLo;
4802 break;
4803 }
4804
Chris Lattner55e9cde2005-05-11 04:51:16 +00004805 case ISD::CTPOP:
4806 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004807 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4808 DAG.getNode(ISD::CTPOP, NVT, Lo),
4809 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004810 Hi = DAG.getConstant(0, NVT);
4811 break;
4812
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004813 case ISD::CTLZ: {
4814 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004815 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004816 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4817 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004818 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4819 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004820 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4821 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4822
4823 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4824 Hi = DAG.getConstant(0, NVT);
4825 break;
4826 }
4827
4828 case ISD::CTTZ: {
4829 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004830 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004831 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4832 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004833 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4834 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004835 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4836 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4837
4838 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4839 Hi = DAG.getConstant(0, NVT);
4840 break;
4841 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004842
Nate Begemane74795c2006-01-25 18:21:52 +00004843 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004844 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4845 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004846 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4847 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4848
4849 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004850 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004851 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4852 if (!TLI.isLittleEndian())
4853 std::swap(Lo, Hi);
4854 break;
4855 }
4856
Chris Lattnerdc750592005-01-07 07:47:09 +00004857 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004858 LoadSDNode *LD = cast<LoadSDNode>(Node);
4859 SDOperand Ch = LD->getChain(); // Legalize the chain.
4860 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4861 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00004862 int SVOffset = LD->getSrcValueOffset();
4863 unsigned Alignment = LD->getAlignment();
4864 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00004865
Evan Chenge71fe34d2006-10-09 20:57:25 +00004866 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00004867 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4868 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00004869 if (VT == MVT::f32 || VT == MVT::f64) {
4870 // f32->i32 or f64->i64 one to one expansion.
4871 // Remember that we legalized the chain.
4872 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004873 // Recursively expand the new load.
4874 if (getTypeAction(NVT) == Expand)
4875 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004876 break;
4877 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004878
Evan Chenge71fe34d2006-10-09 20:57:25 +00004879 // Increment the pointer to the other half.
4880 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4881 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4882 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00004883 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00004884 if (Alignment > IncrementSize)
4885 Alignment = IncrementSize;
4886 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
4887 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00004888
Evan Chenge71fe34d2006-10-09 20:57:25 +00004889 // Build a factor node to remember that this load is independent of the
4890 // other one.
4891 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4892 Hi.getValue(1));
4893
4894 // Remember that we legalized the chain.
4895 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4896 if (!TLI.isLittleEndian())
4897 std::swap(Lo, Hi);
4898 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004899 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004900
4901 if (VT == MVT::f64 && EVT == MVT::f32) {
4902 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4903 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00004904 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00004905 // Remember that we legalized the chain.
4906 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4907 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4908 break;
4909 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004910
4911 if (EVT == NVT)
4912 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00004913 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004914 else
4915 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00004916 SVOffset, EVT, isVolatile,
4917 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004918
4919 // Remember that we legalized the chain.
4920 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4921
4922 if (ExtType == ISD::SEXTLOAD) {
4923 // The high part is obtained by SRA'ing all but one of the bits of the
4924 // lo part.
4925 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4926 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4927 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4928 } else if (ExtType == ISD::ZEXTLOAD) {
4929 // The high part is just a zero.
4930 Hi = DAG.getConstant(0, NVT);
4931 } else /* if (ExtType == ISD::EXTLOAD) */ {
4932 // The high part is undefined.
4933 Hi = DAG.getNode(ISD::UNDEF, NVT);
4934 }
4935 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004936 break;
4937 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004938 case ISD::AND:
4939 case ISD::OR:
4940 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4941 SDOperand LL, LH, RL, RH;
4942 ExpandOp(Node->getOperand(0), LL, LH);
4943 ExpandOp(Node->getOperand(1), RL, RH);
4944 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4945 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4946 break;
4947 }
4948 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004949 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004950 ExpandOp(Node->getOperand(1), LL, LH);
4951 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004952 if (getTypeAction(NVT) == Expand)
4953 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004954 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004955 if (VT != MVT::f32)
4956 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004957 break;
4958 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004959 case ISD::SELECT_CC: {
4960 SDOperand TL, TH, FL, FH;
4961 ExpandOp(Node->getOperand(2), TL, TH);
4962 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004963 if (getTypeAction(NVT) == Expand)
4964 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004965 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4966 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004967 if (VT != MVT::f32)
4968 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4969 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004970 break;
4971 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004972 case ISD::ANY_EXTEND:
4973 // The low part is any extension of the input (which degenerates to a copy).
4974 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4975 // The high part is undefined.
4976 Hi = DAG.getNode(ISD::UNDEF, NVT);
4977 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004978 case ISD::SIGN_EXTEND: {
4979 // The low part is just a sign extension of the input (which degenerates to
4980 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004981 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004982
Chris Lattnerdc750592005-01-07 07:47:09 +00004983 // The high part is obtained by SRA'ing all but one of the bits of the lo
4984 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004985 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004986 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4987 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004988 break;
4989 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004990 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004991 // The low part is just a zero extension of the input (which degenerates to
4992 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004993 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004994
Chris Lattnerdc750592005-01-07 07:47:09 +00004995 // The high part is just a zero.
4996 Hi = DAG.getConstant(0, NVT);
4997 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004998
Chris Lattner59b27fa372007-02-13 23:55:16 +00004999 case ISD::TRUNCATE: {
5000 // The input value must be larger than this value. Expand *it*.
5001 SDOperand NewLo;
5002 ExpandOp(Node->getOperand(0), NewLo, Hi);
5003
5004 // The low part is now either the right size, or it is closer. If not the
5005 // right size, make an illegal truncate so we recursively expand it.
5006 if (NewLo.getValueType() != Node->getValueType(0))
5007 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5008 ExpandOp(NewLo, Lo, Hi);
5009 break;
5010 }
5011
Chris Lattner36e663d2005-12-23 00:16:34 +00005012 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005013 SDOperand Tmp;
5014 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5015 // If the target wants to, allow it to lower this itself.
5016 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5017 case Expand: assert(0 && "cannot expand FP!");
5018 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5019 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5020 }
5021 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5022 }
5023
Evan Cheng4eee7242006-12-09 02:42:38 +00005024 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005025 if (VT == MVT::f32 || VT == MVT::f64) {
5026 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005027 if (getTypeAction(NVT) == Expand)
5028 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005029 break;
5030 }
5031
5032 // If source operand will be expanded to the same type as VT, i.e.
5033 // i64 <- f64, i32 <- f32, expand the source operand instead.
5034 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5035 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5036 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005037 break;
5038 }
5039
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005040 // Turn this into a load/store pair by default.
5041 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005042 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005043
Chris Lattner36e663d2005-12-23 00:16:34 +00005044 ExpandOp(Tmp, Lo, Hi);
5045 break;
5046 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005047
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005048 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005049 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5050 TargetLowering::Custom &&
5051 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005052 Lo = TLI.LowerOperation(Op, DAG);
5053 assert(Lo.Val && "Node must be custom expanded!");
5054 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005055 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005056 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005057 break;
5058
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005059 // These operators cannot be expanded directly, emit them as calls to
5060 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005061 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005062 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005063 SDOperand Op;
5064 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5065 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005066 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5067 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005068 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005069
Chris Lattner941d84a2005-07-30 01:40:57 +00005070 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5071
Chris Lattnerfe68d752005-07-29 00:33:32 +00005072 // Now that the custom expander is done, expand the result, which is still
5073 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005074 if (Op.Val) {
5075 ExpandOp(Op, Lo, Hi);
5076 break;
5077 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005078 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005079
Evan Cheng31cbddf2007-01-12 02:11:51 +00005080 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005081 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005082 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005083 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005084 LC = RTLIB::FPTOSINT_F64_I64;
5085 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5086 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005087 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005088 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005089
Evan Cheng31cbddf2007-01-12 02:11:51 +00005090 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005091 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005092 SDOperand Op;
5093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5094 case Expand: assert(0 && "cannot expand FP!");
5095 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5096 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5097 }
5098
5099 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5100
5101 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005102 if (Op.Val) {
5103 ExpandOp(Op, Lo, Hi);
5104 break;
5105 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005106 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005107
Evan Cheng31cbddf2007-01-12 02:11:51 +00005108 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005109 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005110 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005111 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005112 LC = RTLIB::FPTOUINT_F64_I64;
5113 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5114 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005115 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005116 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005117
Evan Cheng870e4f82006-01-09 18:31:59 +00005118 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005119 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005120 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005121 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005122 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005123 Op = TLI.LowerOperation(Op, DAG);
5124 if (Op.Val) {
5125 // Now that the custom expander is done, expand the result, which is
5126 // still VT.
5127 ExpandOp(Op, Lo, Hi);
5128 break;
5129 }
5130 }
5131
Chris Lattner72b503b2006-09-13 03:50:39 +00005132 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5133 // this X << 1 as X+X.
5134 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5135 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5136 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5137 SDOperand LoOps[2], HiOps[3];
5138 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5139 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5140 LoOps[1] = LoOps[0];
5141 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5142
5143 HiOps[1] = HiOps[0];
5144 HiOps[2] = Lo.getValue(1);
5145 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5146 break;
5147 }
5148 }
5149
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005150 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005151 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005152 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005153
5154 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005155 TargetLowering::LegalizeAction Action =
5156 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5157 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5158 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005159 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005160 break;
5161 }
5162
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005163 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005164 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5165 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005166 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005167 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005168
Evan Cheng870e4f82006-01-09 18:31:59 +00005169 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005170 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005171 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005172 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005173 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005174 Op = TLI.LowerOperation(Op, DAG);
5175 if (Op.Val) {
5176 // Now that the custom expander is done, expand the result, which is
5177 // still VT.
5178 ExpandOp(Op, Lo, Hi);
5179 break;
5180 }
5181 }
5182
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005183 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005184 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005185 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005186
5187 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005188 TargetLowering::LegalizeAction Action =
5189 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5190 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5191 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005192 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005193 break;
5194 }
5195
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005196 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005197 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5198 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005199 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005200 }
5201
5202 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005203 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005204 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005205 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005206 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005207 Op = TLI.LowerOperation(Op, DAG);
5208 if (Op.Val) {
5209 // Now that the custom expander is done, expand the result, which is
5210 // still VT.
5211 ExpandOp(Op, Lo, Hi);
5212 break;
5213 }
5214 }
5215
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005216 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005217 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005218 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005219
5220 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005221 TargetLowering::LegalizeAction Action =
5222 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5223 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5224 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005225 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005226 break;
5227 }
5228
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005229 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005230 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5231 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005232 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005233 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005234
Misha Brukman835702a2005-04-21 22:36:52 +00005235 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005236 case ISD::SUB: {
5237 // If the target wants to custom expand this, let them.
5238 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5239 TargetLowering::Custom) {
5240 Op = TLI.LowerOperation(Op, DAG);
5241 if (Op.Val) {
5242 ExpandOp(Op, Lo, Hi);
5243 break;
5244 }
5245 }
5246
5247 // Expand the subcomponents.
5248 SDOperand LHSL, LHSH, RHSL, RHSH;
5249 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5250 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005251 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5252 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005253 LoOps[0] = LHSL;
5254 LoOps[1] = RHSL;
5255 HiOps[0] = LHSH;
5256 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005257 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005258 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005259 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005260 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005261 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005262 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005263 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005264 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005265 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005266 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005267 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005268
5269 case ISD::ADDC:
5270 case ISD::SUBC: {
5271 // Expand the subcomponents.
5272 SDOperand LHSL, LHSH, RHSL, RHSH;
5273 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5274 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5275 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5276 SDOperand LoOps[2] = { LHSL, RHSL };
5277 SDOperand HiOps[3] = { LHSH, RHSH };
5278
5279 if (Node->getOpcode() == ISD::ADDC) {
5280 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5281 HiOps[2] = Lo.getValue(1);
5282 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5283 } else {
5284 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5285 HiOps[2] = Lo.getValue(1);
5286 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5287 }
5288 // Remember that we legalized the flag.
5289 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5290 break;
5291 }
5292 case ISD::ADDE:
5293 case ISD::SUBE: {
5294 // Expand the subcomponents.
5295 SDOperand LHSL, LHSH, RHSL, RHSH;
5296 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5297 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5298 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5299 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5300 SDOperand HiOps[3] = { LHSH, RHSH };
5301
5302 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5303 HiOps[2] = Lo.getValue(1);
5304 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5305
5306 // Remember that we legalized the flag.
5307 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5308 break;
5309 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005310 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005311 // If the target wants to custom expand this, let them.
5312 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005313 SDOperand New = TLI.LowerOperation(Op, DAG);
5314 if (New.Val) {
5315 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005316 break;
5317 }
5318 }
5319
Evan Chenge93762d2006-09-01 18:17:58 +00005320 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5321 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005322 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005323 SDOperand LL, LH, RL, RH;
5324 ExpandOp(Node->getOperand(0), LL, LH);
5325 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005326 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005327 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005328 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5329 // extended the sign bit of the low half through the upper half, and if so
5330 // emit a MULHS instead of the alternate sequence that is valid for any
5331 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005332 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005333 // is RH an extension of the sign bit of RL?
5334 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5335 RH.getOperand(1).getOpcode() == ISD::Constant &&
5336 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5337 // is LH an extension of the sign bit of LL?
5338 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5339 LH.getOperand(1).getOpcode() == ISD::Constant &&
5340 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005341 // Low part:
5342 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5343 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005344 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005345 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005346 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005347 // Low part:
5348 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5349
5350 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005351 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5352 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5353 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5354 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5355 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005356 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005357 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005358 }
Evan Chenge93762d2006-09-01 18:17:58 +00005359
Evan Cheng31cbddf2007-01-12 02:11:51 +00005360 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5361 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005362 break;
5363 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005364 case ISD::SDIV:
5365 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5366 break;
5367 case ISD::UDIV:
5368 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5369 break;
5370 case ISD::SREM:
5371 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5372 break;
5373 case ISD::UREM:
5374 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5375 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005376
Evan Cheng4eee7242006-12-09 02:42:38 +00005377 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005378 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5379 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5380 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005381 break;
5382 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005383 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5384 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5385 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005386 break;
5387 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005388 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5389 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5390 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005391 break;
5392 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005393 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5394 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5395 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005396 break;
5397 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005398 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005399 break;
5400 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005401 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005402 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005403 case ISD::FSQRT:
5404 case ISD::FSIN:
5405 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005406 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005407 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005408 case ISD::FSQRT:
5409 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5410 break;
5411 case ISD::FSIN:
5412 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5413 break;
5414 case ISD::FCOS:
5415 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5416 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005417 default: assert(0 && "Unreachable!");
5418 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005419 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005420 break;
5421 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005422 case ISD::FABS: {
5423 SDOperand Mask = (VT == MVT::f64)
5424 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5425 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5426 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5427 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5428 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5429 if (getTypeAction(NVT) == Expand)
5430 ExpandOp(Lo, Lo, Hi);
5431 break;
5432 }
5433 case ISD::FNEG: {
5434 SDOperand Mask = (VT == MVT::f64)
5435 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5436 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5437 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5438 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5439 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5440 if (getTypeAction(NVT) == Expand)
5441 ExpandOp(Lo, Lo, Hi);
5442 break;
5443 }
Evan Cheng003feb02007-01-04 21:56:39 +00005444 case ISD::FCOPYSIGN: {
5445 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5446 if (getTypeAction(NVT) == Expand)
5447 ExpandOp(Lo, Lo, Hi);
5448 break;
5449 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005450 case ISD::SINT_TO_FP:
5451 case ISD::UINT_TO_FP: {
5452 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5453 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005454 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005455 if (Node->getOperand(0).getValueType() == MVT::i64) {
5456 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005457 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005458 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005459 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005460 } else {
5461 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005462 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005463 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005464 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005465 }
5466
5467 // Promote the operand if needed.
5468 if (getTypeAction(SrcVT) == Promote) {
5469 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5470 Tmp = isSigned
5471 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5472 DAG.getValueType(SrcVT))
5473 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5474 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5475 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005476
5477 const char *LibCall = TLI.getLibcallName(LC);
5478 if (LibCall)
5479 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5480 else {
5481 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5482 Node->getOperand(0));
5483 if (getTypeAction(Lo.getValueType()) == Expand)
5484 ExpandOp(Lo, Lo, Hi);
5485 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005486 break;
5487 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005488 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005489
Chris Lattnerac12f682005-12-21 18:02:52 +00005490 // Make sure the resultant values have been legalized themselves, unless this
5491 // is a type that requires multi-step expansion.
5492 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5493 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005494 if (Hi.Val)
5495 // Don't legalize the high part if it is expanded to a single node.
5496 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005497 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005498
5499 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005500 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005501 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005502}
5503
Dan Gohmana8665142007-06-25 16:23:39 +00005504/// SplitVectorOp - Given an operand of vector type, break it down into
5505/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005506void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5507 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005508 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005509 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005510 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattner32206f52006-03-18 01:44:44 +00005511 assert(NumElements > 1 && "Cannot split a single element vector!");
5512 unsigned NewNumElts = NumElements/2;
Dan Gohmana8665142007-06-25 16:23:39 +00005513 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5514 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005515
5516 // See if we already split it.
5517 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5518 = SplitNodes.find(Op);
5519 if (I != SplitNodes.end()) {
5520 Lo = I->second.first;
5521 Hi = I->second.second;
5522 return;
5523 }
5524
5525 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005526 default:
5527#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005528 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005529#endif
5530 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00005531 case ISD::BUILD_PAIR:
5532 Lo = Node->getOperand(0);
5533 Hi = Node->getOperand(1);
5534 break;
5535 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005536 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5537 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00005538 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005539
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005540 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00005541 Node->op_end());
5542 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005543 break;
5544 }
Dan Gohmana8665142007-06-25 16:23:39 +00005545 case ISD::CONCAT_VECTORS: {
5546 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5547 if (NewNumSubvectors == 1) {
5548 Lo = Node->getOperand(0);
5549 Hi = Node->getOperand(1);
5550 } else {
5551 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5552 Node->op_begin()+NewNumSubvectors);
5553 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00005554
Dan Gohmana8665142007-06-25 16:23:39 +00005555 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5556 Node->op_end());
5557 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5558 }
Dan Gohman26455c42007-06-13 15:12:02 +00005559 break;
5560 }
Dan Gohmana8665142007-06-25 16:23:39 +00005561 case ISD::ADD:
5562 case ISD::SUB:
5563 case ISD::MUL:
5564 case ISD::FADD:
5565 case ISD::FSUB:
5566 case ISD::FMUL:
5567 case ISD::SDIV:
5568 case ISD::UDIV:
5569 case ISD::FDIV:
5570 case ISD::AND:
5571 case ISD::OR:
5572 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00005573 SDOperand LL, LH, RL, RH;
5574 SplitVectorOp(Node->getOperand(0), LL, LH);
5575 SplitVectorOp(Node->getOperand(1), RL, RH);
5576
Dan Gohmana8665142007-06-25 16:23:39 +00005577 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5578 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00005579 break;
5580 }
Dan Gohmana8665142007-06-25 16:23:39 +00005581 case ISD::LOAD: {
5582 LoadSDNode *LD = cast<LoadSDNode>(Node);
5583 SDOperand Ch = LD->getChain();
5584 SDOperand Ptr = LD->getBasePtr();
5585 const Value *SV = LD->getSrcValue();
5586 int SVOffset = LD->getSrcValueOffset();
5587 unsigned Alignment = LD->getAlignment();
5588 bool isVolatile = LD->isVolatile();
5589
5590 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5591 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00005592 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5593 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005594 SVOffset += IncrementSize;
5595 if (Alignment > IncrementSize)
5596 Alignment = IncrementSize;
5597 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00005598
5599 // Build a factor node to remember that this load is independent of the
5600 // other one.
5601 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5602 Hi.getValue(1));
5603
5604 // Remember that we legalized the chain.
5605 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005606 break;
5607 }
Dan Gohmana8665142007-06-25 16:23:39 +00005608 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005609 // We know the result is a vector. The input may be either a vector or a
5610 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00005611 SDOperand InOp = Node->getOperand(0);
5612 if (!MVT::isVector(InOp.getValueType()) ||
5613 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5614 // The input is a scalar or single-element vector.
5615 // Lower to a store/load so that it can be split.
5616 // FIXME: this could be improved probably.
5617 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005618
Evan Chengdf9ac472006-10-05 23:01:46 +00005619 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00005620 InOp, Ptr, NULL, 0);
5621 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005622 }
Dan Gohman0de76942007-06-29 00:09:08 +00005623 // Split the vector and convert each of the pieces now.
5624 SplitVectorOp(InOp, Lo, Hi);
5625 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5626 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5627 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005628 }
Chris Lattner32206f52006-03-18 01:44:44 +00005629 }
5630
5631 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005632 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005633 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00005634 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00005635}
5636
5637
Dan Gohmanf4e86da2007-06-27 14:06:22 +00005638/// ScalarizeVectorOp - Given an operand of single-element vector type
5639/// (e.g. v1f32), convert it into the equivalent operation that returns a
5640/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00005641SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5642 assert(MVT::isVector(Op.getValueType()) &&
5643 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00005644 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005645 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5646 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00005647
Dan Gohmana8665142007-06-25 16:23:39 +00005648 // See if we already scalarized it.
5649 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5650 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00005651
5652 SDOperand Result;
5653 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005654 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005655#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005656 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005657#endif
Dan Gohmana8665142007-06-25 16:23:39 +00005658 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5659 case ISD::ADD:
5660 case ISD::FADD:
5661 case ISD::SUB:
5662 case ISD::FSUB:
5663 case ISD::MUL:
5664 case ISD::FMUL:
5665 case ISD::SDIV:
5666 case ISD::UDIV:
5667 case ISD::FDIV:
5668 case ISD::SREM:
5669 case ISD::UREM:
5670 case ISD::FREM:
5671 case ISD::AND:
5672 case ISD::OR:
5673 case ISD::XOR:
5674 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00005675 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00005676 ScalarizeVectorOp(Node->getOperand(0)),
5677 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00005678 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005679 case ISD::FNEG:
5680 case ISD::FABS:
5681 case ISD::FSQRT:
5682 case ISD::FSIN:
5683 case ISD::FCOS:
5684 Result = DAG.getNode(Node->getOpcode(),
5685 NewVT,
5686 ScalarizeVectorOp(Node->getOperand(0)));
5687 break;
5688 case ISD::LOAD: {
5689 LoadSDNode *LD = cast<LoadSDNode>(Node);
5690 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5691 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005692
Dan Gohmana8665142007-06-25 16:23:39 +00005693 const Value *SV = LD->getSrcValue();
5694 int SVOffset = LD->getSrcValueOffset();
5695 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5696 LD->isVolatile(), LD->getAlignment());
5697
Chris Lattner32206f52006-03-18 01:44:44 +00005698 // Remember that we legalized the chain.
5699 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5700 break;
5701 }
Dan Gohmana8665142007-06-25 16:23:39 +00005702 case ISD::BUILD_VECTOR:
5703 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00005704 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005705 case ISD::INSERT_VECTOR_ELT:
5706 // Returning the inserted scalar element.
5707 Result = Node->getOperand(1);
5708 break;
5709 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00005710 assert(Node->getOperand(0).getValueType() == NewVT &&
5711 "Concat of non-legal vectors not yet supported!");
5712 Result = Node->getOperand(0);
5713 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005714 case ISD::VECTOR_SHUFFLE: {
5715 // Figure out if the scalar is the LHS or RHS and return it.
5716 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5717 if (cast<ConstantSDNode>(EltNum)->getValue())
5718 Result = ScalarizeVectorOp(Node->getOperand(1));
5719 else
5720 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00005721 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005722 }
5723 case ISD::EXTRACT_SUBVECTOR:
5724 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00005725 assert(Result.getValueType() == NewVT);
5726 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005727 case ISD::BIT_CONVERT:
5728 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005729 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005730 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00005731 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00005732 ScalarizeVectorOp(Op.getOperand(1)),
5733 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00005734 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005735 }
5736
5737 if (TLI.isTypeLegal(NewVT))
5738 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00005739 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5740 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00005741 return Result;
5742}
5743
Chris Lattnerdc750592005-01-07 07:47:09 +00005744
5745// SelectionDAG::Legalize - This is the entry point for the file.
5746//
Chris Lattner4add7e32005-01-23 04:42:50 +00005747void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005748 if (ViewLegalizeDAGs) viewGraph();
5749
Chris Lattnerdc750592005-01-07 07:47:09 +00005750 /// run - This is the main entry point to this class.
5751 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005752 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005753}
5754