blob: b25f86f774ce3716877bb324cd82c12ed27814a2 [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
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000553/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
554static
555SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
556 TargetLowering &TLI) {
557 assert(MVT::isInteger(ST->getStoredVT()) &&
558 "Non integer unaligned stores not implemented.");
559 int SVOffset = ST->getSrcValueOffset();
560 SDOperand Chain = ST->getChain();
561 SDOperand Ptr = ST->getBasePtr();
562 SDOperand Val = ST->getValue();
563 MVT::ValueType VT = Val.getValueType();
564 // Get the half-size VT
565 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
566 int NumBits = MVT::getSizeInBits(NewStoredVT);
567 int Alignment = ST->getAlignment();
568 int IncrementSize = NumBits / 8;
569
570 // Divide the stored value in two parts.
571 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
572 SDOperand Lo = Val;
573 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
574
575 // Store the two parts
576 SDOperand Store1, Store2;
577 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
578 ST->getSrcValue(), SVOffset, NewStoredVT,
579 ST->isVolatile(), Alignment);
580 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
581 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
582 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
583 ST->getSrcValue(), SVOffset + IncrementSize,
584 NewStoredVT, ST->isVolatile(), Alignment);
585
586 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
587}
588
589/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
590static
591SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
592 TargetLowering &TLI) {
593 assert(MVT::isInteger(LD->getLoadedVT()) &&
594 "Non integer unaligned loads not implemented.");
595 int SVOffset = LD->getSrcValueOffset();
596 SDOperand Chain = LD->getChain();
597 SDOperand Ptr = LD->getBasePtr();
598 MVT::ValueType VT = LD->getValueType(0);
599 MVT::ValueType NewLoadedVT = LD->getLoadedVT() - 1;
600 int NumBits = MVT::getSizeInBits(NewLoadedVT);
601 int Alignment = LD->getAlignment();
602 int IncrementSize = NumBits / 8;
603 ISD::LoadExtType HiExtType = LD->getExtensionType();
604
605 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
606 if (HiExtType == ISD::NON_EXTLOAD)
607 HiExtType = ISD::ZEXTLOAD;
608
609 // Load the value in two parts
610 SDOperand Lo, Hi;
611 if (TLI.isLittleEndian()) {
612 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
613 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
616 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
617 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
618 Alignment);
619 } else {
620 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
621 NewLoadedVT,LD->isVolatile(), Alignment);
622 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
623 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
624 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
625 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
626 Alignment);
627 }
628
629 // aggregate the two parts
630 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
631 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
632 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
633
634 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
635 Hi.getValue(1));
636
637 SDOperand Ops[] = { Result, TF };
638 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
639}
Evan Cheng003feb02007-01-04 21:56:39 +0000640
Dan Gohmanff727882007-07-13 20:14:11 +0000641/// LegalizeOp - We know that the specified value has a legal type, and
642/// that its operands are legal. Now ensure that the operation itself
643/// is legal, recursively ensuring that the operands' operations remain
644/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000645SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000646 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000647 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000648 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000649
Chris Lattnerdc750592005-01-07 07:47:09 +0000650 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000651 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000652 if (Node->getNumValues() > 1) {
653 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000654 if (getTypeAction(Node->getValueType(i)) != Legal) {
655 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000656 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000657 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000658 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000659 }
660 }
661
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000662 // Note that LegalizeOp may be reentered even from single-use nodes, which
663 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000664 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000665 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000666
Nate Begemane5b86d72005-08-10 20:51:12 +0000667 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000668 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000669 bool isCustom = false;
670
Chris Lattnerdc750592005-01-07 07:47:09 +0000671 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000672 case ISD::FrameIndex:
673 case ISD::EntryToken:
674 case ISD::Register:
675 case ISD::BasicBlock:
676 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000677 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000678 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000679 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000680 case ISD::TargetConstantPool:
681 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000682 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000683 case ISD::TargetExternalSymbol:
684 case ISD::VALUETYPE:
685 case ISD::SRCVALUE:
686 case ISD::STRING:
687 case ISD::CONDCODE:
688 // Primitives must all be legal.
689 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
690 "This must be legal!");
691 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000692 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000693 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
694 // If this is a target node, legalize it by legalizing the operands then
695 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000696 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000697 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000698 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000699
Chris Lattner97af9d52006-08-08 01:09:31 +0000700 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000701
702 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
703 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
704 return Result.getValue(Op.ResNo);
705 }
706 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000707#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000708 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000709#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000710 assert(0 && "Do not know how to legalize this operator!");
711 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000712 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000713 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000714 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000715 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000716 case ISD::ConstantPool:
717 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000718 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
719 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000720 case TargetLowering::Custom:
721 Tmp1 = TLI.LowerOperation(Op, DAG);
722 if (Tmp1.Val) Result = Tmp1;
723 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000724 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000725 break;
726 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000727 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000728 case ISD::FRAMEADDR:
729 case ISD::RETURNADDR:
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000730 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemaneda59972007-01-29 22:58:52 +0000731 // The only option for these nodes is to custom lower them. If the target
732 // does not custom lower them, then return zero.
733 Tmp1 = TLI.LowerOperation(Op, DAG);
734 if (Tmp1.Val)
735 Result = Tmp1;
736 else
737 Result = DAG.getConstant(0, TLI.getPointerTy());
738 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000739 case ISD::EXCEPTIONADDR: {
740 Tmp1 = LegalizeOp(Node->getOperand(0));
741 MVT::ValueType VT = Node->getValueType(0);
742 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000745 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000746 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
747 }
748 break;
749 case TargetLowering::Custom:
750 Result = TLI.LowerOperation(Op, DAG);
751 if (Result.Val) break;
752 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000753 case TargetLowering::Legal: {
754 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
755 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
756 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000757 break;
758 }
759 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000760 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000761 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000762 case ISD::EHSELECTION: {
763 Tmp1 = LegalizeOp(Node->getOperand(0));
764 Tmp2 = LegalizeOp(Node->getOperand(1));
765 MVT::ValueType VT = Node->getValueType(0);
766 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
767 default: assert(0 && "This action is not supported yet!");
768 case TargetLowering::Expand: {
769 unsigned Reg = TLI.getExceptionSelectorRegister();
770 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
771 }
772 break;
773 case TargetLowering::Custom:
774 Result = TLI.LowerOperation(Op, DAG);
775 if (Result.Val) break;
776 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000777 case TargetLowering::Legal: {
778 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
779 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
780 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000781 break;
782 }
783 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000784 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000785 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000786 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000787 MVT::ValueType VT = Node->getValueType(0);
788 // The only "good" option for this node is to custom lower it.
789 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
790 default: assert(0 && "This action is not supported at all!");
791 case TargetLowering::Custom:
792 Result = TLI.LowerOperation(Op, DAG);
793 if (Result.Val) break;
794 // Fall Thru
795 case TargetLowering::Legal:
796 // Target does not know, how to lower this, lower to noop
797 Result = LegalizeOp(Node->getOperand(0));
798 break;
799 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000800 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000801 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000802 case ISD::AssertSext:
803 case ISD::AssertZext:
804 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000806 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000807 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000808 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000809 Result = Node->getOperand(Op.ResNo);
810 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000811 case ISD::CopyFromReg:
812 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000813 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000814 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000816 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000817 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000818 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000819 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
821 } else {
822 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
823 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000824 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
825 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000826 // Since CopyFromReg produces two values, make sure to remember that we
827 // legalized both of them.
828 AddLegalizedOperand(Op.getValue(0), Result);
829 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
830 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000831 case ISD::UNDEF: {
832 MVT::ValueType VT = Op.getValueType();
833 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000836 if (MVT::isInteger(VT))
837 Result = DAG.getConstant(0, VT);
838 else if (MVT::isFloatingPoint(VT))
839 Result = DAG.getConstantFP(0, VT);
840 else
841 assert(0 && "Unknown value type!");
842 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000843 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000844 break;
845 }
846 break;
847 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000848
Chris Lattnere55d1712006-03-28 00:40:33 +0000849 case ISD::INTRINSIC_W_CHAIN:
850 case ISD::INTRINSIC_WO_CHAIN:
851 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000852 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000853 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
854 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000855 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000856
857 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000858 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000859 TargetLowering::Custom) {
860 Tmp3 = TLI.LowerOperation(Result, DAG);
861 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000862 }
863
864 if (Result.Val->getNumValues() == 1) break;
865
866 // Must have return value and chain result.
867 assert(Result.Val->getNumValues() == 2 &&
868 "Cannot return more than two values!");
869
870 // Since loads produce two values, make sure to remember that we
871 // legalized both of them.
872 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
873 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
874 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000875 }
Chris Lattner435b4022005-11-29 06:21:05 +0000876
877 case ISD::LOCATION:
878 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
880
881 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
882 case TargetLowering::Promote:
883 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000884 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000885 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000886 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000887 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000888
Jim Laskeyc56315c2007-01-26 21:22:28 +0000889 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000890 const std::string &FName =
891 cast<StringSDNode>(Node->getOperand(3))->getValue();
892 const std::string &DirName =
893 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000894 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000895
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000896 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000897 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000898 SDOperand LineOp = Node->getOperand(1);
899 SDOperand ColOp = Node->getOperand(2);
900
901 if (useDEBUG_LOC) {
902 Ops.push_back(LineOp); // line #
903 Ops.push_back(ColOp); // col #
904 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000905 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000906 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000907 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
908 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000909 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000910 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000911 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000912 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000913 } else {
914 Result = Tmp1; // chain
915 }
Chris Lattner435b4022005-11-29 06:21:05 +0000916 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000917 }
Chris Lattner435b4022005-11-29 06:21:05 +0000918 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000919 if (Tmp1 != Node->getOperand(0) ||
920 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000921 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000922 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000923 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
924 Ops.push_back(Node->getOperand(1)); // line # must be legal.
925 Ops.push_back(Node->getOperand(2)); // col # must be legal.
926 } else {
927 // Otherwise promote them.
928 Ops.push_back(PromoteOp(Node->getOperand(1)));
929 Ops.push_back(PromoteOp(Node->getOperand(2)));
930 }
Chris Lattner435b4022005-11-29 06:21:05 +0000931 Ops.push_back(Node->getOperand(3)); // filename must be legal.
932 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000933 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000934 }
935 break;
936 }
937 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000938
939 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000940 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000941 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000942 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000943 case TargetLowering::Legal:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
945 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
946 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
947 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000949 break;
950 }
951 break;
952
Jim Laskeyf9e54452007-01-26 14:34:52 +0000953 case ISD::LABEL:
954 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
955 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000956 default: assert(0 && "This action is not supported yet!");
957 case TargetLowering::Legal:
958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000960 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000961 break;
Chris Lattner567b9252007-03-03 19:21:38 +0000962 case TargetLowering::Expand:
963 Result = LegalizeOp(Node->getOperand(0));
964 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000965 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000966 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000967
Scott Michel9d09c5c2007-08-08 23:23:31 +0000968 case ISD::Constant: {
969 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
970 unsigned opAction =
971 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
972
Chris Lattnerdc750592005-01-07 07:47:09 +0000973 // We know we don't need to expand constants here, constants only have one
974 // value and we check that it is fine above.
975
Scott Michel9d09c5c2007-08-08 23:23:31 +0000976 if (opAction == TargetLowering::Custom) {
977 Tmp1 = TLI.LowerOperation(Result, DAG);
978 if (Tmp1.Val)
979 Result = Tmp1;
980 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000981 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +0000982 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000983 case ISD::ConstantFP: {
984 // Spill FP immediates to the constant pool if the target cannot directly
985 // codegen them. Targets often have some immediate values that can be
986 // efficiently generated into an FP register without a load. We explicitly
987 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000988 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
989
990 // Check to see if this FP immediate is already legal.
991 bool isLegal = false;
992 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
993 E = TLI.legal_fpimm_end(); I != E; ++I)
994 if (CFP->isExactlyValue(*I)) {
995 isLegal = true;
996 break;
997 }
998
Chris Lattner758b0ac2006-01-29 06:26:56 +0000999 // If this is a legal constant, turn it into a TargetConstantFP node.
1000 if (isLegal) {
1001 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
1002 break;
1003 }
1004
1005 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1006 default: assert(0 && "This action is not supported yet!");
1007 case TargetLowering::Custom:
1008 Tmp3 = TLI.LowerOperation(Result, DAG);
1009 if (Tmp3.Val) {
1010 Result = Tmp3;
1011 break;
1012 }
1013 // FALLTHROUGH
1014 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001015 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001016 }
1017 break;
1018 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001019 case ISD::TokenFactor:
1020 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001021 Tmp1 = LegalizeOp(Node->getOperand(0));
1022 Tmp2 = LegalizeOp(Node->getOperand(1));
1023 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1024 } else if (Node->getNumOperands() == 3) {
1025 Tmp1 = LegalizeOp(Node->getOperand(0));
1026 Tmp2 = LegalizeOp(Node->getOperand(1));
1027 Tmp3 = LegalizeOp(Node->getOperand(2));
1028 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001029 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001030 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001031 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001032 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1033 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001035 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001036 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001037
1038 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001039 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001040 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001041 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1042 assert(Tmp3.Val && "Target didn't custom lower this node!");
1043 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1044 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001045
Chris Lattneraaa23d92006-05-16 22:53:20 +00001046 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001047 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001048 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1049 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001050 if (Op.ResNo == i)
1051 Tmp2 = Tmp1;
1052 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1053 }
1054 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001055 case ISD::EXTRACT_SUBREG: {
1056 Tmp1 = LegalizeOp(Node->getOperand(0));
1057 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1058 assert(idx && "Operand must be a constant");
1059 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1061 }
1062 break;
1063 case ISD::INSERT_SUBREG: {
1064 Tmp1 = LegalizeOp(Node->getOperand(0));
1065 Tmp2 = LegalizeOp(Node->getOperand(1));
1066 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1067 assert(idx && "Operand must be a constant");
1068 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1070 }
1071 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001072 case ISD::BUILD_VECTOR:
1073 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001074 default: assert(0 && "This action is not supported yet!");
1075 case TargetLowering::Custom:
1076 Tmp3 = TLI.LowerOperation(Result, DAG);
1077 if (Tmp3.Val) {
1078 Result = Tmp3;
1079 break;
1080 }
1081 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001082 case TargetLowering::Expand:
1083 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001084 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001085 }
Chris Lattner29b23012006-03-19 01:17:20 +00001086 break;
1087 case ISD::INSERT_VECTOR_ELT:
1088 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1089 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1090 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1092
1093 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1094 Node->getValueType(0))) {
1095 default: assert(0 && "This action is not supported yet!");
1096 case TargetLowering::Legal:
1097 break;
1098 case TargetLowering::Custom:
1099 Tmp3 = TLI.LowerOperation(Result, DAG);
1100 if (Tmp3.Val) {
1101 Result = Tmp3;
1102 break;
1103 }
1104 // FALLTHROUGH
1105 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001106 // If the insert index is a constant, codegen this as a scalar_to_vector,
1107 // then a shuffle that inserts it into the right position in the vector.
1108 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1109 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1110 Tmp1.getValueType(), Tmp2);
1111
1112 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1113 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001114 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001115
1116 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1117 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1118 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001119 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001120 for (unsigned i = 0; i != NumElts; ++i) {
1121 if (i != InsertPos->getValue())
1122 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1123 else
1124 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1125 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001126 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1127 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001128
1129 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1130 Tmp1, ScVec, ShufMask);
1131 Result = LegalizeOp(Result);
1132 break;
1133 }
1134
Chris Lattner29b23012006-03-19 01:17:20 +00001135 // If the target doesn't support this, we have to spill the input vector
1136 // to a temporary stack slot, update the element, then reload it. This is
1137 // badness. We could also load the value into a vector register (either
1138 // with a "move to register" or "extload into register" instruction, then
1139 // permute it into place, if the idx is a constant and if the idx is
1140 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001141 MVT::ValueType VT = Tmp1.getValueType();
1142 MVT::ValueType EltVT = Tmp2.getValueType();
1143 MVT::ValueType IdxVT = Tmp3.getValueType();
1144 MVT::ValueType PtrVT = TLI.getPointerTy();
1145 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001146 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001147 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001148
1149 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001150 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1151 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001152 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001153 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1154 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1155 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001156 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001157 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001158 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001159 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001160 break;
1161 }
1162 }
1163 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001164 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001165 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1166 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1167 break;
1168 }
1169
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001170 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1171 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1172 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1173 Node->getValueType(0))) {
1174 default: assert(0 && "This action is not supported yet!");
1175 case TargetLowering::Legal:
1176 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001177 case TargetLowering::Custom:
1178 Tmp3 = TLI.LowerOperation(Result, DAG);
1179 if (Tmp3.Val) {
1180 Result = Tmp3;
1181 break;
1182 }
1183 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001184 case TargetLowering::Expand:
1185 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001186 break;
1187 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001188 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001189 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1191 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1193
1194 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001195 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1196 default: assert(0 && "Unknown operation action!");
1197 case TargetLowering::Legal:
1198 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1199 "vector shuffle should not be created if not legal!");
1200 break;
1201 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001202 Tmp3 = TLI.LowerOperation(Result, DAG);
1203 if (Tmp3.Val) {
1204 Result = Tmp3;
1205 break;
1206 }
1207 // FALLTHROUGH
1208 case TargetLowering::Expand: {
1209 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001210 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001211 MVT::ValueType PtrVT = TLI.getPointerTy();
1212 SDOperand Mask = Node->getOperand(2);
1213 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001214 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001215 for (unsigned i = 0; i != NumElems; ++i) {
1216 SDOperand Arg = Mask.getOperand(i);
1217 if (Arg.getOpcode() == ISD::UNDEF) {
1218 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1219 } else {
1220 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1221 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1222 if (Idx < NumElems)
1223 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1224 DAG.getConstant(Idx, PtrVT)));
1225 else
1226 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1227 DAG.getConstant(Idx - NumElems, PtrVT)));
1228 }
1229 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001230 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001231 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001232 }
Chris Lattner6be79822006-04-04 17:23:26 +00001233 case TargetLowering::Promote: {
1234 // Change base type to a different vector type.
1235 MVT::ValueType OVT = Node->getValueType(0);
1236 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1237
1238 // Cast the two input vectors.
1239 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1240 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1241
1242 // Convert the shuffle mask to the right # elements.
1243 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1244 assert(Tmp3.Val && "Shuffle not legal?");
1245 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1246 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1247 break;
1248 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001249 }
1250 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001251
1252 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001253 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001254 Tmp2 = LegalizeOp(Node->getOperand(1));
1255 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001256 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001257 break;
1258
Dan Gohmana8665142007-06-25 16:23:39 +00001259 case ISD::EXTRACT_SUBVECTOR:
1260 Tmp1 = Node->getOperand(0);
1261 Tmp2 = LegalizeOp(Node->getOperand(1));
1262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1263 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001264 break;
1265
Chris Lattner462505f2006-02-13 09:18:02 +00001266 case ISD::CALLSEQ_START: {
1267 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1268
1269 // Recursively Legalize all of the inputs of the call end that do not lead
1270 // to this call start. This ensures that any libcalls that need be inserted
1271 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001272 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001273 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001274 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1275 NodesLeadingTo);
1276 }
Chris Lattner462505f2006-02-13 09:18:02 +00001277
1278 // Now that we legalized all of the inputs (which may have inserted
1279 // libcalls) create the new CALLSEQ_START node.
1280 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1281
1282 // Merge in the last call, to ensure that this call start after the last
1283 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001284 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001285 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1286 Tmp1 = LegalizeOp(Tmp1);
1287 }
Chris Lattner462505f2006-02-13 09:18:02 +00001288
1289 // Do not try to legalize the target-specific arguments (#1+).
1290 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001291 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001292 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001293 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001294 }
1295
1296 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001297 AddLegalizedOperand(Op.getValue(0), Result);
1298 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1299 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1300
Chris Lattner462505f2006-02-13 09:18:02 +00001301 // Now that the callseq_start and all of the non-call nodes above this call
1302 // sequence have been legalized, legalize the call itself. During this
1303 // process, no libcalls can/will be inserted, guaranteeing that no calls
1304 // can overlap.
1305 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1306 SDOperand InCallSEQ = LastCALLSEQ_END;
1307 // Note that we are selecting this call!
1308 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1309 IsLegalizingCall = true;
1310
1311 // Legalize the call, starting from the CALLSEQ_END.
1312 LegalizeOp(LastCALLSEQ_END);
1313 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1314 return Result;
1315 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001316 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001317 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1318 // will cause this node to be legalized as well as handling libcalls right.
1319 if (LastCALLSEQ_END.Val != Node) {
1320 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001321 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001322 assert(I != LegalizedNodes.end() &&
1323 "Legalizing the call start should have legalized this node!");
1324 return I->second;
1325 }
1326
1327 // Otherwise, the call start has been legalized and everything is going
1328 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001330 // Do not try to legalize the target-specific arguments (#1+), except for
1331 // an optional flag input.
1332 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1333 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001334 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001335 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001336 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001337 }
1338 } else {
1339 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1340 if (Tmp1 != Node->getOperand(0) ||
1341 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001342 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001343 Ops[0] = Tmp1;
1344 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001345 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001346 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001347 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001348 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001349 // This finishes up call legalization.
1350 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001351
1352 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1353 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1354 if (Node->getNumValues() == 2)
1355 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1356 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001357 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1360 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001362
Chris Lattnerd02b0542006-01-28 10:58:55 +00001363 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001364 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001365 switch (TLI.getOperationAction(Node->getOpcode(),
1366 Node->getValueType(0))) {
1367 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001368 case TargetLowering::Expand: {
1369 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1370 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1371 " not tell us which reg is the stack pointer!");
1372 SDOperand Chain = Tmp1.getOperand(0);
1373 SDOperand Size = Tmp2.getOperand(1);
1374 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1375 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1376 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001377 Tmp1 = LegalizeOp(Tmp1);
1378 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001379 break;
1380 }
1381 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001382 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1383 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001384 Tmp1 = LegalizeOp(Tmp3);
1385 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001386 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001387 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001388 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001389 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001390 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001391 // Since this op produce two values, make sure to remember that we
1392 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001393 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1394 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001395 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001396 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001397 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001398 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001399 bool Changed = false;
1400 // Legalize all of the operands of the inline asm, in case they are nodes
1401 // that need to be expanded or something. Note we skip the asm string and
1402 // all of the TargetConstant flags.
1403 SDOperand Op = LegalizeOp(Ops[0]);
1404 Changed = Op != Ops[0];
1405 Ops[0] = Op;
1406
1407 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1408 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1409 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1410 for (++i; NumVals; ++i, --NumVals) {
1411 SDOperand Op = LegalizeOp(Ops[i]);
1412 if (Op != Ops[i]) {
1413 Changed = true;
1414 Ops[i] = Op;
1415 }
1416 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001417 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001418
1419 if (HasInFlag) {
1420 Op = LegalizeOp(Ops.back());
1421 Changed |= Op != Ops.back();
1422 Ops.back() = Op;
1423 }
1424
1425 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001426 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001427
1428 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001429 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001430 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1431 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001432 }
Chris Lattner68a12142005-01-07 22:12:08 +00001433 case ISD::BR:
1434 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001435 // Ensure that libcalls are emitted before a branch.
1436 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1437 Tmp1 = LegalizeOp(Tmp1);
1438 LastCALLSEQ_END = DAG.getEntryNode();
1439
Chris Lattnerd02b0542006-01-28 10:58:55 +00001440 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001441 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001442 case ISD::BRIND:
1443 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1444 // Ensure that libcalls are emitted before a branch.
1445 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1446 Tmp1 = LegalizeOp(Tmp1);
1447 LastCALLSEQ_END = DAG.getEntryNode();
1448
1449 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1450 default: assert(0 && "Indirect target must be legal type (pointer)!");
1451 case Legal:
1452 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1453 break;
1454 }
1455 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1456 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001457 case ISD::BR_JT:
1458 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1459 // Ensure that libcalls are emitted before a branch.
1460 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1461 Tmp1 = LegalizeOp(Tmp1);
1462 LastCALLSEQ_END = DAG.getEntryNode();
1463
1464 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1466
1467 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1468 default: assert(0 && "This action is not supported yet!");
1469 case TargetLowering::Legal: break;
1470 case TargetLowering::Custom:
1471 Tmp1 = TLI.LowerOperation(Result, DAG);
1472 if (Tmp1.Val) Result = Tmp1;
1473 break;
1474 case TargetLowering::Expand: {
1475 SDOperand Chain = Result.getOperand(0);
1476 SDOperand Table = Result.getOperand(1);
1477 SDOperand Index = Result.getOperand(2);
1478
1479 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001480 MachineFunction &MF = DAG.getMachineFunction();
1481 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001482 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1483 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001484
1485 SDOperand LD;
1486 switch (EntrySize) {
1487 default: assert(0 && "Size of jump table not supported yet."); break;
1488 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1489 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1490 }
1491
1492 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001493 // For PIC, the sequence is:
1494 // BRIND(load(Jumptable + index) + RelocBase)
1495 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1496 SDOperand Reloc;
1497 if (TLI.usesGlobalOffsetTable())
1498 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1499 else
1500 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001501 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001502 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1503 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1504 } else {
1505 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1506 }
1507 }
1508 }
1509 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001510 case ISD::BRCOND:
1511 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001512 // Ensure that libcalls are emitted before a return.
1513 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1514 Tmp1 = LegalizeOp(Tmp1);
1515 LastCALLSEQ_END = DAG.getEntryNode();
1516
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001517 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1518 case Expand: assert(0 && "It's impossible to expand bools");
1519 case Legal:
1520 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1521 break;
1522 case Promote:
1523 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001524
1525 // The top bits of the promoted condition are not necessarily zero, ensure
1526 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001527 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001528 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1529 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001530 break;
1531 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001532
1533 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001535
1536 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1537 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001538 case TargetLowering::Legal: break;
1539 case TargetLowering::Custom:
1540 Tmp1 = TLI.LowerOperation(Result, DAG);
1541 if (Tmp1.Val) Result = Tmp1;
1542 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001543 case TargetLowering::Expand:
1544 // Expand brcond's setcc into its constituent parts and create a BR_CC
1545 // Node.
1546 if (Tmp2.getOpcode() == ISD::SETCC) {
1547 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1548 Tmp2.getOperand(0), Tmp2.getOperand(1),
1549 Node->getOperand(2));
1550 } else {
1551 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1552 DAG.getCondCode(ISD::SETNE), Tmp2,
1553 DAG.getConstant(0, Tmp2.getValueType()),
1554 Node->getOperand(2));
1555 }
1556 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001557 }
1558 break;
1559 case ISD::BR_CC:
1560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001561 // Ensure that libcalls are emitted before a branch.
1562 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1563 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001564 Tmp2 = Node->getOperand(2); // LHS
1565 Tmp3 = Node->getOperand(3); // RHS
1566 Tmp4 = Node->getOperand(1); // CC
1567
1568 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001569 LastCALLSEQ_END = DAG.getEntryNode();
1570
Nate Begeman7e7f4392006-02-01 07:19:44 +00001571 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1572 // the LHS is a legal SETCC itself. In this case, we need to compare
1573 // the result against zero to select between true and false values.
1574 if (Tmp3.Val == 0) {
1575 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1576 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001577 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001578
1579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1580 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001581
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001582 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1583 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001584 case TargetLowering::Legal: break;
1585 case TargetLowering::Custom:
1586 Tmp4 = TLI.LowerOperation(Result, DAG);
1587 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001588 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001589 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001590 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001591 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001592 LoadSDNode *LD = cast<LoadSDNode>(Node);
1593 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1594 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001595
Evan Chenge71fe34d2006-10-09 20:57:25 +00001596 ISD::LoadExtType ExtType = LD->getExtensionType();
1597 if (ExtType == ISD::NON_EXTLOAD) {
1598 MVT::ValueType VT = Node->getValueType(0);
1599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1600 Tmp3 = Result.getValue(0);
1601 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001602
Evan Chenge71fe34d2006-10-09 20:57:25 +00001603 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1604 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001605 case TargetLowering::Legal:
1606 // If this is an unaligned load and the target doesn't support it,
1607 // expand it.
1608 if (!TLI.allowsUnalignedMemoryAccesses()) {
1609 unsigned ABIAlignment = TLI.getTargetData()->
1610 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1611 if (LD->getAlignment() < ABIAlignment){
1612 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1613 TLI);
1614 Tmp3 = Result.getOperand(0);
1615 Tmp4 = Result.getOperand(1);
1616 LegalizeOp(Tmp3);
1617 LegalizeOp(Tmp4);
1618 }
1619 }
1620 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001621 case TargetLowering::Custom:
1622 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1623 if (Tmp1.Val) {
1624 Tmp3 = LegalizeOp(Tmp1);
1625 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001626 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001627 break;
1628 case TargetLowering::Promote: {
1629 // Only promote a load of vector type to another.
1630 assert(MVT::isVector(VT) && "Cannot promote this load!");
1631 // Change base type to a different vector type.
1632 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1633
1634 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001635 LD->getSrcValueOffset(),
1636 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001637 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1638 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001639 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001640 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001641 }
1642 // Since loads produce two values, make sure to remember that we
1643 // legalized both of them.
1644 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1645 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1646 return Op.ResNo ? Tmp4 : Tmp3;
1647 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001648 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001649 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Promote:
1652 assert(SrcVT == MVT::i1 &&
1653 "Can only promote extending LOAD from i1 -> i8!");
1654 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1655 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001656 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001657 Tmp1 = Result.getValue(0);
1658 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001659 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001660 case TargetLowering::Custom:
1661 isCustom = true;
1662 // FALLTHROUGH
1663 case TargetLowering::Legal:
1664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1665 Tmp1 = Result.getValue(0);
1666 Tmp2 = Result.getValue(1);
1667
1668 if (isCustom) {
1669 Tmp3 = TLI.LowerOperation(Result, DAG);
1670 if (Tmp3.Val) {
1671 Tmp1 = LegalizeOp(Tmp3);
1672 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1673 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001674 } else {
1675 // If this is an unaligned load and the target doesn't support it,
1676 // expand it.
1677 if (!TLI.allowsUnalignedMemoryAccesses()) {
1678 unsigned ABIAlignment = TLI.getTargetData()->
1679 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1680 if (LD->getAlignment() < ABIAlignment){
1681 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1682 TLI);
1683 Tmp1 = Result.getOperand(0);
1684 Tmp2 = Result.getOperand(1);
1685 LegalizeOp(Tmp1);
1686 LegalizeOp(Tmp2);
1687 }
1688 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001689 }
1690 break;
1691 case TargetLowering::Expand:
1692 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1693 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1694 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001695 LD->getSrcValueOffset(),
1696 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001697 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1698 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1699 Tmp2 = LegalizeOp(Load.getValue(1));
1700 break;
1701 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001702 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001703 // Turn the unsupported load into an EXTLOAD followed by an explicit
1704 // zero/sign extend inreg.
1705 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1706 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001707 LD->getSrcValueOffset(), SrcVT,
1708 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001709 SDOperand ValRes;
1710 if (ExtType == ISD::SEXTLOAD)
1711 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1712 Result, DAG.getValueType(SrcVT));
1713 else
1714 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1715 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1716 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1717 break;
1718 }
1719 // Since loads produce two values, make sure to remember that we legalized
1720 // both of them.
1721 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1722 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1723 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001724 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001725 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001726 case ISD::EXTRACT_ELEMENT: {
1727 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1728 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001729 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001730 case Legal:
1731 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1732 // 1 -> Hi
1733 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1734 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1735 TLI.getShiftAmountTy()));
1736 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1737 } else {
1738 // 0 -> Lo
1739 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1740 Node->getOperand(0));
1741 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001742 break;
1743 case Expand:
1744 // Get both the low and high parts.
1745 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1746 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1747 Result = Tmp2; // 1 -> Hi
1748 else
1749 Result = Tmp1; // 0 -> Lo
1750 break;
1751 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001752 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001753 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001754
1755 case ISD::CopyToReg:
1756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001757
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001758 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001759 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001760 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001761 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001762 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001764 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001765 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001766 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001767 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001768 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1769 Tmp3);
1770 } else {
1771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001772 }
1773
1774 // Since this produces two values, make sure to remember that we legalized
1775 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001776 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001777 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001778 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001779 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001780 break;
1781
1782 case ISD::RET:
1783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001784
1785 // Ensure that libcalls are emitted before a return.
1786 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1787 Tmp1 = LegalizeOp(Tmp1);
1788 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001789
Chris Lattnerdc750592005-01-07 07:47:09 +00001790 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001791 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001792 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001793 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001794 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001795 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001796 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001797 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001798 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001799 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001800 SDOperand Lo, Hi;
1801 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001802
1803 // Big endian systems want the hi reg first.
1804 if (!TLI.isLittleEndian())
1805 std::swap(Lo, Hi);
1806
Evan Cheng3432ab92006-12-11 19:27:14 +00001807 if (Hi.Val)
1808 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1809 else
1810 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001811 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001812 } else {
1813 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001814 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1815 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001816
Dan Gohmana8665142007-06-25 16:23:39 +00001817 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001818 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001819 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001820 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001821 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001822 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001824 } else if (NumElems == 1) {
1825 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001826 Tmp2 = ScalarizeVectorOp(Tmp2);
1827 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001829
1830 // FIXME: Returns of gcc generic vectors smaller than a legal type
1831 // should be returned in integer registers!
1832
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001833 // The scalarized value type may not be legal, e.g. it might require
1834 // promotion or expansion. Relegalize the return.
1835 Result = LegalizeOp(Result);
1836 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001837 // FIXME: Returns of gcc generic vectors larger than a legal vector
1838 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001839 SDOperand Lo, Hi;
1840 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001841 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001842 Result = LegalizeOp(Result);
1843 }
1844 }
Misha Brukman835702a2005-04-21 22:36:52 +00001845 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001846 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001847 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001848 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001849 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001850 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001851 }
1852 break;
1853 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001854 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001855 break;
1856 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001857 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001858 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001859 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001860 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1861 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001862 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001863 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001864 break;
1865 case Expand: {
1866 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001867 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001868 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001869 ExpandOp(Node->getOperand(i), Lo, Hi);
1870 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001871 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001872 if (Hi.Val) {
1873 NewValues.push_back(Hi);
1874 NewValues.push_back(Node->getOperand(i+1));
1875 }
Misha Brukman835702a2005-04-21 22:36:52 +00001876 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001877 }
1878 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001879 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001880 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001881
1882 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001883 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001884 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001885 Result = DAG.getNode(ISD::RET, MVT::Other,
1886 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001887 break;
1888 }
1889 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001890
Chris Lattner4d1ea712006-01-29 21:02:23 +00001891 if (Result.getOpcode() == ISD::RET) {
1892 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1893 default: assert(0 && "This action is not supported yet!");
1894 case TargetLowering::Legal: break;
1895 case TargetLowering::Custom:
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
1898 break;
1899 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001900 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001901 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001902 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001903 StoreSDNode *ST = cast<StoreSDNode>(Node);
1904 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1905 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00001906 int SVOffset = ST->getSrcValueOffset();
1907 unsigned Alignment = ST->getAlignment();
1908 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00001909
Evan Chengab51cf22006-10-13 21:14:26 +00001910 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001911 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1912 // FIXME: We shouldn't do this for TargetConstantFP's.
1913 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1914 // to phase ordering between legalized code and the dag combiner. This
1915 // probably means that we need to integrate dag combiner and legalizer
1916 // together.
1917 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1918 if (CFP->getValueType(0) == MVT::f32) {
1919 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1920 } else {
1921 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1922 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1923 }
1924 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001925 SVOffset, isVolatile, Alignment);
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001926 break;
1927 }
1928
Evan Chengab51cf22006-10-13 21:14:26 +00001929 switch (getTypeAction(ST->getStoredVT())) {
1930 case Legal: {
1931 Tmp3 = LegalizeOp(ST->getValue());
1932 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1933 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001934
Evan Chengab51cf22006-10-13 21:14:26 +00001935 MVT::ValueType VT = Tmp3.getValueType();
1936 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1937 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001938 case TargetLowering::Legal:
1939 // If this is an unaligned store and the target doesn't support it,
1940 // expand it.
1941 if (!TLI.allowsUnalignedMemoryAccesses()) {
1942 unsigned ABIAlignment = TLI.getTargetData()->
1943 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
1944 if (ST->getAlignment() < ABIAlignment)
1945 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
1946 TLI);
1947 }
1948 break;
Evan Chengab51cf22006-10-13 21:14:26 +00001949 case TargetLowering::Custom:
1950 Tmp1 = TLI.LowerOperation(Result, DAG);
1951 if (Tmp1.Val) Result = Tmp1;
1952 break;
1953 case TargetLowering::Promote:
1954 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1955 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1956 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1957 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00001958 ST->getSrcValue(), SVOffset, isVolatile,
1959 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001960 break;
1961 }
1962 break;
1963 }
1964 case Promote:
1965 // Truncate the value and store the result.
1966 Tmp3 = PromoteOp(ST->getValue());
1967 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001968 SVOffset, ST->getStoredVT(),
1969 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001970 break;
1971
1972 case Expand:
1973 unsigned IncrementSize = 0;
1974 SDOperand Lo, Hi;
1975
1976 // If this is a vector type, then we have to calculate the increment as
1977 // the product of the element size in bytes, and the number of elements
1978 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00001979 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00001980 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001981 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1982 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00001983
Dan Gohmana8665142007-06-25 16:23:39 +00001984 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001985 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001986 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001987 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001988 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001989 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001990 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001991 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001992 Result = LegalizeOp(Result);
1993 break;
1994 } else if (NumElems == 1) {
1995 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001996 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00001997 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001998 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00001999 // The scalarized value type may not be legal, e.g. it might require
2000 // promotion or expansion. Relegalize the scalar store.
2001 Result = LegalizeOp(Result);
2002 break;
2003 } else {
2004 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2005 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2006 }
2007 } else {
2008 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002009 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002010
2011 if (!TLI.isLittleEndian())
2012 std::swap(Lo, Hi);
2013 }
2014
2015 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002016 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002017
2018 if (Hi.Val == NULL) {
2019 // Must be int <-> float one-to-one expansion.
2020 Result = Lo;
2021 break;
2022 }
2023
Evan Chengab51cf22006-10-13 21:14:26 +00002024 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2025 getIntPtrConstant(IncrementSize));
2026 assert(isTypeLegal(Tmp2.getValueType()) &&
2027 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002028 SVOffset += IncrementSize;
2029 if (Alignment > IncrementSize)
2030 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00002031 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002032 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002033 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2034 break;
2035 }
2036 } else {
2037 // Truncating store
2038 assert(isTypeLegal(ST->getValue().getValueType()) &&
2039 "Cannot handle illegal TRUNCSTORE yet!");
2040 Tmp3 = LegalizeOp(ST->getValue());
2041
2042 // The only promote case we handle is TRUNCSTORE:i1 X into
2043 // -> TRUNCSTORE:i8 (and X, 1)
2044 if (ST->getStoredVT() == MVT::i1 &&
2045 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2046 // Promote the bool to a mask then store.
2047 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2048 DAG.getConstant(1, Tmp3.getValueType()));
2049 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002050 SVOffset, MVT::i8,
2051 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002052 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2053 Tmp2 != ST->getBasePtr()) {
2054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2055 ST->getOffset());
2056 }
2057
2058 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2059 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002060 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002061 case TargetLowering::Legal:
2062 // If this is an unaligned store and the target doesn't support it,
2063 // expand it.
2064 if (!TLI.allowsUnalignedMemoryAccesses()) {
2065 unsigned ABIAlignment = TLI.getTargetData()->
2066 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2067 if (ST->getAlignment() < ABIAlignment)
2068 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2069 TLI);
2070 }
2071 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002072 case TargetLowering::Custom:
2073 Tmp1 = TLI.LowerOperation(Result, DAG);
2074 if (Tmp1.Val) Result = Tmp1;
2075 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002076 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002077 }
2078 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002079 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002080 case ISD::PCMARKER:
2081 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002083 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002084 case ISD::STACKSAVE:
2085 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002086 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2087 Tmp1 = Result.getValue(0);
2088 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002089
Chris Lattnerb3266452006-01-13 02:50:02 +00002090 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2091 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002094 Tmp3 = TLI.LowerOperation(Result, DAG);
2095 if (Tmp3.Val) {
2096 Tmp1 = LegalizeOp(Tmp3);
2097 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002098 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002099 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002100 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002101 // Expand to CopyFromReg if the target set
2102 // StackPointerRegisterToSaveRestore.
2103 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002104 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002105 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002106 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002107 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002108 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2109 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002110 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002111 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002112 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002113
2114 // Since stacksave produce two values, make sure to remember that we
2115 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002116 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2117 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2118 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002119
Chris Lattnerb3266452006-01-13 02:50:02 +00002120 case ISD::STACKRESTORE:
2121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002124
2125 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2126 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002127 case TargetLowering::Legal: break;
2128 case TargetLowering::Custom:
2129 Tmp1 = TLI.LowerOperation(Result, DAG);
2130 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002131 break;
2132 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002133 // Expand to CopyToReg if the target set
2134 // StackPointerRegisterToSaveRestore.
2135 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2136 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2137 } else {
2138 Result = Tmp1;
2139 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002140 break;
2141 }
2142 break;
2143
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002144 case ISD::READCYCLECOUNTER:
2145 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002146 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002147 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2148 Node->getValueType(0))) {
2149 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002150 case TargetLowering::Legal:
2151 Tmp1 = Result.getValue(0);
2152 Tmp2 = Result.getValue(1);
2153 break;
Evan Cheng69739932006-11-29 08:26:18 +00002154 case TargetLowering::Custom:
2155 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002156 Tmp1 = LegalizeOp(Result.getValue(0));
2157 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002158 break;
2159 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002160
2161 // Since rdcc produce two values, make sure to remember that we legalized
2162 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002163 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2164 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002165 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002166
Chris Lattner39c67442005-01-14 22:08:15 +00002167 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2169 case Expand: assert(0 && "It's impossible to expand bools");
2170 case Legal:
2171 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2172 break;
2173 case Promote:
2174 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002175 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002176 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002177 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2178 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002179 break;
2180 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002181 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002182 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002183
Chris Lattnerd02b0542006-01-28 10:58:55 +00002184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002185
Nate Begeman987121a2005-08-23 04:29:48 +00002186 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002187 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002188 case TargetLowering::Legal: break;
2189 case TargetLowering::Custom: {
2190 Tmp1 = TLI.LowerOperation(Result, DAG);
2191 if (Tmp1.Val) Result = Tmp1;
2192 break;
2193 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002194 case TargetLowering::Expand:
2195 if (Tmp1.getOpcode() == ISD::SETCC) {
2196 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2197 Tmp2, Tmp3,
2198 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2199 } else {
2200 Result = DAG.getSelectCC(Tmp1,
2201 DAG.getConstant(0, Tmp1.getValueType()),
2202 Tmp2, Tmp3, ISD::SETNE);
2203 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002204 break;
2205 case TargetLowering::Promote: {
2206 MVT::ValueType NVT =
2207 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2208 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002209 if (MVT::isVector(Tmp2.getValueType())) {
2210 ExtOp = ISD::BIT_CONVERT;
2211 TruncOp = ISD::BIT_CONVERT;
2212 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002213 ExtOp = ISD::ANY_EXTEND;
2214 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002215 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002216 ExtOp = ISD::FP_EXTEND;
2217 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002218 }
2219 // Promote each of the values to the new type.
2220 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2221 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2222 // Perform the larger operation, then round down.
2223 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2224 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2225 break;
2226 }
2227 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002228 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002229 case ISD::SELECT_CC: {
2230 Tmp1 = Node->getOperand(0); // LHS
2231 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002232 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2233 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002234 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002235
Nate Begeman7e7f4392006-02-01 07:19:44 +00002236 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2237
2238 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2239 // the LHS is a legal SETCC itself. In this case, we need to compare
2240 // the result against zero to select between true and false values.
2241 if (Tmp2.Val == 0) {
2242 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2243 CC = DAG.getCondCode(ISD::SETNE);
2244 }
2245 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2246
2247 // Everything is legal, see if we should expand this op or something.
2248 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2249 default: assert(0 && "This action is not supported yet!");
2250 case TargetLowering::Legal: break;
2251 case TargetLowering::Custom:
2252 Tmp1 = TLI.LowerOperation(Result, DAG);
2253 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002254 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002255 }
2256 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002257 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002258 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002259 Tmp1 = Node->getOperand(0);
2260 Tmp2 = Node->getOperand(1);
2261 Tmp3 = Node->getOperand(2);
2262 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2263
2264 // If we had to Expand the SetCC operands into a SELECT node, then it may
2265 // not always be possible to return a true LHS & RHS. In this case, just
2266 // return the value we legalized, returned in the LHS
2267 if (Tmp2.Val == 0) {
2268 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002269 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002270 }
Nate Begeman987121a2005-08-23 04:29:48 +00002271
Chris Lattnerf263a232006-01-30 22:43:50 +00002272 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002273 default: assert(0 && "Cannot handle this action for SETCC yet!");
2274 case TargetLowering::Custom:
2275 isCustom = true;
2276 // FALLTHROUGH.
2277 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002279 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002280 Tmp4 = TLI.LowerOperation(Result, DAG);
2281 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002282 }
Nate Begeman987121a2005-08-23 04:29:48 +00002283 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002284 case TargetLowering::Promote: {
2285 // First step, figure out the appropriate operation to use.
2286 // Allow SETCC to not be supported for all legal data types
2287 // Mostly this targets FP
2288 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002289 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002290
2291 // Scan for the appropriate larger type to use.
2292 while (1) {
2293 NewInTy = (MVT::ValueType)(NewInTy+1);
2294
2295 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2296 "Fell off of the edge of the integer world");
2297 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2298 "Fell off of the edge of the floating point world");
2299
2300 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002301 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002302 break;
2303 }
2304 if (MVT::isInteger(NewInTy))
2305 assert(0 && "Cannot promote Legal Integer SETCC yet");
2306 else {
2307 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2308 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2309 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002310 Tmp1 = LegalizeOp(Tmp1);
2311 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002313 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002314 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002315 }
Nate Begeman987121a2005-08-23 04:29:48 +00002316 case TargetLowering::Expand:
2317 // Expand a setcc node into a select_cc of the same condition, lhs, and
2318 // rhs that selects between const 1 (true) and const 0 (false).
2319 MVT::ValueType VT = Node->getValueType(0);
2320 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2321 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002322 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002323 break;
2324 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002325 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002326 case ISD::MEMSET:
2327 case ISD::MEMCPY:
2328 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2331
2332 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2333 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2334 case Expand: assert(0 && "Cannot expand a byte!");
2335 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002336 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002337 break;
2338 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002339 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002340 break;
2341 }
2342 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002343 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002344 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002345
2346 SDOperand Tmp4;
2347 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002348 case Expand: {
2349 // Length is too big, just take the lo-part of the length.
2350 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002351 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002352 break;
2353 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002354 case Legal:
2355 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002356 break;
2357 case Promote:
2358 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002359 break;
2360 }
2361
2362 SDOperand Tmp5;
2363 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2364 case Expand: assert(0 && "Cannot expand this yet!");
2365 case Legal:
2366 Tmp5 = LegalizeOp(Node->getOperand(4));
2367 break;
2368 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002369 Tmp5 = PromoteOp(Node->getOperand(4));
2370 break;
2371 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002372
2373 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2374 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002375 case TargetLowering::Custom:
2376 isCustom = true;
2377 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002378 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002380 if (isCustom) {
2381 Tmp1 = TLI.LowerOperation(Result, DAG);
2382 if (Tmp1.Val) Result = Tmp1;
2383 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002384 break;
2385 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002386 // Otherwise, the target does not support this operation. Lower the
2387 // operation to an explicit libcall as appropriate.
2388 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002389 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002390 TargetLowering::ArgListTy Args;
2391 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002392
Reid Spencer6dced922005-01-12 14:53:45 +00002393 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002394 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002395 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002396 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002397 // Extend the (previously legalized) ubyte argument to be an int value
2398 // for the call.
2399 if (Tmp3.getValueType() > MVT::i32)
2400 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2401 else
2402 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002403 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002404 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002405 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002406 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002407
2408 FnName = "memset";
2409 } else if (Node->getOpcode() == ISD::MEMCPY ||
2410 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002411 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002412 Entry.Node = Tmp2; Args.push_back(Entry);
2413 Entry.Node = Tmp3; Args.push_back(Entry);
2414 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002415 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2416 } else {
2417 assert(0 && "Unknown op!");
2418 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002419
Chris Lattner85d70c62005-01-11 05:57:22 +00002420 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002421 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002422 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002423 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002424 break;
2425 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002426 }
2427 break;
2428 }
Chris Lattner5385db52005-05-09 20:23:03 +00002429
Chris Lattner4157c412005-04-02 04:00:59 +00002430 case ISD::SHL_PARTS:
2431 case ISD::SRA_PARTS:
2432 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002433 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002434 bool Changed = false;
2435 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2436 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2437 Changed |= Ops.back() != Node->getOperand(i);
2438 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002439 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002440 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002441
Evan Cheng870e4f82006-01-09 18:31:59 +00002442 switch (TLI.getOperationAction(Node->getOpcode(),
2443 Node->getValueType(0))) {
2444 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002445 case TargetLowering::Legal: break;
2446 case TargetLowering::Custom:
2447 Tmp1 = TLI.LowerOperation(Result, DAG);
2448 if (Tmp1.Val) {
2449 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002450 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002451 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002452 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2453 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002454 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002455 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002456 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002457 return RetVal;
2458 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002459 break;
2460 }
2461
Chris Lattner13fe99c2005-04-02 05:00:07 +00002462 // Since these produce multiple values, make sure to remember that we
2463 // legalized all of them.
2464 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2465 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2466 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002467 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002468
2469 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002470 case ISD::ADD:
2471 case ISD::SUB:
2472 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002473 case ISD::MULHS:
2474 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002475 case ISD::UDIV:
2476 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002477 case ISD::AND:
2478 case ISD::OR:
2479 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002480 case ISD::SHL:
2481 case ISD::SRL:
2482 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002483 case ISD::FADD:
2484 case ISD::FSUB:
2485 case ISD::FMUL:
2486 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002487 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002488 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2489 case Expand: assert(0 && "Not possible");
2490 case Legal:
2491 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2492 break;
2493 case Promote:
2494 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2495 break;
2496 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002497
2498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002499
Andrew Lenharth72594262005-12-24 23:42:32 +00002500 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002501 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002502 case TargetLowering::Legal: break;
2503 case TargetLowering::Custom:
2504 Tmp1 = TLI.LowerOperation(Result, DAG);
2505 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002506 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002507 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002508 if (Node->getValueType(0) == MVT::i32) {
2509 switch (Node->getOpcode()) {
2510 default: assert(0 && "Do not know how to expand this integer BinOp!");
2511 case ISD::UDIV:
2512 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002513 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2514 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002515 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002516 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002517 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002518 };
2519 break;
2520 }
2521
Chris Lattner87f08092006-04-02 03:57:31 +00002522 assert(MVT::isVector(Node->getValueType(0)) &&
2523 "Cannot expand this binary operator!");
2524 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002525 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002526 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002527 MVT::ValueType PtrVT = TLI.getPointerTy();
2528 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2529 i != e; ++i) {
2530 SDOperand Idx = DAG.getConstant(i, PtrVT);
2531 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2532 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2533 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2534 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002535 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2536 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002537 break;
2538 }
Evan Cheng119266e2006-04-12 21:20:24 +00002539 case TargetLowering::Promote: {
2540 switch (Node->getOpcode()) {
2541 default: assert(0 && "Do not know how to promote this BinOp!");
2542 case ISD::AND:
2543 case ISD::OR:
2544 case ISD::XOR: {
2545 MVT::ValueType OVT = Node->getValueType(0);
2546 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2547 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2548 // Bit convert each of the values to the new type.
2549 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2550 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2551 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2552 // Bit convert the result back the original type.
2553 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2554 break;
2555 }
2556 }
2557 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002558 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002559 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002560
2561 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2562 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2563 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2564 case Expand: assert(0 && "Not possible");
2565 case Legal:
2566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2567 break;
2568 case Promote:
2569 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2570 break;
2571 }
2572
2573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2574
2575 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2576 default: assert(0 && "Operation not supported");
2577 case TargetLowering::Custom:
2578 Tmp1 = TLI.LowerOperation(Result, DAG);
2579 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002580 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002581 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002582 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002583 // If this target supports fabs/fneg natively and select is cheap,
2584 // do this efficiently.
2585 if (!TLI.isSelectExpensive() &&
2586 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2587 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002588 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002589 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002590 // Get the sign bit of the RHS.
2591 MVT::ValueType IVT =
2592 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2593 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2594 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2595 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2596 // Get the absolute value of the result.
2597 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2598 // Select between the nabs and abs value based on the sign bit of
2599 // the input.
2600 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2601 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2602 AbsVal),
2603 AbsVal);
2604 Result = LegalizeOp(Result);
2605 break;
2606 }
2607
2608 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002609 MVT::ValueType NVT =
2610 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2611 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2612 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2613 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002614 break;
2615 }
Evan Cheng003feb02007-01-04 21:56:39 +00002616 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002617 break;
2618
Nate Begeman5965bd12006-02-17 05:43:56 +00002619 case ISD::ADDC:
2620 case ISD::SUBC:
2621 Tmp1 = LegalizeOp(Node->getOperand(0));
2622 Tmp2 = LegalizeOp(Node->getOperand(1));
2623 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2624 // Since this produces two values, make sure to remember that we legalized
2625 // both of them.
2626 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2627 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2628 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002629
Nate Begeman5965bd12006-02-17 05:43:56 +00002630 case ISD::ADDE:
2631 case ISD::SUBE:
2632 Tmp1 = LegalizeOp(Node->getOperand(0));
2633 Tmp2 = LegalizeOp(Node->getOperand(1));
2634 Tmp3 = LegalizeOp(Node->getOperand(2));
2635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2636 // Since this produces two values, make sure to remember that we legalized
2637 // both of them.
2638 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2639 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2640 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002641
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002642 case ISD::BUILD_PAIR: {
2643 MVT::ValueType PairTy = Node->getValueType(0);
2644 // TODO: handle the case where the Lo and Hi operands are not of legal type
2645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2647 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002648 case TargetLowering::Promote:
2649 case TargetLowering::Custom:
2650 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002651 case TargetLowering::Legal:
2652 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2653 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2654 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002655 case TargetLowering::Expand:
2656 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2657 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2658 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2659 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2660 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002661 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002662 break;
2663 }
2664 break;
2665 }
2666
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002667 case ISD::UREM:
2668 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002669 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002670 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2671 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002672
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002673 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002674 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2675 case TargetLowering::Custom:
2676 isCustom = true;
2677 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002678 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002679 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002680 if (isCustom) {
2681 Tmp1 = TLI.LowerOperation(Result, DAG);
2682 if (Tmp1.Val) Result = Tmp1;
2683 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002684 break;
Chris Lattner81914422005-08-03 20:31:37 +00002685 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002686 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002687 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002688 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002689 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2690 TargetLowering::Legal) {
2691 // X % Y -> X-X/Y*Y
2692 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002693 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002694 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2695 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2696 } else {
2697 assert(Node->getValueType(0) == MVT::i32 &&
2698 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002699 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2700 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002701 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002702 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002703 }
Chris Lattner81914422005-08-03 20:31:37 +00002704 } else {
2705 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002706 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2707 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002708 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002709 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2710 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002711 }
2712 break;
2713 }
2714 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002715 case ISD::VAARG: {
2716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2717 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2718
Chris Lattner364b89a2006-01-28 07:42:08 +00002719 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002720 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2721 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002722 case TargetLowering::Custom:
2723 isCustom = true;
2724 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002725 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2727 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002728 Tmp1 = Result.getValue(1);
2729
2730 if (isCustom) {
2731 Tmp2 = TLI.LowerOperation(Result, DAG);
2732 if (Tmp2.Val) {
2733 Result = LegalizeOp(Tmp2);
2734 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2735 }
2736 }
Nate Begemane74795c2006-01-25 18:21:52 +00002737 break;
2738 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002739 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002740 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002741 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002742 // Increment the pointer, VAList, to the next vaarg
2743 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2744 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2745 TLI.getPointerTy()));
2746 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002747 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2748 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002749 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002750 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002751 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002752 Result = LegalizeOp(Result);
2753 break;
2754 }
2755 }
2756 // Since VAARG produces two values, make sure to remember that we
2757 // legalized both of them.
2758 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002759 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2760 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002761 }
2762
2763 case ISD::VACOPY:
2764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2765 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2766 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2767
2768 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2769 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002770 case TargetLowering::Custom:
2771 isCustom = true;
2772 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002773 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002774 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2775 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002776 if (isCustom) {
2777 Tmp1 = TLI.LowerOperation(Result, DAG);
2778 if (Tmp1.Val) Result = Tmp1;
2779 }
Nate Begemane74795c2006-01-25 18:21:52 +00002780 break;
2781 case TargetLowering::Expand:
2782 // This defaults to loading a pointer from the input and storing it to the
2783 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002784 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002785 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002786 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2787 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002788 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2789 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002790 break;
2791 }
2792 break;
2793
2794 case ISD::VAEND:
2795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2797
2798 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2799 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800 case TargetLowering::Custom:
2801 isCustom = true;
2802 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002803 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002804 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002805 if (isCustom) {
2806 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2807 if (Tmp1.Val) Result = Tmp1;
2808 }
Nate Begemane74795c2006-01-25 18:21:52 +00002809 break;
2810 case TargetLowering::Expand:
2811 Result = Tmp1; // Default to a no-op, return the chain
2812 break;
2813 }
2814 break;
2815
2816 case ISD::VASTART:
2817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2818 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2819
Chris Lattnerd02b0542006-01-28 10:58:55 +00002820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2821
Nate Begemane74795c2006-01-25 18:21:52 +00002822 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2823 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002824 case TargetLowering::Legal: break;
2825 case TargetLowering::Custom:
2826 Tmp1 = TLI.LowerOperation(Result, DAG);
2827 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002828 break;
2829 }
2830 break;
2831
Nate Begeman1b8121b2006-01-11 21:21:00 +00002832 case ISD::ROTL:
2833 case ISD::ROTR:
2834 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2835 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002837 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2838 default:
2839 assert(0 && "ROTL/ROTR legalize operation not supported");
2840 break;
2841 case TargetLowering::Legal:
2842 break;
2843 case TargetLowering::Custom:
2844 Tmp1 = TLI.LowerOperation(Result, DAG);
2845 if (Tmp1.Val) Result = Tmp1;
2846 break;
2847 case TargetLowering::Promote:
2848 assert(0 && "Do not know how to promote ROTL/ROTR");
2849 break;
2850 case TargetLowering::Expand:
2851 assert(0 && "Do not know how to expand ROTL/ROTR");
2852 break;
2853 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00002854 break;
2855
Nate Begeman2fba8a32006-01-14 03:14:10 +00002856 case ISD::BSWAP:
2857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2858 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002859 case TargetLowering::Custom:
2860 assert(0 && "Cannot custom legalize this yet!");
2861 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002862 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002863 break;
2864 case TargetLowering::Promote: {
2865 MVT::ValueType OVT = Tmp1.getValueType();
2866 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00002867 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002868
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002869 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2870 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2871 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2872 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2873 break;
2874 }
2875 case TargetLowering::Expand:
2876 Result = ExpandBSWAP(Tmp1);
2877 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002878 }
2879 break;
2880
Andrew Lenharth5e177822005-05-03 17:19:30 +00002881 case ISD::CTPOP:
2882 case ISD::CTTZ:
2883 case ISD::CTLZ:
2884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2885 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00002886 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00002887 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002888 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00002889 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00002890 TargetLowering::Custom) {
2891 Tmp1 = TLI.LowerOperation(Result, DAG);
2892 if (Tmp1.Val) {
2893 Result = Tmp1;
2894 }
Scott Michel34e2d222007-07-30 21:00:31 +00002895 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002896 break;
2897 case TargetLowering::Promote: {
2898 MVT::ValueType OVT = Tmp1.getValueType();
2899 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002900
2901 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002902 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2903 // Perform the larger operation, then subtract if needed.
2904 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002905 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002906 case ISD::CTPOP:
2907 Result = Tmp1;
2908 break;
2909 case ISD::CTTZ:
2910 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002911 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002912 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00002913 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002914 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00002915 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002916 break;
2917 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002918 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002919 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002920 DAG.getConstant(MVT::getSizeInBits(NVT) -
2921 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00002922 break;
2923 }
2924 break;
2925 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002926 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002927 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002928 break;
2929 }
2930 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002931
Chris Lattner13fe99c2005-04-02 05:00:07 +00002932 // Unary operators
2933 case ISD::FABS:
2934 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002935 case ISD::FSQRT:
2936 case ISD::FSIN:
2937 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002938 Tmp1 = LegalizeOp(Node->getOperand(0));
2939 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002940 case TargetLowering::Promote:
2941 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002942 isCustom = true;
2943 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002944 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002945 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002946 if (isCustom) {
2947 Tmp1 = TLI.LowerOperation(Result, DAG);
2948 if (Tmp1.Val) Result = Tmp1;
2949 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002950 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002951 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002952 switch (Node->getOpcode()) {
2953 default: assert(0 && "Unreachable!");
2954 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002955 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2956 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002957 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002958 break;
Chris Lattner80026402005-04-30 04:43:14 +00002959 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002960 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2961 MVT::ValueType VT = Node->getValueType(0);
2962 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002963 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002964 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2965 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002966 break;
2967 }
2968 case ISD::FSQRT:
2969 case ISD::FSIN:
2970 case ISD::FCOS: {
2971 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002972 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002973 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002974 case ISD::FSQRT:
2975 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2976 break;
2977 case ISD::FSIN:
2978 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2979 break;
2980 case ISD::FCOS:
2981 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2982 break;
Chris Lattner80026402005-04-30 04:43:14 +00002983 default: assert(0 && "Unreachable!");
2984 }
Nate Begeman77558da2005-08-04 21:43:28 +00002985 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002986 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2987 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002988 break;
2989 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002990 }
2991 break;
2992 }
2993 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002994 case ISD::FPOWI: {
2995 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002996 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2997 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002998 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002999 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3000 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003001 break;
3002 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003003 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003004 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003005 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003006 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3007 // The input has to be a vector type, we have to either scalarize it, pack
3008 // it, or convert it based on whether the input vector type is legal.
3009 SDNode *InVal = Node->getOperand(0).Val;
3010 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3011 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3012
3013 // Figure out if there is a simple type corresponding to this Vector
3014 // type. If so, convert to the vector type.
3015 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3016 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003017 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003018 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3019 LegalizeOp(Node->getOperand(0)));
3020 break;
3021 } else if (NumElems == 1) {
3022 // Turn this into a bit convert of the scalar input.
3023 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3024 ScalarizeVectorOp(Node->getOperand(0)));
3025 break;
3026 } else {
3027 // FIXME: UNIMP! Store then reload
3028 assert(0 && "Cast from unsupported vector type not implemented yet!");
3029 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003030 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003031 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3032 Node->getOperand(0).getValueType())) {
3033 default: assert(0 && "Unknown operation action!");
3034 case TargetLowering::Expand:
3035 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3036 break;
3037 case TargetLowering::Legal:
3038 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003039 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003040 break;
3041 }
3042 }
3043 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003044
Chris Lattner13fe99c2005-04-02 05:00:07 +00003045 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003046 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003047 case ISD::UINT_TO_FP: {
3048 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003049 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3050 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003051 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003052 Node->getOperand(0).getValueType())) {
3053 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003054 case TargetLowering::Custom:
3055 isCustom = true;
3056 // FALLTHROUGH
3057 case TargetLowering::Legal:
3058 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003060 if (isCustom) {
3061 Tmp1 = TLI.LowerOperation(Result, DAG);
3062 if (Tmp1.Val) Result = Tmp1;
3063 }
3064 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003065 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003066 Result = ExpandLegalINT_TO_FP(isSigned,
3067 LegalizeOp(Node->getOperand(0)),
3068 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003069 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003070 case TargetLowering::Promote:
3071 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3072 Node->getValueType(0),
3073 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003074 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003075 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003076 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003077 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003078 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3079 Node->getValueType(0), Node->getOperand(0));
3080 break;
3081 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003082 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003083 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003084 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3085 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003086 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003087 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3088 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003089 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003090 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3091 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003092 break;
3093 }
3094 break;
3095 }
3096 case ISD::TRUNCATE:
3097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3098 case Legal:
3099 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003100 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003101 break;
3102 case Expand:
3103 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3104
3105 // Since the result is legal, we should just be able to truncate the low
3106 // part of the source.
3107 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3108 break;
3109 case Promote:
3110 Result = PromoteOp(Node->getOperand(0));
3111 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3112 break;
3113 }
3114 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003115
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003116 case ISD::FP_TO_SINT:
3117 case ISD::FP_TO_UINT:
3118 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3119 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003120 Tmp1 = LegalizeOp(Node->getOperand(0));
3121
Chris Lattner44fe26f2005-07-29 00:11:56 +00003122 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3123 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003124 case TargetLowering::Custom:
3125 isCustom = true;
3126 // FALLTHROUGH
3127 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003128 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003129 if (isCustom) {
3130 Tmp1 = TLI.LowerOperation(Result, DAG);
3131 if (Tmp1.Val) Result = Tmp1;
3132 }
3133 break;
3134 case TargetLowering::Promote:
3135 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3136 Node->getOpcode() == ISD::FP_TO_SINT);
3137 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003138 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003139 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3140 SDOperand True, False;
3141 MVT::ValueType VT = Node->getOperand(0).getValueType();
3142 MVT::ValueType NVT = Node->getValueType(0);
3143 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3144 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3145 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3146 Node->getOperand(0), Tmp2, ISD::SETLT);
3147 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3148 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003149 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003150 Tmp2));
3151 False = DAG.getNode(ISD::XOR, NVT, False,
3152 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003153 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3154 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003155 } else {
3156 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3157 }
3158 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003159 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003160 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003161 case Expand: {
3162 // Convert f32 / f64 to i32 / i64.
3163 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00003164 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003165 switch (Node->getOpcode()) {
3166 case ISD::FP_TO_SINT:
3167 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003168 LC = (VT == MVT::i32)
3169 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003170 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003171 LC = (VT == MVT::i32)
3172 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003173 break;
3174 case ISD::FP_TO_UINT:
3175 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003176 LC = (VT == MVT::i32)
3177 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003178 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003179 LC = (VT == MVT::i32)
3180 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003181 break;
3182 default: assert(0 && "Unreachable!");
3183 }
3184 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003185 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3186 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003187 break;
3188 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003189 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003190 Tmp1 = PromoteOp(Node->getOperand(0));
3191 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3192 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003193 break;
3194 }
3195 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003196
Dale Johannesenc339e452007-08-09 17:27:48 +00003197 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003198 case ISD::FP_ROUND: {
3199 MVT::ValueType newVT = Op.getValueType();
3200 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3201 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenc339e452007-08-09 17:27:48 +00003202 // The only way we can lower this is to turn it into a STORE,
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003203 // LOAD pair, targetting a temporary location (a stack slot).
3204
3205 // NOTE: there is a choice here between constantly creating new stack
3206 // slots and always reusing the same one. We currently always create
3207 // new ones, as reuse may inhibit scheduling.
Dale Johannesenc339e452007-08-09 17:27:48 +00003208 MVT::ValueType slotVT =
3209 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3210 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003211 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3212 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3213 MachineFunction &MF = DAG.getMachineFunction();
3214 int SSFI =
3215 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3216 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenc339e452007-08-09 17:27:48 +00003217 if (Node->getOpcode() == ISD::FP_EXTEND) {
3218 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3219 StackSlot, NULL, 0);
3220 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3221 Result, StackSlot, NULL, 0, oldVT);
3222 } else {
3223 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3224 StackSlot, NULL, 0, newVT);
3225 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3226 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003227 break;
3228 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003229 }
3230 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003231 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003232 case ISD::ZERO_EXTEND:
3233 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003234 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003235 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003236 case Legal:
3237 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003238 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003239 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003240 case Promote:
3241 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003242 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003243 Tmp1 = PromoteOp(Node->getOperand(0));
3244 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003245 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003246 case ISD::ZERO_EXTEND:
3247 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003248 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003249 Result = DAG.getZeroExtendInReg(Result,
3250 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003251 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003252 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003253 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003254 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003255 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003256 Result,
3257 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003258 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003259 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003260 Result = PromoteOp(Node->getOperand(0));
3261 if (Result.getValueType() != Op.getValueType())
3262 // Dynamically dead while we have only 2 FP types.
3263 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3264 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003265 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003266 Result = PromoteOp(Node->getOperand(0));
3267 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3268 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003269 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003270 }
3271 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003272 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003273 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003274 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003275 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003276
3277 // If this operation is not supported, convert it to a shl/shr or load/store
3278 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003279 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3280 default: assert(0 && "This action not supported for this op yet!");
3281 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003282 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003283 break;
3284 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003285 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003286 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003287 // NOTE: we could fall back on load/store here too for targets without
3288 // SAR. However, it is doubtful that any exist.
3289 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3290 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003291 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003292 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3293 Node->getOperand(0), ShiftCst);
3294 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3295 Result, ShiftCst);
3296 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003297 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003298 // EXTLOAD pair, targetting a temporary location (a stack slot).
3299
3300 // NOTE: there is a choice here between constantly creating new stack
3301 // slots and always reusing the same one. We currently always create
3302 // new ones, as reuse may inhibit scheduling.
3303 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003304 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003305 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003306 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003307 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003308 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003309 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003310 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3311 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003312 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003313 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003314 } else {
3315 assert(0 && "Unknown op");
3316 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003317 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003318 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003319 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003320 }
Duncan Sands644f9172007-07-27 12:58:54 +00003321 case ISD::ADJUST_TRAMP: {
3322 Tmp1 = LegalizeOp(Node->getOperand(0));
3323 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3324 default: assert(0 && "This action is not supported yet!");
3325 case TargetLowering::Custom:
3326 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3327 Result = TLI.LowerOperation(Result, DAG);
3328 if (Result.Val) break;
3329 // FALL THROUGH
3330 case TargetLowering::Expand:
3331 Result = Tmp1;
3332 break;
3333 }
3334 break;
3335 }
3336 case ISD::TRAMPOLINE: {
3337 SDOperand Ops[6];
3338 for (unsigned i = 0; i != 6; ++i)
3339 Ops[i] = LegalizeOp(Node->getOperand(i));
3340 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3341 // The only option for this node is to custom lower it.
3342 Result = TLI.LowerOperation(Result, DAG);
3343 assert(Result.Val && "Should always custom lower!");
3344 break;
3345 }
Chris Lattner99222f72005-01-15 07:15:18 +00003346 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003347
Chris Lattner101ea662006-04-08 04:13:17 +00003348 assert(Result.getValueType() == Op.getValueType() &&
3349 "Bad legalization!");
3350
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003351 // Make sure that the generated code is itself legal.
3352 if (Result != Op)
3353 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003354
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003355 // Note that LegalizeOp may be reentered even from single-use nodes, which
3356 // means that we always must cache transformed nodes.
3357 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003358 return Result;
3359}
3360
Chris Lattner4d978642005-01-15 22:16:26 +00003361/// PromoteOp - Given an operation that produces a value in an invalid type,
3362/// promote it to compute the value into a larger type. The produced value will
3363/// have the correct bits for the low portion of the register, but no guarantee
3364/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003365SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3366 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003367 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003368 assert(getTypeAction(VT) == Promote &&
3369 "Caller should expand or legalize operands that are not promotable!");
3370 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3371 "Cannot promote to smaller type!");
3372
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003373 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003374 SDOperand Result;
3375 SDNode *Node = Op.Val;
3376
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003377 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003378 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003379
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003380 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003381 case ISD::CopyFromReg:
3382 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003383 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003384#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003385 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003386#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003387 assert(0 && "Do not know how to promote this operator!");
3388 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003389 case ISD::UNDEF:
3390 Result = DAG.getNode(ISD::UNDEF, NVT);
3391 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003392 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003393 if (VT != MVT::i1)
3394 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3395 else
3396 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003397 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3398 break;
3399 case ISD::ConstantFP:
3400 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3401 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3402 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003403
Chris Lattner2cb338d2005-01-18 02:59:52 +00003404 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003405 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003406 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3407 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003408 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003409
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003410 case ISD::TRUNCATE:
3411 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3412 case Legal:
3413 Result = LegalizeOp(Node->getOperand(0));
3414 assert(Result.getValueType() >= NVT &&
3415 "This truncation doesn't make sense!");
3416 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3417 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3418 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003419 case Promote:
3420 // The truncation is not required, because we don't guarantee anything
3421 // about high bits anyway.
3422 Result = PromoteOp(Node->getOperand(0));
3423 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003424 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003425 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3426 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003427 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003428 }
3429 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003430 case ISD::SIGN_EXTEND:
3431 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003432 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3434 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3435 case Legal:
3436 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003437 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003438 break;
3439 case Promote:
3440 // Promote the reg if it's smaller.
3441 Result = PromoteOp(Node->getOperand(0));
3442 // The high bits are not guaranteed to be anything. Insert an extend.
3443 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003444 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003445 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003446 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003447 Result = DAG.getZeroExtendInReg(Result,
3448 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003449 break;
3450 }
3451 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003452 case ISD::BIT_CONVERT:
3453 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3454 Result = PromoteOp(Result);
3455 break;
3456
Chris Lattner4d978642005-01-15 22:16:26 +00003457 case ISD::FP_EXTEND:
3458 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3459 case ISD::FP_ROUND:
3460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3461 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3462 case Promote: assert(0 && "Unreachable with 2 FP types!");
3463 case Legal:
3464 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003465 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003466 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003467 break;
3468 }
3469 break;
3470
3471 case ISD::SINT_TO_FP:
3472 case ISD::UINT_TO_FP:
3473 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3474 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003475 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003476 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003477 break;
3478
3479 case Promote:
3480 Result = PromoteOp(Node->getOperand(0));
3481 if (Node->getOpcode() == ISD::SINT_TO_FP)
3482 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003483 Result,
3484 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003485 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003486 Result = DAG.getZeroExtendInReg(Result,
3487 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003488 // No extra round required here.
3489 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003490 break;
3491 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003492 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3493 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003494 // Round if we cannot tolerate excess precision.
3495 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003496 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3497 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003498 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003499 }
Chris Lattner4d978642005-01-15 22:16:26 +00003500 break;
3501
Chris Lattner268d4572005-12-09 17:32:47 +00003502 case ISD::SIGN_EXTEND_INREG:
3503 Result = PromoteOp(Node->getOperand(0));
3504 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3505 Node->getOperand(1));
3506 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003507 case ISD::FP_TO_SINT:
3508 case ISD::FP_TO_UINT:
3509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3510 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003511 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003512 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003513 break;
3514 case Promote:
3515 // The input result is prerounded, so we don't have to do anything
3516 // special.
3517 Tmp1 = PromoteOp(Node->getOperand(0));
3518 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003519 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003520 // If we're promoting a UINT to a larger size, check to see if the new node
3521 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3522 // we can use that instead. This allows us to generate better code for
3523 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3524 // legal, such as PowerPC.
3525 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003526 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003527 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3528 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003529 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3530 } else {
3531 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3532 }
Chris Lattner4d978642005-01-15 22:16:26 +00003533 break;
3534
Chris Lattner13fe99c2005-04-02 05:00:07 +00003535 case ISD::FABS:
3536 case ISD::FNEG:
3537 Tmp1 = PromoteOp(Node->getOperand(0));
3538 assert(Tmp1.getValueType() == NVT);
3539 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3540 // NOTE: we do not have to do any extra rounding here for
3541 // NoExcessFPPrecision, because we know the input will have the appropriate
3542 // precision, and these operations don't modify precision at all.
3543 break;
3544
Chris Lattner9d6fa982005-04-28 21:44:33 +00003545 case ISD::FSQRT:
3546 case ISD::FSIN:
3547 case ISD::FCOS:
3548 Tmp1 = PromoteOp(Node->getOperand(0));
3549 assert(Tmp1.getValueType() == NVT);
3550 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003551 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003552 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3553 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003554 break;
3555
Chris Lattnerca401aa2007-03-03 23:43:21 +00003556 case ISD::FPOWI: {
3557 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3558 // directly as well, which may be better.
3559 Tmp1 = PromoteOp(Node->getOperand(0));
3560 assert(Tmp1.getValueType() == NVT);
3561 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3562 if (NoExcessFPPrecision)
3563 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3564 DAG.getValueType(VT));
3565 break;
3566 }
3567
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003568 case ISD::AND:
3569 case ISD::OR:
3570 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003571 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003572 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003573 case ISD::MUL:
3574 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003575 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003576 // that too is okay if they are integer operations.
3577 Tmp1 = PromoteOp(Node->getOperand(0));
3578 Tmp2 = PromoteOp(Node->getOperand(1));
3579 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3580 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003581 break;
3582 case ISD::FADD:
3583 case ISD::FSUB:
3584 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003585 Tmp1 = PromoteOp(Node->getOperand(0));
3586 Tmp2 = PromoteOp(Node->getOperand(1));
3587 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3588 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3589
3590 // Floating point operations will give excess precision that we may not be
3591 // able to tolerate. If we DO allow excess precision, just leave it,
3592 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003593 // FIXME: Why would we need to round FP ops more than integer ones?
3594 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003595 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003596 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3597 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003598 break;
3599
Chris Lattner4d978642005-01-15 22:16:26 +00003600 case ISD::SDIV:
3601 case ISD::SREM:
3602 // These operators require that their input be sign extended.
3603 Tmp1 = PromoteOp(Node->getOperand(0));
3604 Tmp2 = PromoteOp(Node->getOperand(1));
3605 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003606 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3607 DAG.getValueType(VT));
3608 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3609 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003610 }
3611 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3612
3613 // Perform FP_ROUND: this is probably overly pessimistic.
3614 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003615 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3616 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003617 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003618 case ISD::FDIV:
3619 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003620 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003621 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3623 case Legal:
3624 Tmp1 = LegalizeOp(Node->getOperand(0));
3625 break;
3626 case Promote:
3627 Tmp1 = PromoteOp(Node->getOperand(0));
3628 break;
3629 case Expand:
3630 assert(0 && "not implemented");
3631 }
3632 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3633 case Legal:
3634 Tmp2 = LegalizeOp(Node->getOperand(1));
3635 break;
3636 case Promote:
3637 Tmp2 = PromoteOp(Node->getOperand(1));
3638 break;
3639 case Expand:
3640 assert(0 && "not implemented");
3641 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003642 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3643
3644 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003645 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003646 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3647 DAG.getValueType(VT));
3648 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003649
3650 case ISD::UDIV:
3651 case ISD::UREM:
3652 // These operators require that their input be zero extended.
3653 Tmp1 = PromoteOp(Node->getOperand(0));
3654 Tmp2 = PromoteOp(Node->getOperand(1));
3655 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003656 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3657 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003658 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3659 break;
3660
3661 case ISD::SHL:
3662 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003663 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003664 break;
3665 case ISD::SRA:
3666 // The input value must be properly sign extended.
3667 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003668 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3669 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003670 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003671 break;
3672 case ISD::SRL:
3673 // The input value must be properly zero extended.
3674 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003675 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003676 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003677 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003678
3679 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003680 Tmp1 = Node->getOperand(0); // Get the chain.
3681 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003682 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3683 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3684 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3685 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003686 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003687 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003688 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003689 // Increment the pointer, VAList, to the next vaarg
3690 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3691 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3692 TLI.getPointerTy()));
3693 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003694 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3695 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003696 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003697 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003698 }
3699 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003700 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003701 break;
3702
Evan Chenge71fe34d2006-10-09 20:57:25 +00003703 case ISD::LOAD: {
3704 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003705 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3706 ? ISD::EXTLOAD : LD->getExtensionType();
3707 Result = DAG.getExtLoad(ExtType, NVT,
3708 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003709 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00003710 LD->getLoadedVT(),
3711 LD->isVolatile(),
3712 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003713 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003714 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003715 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003716 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003717 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003718 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3719 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003720 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003721 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003722 case ISD::SELECT_CC:
3723 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3724 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3725 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003726 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003727 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003728 case ISD::BSWAP:
3729 Tmp1 = Node->getOperand(0);
3730 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3731 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3732 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003733 DAG.getConstant(MVT::getSizeInBits(NVT) -
3734 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003735 TLI.getShiftAmountTy()));
3736 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003737 case ISD::CTPOP:
3738 case ISD::CTTZ:
3739 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003740 // Zero extend the argument
3741 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003742 // Perform the larger operation, then subtract if needed.
3743 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003744 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003745 case ISD::CTPOP:
3746 Result = Tmp1;
3747 break;
3748 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003749 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003750 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003751 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3752 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003753 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003754 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003755 break;
3756 case ISD::CTLZ:
3757 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003758 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003759 DAG.getConstant(MVT::getSizeInBits(NVT) -
3760 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003761 break;
3762 }
3763 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003764 case ISD::EXTRACT_SUBVECTOR:
3765 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003766 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003767 case ISD::EXTRACT_VECTOR_ELT:
3768 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3769 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003770 }
3771
3772 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003773
3774 // Make sure the result is itself legal.
3775 Result = LegalizeOp(Result);
3776
3777 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003778 AddPromotedOperand(Op, Result);
3779 return Result;
3780}
Chris Lattnerdc750592005-01-07 07:47:09 +00003781
Dan Gohmana8665142007-06-25 16:23:39 +00003782/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3783/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3784/// based on the vector type. The return type of this matches the element type
3785/// of the vector, which may not be legal for the target.
3786SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003787 // We know that operand #0 is the Vec vector. If the index is a constant
3788 // or if the invec is a supported hardware type, we can use it. Otherwise,
3789 // lower to a store then an indexed load.
3790 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003791 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003792
3793 SDNode *InVal = Vec.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00003794 MVT::ValueType TVT = InVal->getValueType(0);
3795 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003796
Dan Gohmana8665142007-06-25 16:23:39 +00003797 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3798 default: assert(0 && "This action is not supported yet!");
3799 case TargetLowering::Custom: {
3800 Vec = LegalizeOp(Vec);
3801 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3802 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3803 if (Tmp3.Val)
3804 return Tmp3;
3805 break;
3806 }
3807 case TargetLowering::Legal:
3808 if (isTypeLegal(TVT)) {
3809 Vec = LegalizeOp(Vec);
3810 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00003811 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00003812 }
3813 break;
3814 case TargetLowering::Expand:
3815 break;
3816 }
3817
3818 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00003819 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003820 Op = ScalarizeVectorOp(Vec);
3821 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3822 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00003823 SDOperand Lo, Hi;
3824 SplitVectorOp(Vec, Lo, Hi);
3825 if (CIdx->getValue() < NumElems/2) {
3826 Vec = Lo;
3827 } else {
3828 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00003829 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3830 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00003831 }
Dan Gohmana8665142007-06-25 16:23:39 +00003832
Chris Lattner6f423252006-03-31 17:55:51 +00003833 // It's now an extract from the appropriate high or low part. Recurse.
3834 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003835 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00003836 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00003837 // Store the value to a temporary stack slot, then LOAD the scalar
3838 // element back out.
3839 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3840 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3841
3842 // Add the offset to the index.
3843 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3844 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3845 DAG.getConstant(EltSize, Idx.getValueType()));
3846 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3847
3848 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00003849 }
Dan Gohmana8665142007-06-25 16:23:39 +00003850 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00003851}
3852
Dan Gohmana8665142007-06-25 16:23:39 +00003853/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00003854/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00003855SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00003856 // We know that operand #0 is the Vec vector. For now we assume the index
3857 // is a constant and that the extracted result is a supported hardware type.
3858 SDOperand Vec = Op.getOperand(0);
3859 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3860
Dan Gohmana8665142007-06-25 16:23:39 +00003861 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00003862
3863 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3864 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003865 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00003866 }
3867
3868 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3869 SDOperand Lo, Hi;
3870 SplitVectorOp(Vec, Lo, Hi);
3871 if (CIdx->getValue() < NumElems/2) {
3872 Vec = Lo;
3873 } else {
3874 Vec = Hi;
3875 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3876 }
3877
3878 // It's now an extract from the appropriate high or low part. Recurse.
3879 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003880 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00003881}
3882
Nate Begeman7e7f4392006-02-01 07:19:44 +00003883/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3884/// with condition CC on the current target. This usually involves legalizing
3885/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3886/// there may be no choice but to create a new SetCC node to represent the
3887/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3888/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3889void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3890 SDOperand &RHS,
3891 SDOperand &CC) {
3892 SDOperand Tmp1, Tmp2, Result;
3893
3894 switch (getTypeAction(LHS.getValueType())) {
3895 case Legal:
3896 Tmp1 = LegalizeOp(LHS); // LHS
3897 Tmp2 = LegalizeOp(RHS); // RHS
3898 break;
3899 case Promote:
3900 Tmp1 = PromoteOp(LHS); // LHS
3901 Tmp2 = PromoteOp(RHS); // RHS
3902
3903 // If this is an FP compare, the operands have already been extended.
3904 if (MVT::isInteger(LHS.getValueType())) {
3905 MVT::ValueType VT = LHS.getValueType();
3906 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3907
3908 // Otherwise, we have to insert explicit sign or zero extends. Note
3909 // that we could insert sign extends for ALL conditions, but zero extend
3910 // is cheaper on many machines (an AND instead of two shifts), so prefer
3911 // it.
3912 switch (cast<CondCodeSDNode>(CC)->get()) {
3913 default: assert(0 && "Unknown integer comparison!");
3914 case ISD::SETEQ:
3915 case ISD::SETNE:
3916 case ISD::SETUGE:
3917 case ISD::SETUGT:
3918 case ISD::SETULE:
3919 case ISD::SETULT:
3920 // ALL of these operations will work if we either sign or zero extend
3921 // the operands (including the unsigned comparisons!). Zero extend is
3922 // usually a simpler/cheaper operation, so prefer it.
3923 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3924 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3925 break;
3926 case ISD::SETGE:
3927 case ISD::SETGT:
3928 case ISD::SETLT:
3929 case ISD::SETLE:
3930 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3931 DAG.getValueType(VT));
3932 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3933 DAG.getValueType(VT));
3934 break;
3935 }
3936 }
3937 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003938 case Expand: {
3939 MVT::ValueType VT = LHS.getValueType();
3940 if (VT == MVT::f32 || VT == MVT::f64) {
3941 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003942 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003943 switch (cast<CondCodeSDNode>(CC)->get()) {
3944 case ISD::SETEQ:
3945 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003946 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003947 break;
3948 case ISD::SETNE:
3949 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003950 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003951 break;
3952 case ISD::SETGE:
3953 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003954 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003955 break;
3956 case ISD::SETLT:
3957 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003958 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003959 break;
3960 case ISD::SETLE:
3961 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003962 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003963 break;
3964 case ISD::SETGT:
3965 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003966 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003967 break;
3968 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003969 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3970 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003971 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003972 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003973 break;
3974 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003975 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003976 switch (cast<CondCodeSDNode>(CC)->get()) {
3977 case ISD::SETONE:
3978 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003979 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003980 // Fallthrough
3981 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003982 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003983 break;
3984 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003985 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003986 break;
3987 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003988 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003989 break;
3990 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003991 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003992 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003993 case ISD::SETUEQ:
3994 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003995 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003996 default: assert(0 && "Unsupported FP setcc!");
3997 }
3998 }
3999
4000 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004001 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004002 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004003 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004004 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004005 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004006 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004007 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004008 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004009 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004010 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004011 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004012 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004013 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4014 Tmp2 = SDOperand();
4015 }
4016 LHS = Tmp1;
4017 RHS = Tmp2;
4018 return;
4019 }
4020
Nate Begeman7e7f4392006-02-01 07:19:44 +00004021 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4022 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004023 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00004024 switch (cast<CondCodeSDNode>(CC)->get()) {
4025 case ISD::SETEQ:
4026 case ISD::SETNE:
4027 if (RHSLo == RHSHi)
4028 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4029 if (RHSCST->isAllOnesValue()) {
4030 // Comparison to -1.
4031 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4032 Tmp2 = RHSLo;
4033 break;
4034 }
4035
4036 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4037 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4038 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4039 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4040 break;
4041 default:
4042 // If this is a comparison of the sign bit, just look at the top part.
4043 // X > -1, x < 0
4044 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4045 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4046 CST->getValue() == 0) || // X < 0
4047 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4048 CST->isAllOnesValue())) { // X > -1
4049 Tmp1 = LHSHi;
4050 Tmp2 = RHSHi;
4051 break;
4052 }
4053
4054 // FIXME: This generated code sucks.
4055 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004056 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4057 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004058 default: assert(0 && "Unknown integer setcc!");
4059 case ISD::SETLT:
4060 case ISD::SETULT: LowCC = ISD::SETULT; break;
4061 case ISD::SETGT:
4062 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4063 case ISD::SETLE:
4064 case ISD::SETULE: LowCC = ISD::SETULE; break;
4065 case ISD::SETGE:
4066 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4067 }
4068
4069 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4070 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4071 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4072
4073 // NOTE: on targets without efficient SELECT of bools, we can always use
4074 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004075 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4076 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4077 false, DagCombineInfo);
4078 if (!Tmp1.Val)
4079 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4080 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4081 CCCode, false, DagCombineInfo);
4082 if (!Tmp2.Val)
4083 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4084
4085 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4086 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4087 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4088 (Tmp2C && Tmp2C->getValue() == 0 &&
4089 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4090 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4091 (Tmp2C && Tmp2C->getValue() == 1 &&
4092 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4093 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4094 // low part is known false, returns high part.
4095 // For LE / GE, if high part is known false, ignore the low part.
4096 // For LT / GT, if high part is known true, ignore the low part.
4097 Tmp1 = Tmp2;
4098 Tmp2 = SDOperand();
4099 } else {
4100 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4101 ISD::SETEQ, false, DagCombineInfo);
4102 if (!Result.Val)
4103 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4104 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4105 Result, Tmp1, Tmp2));
4106 Tmp1 = Result;
4107 Tmp2 = SDOperand();
4108 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004109 }
4110 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004111 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004112 LHS = Tmp1;
4113 RHS = Tmp2;
4114}
4115
Chris Lattner36e663d2005-12-23 00:16:34 +00004116/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004117/// The resultant code need not be legal. Note that SrcOp is the input operand
4118/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004119SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4120 SDOperand SrcOp) {
4121 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004122 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004123
4124 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004125 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004126 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004127 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004128}
4129
Chris Lattner6be79822006-04-04 17:23:26 +00004130SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4131 // Create a vector sized/aligned stack slot, store the value to element #0,
4132 // then load the whole vector back out.
4133 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004134 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004135 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004136 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004137}
4138
4139
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004140/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004141/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004142SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4143
4144 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004145 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004146 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004147 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004148 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004149 std::map<SDOperand, std::vector<unsigned> > Values;
4150 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004151 bool isConstant = true;
4152 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4153 SplatValue.getOpcode() != ISD::UNDEF)
4154 isConstant = false;
4155
Evan Cheng1d2e9952006-03-24 01:17:21 +00004156 for (unsigned i = 1; i < NumElems; ++i) {
4157 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004158 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004159 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004160 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004161 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004162 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004163
4164 // If this isn't a constant element or an undef, we can't use a constant
4165 // pool load.
4166 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4167 V.getOpcode() != ISD::UNDEF)
4168 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004169 }
4170
4171 if (isOnlyLowElement) {
4172 // If the low element is an undef too, then this whole things is an undef.
4173 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4174 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4175 // Otherwise, turn this into a scalar_to_vector node.
4176 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4177 Node->getOperand(0));
4178 }
4179
Chris Lattner77e271c2006-03-24 07:29:17 +00004180 // If all elements are constants, create a load from the constant pool.
4181 if (isConstant) {
4182 MVT::ValueType VT = Node->getValueType(0);
4183 const Type *OpNTy =
4184 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4185 std::vector<Constant*> CV;
4186 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4187 if (ConstantFPSDNode *V =
4188 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4189 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4190 } else if (ConstantSDNode *V =
4191 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004192 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004193 } else {
4194 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4195 CV.push_back(UndefValue::get(OpNTy));
4196 }
4197 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004198 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004199 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004200 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004201 }
4202
Chris Lattner21e68c82006-03-20 01:52:29 +00004203 if (SplatValue.Val) { // Splat of one value?
4204 // Build the shuffle constant vector: <0, 0, 0, 0>
4205 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004206 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004207 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004208 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004209 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4210 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004211
4212 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004213 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004214 // Get the splatted value into the low element of a vector register.
4215 SDOperand LowValVec =
4216 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4217
4218 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4219 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4220 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4221 SplatMask);
4222 }
4223 }
4224
Evan Cheng1d2e9952006-03-24 01:17:21 +00004225 // If there are only two unique elements, we may be able to turn this into a
4226 // vector shuffle.
4227 if (Values.size() == 2) {
4228 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4229 MVT::ValueType MaskVT =
4230 MVT::getIntVectorWithNumElements(NumElems);
4231 std::vector<SDOperand> MaskVec(NumElems);
4232 unsigned i = 0;
4233 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4234 E = Values.end(); I != E; ++I) {
4235 for (std::vector<unsigned>::iterator II = I->second.begin(),
4236 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004237 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004238 i += NumElems;
4239 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004240 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4241 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004242
4243 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004244 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4245 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004246 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004247 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4248 E = Values.end(); I != E; ++I) {
4249 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4250 I->first);
4251 Ops.push_back(Op);
4252 }
4253 Ops.push_back(ShuffleMask);
4254
4255 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004256 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4257 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004258 }
4259 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004260
4261 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4262 // aligned object on the stack, store each element into it, then load
4263 // the result as a vector.
4264 MVT::ValueType VT = Node->getValueType(0);
4265 // Create the stack frame object.
4266 SDOperand FIPtr = CreateStackTemporary(VT);
4267
4268 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004269 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004270 unsigned TypeByteSize =
4271 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004272 // Store (in the right endianness) the elements to memory.
4273 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4274 // Ignore undef elements.
4275 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4276
Chris Lattner8fa445a2006-03-22 01:46:54 +00004277 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004278
4279 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4280 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4281
Evan Chengdf9ac472006-10-05 23:01:46 +00004282 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004283 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004284 }
4285
4286 SDOperand StoreChain;
4287 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004288 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4289 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004290 else
4291 StoreChain = DAG.getEntryNode();
4292
4293 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004294 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004295}
4296
4297/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4298/// specified value type.
4299SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4300 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4301 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004302 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004303 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004304 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004305 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4306}
4307
Chris Lattner4157c412005-04-02 04:00:59 +00004308void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4309 SDOperand Op, SDOperand Amt,
4310 SDOperand &Lo, SDOperand &Hi) {
4311 // Expand the subcomponents.
4312 SDOperand LHSL, LHSH;
4313 ExpandOp(Op, LHSL, LHSH);
4314
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004315 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004316 MVT::ValueType VT = LHSL.getValueType();
4317 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004318 Hi = Lo.getValue(1);
4319}
4320
4321
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004322/// ExpandShift - Try to find a clever way to expand this shift operation out to
4323/// smaller elements. If we can't find a way that is more efficient than a
4324/// libcall on this target, return false. Otherwise, return true with the
4325/// low-parts expanded into Lo and Hi.
4326bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4327 SDOperand &Lo, SDOperand &Hi) {
4328 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4329 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004330
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004331 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004332 SDOperand ShAmt = LegalizeOp(Amt);
4333 MVT::ValueType ShTy = ShAmt.getValueType();
4334 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4335 unsigned NVTBits = MVT::getSizeInBits(NVT);
4336
4337 // Handle the case when Amt is an immediate. Other cases are currently broken
4338 // and are disabled.
4339 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4340 unsigned Cst = CN->getValue();
4341 // Expand the incoming operand to be shifted, so that we have its parts
4342 SDOperand InL, InH;
4343 ExpandOp(Op, InL, InH);
4344 switch(Opc) {
4345 case ISD::SHL:
4346 if (Cst > VTBits) {
4347 Lo = DAG.getConstant(0, NVT);
4348 Hi = DAG.getConstant(0, NVT);
4349 } else if (Cst > NVTBits) {
4350 Lo = DAG.getConstant(0, NVT);
4351 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004352 } else if (Cst == NVTBits) {
4353 Lo = DAG.getConstant(0, NVT);
4354 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004355 } else {
4356 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4357 Hi = DAG.getNode(ISD::OR, NVT,
4358 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4359 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4360 }
4361 return true;
4362 case ISD::SRL:
4363 if (Cst > VTBits) {
4364 Lo = DAG.getConstant(0, NVT);
4365 Hi = DAG.getConstant(0, NVT);
4366 } else if (Cst > NVTBits) {
4367 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4368 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004369 } else if (Cst == NVTBits) {
4370 Lo = InH;
4371 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004372 } else {
4373 Lo = DAG.getNode(ISD::OR, NVT,
4374 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4375 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4376 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4377 }
4378 return true;
4379 case ISD::SRA:
4380 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004381 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004382 DAG.getConstant(NVTBits-1, ShTy));
4383 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004384 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004385 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004386 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004387 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004388 } else if (Cst == NVTBits) {
4389 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004390 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004391 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004392 } else {
4393 Lo = DAG.getNode(ISD::OR, NVT,
4394 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4395 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4396 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4397 }
4398 return true;
4399 }
4400 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004401
4402 // Okay, the shift amount isn't constant. However, if we can tell that it is
4403 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4404 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004405 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004406
4407 // If we know that the high bit of the shift amount is one, then we can do
4408 // this as a couple of simple shifts.
4409 if (KnownOne & Mask) {
4410 // Mask out the high bit, which we know is set.
4411 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4412 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4413
4414 // Expand the incoming operand to be shifted, so that we have its parts
4415 SDOperand InL, InH;
4416 ExpandOp(Op, InL, InH);
4417 switch(Opc) {
4418 case ISD::SHL:
4419 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4420 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4421 return true;
4422 case ISD::SRL:
4423 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4424 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4425 return true;
4426 case ISD::SRA:
4427 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4428 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4429 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4430 return true;
4431 }
4432 }
4433
4434 // If we know that the high bit of the shift amount is zero, then we can do
4435 // this as a couple of simple shifts.
4436 if (KnownZero & Mask) {
4437 // Compute 32-amt.
4438 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4439 DAG.getConstant(NVTBits, Amt.getValueType()),
4440 Amt);
4441
4442 // Expand the incoming operand to be shifted, so that we have its parts
4443 SDOperand InL, InH;
4444 ExpandOp(Op, InL, InH);
4445 switch(Opc) {
4446 case ISD::SHL:
4447 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4448 Hi = DAG.getNode(ISD::OR, NVT,
4449 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4450 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4451 return true;
4452 case ISD::SRL:
4453 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4454 Lo = DAG.getNode(ISD::OR, NVT,
4455 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4456 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4457 return true;
4458 case ISD::SRA:
4459 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4460 Lo = DAG.getNode(ISD::OR, NVT,
4461 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4462 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4463 return true;
4464 }
4465 }
4466
Nate Begemanb0674922005-04-06 21:13:14 +00004467 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004468}
Chris Lattneraac464e2005-01-21 06:05:23 +00004469
Chris Lattner4add7e32005-01-23 04:42:50 +00004470
Chris Lattneraac464e2005-01-21 06:05:23 +00004471// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4472// does not fit into a register, return the lo part and set the hi part to the
4473// by-reg argument. If it does fit into a single register, return the result
4474// and leave the Hi part unset.
4475SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004476 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004477 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4478 // The input chain to this libcall is the entry node of the function.
4479 // Legalizing the call will automatically add the previous call to the
4480 // dependence.
4481 SDOperand InChain = DAG.getEntryNode();
4482
Chris Lattneraac464e2005-01-21 06:05:23 +00004483 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004484 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004485 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4486 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4487 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004488 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004489 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004490 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004491 }
4492 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004493
Chris Lattner06bbeb62005-05-11 19:02:11 +00004494 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004495 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004496 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004497 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004498 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004499
Chris Lattner462505f2006-02-13 09:18:02 +00004500 // Legalize the call sequence, starting with the chain. This will advance
4501 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4502 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4503 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004504 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004505 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004506 default: assert(0 && "Unknown thing");
4507 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004508 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004509 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004510 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004511 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004512 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004513 }
Chris Lattner63022662005-09-02 20:26:58 +00004514 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004515}
4516
Chris Lattner4add7e32005-01-23 04:42:50 +00004517
Evan Chengbf535fc2007-04-27 07:33:31 +00004518/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4519///
Chris Lattneraac464e2005-01-21 06:05:23 +00004520SDOperand SelectionDAGLegalize::
4521ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004522 assert(getTypeAction(Source.getValueType()) == Expand &&
4523 "This is not an expansion!");
4524 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4525
Chris Lattner06bbeb62005-05-11 19:02:11 +00004526 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004527 assert(Source.getValueType() == MVT::i64 &&
4528 "This only works for 64-bit -> FP");
4529 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4530 // incoming integer is set. To handle this, we dynamically test to see if
4531 // it is set, and, if so, add a fudge factor.
4532 SDOperand Lo, Hi;
4533 ExpandOp(Source, Lo, Hi);
4534
Chris Lattner2a4f7312005-05-13 04:45:13 +00004535 // If this is unsigned, and not supported, first perform the conversion to
4536 // signed, then adjust the result if the sign bit is set.
4537 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4538 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4539
Chris Lattnerd47675e2005-08-09 20:20:18 +00004540 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4541 DAG.getConstant(0, Hi.getValueType()),
4542 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004543 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4544 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4545 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004546 uint64_t FF = 0x5f800000ULL;
4547 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004548 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004549
Chris Lattnerc30405e2005-08-26 17:15:30 +00004550 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004551 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4552 SDOperand FudgeInReg;
4553 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004554 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004555 else {
4556 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Chengbf535fc2007-04-27 07:33:31 +00004557 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004558 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004559 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004560 }
Evan Chengbf535fc2007-04-27 07:33:31 +00004561 MVT::ValueType SCVT = SignedConv.getValueType();
4562 if (SCVT != DestTy) {
4563 // Destination type needs to be expanded as well. The FADD now we are
4564 // constructing will be expanded into a libcall.
4565 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4566 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4567 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4568 SignedConv, SignedConv.getValue(1));
4569 }
4570 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4571 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004572 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004573 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004574
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004575 // Check to see if the target has a custom way to lower this. If so, use it.
4576 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4577 default: assert(0 && "This action not implemented for this operation!");
4578 case TargetLowering::Legal:
4579 case TargetLowering::Expand:
4580 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004581 case TargetLowering::Custom: {
4582 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4583 Source), DAG);
4584 if (NV.Val)
4585 return LegalizeOp(NV);
4586 break; // The target decided this was legal after all
4587 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004588 }
4589
Chris Lattner153587e2005-05-12 07:00:44 +00004590 // Expand the source, then glue it back together for the call. We must expand
4591 // the source in case it is shared (this pass of legalize must traverse it).
4592 SDOperand SrcLo, SrcHi;
4593 ExpandOp(Source, SrcLo, SrcHi);
4594 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4595
Evan Cheng31cbddf2007-01-12 02:11:51 +00004596 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004597 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004598 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004599 else {
4600 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004601 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004602 }
Chris Lattner462505f2006-02-13 09:18:02 +00004603
Evan Chengbf535fc2007-04-27 07:33:31 +00004604 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004605 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4606 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004607 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4608 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004609}
Misha Brukman835702a2005-04-21 22:36:52 +00004610
Chris Lattner689bdcc2006-01-28 08:25:58 +00004611/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4612/// INT_TO_FP operation of the specified operand when the target requests that
4613/// we expand it. At this point, we know that the result and operand types are
4614/// legal for the target.
4615SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4616 SDOperand Op0,
4617 MVT::ValueType DestVT) {
4618 if (Op0.getValueType() == MVT::i32) {
4619 // simple 32-bit [signed|unsigned] integer to float/double expansion
4620
Chris Lattner50ee0e42007-01-20 22:35:55 +00004621 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004622 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004623 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4624 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004625 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004626 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004627 // get address of 8 byte buffer
4628 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4629 // word offset constant for Hi/Lo address computation
4630 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4631 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004632 SDOperand Hi = StackSlot;
4633 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4634 if (TLI.isLittleEndian())
4635 std::swap(Hi, Lo);
4636
Chris Lattner689bdcc2006-01-28 08:25:58 +00004637 // if signed map to unsigned space
4638 SDOperand Op0Mapped;
4639 if (isSigned) {
4640 // constant used to invert sign bit (signed to unsigned mapping)
4641 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4642 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4643 } else {
4644 Op0Mapped = Op0;
4645 }
4646 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004647 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004648 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004649 // initial hi portion of constructed double
4650 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4651 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004652 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004653 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004654 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004655 // FP constant to bias correct the final result
4656 SDOperand Bias = DAG.getConstantFP(isSigned ?
4657 BitsToDouble(0x4330000080000000ULL)
4658 : BitsToDouble(0x4330000000000000ULL),
4659 MVT::f64);
4660 // subtract the bias
4661 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4662 // final result
4663 SDOperand Result;
4664 // handle final rounding
4665 if (DestVT == MVT::f64) {
4666 // do nothing
4667 Result = Sub;
4668 } else {
4669 // if f32 then cast to f32
4670 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4671 }
4672 return Result;
4673 }
4674 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4675 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4676
4677 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4678 DAG.getConstant(0, Op0.getValueType()),
4679 ISD::SETLT);
4680 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4681 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4682 SignSet, Four, Zero);
4683
4684 // If the sign bit of the integer is set, the large number will be treated
4685 // as a negative number. To counteract this, the dynamic code adds an
4686 // offset depending on the data type.
4687 uint64_t FF;
4688 switch (Op0.getValueType()) {
4689 default: assert(0 && "Unsupported integer type!");
4690 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4691 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4692 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4693 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4694 }
4695 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004696 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004697
4698 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4699 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4700 SDOperand FudgeInReg;
4701 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004702 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004703 else {
4704 assert(DestVT == MVT::f64 && "Unexpected conversion");
4705 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4706 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004707 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004708 }
4709
4710 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4711}
4712
4713/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4714/// *INT_TO_FP operation of the specified operand when the target requests that
4715/// we promote it. At this point, we know that the result and operand types are
4716/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4717/// operation that takes a larger input.
4718SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4719 MVT::ValueType DestVT,
4720 bool isSigned) {
4721 // First step, figure out the appropriate *INT_TO_FP operation to use.
4722 MVT::ValueType NewInTy = LegalOp.getValueType();
4723
4724 unsigned OpToUse = 0;
4725
4726 // Scan for the appropriate larger type to use.
4727 while (1) {
4728 NewInTy = (MVT::ValueType)(NewInTy+1);
4729 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4730
4731 // If the target supports SINT_TO_FP of this type, use it.
4732 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4733 default: break;
4734 case TargetLowering::Legal:
4735 if (!TLI.isTypeLegal(NewInTy))
4736 break; // Can't use this datatype.
4737 // FALL THROUGH.
4738 case TargetLowering::Custom:
4739 OpToUse = ISD::SINT_TO_FP;
4740 break;
4741 }
4742 if (OpToUse) break;
4743 if (isSigned) continue;
4744
4745 // If the target supports UINT_TO_FP of this type, use it.
4746 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4747 default: break;
4748 case TargetLowering::Legal:
4749 if (!TLI.isTypeLegal(NewInTy))
4750 break; // Can't use this datatype.
4751 // FALL THROUGH.
4752 case TargetLowering::Custom:
4753 OpToUse = ISD::UINT_TO_FP;
4754 break;
4755 }
4756 if (OpToUse) break;
4757
4758 // Otherwise, try a larger type.
4759 }
4760
4761 // Okay, we found the operation and type to use. Zero extend our input to the
4762 // desired type then run the operation on it.
4763 return DAG.getNode(OpToUse, DestVT,
4764 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4765 NewInTy, LegalOp));
4766}
4767
4768/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4769/// FP_TO_*INT operation of the specified operand when the target requests that
4770/// we promote it. At this point, we know that the result and operand types are
4771/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4772/// operation that returns a larger result.
4773SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4774 MVT::ValueType DestVT,
4775 bool isSigned) {
4776 // First step, figure out the appropriate FP_TO*INT operation to use.
4777 MVT::ValueType NewOutTy = DestVT;
4778
4779 unsigned OpToUse = 0;
4780
4781 // Scan for the appropriate larger type to use.
4782 while (1) {
4783 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4784 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4785
4786 // If the target supports FP_TO_SINT returning this type, use it.
4787 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4788 default: break;
4789 case TargetLowering::Legal:
4790 if (!TLI.isTypeLegal(NewOutTy))
4791 break; // Can't use this datatype.
4792 // FALL THROUGH.
4793 case TargetLowering::Custom:
4794 OpToUse = ISD::FP_TO_SINT;
4795 break;
4796 }
4797 if (OpToUse) break;
4798
4799 // If the target supports FP_TO_UINT of this type, use it.
4800 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4801 default: break;
4802 case TargetLowering::Legal:
4803 if (!TLI.isTypeLegal(NewOutTy))
4804 break; // Can't use this datatype.
4805 // FALL THROUGH.
4806 case TargetLowering::Custom:
4807 OpToUse = ISD::FP_TO_UINT;
4808 break;
4809 }
4810 if (OpToUse) break;
4811
4812 // Otherwise, try a larger type.
4813 }
4814
4815 // Okay, we found the operation and type to use. Truncate the result of the
4816 // extended FP_TO_*INT operation to the desired size.
4817 return DAG.getNode(ISD::TRUNCATE, DestVT,
4818 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4819}
4820
4821/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4822///
4823SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4824 MVT::ValueType VT = Op.getValueType();
4825 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4826 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4827 switch (VT) {
4828 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4829 case MVT::i16:
4830 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4831 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4832 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4833 case MVT::i32:
4834 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4835 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4836 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4837 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4838 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4839 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4840 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4841 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4842 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4843 case MVT::i64:
4844 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4845 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4846 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4847 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4848 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4849 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4850 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4851 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4852 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4853 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4854 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4855 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4856 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4857 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4858 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4859 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4860 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4861 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4862 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4863 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4864 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4865 }
4866}
4867
4868/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4869///
4870SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4871 switch (Opc) {
4872 default: assert(0 && "Cannot expand this yet!");
4873 case ISD::CTPOP: {
4874 static const uint64_t mask[6] = {
4875 0x5555555555555555ULL, 0x3333333333333333ULL,
4876 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4877 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4878 };
4879 MVT::ValueType VT = Op.getValueType();
4880 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004881 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004882 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4883 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4884 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4885 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4886 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4887 DAG.getNode(ISD::AND, VT,
4888 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4889 }
4890 return Op;
4891 }
4892 case ISD::CTLZ: {
4893 // for now, we do this:
4894 // x = x | (x >> 1);
4895 // x = x | (x >> 2);
4896 // ...
4897 // x = x | (x >>16);
4898 // x = x | (x >>32); // for 64-bit input
4899 // return popcount(~x);
4900 //
4901 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4902 MVT::ValueType VT = Op.getValueType();
4903 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004904 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004905 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4906 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4907 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4908 }
4909 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4910 return DAG.getNode(ISD::CTPOP, VT, Op);
4911 }
4912 case ISD::CTTZ: {
4913 // for now, we use: { return popcount(~x & (x - 1)); }
4914 // unless the target has ctlz but not ctpop, in which case we use:
4915 // { return 32 - nlz(~x & (x-1)); }
4916 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4917 MVT::ValueType VT = Op.getValueType();
4918 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4919 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4920 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4921 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4922 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4923 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4924 TLI.isOperationLegal(ISD::CTLZ, VT))
4925 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004926 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00004927 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4928 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4929 }
4930 }
4931}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004932
Chris Lattnerdc750592005-01-07 07:47:09 +00004933/// ExpandOp - Expand the specified SDOperand into its two component pieces
4934/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4935/// LegalizeNodes map is filled in for any results that are not expanded, the
4936/// ExpandedNodes map is filled in for any results that are expanded, and the
4937/// Lo/Hi values are returned.
4938void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4939 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004940 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004941 SDNode *Node = Op.Val;
4942 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004943 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00004944 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004945 "Cannot expand to FP value or to larger int value!");
4946
Chris Lattner1a570f12005-09-02 20:32:45 +00004947 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004948 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004949 = ExpandedNodes.find(Op);
4950 if (I != ExpandedNodes.end()) {
4951 Lo = I->second.first;
4952 Hi = I->second.second;
4953 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004954 }
4955
Chris Lattnerdc750592005-01-07 07:47:09 +00004956 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004957 case ISD::CopyFromReg:
4958 assert(0 && "CopyFromReg must be legal!");
4959 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004960#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004961 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004962#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004963 assert(0 && "Do not know how to expand this operator!");
4964 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004965 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004966 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004967 Lo = DAG.getNode(ISD::UNDEF, NVT);
4968 Hi = DAG.getNode(ISD::UNDEF, NVT);
4969 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004970 case ISD::Constant: {
4971 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4972 Lo = DAG.getConstant(Cst, NVT);
4973 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4974 break;
4975 }
Evan Cheng47833a12006-12-12 21:32:44 +00004976 case ISD::ConstantFP: {
4977 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004978 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004979 if (getTypeAction(Lo.getValueType()) == Expand)
4980 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004981 break;
4982 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004983 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004984 // Return the operands.
4985 Lo = Node->getOperand(0);
4986 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004987 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004988
4989 case ISD::SIGN_EXTEND_INREG:
4990 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004991 // sext_inreg the low part if needed.
4992 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4993
4994 // The high part gets the sign extension from the lo-part. This handles
4995 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004996 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4997 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4998 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004999 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005000
Nate Begeman2fba8a32006-01-14 03:14:10 +00005001 case ISD::BSWAP: {
5002 ExpandOp(Node->getOperand(0), Lo, Hi);
5003 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5004 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5005 Lo = TempLo;
5006 break;
5007 }
5008
Chris Lattner55e9cde2005-05-11 04:51:16 +00005009 case ISD::CTPOP:
5010 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005011 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5012 DAG.getNode(ISD::CTPOP, NVT, Lo),
5013 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005014 Hi = DAG.getConstant(0, NVT);
5015 break;
5016
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005017 case ISD::CTLZ: {
5018 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005019 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005020 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5021 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005022 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5023 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005024 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5025 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5026
5027 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5028 Hi = DAG.getConstant(0, NVT);
5029 break;
5030 }
5031
5032 case ISD::CTTZ: {
5033 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005034 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005035 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5036 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005037 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5038 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005039 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5040 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5041
5042 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5043 Hi = DAG.getConstant(0, NVT);
5044 break;
5045 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005046
Nate Begemane74795c2006-01-25 18:21:52 +00005047 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005048 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5049 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005050 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5051 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5052
5053 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005054 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005055 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5056 if (!TLI.isLittleEndian())
5057 std::swap(Lo, Hi);
5058 break;
5059 }
5060
Chris Lattnerdc750592005-01-07 07:47:09 +00005061 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005062 LoadSDNode *LD = cast<LoadSDNode>(Node);
5063 SDOperand Ch = LD->getChain(); // Legalize the chain.
5064 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5065 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005066 int SVOffset = LD->getSrcValueOffset();
5067 unsigned Alignment = LD->getAlignment();
5068 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005069
Evan Chenge71fe34d2006-10-09 20:57:25 +00005070 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005071 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5072 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005073 if (VT == MVT::f32 || VT == MVT::f64) {
5074 // f32->i32 or f64->i64 one to one expansion.
5075 // Remember that we legalized the chain.
5076 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005077 // Recursively expand the new load.
5078 if (getTypeAction(NVT) == Expand)
5079 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005080 break;
5081 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005082
Evan Chenge71fe34d2006-10-09 20:57:25 +00005083 // Increment the pointer to the other half.
5084 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5085 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5086 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005087 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00005088 if (Alignment > IncrementSize)
5089 Alignment = IncrementSize;
5090 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5091 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005092
Evan Chenge71fe34d2006-10-09 20:57:25 +00005093 // Build a factor node to remember that this load is independent of the
5094 // other one.
5095 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5096 Hi.getValue(1));
5097
5098 // Remember that we legalized the chain.
5099 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5100 if (!TLI.isLittleEndian())
5101 std::swap(Lo, Hi);
5102 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005103 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005104
5105 if (VT == MVT::f64 && EVT == MVT::f32) {
5106 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5107 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005108 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005109 // Remember that we legalized the chain.
5110 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5111 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5112 break;
5113 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005114
5115 if (EVT == NVT)
5116 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005117 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005118 else
5119 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005120 SVOffset, EVT, isVolatile,
5121 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005122
5123 // Remember that we legalized the chain.
5124 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5125
5126 if (ExtType == ISD::SEXTLOAD) {
5127 // The high part is obtained by SRA'ing all but one of the bits of the
5128 // lo part.
5129 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5130 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5131 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5132 } else if (ExtType == ISD::ZEXTLOAD) {
5133 // The high part is just a zero.
5134 Hi = DAG.getConstant(0, NVT);
5135 } else /* if (ExtType == ISD::EXTLOAD) */ {
5136 // The high part is undefined.
5137 Hi = DAG.getNode(ISD::UNDEF, NVT);
5138 }
5139 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005140 break;
5141 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005142 case ISD::AND:
5143 case ISD::OR:
5144 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5145 SDOperand LL, LH, RL, RH;
5146 ExpandOp(Node->getOperand(0), LL, LH);
5147 ExpandOp(Node->getOperand(1), RL, RH);
5148 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5149 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5150 break;
5151 }
5152 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005153 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005154 ExpandOp(Node->getOperand(1), LL, LH);
5155 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005156 if (getTypeAction(NVT) == Expand)
5157 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005158 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005159 if (VT != MVT::f32)
5160 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005161 break;
5162 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005163 case ISD::SELECT_CC: {
5164 SDOperand TL, TH, FL, FH;
5165 ExpandOp(Node->getOperand(2), TL, TH);
5166 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005167 if (getTypeAction(NVT) == Expand)
5168 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005169 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5170 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005171 if (VT != MVT::f32)
5172 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5173 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005174 break;
5175 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005176 case ISD::ANY_EXTEND:
5177 // The low part is any extension of the input (which degenerates to a copy).
5178 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5179 // The high part is undefined.
5180 Hi = DAG.getNode(ISD::UNDEF, NVT);
5181 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005182 case ISD::SIGN_EXTEND: {
5183 // The low part is just a sign extension of the input (which degenerates to
5184 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005185 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005186
Chris Lattnerdc750592005-01-07 07:47:09 +00005187 // The high part is obtained by SRA'ing all but one of the bits of the lo
5188 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005189 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005190 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5191 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005192 break;
5193 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005194 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005195 // The low part is just a zero extension of the input (which degenerates to
5196 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005197 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005198
Chris Lattnerdc750592005-01-07 07:47:09 +00005199 // The high part is just a zero.
5200 Hi = DAG.getConstant(0, NVT);
5201 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005202
Chris Lattner59b27fa372007-02-13 23:55:16 +00005203 case ISD::TRUNCATE: {
5204 // The input value must be larger than this value. Expand *it*.
5205 SDOperand NewLo;
5206 ExpandOp(Node->getOperand(0), NewLo, Hi);
5207
5208 // The low part is now either the right size, or it is closer. If not the
5209 // right size, make an illegal truncate so we recursively expand it.
5210 if (NewLo.getValueType() != Node->getValueType(0))
5211 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5212 ExpandOp(NewLo, Lo, Hi);
5213 break;
5214 }
5215
Chris Lattner36e663d2005-12-23 00:16:34 +00005216 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005217 SDOperand Tmp;
5218 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5219 // If the target wants to, allow it to lower this itself.
5220 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5221 case Expand: assert(0 && "cannot expand FP!");
5222 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5223 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5224 }
5225 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5226 }
5227
Evan Cheng4eee7242006-12-09 02:42:38 +00005228 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005229 if (VT == MVT::f32 || VT == MVT::f64) {
5230 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005231 if (getTypeAction(NVT) == Expand)
5232 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005233 break;
5234 }
5235
5236 // If source operand will be expanded to the same type as VT, i.e.
5237 // i64 <- f64, i32 <- f32, expand the source operand instead.
5238 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5239 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5240 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005241 break;
5242 }
5243
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005244 // Turn this into a load/store pair by default.
5245 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005246 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005247
Chris Lattner36e663d2005-12-23 00:16:34 +00005248 ExpandOp(Tmp, Lo, Hi);
5249 break;
5250 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005251
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005252 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005253 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5254 TargetLowering::Custom &&
5255 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005256 Lo = TLI.LowerOperation(Op, DAG);
5257 assert(Lo.Val && "Node must be custom expanded!");
5258 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005259 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005260 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005261 break;
5262
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005263 // These operators cannot be expanded directly, emit them as calls to
5264 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005265 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005266 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005267 SDOperand Op;
5268 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5269 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005270 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5271 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005272 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005273
Chris Lattner941d84a2005-07-30 01:40:57 +00005274 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5275
Chris Lattnerfe68d752005-07-29 00:33:32 +00005276 // Now that the custom expander is done, expand the result, which is still
5277 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005278 if (Op.Val) {
5279 ExpandOp(Op, Lo, Hi);
5280 break;
5281 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005282 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005283
Evan Cheng31cbddf2007-01-12 02:11:51 +00005284 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005285 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005286 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005287 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005288 LC = RTLIB::FPTOSINT_F64_I64;
5289 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5290 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005291 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005292 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005293
Evan Cheng31cbddf2007-01-12 02:11:51 +00005294 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005295 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005296 SDOperand Op;
5297 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5298 case Expand: assert(0 && "cannot expand FP!");
5299 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5300 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5301 }
5302
5303 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5304
5305 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005306 if (Op.Val) {
5307 ExpandOp(Op, Lo, Hi);
5308 break;
5309 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005310 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005311
Evan Cheng31cbddf2007-01-12 02:11:51 +00005312 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005313 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005314 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005315 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005316 LC = RTLIB::FPTOUINT_F64_I64;
5317 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5318 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005319 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005320 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005321
Evan Cheng870e4f82006-01-09 18:31:59 +00005322 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005323 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005324 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005325 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005326 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005327 Op = TLI.LowerOperation(Op, DAG);
5328 if (Op.Val) {
5329 // Now that the custom expander is done, expand the result, which is
5330 // still VT.
5331 ExpandOp(Op, Lo, Hi);
5332 break;
5333 }
5334 }
5335
Chris Lattner72b503b2006-09-13 03:50:39 +00005336 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5337 // this X << 1 as X+X.
5338 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5339 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5340 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5341 SDOperand LoOps[2], HiOps[3];
5342 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5343 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5344 LoOps[1] = LoOps[0];
5345 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5346
5347 HiOps[1] = HiOps[0];
5348 HiOps[2] = Lo.getValue(1);
5349 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5350 break;
5351 }
5352 }
5353
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005354 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005355 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005356 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005357
5358 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005359 TargetLowering::LegalizeAction Action =
5360 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5361 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5362 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005363 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005364 break;
5365 }
5366
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005367 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005368 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5369 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005370 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005371 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005372
Evan Cheng870e4f82006-01-09 18:31:59 +00005373 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005374 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005375 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005376 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005377 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005378 Op = TLI.LowerOperation(Op, DAG);
5379 if (Op.Val) {
5380 // Now that the custom expander is done, expand the result, which is
5381 // still VT.
5382 ExpandOp(Op, Lo, Hi);
5383 break;
5384 }
5385 }
5386
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005387 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005388 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005389 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005390
5391 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005392 TargetLowering::LegalizeAction Action =
5393 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5394 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5395 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005396 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005397 break;
5398 }
5399
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005400 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005401 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5402 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005403 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005404 }
5405
5406 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005407 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005408 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005409 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005410 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005411 Op = TLI.LowerOperation(Op, DAG);
5412 if (Op.Val) {
5413 // Now that the custom expander is done, expand the result, which is
5414 // still VT.
5415 ExpandOp(Op, Lo, Hi);
5416 break;
5417 }
5418 }
5419
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005420 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005421 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005422 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005423
5424 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005425 TargetLowering::LegalizeAction Action =
5426 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5427 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5428 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005429 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005430 break;
5431 }
5432
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005433 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005434 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5435 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005436 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005437 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005438
Misha Brukman835702a2005-04-21 22:36:52 +00005439 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005440 case ISD::SUB: {
5441 // If the target wants to custom expand this, let them.
5442 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5443 TargetLowering::Custom) {
5444 Op = TLI.LowerOperation(Op, DAG);
5445 if (Op.Val) {
5446 ExpandOp(Op, Lo, Hi);
5447 break;
5448 }
5449 }
5450
5451 // Expand the subcomponents.
5452 SDOperand LHSL, LHSH, RHSL, RHSH;
5453 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5454 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005455 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5456 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005457 LoOps[0] = LHSL;
5458 LoOps[1] = RHSL;
5459 HiOps[0] = LHSH;
5460 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005461 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005462 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005463 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005464 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005465 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005466 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005467 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005468 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005469 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005470 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005471 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005472
5473 case ISD::ADDC:
5474 case ISD::SUBC: {
5475 // Expand the subcomponents.
5476 SDOperand LHSL, LHSH, RHSL, RHSH;
5477 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5478 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5479 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5480 SDOperand LoOps[2] = { LHSL, RHSL };
5481 SDOperand HiOps[3] = { LHSH, RHSH };
5482
5483 if (Node->getOpcode() == ISD::ADDC) {
5484 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5485 HiOps[2] = Lo.getValue(1);
5486 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5487 } else {
5488 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5489 HiOps[2] = Lo.getValue(1);
5490 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5491 }
5492 // Remember that we legalized the flag.
5493 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5494 break;
5495 }
5496 case ISD::ADDE:
5497 case ISD::SUBE: {
5498 // Expand the subcomponents.
5499 SDOperand LHSL, LHSH, RHSL, RHSH;
5500 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5501 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5502 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5503 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5504 SDOperand HiOps[3] = { LHSH, RHSH };
5505
5506 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5507 HiOps[2] = Lo.getValue(1);
5508 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5509
5510 // Remember that we legalized the flag.
5511 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5512 break;
5513 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005514 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005515 // If the target wants to custom expand this, let them.
5516 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005517 SDOperand New = TLI.LowerOperation(Op, DAG);
5518 if (New.Val) {
5519 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005520 break;
5521 }
5522 }
5523
Evan Chenge93762d2006-09-01 18:17:58 +00005524 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5525 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005526 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005527 SDOperand LL, LH, RL, RH;
5528 ExpandOp(Node->getOperand(0), LL, LH);
5529 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005530 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005531 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005532 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5533 // extended the sign bit of the low half through the upper half, and if so
5534 // emit a MULHS instead of the alternate sequence that is valid for any
5535 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005536 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005537 // is RH an extension of the sign bit of RL?
5538 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5539 RH.getOperand(1).getOpcode() == ISD::Constant &&
5540 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5541 // is LH an extension of the sign bit of LL?
5542 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5543 LH.getOperand(1).getOpcode() == ISD::Constant &&
5544 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005545 // Low part:
5546 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5547 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005548 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005549 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005550 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005551 // Low part:
5552 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5553
5554 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005555 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5556 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5557 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5558 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5559 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005560 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005561 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005562 }
Evan Chenge93762d2006-09-01 18:17:58 +00005563
Evan Cheng31cbddf2007-01-12 02:11:51 +00005564 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5565 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005566 break;
5567 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005568 case ISD::SDIV:
5569 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5570 break;
5571 case ISD::UDIV:
5572 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5573 break;
5574 case ISD::SREM:
5575 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5576 break;
5577 case ISD::UREM:
5578 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5579 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005580
Evan Cheng4eee7242006-12-09 02:42:38 +00005581 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005582 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5583 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5584 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005585 break;
5586 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005587 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5588 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5589 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005590 break;
5591 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005592 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5593 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5594 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005595 break;
5596 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005597 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5598 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5599 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005600 break;
5601 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005602 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005603 break;
5604 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005605 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005606 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005607 case ISD::FPOWI:
5608 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5609 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5610 Node, false, Hi);
5611 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005612 case ISD::FSQRT:
5613 case ISD::FSIN:
5614 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005615 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005616 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005617 case ISD::FSQRT:
5618 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5619 break;
5620 case ISD::FSIN:
5621 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5622 break;
5623 case ISD::FCOS:
5624 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5625 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005626 default: assert(0 && "Unreachable!");
5627 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005628 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005629 break;
5630 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005631 case ISD::FABS: {
5632 SDOperand Mask = (VT == MVT::f64)
5633 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5634 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5635 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5636 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5637 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5638 if (getTypeAction(NVT) == Expand)
5639 ExpandOp(Lo, Lo, Hi);
5640 break;
5641 }
5642 case ISD::FNEG: {
5643 SDOperand Mask = (VT == MVT::f64)
5644 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5645 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5646 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5647 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5648 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5649 if (getTypeAction(NVT) == Expand)
5650 ExpandOp(Lo, Lo, Hi);
5651 break;
5652 }
Evan Cheng003feb02007-01-04 21:56:39 +00005653 case ISD::FCOPYSIGN: {
5654 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5655 if (getTypeAction(NVT) == Expand)
5656 ExpandOp(Lo, Lo, Hi);
5657 break;
5658 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005659 case ISD::SINT_TO_FP:
5660 case ISD::UINT_TO_FP: {
5661 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5662 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005663 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005664 if (Node->getOperand(0).getValueType() == MVT::i64) {
5665 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005666 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005667 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005668 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005669 } else {
5670 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005671 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005672 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005673 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005674 }
5675
5676 // Promote the operand if needed.
5677 if (getTypeAction(SrcVT) == Promote) {
5678 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5679 Tmp = isSigned
5680 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5681 DAG.getValueType(SrcVT))
5682 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5683 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5684 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005685
5686 const char *LibCall = TLI.getLibcallName(LC);
5687 if (LibCall)
5688 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5689 else {
5690 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5691 Node->getOperand(0));
5692 if (getTypeAction(Lo.getValueType()) == Expand)
5693 ExpandOp(Lo, Lo, Hi);
5694 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005695 break;
5696 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005697 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005698
Chris Lattnerac12f682005-12-21 18:02:52 +00005699 // Make sure the resultant values have been legalized themselves, unless this
5700 // is a type that requires multi-step expansion.
5701 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5702 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005703 if (Hi.Val)
5704 // Don't legalize the high part if it is expanded to a single node.
5705 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005706 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005707
5708 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005709 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005710 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005711}
5712
Dan Gohmana8665142007-06-25 16:23:39 +00005713/// SplitVectorOp - Given an operand of vector type, break it down into
5714/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005715void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5716 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005717 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005718 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005719 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattner32206f52006-03-18 01:44:44 +00005720 assert(NumElements > 1 && "Cannot split a single element vector!");
5721 unsigned NewNumElts = NumElements/2;
Dan Gohmana8665142007-06-25 16:23:39 +00005722 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5723 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005724
5725 // See if we already split it.
5726 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5727 = SplitNodes.find(Op);
5728 if (I != SplitNodes.end()) {
5729 Lo = I->second.first;
5730 Hi = I->second.second;
5731 return;
5732 }
5733
5734 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005735 default:
5736#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005737 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005738#endif
5739 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00005740 case ISD::BUILD_PAIR:
5741 Lo = Node->getOperand(0);
5742 Hi = Node->getOperand(1);
5743 break;
5744 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005745 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5746 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00005747 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005748
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005749 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00005750 Node->op_end());
5751 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005752 break;
5753 }
Dan Gohmana8665142007-06-25 16:23:39 +00005754 case ISD::CONCAT_VECTORS: {
5755 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5756 if (NewNumSubvectors == 1) {
5757 Lo = Node->getOperand(0);
5758 Hi = Node->getOperand(1);
5759 } else {
5760 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5761 Node->op_begin()+NewNumSubvectors);
5762 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00005763
Dan Gohmana8665142007-06-25 16:23:39 +00005764 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5765 Node->op_end());
5766 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5767 }
Dan Gohman26455c42007-06-13 15:12:02 +00005768 break;
5769 }
Dan Gohmana8665142007-06-25 16:23:39 +00005770 case ISD::ADD:
5771 case ISD::SUB:
5772 case ISD::MUL:
5773 case ISD::FADD:
5774 case ISD::FSUB:
5775 case ISD::FMUL:
5776 case ISD::SDIV:
5777 case ISD::UDIV:
5778 case ISD::FDIV:
5779 case ISD::AND:
5780 case ISD::OR:
5781 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00005782 SDOperand LL, LH, RL, RH;
5783 SplitVectorOp(Node->getOperand(0), LL, LH);
5784 SplitVectorOp(Node->getOperand(1), RL, RH);
5785
Dan Gohmana8665142007-06-25 16:23:39 +00005786 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5787 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00005788 break;
5789 }
Dan Gohmana8665142007-06-25 16:23:39 +00005790 case ISD::LOAD: {
5791 LoadSDNode *LD = cast<LoadSDNode>(Node);
5792 SDOperand Ch = LD->getChain();
5793 SDOperand Ptr = LD->getBasePtr();
5794 const Value *SV = LD->getSrcValue();
5795 int SVOffset = LD->getSrcValueOffset();
5796 unsigned Alignment = LD->getAlignment();
5797 bool isVolatile = LD->isVolatile();
5798
5799 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5800 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00005801 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5802 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005803 SVOffset += IncrementSize;
5804 if (Alignment > IncrementSize)
5805 Alignment = IncrementSize;
5806 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00005807
5808 // Build a factor node to remember that this load is independent of the
5809 // other one.
5810 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5811 Hi.getValue(1));
5812
5813 // Remember that we legalized the chain.
5814 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005815 break;
5816 }
Dan Gohmana8665142007-06-25 16:23:39 +00005817 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005818 // We know the result is a vector. The input may be either a vector or a
5819 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00005820 SDOperand InOp = Node->getOperand(0);
5821 if (!MVT::isVector(InOp.getValueType()) ||
5822 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5823 // The input is a scalar or single-element vector.
5824 // Lower to a store/load so that it can be split.
5825 // FIXME: this could be improved probably.
5826 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005827
Evan Chengdf9ac472006-10-05 23:01:46 +00005828 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00005829 InOp, Ptr, NULL, 0);
5830 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005831 }
Dan Gohman0de76942007-06-29 00:09:08 +00005832 // Split the vector and convert each of the pieces now.
5833 SplitVectorOp(InOp, Lo, Hi);
5834 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5835 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5836 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005837 }
Chris Lattner32206f52006-03-18 01:44:44 +00005838 }
5839
5840 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005841 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005842 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00005843 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00005844}
5845
5846
Dan Gohmanf4e86da2007-06-27 14:06:22 +00005847/// ScalarizeVectorOp - Given an operand of single-element vector type
5848/// (e.g. v1f32), convert it into the equivalent operation that returns a
5849/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00005850SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5851 assert(MVT::isVector(Op.getValueType()) &&
5852 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00005853 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005854 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5855 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00005856
Dan Gohmana8665142007-06-25 16:23:39 +00005857 // See if we already scalarized it.
5858 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5859 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00005860
5861 SDOperand Result;
5862 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005863 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005864#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005865 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005866#endif
Dan Gohmana8665142007-06-25 16:23:39 +00005867 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5868 case ISD::ADD:
5869 case ISD::FADD:
5870 case ISD::SUB:
5871 case ISD::FSUB:
5872 case ISD::MUL:
5873 case ISD::FMUL:
5874 case ISD::SDIV:
5875 case ISD::UDIV:
5876 case ISD::FDIV:
5877 case ISD::SREM:
5878 case ISD::UREM:
5879 case ISD::FREM:
5880 case ISD::AND:
5881 case ISD::OR:
5882 case ISD::XOR:
5883 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00005884 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00005885 ScalarizeVectorOp(Node->getOperand(0)),
5886 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00005887 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005888 case ISD::FNEG:
5889 case ISD::FABS:
5890 case ISD::FSQRT:
5891 case ISD::FSIN:
5892 case ISD::FCOS:
5893 Result = DAG.getNode(Node->getOpcode(),
5894 NewVT,
5895 ScalarizeVectorOp(Node->getOperand(0)));
5896 break;
5897 case ISD::LOAD: {
5898 LoadSDNode *LD = cast<LoadSDNode>(Node);
5899 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5900 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005901
Dan Gohmana8665142007-06-25 16:23:39 +00005902 const Value *SV = LD->getSrcValue();
5903 int SVOffset = LD->getSrcValueOffset();
5904 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5905 LD->isVolatile(), LD->getAlignment());
5906
Chris Lattner32206f52006-03-18 01:44:44 +00005907 // Remember that we legalized the chain.
5908 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5909 break;
5910 }
Dan Gohmana8665142007-06-25 16:23:39 +00005911 case ISD::BUILD_VECTOR:
5912 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00005913 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005914 case ISD::INSERT_VECTOR_ELT:
5915 // Returning the inserted scalar element.
5916 Result = Node->getOperand(1);
5917 break;
5918 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00005919 assert(Node->getOperand(0).getValueType() == NewVT &&
5920 "Concat of non-legal vectors not yet supported!");
5921 Result = Node->getOperand(0);
5922 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005923 case ISD::VECTOR_SHUFFLE: {
5924 // Figure out if the scalar is the LHS or RHS and return it.
5925 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5926 if (cast<ConstantSDNode>(EltNum)->getValue())
5927 Result = ScalarizeVectorOp(Node->getOperand(1));
5928 else
5929 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00005930 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005931 }
5932 case ISD::EXTRACT_SUBVECTOR:
5933 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00005934 assert(Result.getValueType() == NewVT);
5935 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005936 case ISD::BIT_CONVERT:
5937 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005938 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005939 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00005940 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00005941 ScalarizeVectorOp(Op.getOperand(1)),
5942 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00005943 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005944 }
5945
5946 if (TLI.isTypeLegal(NewVT))
5947 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00005948 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5949 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00005950 return Result;
5951}
5952
Chris Lattnerdc750592005-01-07 07:47:09 +00005953
5954// SelectionDAG::Legalize - This is the entry point for the file.
5955//
Chris Lattner4add7e32005-01-23 04:42:50 +00005956void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005957 if (ViewLegalizeDAGs) viewGraph();
5958
Chris Lattnerdc750592005-01-07 07:47:09 +00005959 /// run - This is the main entry point to this class.
5960 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005961 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005962}
5963