blob: 7a3c506f4e58bc446312266830d01ed96178ac4d [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 Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.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"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000056
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 /// LegalizedNodes - For nodes that are of legal width, and that have more
58 /// than one use, this map indicates what regularized operand to use. This
59 /// allows us to avoid legalizing the same thing more than once.
60 std::map<SDOperand, SDOperand> LegalizedNodes;
61
Chris Lattner03c85462005-01-15 05:21:40 +000062 /// PromotedNodes - For nodes that are below legal width, and that have more
63 /// than one use, this map indicates what promoted value to use. This allows
64 /// us to avoid promoting the same thing more than once.
65 std::map<SDOperand, SDOperand> PromotedNodes;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// ExpandedNodes - For nodes that need to be expanded, and which have more
68 /// than one use, this map indicates which which operands are the expanded
69 /// version of the input. This allows us to avoid expanding the same node
70 /// more than once.
71 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
72
Chris Lattner8afc48e2005-01-07 22:28:47 +000073 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000074 LegalizedNodes.insert(std::make_pair(From, To));
75 // If someone requests legalization of the new node, return itself.
76 if (From != To)
77 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000078 }
Chris Lattner03c85462005-01-15 05:21:40 +000079 void AddPromotedOperand(SDOperand From, SDOperand To) {
80 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000082 // If someone requests legalization of the new node, return itself.
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000084 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
Chris Lattner9c32d3b2005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
Chris Lattner3e928bb2005-01-07 07:47:09 +000090 /// getTypeAction - Return how we should legalize values of this type, either
91 /// it is already legal or we need to expand it into multiple registers of
92 /// smaller integer type, or we need to promote it to a larger type.
93 LegalizeAction getTypeAction(MVT::ValueType VT) const {
94 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
95 }
96
97 /// isTypeLegal - Return true if this type is legal on this target.
98 ///
99 bool isTypeLegal(MVT::ValueType VT) const {
100 return getTypeAction(VT) == Legal;
101 }
102
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 void LegalizeDAG();
104
Chris Lattner456a93a2006-01-28 07:39:30 +0000105private:
106
Chris Lattner3e928bb2005-01-07 07:47:09 +0000107 SDOperand LegalizeOp(SDOperand O);
108 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000109 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000110
Chris Lattner77e77a62005-01-21 06:05:23 +0000111 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
112 SDOperand &Hi);
113 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
114 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000115
Chris Lattner35481892005-12-23 00:16:34 +0000116 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000117 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
118 SDOperand LegalOp,
119 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000120 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
121 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000122 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
123 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000124
Chris Lattner456a93a2006-01-28 07:39:30 +0000125 SDOperand ExpandBSWAP(SDOperand Op);
126 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000131 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
Chris Lattnerb89175f2005-11-19 05:51:46 +0000139static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000140 switch (VecOp) {
141 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000142 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
143 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
144 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
145 }
146}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
149 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
150 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000151 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000153}
154
Chris Lattner32fca002005-10-06 01:20:27 +0000155/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
156/// not been visited yet and if all of its operands have already been visited.
157static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
158 std::map<SDNode*, unsigned> &Visited) {
159 if (++Visited[N] != N->getNumOperands())
160 return; // Haven't visited all operands yet
161
162 Order.push_back(N);
163
164 if (N->hasOneUse()) { // Tail recurse in common case.
165 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
166 return;
167 }
168
169 // Now that we have N in, add anything that uses it if all of their operands
170 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000171 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
172 ComputeTopDownOrdering(*UI, Order, Visited);
173}
174
Chris Lattner1618beb2005-07-29 00:11:56 +0000175
Chris Lattner3e928bb2005-01-07 07:47:09 +0000176void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000177 // The legalize process is inherently a bottom-up recursive process (users
178 // legalize their uses before themselves). Given infinite stack space, we
179 // could just start legalizing on the root and traverse the whole graph. In
180 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000181 // blocks. To avoid this problem, compute an ordering of the nodes where each
182 // node is only legalized after all of its operands are legalized.
183 std::map<SDNode*, unsigned> Visited;
184 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000185
Chris Lattner32fca002005-10-06 01:20:27 +0000186 // Compute ordering from all of the leaves in the graphs, those (like the
187 // entry node) that have no operands.
188 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
189 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000190 if (I->getNumOperands() == 0) {
191 Visited[I] = 0 - 1U;
192 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000193 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000194 }
195
Chris Lattnerde202b32005-11-09 23:47:37 +0000196 assert(Order.size() == Visited.size() &&
197 Order.size() ==
198 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000199 "Error: DAG is cyclic!");
200 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000201
Chris Lattner32fca002005-10-06 01:20:27 +0000202 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
203 SDNode *N = Order[i];
204 switch (getTypeAction(N->getValueType(0))) {
205 default: assert(0 && "Bad type action!");
206 case Legal:
207 LegalizeOp(SDOperand(N, 0));
208 break;
209 case Promote:
210 PromoteOp(SDOperand(N, 0));
211 break;
212 case Expand: {
213 SDOperand X, Y;
214 ExpandOp(SDOperand(N, 0), X, Y);
215 break;
216 }
217 }
218 }
219
220 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000221 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000222 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
223 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224
225 ExpandedNodes.clear();
226 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000227 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000228
229 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000230 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000231}
232
233SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000234 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000235 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000236 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000237
Chris Lattner3e928bb2005-01-07 07:47:09 +0000238 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000239 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000240 if (Node->getNumValues() > 1) {
241 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
242 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000243 case Legal: break; // Nothing to do.
244 case Expand: {
245 SDOperand T1, T2;
246 ExpandOp(Op.getValue(i), T1, T2);
247 assert(LegalizedNodes.count(Op) &&
248 "Expansion didn't add legal operands!");
249 return LegalizedNodes[Op];
250 }
251 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000252 PromoteOp(Op.getValue(i));
253 assert(LegalizedNodes.count(Op) &&
Chris Lattner456a93a2006-01-28 07:39:30 +0000254 "Promotion didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000255 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000256 }
257 }
258
Chris Lattner45982da2005-05-12 16:53:42 +0000259 // Note that LegalizeOp may be reentered even from single-use nodes, which
260 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000261 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
262 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000263
Nate Begeman9373a812005-08-10 20:51:12 +0000264 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000265 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000266 bool isCustom = false;
267
Chris Lattner3e928bb2005-01-07 07:47:09 +0000268 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000269 case ISD::FrameIndex:
270 case ISD::EntryToken:
271 case ISD::Register:
272 case ISD::BasicBlock:
273 case ISD::TargetFrameIndex:
274 case ISD::TargetConstant:
275 case ISD::TargetConstantPool:
276 case ISD::TargetGlobalAddress:
277 case ISD::TargetExternalSymbol:
278 case ISD::VALUETYPE:
279 case ISD::SRCVALUE:
280 case ISD::STRING:
281 case ISD::CONDCODE:
282 // Primitives must all be legal.
283 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
284 "This must be legal!");
285 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000286 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000287 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
288 // If this is a target node, legalize it by legalizing the operands then
289 // passing it through.
290 std::vector<SDOperand> Ops;
291 bool Changed = false;
292 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
293 Ops.push_back(LegalizeOp(Node->getOperand(i)));
294 Changed = Changed || Node->getOperand(i) != Ops.back();
295 }
296 if (Changed)
297 if (Node->getNumValues() == 1)
298 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
299 else {
300 std::vector<MVT::ValueType> VTs(Node->value_begin(),
301 Node->value_end());
302 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
303 }
304
305 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
306 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
307 return Result.getValue(Op.ResNo);
308 }
309 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000310 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
311 assert(0 && "Do not know how to legalize this operator!");
312 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000313 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000314 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000315 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
317 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000318 case TargetLowering::Custom:
319 Tmp1 = TLI.LowerOperation(Op, DAG);
320 if (Tmp1.Val) Result = Tmp1;
321 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000322 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000323 break;
324 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000325 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000326 case ISD::AssertSext:
327 case ISD::AssertZext:
328 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000330 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000331 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000332 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000333 Result = Node->getOperand(Op.ResNo);
334 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000335 case ISD::CopyFromReg:
336 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000337 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000338 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000339 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000340 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000341 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000342 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000343 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
345 } else {
346 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
347 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000348 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
349 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000350 // Since CopyFromReg produces two values, make sure to remember that we
351 // legalized both of them.
352 AddLegalizedOperand(Op.getValue(0), Result);
353 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
354 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000355 case ISD::UNDEF: {
356 MVT::ValueType VT = Op.getValueType();
357 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000358 default: assert(0 && "This action is not supported yet!");
359 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000360 if (MVT::isInteger(VT))
361 Result = DAG.getConstant(0, VT);
362 else if (MVT::isFloatingPoint(VT))
363 Result = DAG.getConstantFP(0, VT);
364 else
365 assert(0 && "Unknown value type!");
366 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000367 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000368 break;
369 }
370 break;
371 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000372
373 case ISD::LOCATION:
374 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
376
377 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
378 case TargetLowering::Promote:
379 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000380 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000381 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000382 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
383 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
384
385 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
386 const std::string &FName =
387 cast<StringSDNode>(Node->getOperand(3))->getValue();
388 const std::string &DirName =
389 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000390 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000391
Jim Laskeye81aecb2005-12-21 20:51:37 +0000392 std::vector<SDOperand> Ops;
393 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000394 SDOperand LineOp = Node->getOperand(1);
395 SDOperand ColOp = Node->getOperand(2);
396
397 if (useDEBUG_LOC) {
398 Ops.push_back(LineOp); // line #
399 Ops.push_back(ColOp); // col #
400 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
401 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
402 } else {
403 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
404 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
405 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
406 Ops.push_back(DAG.getConstant(ID, MVT::i32));
407 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
408 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000409 } else {
410 Result = Tmp1; // chain
411 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000412 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000413 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000414 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000415 if (Tmp1 != Node->getOperand(0) ||
416 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000417 std::vector<SDOperand> Ops;
418 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000419 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
420 Ops.push_back(Node->getOperand(1)); // line # must be legal.
421 Ops.push_back(Node->getOperand(2)); // col # must be legal.
422 } else {
423 // Otherwise promote them.
424 Ops.push_back(PromoteOp(Node->getOperand(1)));
425 Ops.push_back(PromoteOp(Node->getOperand(2)));
426 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000427 Ops.push_back(Node->getOperand(3)); // filename must be legal.
428 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000429 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000430 }
431 break;
432 }
433 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000434
435 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000436 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000437 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000438 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000439 case TargetLowering::Legal:
440 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
441 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
442 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
443 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000445 break;
446 }
447 break;
448
449 case ISD::DEBUG_LABEL:
450 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
451 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000452 default: assert(0 && "This action is not supported yet!");
453 case TargetLowering::Legal:
454 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
455 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000457 break;
458 }
459 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000460
Chris Lattner3e928bb2005-01-07 07:47:09 +0000461 case ISD::Constant:
462 // We know we don't need to expand constants here, constants only have one
463 // value and we check that it is fine above.
464
465 // FIXME: Maybe we should handle things like targets that don't support full
466 // 32-bit immediates?
467 break;
468 case ISD::ConstantFP: {
469 // Spill FP immediates to the constant pool if the target cannot directly
470 // codegen them. Targets often have some immediate values that can be
471 // efficiently generated into an FP register without a load. We explicitly
472 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
474
475 // Check to see if this FP immediate is already legal.
476 bool isLegal = false;
477 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
478 E = TLI.legal_fpimm_end(); I != E; ++I)
479 if (CFP->isExactlyValue(*I)) {
480 isLegal = true;
481 break;
482 }
483
484 if (!isLegal) {
485 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000486 bool Extend = false;
487
Chris Lattner456a93a2006-01-28 07:39:30 +0000488 // If a FP immediate is precise when represented as a float and if the
489 // target can do an extending load from float to double, we put it into
490 // the constant pool as a float, even if it's is statically typed as a
491 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492 MVT::ValueType VT = CFP->getValueType(0);
493 bool isDouble = VT == MVT::f64;
494 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
495 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000496 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
497 // Only do this if the target has a native EXTLOAD instruction from
498 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000499 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000500 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
501 VT = MVT::f32;
502 Extend = true;
503 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504
Chris Lattner456a93a2006-01-28 07:39:30 +0000505 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000506 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000507 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
508 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000509 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000510 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
511 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000512 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000513 }
514 break;
515 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000516 case ISD::ConstantVec: {
517 // We assume that vector constants are not legal, and will be immediately
518 // spilled to the constant pool.
519 //
Chris Lattner456a93a2006-01-28 07:39:30 +0000520 // FIXME: Allow custom lowering to TargetConstantVec's.
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000521 //
522 // Create a ConstantPacked, and put it in the constant pool.
523 std::vector<Constant*> CV;
524 MVT::ValueType VT = Node->getValueType(0);
525 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
526 SDOperand OpN = Node->getOperand(I);
527 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
528 if (MVT::isFloatingPoint(VT))
529 CV.push_back(ConstantFP::get(OpNTy,
530 cast<ConstantFPSDNode>(OpN)->getValue()));
531 else
532 CV.push_back(ConstantUInt::get(OpNTy,
533 cast<ConstantSDNode>(OpN)->getValue()));
534 }
535 Constant *CP = ConstantPacked::get(CV);
Chris Lattner456a93a2006-01-28 07:39:30 +0000536 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000537 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
538 break;
539 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000540 case ISD::TokenFactor:
541 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000542 Tmp1 = LegalizeOp(Node->getOperand(0));
543 Tmp2 = LegalizeOp(Node->getOperand(1));
544 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
545 } else if (Node->getNumOperands() == 3) {
546 Tmp1 = LegalizeOp(Node->getOperand(0));
547 Tmp2 = LegalizeOp(Node->getOperand(1));
548 Tmp3 = LegalizeOp(Node->getOperand(2));
549 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000550 } else {
551 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000552 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000553 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
554 Ops.push_back(LegalizeOp(Node->getOperand(i)));
555 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000556 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000557 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000558
Chris Lattner16cd04d2005-05-12 23:24:06 +0000559 case ISD::CALLSEQ_START:
560 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000562 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000563 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000564 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000565 Node->setAdjCallChain(Tmp1);
Chris Lattner6a542892006-01-24 05:48:21 +0000566
567 // If this has a flag input, do legalize it.
568 if (Node->getOperand(Node->getNumOperands()-1).getValueType() == MVT::Flag){
569 Tmp1 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
570 if (Tmp1 != Node->getOperand(Node->getNumOperands()-1))
571 Node->setAdjCallFlag(Tmp1);
572 }
Nate Begeman27d404c2005-10-04 00:37:37 +0000573
Chris Lattner16cd04d2005-05-12 23:24:06 +0000574 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000575 // nodes are treated specially and are mutated in place. This makes the dag
576 // legalization process more efficient and also makes libcall insertion
577 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000578 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000579 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
581 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
582 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000583 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000584
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000585 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000586 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000587 switch (TLI.getOperationAction(Node->getOpcode(),
588 Node->getValueType(0))) {
589 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000590 case TargetLowering::Expand: {
591 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
592 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
593 " not tell us which reg is the stack pointer!");
594 SDOperand Chain = Tmp1.getOperand(0);
595 SDOperand Size = Tmp2.getOperand(1);
596 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
597 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
598 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000599 Tmp1 = LegalizeOp(Tmp1);
600 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000601 break;
602 }
603 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000604 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
605 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000606 Tmp1 = LegalizeOp(Tmp3);
607 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000608 }
Chris Lattner903d2782006-01-15 08:54:32 +0000609 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000610 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000611 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000612 }
Chris Lattner903d2782006-01-15 08:54:32 +0000613 // Since this op produce two values, make sure to remember that we
614 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000615 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
616 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000617 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000618 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000619 case ISD::INLINEASM:
620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
621 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000622 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000623 Tmp2 = Tmp3 = SDOperand(0, 0);
624 else
625 Tmp3 = LegalizeOp(Tmp2);
626
627 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
628 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
629 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000630 if (Tmp3.Val) Ops.back() = Tmp3;
631 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000632 }
633
634 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000635 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000636 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
637 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000638 case ISD::BR:
639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000640 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000641 break;
642
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000643 case ISD::BRCOND:
644 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000645
Chris Lattner47e92232005-01-18 19:27:06 +0000646 switch (getTypeAction(Node->getOperand(1).getValueType())) {
647 case Expand: assert(0 && "It's impossible to expand bools");
648 case Legal:
649 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
650 break;
651 case Promote:
652 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
653 break;
654 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000655
656 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000657 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000658
659 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
660 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000661 case TargetLowering::Legal: break;
662 case TargetLowering::Custom:
663 Tmp1 = TLI.LowerOperation(Result, DAG);
664 if (Tmp1.Val) Result = Tmp1;
665 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000666 case TargetLowering::Expand:
667 // Expand brcond's setcc into its constituent parts and create a BR_CC
668 // Node.
669 if (Tmp2.getOpcode() == ISD::SETCC) {
670 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
671 Tmp2.getOperand(0), Tmp2.getOperand(1),
672 Node->getOperand(2));
673 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000674 // Make sure the condition is either zero or one. It may have been
675 // promoted from something else.
676 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
677
Nate Begeman7cbd5252005-08-16 19:49:35 +0000678 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
679 DAG.getCondCode(ISD::SETNE), Tmp2,
680 DAG.getConstant(0, Tmp2.getValueType()),
681 Node->getOperand(2));
682 }
683 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000684 }
685 break;
686 case ISD::BR_CC:
687 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000688 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000689 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
690 Node->getOperand(2), // LHS
691 Node->getOperand(3), // RHS
692 Node->getOperand(1)));
693 // If we get a SETCC back from legalizing the SETCC node we just
694 // created, then use its LHS, RHS, and CC directly in creating a new
695 // node. Otherwise, select between the true and false value based on
696 // comparing the result of the legalized with zero.
697 if (Tmp2.getOpcode() == ISD::SETCC) {
698 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
699 Tmp2.getOperand(0), Tmp2.getOperand(1),
700 Node->getOperand(4));
701 } else {
702 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
703 DAG.getCondCode(ISD::SETNE),
704 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
705 Node->getOperand(4));
706 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000707 break;
708 }
709
710 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
711 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
712
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000713 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
714 Tmp3, Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +0000715
Chris Lattner181b7a32005-12-17 23:46:46 +0000716 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
717 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000718 case TargetLowering::Legal: break;
719 case TargetLowering::Custom:
720 Tmp4 = TLI.LowerOperation(Result, DAG);
721 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +0000722 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000723 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000724 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000725 case ISD::BRCONDTWOWAY:
726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
727 switch (getTypeAction(Node->getOperand(1).getValueType())) {
728 case Expand: assert(0 && "It's impossible to expand bools");
729 case Legal:
730 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
731 break;
732 case Promote:
733 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
734 break;
735 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000736
Chris Lattner411e8882005-04-09 03:30:19 +0000737 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
738 // pair.
739 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
740 case TargetLowering::Promote:
741 default: assert(0 && "This action is not supported yet!");
742 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000743 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
744 Node->getOperand(3));
Chris Lattner411e8882005-04-09 03:30:19 +0000745 break;
746 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000747 // If BRTWOWAY_CC is legal for this target, then simply expand this node
748 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
749 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000750 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000751 if (Tmp2.getOpcode() == ISD::SETCC) {
752 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
753 Tmp2.getOperand(0), Tmp2.getOperand(1),
754 Node->getOperand(2), Node->getOperand(3));
755 } else {
756 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
757 DAG.getConstant(0, Tmp2.getValueType()),
758 Node->getOperand(2), Node->getOperand(3));
759 }
760 } else {
761 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +0000762 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000763 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
764 }
Chris Lattner411e8882005-04-09 03:30:19 +0000765 break;
766 }
767 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000768 case ISD::BRTWOWAY_CC:
769 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000770 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000771 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
772 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
773 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
774 Tmp3 != Node->getOperand(3)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000775 std::vector<SDOperand> Ops;
776 Ops.push_back(Tmp1);
777 Ops.push_back(Node->getOperand(1));
778 Ops.push_back(Tmp2);
779 Ops.push_back(Tmp3);
780 Ops.push_back(Node->getOperand(4));
781 Ops.push_back(Node->getOperand(5));
782 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000783 }
784 break;
785 } else {
786 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
787 Node->getOperand(2), // LHS
788 Node->getOperand(3), // RHS
789 Node->getOperand(1)));
790 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
791 // pair.
792 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
793 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000794 case TargetLowering::Legal: {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000795 // If we get a SETCC back from legalizing the SETCC node we just
796 // created, then use its LHS, RHS, and CC directly in creating a new
797 // node. Otherwise, select between the true and false value based on
798 // comparing the result of the legalized with zero.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000799 std::vector<SDOperand> Ops;
800 Ops.push_back(Tmp1);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000801 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000802 Ops.push_back(Tmp2.getOperand(2));
803 Ops.push_back(Tmp2.getOperand(0));
804 Ops.push_back(Tmp2.getOperand(1));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000805 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000806 Ops.push_back(DAG.getCondCode(ISD::SETNE));
807 Ops.push_back(Tmp2);
808 Ops.push_back(DAG.getConstant(0, Tmp2.getValueType()));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000809 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000810 Ops.push_back(Node->getOperand(4));
811 Ops.push_back(Node->getOperand(5));
812 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000813 break;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000814 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000815 case TargetLowering::Expand:
816 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
817 Node->getOperand(4));
818 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
819 break;
820 }
821 }
822 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000823 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000826
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000827 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
829 Tmp2 = Result.getValue(0);
830 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000831
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000832 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
833 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000834 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000835 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000836 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +0000837 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000838 Tmp2 = LegalizeOp(Tmp1);
839 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000840 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000841 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000842 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000843 // Since loads produce two values, make sure to remember that we
844 // legalized both of them.
845 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
846 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
847 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000848 }
Chris Lattner0f69b292005-01-15 06:18:18 +0000849 case ISD::EXTLOAD:
850 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000851 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000852 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
853 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000854
Chris Lattner5f056bf2005-07-10 01:55:33 +0000855 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000856 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000857 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000858 case TargetLowering::Promote:
859 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
861 DAG.getValueType(MVT::i8));
862 Tmp1 = Result.getValue(0);
863 Tmp2 = Result.getValue(1);
864 break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000865 case TargetLowering::Custom:
866 isCustom = true;
867 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +0000868 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
870 Node->getOperand(3));
871 Tmp1 = Result.getValue(0);
872 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000873
874 if (isCustom) {
875 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
876 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000877 Tmp1 = LegalizeOp(Tmp3);
878 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +0000879 }
880 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000881 break;
Chris Lattner01ff7212005-04-10 22:54:25 +0000882 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +0000883 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000884 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
885 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000886 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000887 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
888 Tmp2 = LegalizeOp(Load.getValue(1));
889 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000890 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000891 assert(Node->getOpcode() != ISD::EXTLOAD &&
892 "EXTLOAD should always be supported!");
893 // Turn the unsupported load into an EXTLOAD followed by an explicit
894 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000895 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
896 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000897 SDOperand ValRes;
898 if (Node->getOpcode() == ISD::SEXTLOAD)
899 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000900 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000901 else
902 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000903 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
904 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
905 break;
Chris Lattner01ff7212005-04-10 22:54:25 +0000906 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000907 // Since loads produce two values, make sure to remember that we legalized
908 // both of them.
909 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
910 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
911 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +0000912 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000913 case ISD::EXTRACT_ELEMENT: {
914 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
915 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000916 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +0000917 case Legal:
918 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
919 // 1 -> Hi
920 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
921 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
922 TLI.getShiftAmountTy()));
923 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
924 } else {
925 // 0 -> Lo
926 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
927 Node->getOperand(0));
928 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000929 break;
930 case Expand:
931 // Get both the low and high parts.
932 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
933 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
934 Result = Tmp2; // 1 -> Hi
935 else
936 Result = Tmp1; // 0 -> Lo
937 break;
938 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000939 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +0000940 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000941
942 case ISD::CopyToReg:
943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000944
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000945 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000946 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +0000947 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000948 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000949 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000950 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +0000951 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000952 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000953 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000954 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
956 Tmp3);
957 } else {
958 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +0000959 }
960
961 // Since this produces two values, make sure to remember that we legalized
962 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000964 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000965 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +0000966 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000967 break;
968
969 case ISD::RET:
970 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
971 switch (Node->getNumOperands()) {
972 case 2: // ret val
973 switch (getTypeAction(Node->getOperand(1).getValueType())) {
974 case Legal:
975 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000976 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000977 break;
978 case Expand: {
979 SDOperand Lo, Hi;
980 ExpandOp(Node->getOperand(1), Lo, Hi);
981 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000982 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000983 }
984 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000985 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
987 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000988 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000989 }
990 break;
991 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000992 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000993 break;
994 default: { // ret <values>
995 std::vector<SDOperand> NewValues;
996 NewValues.push_back(Tmp1);
997 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
998 switch (getTypeAction(Node->getOperand(i).getValueType())) {
999 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001000 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001001 break;
1002 case Expand: {
1003 SDOperand Lo, Hi;
1004 ExpandOp(Node->getOperand(i), Lo, Hi);
1005 NewValues.push_back(Lo);
1006 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001007 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001008 }
1009 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001010 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001011 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001012
1013 if (NewValues.size() == Node->getNumOperands())
1014 Result = DAG.UpdateNodeOperands(Result, NewValues);
1015 else
1016 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001017 break;
1018 }
1019 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001020
Chris Lattner47f5bea2006-01-06 05:47:48 +00001021 switch (TLI.getOperationAction(Node->getOpcode(),
1022 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001023 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001024 case TargetLowering::Legal: break;
1025 case TargetLowering::Custom:
1026 Tmp1 = TLI.LowerOperation(Result, DAG);
1027 if (Tmp1.Val) Result = Tmp1;
Evan Cheng17c428e2006-01-06 00:41:43 +00001028 break;
1029 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001030 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001031 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1033 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1034
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001035 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001036 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner03c85462005-01-15 05:21:40 +00001037 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001038 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001039 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001040 } else {
1041 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001042 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001043 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001044 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1045 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001046 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001047 }
1048
Chris Lattner3e928bb2005-01-07 07:47:09 +00001049 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1050 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001051 Tmp3 = LegalizeOp(Node->getOperand(1));
1052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1053 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001054
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001055 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001056 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1057 default: assert(0 && "This action is not supported yet!");
1058 case TargetLowering::Legal: break;
1059 case TargetLowering::Custom:
1060 Tmp1 = TLI.LowerOperation(Result, DAG);
1061 if (Tmp1.Val) Result = Tmp1;
1062 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001063 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001064 break;
1065 }
1066 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001067 // Truncate the value and store the result.
1068 Tmp3 = PromoteOp(Node->getOperand(1));
1069 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001070 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001071 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001072 break;
1073
Chris Lattner3e928bb2005-01-07 07:47:09 +00001074 case Expand:
1075 SDOperand Lo, Hi;
1076 ExpandOp(Node->getOperand(1), Lo, Hi);
1077
1078 if (!TLI.isLittleEndian())
1079 std::swap(Lo, Hi);
1080
Chris Lattneredb1add2005-05-11 04:51:16 +00001081 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1082 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001083 // If this is a vector type, then we have to calculate the increment as
1084 // the product of the element size in bytes, and the number of elements
1085 // in the high half of the vector.
Chris Lattner456a93a2006-01-28 07:39:30 +00001086 unsigned IncrementSize;
Nate Begemanab48be32005-11-22 18:16:00 +00001087 if (MVT::Vector == Hi.getValueType()) {
1088 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1089 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1090 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1091 } else {
1092 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1093 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001094 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1095 getIntPtrConstant(IncrementSize));
1096 assert(isTypeLegal(Tmp2.getValueType()) &&
1097 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001098 // FIXME: This sets the srcvalue of both halves to be the same, which is
1099 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001100 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1101 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001102 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1103 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001104 }
1105 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001106 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001107 case ISD::PCMARKER:
1108 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001110 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001111 case ISD::STACKSAVE:
1112 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001113 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1114 Tmp1 = Result.getValue(0);
1115 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001116
Chris Lattner140d53c2006-01-13 02:50:02 +00001117 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1118 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001119 case TargetLowering::Legal: break;
1120 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001121 Tmp3 = TLI.LowerOperation(Result, DAG);
1122 if (Tmp3.Val) {
1123 Tmp1 = LegalizeOp(Tmp3);
1124 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001125 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001126 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001127 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001128 // Expand to CopyFromReg if the target set
1129 // StackPointerRegisterToSaveRestore.
1130 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001131 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001132 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001133 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001134 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001135 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1136 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001137 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001138 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001139 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001140
1141 // Since stacksave produce two values, make sure to remember that we
1142 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001143 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1144 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1145 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001146
Chris Lattner140d53c2006-01-13 02:50:02 +00001147 case ISD::STACKRESTORE:
1148 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1149 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001151
1152 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1153 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001154 case TargetLowering::Legal: break;
1155 case TargetLowering::Custom:
1156 Tmp1 = TLI.LowerOperation(Result, DAG);
1157 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001158 break;
1159 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001160 // Expand to CopyToReg if the target set
1161 // StackPointerRegisterToSaveRestore.
1162 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1163 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1164 } else {
1165 Result = Tmp1;
1166 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001167 break;
1168 }
1169 break;
1170
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001171 case ISD::READCYCLECOUNTER:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001173 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001174
1175 // Since rdcc produce two values, make sure to remember that we legalized
1176 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001177 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001178 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001179 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001180
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001181 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001182 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1183 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1184
Chris Lattner456a93a2006-01-28 07:39:30 +00001185 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1186 "Cannot handle illegal TRUNCSTORE yet!");
1187 Tmp2 = LegalizeOp(Node->getOperand(1));
1188
1189 // The only promote case we handle is TRUNCSTORE:i1 X into
1190 // -> TRUNCSTORE:i8 (and X, 1)
1191 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1192 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1193 TargetLowering::Promote) {
1194 // Promote the bool to a mask then store.
1195 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1196 DAG.getConstant(1, Tmp2.getValueType()));
1197 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1198 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001199
Chris Lattner456a93a2006-01-28 07:39:30 +00001200 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1201 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001202 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1203 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001204 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001205
Chris Lattner456a93a2006-01-28 07:39:30 +00001206 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1207 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1208 default: assert(0 && "This action is not supported yet!");
1209 case TargetLowering::Legal: break;
1210 case TargetLowering::Custom:
1211 Tmp1 = TLI.LowerOperation(Result, DAG);
1212 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001213 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001214 }
1215 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001216 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001217 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1219 case Expand: assert(0 && "It's impossible to expand bools");
1220 case Legal:
1221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1222 break;
1223 case Promote:
1224 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1225 break;
1226 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001227 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001228 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001229
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001230 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001231
Nate Begemanb942a3d2005-08-23 04:29:48 +00001232 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001233 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001234 case TargetLowering::Legal: break;
1235 case TargetLowering::Custom: {
1236 Tmp1 = TLI.LowerOperation(Result, DAG);
1237 if (Tmp1.Val) Result = Tmp1;
1238 break;
1239 }
Nate Begeman9373a812005-08-10 20:51:12 +00001240 case TargetLowering::Expand:
1241 if (Tmp1.getOpcode() == ISD::SETCC) {
1242 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1243 Tmp2, Tmp3,
1244 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1245 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001246 // Make sure the condition is either zero or one. It may have been
1247 // promoted from something else.
1248 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001249 Result = DAG.getSelectCC(Tmp1,
1250 DAG.getConstant(0, Tmp1.getValueType()),
1251 Tmp2, Tmp3, ISD::SETNE);
1252 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001253 break;
1254 case TargetLowering::Promote: {
1255 MVT::ValueType NVT =
1256 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1257 unsigned ExtOp, TruncOp;
1258 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001259 ExtOp = ISD::ANY_EXTEND;
1260 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001261 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001262 ExtOp = ISD::FP_EXTEND;
1263 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001264 }
1265 // Promote each of the values to the new type.
1266 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1267 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1268 // Perform the larger operation, then round down.
1269 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1270 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1271 break;
1272 }
1273 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001274 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001275 case ISD::SELECT_CC:
1276 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1277 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1278
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001279 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001280 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1281 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001282
1283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4,
1284 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001285
Chris Lattner23004e52005-08-26 00:23:59 +00001286 // Everything is legal, see if we should expand this op or something.
1287 switch (TLI.getOperationAction(ISD::SELECT_CC,
1288 Node->getOperand(0).getValueType())) {
1289 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001290 case TargetLowering::Legal: break;
1291 case TargetLowering::Custom:
1292 Tmp1 = TLI.LowerOperation(Result, DAG);
1293 if (Tmp1.Val) Result = Tmp1;
Chris Lattner23004e52005-08-26 00:23:59 +00001294 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001295 }
1296 break;
1297 } else {
1298 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1299 Node->getOperand(0), // LHS
1300 Node->getOperand(1), // RHS
1301 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001302 // If we get a SETCC back from legalizing the SETCC node we just
1303 // created, then use its LHS, RHS, and CC directly in creating a new
1304 // node. Otherwise, select between the true and false value based on
1305 // comparing the result of the legalized with zero.
1306 if (Tmp1.getOpcode() == ISD::SETCC) {
1307 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1308 Tmp1.getOperand(0), Tmp1.getOperand(1),
1309 Tmp3, Tmp4, Tmp1.getOperand(2));
1310 } else {
1311 Result = DAG.getSelectCC(Tmp1,
1312 DAG.getConstant(0, Tmp1.getValueType()),
1313 Tmp3, Tmp4, ISD::SETNE);
1314 }
Nate Begeman9373a812005-08-10 20:51:12 +00001315 }
1316 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001317 case ISD::SETCC:
1318 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1319 case Legal:
1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1321 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001322 break;
1323 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001324 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1325 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1326
1327 // If this is an FP compare, the operands have already been extended.
1328 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1329 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001330 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001331
1332 // Otherwise, we have to insert explicit sign or zero extends. Note
1333 // that we could insert sign extends for ALL conditions, but zero extend
1334 // is cheaper on many machines (an AND instead of two shifts), so prefer
1335 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001336 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001337 default: assert(0 && "Unknown integer comparison!");
1338 case ISD::SETEQ:
1339 case ISD::SETNE:
1340 case ISD::SETUGE:
1341 case ISD::SETUGT:
1342 case ISD::SETULE:
1343 case ISD::SETULT:
1344 // ALL of these operations will work if we either sign or zero extend
1345 // the operands (including the unsigned comparisons!). Zero extend is
1346 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001347 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1348 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001349 break;
1350 case ISD::SETGE:
1351 case ISD::SETGT:
1352 case ISD::SETLT:
1353 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001354 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1355 DAG.getValueType(VT));
1356 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1357 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001358 break;
1359 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001360 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001361 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001362 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001363 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1364 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1365 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001366 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001367 case ISD::SETEQ:
1368 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001369 if (RHSLo == RHSHi)
1370 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1371 if (RHSCST->isAllOnesValue()) {
1372 // Comparison to -1.
1373 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001374 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001375 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001376 }
1377
Chris Lattner3e928bb2005-01-07 07:47:09 +00001378 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1379 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1380 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001381 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001382 break;
1383 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001384 // If this is a comparison of the sign bit, just look at the top part.
1385 // X > -1, x < 0
1386 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001387 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001388 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001389 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001390 (CST->isAllOnesValue()))) { // X > -1
1391 Tmp1 = LHSHi;
1392 Tmp2 = RHSHi;
1393 break;
1394 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001395
Chris Lattner3e928bb2005-01-07 07:47:09 +00001396 // FIXME: This generated code sucks.
1397 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001398 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001399 default: assert(0 && "Unknown integer setcc!");
1400 case ISD::SETLT:
1401 case ISD::SETULT: LowCC = ISD::SETULT; break;
1402 case ISD::SETGT:
1403 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1404 case ISD::SETLE:
1405 case ISD::SETULE: LowCC = ISD::SETULE; break;
1406 case ISD::SETGE:
1407 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1408 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001409
Chris Lattner3e928bb2005-01-07 07:47:09 +00001410 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1411 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1412 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1413
1414 // NOTE: on targets without efficient SELECT of bools, we can always use
1415 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001416 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1417 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1418 Node->getOperand(2));
1419 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001420 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1421 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001422 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001423 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001424 }
1425 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001426
Chris Lattner456a93a2006-01-28 07:39:30 +00001427 switch (TLI.getOperationAction(ISD::SETCC,
1428 Node->getOperand(0).getValueType())) {
1429 default: assert(0 && "Cannot handle this action for SETCC yet!");
1430 case TargetLowering::Custom:
1431 isCustom = true;
1432 // FALLTHROUGH.
1433 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001435 if (isCustom) {
1436 Tmp3 = TLI.LowerOperation(Result, DAG);
1437 if (Tmp3.Val) Result = Tmp3;
1438 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001439 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001440 case TargetLowering::Promote: {
1441 // First step, figure out the appropriate operation to use.
1442 // Allow SETCC to not be supported for all legal data types
1443 // Mostly this targets FP
1444 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1445 MVT::ValueType OldVT = NewInTy;
1446
1447 // Scan for the appropriate larger type to use.
1448 while (1) {
1449 NewInTy = (MVT::ValueType)(NewInTy+1);
1450
1451 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1452 "Fell off of the edge of the integer world");
1453 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1454 "Fell off of the edge of the floating point world");
1455
1456 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001457 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001458 break;
1459 }
1460 if (MVT::isInteger(NewInTy))
1461 assert(0 && "Cannot promote Legal Integer SETCC yet");
1462 else {
1463 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1464 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1465 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001466 Tmp1 = LegalizeOp(Tmp1);
1467 Tmp2 = LegalizeOp(Tmp2);
1468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001469 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001470 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001471 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001472 case TargetLowering::Expand:
1473 // Expand a setcc node into a select_cc of the same condition, lhs, and
1474 // rhs that selects between const 1 (true) and const 0 (false).
1475 MVT::ValueType VT = Node->getValueType(0);
1476 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1477 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1478 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001479 break;
1480 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001481 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001482 case ISD::MEMSET:
1483 case ISD::MEMCPY:
1484 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001485 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001486 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1487
1488 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1489 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1490 case Expand: assert(0 && "Cannot expand a byte!");
1491 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001492 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001493 break;
1494 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001495 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001496 break;
1497 }
1498 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001499 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001500 }
Chris Lattner272455b2005-02-02 03:44:41 +00001501
1502 SDOperand Tmp4;
1503 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001504 case Expand: {
1505 // Length is too big, just take the lo-part of the length.
1506 SDOperand HiPart;
1507 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1508 break;
1509 }
Chris Lattnere5605212005-01-28 22:29:18 +00001510 case Legal:
1511 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001512 break;
1513 case Promote:
1514 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001515 break;
1516 }
1517
1518 SDOperand Tmp5;
1519 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1520 case Expand: assert(0 && "Cannot expand this yet!");
1521 case Legal:
1522 Tmp5 = LegalizeOp(Node->getOperand(4));
1523 break;
1524 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001525 Tmp5 = PromoteOp(Node->getOperand(4));
1526 break;
1527 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001528
1529 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1530 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001531 case TargetLowering::Custom:
1532 isCustom = true;
1533 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001534 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001536 if (isCustom) {
1537 Tmp1 = TLI.LowerOperation(Result, DAG);
1538 if (Tmp1.Val) Result = Tmp1;
1539 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001540 break;
1541 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001542 // Otherwise, the target does not support this operation. Lower the
1543 // operation to an explicit libcall as appropriate.
1544 MVT::ValueType IntPtr = TLI.getPointerTy();
1545 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1546 std::vector<std::pair<SDOperand, const Type*> > Args;
1547
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001548 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001549 if (Node->getOpcode() == ISD::MEMSET) {
1550 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1551 // Extend the ubyte argument to be an int value for the call.
1552 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1553 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1554 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1555
1556 FnName = "memset";
1557 } else if (Node->getOpcode() == ISD::MEMCPY ||
1558 Node->getOpcode() == ISD::MEMMOVE) {
1559 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1560 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1561 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1562 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1563 } else {
1564 assert(0 && "Unknown op!");
1565 }
Chris Lattner45982da2005-05-12 16:53:42 +00001566
Chris Lattnere1bd8222005-01-11 05:57:22 +00001567 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001568 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001569 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001570 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001571 break;
1572 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001573 }
1574 break;
1575 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001576
1577 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001578 Tmp1 = LegalizeOp(Node->getOperand(0));
1579 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001581
Chris Lattner52d08bd2005-05-09 20:23:03 +00001582 // Since these produce two values, make sure to remember that we legalized
1583 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner52d08bd2005-05-09 20:23:03 +00001585 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001586 return Result;
Chris Lattner52d08bd2005-05-09 20:23:03 +00001587 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001588 Tmp1 = LegalizeOp(Node->getOperand(0));
1589 Tmp2 = LegalizeOp(Node->getOperand(1));
1590 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001592 break;
1593
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001594 case ISD::READIO:
1595 Tmp1 = LegalizeOp(Node->getOperand(0));
1596 Tmp2 = LegalizeOp(Node->getOperand(1));
1597
1598 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1599 case TargetLowering::Custom:
1600 default: assert(0 && "This action not implemented for this operation!");
1601 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001602 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001603 break;
1604 case TargetLowering::Expand:
1605 // Replace this with a load from memory.
1606 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1607 Node->getOperand(1), DAG.getSrcValue(NULL));
1608 Result = LegalizeOp(Result);
1609 break;
1610 }
1611
1612 // Since these produce two values, make sure to remember that we legalized
1613 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001614 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001615 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1616 return Result.getValue(Op.ResNo);
1617
1618 case ISD::WRITEIO:
1619 Tmp1 = LegalizeOp(Node->getOperand(0));
1620 Tmp2 = LegalizeOp(Node->getOperand(1));
1621 Tmp3 = LegalizeOp(Node->getOperand(2));
1622
1623 switch (TLI.getOperationAction(Node->getOpcode(),
1624 Node->getOperand(1).getValueType())) {
1625 case TargetLowering::Custom:
1626 default: assert(0 && "This action not implemented for this operation!");
1627 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001628 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001629 break;
1630 case TargetLowering::Expand:
1631 // Replace this with a store to memory.
1632 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1633 Node->getOperand(1), Node->getOperand(2),
1634 DAG.getSrcValue(NULL));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001635 break;
1636 }
1637 break;
1638
Chris Lattner84f67882005-01-20 18:52:28 +00001639 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001640 case ISD::SUB_PARTS:
1641 case ISD::SHL_PARTS:
1642 case ISD::SRA_PARTS:
1643 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001644 std::vector<SDOperand> Ops;
1645 bool Changed = false;
1646 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1647 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1648 Changed |= Ops.back() != Node->getOperand(i);
1649 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001650 if (Changed)
1651 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001652
Evan Cheng05a2d562006-01-09 18:31:59 +00001653 switch (TLI.getOperationAction(Node->getOpcode(),
1654 Node->getValueType(0))) {
1655 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001656 case TargetLowering::Legal: break;
1657 case TargetLowering::Custom:
1658 Tmp1 = TLI.LowerOperation(Result, DAG);
1659 if (Tmp1.Val) {
1660 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001661 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001662 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001663 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1664 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001665 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001666 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001667 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001668 return RetVal;
1669 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001670 break;
1671 }
1672
Chris Lattner2c8086f2005-04-02 05:00:07 +00001673 // Since these produce multiple values, make sure to remember that we
1674 // legalized all of them.
1675 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1676 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1677 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001678 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001679
1680 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001681 case ISD::ADD:
1682 case ISD::SUB:
1683 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001684 case ISD::MULHS:
1685 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001686 case ISD::UDIV:
1687 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001688 case ISD::AND:
1689 case ISD::OR:
1690 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001691 case ISD::SHL:
1692 case ISD::SRL:
1693 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001694 case ISD::FADD:
1695 case ISD::FSUB:
1696 case ISD::FMUL:
1697 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001698 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001699 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1700 case Expand: assert(0 && "Not possible");
1701 case Legal:
1702 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1703 break;
1704 case Promote:
1705 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1706 break;
1707 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001708
1709 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001710
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001711 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001712 default: assert(0 && "Operation not supported");
1713 case TargetLowering::Legal: break;
1714 case TargetLowering::Custom:
1715 Tmp1 = TLI.LowerOperation(Result, DAG);
1716 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001717 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001718 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001719 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001720
Nate Begeman419f8b62005-10-18 00:27:41 +00001721 case ISD::BUILD_PAIR: {
1722 MVT::ValueType PairTy = Node->getValueType(0);
1723 // TODO: handle the case where the Lo and Hi operands are not of legal type
1724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1725 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1726 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001727 case TargetLowering::Promote:
1728 case TargetLowering::Custom:
1729 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001730 case TargetLowering::Legal:
1731 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1732 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1733 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001734 case TargetLowering::Expand:
1735 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1736 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1737 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1738 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1739 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001740 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001741 break;
1742 }
1743 break;
1744 }
1745
Nate Begemanc105e192005-04-06 00:23:54 +00001746 case ISD::UREM:
1747 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001748 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001749 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1750 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001751
Nate Begemanc105e192005-04-06 00:23:54 +00001752 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001753 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1754 case TargetLowering::Custom:
1755 isCustom = true;
1756 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001757 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001758 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001759 if (isCustom) {
1760 Tmp1 = TLI.LowerOperation(Result, DAG);
1761 if (Tmp1.Val) Result = Tmp1;
1762 }
Nate Begemanc105e192005-04-06 00:23:54 +00001763 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001764 case TargetLowering::Expand:
1765 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001766 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001767 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001768 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001769 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1770 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1771 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1772 } else {
1773 // Floating point mod -> fmod libcall.
1774 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1775 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001776 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001777 }
1778 break;
1779 }
1780 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001781 case ISD::VAARG: {
1782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1783 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1784
Chris Lattner5c62f332006-01-28 07:42:08 +00001785 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001786 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1787 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001788 case TargetLowering::Custom:
1789 isCustom = true;
1790 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001791 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1793 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001794 Tmp1 = Result.getValue(1);
1795
1796 if (isCustom) {
1797 Tmp2 = TLI.LowerOperation(Result, DAG);
1798 if (Tmp2.Val) {
1799 Result = LegalizeOp(Tmp2);
1800 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1801 }
1802 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001803 break;
1804 case TargetLowering::Expand: {
1805 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1806 Node->getOperand(2));
1807 // Increment the pointer, VAList, to the next vaarg
1808 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1809 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1810 TLI.getPointerTy()));
1811 // Store the incremented VAList to the legalized pointer
1812 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1813 Node->getOperand(2));
1814 // Load the actual argument out of the pointer VAList
1815 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001816 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001817 Result = LegalizeOp(Result);
1818 break;
1819 }
1820 }
1821 // Since VAARG produces two values, make sure to remember that we
1822 // legalized both of them.
1823 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001824 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1825 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001826 }
1827
1828 case ISD::VACOPY:
1829 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1830 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1831 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1832
1833 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1834 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001835 case TargetLowering::Custom:
1836 isCustom = true;
1837 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001838 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1840 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001841 if (isCustom) {
1842 Tmp1 = TLI.LowerOperation(Result, DAG);
1843 if (Tmp1.Val) Result = Tmp1;
1844 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001845 break;
1846 case TargetLowering::Expand:
1847 // This defaults to loading a pointer from the input and storing it to the
1848 // output, returning the chain.
1849 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1850 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1851 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00001852 break;
1853 }
1854 break;
1855
1856 case ISD::VAEND:
1857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1859
1860 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1861 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001862 case TargetLowering::Custom:
1863 isCustom = true;
1864 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001865 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001867 if (isCustom) {
1868 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1869 if (Tmp1.Val) Result = Tmp1;
1870 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001871 break;
1872 case TargetLowering::Expand:
1873 Result = Tmp1; // Default to a no-op, return the chain
1874 break;
1875 }
1876 break;
1877
1878 case ISD::VASTART:
1879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1880 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1881
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1883
Nate Begemanacc398c2006-01-25 18:21:52 +00001884 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1885 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001886 case TargetLowering::Legal: break;
1887 case TargetLowering::Custom:
1888 Tmp1 = TLI.LowerOperation(Result, DAG);
1889 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00001890 break;
1891 }
1892 break;
1893
Nate Begeman35ef9132006-01-11 21:21:00 +00001894 case ISD::ROTL:
1895 case ISD::ROTR:
1896 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001898
1899 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1900 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001901 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00001902 break;
1903
Nate Begemand88fc032006-01-14 03:14:10 +00001904 case ISD::BSWAP:
1905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1906 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001907 case TargetLowering::Custom:
1908 assert(0 && "Cannot custom legalize this yet!");
1909 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001911 break;
1912 case TargetLowering::Promote: {
1913 MVT::ValueType OVT = Tmp1.getValueType();
1914 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1915 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00001916
Chris Lattner456a93a2006-01-28 07:39:30 +00001917 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1918 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1919 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1920 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1921 break;
1922 }
1923 case TargetLowering::Expand:
1924 Result = ExpandBSWAP(Tmp1);
1925 break;
Nate Begemand88fc032006-01-14 03:14:10 +00001926 }
1927 break;
1928
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001929 case ISD::CTPOP:
1930 case ISD::CTTZ:
1931 case ISD::CTLZ:
1932 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1933 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001934 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001935 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001936 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001937 break;
1938 case TargetLowering::Promote: {
1939 MVT::ValueType OVT = Tmp1.getValueType();
1940 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001941
1942 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001943 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1944 // Perform the larger operation, then subtract if needed.
1945 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001946 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001947 case ISD::CTPOP:
1948 Result = Tmp1;
1949 break;
1950 case ISD::CTTZ:
1951 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001952 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1953 DAG.getConstant(getSizeInBits(NVT), NVT),
1954 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001955 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001956 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1957 break;
1958 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00001959 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001960 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1961 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001962 getSizeInBits(OVT), NVT));
1963 break;
1964 }
1965 break;
1966 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001967 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00001968 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001969 break;
1970 }
1971 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001972
Chris Lattner2c8086f2005-04-02 05:00:07 +00001973 // Unary operators
1974 case ISD::FABS:
1975 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001976 case ISD::FSQRT:
1977 case ISD::FSIN:
1978 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001979 Tmp1 = LegalizeOp(Node->getOperand(0));
1980 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001981 case TargetLowering::Promote:
1982 case TargetLowering::Custom:
1983 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001984 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001985 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001986 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001987 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00001988 switch (Node->getOpcode()) {
1989 default: assert(0 && "Unreachable!");
1990 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001991 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1992 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001993 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001994 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001995 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001996 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1997 MVT::ValueType VT = Node->getValueType(0);
1998 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001999 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002000 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2001 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002002 break;
2003 }
2004 case ISD::FSQRT:
2005 case ISD::FSIN:
2006 case ISD::FCOS: {
2007 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002008 const char *FnName = 0;
2009 switch(Node->getOpcode()) {
2010 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2011 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2012 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2013 default: assert(0 && "Unreachable!");
2014 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002015 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002016 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002017 break;
2018 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002019 }
2020 break;
2021 }
2022 break;
Chris Lattner35481892005-12-23 00:16:34 +00002023
2024 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002025 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002026 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002027 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002028 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2029 Node->getOperand(0).getValueType())) {
2030 default: assert(0 && "Unknown operation action!");
2031 case TargetLowering::Expand:
2032 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2033 break;
2034 case TargetLowering::Legal:
2035 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002036 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002037 break;
2038 }
2039 }
2040 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002041 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002042 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002043 case ISD::UINT_TO_FP: {
2044 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002045 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2046 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002047 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002048 Node->getOperand(0).getValueType())) {
2049 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002050 case TargetLowering::Custom:
2051 isCustom = true;
2052 // FALLTHROUGH
2053 case TargetLowering::Legal:
2054 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002055 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002056 if (isCustom) {
2057 Tmp1 = TLI.LowerOperation(Result, DAG);
2058 if (Tmp1.Val) Result = Tmp1;
2059 }
2060 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002061 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002062 Result = ExpandLegalINT_TO_FP(isSigned,
2063 LegalizeOp(Node->getOperand(0)),
2064 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002065 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002066 case TargetLowering::Promote:
2067 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2068 Node->getValueType(0),
2069 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002070 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002071 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002072 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002073 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002074 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2075 Node->getValueType(0), Node->getOperand(0));
2076 break;
2077 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002078 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002079 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002080 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2081 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002082 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002083 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2084 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002085 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002086 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2087 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002088 break;
2089 }
2090 break;
2091 }
2092 case ISD::TRUNCATE:
2093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2094 case Legal:
2095 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002096 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002097 break;
2098 case Expand:
2099 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2100
2101 // Since the result is legal, we should just be able to truncate the low
2102 // part of the source.
2103 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2104 break;
2105 case Promote:
2106 Result = PromoteOp(Node->getOperand(0));
2107 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2108 break;
2109 }
2110 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002111
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002112 case ISD::FP_TO_SINT:
2113 case ISD::FP_TO_UINT:
2114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2115 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002116 Tmp1 = LegalizeOp(Node->getOperand(0));
2117
Chris Lattner1618beb2005-07-29 00:11:56 +00002118 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2119 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002120 case TargetLowering::Custom:
2121 isCustom = true;
2122 // FALLTHROUGH
2123 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002124 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002125 if (isCustom) {
2126 Tmp1 = TLI.LowerOperation(Result, DAG);
2127 if (Tmp1.Val) Result = Tmp1;
2128 }
2129 break;
2130 case TargetLowering::Promote:
2131 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2132 Node->getOpcode() == ISD::FP_TO_SINT);
2133 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002134 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002135 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2136 SDOperand True, False;
2137 MVT::ValueType VT = Node->getOperand(0).getValueType();
2138 MVT::ValueType NVT = Node->getValueType(0);
2139 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2140 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2141 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2142 Node->getOperand(0), Tmp2, ISD::SETLT);
2143 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2144 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002145 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002146 Tmp2));
2147 False = DAG.getNode(ISD::XOR, NVT, False,
2148 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002149 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2150 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002151 } else {
2152 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2153 }
2154 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002155 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002156 break;
2157 case Expand:
2158 assert(0 && "Shouldn't need to expand other operators here!");
2159 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002160 Tmp1 = PromoteOp(Node->getOperand(0));
2161 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2162 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002163 break;
2164 }
2165 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002166
Chris Lattner13c78e22005-09-02 00:18:10 +00002167 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002168 case ISD::ZERO_EXTEND:
2169 case ISD::SIGN_EXTEND:
2170 case ISD::FP_EXTEND:
2171 case ISD::FP_ROUND:
2172 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002173 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002174 case Legal:
2175 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002176 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002177 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002178 case Promote:
2179 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002180 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002181 Tmp1 = PromoteOp(Node->getOperand(0));
2182 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002183 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002184 case ISD::ZERO_EXTEND:
2185 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002186 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002187 Result = DAG.getZeroExtendInReg(Result,
2188 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002189 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002190 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002191 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002192 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002193 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002194 Result,
2195 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002196 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002197 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002198 Result = PromoteOp(Node->getOperand(0));
2199 if (Result.getValueType() != Op.getValueType())
2200 // Dynamically dead while we have only 2 FP types.
2201 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2202 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002203 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002204 Result = PromoteOp(Node->getOperand(0));
2205 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2206 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002207 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002208 }
2209 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002210 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002211 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002212 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002213 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002214
2215 // If this operation is not supported, convert it to a shl/shr or load/store
2216 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002217 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2218 default: assert(0 && "This action not supported for this op yet!");
2219 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002220 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002221 break;
2222 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002223 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002224 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002225 // NOTE: we could fall back on load/store here too for targets without
2226 // SAR. However, it is doubtful that any exist.
2227 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2228 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002229 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002230 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2231 Node->getOperand(0), ShiftCst);
2232 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2233 Result, ShiftCst);
2234 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2235 // The only way we can lower this is to turn it into a STORETRUNC,
2236 // EXTLOAD pair, targetting a temporary location (a stack slot).
2237
2238 // NOTE: there is a choice here between constantly creating new stack
2239 // slots and always reusing the same one. We currently always create
2240 // new ones, as reuse may inhibit scheduling.
2241 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2242 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2243 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2244 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002245 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002246 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2247 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2248 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002249 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002250 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002251 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2252 Result, StackSlot, DAG.getSrcValue(NULL),
2253 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002254 } else {
2255 assert(0 && "Unknown op");
2256 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002257 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002258 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002259 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002260 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002261 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002262
2263 // Make sure that the generated code is itself legal.
2264 if (Result != Op)
2265 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002266
Chris Lattner45982da2005-05-12 16:53:42 +00002267 // Note that LegalizeOp may be reentered even from single-use nodes, which
2268 // means that we always must cache transformed nodes.
2269 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002270 return Result;
2271}
2272
Chris Lattner8b6fa222005-01-15 22:16:26 +00002273/// PromoteOp - Given an operation that produces a value in an invalid type,
2274/// promote it to compute the value into a larger type. The produced value will
2275/// have the correct bits for the low portion of the register, but no guarantee
2276/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002277SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2278 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002279 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002280 assert(getTypeAction(VT) == Promote &&
2281 "Caller should expand or legalize operands that are not promotable!");
2282 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2283 "Cannot promote to smaller type!");
2284
Chris Lattner03c85462005-01-15 05:21:40 +00002285 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002286 SDOperand Result;
2287 SDNode *Node = Op.Val;
2288
Chris Lattner6fdcb252005-09-02 20:32:45 +00002289 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2290 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002291
Chris Lattner03c85462005-01-15 05:21:40 +00002292 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002293 case ISD::CopyFromReg:
2294 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002295 default:
2296 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2297 assert(0 && "Do not know how to promote this operator!");
2298 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002299 case ISD::UNDEF:
2300 Result = DAG.getNode(ISD::UNDEF, NVT);
2301 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002302 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002303 if (VT != MVT::i1)
2304 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2305 else
2306 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002307 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2308 break;
2309 case ISD::ConstantFP:
2310 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2311 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2312 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002313
Chris Lattner82fbfb62005-01-18 02:59:52 +00002314 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002315 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002316 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2317 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002318 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002319
2320 case ISD::TRUNCATE:
2321 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2322 case Legal:
2323 Result = LegalizeOp(Node->getOperand(0));
2324 assert(Result.getValueType() >= NVT &&
2325 "This truncation doesn't make sense!");
2326 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2327 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2328 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002329 case Promote:
2330 // The truncation is not required, because we don't guarantee anything
2331 // about high bits anyway.
2332 Result = PromoteOp(Node->getOperand(0));
2333 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002334 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002335 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2336 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002337 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002338 }
2339 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002340 case ISD::SIGN_EXTEND:
2341 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002342 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002343 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2344 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2345 case Legal:
2346 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002347 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002348 break;
2349 case Promote:
2350 // Promote the reg if it's smaller.
2351 Result = PromoteOp(Node->getOperand(0));
2352 // The high bits are not guaranteed to be anything. Insert an extend.
2353 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002354 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002355 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002356 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002357 Result = DAG.getZeroExtendInReg(Result,
2358 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002359 break;
2360 }
2361 break;
Chris Lattner35481892005-12-23 00:16:34 +00002362 case ISD::BIT_CONVERT:
2363 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2364 Result = PromoteOp(Result);
2365 break;
2366
Chris Lattner8b6fa222005-01-15 22:16:26 +00002367 case ISD::FP_EXTEND:
2368 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2369 case ISD::FP_ROUND:
2370 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2371 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2372 case Promote: assert(0 && "Unreachable with 2 FP types!");
2373 case Legal:
2374 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002375 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002376 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002377 break;
2378 }
2379 break;
2380
2381 case ISD::SINT_TO_FP:
2382 case ISD::UINT_TO_FP:
2383 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2384 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002385 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002386 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002387 break;
2388
2389 case Promote:
2390 Result = PromoteOp(Node->getOperand(0));
2391 if (Node->getOpcode() == ISD::SINT_TO_FP)
2392 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002393 Result,
2394 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002395 else
Chris Lattner23993562005-04-13 02:38:47 +00002396 Result = DAG.getZeroExtendInReg(Result,
2397 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002398 // No extra round required here.
2399 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002400 break;
2401 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002402 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2403 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002404 // Round if we cannot tolerate excess precision.
2405 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002406 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2407 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002408 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002409 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002410 break;
2411
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002412 case ISD::SIGN_EXTEND_INREG:
2413 Result = PromoteOp(Node->getOperand(0));
2414 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2415 Node->getOperand(1));
2416 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002417 case ISD::FP_TO_SINT:
2418 case ISD::FP_TO_UINT:
2419 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2420 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002421 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002422 break;
2423 case Promote:
2424 // The input result is prerounded, so we don't have to do anything
2425 // special.
2426 Tmp1 = PromoteOp(Node->getOperand(0));
2427 break;
2428 case Expand:
2429 assert(0 && "not implemented");
2430 }
Nate Begemand2558e32005-08-14 01:20:53 +00002431 // If we're promoting a UINT to a larger size, check to see if the new node
2432 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2433 // we can use that instead. This allows us to generate better code for
2434 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2435 // legal, such as PowerPC.
2436 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002437 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002438 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2439 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002440 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2441 } else {
2442 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2443 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002444 break;
2445
Chris Lattner2c8086f2005-04-02 05:00:07 +00002446 case ISD::FABS:
2447 case ISD::FNEG:
2448 Tmp1 = PromoteOp(Node->getOperand(0));
2449 assert(Tmp1.getValueType() == NVT);
2450 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2451 // NOTE: we do not have to do any extra rounding here for
2452 // NoExcessFPPrecision, because we know the input will have the appropriate
2453 // precision, and these operations don't modify precision at all.
2454 break;
2455
Chris Lattnerda6ba872005-04-28 21:44:33 +00002456 case ISD::FSQRT:
2457 case ISD::FSIN:
2458 case ISD::FCOS:
2459 Tmp1 = PromoteOp(Node->getOperand(0));
2460 assert(Tmp1.getValueType() == NVT);
2461 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002462 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002463 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2464 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002465 break;
2466
Chris Lattner03c85462005-01-15 05:21:40 +00002467 case ISD::AND:
2468 case ISD::OR:
2469 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002470 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002471 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002472 case ISD::MUL:
2473 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002474 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002475 // that too is okay if they are integer operations.
2476 Tmp1 = PromoteOp(Node->getOperand(0));
2477 Tmp2 = PromoteOp(Node->getOperand(1));
2478 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2479 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002480 break;
2481 case ISD::FADD:
2482 case ISD::FSUB:
2483 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002484 Tmp1 = PromoteOp(Node->getOperand(0));
2485 Tmp2 = PromoteOp(Node->getOperand(1));
2486 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2487 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2488
2489 // Floating point operations will give excess precision that we may not be
2490 // able to tolerate. If we DO allow excess precision, just leave it,
2491 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002492 // FIXME: Why would we need to round FP ops more than integer ones?
2493 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002494 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002495 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2496 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002497 break;
2498
Chris Lattner8b6fa222005-01-15 22:16:26 +00002499 case ISD::SDIV:
2500 case ISD::SREM:
2501 // These operators require that their input be sign extended.
2502 Tmp1 = PromoteOp(Node->getOperand(0));
2503 Tmp2 = PromoteOp(Node->getOperand(1));
2504 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002505 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2506 DAG.getValueType(VT));
2507 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2508 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002509 }
2510 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2511
2512 // Perform FP_ROUND: this is probably overly pessimistic.
2513 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002514 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2515 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002516 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002517 case ISD::FDIV:
2518 case ISD::FREM:
2519 // These operators require that their input be fp extended.
2520 Tmp1 = PromoteOp(Node->getOperand(0));
2521 Tmp2 = PromoteOp(Node->getOperand(1));
2522 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2523
2524 // Perform FP_ROUND: this is probably overly pessimistic.
2525 if (NoExcessFPPrecision)
2526 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2527 DAG.getValueType(VT));
2528 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002529
2530 case ISD::UDIV:
2531 case ISD::UREM:
2532 // These operators require that their input be zero extended.
2533 Tmp1 = PromoteOp(Node->getOperand(0));
2534 Tmp2 = PromoteOp(Node->getOperand(1));
2535 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002536 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2537 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002538 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2539 break;
2540
2541 case ISD::SHL:
2542 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002543 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002544 break;
2545 case ISD::SRA:
2546 // The input value must be properly sign extended.
2547 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002548 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2549 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002550 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002551 break;
2552 case ISD::SRL:
2553 // The input value must be properly zero extended.
2554 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002555 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002556 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002557 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002558
2559 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002560 Tmp1 = Node->getOperand(0); // Get the chain.
2561 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002562 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2563 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2564 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2565 } else {
2566 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2567 Node->getOperand(2));
2568 // Increment the pointer, VAList, to the next vaarg
2569 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2570 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2571 TLI.getPointerTy()));
2572 // Store the incremented VAList to the legalized pointer
2573 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2574 Node->getOperand(2));
2575 // Load the actual argument out of the pointer VAList
2576 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2577 DAG.getSrcValue(0), VT);
2578 }
2579 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002580 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002581 break;
2582
Chris Lattner03c85462005-01-15 05:21:40 +00002583 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002584 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2585 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002586 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002587 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002588 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002589 case ISD::SEXTLOAD:
2590 case ISD::ZEXTLOAD:
2591 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002592 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2593 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002594 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002595 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002596 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002597 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002598 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002599 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2600 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002601 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002602 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002603 case ISD::SELECT_CC:
2604 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2605 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2606 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002607 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002608 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002609 case ISD::BSWAP:
2610 Tmp1 = Node->getOperand(0);
2611 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2612 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2613 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2614 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2615 TLI.getShiftAmountTy()));
2616 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002617 case ISD::CTPOP:
2618 case ISD::CTTZ:
2619 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002620 // Zero extend the argument
2621 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002622 // Perform the larger operation, then subtract if needed.
2623 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002624 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002625 case ISD::CTPOP:
2626 Result = Tmp1;
2627 break;
2628 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002629 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002630 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002631 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002632 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002633 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002634 break;
2635 case ISD::CTLZ:
2636 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002637 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2638 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002639 getSizeInBits(VT), NVT));
2640 break;
2641 }
2642 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002643 }
2644
2645 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002646
2647 // Make sure the result is itself legal.
2648 Result = LegalizeOp(Result);
2649
2650 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002651 AddPromotedOperand(Op, Result);
2652 return Result;
2653}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002654
Chris Lattner35481892005-12-23 00:16:34 +00002655/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002656/// The resultant code need not be legal. Note that SrcOp is the input operand
2657/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002658SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2659 SDOperand SrcOp) {
2660 // Create the stack frame object.
2661 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2662 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002663 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002664 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2665
2666 // Emit a store to the stack slot.
2667 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002668 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002669 // Result is a load from the stack slot.
2670 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2671}
2672
Chris Lattner5b359c62005-04-02 04:00:59 +00002673void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2674 SDOperand Op, SDOperand Amt,
2675 SDOperand &Lo, SDOperand &Hi) {
2676 // Expand the subcomponents.
2677 SDOperand LHSL, LHSH;
2678 ExpandOp(Op, LHSL, LHSH);
2679
2680 std::vector<SDOperand> Ops;
2681 Ops.push_back(LHSL);
2682 Ops.push_back(LHSH);
2683 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002684 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002685 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002686 Hi = Lo.getValue(1);
2687}
2688
2689
Chris Lattnere34b3962005-01-19 04:19:40 +00002690/// ExpandShift - Try to find a clever way to expand this shift operation out to
2691/// smaller elements. If we can't find a way that is more efficient than a
2692/// libcall on this target, return false. Otherwise, return true with the
2693/// low-parts expanded into Lo and Hi.
2694bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2695 SDOperand &Lo, SDOperand &Hi) {
2696 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2697 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002698
Chris Lattnere34b3962005-01-19 04:19:40 +00002699 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002700 SDOperand ShAmt = LegalizeOp(Amt);
2701 MVT::ValueType ShTy = ShAmt.getValueType();
2702 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2703 unsigned NVTBits = MVT::getSizeInBits(NVT);
2704
2705 // Handle the case when Amt is an immediate. Other cases are currently broken
2706 // and are disabled.
2707 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2708 unsigned Cst = CN->getValue();
2709 // Expand the incoming operand to be shifted, so that we have its parts
2710 SDOperand InL, InH;
2711 ExpandOp(Op, InL, InH);
2712 switch(Opc) {
2713 case ISD::SHL:
2714 if (Cst > VTBits) {
2715 Lo = DAG.getConstant(0, NVT);
2716 Hi = DAG.getConstant(0, NVT);
2717 } else if (Cst > NVTBits) {
2718 Lo = DAG.getConstant(0, NVT);
2719 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002720 } else if (Cst == NVTBits) {
2721 Lo = DAG.getConstant(0, NVT);
2722 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002723 } else {
2724 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2725 Hi = DAG.getNode(ISD::OR, NVT,
2726 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2727 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2728 }
2729 return true;
2730 case ISD::SRL:
2731 if (Cst > VTBits) {
2732 Lo = DAG.getConstant(0, NVT);
2733 Hi = DAG.getConstant(0, NVT);
2734 } else if (Cst > NVTBits) {
2735 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2736 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002737 } else if (Cst == NVTBits) {
2738 Lo = InH;
2739 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002740 } else {
2741 Lo = DAG.getNode(ISD::OR, NVT,
2742 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2743 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2744 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2745 }
2746 return true;
2747 case ISD::SRA:
2748 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002749 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002750 DAG.getConstant(NVTBits-1, ShTy));
2751 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002752 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002753 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002754 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002755 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002756 } else if (Cst == NVTBits) {
2757 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002758 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002759 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002760 } else {
2761 Lo = DAG.getNode(ISD::OR, NVT,
2762 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2763 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2764 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2765 }
2766 return true;
2767 }
2768 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002769 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002770}
Chris Lattner77e77a62005-01-21 06:05:23 +00002771
Chris Lattner9530ddc2005-05-13 05:09:11 +00002772/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2773/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002774/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00002775static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
2776 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00002777 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattnerde387ce2006-01-09 23:21:49 +00002778 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002779
Chris Lattner16cd04d2005-05-12 23:24:06 +00002780 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002781 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002782 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002783 Found = Node;
2784 return;
2785 }
2786
2787 // Otherwise, scan the operands of Node to see if any of them is a call.
2788 assert(Node->getNumOperands() != 0 &&
2789 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002790 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00002791 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002792
2793 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002794 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00002795 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002796}
2797
2798
Chris Lattner9530ddc2005-05-13 05:09:11 +00002799/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2800/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002801/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002802static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2803 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00002804 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner82299e72005-08-05 18:10:27 +00002805 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002806
Chris Lattner16cd04d2005-05-12 23:24:06 +00002807 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002808 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002809 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002810 Found = Node;
2811 return;
2812 }
2813
2814 // Otherwise, scan the operands of Node to see if any of them is a call.
2815 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2816 if (UI == E) return;
2817 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002818 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002819
2820 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002821 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002822}
2823
Chris Lattner9530ddc2005-05-13 05:09:11 +00002824/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002825/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002826static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002827 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002828 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002829 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002830 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002831
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002832 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002833 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002834 if (TheChain.getValueType() != MVT::Other) {
2835 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00002836 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002837 if (TheChain.getValueType() != MVT::Other) {
2838 // Otherwise, hunt for it.
2839 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
2840 if (Node->getValueType(i) == MVT::Other) {
2841 TheChain = SDOperand(Node, i);
2842 break;
2843 }
2844
2845 // Otherwise, we walked into a node without a chain.
2846 if (TheChain.getValueType() != MVT::Other)
2847 return 0;
2848 }
2849 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002850
2851 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002852 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002853
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002854 // Make sure to only follow users of our token chain.
2855 SDNode *User = *UI;
2856 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2857 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002858 if (SDNode *Result = FindCallSeqEnd(User))
2859 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002860 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002861 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002862}
2863
Chris Lattner9530ddc2005-05-13 05:09:11 +00002864/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002865/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002866static SDNode *FindCallSeqStart(SDNode *Node) {
2867 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002868 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002869
2870 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2871 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002872 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002873}
2874
2875
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002876/// FindInputOutputChains - If we are replacing an operation with a call we need
2877/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002878/// properly serialize the calls in the block. The returned operand is the
2879/// input chain value for the new call (e.g. the entry node or the previous
2880/// call), and OutChain is set to be the chain node to update to point to the
2881/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002882static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2883 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002884 SDNode *LatestCallSeqStart = Entry.Val;
2885 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00002886 std::set<SDNode*> Visited;
2887 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
2888 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00002889 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002890
Chris Lattner16cd04d2005-05-12 23:24:06 +00002891 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002892 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002893 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2894 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002895 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002896 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002897 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2898 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002899 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002900 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002901 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002902
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002903 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002904 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002905 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002906 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00002907 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002908
Chris Lattner9530ddc2005-05-13 05:09:11 +00002909 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002910 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002911 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002912
Chris Lattner9530ddc2005-05-13 05:09:11 +00002913 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002914}
2915
Jeff Cohen00b168892005-07-27 06:12:32 +00002916/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002917void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2918 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002919 // Nothing to splice it into?
2920 if (OutChain == 0) return;
2921
2922 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2923 //OutChain->dump();
2924
2925 // Form a token factor node merging the old inval and the new inval.
2926 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2927 OutChain->getOperand(0));
2928 // Change the node to refer to the new token.
2929 OutChain->setAdjCallChain(InToken);
2930}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002931
2932
Chris Lattner77e77a62005-01-21 06:05:23 +00002933// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2934// does not fit into a register, return the lo part and set the hi part to the
2935// by-reg argument. If it does fit into a single register, return the result
2936// and leave the Hi part unset.
2937SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2938 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002939 SDNode *OutChain;
2940 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2941 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002942 if (InChain.Val == 0)
2943 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002944
Chris Lattner77e77a62005-01-21 06:05:23 +00002945 TargetLowering::ArgListTy Args;
2946 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2947 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2948 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2949 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2950 }
2951 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002952
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002953 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002954 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002955 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002956 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2957 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002958
Chris Lattner99c25b82005-09-02 20:26:58 +00002959 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002960 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002961 default: assert(0 && "Unknown thing");
2962 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002963 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00002964 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002965 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002966 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00002967 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002968 }
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002969
2970 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00002971 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner99c25b82005-09-02 20:26:58 +00002972 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002973}
2974
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002975
Chris Lattner77e77a62005-01-21 06:05:23 +00002976/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2977/// destination type is legal.
2978SDOperand SelectionDAGLegalize::
2979ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002980 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002981 assert(getTypeAction(Source.getValueType()) == Expand &&
2982 "This is not an expansion!");
2983 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2984
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002985 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002986 assert(Source.getValueType() == MVT::i64 &&
2987 "This only works for 64-bit -> FP");
2988 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2989 // incoming integer is set. To handle this, we dynamically test to see if
2990 // it is set, and, if so, add a fudge factor.
2991 SDOperand Lo, Hi;
2992 ExpandOp(Source, Lo, Hi);
2993
Chris Lattner66de05b2005-05-13 04:45:13 +00002994 // If this is unsigned, and not supported, first perform the conversion to
2995 // signed, then adjust the result if the sign bit is set.
2996 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2997 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2998
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002999 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3000 DAG.getConstant(0, Hi.getValueType()),
3001 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003002 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3003 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3004 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003005 uint64_t FF = 0x5f800000ULL;
3006 if (TLI.isLittleEndian()) FF <<= 32;
3007 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003008
Chris Lattner5839bf22005-08-26 17:15:30 +00003009 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003010 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3011 SDOperand FudgeInReg;
3012 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003013 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3014 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003015 else {
3016 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003017 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3018 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003019 }
Chris Lattner473a9902005-09-29 06:44:39 +00003020 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003021 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003022
Chris Lattnera88a2602005-05-14 05:33:54 +00003023 // Check to see if the target has a custom way to lower this. If so, use it.
3024 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3025 default: assert(0 && "This action not implemented for this operation!");
3026 case TargetLowering::Legal:
3027 case TargetLowering::Expand:
3028 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003029 case TargetLowering::Custom: {
3030 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3031 Source), DAG);
3032 if (NV.Val)
3033 return LegalizeOp(NV);
3034 break; // The target decided this was legal after all
3035 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003036 }
3037
Chris Lattner13689e22005-05-12 07:00:44 +00003038 // Expand the source, then glue it back together for the call. We must expand
3039 // the source in case it is shared (this pass of legalize must traverse it).
3040 SDOperand SrcLo, SrcHi;
3041 ExpandOp(Source, SrcLo, SrcHi);
3042 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3043
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003044 SDNode *OutChain = 0;
3045 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3046 DAG.getEntryNode());
3047 const char *FnName = 0;
3048 if (DestTy == MVT::f32)
3049 FnName = "__floatdisf";
3050 else {
3051 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3052 FnName = "__floatdidf";
3053 }
3054
Chris Lattner77e77a62005-01-21 06:05:23 +00003055 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3056
3057 TargetLowering::ArgListTy Args;
3058 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003059
Chris Lattner77e77a62005-01-21 06:05:23 +00003060 Args.push_back(std::make_pair(Source, ArgTy));
3061
3062 // We don't care about token chains for libcalls. We just use the entry
3063 // node as our input and ignore the output chain. This allows us to place
3064 // calls wherever we need them to satisfy data dependences.
3065 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003066
3067 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003068 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3069 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003070
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003071 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003072 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003073}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003074
Chris Lattner22cde6a2006-01-28 08:25:58 +00003075/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3076/// INT_TO_FP operation of the specified operand when the target requests that
3077/// we expand it. At this point, we know that the result and operand types are
3078/// legal for the target.
3079SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3080 SDOperand Op0,
3081 MVT::ValueType DestVT) {
3082 if (Op0.getValueType() == MVT::i32) {
3083 // simple 32-bit [signed|unsigned] integer to float/double expansion
3084
3085 // get the stack frame index of a 8 byte buffer
3086 MachineFunction &MF = DAG.getMachineFunction();
3087 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3088 // get address of 8 byte buffer
3089 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3090 // word offset constant for Hi/Lo address computation
3091 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3092 // set up Hi and Lo (into buffer) address based on endian
3093 SDOperand Hi, Lo;
3094 if (TLI.isLittleEndian()) {
3095 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3096 Lo = StackSlot;
3097 } else {
3098 Hi = StackSlot;
3099 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3100 }
3101 // if signed map to unsigned space
3102 SDOperand Op0Mapped;
3103 if (isSigned) {
3104 // constant used to invert sign bit (signed to unsigned mapping)
3105 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3106 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3107 } else {
3108 Op0Mapped = Op0;
3109 }
3110 // store the lo of the constructed double - based on integer input
3111 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3112 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3113 // initial hi portion of constructed double
3114 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3115 // store the hi of the constructed double - biased exponent
3116 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3117 InitialHi, Hi, DAG.getSrcValue(NULL));
3118 // load the constructed double
3119 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3120 DAG.getSrcValue(NULL));
3121 // FP constant to bias correct the final result
3122 SDOperand Bias = DAG.getConstantFP(isSigned ?
3123 BitsToDouble(0x4330000080000000ULL)
3124 : BitsToDouble(0x4330000000000000ULL),
3125 MVT::f64);
3126 // subtract the bias
3127 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3128 // final result
3129 SDOperand Result;
3130 // handle final rounding
3131 if (DestVT == MVT::f64) {
3132 // do nothing
3133 Result = Sub;
3134 } else {
3135 // if f32 then cast to f32
3136 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3137 }
3138 return Result;
3139 }
3140 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3141 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3142
3143 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3144 DAG.getConstant(0, Op0.getValueType()),
3145 ISD::SETLT);
3146 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3147 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3148 SignSet, Four, Zero);
3149
3150 // If the sign bit of the integer is set, the large number will be treated
3151 // as a negative number. To counteract this, the dynamic code adds an
3152 // offset depending on the data type.
3153 uint64_t FF;
3154 switch (Op0.getValueType()) {
3155 default: assert(0 && "Unsupported integer type!");
3156 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3157 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3158 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3159 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3160 }
3161 if (TLI.isLittleEndian()) FF <<= 32;
3162 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3163
3164 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3165 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3166 SDOperand FudgeInReg;
3167 if (DestVT == MVT::f32)
3168 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3169 DAG.getSrcValue(NULL));
3170 else {
3171 assert(DestVT == MVT::f64 && "Unexpected conversion");
3172 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3173 DAG.getEntryNode(), CPIdx,
3174 DAG.getSrcValue(NULL), MVT::f32));
3175 }
3176
3177 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3178}
3179
3180/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3181/// *INT_TO_FP operation of the specified operand when the target requests that
3182/// we promote it. At this point, we know that the result and operand types are
3183/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3184/// operation that takes a larger input.
3185SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3186 MVT::ValueType DestVT,
3187 bool isSigned) {
3188 // First step, figure out the appropriate *INT_TO_FP operation to use.
3189 MVT::ValueType NewInTy = LegalOp.getValueType();
3190
3191 unsigned OpToUse = 0;
3192
3193 // Scan for the appropriate larger type to use.
3194 while (1) {
3195 NewInTy = (MVT::ValueType)(NewInTy+1);
3196 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3197
3198 // If the target supports SINT_TO_FP of this type, use it.
3199 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3200 default: break;
3201 case TargetLowering::Legal:
3202 if (!TLI.isTypeLegal(NewInTy))
3203 break; // Can't use this datatype.
3204 // FALL THROUGH.
3205 case TargetLowering::Custom:
3206 OpToUse = ISD::SINT_TO_FP;
3207 break;
3208 }
3209 if (OpToUse) break;
3210 if (isSigned) continue;
3211
3212 // If the target supports UINT_TO_FP of this type, use it.
3213 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3214 default: break;
3215 case TargetLowering::Legal:
3216 if (!TLI.isTypeLegal(NewInTy))
3217 break; // Can't use this datatype.
3218 // FALL THROUGH.
3219 case TargetLowering::Custom:
3220 OpToUse = ISD::UINT_TO_FP;
3221 break;
3222 }
3223 if (OpToUse) break;
3224
3225 // Otherwise, try a larger type.
3226 }
3227
3228 // Okay, we found the operation and type to use. Zero extend our input to the
3229 // desired type then run the operation on it.
3230 return DAG.getNode(OpToUse, DestVT,
3231 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3232 NewInTy, LegalOp));
3233}
3234
3235/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3236/// FP_TO_*INT operation of the specified operand when the target requests that
3237/// we promote it. At this point, we know that the result and operand types are
3238/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3239/// operation that returns a larger result.
3240SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3241 MVT::ValueType DestVT,
3242 bool isSigned) {
3243 // First step, figure out the appropriate FP_TO*INT operation to use.
3244 MVT::ValueType NewOutTy = DestVT;
3245
3246 unsigned OpToUse = 0;
3247
3248 // Scan for the appropriate larger type to use.
3249 while (1) {
3250 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3251 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3252
3253 // If the target supports FP_TO_SINT returning this type, use it.
3254 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3255 default: break;
3256 case TargetLowering::Legal:
3257 if (!TLI.isTypeLegal(NewOutTy))
3258 break; // Can't use this datatype.
3259 // FALL THROUGH.
3260 case TargetLowering::Custom:
3261 OpToUse = ISD::FP_TO_SINT;
3262 break;
3263 }
3264 if (OpToUse) break;
3265
3266 // If the target supports FP_TO_UINT of this type, use it.
3267 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3268 default: break;
3269 case TargetLowering::Legal:
3270 if (!TLI.isTypeLegal(NewOutTy))
3271 break; // Can't use this datatype.
3272 // FALL THROUGH.
3273 case TargetLowering::Custom:
3274 OpToUse = ISD::FP_TO_UINT;
3275 break;
3276 }
3277 if (OpToUse) break;
3278
3279 // Otherwise, try a larger type.
3280 }
3281
3282 // Okay, we found the operation and type to use. Truncate the result of the
3283 // extended FP_TO_*INT operation to the desired size.
3284 return DAG.getNode(ISD::TRUNCATE, DestVT,
3285 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3286}
3287
3288/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3289///
3290SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3291 MVT::ValueType VT = Op.getValueType();
3292 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3293 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3294 switch (VT) {
3295 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3296 case MVT::i16:
3297 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3298 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3299 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3300 case MVT::i32:
3301 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3302 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3303 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3304 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3305 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3306 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3307 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3308 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3309 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3310 case MVT::i64:
3311 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3312 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3313 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3314 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3315 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3316 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3317 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3318 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3319 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3320 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3321 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3322 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3323 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3324 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3325 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3326 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3327 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3328 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3329 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3330 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3331 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3332 }
3333}
3334
3335/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3336///
3337SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3338 switch (Opc) {
3339 default: assert(0 && "Cannot expand this yet!");
3340 case ISD::CTPOP: {
3341 static const uint64_t mask[6] = {
3342 0x5555555555555555ULL, 0x3333333333333333ULL,
3343 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3344 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3345 };
3346 MVT::ValueType VT = Op.getValueType();
3347 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3348 unsigned len = getSizeInBits(VT);
3349 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3350 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3351 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3352 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3353 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3354 DAG.getNode(ISD::AND, VT,
3355 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3356 }
3357 return Op;
3358 }
3359 case ISD::CTLZ: {
3360 // for now, we do this:
3361 // x = x | (x >> 1);
3362 // x = x | (x >> 2);
3363 // ...
3364 // x = x | (x >>16);
3365 // x = x | (x >>32); // for 64-bit input
3366 // return popcount(~x);
3367 //
3368 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3369 MVT::ValueType VT = Op.getValueType();
3370 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3371 unsigned len = getSizeInBits(VT);
3372 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3373 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3374 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3375 }
3376 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3377 return DAG.getNode(ISD::CTPOP, VT, Op);
3378 }
3379 case ISD::CTTZ: {
3380 // for now, we use: { return popcount(~x & (x - 1)); }
3381 // unless the target has ctlz but not ctpop, in which case we use:
3382 // { return 32 - nlz(~x & (x-1)); }
3383 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3384 MVT::ValueType VT = Op.getValueType();
3385 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3386 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3387 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3388 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3389 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3390 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3391 TLI.isOperationLegal(ISD::CTLZ, VT))
3392 return DAG.getNode(ISD::SUB, VT,
3393 DAG.getConstant(getSizeInBits(VT), VT),
3394 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3395 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3396 }
3397 }
3398}
Chris Lattnere34b3962005-01-19 04:19:40 +00003399
3400
Chris Lattner3e928bb2005-01-07 07:47:09 +00003401/// ExpandOp - Expand the specified SDOperand into its two component pieces
3402/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3403/// LegalizeNodes map is filled in for any results that are not expanded, the
3404/// ExpandedNodes map is filled in for any results that are expanded, and the
3405/// Lo/Hi values are returned.
3406void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3407 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003408 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003409 SDNode *Node = Op.Val;
3410 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003411 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3412 "Cannot expand FP values!");
3413 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003414 "Cannot expand to FP value or to larger int value!");
3415
Chris Lattner6fdcb252005-09-02 20:32:45 +00003416 // See if we already expanded it.
3417 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3418 = ExpandedNodes.find(Op);
3419 if (I != ExpandedNodes.end()) {
3420 Lo = I->second.first;
3421 Hi = I->second.second;
3422 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003423 }
3424
Chris Lattner3e928bb2005-01-07 07:47:09 +00003425 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003426 case ISD::CopyFromReg:
3427 assert(0 && "CopyFromReg must be legal!");
3428 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003429 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3430 assert(0 && "Do not know how to expand this operator!");
3431 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003432 case ISD::UNDEF:
3433 Lo = DAG.getNode(ISD::UNDEF, NVT);
3434 Hi = DAG.getNode(ISD::UNDEF, NVT);
3435 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003436 case ISD::Constant: {
3437 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3438 Lo = DAG.getConstant(Cst, NVT);
3439 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3440 break;
3441 }
Nate Begemancc827e62005-12-07 19:48:11 +00003442 case ISD::ConstantVec: {
3443 unsigned NumElements = Node->getNumOperands();
3444 // If we only have two elements left in the constant vector, just break it
3445 // apart into the two scalar constants it contains. Otherwise, bisect the
3446 // ConstantVec, and return each half as a new ConstantVec.
3447 // FIXME: this is hard coded as big endian, it may have to change to support
3448 // SSE and Alpha MVI
3449 if (NumElements == 2) {
3450 Hi = Node->getOperand(0);
3451 Lo = Node->getOperand(1);
3452 } else {
3453 NumElements /= 2;
3454 std::vector<SDOperand> LoOps, HiOps;
3455 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3456 HiOps.push_back(Node->getOperand(I));
3457 LoOps.push_back(Node->getOperand(I+NumElements));
3458 }
3459 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3460 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3461 }
3462 break;
3463 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003464
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003465 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003466 // Return the operands.
3467 Lo = Node->getOperand(0);
3468 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003469 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003470
3471 case ISD::SIGN_EXTEND_INREG:
3472 ExpandOp(Node->getOperand(0), Lo, Hi);
3473 // Sign extend the lo-part.
3474 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3475 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3476 TLI.getShiftAmountTy()));
3477 // sext_inreg the low part if needed.
3478 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3479 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003480
Nate Begemand88fc032006-01-14 03:14:10 +00003481 case ISD::BSWAP: {
3482 ExpandOp(Node->getOperand(0), Lo, Hi);
3483 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3484 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3485 Lo = TempLo;
3486 break;
3487 }
3488
Chris Lattneredb1add2005-05-11 04:51:16 +00003489 case ISD::CTPOP:
3490 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003491 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3492 DAG.getNode(ISD::CTPOP, NVT, Lo),
3493 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003494 Hi = DAG.getConstant(0, NVT);
3495 break;
3496
Chris Lattner39a8f332005-05-12 19:05:01 +00003497 case ISD::CTLZ: {
3498 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003499 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003500 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3501 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003502 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3503 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003504 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3505 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3506
3507 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3508 Hi = DAG.getConstant(0, NVT);
3509 break;
3510 }
3511
3512 case ISD::CTTZ: {
3513 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003514 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003515 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3516 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003517 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3518 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003519 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3520 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3521
3522 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3523 Hi = DAG.getConstant(0, NVT);
3524 break;
3525 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003526
Nate Begemanacc398c2006-01-25 18:21:52 +00003527 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003528 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3529 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003530 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3531 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3532
3533 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003534 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003535 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3536 if (!TLI.isLittleEndian())
3537 std::swap(Lo, Hi);
3538 break;
3539 }
3540
Chris Lattner3e928bb2005-01-07 07:47:09 +00003541 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003542 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3543 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003544 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003545
3546 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003547 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003548 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3549 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003550 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003551 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003552
3553 // Build a factor node to remember that this load is independent of the
3554 // other one.
3555 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3556 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003557
Chris Lattner3e928bb2005-01-07 07:47:09 +00003558 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003559 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003560 if (!TLI.isLittleEndian())
3561 std::swap(Lo, Hi);
3562 break;
3563 }
Nate Begemanab48be32005-11-22 18:16:00 +00003564 case ISD::VLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003565 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3566 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanab48be32005-11-22 18:16:00 +00003567 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3568 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3569
3570 // If we only have two elements, turn into a pair of scalar loads.
3571 // FIXME: handle case where a vector of two elements is fine, such as
3572 // 2 x double on SSE2.
3573 if (NumElements == 2) {
3574 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3575 // Increment the pointer to the other half.
3576 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3577 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3578 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003579 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003580 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3581 } else {
3582 NumElements /= 2; // Split the vector in half
3583 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3584 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3585 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3586 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003587 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003588 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3589 }
3590
3591 // Build a factor node to remember that this load is independent of the
3592 // other one.
3593 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3594 Hi.getValue(1));
3595
3596 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003597 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemanab48be32005-11-22 18:16:00 +00003598 if (!TLI.isLittleEndian())
3599 std::swap(Lo, Hi);
3600 break;
3601 }
3602 case ISD::VADD:
3603 case ISD::VSUB:
3604 case ISD::VMUL: {
3605 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3606 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3607 SDOperand LL, LH, RL, RH;
3608
3609 ExpandOp(Node->getOperand(0), LL, LH);
3610 ExpandOp(Node->getOperand(1), RL, RH);
3611
3612 // If we only have two elements, turn into a pair of scalar loads.
3613 // FIXME: handle case where a vector of two elements is fine, such as
3614 // 2 x double on SSE2.
3615 if (NumElements == 2) {
3616 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3617 Lo = DAG.getNode(Opc, EVT, LL, RL);
3618 Hi = DAG.getNode(Opc, EVT, LH, RH);
3619 } else {
3620 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3621 LL.getOperand(3));
3622 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3623 LH.getOperand(3));
3624 }
3625 break;
3626 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003627 case ISD::AND:
3628 case ISD::OR:
3629 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3630 SDOperand LL, LH, RL, RH;
3631 ExpandOp(Node->getOperand(0), LL, LH);
3632 ExpandOp(Node->getOperand(1), RL, RH);
3633 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3634 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3635 break;
3636 }
3637 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003638 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003639 ExpandOp(Node->getOperand(1), LL, LH);
3640 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003641 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3642 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003643 break;
3644 }
Nate Begeman9373a812005-08-10 20:51:12 +00003645 case ISD::SELECT_CC: {
3646 SDOperand TL, TH, FL, FH;
3647 ExpandOp(Node->getOperand(2), TL, TH);
3648 ExpandOp(Node->getOperand(3), FL, FH);
3649 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3650 Node->getOperand(1), TL, FL, Node->getOperand(4));
3651 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3652 Node->getOperand(1), TH, FH, Node->getOperand(4));
3653 break;
3654 }
Nate Begeman144ff662005-10-13 17:15:37 +00003655 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003656 SDOperand Chain = Node->getOperand(0);
3657 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003658 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3659
3660 if (EVT == NVT)
3661 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3662 else
3663 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3664 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003665
3666 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003667 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003668
Nate Begeman144ff662005-10-13 17:15:37 +00003669 // The high part is obtained by SRA'ing all but one of the bits of the lo
3670 // part.
3671 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3672 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3673 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003674 break;
3675 }
3676 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003677 SDOperand Chain = Node->getOperand(0);
3678 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003679 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3680
3681 if (EVT == NVT)
3682 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3683 else
3684 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3685 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003686
3687 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003688 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003689
Nate Begeman144ff662005-10-13 17:15:37 +00003690 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003691 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003692 break;
3693 }
3694 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003695 SDOperand Chain = Node->getOperand(0);
3696 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003697 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3698
3699 if (EVT == NVT)
3700 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3701 else
3702 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3703 EVT);
3704
3705 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003706 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003707
3708 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003709 Hi = DAG.getNode(ISD::UNDEF, NVT);
3710 break;
3711 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003712 case ISD::ANY_EXTEND:
3713 // The low part is any extension of the input (which degenerates to a copy).
3714 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3715 // The high part is undefined.
3716 Hi = DAG.getNode(ISD::UNDEF, NVT);
3717 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003718 case ISD::SIGN_EXTEND: {
3719 // The low part is just a sign extension of the input (which degenerates to
3720 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003721 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003722
Chris Lattner3e928bb2005-01-07 07:47:09 +00003723 // The high part is obtained by SRA'ing all but one of the bits of the lo
3724 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003725 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003726 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3727 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003728 break;
3729 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003730 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003731 // The low part is just a zero extension of the input (which degenerates to
3732 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003733 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003734
Chris Lattner3e928bb2005-01-07 07:47:09 +00003735 // The high part is just a zero.
3736 Hi = DAG.getConstant(0, NVT);
3737 break;
Chris Lattner35481892005-12-23 00:16:34 +00003738
3739 case ISD::BIT_CONVERT: {
3740 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3741 Node->getOperand(0));
3742 ExpandOp(Tmp, Lo, Hi);
3743 break;
3744 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003745
Chris Lattner8137c9e2006-01-28 05:07:51 +00003746 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003747 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3748 TargetLowering::Custom &&
3749 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003750 Lo = TLI.LowerOperation(Op, DAG);
3751 assert(Lo.Val && "Node must be custom expanded!");
3752 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003753 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003754 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003755 break;
3756
Chris Lattner4e6c7462005-01-08 19:27:05 +00003757 // These operators cannot be expanded directly, emit them as calls to
3758 // library functions.
3759 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003760 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003761 SDOperand Op;
3762 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3763 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003764 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3765 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003766 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003767
Chris Lattnerf20d1832005-07-30 01:40:57 +00003768 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3769
Chris Lattner80a3e942005-07-29 00:33:32 +00003770 // Now that the custom expander is done, expand the result, which is still
3771 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003772 if (Op.Val) {
3773 ExpandOp(Op, Lo, Hi);
3774 break;
3775 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003776 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003777
Chris Lattner4e6c7462005-01-08 19:27:05 +00003778 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003779 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003780 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003781 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003782 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003783
Chris Lattner4e6c7462005-01-08 19:27:05 +00003784 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003785 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003786 SDOperand Op;
3787 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3788 case Expand: assert(0 && "cannot expand FP!");
3789 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3790 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3791 }
3792
3793 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3794
3795 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003796 if (Op.Val) {
3797 ExpandOp(Op, Lo, Hi);
3798 break;
3799 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003800 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003801
Chris Lattner4e6c7462005-01-08 19:27:05 +00003802 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003803 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003804 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003805 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003806 break;
3807
Evan Cheng05a2d562006-01-09 18:31:59 +00003808 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003809 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003810 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003811 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003812 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003813 Op = TLI.LowerOperation(Op, DAG);
3814 if (Op.Val) {
3815 // Now that the custom expander is done, expand the result, which is
3816 // still VT.
3817 ExpandOp(Op, Lo, Hi);
3818 break;
3819 }
3820 }
3821
Chris Lattnere34b3962005-01-19 04:19:40 +00003822 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003823 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003824 break;
Chris Lattner47599822005-04-02 03:38:53 +00003825
3826 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003827 TargetLowering::LegalizeAction Action =
3828 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3829 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3830 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003831 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003832 break;
3833 }
3834
Chris Lattnere34b3962005-01-19 04:19:40 +00003835 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003836 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003837 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003838 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003839
Evan Cheng05a2d562006-01-09 18:31:59 +00003840 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003841 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003842 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003843 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003844 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003845 Op = TLI.LowerOperation(Op, DAG);
3846 if (Op.Val) {
3847 // Now that the custom expander is done, expand the result, which is
3848 // still VT.
3849 ExpandOp(Op, Lo, Hi);
3850 break;
3851 }
3852 }
3853
Chris Lattnere34b3962005-01-19 04:19:40 +00003854 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003855 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003856 break;
Chris Lattner47599822005-04-02 03:38:53 +00003857
3858 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003859 TargetLowering::LegalizeAction Action =
3860 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3861 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3862 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003863 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003864 break;
3865 }
3866
Chris Lattnere34b3962005-01-19 04:19:40 +00003867 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003868 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003869 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003870 }
3871
3872 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003873 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003874 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003875 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003876 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003877 Op = TLI.LowerOperation(Op, DAG);
3878 if (Op.Val) {
3879 // Now that the custom expander is done, expand the result, which is
3880 // still VT.
3881 ExpandOp(Op, Lo, Hi);
3882 break;
3883 }
3884 }
3885
Chris Lattnere34b3962005-01-19 04:19:40 +00003886 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003887 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003888 break;
Chris Lattner47599822005-04-02 03:38:53 +00003889
3890 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003891 TargetLowering::LegalizeAction Action =
3892 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3893 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3894 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003895 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003896 break;
3897 }
3898
Chris Lattnere34b3962005-01-19 04:19:40 +00003899 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003900 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003901 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003902 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003903
Misha Brukmanedf128a2005-04-21 22:36:52 +00003904 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003905 case ISD::SUB: {
3906 // If the target wants to custom expand this, let them.
3907 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3908 TargetLowering::Custom) {
3909 Op = TLI.LowerOperation(Op, DAG);
3910 if (Op.Val) {
3911 ExpandOp(Op, Lo, Hi);
3912 break;
3913 }
3914 }
3915
3916 // Expand the subcomponents.
3917 SDOperand LHSL, LHSH, RHSL, RHSH;
3918 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3919 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3920
3921 std::vector<SDOperand> Ops;
3922 Ops.push_back(LHSL);
3923 Ops.push_back(LHSH);
3924 Ops.push_back(RHSL);
3925 Ops.push_back(RHSH);
3926 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3927 unsigned Opc =
3928 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3929 Lo = DAG.getNode(Opc, VTs, Ops);
3930 Hi = Lo.getValue(1);
Chris Lattner84f67882005-01-20 18:52:28 +00003931 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003932 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003933 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003934 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003935 SDOperand LL, LH, RL, RH;
3936 ExpandOp(Node->getOperand(0), LL, LH);
3937 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003938 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3939 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3940 // extended the sign bit of the low half through the upper half, and if so
3941 // emit a MULHS instead of the alternate sequence that is valid for any
3942 // i64 x i64 multiply.
3943 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3944 // is RH an extension of the sign bit of RL?
3945 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3946 RH.getOperand(1).getOpcode() == ISD::Constant &&
3947 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3948 // is LH an extension of the sign bit of LL?
3949 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3950 LH.getOperand(1).getOpcode() == ISD::Constant &&
3951 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3952 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3953 } else {
3954 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3955 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3956 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3957 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3958 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3959 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003960 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3961 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003962 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00003963 }
3964 break;
3965 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003966 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3967 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3968 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3969 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003970 }
3971
Chris Lattner83397362005-12-21 18:02:52 +00003972 // Make sure the resultant values have been legalized themselves, unless this
3973 // is a type that requires multi-step expansion.
3974 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3975 Lo = LegalizeOp(Lo);
3976 Hi = LegalizeOp(Hi);
3977 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003978
3979 // Remember in a map if the values will be reused later.
3980 bool isNew =
3981 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
3982 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003983}
3984
3985
3986// SelectionDAG::Legalize - This is the entry point for the file.
3987//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003988void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003989 /// run - This is the main entry point to this class.
3990 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00003991 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003992}
3993