blob: e87dfa615c552ffb3182c7bf6b512a85c8be7c52 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000031#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000032using namespace llvm;
33
Chris Lattner5e46a192006-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 Lattner3e928bb2005-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 Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
58
Chris Lattner6831a812006-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 Lattner3e928bb2005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner68a17fe2006-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 Lattnerd74ea2b2006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 };
Chris Lattner6831a812006-02-13 09:18:02 +000076
Chris Lattner3e928bb2005-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 Lattner68a17fe2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000081
Chris Lattner3e928bb2005-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.
85 std::map<SDOperand, SDOperand> LegalizedNodes;
86
Chris Lattner03c85462005-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.
90 std::map<SDOperand, SDOperand> PromotedNodes;
91
Chris Lattnerc7029802006-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 Lattner3e928bb2005-01-07 07:47:09 +000095 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
96
Chris Lattnerc7029802006-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
102 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
103 /// concrete packed types, this contains the mapping of ones we have already
104 /// processed to the result.
105 std::map<SDOperand, SDOperand> PackedNodes;
106
Chris Lattner8afc48e2005-01-07 22:28:47 +0000107 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-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 Lattner8afc48e2005-01-07 22:28:47 +0000112 }
Chris Lattner03c85462005-01-15 05:21:40 +0000113 void AddPromotedOperand(SDOperand From, SDOperand To) {
114 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
115 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-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 Lattner03c85462005-01-15 05:21:40 +0000118 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000119
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120public:
121
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000122 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner3e928bb2005-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 Lattner68a17fe2006-01-29 08:42:06 +0000128 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-01-07 07:47:09 +0000137 void LegalizeDAG();
138
Chris Lattner456a93a2006-01-28 07:39:30 +0000139private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000140 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
141 /// 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 Lattner3e928bb2005-01-07 07:47:09 +0000147 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-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 Lattner03c85462005-01-15 05:21:40 +0000154 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
Chris Lattnerc7029802006-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
164 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
165 /// two smaller values of MVT::Vector type.
166 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
167
168 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
169 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
170 /// this is called, we know that PackedVT is the right type for the result and
171 /// we know that this type is legal for the target.
172 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
173
Chris Lattner4352cc92006-04-04 17:23:26 +0000174 /// isShuffleLegal - Return true if a vector shuffle is legal with the
175 /// specified mask and type. Targets can specify exactly which masks they
176 /// support and the code generator is tasked with not creating illegal masks.
177 ///
178 /// Note that this will also return true for shuffles that are promoted to a
179 /// different type.
180 ///
181 /// If this is a legal shuffle, this method returns the (possibly promoted)
182 /// build_vector Mask. If it's not a legal shuffle, it returns null.
183 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000187
Nate Begeman750ac1b2006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattnerce872152006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencer47857812006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000196
Chris Lattner35481892005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Chris Lattner456a93a2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000214
Chris Lattner15972212006-03-31 17:55:51 +0000215 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000217
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner4352cc92006-04-04 17:23:26 +0000224/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
225/// specified mask and type. Targets can specify exactly which masks they
226/// support and the code generator is tasked with not creating illegal masks.
227///
228/// Note that this will also return true for shuffles that are promoted to a
229/// different type.
230SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
231 SDOperand Mask) const {
232 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 default: return 0;
234 case TargetLowering::Legal:
235 case TargetLowering::Custom:
236 break;
237 case TargetLowering::Promote: {
238 // If this is promoted to a different type, convert the shuffle mask and
239 // ask if it is legal in the promoted type!
240 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241
242 // If we changed # elements, change the shuffle mask.
243 unsigned NumEltsGrowth =
244 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
245 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
246 if (NumEltsGrowth > 1) {
247 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000249 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
250 SDOperand InOp = Mask.getOperand(i);
251 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
252 if (InOp.getOpcode() == ISD::UNDEF)
253 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 else {
255 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
256 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
257 }
258 }
259 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000261 }
262 VT = NVT;
263 break;
264 }
265 }
266 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
267}
268
Chris Lattnerc7029802006-03-18 01:44:44 +0000269/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
270/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000271static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000272 switch (VecOp) {
273 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000274 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
275 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
276 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
277 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
278 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
279 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
280 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
281 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000282 }
283}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000285SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
286 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
287 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000288 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000290}
291
Chris Lattner32fca002005-10-06 01:20:27 +0000292/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
293/// not been visited yet and if all of its operands have already been visited.
294static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
Chris Lattner79715142007-02-03 01:12:36 +0000295 DenseMap<SDNode*, unsigned> &Visited) {
Chris Lattner32fca002005-10-06 01:20:27 +0000296 if (++Visited[N] != N->getNumOperands())
297 return; // Haven't visited all operands yet
298
299 Order.push_back(N);
300
301 if (N->hasOneUse()) { // Tail recurse in common case.
302 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
303 return;
304 }
305
306 // Now that we have N in, add anything that uses it if all of their operands
307 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000308 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
309 ComputeTopDownOrdering(*UI, Order, Visited);
310}
311
Chris Lattner1618beb2005-07-29 00:11:56 +0000312
Chris Lattner3e928bb2005-01-07 07:47:09 +0000313void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000314 LastCALLSEQ_END = DAG.getEntryNode();
315 IsLegalizingCall = false;
316
Chris Lattnerab510a72005-10-02 17:49:46 +0000317 // The legalize process is inherently a bottom-up recursive process (users
318 // legalize their uses before themselves). Given infinite stack space, we
319 // could just start legalizing on the root and traverse the whole graph. In
320 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000321 // blocks. To avoid this problem, compute an ordering of the nodes where each
322 // node is only legalized after all of its operands are legalized.
Chris Lattner79715142007-02-03 01:12:36 +0000323 DenseMap<SDNode*, unsigned> Visited;
Chris Lattner32fca002005-10-06 01:20:27 +0000324 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000325
Chris Lattner32fca002005-10-06 01:20:27 +0000326 // Compute ordering from all of the leaves in the graphs, those (like the
327 // entry node) that have no operands.
328 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
329 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000330 if (I->getNumOperands() == 0) {
331 Visited[I] = 0 - 1U;
332 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000333 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000334 }
335
Chris Lattnerde202b32005-11-09 23:47:37 +0000336 assert(Order.size() == Visited.size() &&
337 Order.size() ==
338 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000339 "Error: DAG is cyclic!");
340 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000341
Chris Lattnerc7029802006-03-18 01:44:44 +0000342 for (unsigned i = 0, e = Order.size(); i != e; ++i)
343 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000344
345 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000347 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
348 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349
350 ExpandedNodes.clear();
351 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000352 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000353 SplitNodes.clear();
354 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355
356 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000357 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358}
359
Chris Lattner6831a812006-02-13 09:18:02 +0000360
361/// FindCallEndFromCallStart - Given a chained node that is part of a call
362/// sequence, find the CALLSEQ_END node that terminates the call sequence.
363static SDNode *FindCallEndFromCallStart(SDNode *Node) {
364 if (Node->getOpcode() == ISD::CALLSEQ_END)
365 return Node;
366 if (Node->use_empty())
367 return 0; // No CallSeqEnd
368
369 // The chain is usually at the end.
370 SDOperand TheChain(Node, Node->getNumValues()-1);
371 if (TheChain.getValueType() != MVT::Other) {
372 // Sometimes it's at the beginning.
373 TheChain = SDOperand(Node, 0);
374 if (TheChain.getValueType() != MVT::Other) {
375 // Otherwise, hunt for it.
376 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
377 if (Node->getValueType(i) == MVT::Other) {
378 TheChain = SDOperand(Node, i);
379 break;
380 }
381
382 // Otherwise, we walked into a node without a chain.
383 if (TheChain.getValueType() != MVT::Other)
384 return 0;
385 }
386 }
387
388 for (SDNode::use_iterator UI = Node->use_begin(),
389 E = Node->use_end(); UI != E; ++UI) {
390
391 // Make sure to only follow users of our token chain.
392 SDNode *User = *UI;
393 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
394 if (User->getOperand(i) == TheChain)
395 if (SDNode *Result = FindCallEndFromCallStart(User))
396 return Result;
397 }
398 return 0;
399}
400
401/// FindCallStartFromCallEnd - Given a chained node that is part of a call
402/// sequence, find the CALLSEQ_START node that initiates the call sequence.
403static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
404 assert(Node && "Didn't find callseq_start for a call??");
405 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
406
407 assert(Node->getOperand(0).getValueType() == MVT::Other &&
408 "Node doesn't have a token chain argument!");
409 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
410}
411
412/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
413/// see if any uses can reach Dest. If no dest operands can get to dest,
414/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000415///
416/// Keep track of the nodes we fine that actually do lead to Dest in
417/// NodesLeadingTo. This avoids retraversing them exponential number of times.
418///
419bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000420 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000421 if (N == Dest) return true; // N certainly leads to Dest :)
422
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000423 // If we've already processed this node and it does lead to Dest, there is no
424 // need to reprocess it.
425 if (NodesLeadingTo.count(N)) return true;
426
Chris Lattner6831a812006-02-13 09:18:02 +0000427 // If the first result of this node has been already legalized, then it cannot
428 // reach N.
429 switch (getTypeAction(N->getValueType(0))) {
430 case Legal:
431 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 case Promote:
434 if (PromotedNodes.count(SDOperand(N, 0))) return false;
435 break;
436 case Expand:
437 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
438 break;
439 }
440
441 // Okay, this node has not already been legalized. Check and legalize all
442 // operands. If none lead to Dest, then we can legalize this node.
443 bool OperandsLeadToDest = false;
444 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
445 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000446 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000447
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000448 if (OperandsLeadToDest) {
449 NodesLeadingTo.insert(N);
450 return true;
451 }
Chris Lattner6831a812006-02-13 09:18:02 +0000452
453 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000454 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000455 return false;
456}
457
Chris Lattnerc7029802006-03-18 01:44:44 +0000458/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
459/// appropriate for its type.
460void SelectionDAGLegalize::HandleOp(SDOperand Op) {
461 switch (getTypeAction(Op.getValueType())) {
462 default: assert(0 && "Bad type action!");
463 case Legal: LegalizeOp(Op); break;
464 case Promote: PromoteOp(Op); break;
465 case Expand:
466 if (Op.getValueType() != MVT::Vector) {
467 SDOperand X, Y;
468 ExpandOp(Op, X, Y);
469 } else {
470 SDNode *N = Op.Val;
471 unsigned NumOps = N->getNumOperands();
472 unsigned NumElements =
473 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
474 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
475 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
476 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
477 // In the common case, this is a legal vector type, convert it to the
478 // packed operation and type now.
479 PackVectorOp(Op, PackedVT);
480 } else if (NumElements == 1) {
481 // Otherwise, if this is a single element vector, convert it to a
482 // scalar operation.
483 PackVectorOp(Op, EVT);
484 } else {
485 // Otherwise, this is a multiple element vector that isn't supported.
486 // Split it in half and legalize both parts.
487 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000488 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000489 }
490 }
491 break;
492 }
493}
Chris Lattner6831a812006-02-13 09:18:02 +0000494
Evan Cheng9f877882006-12-13 20:57:08 +0000495/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
496/// a load from the constant pool.
497static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000498 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000499 bool Extend = false;
500
501 // If a FP immediate is precise when represented as a float and if the
502 // target can do an extending load from float to double, we put it into
503 // the constant pool as a float, even if it's is statically typed as a
504 // double.
505 MVT::ValueType VT = CFP->getValueType(0);
506 bool isDouble = VT == MVT::f64;
507 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
508 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000509 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000510 double Val = LLVMC->getValue();
511 return isDouble
512 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
513 : DAG.getConstant(FloatToBits(Val), MVT::i32);
514 }
515
Evan Cheng00495212006-12-12 21:32:44 +0000516 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
517 // Only do this if the target has a native EXTLOAD instruction from f32.
518 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
519 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
520 VT = MVT::f32;
521 Extend = true;
522 }
523
524 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
525 if (Extend) {
526 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
527 CPIdx, NULL, 0, MVT::f32);
528 } else {
529 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
530 }
531}
532
Chris Lattner6831a812006-02-13 09:18:02 +0000533
Evan Cheng912095b2007-01-04 21:56:39 +0000534/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
535/// operations.
536static
537SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
538 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000539 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000540 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
541 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000542
Evan Cheng912095b2007-01-04 21:56:39 +0000543 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000544 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000545 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
546 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000547 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000548 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000549 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000550 // Shift right or sign-extend it if the two operands have different types.
551 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
552 if (SizeDiff > 0) {
553 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
554 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
555 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
556 } else if (SizeDiff < 0)
557 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000558
559 // Clear the sign bit of first operand.
560 SDOperand Mask2 = (VT == MVT::f64)
561 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
562 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
563 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000564 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000565 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
566
567 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000568 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
569 return Result;
570}
571
572
Chris Lattnerc7029802006-03-18 01:44:44 +0000573/// LegalizeOp - We know that the specified value has a legal type.
574/// Recursively ensure that the operands have legal types, then return the
575/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000577 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000578 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000579 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000580
Chris Lattner3e928bb2005-01-07 07:47:09 +0000581 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000582 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000583 if (Node->getNumValues() > 1) {
584 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000585 if (getTypeAction(Node->getValueType(i)) != Legal) {
586 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000587 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000588 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000589 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000590 }
591 }
592
Chris Lattner45982da2005-05-12 16:53:42 +0000593 // Note that LegalizeOp may be reentered even from single-use nodes, which
594 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000595 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
596 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000597
Nate Begeman9373a812005-08-10 20:51:12 +0000598 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000599 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000600 bool isCustom = false;
601
Chris Lattner3e928bb2005-01-07 07:47:09 +0000602 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000603 case ISD::FrameIndex:
604 case ISD::EntryToken:
605 case ISD::Register:
606 case ISD::BasicBlock:
607 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000608 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000609 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000610 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000611 case ISD::TargetConstantPool:
612 case ISD::TargetGlobalAddress:
613 case ISD::TargetExternalSymbol:
614 case ISD::VALUETYPE:
615 case ISD::SRCVALUE:
616 case ISD::STRING:
617 case ISD::CONDCODE:
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +0000618 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner948c1b12006-01-28 08:31:04 +0000619 // Primitives must all be legal.
620 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
621 "This must be legal!");
622 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000624 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
625 // If this is a target node, legalize it by legalizing the operands then
626 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000627 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000628 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000629 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000630
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000631 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000632
633 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
634 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
635 return Result.getValue(Op.ResNo);
636 }
637 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000638#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +0000639 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000640#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 assert(0 && "Do not know how to legalize this operator!");
642 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000643 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000644 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000645 case ISD::ConstantPool:
646 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000647 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
648 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000649 case TargetLowering::Custom:
650 Tmp1 = TLI.LowerOperation(Op, DAG);
651 if (Tmp1.Val) Result = Tmp1;
652 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000653 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000654 break;
655 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000657 case ISD::FRAMEADDR:
658 case ISD::RETURNADDR:
659 // The only option for these nodes is to custom lower them. If the target
660 // does not custom lower them, then return zero.
661 Tmp1 = TLI.LowerOperation(Op, DAG);
662 if (Tmp1.Val)
663 Result = Tmp1;
664 else
665 Result = DAG.getConstant(0, TLI.getPointerTy());
666 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000667 case ISD::AssertSext:
668 case ISD::AssertZext:
669 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000671 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000672 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000673 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000674 Result = Node->getOperand(Op.ResNo);
675 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000676 case ISD::CopyFromReg:
677 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000678 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000679 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000680 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000681 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000682 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000683 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000684 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000685 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
686 } else {
687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
688 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000689 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
690 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000691 // Since CopyFromReg produces two values, make sure to remember that we
692 // legalized both of them.
693 AddLegalizedOperand(Op.getValue(0), Result);
694 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
695 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000696 case ISD::UNDEF: {
697 MVT::ValueType VT = Op.getValueType();
698 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000699 default: assert(0 && "This action is not supported yet!");
700 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000701 if (MVT::isInteger(VT))
702 Result = DAG.getConstant(0, VT);
703 else if (MVT::isFloatingPoint(VT))
704 Result = DAG.getConstantFP(0, VT);
705 else
706 assert(0 && "Unknown value type!");
707 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000708 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000709 break;
710 }
711 break;
712 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000713
Chris Lattner48b61a72006-03-28 00:40:33 +0000714 case ISD::INTRINSIC_W_CHAIN:
715 case ISD::INTRINSIC_WO_CHAIN:
716 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000717 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000718 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
719 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000720 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000721
722 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000723 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000724 TargetLowering::Custom) {
725 Tmp3 = TLI.LowerOperation(Result, DAG);
726 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000727 }
728
729 if (Result.Val->getNumValues() == 1) break;
730
731 // Must have return value and chain result.
732 assert(Result.Val->getNumValues() == 2 &&
733 "Cannot return more than two values!");
734
735 // Since loads produce two values, make sure to remember that we
736 // legalized both of them.
737 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
738 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
739 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000740 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000741
742 case ISD::LOCATION:
743 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
744 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
745
746 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
747 case TargetLowering::Promote:
748 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000749 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000750 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000751 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000752 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000753
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000754 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000755 const std::string &FName =
756 cast<StringSDNode>(Node->getOperand(3))->getValue();
757 const std::string &DirName =
758 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000759 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000760
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000761 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000762 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000763 SDOperand LineOp = Node->getOperand(1);
764 SDOperand ColOp = Node->getOperand(2);
765
766 if (useDEBUG_LOC) {
767 Ops.push_back(LineOp); // line #
768 Ops.push_back(ColOp); // col #
769 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000770 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000771 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000772 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
773 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000774 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000775 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000776 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000777 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000778 } else {
779 Result = Tmp1; // chain
780 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000781 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000782 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000783 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000784 if (Tmp1 != Node->getOperand(0) ||
785 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000786 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000787 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000788 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
789 Ops.push_back(Node->getOperand(1)); // line # must be legal.
790 Ops.push_back(Node->getOperand(2)); // col # must be legal.
791 } else {
792 // Otherwise promote them.
793 Ops.push_back(PromoteOp(Node->getOperand(1)));
794 Ops.push_back(PromoteOp(Node->getOperand(2)));
795 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000796 Ops.push_back(Node->getOperand(3)); // filename must be legal.
797 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000798 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000799 }
800 break;
801 }
802 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000803
804 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000805 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000806 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000807 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000808 case TargetLowering::Legal:
809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
810 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
811 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
812 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000813 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000814 break;
815 }
816 break;
817
Jim Laskey1ee29252007-01-26 14:34:52 +0000818 case ISD::LABEL:
819 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
820 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000821 default: assert(0 && "This action is not supported yet!");
822 case TargetLowering::Legal:
823 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
824 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000825 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000826 break;
827 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000828 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000829
Chris Lattner3e928bb2005-01-07 07:47:09 +0000830 case ISD::Constant:
831 // We know we don't need to expand constants here, constants only have one
832 // value and we check that it is fine above.
833
834 // FIXME: Maybe we should handle things like targets that don't support full
835 // 32-bit immediates?
836 break;
837 case ISD::ConstantFP: {
838 // Spill FP immediates to the constant pool if the target cannot directly
839 // codegen them. Targets often have some immediate values that can be
840 // efficiently generated into an FP register without a load. We explicitly
841 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000842 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
843
844 // Check to see if this FP immediate is already legal.
845 bool isLegal = false;
846 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
847 E = TLI.legal_fpimm_end(); I != E; ++I)
848 if (CFP->isExactlyValue(*I)) {
849 isLegal = true;
850 break;
851 }
852
Chris Lattner3181a772006-01-29 06:26:56 +0000853 // If this is a legal constant, turn it into a TargetConstantFP node.
854 if (isLegal) {
855 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
856 break;
857 }
858
859 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
860 default: assert(0 && "This action is not supported yet!");
861 case TargetLowering::Custom:
862 Tmp3 = TLI.LowerOperation(Result, DAG);
863 if (Tmp3.Val) {
864 Result = Tmp3;
865 break;
866 }
867 // FALLTHROUGH
868 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000869 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000870 }
871 break;
872 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000873 case ISD::TokenFactor:
874 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000875 Tmp1 = LegalizeOp(Node->getOperand(0));
876 Tmp2 = LegalizeOp(Node->getOperand(1));
877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
878 } else if (Node->getNumOperands() == 3) {
879 Tmp1 = LegalizeOp(Node->getOperand(0));
880 Tmp2 = LegalizeOp(Node->getOperand(1));
881 Tmp3 = LegalizeOp(Node->getOperand(2));
882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000883 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000884 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000885 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000886 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
887 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000888 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000889 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000890 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000891
892 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000893 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000894 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000895 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
896 assert(Tmp3.Val && "Target didn't custom lower this node!");
897 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
898 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000899
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000900 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000901 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000902 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
903 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000904 if (Op.ResNo == i)
905 Tmp2 = Tmp1;
906 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
907 }
908 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000909
Chris Lattnerb2827b02006-03-19 00:52:58 +0000910 case ISD::BUILD_VECTOR:
911 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000912 default: assert(0 && "This action is not supported yet!");
913 case TargetLowering::Custom:
914 Tmp3 = TLI.LowerOperation(Result, DAG);
915 if (Tmp3.Val) {
916 Result = Tmp3;
917 break;
918 }
919 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000920 case TargetLowering::Expand:
921 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000922 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000923 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000924 break;
925 case ISD::INSERT_VECTOR_ELT:
926 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
927 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
928 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
930
931 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
932 Node->getValueType(0))) {
933 default: assert(0 && "This action is not supported yet!");
934 case TargetLowering::Legal:
935 break;
936 case TargetLowering::Custom:
937 Tmp3 = TLI.LowerOperation(Result, DAG);
938 if (Tmp3.Val) {
939 Result = Tmp3;
940 break;
941 }
942 // FALLTHROUGH
943 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000944 // If the insert index is a constant, codegen this as a scalar_to_vector,
945 // then a shuffle that inserts it into the right position in the vector.
946 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
947 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
948 Tmp1.getValueType(), Tmp2);
949
950 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
951 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
952 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
953
954 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
955 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
956 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000957 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000958 for (unsigned i = 0; i != NumElts; ++i) {
959 if (i != InsertPos->getValue())
960 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
961 else
962 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
963 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000964 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
965 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +0000966
967 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
968 Tmp1, ScVec, ShufMask);
969 Result = LegalizeOp(Result);
970 break;
971 }
972
Chris Lattner2332b9f2006-03-19 01:17:20 +0000973 // If the target doesn't support this, we have to spill the input vector
974 // to a temporary stack slot, update the element, then reload it. This is
975 // badness. We could also load the value into a vector register (either
976 // with a "move to register" or "extload into register" instruction, then
977 // permute it into place, if the idx is a constant and if the idx is
978 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000979 MVT::ValueType VT = Tmp1.getValueType();
980 MVT::ValueType EltVT = Tmp2.getValueType();
981 MVT::ValueType IdxVT = Tmp3.getValueType();
982 MVT::ValueType PtrVT = TLI.getPointerTy();
983 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000984 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000985 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000986
987 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000988 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
989 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000990 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000991 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
992 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
993 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000994 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000995 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000996 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +0000997 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +0000998 break;
999 }
1000 }
1001 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001002 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001003 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1004 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1005 break;
1006 }
1007
Chris Lattnerce872152006-03-19 06:31:19 +00001008 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1009 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1010 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1011 Node->getValueType(0))) {
1012 default: assert(0 && "This action is not supported yet!");
1013 case TargetLowering::Legal:
1014 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001015 case TargetLowering::Custom:
1016 Tmp3 = TLI.LowerOperation(Result, DAG);
1017 if (Tmp3.Val) {
1018 Result = Tmp3;
1019 break;
1020 }
1021 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001022 case TargetLowering::Expand:
1023 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001024 break;
1025 }
Chris Lattnerce872152006-03-19 06:31:19 +00001026 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001027 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001028 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1029 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1031
1032 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001033 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1034 default: assert(0 && "Unknown operation action!");
1035 case TargetLowering::Legal:
1036 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1037 "vector shuffle should not be created if not legal!");
1038 break;
1039 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001040 Tmp3 = TLI.LowerOperation(Result, DAG);
1041 if (Tmp3.Val) {
1042 Result = Tmp3;
1043 break;
1044 }
1045 // FALLTHROUGH
1046 case TargetLowering::Expand: {
1047 MVT::ValueType VT = Node->getValueType(0);
1048 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1049 MVT::ValueType PtrVT = TLI.getPointerTy();
1050 SDOperand Mask = Node->getOperand(2);
1051 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001052 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001053 for (unsigned i = 0; i != NumElems; ++i) {
1054 SDOperand Arg = Mask.getOperand(i);
1055 if (Arg.getOpcode() == ISD::UNDEF) {
1056 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1057 } else {
1058 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1059 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1060 if (Idx < NumElems)
1061 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1062 DAG.getConstant(Idx, PtrVT)));
1063 else
1064 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1065 DAG.getConstant(Idx - NumElems, PtrVT)));
1066 }
1067 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001068 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001069 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001070 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001071 case TargetLowering::Promote: {
1072 // Change base type to a different vector type.
1073 MVT::ValueType OVT = Node->getValueType(0);
1074 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1075
1076 // Cast the two input vectors.
1077 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1078 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1079
1080 // Convert the shuffle mask to the right # elements.
1081 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1082 assert(Tmp3.Val && "Shuffle not legal?");
1083 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1084 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1085 break;
1086 }
Chris Lattner87100e02006-03-20 01:52:29 +00001087 }
1088 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001089
1090 case ISD::EXTRACT_VECTOR_ELT:
1091 Tmp1 = LegalizeOp(Node->getOperand(0));
1092 Tmp2 = LegalizeOp(Node->getOperand(1));
1093 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001094
1095 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1096 Tmp1.getValueType())) {
1097 default: assert(0 && "This action is not supported yet!");
1098 case TargetLowering::Legal:
1099 break;
1100 case TargetLowering::Custom:
1101 Tmp3 = TLI.LowerOperation(Result, DAG);
1102 if (Tmp3.Val) {
1103 Result = Tmp3;
1104 break;
1105 }
1106 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001107 case TargetLowering::Expand:
1108 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001109 break;
1110 }
Chris Lattner384504c2006-03-21 20:44:12 +00001111 break;
1112
Chris Lattner15972212006-03-31 17:55:51 +00001113 case ISD::VEXTRACT_VECTOR_ELT:
1114 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001115 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001116
Chris Lattner6831a812006-02-13 09:18:02 +00001117 case ISD::CALLSEQ_START: {
1118 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1119
1120 // Recursively Legalize all of the inputs of the call end that do not lead
1121 // to this call start. This ensures that any libcalls that need be inserted
1122 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001123 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001124 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001125 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1126 NodesLeadingTo);
1127 }
Chris Lattner6831a812006-02-13 09:18:02 +00001128
1129 // Now that we legalized all of the inputs (which may have inserted
1130 // libcalls) create the new CALLSEQ_START node.
1131 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1132
1133 // Merge in the last call, to ensure that this call start after the last
1134 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001135 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001136 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1137 Tmp1 = LegalizeOp(Tmp1);
1138 }
Chris Lattner6831a812006-02-13 09:18:02 +00001139
1140 // Do not try to legalize the target-specific arguments (#1+).
1141 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001142 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001143 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001144 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001145 }
1146
1147 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001148 AddLegalizedOperand(Op.getValue(0), Result);
1149 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1150 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1151
Chris Lattner6831a812006-02-13 09:18:02 +00001152 // Now that the callseq_start and all of the non-call nodes above this call
1153 // sequence have been legalized, legalize the call itself. During this
1154 // process, no libcalls can/will be inserted, guaranteeing that no calls
1155 // can overlap.
1156 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1157 SDOperand InCallSEQ = LastCALLSEQ_END;
1158 // Note that we are selecting this call!
1159 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1160 IsLegalizingCall = true;
1161
1162 // Legalize the call, starting from the CALLSEQ_END.
1163 LegalizeOp(LastCALLSEQ_END);
1164 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1165 return Result;
1166 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001167 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001168 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1169 // will cause this node to be legalized as well as handling libcalls right.
1170 if (LastCALLSEQ_END.Val != Node) {
1171 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1172 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1173 assert(I != LegalizedNodes.end() &&
1174 "Legalizing the call start should have legalized this node!");
1175 return I->second;
1176 }
1177
1178 // Otherwise, the call start has been legalized and everything is going
1179 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001181 // Do not try to legalize the target-specific arguments (#1+), except for
1182 // an optional flag input.
1183 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1184 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001185 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001186 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001187 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001188 }
1189 } else {
1190 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1191 if (Tmp1 != Node->getOperand(0) ||
1192 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001193 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001194 Ops[0] = Tmp1;
1195 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001196 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001197 }
Chris Lattner6a542892006-01-24 05:48:21 +00001198 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001199 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001200 // This finishes up call legalization.
1201 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001202
1203 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1204 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1205 if (Node->getNumValues() == 2)
1206 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1207 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001208 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001209 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1210 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1211 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001213
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001214 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001215 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001216 switch (TLI.getOperationAction(Node->getOpcode(),
1217 Node->getValueType(0))) {
1218 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001219 case TargetLowering::Expand: {
1220 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1221 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1222 " not tell us which reg is the stack pointer!");
1223 SDOperand Chain = Tmp1.getOperand(0);
1224 SDOperand Size = Tmp2.getOperand(1);
1225 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1226 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1227 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001228 Tmp1 = LegalizeOp(Tmp1);
1229 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001230 break;
1231 }
1232 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001233 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1234 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 Tmp1 = LegalizeOp(Tmp3);
1236 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001237 }
Chris Lattner903d2782006-01-15 08:54:32 +00001238 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001239 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001240 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001241 }
Chris Lattner903d2782006-01-15 08:54:32 +00001242 // Since this op produce two values, make sure to remember that we
1243 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001244 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1245 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001246 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001247 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001248 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001249 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001250 bool Changed = false;
1251 // Legalize all of the operands of the inline asm, in case they are nodes
1252 // that need to be expanded or something. Note we skip the asm string and
1253 // all of the TargetConstant flags.
1254 SDOperand Op = LegalizeOp(Ops[0]);
1255 Changed = Op != Ops[0];
1256 Ops[0] = Op;
1257
1258 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1259 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1260 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1261 for (++i; NumVals; ++i, --NumVals) {
1262 SDOperand Op = LegalizeOp(Ops[i]);
1263 if (Op != Ops[i]) {
1264 Changed = true;
1265 Ops[i] = Op;
1266 }
1267 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001268 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001269
1270 if (HasInFlag) {
1271 Op = LegalizeOp(Ops.back());
1272 Changed |= Op != Ops.back();
1273 Ops.back() = Op;
1274 }
1275
1276 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001277 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001278
1279 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001280 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001281 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1282 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001283 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001284 case ISD::BR:
1285 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001286 // Ensure that libcalls are emitted before a branch.
1287 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1288 Tmp1 = LegalizeOp(Tmp1);
1289 LastCALLSEQ_END = DAG.getEntryNode();
1290
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001292 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001293 case ISD::BRIND:
1294 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1295 // Ensure that libcalls are emitted before a branch.
1296 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1297 Tmp1 = LegalizeOp(Tmp1);
1298 LastCALLSEQ_END = DAG.getEntryNode();
1299
1300 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1301 default: assert(0 && "Indirect target must be legal type (pointer)!");
1302 case Legal:
1303 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1304 break;
1305 }
1306 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1307 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001308 case ISD::BR_JT:
1309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1310 // Ensure that libcalls are emitted before a branch.
1311 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1312 Tmp1 = LegalizeOp(Tmp1);
1313 LastCALLSEQ_END = DAG.getEntryNode();
1314
1315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1316 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1317
1318 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1319 default: assert(0 && "This action is not supported yet!");
1320 case TargetLowering::Legal: break;
1321 case TargetLowering::Custom:
1322 Tmp1 = TLI.LowerOperation(Result, DAG);
1323 if (Tmp1.Val) Result = Tmp1;
1324 break;
1325 case TargetLowering::Expand: {
1326 SDOperand Chain = Result.getOperand(0);
1327 SDOperand Table = Result.getOperand(1);
1328 SDOperand Index = Result.getOperand(2);
1329
1330 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001331 MachineFunction &MF = DAG.getMachineFunction();
1332 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001333 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1334 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001335
1336 SDOperand LD;
1337 switch (EntrySize) {
1338 default: assert(0 && "Size of jump table not supported yet."); break;
1339 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1340 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1341 }
1342
1343 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001344 // For PIC, the sequence is:
1345 // BRIND(load(Jumptable + index) + RelocBase)
1346 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1347 SDOperand Reloc;
1348 if (TLI.usesGlobalOffsetTable())
1349 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1350 else
1351 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001352 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001353 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1354 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1355 } else {
1356 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1357 }
1358 }
1359 }
1360 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001361 case ISD::BRCOND:
1362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001363 // Ensure that libcalls are emitted before a return.
1364 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1365 Tmp1 = LegalizeOp(Tmp1);
1366 LastCALLSEQ_END = DAG.getEntryNode();
1367
Chris Lattner47e92232005-01-18 19:27:06 +00001368 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1369 case Expand: assert(0 && "It's impossible to expand bools");
1370 case Legal:
1371 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1372 break;
1373 case Promote:
1374 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001375
1376 // The top bits of the promoted condition are not necessarily zero, ensure
1377 // that the value is properly zero extended.
1378 if (!TLI.MaskedValueIsZero(Tmp2,
1379 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1380 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001381 break;
1382 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001383
1384 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001386
1387 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1388 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001389 case TargetLowering::Legal: break;
1390 case TargetLowering::Custom:
1391 Tmp1 = TLI.LowerOperation(Result, DAG);
1392 if (Tmp1.Val) Result = Tmp1;
1393 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001394 case TargetLowering::Expand:
1395 // Expand brcond's setcc into its constituent parts and create a BR_CC
1396 // Node.
1397 if (Tmp2.getOpcode() == ISD::SETCC) {
1398 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1399 Tmp2.getOperand(0), Tmp2.getOperand(1),
1400 Node->getOperand(2));
1401 } else {
1402 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1403 DAG.getCondCode(ISD::SETNE), Tmp2,
1404 DAG.getConstant(0, Tmp2.getValueType()),
1405 Node->getOperand(2));
1406 }
1407 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001408 }
1409 break;
1410 case ISD::BR_CC:
1411 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001412 // Ensure that libcalls are emitted before a branch.
1413 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1414 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001415 Tmp2 = Node->getOperand(2); // LHS
1416 Tmp3 = Node->getOperand(3); // RHS
1417 Tmp4 = Node->getOperand(1); // CC
1418
1419 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001420 LastCALLSEQ_END = DAG.getEntryNode();
1421
Nate Begeman750ac1b2006-02-01 07:19:44 +00001422 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1423 // the LHS is a legal SETCC itself. In this case, we need to compare
1424 // the result against zero to select between true and false values.
1425 if (Tmp3.Val == 0) {
1426 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1427 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001428 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001429
1430 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1431 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001432
Chris Lattner181b7a32005-12-17 23:46:46 +00001433 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1434 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001435 case TargetLowering::Legal: break;
1436 case TargetLowering::Custom:
1437 Tmp4 = TLI.LowerOperation(Result, DAG);
1438 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001439 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001440 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001441 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001442 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001443 LoadSDNode *LD = cast<LoadSDNode>(Node);
1444 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1445 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001446
Evan Cheng466685d2006-10-09 20:57:25 +00001447 ISD::LoadExtType ExtType = LD->getExtensionType();
1448 if (ExtType == ISD::NON_EXTLOAD) {
1449 MVT::ValueType VT = Node->getValueType(0);
1450 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1451 Tmp3 = Result.getValue(0);
1452 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001453
Evan Cheng466685d2006-10-09 20:57:25 +00001454 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1455 default: assert(0 && "This action is not supported yet!");
1456 case TargetLowering::Legal: break;
1457 case TargetLowering::Custom:
1458 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1459 if (Tmp1.Val) {
1460 Tmp3 = LegalizeOp(Tmp1);
1461 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001462 }
Evan Cheng466685d2006-10-09 20:57:25 +00001463 break;
1464 case TargetLowering::Promote: {
1465 // Only promote a load of vector type to another.
1466 assert(MVT::isVector(VT) && "Cannot promote this load!");
1467 // Change base type to a different vector type.
1468 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1469
1470 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1471 LD->getSrcValueOffset());
1472 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1473 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001474 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001475 }
Evan Cheng466685d2006-10-09 20:57:25 +00001476 }
1477 // Since loads produce two values, make sure to remember that we
1478 // legalized both of them.
1479 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1480 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1481 return Op.ResNo ? Tmp4 : Tmp3;
1482 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001483 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001484 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1485 default: assert(0 && "This action is not supported yet!");
1486 case TargetLowering::Promote:
1487 assert(SrcVT == MVT::i1 &&
1488 "Can only promote extending LOAD from i1 -> i8!");
1489 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1490 LD->getSrcValue(), LD->getSrcValueOffset(),
1491 MVT::i8);
1492 Tmp1 = Result.getValue(0);
1493 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001494 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001495 case TargetLowering::Custom:
1496 isCustom = true;
1497 // FALLTHROUGH
1498 case TargetLowering::Legal:
1499 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1500 Tmp1 = Result.getValue(0);
1501 Tmp2 = Result.getValue(1);
1502
1503 if (isCustom) {
1504 Tmp3 = TLI.LowerOperation(Result, DAG);
1505 if (Tmp3.Val) {
1506 Tmp1 = LegalizeOp(Tmp3);
1507 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1508 }
1509 }
1510 break;
1511 case TargetLowering::Expand:
1512 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1513 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1514 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1515 LD->getSrcValueOffset());
1516 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1517 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1518 Tmp2 = LegalizeOp(Load.getValue(1));
1519 break;
1520 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001521 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001522 // Turn the unsupported load into an EXTLOAD followed by an explicit
1523 // zero/sign extend inreg.
1524 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1525 Tmp1, Tmp2, LD->getSrcValue(),
1526 LD->getSrcValueOffset(), SrcVT);
1527 SDOperand ValRes;
1528 if (ExtType == ISD::SEXTLOAD)
1529 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1530 Result, DAG.getValueType(SrcVT));
1531 else
1532 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1533 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1534 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1535 break;
1536 }
1537 // Since loads produce two values, make sure to remember that we legalized
1538 // both of them.
1539 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1540 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1541 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001542 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001543 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001544 case ISD::EXTRACT_ELEMENT: {
1545 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1546 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001547 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001548 case Legal:
1549 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1550 // 1 -> Hi
1551 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1552 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1553 TLI.getShiftAmountTy()));
1554 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1555 } else {
1556 // 0 -> Lo
1557 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1558 Node->getOperand(0));
1559 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001560 break;
1561 case Expand:
1562 // Get both the low and high parts.
1563 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1564 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1565 Result = Tmp2; // 1 -> Hi
1566 else
1567 Result = Tmp1; // 0 -> Lo
1568 break;
1569 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001570 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001571 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001572
1573 case ISD::CopyToReg:
1574 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001575
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001576 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001577 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001578 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001579 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001580 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001582 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001583 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001585 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1587 Tmp3);
1588 } else {
1589 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001590 }
1591
1592 // Since this produces two values, make sure to remember that we legalized
1593 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001594 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001595 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001596 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001597 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001598 break;
1599
1600 case ISD::RET:
1601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001602
1603 // Ensure that libcalls are emitted before a return.
1604 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1605 Tmp1 = LegalizeOp(Tmp1);
1606 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001607
Chris Lattner3e928bb2005-01-07 07:47:09 +00001608 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001609 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001610 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001611 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001612 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001613 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001614 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001615 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001616 case Expand:
1617 if (Tmp2.getValueType() != MVT::Vector) {
1618 SDOperand Lo, Hi;
1619 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng13acce32006-12-11 19:27:14 +00001620 if (Hi.Val)
1621 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1622 else
1623 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001624 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001625 } else {
1626 SDNode *InVal = Tmp2.Val;
1627 unsigned NumElems =
1628 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1629 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1630
1631 // Figure out if there is a Packed type corresponding to this Vector
1632 // type. If so, convert to the packed type.
1633 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1634 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1635 // Turn this into a return of the packed type.
1636 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001638 } else if (NumElems == 1) {
1639 // Turn this into a return of the scalar type.
1640 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001641 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001642
1643 // FIXME: Returns of gcc generic vectors smaller than a legal type
1644 // should be returned in integer registers!
1645
Chris Lattnerf87324e2006-04-11 01:31:51 +00001646 // The scalarized value type may not be legal, e.g. it might require
1647 // promotion or expansion. Relegalize the return.
1648 Result = LegalizeOp(Result);
1649 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001650 // FIXME: Returns of gcc generic vectors larger than a legal vector
1651 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001652 SDOperand Lo, Hi;
1653 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001654 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001655 Result = LegalizeOp(Result);
1656 }
1657 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001658 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001659 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001660 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001661 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001662 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001663 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001664 }
1665 break;
1666 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001667 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001668 break;
1669 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001670 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001671 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001672 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001673 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1674 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001675 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001676 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001677 break;
1678 case Expand: {
1679 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001680 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1681 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001682 ExpandOp(Node->getOperand(i), Lo, Hi);
1683 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001684 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001685 if (Hi.Val) {
1686 NewValues.push_back(Hi);
1687 NewValues.push_back(Node->getOperand(i+1));
1688 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001689 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001690 }
1691 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001692 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001693 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001694
1695 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001696 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001697 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001698 Result = DAG.getNode(ISD::RET, MVT::Other,
1699 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001700 break;
1701 }
1702 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001703
Chris Lattner6862dbc2006-01-29 21:02:23 +00001704 if (Result.getOpcode() == ISD::RET) {
1705 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1706 default: assert(0 && "This action is not supported yet!");
1707 case TargetLowering::Legal: break;
1708 case TargetLowering::Custom:
1709 Tmp1 = TLI.LowerOperation(Result, DAG);
1710 if (Tmp1.Val) Result = Tmp1;
1711 break;
1712 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001713 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001714 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001715 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001716 StoreSDNode *ST = cast<StoreSDNode>(Node);
1717 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1718 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001719
Evan Cheng8b2794a2006-10-13 21:14:26 +00001720 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001721 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1722 // FIXME: We shouldn't do this for TargetConstantFP's.
1723 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1724 // to phase ordering between legalized code and the dag combiner. This
1725 // probably means that we need to integrate dag combiner and legalizer
1726 // together.
1727 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1728 if (CFP->getValueType(0) == MVT::f32) {
1729 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1730 } else {
1731 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1732 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1733 }
1734 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1735 ST->getSrcValueOffset());
1736 break;
1737 }
1738
Evan Cheng8b2794a2006-10-13 21:14:26 +00001739 switch (getTypeAction(ST->getStoredVT())) {
1740 case Legal: {
1741 Tmp3 = LegalizeOp(ST->getValue());
1742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1743 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001744
Evan Cheng8b2794a2006-10-13 21:14:26 +00001745 MVT::ValueType VT = Tmp3.getValueType();
1746 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1747 default: assert(0 && "This action is not supported yet!");
1748 case TargetLowering::Legal: break;
1749 case TargetLowering::Custom:
1750 Tmp1 = TLI.LowerOperation(Result, DAG);
1751 if (Tmp1.Val) Result = Tmp1;
1752 break;
1753 case TargetLowering::Promote:
1754 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1755 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1756 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1757 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1758 ST->getSrcValue(), ST->getSrcValueOffset());
1759 break;
1760 }
1761 break;
1762 }
1763 case Promote:
1764 // Truncate the value and store the result.
1765 Tmp3 = PromoteOp(ST->getValue());
1766 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1767 ST->getSrcValueOffset(), ST->getStoredVT());
1768 break;
1769
1770 case Expand:
1771 unsigned IncrementSize = 0;
1772 SDOperand Lo, Hi;
1773
1774 // If this is a vector type, then we have to calculate the increment as
1775 // the product of the element size in bytes, and the number of elements
1776 // in the high half of the vector.
1777 if (ST->getValue().getValueType() == MVT::Vector) {
1778 SDNode *InVal = ST->getValue().Val;
1779 unsigned NumElems =
1780 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1781 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1782
1783 // Figure out if there is a Packed type corresponding to this Vector
1784 // type. If so, convert to the packed type.
1785 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1786 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1787 // Turn this into a normal store of the packed type.
1788 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1789 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1790 ST->getSrcValueOffset());
1791 Result = LegalizeOp(Result);
1792 break;
1793 } else if (NumElems == 1) {
1794 // Turn this into a normal store of the scalar type.
1795 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1796 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1797 ST->getSrcValueOffset());
1798 // The scalarized value type may not be legal, e.g. it might require
1799 // promotion or expansion. Relegalize the scalar store.
1800 Result = LegalizeOp(Result);
1801 break;
1802 } else {
1803 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1804 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1805 }
1806 } else {
1807 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001808 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001809
1810 if (!TLI.isLittleEndian())
1811 std::swap(Lo, Hi);
1812 }
1813
1814 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1815 ST->getSrcValueOffset());
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001816
1817 if (Hi.Val == NULL) {
1818 // Must be int <-> float one-to-one expansion.
1819 Result = Lo;
1820 break;
1821 }
1822
Evan Cheng8b2794a2006-10-13 21:14:26 +00001823 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1824 getIntPtrConstant(IncrementSize));
1825 assert(isTypeLegal(Tmp2.getValueType()) &&
1826 "Pointers must be legal!");
1827 // FIXME: This sets the srcvalue of both halves to be the same, which is
1828 // wrong.
1829 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1830 ST->getSrcValueOffset());
1831 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1832 break;
1833 }
1834 } else {
1835 // Truncating store
1836 assert(isTypeLegal(ST->getValue().getValueType()) &&
1837 "Cannot handle illegal TRUNCSTORE yet!");
1838 Tmp3 = LegalizeOp(ST->getValue());
1839
1840 // The only promote case we handle is TRUNCSTORE:i1 X into
1841 // -> TRUNCSTORE:i8 (and X, 1)
1842 if (ST->getStoredVT() == MVT::i1 &&
1843 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1844 // Promote the bool to a mask then store.
1845 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1846 DAG.getConstant(1, Tmp3.getValueType()));
1847 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1848 ST->getSrcValueOffset(), MVT::i8);
1849 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1850 Tmp2 != ST->getBasePtr()) {
1851 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1852 ST->getOffset());
1853 }
1854
1855 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1856 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001857 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001858 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001859 case TargetLowering::Custom:
1860 Tmp1 = TLI.LowerOperation(Result, DAG);
1861 if (Tmp1.Val) Result = Tmp1;
1862 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001863 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001864 }
1865 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001866 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001867 case ISD::PCMARKER:
1868 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001870 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001871 case ISD::STACKSAVE:
1872 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001873 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1874 Tmp1 = Result.getValue(0);
1875 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001876
Chris Lattner140d53c2006-01-13 02:50:02 +00001877 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1878 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001879 case TargetLowering::Legal: break;
1880 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001881 Tmp3 = TLI.LowerOperation(Result, DAG);
1882 if (Tmp3.Val) {
1883 Tmp1 = LegalizeOp(Tmp3);
1884 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001885 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001886 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001887 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001888 // Expand to CopyFromReg if the target set
1889 // StackPointerRegisterToSaveRestore.
1890 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001891 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001892 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001893 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001894 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001895 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1896 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001897 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001898 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001899 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001900
1901 // Since stacksave produce two values, make sure to remember that we
1902 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001903 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1904 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1905 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001906
Chris Lattner140d53c2006-01-13 02:50:02 +00001907 case ISD::STACKRESTORE:
1908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001911
1912 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1913 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp1 = TLI.LowerOperation(Result, DAG);
1917 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001918 break;
1919 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001920 // Expand to CopyToReg if the target set
1921 // StackPointerRegisterToSaveRestore.
1922 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1923 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1924 } else {
1925 Result = Tmp1;
1926 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001927 break;
1928 }
1929 break;
1930
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001931 case ISD::READCYCLECOUNTER:
1932 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001933 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001934 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1935 Node->getValueType(0))) {
1936 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001937 case TargetLowering::Legal:
1938 Tmp1 = Result.getValue(0);
1939 Tmp2 = Result.getValue(1);
1940 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001941 case TargetLowering::Custom:
1942 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001943 Tmp1 = LegalizeOp(Result.getValue(0));
1944 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001945 break;
1946 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001947
1948 // Since rdcc produce two values, make sure to remember that we legalized
1949 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001950 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1951 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001952 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001953
Chris Lattner2ee743f2005-01-14 22:08:15 +00001954 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001955 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1956 case Expand: assert(0 && "It's impossible to expand bools");
1957 case Legal:
1958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1959 break;
1960 case Promote:
1961 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001962 // Make sure the condition is either zero or one.
1963 if (!TLI.MaskedValueIsZero(Tmp1,
1964 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1965 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001966 break;
1967 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001968 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001969 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001970
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001972
Nate Begemanb942a3d2005-08-23 04:29:48 +00001973 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001974 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001975 case TargetLowering::Legal: break;
1976 case TargetLowering::Custom: {
1977 Tmp1 = TLI.LowerOperation(Result, DAG);
1978 if (Tmp1.Val) Result = Tmp1;
1979 break;
1980 }
Nate Begeman9373a812005-08-10 20:51:12 +00001981 case TargetLowering::Expand:
1982 if (Tmp1.getOpcode() == ISD::SETCC) {
1983 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1984 Tmp2, Tmp3,
1985 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1986 } else {
1987 Result = DAG.getSelectCC(Tmp1,
1988 DAG.getConstant(0, Tmp1.getValueType()),
1989 Tmp2, Tmp3, ISD::SETNE);
1990 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001991 break;
1992 case TargetLowering::Promote: {
1993 MVT::ValueType NVT =
1994 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1995 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001996 if (MVT::isVector(Tmp2.getValueType())) {
1997 ExtOp = ISD::BIT_CONVERT;
1998 TruncOp = ISD::BIT_CONVERT;
1999 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002000 ExtOp = ISD::ANY_EXTEND;
2001 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002002 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002003 ExtOp = ISD::FP_EXTEND;
2004 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002005 }
2006 // Promote each of the values to the new type.
2007 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2008 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2009 // Perform the larger operation, then round down.
2010 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2011 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2012 break;
2013 }
2014 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002015 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002016 case ISD::SELECT_CC: {
2017 Tmp1 = Node->getOperand(0); // LHS
2018 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002019 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2020 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002021 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002022
Nate Begeman750ac1b2006-02-01 07:19:44 +00002023 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2024
2025 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2026 // the LHS is a legal SETCC itself. In this case, we need to compare
2027 // the result against zero to select between true and false values.
2028 if (Tmp2.Val == 0) {
2029 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2030 CC = DAG.getCondCode(ISD::SETNE);
2031 }
2032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2033
2034 // Everything is legal, see if we should expand this op or something.
2035 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2036 default: assert(0 && "This action is not supported yet!");
2037 case TargetLowering::Legal: break;
2038 case TargetLowering::Custom:
2039 Tmp1 = TLI.LowerOperation(Result, DAG);
2040 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002041 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002042 }
2043 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002044 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002045 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002046 Tmp1 = Node->getOperand(0);
2047 Tmp2 = Node->getOperand(1);
2048 Tmp3 = Node->getOperand(2);
2049 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2050
2051 // If we had to Expand the SetCC operands into a SELECT node, then it may
2052 // not always be possible to return a true LHS & RHS. In this case, just
2053 // return the value we legalized, returned in the LHS
2054 if (Tmp2.Val == 0) {
2055 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002056 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002057 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002058
Chris Lattner73e142f2006-01-30 22:43:50 +00002059 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002060 default: assert(0 && "Cannot handle this action for SETCC yet!");
2061 case TargetLowering::Custom:
2062 isCustom = true;
2063 // FALLTHROUGH.
2064 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002065 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002066 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002067 Tmp4 = TLI.LowerOperation(Result, DAG);
2068 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002069 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002070 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002071 case TargetLowering::Promote: {
2072 // First step, figure out the appropriate operation to use.
2073 // Allow SETCC to not be supported for all legal data types
2074 // Mostly this targets FP
2075 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002076 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002077
2078 // Scan for the appropriate larger type to use.
2079 while (1) {
2080 NewInTy = (MVT::ValueType)(NewInTy+1);
2081
2082 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2083 "Fell off of the edge of the integer world");
2084 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2085 "Fell off of the edge of the floating point world");
2086
2087 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002088 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002089 break;
2090 }
2091 if (MVT::isInteger(NewInTy))
2092 assert(0 && "Cannot promote Legal Integer SETCC yet");
2093 else {
2094 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2095 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2096 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002097 Tmp1 = LegalizeOp(Tmp1);
2098 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002099 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002100 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002101 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002102 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002103 case TargetLowering::Expand:
2104 // Expand a setcc node into a select_cc of the same condition, lhs, and
2105 // rhs that selects between const 1 (true) and const 0 (false).
2106 MVT::ValueType VT = Node->getValueType(0);
2107 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2108 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002109 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002110 break;
2111 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002112 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002113 case ISD::MEMSET:
2114 case ISD::MEMCPY:
2115 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002116 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002117 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2118
2119 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2120 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2121 case Expand: assert(0 && "Cannot expand a byte!");
2122 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002123 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002124 break;
2125 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002126 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002127 break;
2128 }
2129 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002130 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002131 }
Chris Lattner272455b2005-02-02 03:44:41 +00002132
2133 SDOperand Tmp4;
2134 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002135 case Expand: {
2136 // Length is too big, just take the lo-part of the length.
2137 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002138 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002139 break;
2140 }
Chris Lattnere5605212005-01-28 22:29:18 +00002141 case Legal:
2142 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002143 break;
2144 case Promote:
2145 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002146 break;
2147 }
2148
2149 SDOperand Tmp5;
2150 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2151 case Expand: assert(0 && "Cannot expand this yet!");
2152 case Legal:
2153 Tmp5 = LegalizeOp(Node->getOperand(4));
2154 break;
2155 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002156 Tmp5 = PromoteOp(Node->getOperand(4));
2157 break;
2158 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002159
2160 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2161 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002162 case TargetLowering::Custom:
2163 isCustom = true;
2164 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002165 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002166 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002167 if (isCustom) {
2168 Tmp1 = TLI.LowerOperation(Result, DAG);
2169 if (Tmp1.Val) Result = Tmp1;
2170 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002171 break;
2172 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002173 // Otherwise, the target does not support this operation. Lower the
2174 // operation to an explicit libcall as appropriate.
2175 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002176 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002177 TargetLowering::ArgListTy Args;
2178 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002179
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002180 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002181 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002182 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002183 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00002184 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002185 // Extend the (previously legalized) ubyte argument to be an int value
2186 // for the call.
2187 if (Tmp3.getValueType() > MVT::i32)
2188 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2189 else
2190 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencer47857812006-12-31 05:55:36 +00002191 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002192 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00002193 Args.push_back(Entry);
2194 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2195 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002196
2197 FnName = "memset";
2198 } else if (Node->getOpcode() == ISD::MEMCPY ||
2199 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002200 Entry.Ty = IntPtrTy;
2201 Entry.isSigned = false; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002202 Entry.Node = Tmp2; Args.push_back(Entry);
2203 Entry.Node = Tmp3; Args.push_back(Entry);
2204 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002205 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2206 } else {
2207 assert(0 && "Unknown op!");
2208 }
Chris Lattner45982da2005-05-12 16:53:42 +00002209
Chris Lattnere1bd8222005-01-11 05:57:22 +00002210 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002211 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002212 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002213 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002214 break;
2215 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002216 }
2217 break;
2218 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002219
Chris Lattner5b359c62005-04-02 04:00:59 +00002220 case ISD::SHL_PARTS:
2221 case ISD::SRA_PARTS:
2222 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002223 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002224 bool Changed = false;
2225 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2226 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2227 Changed |= Ops.back() != Node->getOperand(i);
2228 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002229 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002230 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002231
Evan Cheng05a2d562006-01-09 18:31:59 +00002232 switch (TLI.getOperationAction(Node->getOpcode(),
2233 Node->getValueType(0))) {
2234 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002235 case TargetLowering::Legal: break;
2236 case TargetLowering::Custom:
2237 Tmp1 = TLI.LowerOperation(Result, DAG);
2238 if (Tmp1.Val) {
2239 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002240 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002241 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002242 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2243 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002244 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002245 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002246 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002247 return RetVal;
2248 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002249 break;
2250 }
2251
Chris Lattner2c8086f2005-04-02 05:00:07 +00002252 // Since these produce multiple values, make sure to remember that we
2253 // legalized all of them.
2254 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2255 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2256 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002257 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002258
2259 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002260 case ISD::ADD:
2261 case ISD::SUB:
2262 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002263 case ISD::MULHS:
2264 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002265 case ISD::UDIV:
2266 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002267 case ISD::AND:
2268 case ISD::OR:
2269 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002270 case ISD::SHL:
2271 case ISD::SRL:
2272 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002273 case ISD::FADD:
2274 case ISD::FSUB:
2275 case ISD::FMUL:
2276 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002277 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002278 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2279 case Expand: assert(0 && "Not possible");
2280 case Legal:
2281 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2282 break;
2283 case Promote:
2284 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2285 break;
2286 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002287
2288 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002289
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002290 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002291 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002292 case TargetLowering::Legal: break;
2293 case TargetLowering::Custom:
2294 Tmp1 = TLI.LowerOperation(Result, DAG);
2295 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002296 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002297 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002298 if (Node->getValueType(0) == MVT::i32) {
2299 switch (Node->getOpcode()) {
2300 default: assert(0 && "Do not know how to expand this integer BinOp!");
2301 case ISD::UDIV:
2302 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002303 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2304 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002305 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002306 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002307 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002308 };
2309 break;
2310 }
2311
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002312 assert(MVT::isVector(Node->getValueType(0)) &&
2313 "Cannot expand this binary operator!");
2314 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002315 SmallVector<SDOperand, 8> Ops;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002316 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2317 MVT::ValueType PtrVT = TLI.getPointerTy();
2318 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2319 i != e; ++i) {
2320 SDOperand Idx = DAG.getConstant(i, PtrVT);
2321 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2322 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2323 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2324 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002325 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2326 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002327 break;
2328 }
Evan Chengcc987612006-04-12 21:20:24 +00002329 case TargetLowering::Promote: {
2330 switch (Node->getOpcode()) {
2331 default: assert(0 && "Do not know how to promote this BinOp!");
2332 case ISD::AND:
2333 case ISD::OR:
2334 case ISD::XOR: {
2335 MVT::ValueType OVT = Node->getValueType(0);
2336 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2337 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2338 // Bit convert each of the values to the new type.
2339 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2340 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2341 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2342 // Bit convert the result back the original type.
2343 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2344 break;
2345 }
2346 }
2347 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002348 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002349 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002350
2351 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2352 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2353 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2354 case Expand: assert(0 && "Not possible");
2355 case Legal:
2356 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2357 break;
2358 case Promote:
2359 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2360 break;
2361 }
2362
2363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2364
2365 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2366 default: assert(0 && "Operation not supported");
2367 case TargetLowering::Custom:
2368 Tmp1 = TLI.LowerOperation(Result, DAG);
2369 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002370 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002371 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002372 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002373 // If this target supports fabs/fneg natively and select is cheap,
2374 // do this efficiently.
2375 if (!TLI.isSelectExpensive() &&
2376 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2377 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002378 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002379 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002380 // Get the sign bit of the RHS.
2381 MVT::ValueType IVT =
2382 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2383 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2384 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2385 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2386 // Get the absolute value of the result.
2387 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2388 // Select between the nabs and abs value based on the sign bit of
2389 // the input.
2390 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2391 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2392 AbsVal),
2393 AbsVal);
2394 Result = LegalizeOp(Result);
2395 break;
2396 }
2397
2398 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002399 MVT::ValueType NVT =
2400 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2401 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2402 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2403 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002404 break;
2405 }
Evan Cheng912095b2007-01-04 21:56:39 +00002406 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002407 break;
2408
Nate Begeman551bf3f2006-02-17 05:43:56 +00002409 case ISD::ADDC:
2410 case ISD::SUBC:
2411 Tmp1 = LegalizeOp(Node->getOperand(0));
2412 Tmp2 = LegalizeOp(Node->getOperand(1));
2413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2414 // Since this produces two values, make sure to remember that we legalized
2415 // both of them.
2416 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2417 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2418 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002419
Nate Begeman551bf3f2006-02-17 05:43:56 +00002420 case ISD::ADDE:
2421 case ISD::SUBE:
2422 Tmp1 = LegalizeOp(Node->getOperand(0));
2423 Tmp2 = LegalizeOp(Node->getOperand(1));
2424 Tmp3 = LegalizeOp(Node->getOperand(2));
2425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2426 // Since this produces two values, make sure to remember that we legalized
2427 // both of them.
2428 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2429 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2430 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002431
Nate Begeman419f8b62005-10-18 00:27:41 +00002432 case ISD::BUILD_PAIR: {
2433 MVT::ValueType PairTy = Node->getValueType(0);
2434 // TODO: handle the case where the Lo and Hi operands are not of legal type
2435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2436 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2437 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002438 case TargetLowering::Promote:
2439 case TargetLowering::Custom:
2440 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002441 case TargetLowering::Legal:
2442 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2443 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2444 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002445 case TargetLowering::Expand:
2446 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2447 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2448 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2449 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2450 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002451 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002452 break;
2453 }
2454 break;
2455 }
2456
Nate Begemanc105e192005-04-06 00:23:54 +00002457 case ISD::UREM:
2458 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002459 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002460 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2461 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002462
Nate Begemanc105e192005-04-06 00:23:54 +00002463 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002464 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2465 case TargetLowering::Custom:
2466 isCustom = true;
2467 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002468 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002469 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002470 if (isCustom) {
2471 Tmp1 = TLI.LowerOperation(Result, DAG);
2472 if (Tmp1.Val) Result = Tmp1;
2473 }
Nate Begemanc105e192005-04-06 00:23:54 +00002474 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002475 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002476 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002477 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002478 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002479 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2480 TargetLowering::Legal) {
2481 // X % Y -> X-X/Y*Y
2482 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002483 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002484 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2485 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2486 } else {
2487 assert(Node->getValueType(0) == MVT::i32 &&
2488 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002489 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2490 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002491 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002492 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002493 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002494 } else {
2495 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002496 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2497 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002498 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002499 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2500 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002501 }
2502 break;
2503 }
2504 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002505 case ISD::VAARG: {
2506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2508
Chris Lattner5c62f332006-01-28 07:42:08 +00002509 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002510 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2511 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002512 case TargetLowering::Custom:
2513 isCustom = true;
2514 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002515 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2517 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002518 Tmp1 = Result.getValue(1);
2519
2520 if (isCustom) {
2521 Tmp2 = TLI.LowerOperation(Result, DAG);
2522 if (Tmp2.Val) {
2523 Result = LegalizeOp(Tmp2);
2524 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2525 }
2526 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002527 break;
2528 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002529 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002530 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002531 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002532 // Increment the pointer, VAList, to the next vaarg
2533 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2534 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2535 TLI.getPointerTy()));
2536 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002537 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2538 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002539 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002540 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002541 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002542 Result = LegalizeOp(Result);
2543 break;
2544 }
2545 }
2546 // Since VAARG produces two values, make sure to remember that we
2547 // legalized both of them.
2548 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002549 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2550 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002551 }
2552
2553 case ISD::VACOPY:
2554 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2555 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2556 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2557
2558 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2559 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002560 case TargetLowering::Custom:
2561 isCustom = true;
2562 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002563 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2565 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002566 if (isCustom) {
2567 Tmp1 = TLI.LowerOperation(Result, DAG);
2568 if (Tmp1.Val) Result = Tmp1;
2569 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002570 break;
2571 case TargetLowering::Expand:
2572 // This defaults to loading a pointer from the input and storing it to the
2573 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002574 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002575 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002576 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2577 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002578 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2579 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002580 break;
2581 }
2582 break;
2583
2584 case ISD::VAEND:
2585 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2586 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2587
2588 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2589 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002590 case TargetLowering::Custom:
2591 isCustom = true;
2592 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002593 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002594 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002595 if (isCustom) {
2596 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2597 if (Tmp1.Val) Result = Tmp1;
2598 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002599 break;
2600 case TargetLowering::Expand:
2601 Result = Tmp1; // Default to a no-op, return the chain
2602 break;
2603 }
2604 break;
2605
2606 case ISD::VASTART:
2607 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2609
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2611
Nate Begemanacc398c2006-01-25 18:21:52 +00002612 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2613 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002614 case TargetLowering::Legal: break;
2615 case TargetLowering::Custom:
2616 Tmp1 = TLI.LowerOperation(Result, DAG);
2617 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002618 break;
2619 }
2620 break;
2621
Nate Begeman35ef9132006-01-11 21:21:00 +00002622 case ISD::ROTL:
2623 case ISD::ROTR:
2624 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2625 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002626
2627 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2628 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002630 break;
2631
Nate Begemand88fc032006-01-14 03:14:10 +00002632 case ISD::BSWAP:
2633 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2634 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002635 case TargetLowering::Custom:
2636 assert(0 && "Cannot custom legalize this yet!");
2637 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002638 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002639 break;
2640 case TargetLowering::Promote: {
2641 MVT::ValueType OVT = Tmp1.getValueType();
2642 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2643 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002644
Chris Lattner456a93a2006-01-28 07:39:30 +00002645 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2646 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2647 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2648 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2649 break;
2650 }
2651 case TargetLowering::Expand:
2652 Result = ExpandBSWAP(Tmp1);
2653 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002654 }
2655 break;
2656
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002657 case ISD::CTPOP:
2658 case ISD::CTTZ:
2659 case ISD::CTLZ:
2660 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2661 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002662 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002663 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002664 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002665 break;
2666 case TargetLowering::Promote: {
2667 MVT::ValueType OVT = Tmp1.getValueType();
2668 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002669
2670 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002671 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2672 // Perform the larger operation, then subtract if needed.
2673 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002674 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002675 case ISD::CTPOP:
2676 Result = Tmp1;
2677 break;
2678 case ISD::CTTZ:
2679 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002680 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2681 DAG.getConstant(getSizeInBits(NVT), NVT),
2682 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002683 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002684 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2685 break;
2686 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002687 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002688 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2689 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002690 getSizeInBits(OVT), NVT));
2691 break;
2692 }
2693 break;
2694 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002695 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002696 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002697 break;
2698 }
2699 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002700
Chris Lattner2c8086f2005-04-02 05:00:07 +00002701 // Unary operators
2702 case ISD::FABS:
2703 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002704 case ISD::FSQRT:
2705 case ISD::FSIN:
2706 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002707 Tmp1 = LegalizeOp(Node->getOperand(0));
2708 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002709 case TargetLowering::Promote:
2710 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002711 isCustom = true;
2712 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002713 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002714 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002715 if (isCustom) {
2716 Tmp1 = TLI.LowerOperation(Result, DAG);
2717 if (Tmp1.Val) Result = Tmp1;
2718 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002719 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002720 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002721 switch (Node->getOpcode()) {
2722 default: assert(0 && "Unreachable!");
2723 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002724 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2725 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002726 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002727 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002728 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002729 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2730 MVT::ValueType VT = Node->getValueType(0);
2731 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002732 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002733 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2734 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002735 break;
2736 }
2737 case ISD::FSQRT:
2738 case ISD::FSIN:
2739 case ISD::FCOS: {
2740 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002741 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002742 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002743 case ISD::FSQRT:
2744 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2745 break;
2746 case ISD::FSIN:
2747 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2748 break;
2749 case ISD::FCOS:
2750 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2751 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002752 default: assert(0 && "Unreachable!");
2753 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002754 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002755 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2756 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002757 break;
2758 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002759 }
2760 break;
2761 }
2762 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002763 case ISD::FPOWI: {
2764 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002765 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2766 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002767 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002768 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2769 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002770 break;
2771 }
Chris Lattner35481892005-12-23 00:16:34 +00002772 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002773 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002774 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002775 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002776 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2777 Node->getOperand(0).getValueType())) {
2778 default: assert(0 && "Unknown operation action!");
2779 case TargetLowering::Expand:
2780 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2781 break;
2782 case TargetLowering::Legal:
2783 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002784 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002785 break;
2786 }
2787 }
2788 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002789 case ISD::VBIT_CONVERT: {
2790 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2791 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2792
2793 // The input has to be a vector type, we have to either scalarize it, pack
2794 // it, or convert it based on whether the input vector type is legal.
2795 SDNode *InVal = Node->getOperand(0).Val;
2796 unsigned NumElems =
2797 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2798 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2799
2800 // Figure out if there is a Packed type corresponding to this Vector
2801 // type. If so, convert to the packed type.
2802 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2803 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2804 // Turn this into a bit convert of the packed input.
2805 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2806 PackVectorOp(Node->getOperand(0), TVT));
2807 break;
2808 } else if (NumElems == 1) {
2809 // Turn this into a bit convert of the scalar input.
2810 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2811 PackVectorOp(Node->getOperand(0), EVT));
2812 break;
2813 } else {
2814 // FIXME: UNIMP! Store then reload
2815 assert(0 && "Cast from unsupported vector type not implemented yet!");
2816 }
2817 }
2818
Chris Lattner2c8086f2005-04-02 05:00:07 +00002819 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002820 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002821 case ISD::UINT_TO_FP: {
2822 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002823 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2824 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002825 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002826 Node->getOperand(0).getValueType())) {
2827 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002828 case TargetLowering::Custom:
2829 isCustom = true;
2830 // FALLTHROUGH
2831 case TargetLowering::Legal:
2832 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002833 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002834 if (isCustom) {
2835 Tmp1 = TLI.LowerOperation(Result, DAG);
2836 if (Tmp1.Val) Result = Tmp1;
2837 }
2838 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002839 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002840 Result = ExpandLegalINT_TO_FP(isSigned,
2841 LegalizeOp(Node->getOperand(0)),
2842 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002843 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002844 case TargetLowering::Promote:
2845 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2846 Node->getValueType(0),
2847 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002848 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002849 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002850 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002851 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002852 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2853 Node->getValueType(0), Node->getOperand(0));
2854 break;
2855 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002856 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002857 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002858 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2859 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002860 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002861 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2862 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002863 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002864 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2865 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002866 break;
2867 }
2868 break;
2869 }
2870 case ISD::TRUNCATE:
2871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2872 case Legal:
2873 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002875 break;
2876 case Expand:
2877 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2878
2879 // Since the result is legal, we should just be able to truncate the low
2880 // part of the source.
2881 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2882 break;
2883 case Promote:
2884 Result = PromoteOp(Node->getOperand(0));
2885 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2886 break;
2887 }
2888 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002889
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002890 case ISD::FP_TO_SINT:
2891 case ISD::FP_TO_UINT:
2892 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2893 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002894 Tmp1 = LegalizeOp(Node->getOperand(0));
2895
Chris Lattner1618beb2005-07-29 00:11:56 +00002896 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2897 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002898 case TargetLowering::Custom:
2899 isCustom = true;
2900 // FALLTHROUGH
2901 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002902 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002903 if (isCustom) {
2904 Tmp1 = TLI.LowerOperation(Result, DAG);
2905 if (Tmp1.Val) Result = Tmp1;
2906 }
2907 break;
2908 case TargetLowering::Promote:
2909 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2910 Node->getOpcode() == ISD::FP_TO_SINT);
2911 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002912 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002913 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2914 SDOperand True, False;
2915 MVT::ValueType VT = Node->getOperand(0).getValueType();
2916 MVT::ValueType NVT = Node->getValueType(0);
2917 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2918 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2919 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2920 Node->getOperand(0), Tmp2, ISD::SETLT);
2921 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2922 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002923 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002924 Tmp2));
2925 False = DAG.getNode(ISD::XOR, NVT, False,
2926 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002927 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2928 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002929 } else {
2930 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2931 }
2932 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002933 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002934 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002935 case Expand: {
2936 // Convert f32 / f64 to i32 / i64.
2937 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00002938 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00002939 switch (Node->getOpcode()) {
2940 case ISD::FP_TO_SINT:
2941 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002942 LC = (VT == MVT::i32)
2943 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002944 else
Evan Cheng56966222007-01-12 02:11:51 +00002945 LC = (VT == MVT::i32)
2946 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002947 break;
2948 case ISD::FP_TO_UINT:
2949 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002950 LC = (VT == MVT::i32)
2951 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002952 else
Evan Cheng56966222007-01-12 02:11:51 +00002953 LC = (VT == MVT::i32)
2954 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002955 break;
2956 default: assert(0 && "Unreachable!");
2957 }
2958 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002959 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2960 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00002961 break;
2962 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002963 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002964 Tmp1 = PromoteOp(Node->getOperand(0));
2965 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2966 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002967 break;
2968 }
2969 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002970
Chris Lattner13c78e22005-09-02 00:18:10 +00002971 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002972 case ISD::ZERO_EXTEND:
2973 case ISD::SIGN_EXTEND:
2974 case ISD::FP_EXTEND:
2975 case ISD::FP_ROUND:
2976 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002977 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002978 case Legal:
2979 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002980 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002981 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002982 case Promote:
2983 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002984 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002985 Tmp1 = PromoteOp(Node->getOperand(0));
2986 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002987 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002988 case ISD::ZERO_EXTEND:
2989 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002990 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002991 Result = DAG.getZeroExtendInReg(Result,
2992 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002993 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002994 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002995 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002996 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002997 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002998 Result,
2999 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003000 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003001 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003002 Result = PromoteOp(Node->getOperand(0));
3003 if (Result.getValueType() != Op.getValueType())
3004 // Dynamically dead while we have only 2 FP types.
3005 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3006 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003007 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003008 Result = PromoteOp(Node->getOperand(0));
3009 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3010 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003011 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003012 }
3013 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003014 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003015 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003016 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003017 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003018
3019 // If this operation is not supported, convert it to a shl/shr or load/store
3020 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003021 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3022 default: assert(0 && "This action not supported for this op yet!");
3023 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003025 break;
3026 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003027 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003028 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003029 // NOTE: we could fall back on load/store here too for targets without
3030 // SAR. However, it is doubtful that any exist.
3031 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3032 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003033 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003034 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3035 Node->getOperand(0), ShiftCst);
3036 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3037 Result, ShiftCst);
3038 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003039 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003040 // EXTLOAD pair, targetting a temporary location (a stack slot).
3041
3042 // NOTE: there is a choice here between constantly creating new stack
3043 // slots and always reusing the same one. We currently always create
3044 // new ones, as reuse may inhibit scheduling.
3045 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00003046 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00003047 unsigned Align = TLI.getTargetData()->getTypeAlignmentPref(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003048 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003049 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00003050 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3051 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003052 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3053 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003054 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003055 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003056 } else {
3057 assert(0 && "Unknown op");
3058 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003059 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003060 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003061 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003062 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003063 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003064
Chris Lattner4ddd2832006-04-08 04:13:17 +00003065 assert(Result.getValueType() == Op.getValueType() &&
3066 "Bad legalization!");
3067
Chris Lattner456a93a2006-01-28 07:39:30 +00003068 // Make sure that the generated code is itself legal.
3069 if (Result != Op)
3070 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003071
Chris Lattner45982da2005-05-12 16:53:42 +00003072 // Note that LegalizeOp may be reentered even from single-use nodes, which
3073 // means that we always must cache transformed nodes.
3074 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 return Result;
3076}
3077
Chris Lattner8b6fa222005-01-15 22:16:26 +00003078/// PromoteOp - Given an operation that produces a value in an invalid type,
3079/// promote it to compute the value into a larger type. The produced value will
3080/// have the correct bits for the low portion of the register, but no guarantee
3081/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003082SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3083 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003084 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003085 assert(getTypeAction(VT) == Promote &&
3086 "Caller should expand or legalize operands that are not promotable!");
3087 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3088 "Cannot promote to smaller type!");
3089
Chris Lattner03c85462005-01-15 05:21:40 +00003090 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003091 SDOperand Result;
3092 SDNode *Node = Op.Val;
3093
Chris Lattner6fdcb252005-09-02 20:32:45 +00003094 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3095 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003096
Chris Lattner03c85462005-01-15 05:21:40 +00003097 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003098 case ISD::CopyFromReg:
3099 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003100 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003101#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00003102 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003103#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003104 assert(0 && "Do not know how to promote this operator!");
3105 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003106 case ISD::UNDEF:
3107 Result = DAG.getNode(ISD::UNDEF, NVT);
3108 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003109 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003110 if (VT != MVT::i1)
3111 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3112 else
3113 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003114 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3115 break;
3116 case ISD::ConstantFP:
3117 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3118 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3119 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003120
Chris Lattner82fbfb62005-01-18 02:59:52 +00003121 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003122 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003123 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3124 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003125 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003126
Chris Lattner03c85462005-01-15 05:21:40 +00003127 case ISD::TRUNCATE:
3128 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3129 case Legal:
3130 Result = LegalizeOp(Node->getOperand(0));
3131 assert(Result.getValueType() >= NVT &&
3132 "This truncation doesn't make sense!");
3133 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3134 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3135 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003136 case Promote:
3137 // The truncation is not required, because we don't guarantee anything
3138 // about high bits anyway.
3139 Result = PromoteOp(Node->getOperand(0));
3140 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003141 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003142 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3143 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003144 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003145 }
3146 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003147 case ISD::SIGN_EXTEND:
3148 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003149 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003150 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3151 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3152 case Legal:
3153 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003155 break;
3156 case Promote:
3157 // Promote the reg if it's smaller.
3158 Result = PromoteOp(Node->getOperand(0));
3159 // The high bits are not guaranteed to be anything. Insert an extend.
3160 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003161 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003162 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003163 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003164 Result = DAG.getZeroExtendInReg(Result,
3165 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003166 break;
3167 }
3168 break;
Chris Lattner35481892005-12-23 00:16:34 +00003169 case ISD::BIT_CONVERT:
3170 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3171 Result = PromoteOp(Result);
3172 break;
3173
Chris Lattner8b6fa222005-01-15 22:16:26 +00003174 case ISD::FP_EXTEND:
3175 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3176 case ISD::FP_ROUND:
3177 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3178 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3179 case Promote: assert(0 && "Unreachable with 2 FP types!");
3180 case Legal:
3181 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003182 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003183 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003184 break;
3185 }
3186 break;
3187
3188 case ISD::SINT_TO_FP:
3189 case ISD::UINT_TO_FP:
3190 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3191 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003192 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003193 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003194 break;
3195
3196 case Promote:
3197 Result = PromoteOp(Node->getOperand(0));
3198 if (Node->getOpcode() == ISD::SINT_TO_FP)
3199 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003200 Result,
3201 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003202 else
Chris Lattner23993562005-04-13 02:38:47 +00003203 Result = DAG.getZeroExtendInReg(Result,
3204 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003205 // No extra round required here.
3206 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003207 break;
3208 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003209 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3210 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003211 // Round if we cannot tolerate excess precision.
3212 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003213 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3214 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003215 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003216 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003217 break;
3218
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003219 case ISD::SIGN_EXTEND_INREG:
3220 Result = PromoteOp(Node->getOperand(0));
3221 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3222 Node->getOperand(1));
3223 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003224 case ISD::FP_TO_SINT:
3225 case ISD::FP_TO_UINT:
3226 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3227 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003228 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003229 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003230 break;
3231 case Promote:
3232 // The input result is prerounded, so we don't have to do anything
3233 // special.
3234 Tmp1 = PromoteOp(Node->getOperand(0));
3235 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003236 }
Nate Begemand2558e32005-08-14 01:20:53 +00003237 // If we're promoting a UINT to a larger size, check to see if the new node
3238 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3239 // we can use that instead. This allows us to generate better code for
3240 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3241 // legal, such as PowerPC.
3242 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003243 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003244 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3245 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003246 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3247 } else {
3248 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3249 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003250 break;
3251
Chris Lattner2c8086f2005-04-02 05:00:07 +00003252 case ISD::FABS:
3253 case ISD::FNEG:
3254 Tmp1 = PromoteOp(Node->getOperand(0));
3255 assert(Tmp1.getValueType() == NVT);
3256 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3257 // NOTE: we do not have to do any extra rounding here for
3258 // NoExcessFPPrecision, because we know the input will have the appropriate
3259 // precision, and these operations don't modify precision at all.
3260 break;
3261
Chris Lattnerda6ba872005-04-28 21:44:33 +00003262 case ISD::FSQRT:
3263 case ISD::FSIN:
3264 case ISD::FCOS:
3265 Tmp1 = PromoteOp(Node->getOperand(0));
3266 assert(Tmp1.getValueType() == NVT);
3267 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003268 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003269 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3270 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003271 break;
3272
Chris Lattner03c85462005-01-15 05:21:40 +00003273 case ISD::AND:
3274 case ISD::OR:
3275 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003276 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003277 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003278 case ISD::MUL:
3279 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003280 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003281 // that too is okay if they are integer operations.
3282 Tmp1 = PromoteOp(Node->getOperand(0));
3283 Tmp2 = PromoteOp(Node->getOperand(1));
3284 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3285 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003286 break;
3287 case ISD::FADD:
3288 case ISD::FSUB:
3289 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003290 Tmp1 = PromoteOp(Node->getOperand(0));
3291 Tmp2 = PromoteOp(Node->getOperand(1));
3292 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3293 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3294
3295 // Floating point operations will give excess precision that we may not be
3296 // able to tolerate. If we DO allow excess precision, just leave it,
3297 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003298 // FIXME: Why would we need to round FP ops more than integer ones?
3299 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003300 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003301 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3302 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003303 break;
3304
Chris Lattner8b6fa222005-01-15 22:16:26 +00003305 case ISD::SDIV:
3306 case ISD::SREM:
3307 // These operators require that their input be sign extended.
3308 Tmp1 = PromoteOp(Node->getOperand(0));
3309 Tmp2 = PromoteOp(Node->getOperand(1));
3310 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003311 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3312 DAG.getValueType(VT));
3313 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3314 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003315 }
3316 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3317
3318 // Perform FP_ROUND: this is probably overly pessimistic.
3319 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003320 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3321 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003322 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003323 case ISD::FDIV:
3324 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003325 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003326 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003327 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3328 case Legal:
3329 Tmp1 = LegalizeOp(Node->getOperand(0));
3330 break;
3331 case Promote:
3332 Tmp1 = PromoteOp(Node->getOperand(0));
3333 break;
3334 case Expand:
3335 assert(0 && "not implemented");
3336 }
3337 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3338 case Legal:
3339 Tmp2 = LegalizeOp(Node->getOperand(1));
3340 break;
3341 case Promote:
3342 Tmp2 = PromoteOp(Node->getOperand(1));
3343 break;
3344 case Expand:
3345 assert(0 && "not implemented");
3346 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003347 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3348
3349 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003350 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003351 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3352 DAG.getValueType(VT));
3353 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003354
3355 case ISD::UDIV:
3356 case ISD::UREM:
3357 // These operators require that their input be zero extended.
3358 Tmp1 = PromoteOp(Node->getOperand(0));
3359 Tmp2 = PromoteOp(Node->getOperand(1));
3360 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003361 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3362 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003363 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3364 break;
3365
3366 case ISD::SHL:
3367 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003368 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003369 break;
3370 case ISD::SRA:
3371 // The input value must be properly sign extended.
3372 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003373 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3374 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003375 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003376 break;
3377 case ISD::SRL:
3378 // The input value must be properly zero extended.
3379 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003380 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003381 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003382 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003383
3384 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003385 Tmp1 = Node->getOperand(0); // Get the chain.
3386 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003387 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3388 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3389 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3390 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003391 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003392 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003393 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003394 // Increment the pointer, VAList, to the next vaarg
3395 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3396 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3397 TLI.getPointerTy()));
3398 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003399 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3400 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003401 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003402 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003403 }
3404 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003405 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003406 break;
3407
Evan Cheng466685d2006-10-09 20:57:25 +00003408 case ISD::LOAD: {
3409 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003410 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3411 ? ISD::EXTLOAD : LD->getExtensionType();
3412 Result = DAG.getExtLoad(ExtType, NVT,
3413 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003414 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003415 LD->getLoadedVT());
Chris Lattner03c85462005-01-15 05:21:40 +00003416 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003417 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003418 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003419 }
Chris Lattner03c85462005-01-15 05:21:40 +00003420 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003421 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3422 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003423 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003424 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003425 case ISD::SELECT_CC:
3426 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3427 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3428 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003429 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003430 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003431 case ISD::BSWAP:
3432 Tmp1 = Node->getOperand(0);
3433 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3434 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3435 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3436 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3437 TLI.getShiftAmountTy()));
3438 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003439 case ISD::CTPOP:
3440 case ISD::CTTZ:
3441 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003442 // Zero extend the argument
3443 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003444 // Perform the larger operation, then subtract if needed.
3445 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003446 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003447 case ISD::CTPOP:
3448 Result = Tmp1;
3449 break;
3450 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003451 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003452 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003453 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003454 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003455 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003456 break;
3457 case ISD::CTLZ:
3458 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003459 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3460 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003461 getSizeInBits(VT), NVT));
3462 break;
3463 }
3464 break;
Chris Lattner15972212006-03-31 17:55:51 +00003465 case ISD::VEXTRACT_VECTOR_ELT:
3466 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3467 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003468 case ISD::EXTRACT_VECTOR_ELT:
3469 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3470 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003471 }
3472
3473 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003474
3475 // Make sure the result is itself legal.
3476 Result = LegalizeOp(Result);
3477
3478 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003479 AddPromotedOperand(Op, Result);
3480 return Result;
3481}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003482
Chris Lattner15972212006-03-31 17:55:51 +00003483/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3484/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3485/// on the vector type. The return type of this matches the element type of the
3486/// vector, which may not be legal for the target.
3487SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3488 // We know that operand #0 is the Vec vector. If the index is a constant
3489 // or if the invec is a supported hardware type, we can use it. Otherwise,
3490 // lower to a store then an indexed load.
3491 SDOperand Vec = Op.getOperand(0);
3492 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3493
3494 SDNode *InVal = Vec.Val;
3495 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3496 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3497
3498 // Figure out if there is a Packed type corresponding to this Vector
3499 // type. If so, convert to the packed type.
3500 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3501 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3502 // Turn this into a packed extract_vector_elt operation.
3503 Vec = PackVectorOp(Vec, TVT);
3504 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3505 } else if (NumElems == 1) {
3506 // This must be an access of the only element. Return it.
3507 return PackVectorOp(Vec, EVT);
3508 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3509 SDOperand Lo, Hi;
3510 SplitVectorOp(Vec, Lo, Hi);
3511 if (CIdx->getValue() < NumElems/2) {
3512 Vec = Lo;
3513 } else {
3514 Vec = Hi;
3515 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3516 }
3517
3518 // It's now an extract from the appropriate high or low part. Recurse.
3519 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3520 return LowerVEXTRACT_VECTOR_ELT(Op);
3521 } else {
3522 // Variable index case for extract element.
3523 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3524 assert(0 && "unimp!");
3525 return SDOperand();
3526 }
3527}
3528
Chris Lattner4aab2f42006-04-02 05:06:04 +00003529/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3530/// memory traffic.
3531SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3532 SDOperand Vector = Op.getOperand(0);
3533 SDOperand Idx = Op.getOperand(1);
3534
3535 // If the target doesn't support this, store the value to a temporary
3536 // stack slot, then LOAD the scalar element back out.
3537 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003538 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003539
3540 // Add the offset to the index.
3541 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3542 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3543 DAG.getConstant(EltSize, Idx.getValueType()));
3544 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3545
Evan Cheng466685d2006-10-09 20:57:25 +00003546 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003547}
3548
Chris Lattner15972212006-03-31 17:55:51 +00003549
Nate Begeman750ac1b2006-02-01 07:19:44 +00003550/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3551/// with condition CC on the current target. This usually involves legalizing
3552/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3553/// there may be no choice but to create a new SetCC node to represent the
3554/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3555/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3556void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3557 SDOperand &RHS,
3558 SDOperand &CC) {
3559 SDOperand Tmp1, Tmp2, Result;
3560
3561 switch (getTypeAction(LHS.getValueType())) {
3562 case Legal:
3563 Tmp1 = LegalizeOp(LHS); // LHS
3564 Tmp2 = LegalizeOp(RHS); // RHS
3565 break;
3566 case Promote:
3567 Tmp1 = PromoteOp(LHS); // LHS
3568 Tmp2 = PromoteOp(RHS); // RHS
3569
3570 // If this is an FP compare, the operands have already been extended.
3571 if (MVT::isInteger(LHS.getValueType())) {
3572 MVT::ValueType VT = LHS.getValueType();
3573 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3574
3575 // Otherwise, we have to insert explicit sign or zero extends. Note
3576 // that we could insert sign extends for ALL conditions, but zero extend
3577 // is cheaper on many machines (an AND instead of two shifts), so prefer
3578 // it.
3579 switch (cast<CondCodeSDNode>(CC)->get()) {
3580 default: assert(0 && "Unknown integer comparison!");
3581 case ISD::SETEQ:
3582 case ISD::SETNE:
3583 case ISD::SETUGE:
3584 case ISD::SETUGT:
3585 case ISD::SETULE:
3586 case ISD::SETULT:
3587 // ALL of these operations will work if we either sign or zero extend
3588 // the operands (including the unsigned comparisons!). Zero extend is
3589 // usually a simpler/cheaper operation, so prefer it.
3590 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3591 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3592 break;
3593 case ISD::SETGE:
3594 case ISD::SETGT:
3595 case ISD::SETLT:
3596 case ISD::SETLE:
3597 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3598 DAG.getValueType(VT));
3599 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3600 DAG.getValueType(VT));
3601 break;
3602 }
3603 }
3604 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003605 case Expand: {
3606 MVT::ValueType VT = LHS.getValueType();
3607 if (VT == MVT::f32 || VT == MVT::f64) {
3608 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003609 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003610 switch (cast<CondCodeSDNode>(CC)->get()) {
3611 case ISD::SETEQ:
3612 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003613 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003614 break;
3615 case ISD::SETNE:
3616 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003617 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003618 break;
3619 case ISD::SETGE:
3620 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003621 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003622 break;
3623 case ISD::SETLT:
3624 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003625 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003626 break;
3627 case ISD::SETLE:
3628 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003629 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003630 break;
3631 case ISD::SETGT:
3632 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003633 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003634 break;
3635 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003636 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3637 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003638 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003639 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003640 break;
3641 default:
Evan Cheng56966222007-01-12 02:11:51 +00003642 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003643 switch (cast<CondCodeSDNode>(CC)->get()) {
3644 case ISD::SETONE:
3645 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003646 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003647 // Fallthrough
3648 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003649 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003650 break;
3651 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003652 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003653 break;
3654 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003655 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003656 break;
3657 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003658 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003659 break;
Evan Cheng56966222007-01-12 02:11:51 +00003660 case ISD::SETUEQ:
3661 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00003662 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003663 default: assert(0 && "Unsupported FP setcc!");
3664 }
3665 }
3666
3667 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003668 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003669 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003670 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003671 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00003672 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00003673 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003674 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003675 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003676 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003677 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003678 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00003679 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00003680 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3681 Tmp2 = SDOperand();
3682 }
3683 LHS = Tmp1;
3684 RHS = Tmp2;
3685 return;
3686 }
3687
Nate Begeman750ac1b2006-02-01 07:19:44 +00003688 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3689 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003690 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003691 switch (cast<CondCodeSDNode>(CC)->get()) {
3692 case ISD::SETEQ:
3693 case ISD::SETNE:
3694 if (RHSLo == RHSHi)
3695 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3696 if (RHSCST->isAllOnesValue()) {
3697 // Comparison to -1.
3698 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3699 Tmp2 = RHSLo;
3700 break;
3701 }
3702
3703 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3704 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3705 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3706 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3707 break;
3708 default:
3709 // If this is a comparison of the sign bit, just look at the top part.
3710 // X > -1, x < 0
3711 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3712 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3713 CST->getValue() == 0) || // X < 0
3714 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3715 CST->isAllOnesValue())) { // X > -1
3716 Tmp1 = LHSHi;
3717 Tmp2 = RHSHi;
3718 break;
3719 }
3720
3721 // FIXME: This generated code sucks.
3722 ISD::CondCode LowCC;
3723 switch (cast<CondCodeSDNode>(CC)->get()) {
3724 default: assert(0 && "Unknown integer setcc!");
3725 case ISD::SETLT:
3726 case ISD::SETULT: LowCC = ISD::SETULT; break;
3727 case ISD::SETGT:
3728 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3729 case ISD::SETLE:
3730 case ISD::SETULE: LowCC = ISD::SETULE; break;
3731 case ISD::SETGE:
3732 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3733 }
3734
3735 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3736 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3737 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3738
3739 // NOTE: on targets without efficient SELECT of bools, we can always use
3740 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3741 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3742 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3743 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3744 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3745 Result, Tmp1, Tmp2));
3746 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003747 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003748 }
3749 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003750 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003751 LHS = Tmp1;
3752 RHS = Tmp2;
3753}
3754
Chris Lattner35481892005-12-23 00:16:34 +00003755/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003756/// The resultant code need not be legal. Note that SrcOp is the input operand
3757/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003758SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3759 SDOperand SrcOp) {
3760 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003761 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003762
3763 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003764 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003765 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003766 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003767}
3768
Chris Lattner4352cc92006-04-04 17:23:26 +00003769SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3770 // Create a vector sized/aligned stack slot, store the value to element #0,
3771 // then load the whole vector back out.
3772 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003773 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003774 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003775 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003776}
3777
3778
Chris Lattnerce872152006-03-19 06:31:19 +00003779/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3780/// support the operation, but do support the resultant packed vector type.
3781SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3782
3783 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003784 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003785 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003786 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003787 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003788 std::map<SDOperand, std::vector<unsigned> > Values;
3789 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003790 bool isConstant = true;
3791 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3792 SplatValue.getOpcode() != ISD::UNDEF)
3793 isConstant = false;
3794
Evan Cheng033e6812006-03-24 01:17:21 +00003795 for (unsigned i = 1; i < NumElems; ++i) {
3796 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003797 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003798 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003799 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003800 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003801 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003802
3803 // If this isn't a constant element or an undef, we can't use a constant
3804 // pool load.
3805 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3806 V.getOpcode() != ISD::UNDEF)
3807 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003808 }
3809
3810 if (isOnlyLowElement) {
3811 // If the low element is an undef too, then this whole things is an undef.
3812 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3813 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3814 // Otherwise, turn this into a scalar_to_vector node.
3815 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3816 Node->getOperand(0));
3817 }
3818
Chris Lattner2eb86532006-03-24 07:29:17 +00003819 // If all elements are constants, create a load from the constant pool.
3820 if (isConstant) {
3821 MVT::ValueType VT = Node->getValueType(0);
3822 const Type *OpNTy =
3823 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3824 std::vector<Constant*> CV;
3825 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3826 if (ConstantFPSDNode *V =
3827 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3828 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3829 } else if (ConstantSDNode *V =
3830 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003831 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003832 } else {
3833 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3834 CV.push_back(UndefValue::get(OpNTy));
3835 }
3836 }
3837 Constant *CP = ConstantPacked::get(CV);
3838 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003839 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003840 }
3841
Chris Lattner87100e02006-03-20 01:52:29 +00003842 if (SplatValue.Val) { // Splat of one value?
3843 // Build the shuffle constant vector: <0, 0, 0, 0>
3844 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003845 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003846 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003847 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003848 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3849 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003850
3851 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003852 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003853 // Get the splatted value into the low element of a vector register.
3854 SDOperand LowValVec =
3855 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3856
3857 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3858 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3859 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3860 SplatMask);
3861 }
3862 }
3863
Evan Cheng033e6812006-03-24 01:17:21 +00003864 // If there are only two unique elements, we may be able to turn this into a
3865 // vector shuffle.
3866 if (Values.size() == 2) {
3867 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3868 MVT::ValueType MaskVT =
3869 MVT::getIntVectorWithNumElements(NumElems);
3870 std::vector<SDOperand> MaskVec(NumElems);
3871 unsigned i = 0;
3872 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3873 E = Values.end(); I != E; ++I) {
3874 for (std::vector<unsigned>::iterator II = I->second.begin(),
3875 EE = I->second.end(); II != EE; ++II)
3876 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3877 i += NumElems;
3878 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003879 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3880 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003881
3882 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003883 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3884 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003885 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00003886 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3887 E = Values.end(); I != E; ++I) {
3888 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3889 I->first);
3890 Ops.push_back(Op);
3891 }
3892 Ops.push_back(ShuffleMask);
3893
3894 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003895 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3896 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003897 }
3898 }
Chris Lattnerce872152006-03-19 06:31:19 +00003899
3900 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3901 // aligned object on the stack, store each element into it, then load
3902 // the result as a vector.
3903 MVT::ValueType VT = Node->getValueType(0);
3904 // Create the stack frame object.
3905 SDOperand FIPtr = CreateStackTemporary(VT);
3906
3907 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003908 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003909 unsigned TypeByteSize =
3910 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00003911 // Store (in the right endianness) the elements to memory.
3912 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3913 // Ignore undef elements.
3914 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3915
Chris Lattner841c8822006-03-22 01:46:54 +00003916 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003917
3918 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3919 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3920
Evan Cheng786225a2006-10-05 23:01:46 +00003921 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003922 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00003923 }
3924
3925 SDOperand StoreChain;
3926 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003927 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3928 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003929 else
3930 StoreChain = DAG.getEntryNode();
3931
3932 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003933 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003934}
3935
3936/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3937/// specified value type.
3938SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3939 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3940 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00003941 const Type *Ty = MVT::getTypeForValueType(VT);
3942 unsigned StackAlign = (unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty);
3943 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00003944 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3945}
3946
Chris Lattner5b359c62005-04-02 04:00:59 +00003947void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3948 SDOperand Op, SDOperand Amt,
3949 SDOperand &Lo, SDOperand &Hi) {
3950 // Expand the subcomponents.
3951 SDOperand LHSL, LHSH;
3952 ExpandOp(Op, LHSL, LHSH);
3953
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003954 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003955 MVT::ValueType VT = LHSL.getValueType();
3956 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00003957 Hi = Lo.getValue(1);
3958}
3959
3960
Chris Lattnere34b3962005-01-19 04:19:40 +00003961/// ExpandShift - Try to find a clever way to expand this shift operation out to
3962/// smaller elements. If we can't find a way that is more efficient than a
3963/// libcall on this target, return false. Otherwise, return true with the
3964/// low-parts expanded into Lo and Hi.
3965bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3966 SDOperand &Lo, SDOperand &Hi) {
3967 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3968 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003969
Chris Lattnere34b3962005-01-19 04:19:40 +00003970 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003971 SDOperand ShAmt = LegalizeOp(Amt);
3972 MVT::ValueType ShTy = ShAmt.getValueType();
3973 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3974 unsigned NVTBits = MVT::getSizeInBits(NVT);
3975
3976 // Handle the case when Amt is an immediate. Other cases are currently broken
3977 // and are disabled.
3978 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3979 unsigned Cst = CN->getValue();
3980 // Expand the incoming operand to be shifted, so that we have its parts
3981 SDOperand InL, InH;
3982 ExpandOp(Op, InL, InH);
3983 switch(Opc) {
3984 case ISD::SHL:
3985 if (Cst > VTBits) {
3986 Lo = DAG.getConstant(0, NVT);
3987 Hi = DAG.getConstant(0, NVT);
3988 } else if (Cst > NVTBits) {
3989 Lo = DAG.getConstant(0, NVT);
3990 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003991 } else if (Cst == NVTBits) {
3992 Lo = DAG.getConstant(0, NVT);
3993 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003994 } else {
3995 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3996 Hi = DAG.getNode(ISD::OR, NVT,
3997 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3998 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3999 }
4000 return true;
4001 case ISD::SRL:
4002 if (Cst > VTBits) {
4003 Lo = DAG.getConstant(0, NVT);
4004 Hi = DAG.getConstant(0, NVT);
4005 } else if (Cst > NVTBits) {
4006 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4007 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004008 } else if (Cst == NVTBits) {
4009 Lo = InH;
4010 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004011 } else {
4012 Lo = DAG.getNode(ISD::OR, NVT,
4013 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4014 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4015 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4016 }
4017 return true;
4018 case ISD::SRA:
4019 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004020 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004021 DAG.getConstant(NVTBits-1, ShTy));
4022 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004023 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004024 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004025 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004026 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004027 } else if (Cst == NVTBits) {
4028 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004029 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004030 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004031 } else {
4032 Lo = DAG.getNode(ISD::OR, NVT,
4033 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4034 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4035 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4036 }
4037 return true;
4038 }
4039 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004040
4041 // Okay, the shift amount isn't constant. However, if we can tell that it is
4042 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4043 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4044 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4045
4046 // If we know that the high bit of the shift amount is one, then we can do
4047 // this as a couple of simple shifts.
4048 if (KnownOne & Mask) {
4049 // Mask out the high bit, which we know is set.
4050 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4051 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4052
4053 // Expand the incoming operand to be shifted, so that we have its parts
4054 SDOperand InL, InH;
4055 ExpandOp(Op, InL, InH);
4056 switch(Opc) {
4057 case ISD::SHL:
4058 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4059 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4060 return true;
4061 case ISD::SRL:
4062 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4063 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4064 return true;
4065 case ISD::SRA:
4066 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4067 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4068 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4069 return true;
4070 }
4071 }
4072
4073 // If we know that the high bit of the shift amount is zero, then we can do
4074 // this as a couple of simple shifts.
4075 if (KnownZero & Mask) {
4076 // Compute 32-amt.
4077 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4078 DAG.getConstant(NVTBits, Amt.getValueType()),
4079 Amt);
4080
4081 // Expand the incoming operand to be shifted, so that we have its parts
4082 SDOperand InL, InH;
4083 ExpandOp(Op, InL, InH);
4084 switch(Opc) {
4085 case ISD::SHL:
4086 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4087 Hi = DAG.getNode(ISD::OR, NVT,
4088 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4089 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4090 return true;
4091 case ISD::SRL:
4092 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4093 Lo = DAG.getNode(ISD::OR, NVT,
4094 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4095 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4096 return true;
4097 case ISD::SRA:
4098 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4099 Lo = DAG.getNode(ISD::OR, NVT,
4100 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4101 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4102 return true;
4103 }
4104 }
4105
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004106 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004107}
Chris Lattner77e77a62005-01-21 06:05:23 +00004108
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004109
Chris Lattner77e77a62005-01-21 06:05:23 +00004110// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4111// does not fit into a register, return the lo part and set the hi part to the
4112// by-reg argument. If it does fit into a single register, return the result
4113// and leave the Hi part unset.
4114SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004115 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004116 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4117 // The input chain to this libcall is the entry node of the function.
4118 // Legalizing the call will automatically add the previous call to the
4119 // dependence.
4120 SDOperand InChain = DAG.getEntryNode();
4121
Chris Lattner77e77a62005-01-21 06:05:23 +00004122 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004123 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004124 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4125 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4126 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004127 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00004128 Entry.isSigned = isSigned; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00004129 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004130 }
4131 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004132
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004133 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004134 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004135 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004136 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004137 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004138
Chris Lattner6831a812006-02-13 09:18:02 +00004139 // Legalize the call sequence, starting with the chain. This will advance
4140 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4141 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4142 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004143 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004144 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004145 default: assert(0 && "Unknown thing");
4146 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004147 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004148 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004149 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004150 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004151 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004152 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004153 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004154}
4155
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004156
Chris Lattner77e77a62005-01-21 06:05:23 +00004157/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4158/// destination type is legal.
4159SDOperand SelectionDAGLegalize::
4160ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004161 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00004162 assert(getTypeAction(Source.getValueType()) == Expand &&
4163 "This is not an expansion!");
4164 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4165
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004166 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004167 assert(Source.getValueType() == MVT::i64 &&
4168 "This only works for 64-bit -> FP");
4169 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4170 // incoming integer is set. To handle this, we dynamically test to see if
4171 // it is set, and, if so, add a fudge factor.
4172 SDOperand Lo, Hi;
4173 ExpandOp(Source, Lo, Hi);
4174
Chris Lattner66de05b2005-05-13 04:45:13 +00004175 // If this is unsigned, and not supported, first perform the conversion to
4176 // signed, then adjust the result if the sign bit is set.
4177 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4178 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4179
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004180 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4181 DAG.getConstant(0, Hi.getValueType()),
4182 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004183 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4184 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4185 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004186 uint64_t FF = 0x5f800000ULL;
4187 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004188 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004189
Chris Lattner5839bf22005-08-26 17:15:30 +00004190 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004191 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4192 SDOperand FudgeInReg;
4193 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004194 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004195 else {
4196 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00004197 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004198 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004199 }
Chris Lattner473a9902005-09-29 06:44:39 +00004200 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004201 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004202
Chris Lattnera88a2602005-05-14 05:33:54 +00004203 // Check to see if the target has a custom way to lower this. If so, use it.
4204 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4205 default: assert(0 && "This action not implemented for this operation!");
4206 case TargetLowering::Legal:
4207 case TargetLowering::Expand:
4208 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004209 case TargetLowering::Custom: {
4210 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4211 Source), DAG);
4212 if (NV.Val)
4213 return LegalizeOp(NV);
4214 break; // The target decided this was legal after all
4215 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004216 }
4217
Chris Lattner13689e22005-05-12 07:00:44 +00004218 // Expand the source, then glue it back together for the call. We must expand
4219 // the source in case it is shared (this pass of legalize must traverse it).
4220 SDOperand SrcLo, SrcHi;
4221 ExpandOp(Source, SrcLo, SrcHi);
4222 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4223
Evan Cheng56966222007-01-12 02:11:51 +00004224 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004225 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004226 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004227 else {
4228 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004229 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004230 }
Chris Lattner6831a812006-02-13 09:18:02 +00004231
4232 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4233 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004234 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4235 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004236}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004237
Chris Lattner22cde6a2006-01-28 08:25:58 +00004238/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4239/// INT_TO_FP operation of the specified operand when the target requests that
4240/// we expand it. At this point, we know that the result and operand types are
4241/// legal for the target.
4242SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4243 SDOperand Op0,
4244 MVT::ValueType DestVT) {
4245 if (Op0.getValueType() == MVT::i32) {
4246 // simple 32-bit [signed|unsigned] integer to float/double expansion
4247
Chris Lattner58092e32007-01-20 22:35:55 +00004248 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004249 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004250 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4251 unsigned StackAlign =
4252 (unsigned)TLI.getTargetData()->getTypeAlignmentPref(F64Type);
4253 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004254 // get address of 8 byte buffer
4255 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4256 // word offset constant for Hi/Lo address computation
4257 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4258 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004259 SDOperand Hi = StackSlot;
4260 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4261 if (TLI.isLittleEndian())
4262 std::swap(Hi, Lo);
4263
Chris Lattner22cde6a2006-01-28 08:25:58 +00004264 // if signed map to unsigned space
4265 SDOperand Op0Mapped;
4266 if (isSigned) {
4267 // constant used to invert sign bit (signed to unsigned mapping)
4268 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4269 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4270 } else {
4271 Op0Mapped = Op0;
4272 }
4273 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004274 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004275 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004276 // initial hi portion of constructed double
4277 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4278 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004279 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004280 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004281 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004282 // FP constant to bias correct the final result
4283 SDOperand Bias = DAG.getConstantFP(isSigned ?
4284 BitsToDouble(0x4330000080000000ULL)
4285 : BitsToDouble(0x4330000000000000ULL),
4286 MVT::f64);
4287 // subtract the bias
4288 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4289 // final result
4290 SDOperand Result;
4291 // handle final rounding
4292 if (DestVT == MVT::f64) {
4293 // do nothing
4294 Result = Sub;
4295 } else {
4296 // if f32 then cast to f32
4297 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4298 }
4299 return Result;
4300 }
4301 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4302 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4303
4304 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4305 DAG.getConstant(0, Op0.getValueType()),
4306 ISD::SETLT);
4307 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4308 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4309 SignSet, Four, Zero);
4310
4311 // If the sign bit of the integer is set, the large number will be treated
4312 // as a negative number. To counteract this, the dynamic code adds an
4313 // offset depending on the data type.
4314 uint64_t FF;
4315 switch (Op0.getValueType()) {
4316 default: assert(0 && "Unsupported integer type!");
4317 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4318 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4319 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4320 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4321 }
4322 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004323 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004324
4325 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4326 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4327 SDOperand FudgeInReg;
4328 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004329 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004330 else {
4331 assert(DestVT == MVT::f64 && "Unexpected conversion");
4332 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4333 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004334 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004335 }
4336
4337 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4338}
4339
4340/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4341/// *INT_TO_FP operation of the specified operand when the target requests that
4342/// we promote it. At this point, we know that the result and operand types are
4343/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4344/// operation that takes a larger input.
4345SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4346 MVT::ValueType DestVT,
4347 bool isSigned) {
4348 // First step, figure out the appropriate *INT_TO_FP operation to use.
4349 MVT::ValueType NewInTy = LegalOp.getValueType();
4350
4351 unsigned OpToUse = 0;
4352
4353 // Scan for the appropriate larger type to use.
4354 while (1) {
4355 NewInTy = (MVT::ValueType)(NewInTy+1);
4356 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4357
4358 // If the target supports SINT_TO_FP of this type, use it.
4359 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4360 default: break;
4361 case TargetLowering::Legal:
4362 if (!TLI.isTypeLegal(NewInTy))
4363 break; // Can't use this datatype.
4364 // FALL THROUGH.
4365 case TargetLowering::Custom:
4366 OpToUse = ISD::SINT_TO_FP;
4367 break;
4368 }
4369 if (OpToUse) break;
4370 if (isSigned) continue;
4371
4372 // If the target supports UINT_TO_FP of this type, use it.
4373 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4374 default: break;
4375 case TargetLowering::Legal:
4376 if (!TLI.isTypeLegal(NewInTy))
4377 break; // Can't use this datatype.
4378 // FALL THROUGH.
4379 case TargetLowering::Custom:
4380 OpToUse = ISD::UINT_TO_FP;
4381 break;
4382 }
4383 if (OpToUse) break;
4384
4385 // Otherwise, try a larger type.
4386 }
4387
4388 // Okay, we found the operation and type to use. Zero extend our input to the
4389 // desired type then run the operation on it.
4390 return DAG.getNode(OpToUse, DestVT,
4391 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4392 NewInTy, LegalOp));
4393}
4394
4395/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4396/// FP_TO_*INT operation of the specified operand when the target requests that
4397/// we promote it. At this point, we know that the result and operand types are
4398/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4399/// operation that returns a larger result.
4400SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4401 MVT::ValueType DestVT,
4402 bool isSigned) {
4403 // First step, figure out the appropriate FP_TO*INT operation to use.
4404 MVT::ValueType NewOutTy = DestVT;
4405
4406 unsigned OpToUse = 0;
4407
4408 // Scan for the appropriate larger type to use.
4409 while (1) {
4410 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4411 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4412
4413 // If the target supports FP_TO_SINT returning this type, use it.
4414 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4415 default: break;
4416 case TargetLowering::Legal:
4417 if (!TLI.isTypeLegal(NewOutTy))
4418 break; // Can't use this datatype.
4419 // FALL THROUGH.
4420 case TargetLowering::Custom:
4421 OpToUse = ISD::FP_TO_SINT;
4422 break;
4423 }
4424 if (OpToUse) break;
4425
4426 // If the target supports FP_TO_UINT of this type, use it.
4427 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4428 default: break;
4429 case TargetLowering::Legal:
4430 if (!TLI.isTypeLegal(NewOutTy))
4431 break; // Can't use this datatype.
4432 // FALL THROUGH.
4433 case TargetLowering::Custom:
4434 OpToUse = ISD::FP_TO_UINT;
4435 break;
4436 }
4437 if (OpToUse) break;
4438
4439 // Otherwise, try a larger type.
4440 }
4441
4442 // Okay, we found the operation and type to use. Truncate the result of the
4443 // extended FP_TO_*INT operation to the desired size.
4444 return DAG.getNode(ISD::TRUNCATE, DestVT,
4445 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4446}
4447
4448/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4449///
4450SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4451 MVT::ValueType VT = Op.getValueType();
4452 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4453 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4454 switch (VT) {
4455 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4456 case MVT::i16:
4457 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4458 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4459 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4460 case MVT::i32:
4461 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4462 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4463 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4464 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4465 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4466 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4467 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4468 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4469 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4470 case MVT::i64:
4471 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4472 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4473 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4474 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4475 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4476 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4477 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4478 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4479 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4480 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4481 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4482 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4483 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4484 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4485 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4486 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4487 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4488 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4489 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4490 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4491 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4492 }
4493}
4494
4495/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4496///
4497SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4498 switch (Opc) {
4499 default: assert(0 && "Cannot expand this yet!");
4500 case ISD::CTPOP: {
4501 static const uint64_t mask[6] = {
4502 0x5555555555555555ULL, 0x3333333333333333ULL,
4503 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4504 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4505 };
4506 MVT::ValueType VT = Op.getValueType();
4507 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4508 unsigned len = getSizeInBits(VT);
4509 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4510 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4511 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4512 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4513 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4514 DAG.getNode(ISD::AND, VT,
4515 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4516 }
4517 return Op;
4518 }
4519 case ISD::CTLZ: {
4520 // for now, we do this:
4521 // x = x | (x >> 1);
4522 // x = x | (x >> 2);
4523 // ...
4524 // x = x | (x >>16);
4525 // x = x | (x >>32); // for 64-bit input
4526 // return popcount(~x);
4527 //
4528 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4529 MVT::ValueType VT = Op.getValueType();
4530 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4531 unsigned len = getSizeInBits(VT);
4532 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4533 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4534 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4535 }
4536 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4537 return DAG.getNode(ISD::CTPOP, VT, Op);
4538 }
4539 case ISD::CTTZ: {
4540 // for now, we use: { return popcount(~x & (x - 1)); }
4541 // unless the target has ctlz but not ctpop, in which case we use:
4542 // { return 32 - nlz(~x & (x-1)); }
4543 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4544 MVT::ValueType VT = Op.getValueType();
4545 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4546 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4547 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4548 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4549 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4550 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4551 TLI.isOperationLegal(ISD::CTLZ, VT))
4552 return DAG.getNode(ISD::SUB, VT,
4553 DAG.getConstant(getSizeInBits(VT), VT),
4554 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4555 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4556 }
4557 }
4558}
Chris Lattnere34b3962005-01-19 04:19:40 +00004559
Chris Lattner3e928bb2005-01-07 07:47:09 +00004560/// ExpandOp - Expand the specified SDOperand into its two component pieces
4561/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4562/// LegalizeNodes map is filled in for any results that are not expanded, the
4563/// ExpandedNodes map is filled in for any results that are expanded, and the
4564/// Lo/Hi values are returned.
4565void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4566 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004567 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004568 SDNode *Node = Op.Val;
4569 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004570 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4571 VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004572 "Cannot expand to FP value or to larger int value!");
4573
Chris Lattner6fdcb252005-09-02 20:32:45 +00004574 // See if we already expanded it.
4575 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4576 = ExpandedNodes.find(Op);
4577 if (I != ExpandedNodes.end()) {
4578 Lo = I->second.first;
4579 Hi = I->second.second;
4580 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004581 }
4582
Chris Lattner3e928bb2005-01-07 07:47:09 +00004583 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004584 case ISD::CopyFromReg:
4585 assert(0 && "CopyFromReg must be legal!");
4586 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004587#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00004588 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004589#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004590 assert(0 && "Do not know how to expand this operator!");
4591 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004592 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004593 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004594 Lo = DAG.getNode(ISD::UNDEF, NVT);
4595 Hi = DAG.getNode(ISD::UNDEF, NVT);
4596 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004597 case ISD::Constant: {
4598 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4599 Lo = DAG.getConstant(Cst, NVT);
4600 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4601 break;
4602 }
Evan Cheng00495212006-12-12 21:32:44 +00004603 case ISD::ConstantFP: {
4604 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004605 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004606 if (getTypeAction(Lo.getValueType()) == Expand)
4607 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004608 break;
4609 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004610 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004611 // Return the operands.
4612 Lo = Node->getOperand(0);
4613 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004614 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004615
4616 case ISD::SIGN_EXTEND_INREG:
4617 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004618 // sext_inreg the low part if needed.
4619 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4620
4621 // The high part gets the sign extension from the lo-part. This handles
4622 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004623 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4624 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4625 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004626 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004627
Nate Begemand88fc032006-01-14 03:14:10 +00004628 case ISD::BSWAP: {
4629 ExpandOp(Node->getOperand(0), Lo, Hi);
4630 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4631 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4632 Lo = TempLo;
4633 break;
4634 }
4635
Chris Lattneredb1add2005-05-11 04:51:16 +00004636 case ISD::CTPOP:
4637 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004638 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4639 DAG.getNode(ISD::CTPOP, NVT, Lo),
4640 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004641 Hi = DAG.getConstant(0, NVT);
4642 break;
4643
Chris Lattner39a8f332005-05-12 19:05:01 +00004644 case ISD::CTLZ: {
4645 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004646 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004647 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4648 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004649 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4650 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004651 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4652 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4653
4654 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4655 Hi = DAG.getConstant(0, NVT);
4656 break;
4657 }
4658
4659 case ISD::CTTZ: {
4660 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004661 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004662 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4663 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004664 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4665 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004666 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4667 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4668
4669 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4670 Hi = DAG.getConstant(0, NVT);
4671 break;
4672 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004673
Nate Begemanacc398c2006-01-25 18:21:52 +00004674 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004675 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4676 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004677 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4678 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4679
4680 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004681 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004682 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4683 if (!TLI.isLittleEndian())
4684 std::swap(Lo, Hi);
4685 break;
4686 }
4687
Chris Lattner3e928bb2005-01-07 07:47:09 +00004688 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004689 LoadSDNode *LD = cast<LoadSDNode>(Node);
4690 SDOperand Ch = LD->getChain(); // Legalize the chain.
4691 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4692 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004693
Evan Cheng466685d2006-10-09 20:57:25 +00004694 if (ExtType == ISD::NON_EXTLOAD) {
Chris Lattnerfea997a2007-02-01 04:55:59 +00004695 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Evan Cheng00495212006-12-12 21:32:44 +00004696 if (VT == MVT::f32 || VT == MVT::f64) {
4697 // f32->i32 or f64->i64 one to one expansion.
4698 // Remember that we legalized the chain.
4699 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004700 // Recursively expand the new load.
4701 if (getTypeAction(NVT) == Expand)
4702 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004703 break;
4704 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004705
Evan Cheng466685d2006-10-09 20:57:25 +00004706 // Increment the pointer to the other half.
4707 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4708 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4709 getIntPtrConstant(IncrementSize));
4710 // FIXME: This creates a bogus srcvalue!
Chris Lattnerfea997a2007-02-01 04:55:59 +00004711 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004712
Evan Cheng466685d2006-10-09 20:57:25 +00004713 // Build a factor node to remember that this load is independent of the
4714 // other one.
4715 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4716 Hi.getValue(1));
4717
4718 // Remember that we legalized the chain.
4719 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4720 if (!TLI.isLittleEndian())
4721 std::swap(Lo, Hi);
4722 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004723 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004724
4725 if (VT == MVT::f64 && EVT == MVT::f32) {
4726 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4727 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4728 LD->getSrcValueOffset());
4729 // Remember that we legalized the chain.
4730 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4731 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4732 break;
4733 }
Evan Cheng466685d2006-10-09 20:57:25 +00004734
4735 if (EVT == NVT)
4736 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4737 LD->getSrcValueOffset());
4738 else
4739 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4740 LD->getSrcValueOffset(), EVT);
4741
4742 // Remember that we legalized the chain.
4743 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4744
4745 if (ExtType == ISD::SEXTLOAD) {
4746 // The high part is obtained by SRA'ing all but one of the bits of the
4747 // lo part.
4748 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4749 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4750 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4751 } else if (ExtType == ISD::ZEXTLOAD) {
4752 // The high part is just a zero.
4753 Hi = DAG.getConstant(0, NVT);
4754 } else /* if (ExtType == ISD::EXTLOAD) */ {
4755 // The high part is undefined.
4756 Hi = DAG.getNode(ISD::UNDEF, NVT);
4757 }
4758 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004759 break;
4760 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004761 case ISD::AND:
4762 case ISD::OR:
4763 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4764 SDOperand LL, LH, RL, RH;
4765 ExpandOp(Node->getOperand(0), LL, LH);
4766 ExpandOp(Node->getOperand(1), RL, RH);
4767 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4768 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4769 break;
4770 }
4771 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004772 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004773 ExpandOp(Node->getOperand(1), LL, LH);
4774 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004775 if (getTypeAction(NVT) == Expand)
4776 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004777 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004778 if (VT != MVT::f32)
4779 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004780 break;
4781 }
Nate Begeman9373a812005-08-10 20:51:12 +00004782 case ISD::SELECT_CC: {
4783 SDOperand TL, TH, FL, FH;
4784 ExpandOp(Node->getOperand(2), TL, TH);
4785 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004786 if (getTypeAction(NVT) == Expand)
4787 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004788 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4789 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004790 if (VT != MVT::f32)
4791 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4792 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004793 break;
4794 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004795 case ISD::ANY_EXTEND:
4796 // The low part is any extension of the input (which degenerates to a copy).
4797 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4798 // The high part is undefined.
4799 Hi = DAG.getNode(ISD::UNDEF, NVT);
4800 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004801 case ISD::SIGN_EXTEND: {
4802 // The low part is just a sign extension of the input (which degenerates to
4803 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004804 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004805
Chris Lattner3e928bb2005-01-07 07:47:09 +00004806 // The high part is obtained by SRA'ing all but one of the bits of the lo
4807 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004808 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004809 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4810 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004811 break;
4812 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004813 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004814 // The low part is just a zero extension of the input (which degenerates to
4815 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004816 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004817
Chris Lattner3e928bb2005-01-07 07:47:09 +00004818 // The high part is just a zero.
4819 Hi = DAG.getConstant(0, NVT);
4820 break;
Chris Lattner35481892005-12-23 00:16:34 +00004821
4822 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004823 SDOperand Tmp;
4824 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4825 // If the target wants to, allow it to lower this itself.
4826 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4827 case Expand: assert(0 && "cannot expand FP!");
4828 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4829 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4830 }
4831 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4832 }
4833
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004834 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00004835 if (VT == MVT::f32 || VT == MVT::f64) {
4836 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00004837 if (getTypeAction(NVT) == Expand)
4838 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00004839 break;
4840 }
4841
4842 // If source operand will be expanded to the same type as VT, i.e.
4843 // i64 <- f64, i32 <- f32, expand the source operand instead.
4844 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4845 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4846 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004847 break;
4848 }
4849
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004850 // Turn this into a load/store pair by default.
4851 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00004852 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004853
Chris Lattner35481892005-12-23 00:16:34 +00004854 ExpandOp(Tmp, Lo, Hi);
4855 break;
4856 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004857
Chris Lattner8137c9e2006-01-28 05:07:51 +00004858 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004859 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4860 TargetLowering::Custom &&
4861 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004862 Lo = TLI.LowerOperation(Op, DAG);
4863 assert(Lo.Val && "Node must be custom expanded!");
4864 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004865 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004866 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004867 break;
4868
Chris Lattner4e6c7462005-01-08 19:27:05 +00004869 // These operators cannot be expanded directly, emit them as calls to
4870 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00004871 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004872 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004873 SDOperand Op;
4874 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4875 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004876 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4877 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004878 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004879
Chris Lattnerf20d1832005-07-30 01:40:57 +00004880 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4881
Chris Lattner80a3e942005-07-29 00:33:32 +00004882 // Now that the custom expander is done, expand the result, which is still
4883 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004884 if (Op.Val) {
4885 ExpandOp(Op, Lo, Hi);
4886 break;
4887 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004888 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004889
Evan Cheng56966222007-01-12 02:11:51 +00004890 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004891 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004892 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004893 else
Evan Cheng56966222007-01-12 02:11:51 +00004894 LC = RTLIB::FPTOSINT_F64_I64;
4895 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4896 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004897 break;
Evan Cheng56966222007-01-12 02:11:51 +00004898 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004899
Evan Cheng56966222007-01-12 02:11:51 +00004900 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004901 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004902 SDOperand Op;
4903 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4904 case Expand: assert(0 && "cannot expand FP!");
4905 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4906 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4907 }
4908
4909 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4910
4911 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004912 if (Op.Val) {
4913 ExpandOp(Op, Lo, Hi);
4914 break;
4915 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004916 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004917
Evan Cheng56966222007-01-12 02:11:51 +00004918 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004919 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004920 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004921 else
Evan Cheng56966222007-01-12 02:11:51 +00004922 LC = RTLIB::FPTOUINT_F64_I64;
4923 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4924 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004925 break;
Evan Cheng56966222007-01-12 02:11:51 +00004926 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00004927
Evan Cheng05a2d562006-01-09 18:31:59 +00004928 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004929 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004930 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004931 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004932 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004933 Op = TLI.LowerOperation(Op, DAG);
4934 if (Op.Val) {
4935 // Now that the custom expander is done, expand the result, which is
4936 // still VT.
4937 ExpandOp(Op, Lo, Hi);
4938 break;
4939 }
4940 }
4941
Chris Lattner79980b02006-09-13 03:50:39 +00004942 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4943 // this X << 1 as X+X.
4944 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4945 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4946 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4947 SDOperand LoOps[2], HiOps[3];
4948 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4949 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4950 LoOps[1] = LoOps[0];
4951 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4952
4953 HiOps[1] = HiOps[0];
4954 HiOps[2] = Lo.getValue(1);
4955 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4956 break;
4957 }
4958 }
4959
Chris Lattnere34b3962005-01-19 04:19:40 +00004960 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004961 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004962 break;
Chris Lattner47599822005-04-02 03:38:53 +00004963
4964 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004965 TargetLowering::LegalizeAction Action =
4966 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4967 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4968 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004969 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004970 break;
4971 }
4972
Chris Lattnere34b3962005-01-19 04:19:40 +00004973 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00004974 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
4975 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004976 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004977 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004978
Evan Cheng05a2d562006-01-09 18:31:59 +00004979 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004980 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004981 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004982 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004983 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004984 Op = TLI.LowerOperation(Op, DAG);
4985 if (Op.Val) {
4986 // Now that the custom expander is done, expand the result, which is
4987 // still VT.
4988 ExpandOp(Op, Lo, Hi);
4989 break;
4990 }
4991 }
4992
Chris Lattnere34b3962005-01-19 04:19:40 +00004993 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004994 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004995 break;
Chris Lattner47599822005-04-02 03:38:53 +00004996
4997 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004998 TargetLowering::LegalizeAction Action =
4999 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5000 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5001 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005002 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005003 break;
5004 }
5005
Chris Lattnere34b3962005-01-19 04:19:40 +00005006 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005007 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5008 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005009 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005010 }
5011
5012 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005013 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005014 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005015 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005016 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005017 Op = TLI.LowerOperation(Op, DAG);
5018 if (Op.Val) {
5019 // Now that the custom expander is done, expand the result, which is
5020 // still VT.
5021 ExpandOp(Op, Lo, Hi);
5022 break;
5023 }
5024 }
5025
Chris Lattnere34b3962005-01-19 04:19:40 +00005026 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005027 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005028 break;
Chris Lattner47599822005-04-02 03:38:53 +00005029
5030 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005031 TargetLowering::LegalizeAction Action =
5032 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5033 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5034 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005035 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005036 break;
5037 }
5038
Chris Lattnere34b3962005-01-19 04:19:40 +00005039 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005040 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5041 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005042 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005043 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005044
Misha Brukmanedf128a2005-04-21 22:36:52 +00005045 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005046 case ISD::SUB: {
5047 // If the target wants to custom expand this, let them.
5048 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5049 TargetLowering::Custom) {
5050 Op = TLI.LowerOperation(Op, DAG);
5051 if (Op.Val) {
5052 ExpandOp(Op, Lo, Hi);
5053 break;
5054 }
5055 }
5056
5057 // Expand the subcomponents.
5058 SDOperand LHSL, LHSH, RHSL, RHSH;
5059 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5060 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005061 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5062 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005063 LoOps[0] = LHSL;
5064 LoOps[1] = RHSL;
5065 HiOps[0] = LHSH;
5066 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005067 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005068 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005069 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005070 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005071 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005072 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005073 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005074 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005075 }
Chris Lattner84f67882005-01-20 18:52:28 +00005076 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005077 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005078 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005079 // If the target wants to custom expand this, let them.
5080 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005081 SDOperand New = TLI.LowerOperation(Op, DAG);
5082 if (New.Val) {
5083 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005084 break;
5085 }
5086 }
5087
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005088 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5089 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005090 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005091 SDOperand LL, LH, RL, RH;
5092 ExpandOp(Node->getOperand(0), LL, LH);
5093 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005094 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005095 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005096 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5097 // extended the sign bit of the low half through the upper half, and if so
5098 // emit a MULHS instead of the alternate sequence that is valid for any
5099 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005100 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005101 // is RH an extension of the sign bit of RL?
5102 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5103 RH.getOperand(1).getOpcode() == ISD::Constant &&
5104 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5105 // is LH an extension of the sign bit of LL?
5106 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5107 LH.getOperand(1).getOpcode() == ISD::Constant &&
5108 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005109 // Low part:
5110 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5111 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005112 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005113 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005114 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005115 // Low part:
5116 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5117
5118 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005119 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5120 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5121 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5122 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5123 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005124 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005125 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005126 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005127
Evan Cheng56966222007-01-12 02:11:51 +00005128 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5129 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005130 break;
5131 }
Evan Cheng56966222007-01-12 02:11:51 +00005132 case ISD::SDIV:
5133 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5134 break;
5135 case ISD::UDIV:
5136 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5137 break;
5138 case ISD::SREM:
5139 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5140 break;
5141 case ISD::UREM:
5142 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5143 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005144
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005145 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005146 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5147 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5148 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005149 break;
5150 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005151 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5152 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5153 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005154 break;
5155 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005156 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5157 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5158 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005159 break;
5160 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005161 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5162 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5163 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005164 break;
5165 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005166 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005167 break;
5168 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005169 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005170 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005171 case ISD::FSQRT:
5172 case ISD::FSIN:
5173 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005174 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005175 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005176 case ISD::FSQRT:
5177 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5178 break;
5179 case ISD::FSIN:
5180 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5181 break;
5182 case ISD::FCOS:
5183 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5184 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005185 default: assert(0 && "Unreachable!");
5186 }
Evan Cheng56966222007-01-12 02:11:51 +00005187 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005188 break;
5189 }
Evan Cheng966bf242006-12-16 00:52:40 +00005190 case ISD::FABS: {
5191 SDOperand Mask = (VT == MVT::f64)
5192 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5193 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5194 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5195 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5196 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5197 if (getTypeAction(NVT) == Expand)
5198 ExpandOp(Lo, Lo, Hi);
5199 break;
5200 }
5201 case ISD::FNEG: {
5202 SDOperand Mask = (VT == MVT::f64)
5203 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5204 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5205 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5206 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5207 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5208 if (getTypeAction(NVT) == Expand)
5209 ExpandOp(Lo, Lo, Hi);
5210 break;
5211 }
Evan Cheng912095b2007-01-04 21:56:39 +00005212 case ISD::FCOPYSIGN: {
5213 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5214 if (getTypeAction(NVT) == Expand)
5215 ExpandOp(Lo, Lo, Hi);
5216 break;
5217 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005218 case ISD::SINT_TO_FP:
5219 case ISD::UINT_TO_FP: {
5220 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5221 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005222 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005223 if (Node->getOperand(0).getValueType() == MVT::i64) {
5224 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005225 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005226 else
Evan Cheng56966222007-01-12 02:11:51 +00005227 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005228 } else {
5229 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005230 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005231 else
Evan Cheng56966222007-01-12 02:11:51 +00005232 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005233 }
5234
5235 // Promote the operand if needed.
5236 if (getTypeAction(SrcVT) == Promote) {
5237 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5238 Tmp = isSigned
5239 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5240 DAG.getValueType(SrcVT))
5241 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5242 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5243 }
Evan Cheng56966222007-01-12 02:11:51 +00005244 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00005245 break;
5246 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005247 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005248
Chris Lattner83397362005-12-21 18:02:52 +00005249 // Make sure the resultant values have been legalized themselves, unless this
5250 // is a type that requires multi-step expansion.
5251 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5252 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005253 if (Hi.Val)
5254 // Don't legalize the high part if it is expanded to a single node.
5255 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005256 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005257
5258 // Remember in a map if the values will be reused later.
5259 bool isNew =
5260 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5261 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005262}
5263
Chris Lattnerc7029802006-03-18 01:44:44 +00005264/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5265/// two smaller values of MVT::Vector type.
5266void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5267 SDOperand &Hi) {
5268 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5269 SDNode *Node = Op.Val;
5270 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5271 assert(NumElements > 1 && "Cannot split a single element vector!");
5272 unsigned NewNumElts = NumElements/2;
5273 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5274 SDOperand TypeNode = *(Node->op_end()-1);
5275
5276 // See if we already split it.
5277 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5278 = SplitNodes.find(Op);
5279 if (I != SplitNodes.end()) {
5280 Lo = I->second.first;
5281 Hi = I->second.second;
5282 return;
5283 }
5284
5285 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005286 default:
5287#ifndef NDEBUG
5288 Node->dump();
5289#endif
5290 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00005291 case ISD::VBUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005292 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5293 Node->op_begin()+NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005294 LoOps.push_back(NewNumEltsNode);
5295 LoOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005296 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005297
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005298 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5299 Node->op_end()-2);
Chris Lattnerc7029802006-03-18 01:44:44 +00005300 HiOps.push_back(NewNumEltsNode);
5301 HiOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005302 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005303 break;
5304 }
5305 case ISD::VADD:
5306 case ISD::VSUB:
5307 case ISD::VMUL:
5308 case ISD::VSDIV:
5309 case ISD::VUDIV:
5310 case ISD::VAND:
5311 case ISD::VOR:
5312 case ISD::VXOR: {
5313 SDOperand LL, LH, RL, RH;
5314 SplitVectorOp(Node->getOperand(0), LL, LH);
5315 SplitVectorOp(Node->getOperand(1), RL, RH);
5316
5317 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5318 NewNumEltsNode, TypeNode);
5319 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5320 NewNumEltsNode, TypeNode);
5321 break;
5322 }
5323 case ISD::VLOAD: {
5324 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5325 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5326 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5327
5328 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5329 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5330 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5331 getIntPtrConstant(IncrementSize));
5332 // FIXME: This creates a bogus srcvalue!
5333 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5334
5335 // Build a factor node to remember that this load is independent of the
5336 // other one.
5337 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5338 Hi.getValue(1));
5339
5340 // Remember that we legalized the chain.
5341 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005342 break;
5343 }
Chris Lattner7692eb42006-03-23 21:16:34 +00005344 case ISD::VBIT_CONVERT: {
5345 // We know the result is a vector. The input may be either a vector or a
5346 // scalar value.
5347 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5348 // Lower to a store/load. FIXME: this could be improved probably.
5349 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5350
Evan Cheng786225a2006-10-05 23:01:46 +00005351 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005352 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005353 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5354 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5355 SplitVectorOp(St, Lo, Hi);
5356 } else {
5357 // If the input is a vector type, we have to either scalarize it, pack it
5358 // or convert it based on whether the input vector type is legal.
5359 SDNode *InVal = Node->getOperand(0).Val;
5360 unsigned NumElems =
5361 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5362 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5363
5364 // If the input is from a single element vector, scalarize the vector,
5365 // then treat like a scalar.
5366 if (NumElems == 1) {
5367 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5368 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5369 Op.getOperand(1), Op.getOperand(2));
5370 SplitVectorOp(Scalar, Lo, Hi);
5371 } else {
5372 // Split the input vector.
5373 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5374
5375 // Convert each of the pieces now.
5376 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5377 NewNumEltsNode, TypeNode);
5378 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5379 NewNumEltsNode, TypeNode);
5380 }
5381 break;
5382 }
5383 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005384 }
5385
5386 // Remember in a map if the values will be reused later.
5387 bool isNew =
5388 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5389 assert(isNew && "Value already expanded?!?");
5390}
5391
5392
5393/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5394/// equivalent operation that returns a scalar (e.g. F32) or packed value
5395/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5396/// type for the result.
5397SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5398 MVT::ValueType NewVT) {
5399 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5400 SDNode *Node = Op.Val;
5401
5402 // See if we already packed it.
5403 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5404 if (I != PackedNodes.end()) return I->second;
5405
5406 SDOperand Result;
5407 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005408 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005409#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00005410 Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005411#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00005412 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005413 case ISD::VADD:
5414 case ISD::VSUB:
5415 case ISD::VMUL:
5416 case ISD::VSDIV:
5417 case ISD::VUDIV:
5418 case ISD::VAND:
5419 case ISD::VOR:
5420 case ISD::VXOR:
5421 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5422 NewVT,
5423 PackVectorOp(Node->getOperand(0), NewVT),
5424 PackVectorOp(Node->getOperand(1), NewVT));
5425 break;
5426 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00005427 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5428 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005429
Evan Cheng466685d2006-10-09 20:57:25 +00005430 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5431 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattnerc7029802006-03-18 01:44:44 +00005432
5433 // Remember that we legalized the chain.
5434 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5435 break;
5436 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00005437 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00005438 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005439 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00005440 Result = Node->getOperand(0);
5441 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005442 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00005443
5444 // If all elements of the build_vector are undefs, return an undef.
5445 bool AllUndef = true;
5446 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5447 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5448 AllUndef = false;
5449 break;
5450 }
5451 if (AllUndef) {
5452 Result = DAG.getNode(ISD::UNDEF, NewVT);
5453 } else {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005454 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5455 Node->getNumOperands()-2);
Chris Lattner17614ea2006-04-08 05:34:25 +00005456 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005457 }
5458 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00005459 case ISD::VINSERT_VECTOR_ELT:
5460 if (!MVT::isVector(NewVT)) {
5461 // Returning a scalar? Must be the inserted element.
5462 Result = Node->getOperand(1);
5463 } else {
5464 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5465 PackVectorOp(Node->getOperand(0), NewVT),
5466 Node->getOperand(1), Node->getOperand(2));
5467 }
5468 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00005469 case ISD::VVECTOR_SHUFFLE:
5470 if (!MVT::isVector(NewVT)) {
5471 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5472 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5473 if (cast<ConstantSDNode>(EltNum)->getValue())
5474 Result = PackVectorOp(Node->getOperand(1), NewVT);
5475 else
5476 Result = PackVectorOp(Node->getOperand(0), NewVT);
5477 } else {
5478 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5479 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5480 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5481 Node->getOperand(2).Val->op_end()-2);
5482 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005483 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5484 Node->getOperand(2).Val->op_begin(),
5485 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattner5b2316e2006-03-28 20:24:43 +00005486
5487 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5488 PackVectorOp(Node->getOperand(0), NewVT),
5489 PackVectorOp(Node->getOperand(1), NewVT), BV);
5490 }
5491 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00005492 case ISD::VBIT_CONVERT:
5493 if (Op.getOperand(0).getValueType() != MVT::Vector)
5494 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5495 else {
5496 // If the input is a vector type, we have to either scalarize it, pack it
5497 // or convert it based on whether the input vector type is legal.
5498 SDNode *InVal = Node->getOperand(0).Val;
5499 unsigned NumElems =
5500 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5501 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5502
5503 // Figure out if there is a Packed type corresponding to this Vector
5504 // type. If so, convert to the packed type.
5505 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5506 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5507 // Turn this into a bit convert of the packed input.
5508 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5509 PackVectorOp(Node->getOperand(0), TVT));
5510 break;
5511 } else if (NumElems == 1) {
5512 // Turn this into a bit convert of the scalar input.
5513 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5514 PackVectorOp(Node->getOperand(0), EVT));
5515 break;
5516 } else {
5517 // FIXME: UNIMP!
5518 assert(0 && "Cast from unsupported vector type not implemented yet!");
5519 }
5520 }
Evan Chengdb3c6262006-04-10 18:54:36 +00005521 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005522 case ISD::VSELECT:
5523 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5524 PackVectorOp(Op.getOperand(1), NewVT),
5525 PackVectorOp(Op.getOperand(2), NewVT));
5526 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005527 }
5528
5529 if (TLI.isTypeLegal(NewVT))
5530 Result = LegalizeOp(Result);
5531 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5532 assert(isNew && "Value already packed?");
5533 return Result;
5534}
5535
Chris Lattner3e928bb2005-01-07 07:47:09 +00005536
5537// SelectionDAG::Legalize - This is the entry point for the file.
5538//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005539void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005540 if (ViewLegalizeDAGs) viewGraph();
5541
Chris Lattner3e928bb2005-01-07 07:47:09 +00005542 /// run - This is the main entry point to this class.
5543 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005544 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005545}
5546