blob: f89b237c053fb5c0d9c96975334ef88570b66777 [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) {
Chris Lattner03d5e872006-01-29 06:00:45 +0000752 Tmp3 = Tmp2.getOperand(0);
753 Tmp4 = Tmp2.getOperand(1);
754 Tmp2 = Tmp2.getOperand(2);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000755 } else {
Chris Lattner03d5e872006-01-29 06:00:45 +0000756 Tmp3 = Tmp2;
757 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
758 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000759 }
Chris Lattner03d5e872006-01-29 06:00:45 +0000760 std::vector<SDOperand> Ops;
761 Ops.push_back(Tmp1);
762 Ops.push_back(Tmp2);
763 Ops.push_back(Tmp3);
764 Ops.push_back(Tmp4);
765 Ops.push_back(Node->getOperand(2));
766 Ops.push_back(Node->getOperand(3));
767 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000768 } else {
769 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +0000770 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000771 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
772 }
Chris Lattner411e8882005-04-09 03:30:19 +0000773 break;
774 }
775 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000776 case ISD::BRTWOWAY_CC:
777 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000778 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000779 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
780 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
781 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
782 Tmp3 != Node->getOperand(3)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000783 std::vector<SDOperand> Ops;
784 Ops.push_back(Tmp1);
785 Ops.push_back(Node->getOperand(1));
786 Ops.push_back(Tmp2);
787 Ops.push_back(Tmp3);
788 Ops.push_back(Node->getOperand(4));
789 Ops.push_back(Node->getOperand(5));
790 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000791 }
792 break;
793 } else {
794 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
795 Node->getOperand(2), // LHS
796 Node->getOperand(3), // RHS
797 Node->getOperand(1)));
798 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
799 // pair.
800 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
801 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000802 case TargetLowering::Legal: {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000803 // If we get a SETCC back from legalizing the SETCC node we just
804 // created, then use its LHS, RHS, and CC directly in creating a new
805 // node. Otherwise, select between the true and false value based on
806 // comparing the result of the legalized with zero.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000807 std::vector<SDOperand> Ops;
808 Ops.push_back(Tmp1);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000809 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000810 Ops.push_back(Tmp2.getOperand(2));
811 Ops.push_back(Tmp2.getOperand(0));
812 Ops.push_back(Tmp2.getOperand(1));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000813 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000814 Ops.push_back(DAG.getCondCode(ISD::SETNE));
815 Ops.push_back(Tmp2);
816 Ops.push_back(DAG.getConstant(0, Tmp2.getValueType()));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000817 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000818 Ops.push_back(Node->getOperand(4));
819 Ops.push_back(Node->getOperand(5));
820 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000821 break;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000822 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000823 case TargetLowering::Expand:
824 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
825 Node->getOperand(4));
826 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
827 break;
828 }
829 }
830 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000831 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000832 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
833 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000834
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000835 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
837 Tmp2 = Result.getValue(0);
838 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000839
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000840 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
841 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000842 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000843 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000844 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +0000845 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000846 Tmp2 = LegalizeOp(Tmp1);
847 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000848 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000849 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000850 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000851 // Since loads produce two values, make sure to remember that we
852 // legalized both of them.
853 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
854 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
855 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000856 }
Chris Lattner0f69b292005-01-15 06:18:18 +0000857 case ISD::EXTLOAD:
858 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000859 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000862
Chris Lattner5f056bf2005-07-10 01:55:33 +0000863 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000864 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000865 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000866 case TargetLowering::Promote:
867 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000868 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
869 DAG.getValueType(MVT::i8));
870 Tmp1 = Result.getValue(0);
871 Tmp2 = Result.getValue(1);
872 break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000873 case TargetLowering::Custom:
874 isCustom = true;
875 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +0000876 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
878 Node->getOperand(3));
879 Tmp1 = Result.getValue(0);
880 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000881
882 if (isCustom) {
883 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
884 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000885 Tmp1 = LegalizeOp(Tmp3);
886 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +0000887 }
888 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000889 break;
Chris Lattner01ff7212005-04-10 22:54:25 +0000890 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +0000891 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000892 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
893 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000894 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000895 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
896 Tmp2 = LegalizeOp(Load.getValue(1));
897 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000898 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000899 assert(Node->getOpcode() != ISD::EXTLOAD &&
900 "EXTLOAD should always be supported!");
901 // Turn the unsupported load into an EXTLOAD followed by an explicit
902 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000903 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
904 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000905 SDOperand ValRes;
906 if (Node->getOpcode() == ISD::SEXTLOAD)
907 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000908 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000909 else
910 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000911 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
912 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
913 break;
Chris Lattner01ff7212005-04-10 22:54:25 +0000914 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000915 // Since loads produce two values, make sure to remember that we legalized
916 // both of them.
917 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
918 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
919 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +0000920 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000921 case ISD::EXTRACT_ELEMENT: {
922 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
923 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000924 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +0000925 case Legal:
926 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
927 // 1 -> Hi
928 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
929 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
930 TLI.getShiftAmountTy()));
931 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
932 } else {
933 // 0 -> Lo
934 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
935 Node->getOperand(0));
936 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000937 break;
938 case Expand:
939 // Get both the low and high parts.
940 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
941 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
942 Result = Tmp2; // 1 -> Hi
943 else
944 Result = Tmp1; // 0 -> Lo
945 break;
946 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000947 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +0000948 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000949
950 case ISD::CopyToReg:
951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000952
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000953 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000954 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +0000955 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000956 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000957 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000958 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +0000959 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000960 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000961 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000962 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
964 Tmp3);
965 } else {
966 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +0000967 }
968
969 // Since this produces two values, make sure to remember that we legalized
970 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000971 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000972 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000973 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +0000974 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000975 break;
976
977 case ISD::RET:
978 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
979 switch (Node->getNumOperands()) {
980 case 2: // ret val
981 switch (getTypeAction(Node->getOperand(1).getValueType())) {
982 case Legal:
983 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000985 break;
986 case Expand: {
987 SDOperand Lo, Hi;
988 ExpandOp(Node->getOperand(1), Lo, Hi);
989 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000990 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000991 }
992 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000993 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000994 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
995 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000996 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000997 }
998 break;
999 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001000 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001001 break;
1002 default: { // ret <values>
1003 std::vector<SDOperand> NewValues;
1004 NewValues.push_back(Tmp1);
1005 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1006 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1007 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001008 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001009 break;
1010 case Expand: {
1011 SDOperand Lo, Hi;
1012 ExpandOp(Node->getOperand(i), Lo, Hi);
1013 NewValues.push_back(Lo);
1014 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001015 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001016 }
1017 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001018 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001019 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001020
1021 if (NewValues.size() == Node->getNumOperands())
1022 Result = DAG.UpdateNodeOperands(Result, NewValues);
1023 else
1024 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025 break;
1026 }
1027 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001028
Chris Lattner47f5bea2006-01-06 05:47:48 +00001029 switch (TLI.getOperationAction(Node->getOpcode(),
1030 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001031 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001032 case TargetLowering::Legal: break;
1033 case TargetLowering::Custom:
1034 Tmp1 = TLI.LowerOperation(Result, DAG);
1035 if (Tmp1.Val) Result = Tmp1;
Evan Cheng17c428e2006-01-06 00:41:43 +00001036 break;
1037 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001038 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001039 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1041 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1042
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001043 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001044 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner03c85462005-01-15 05:21:40 +00001045 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001046 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001047 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001048 } else {
1049 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001050 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001051 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001052 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1053 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001054 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001055 }
1056
Chris Lattner3e928bb2005-01-07 07:47:09 +00001057 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1058 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001059 Tmp3 = LegalizeOp(Node->getOperand(1));
1060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1061 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001062
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001063 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001064 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1065 default: assert(0 && "This action is not supported yet!");
1066 case TargetLowering::Legal: break;
1067 case TargetLowering::Custom:
1068 Tmp1 = TLI.LowerOperation(Result, DAG);
1069 if (Tmp1.Val) Result = Tmp1;
1070 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001071 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001072 break;
1073 }
1074 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001075 // Truncate the value and store the result.
1076 Tmp3 = PromoteOp(Node->getOperand(1));
1077 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001078 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001079 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001080 break;
1081
Chris Lattner3e928bb2005-01-07 07:47:09 +00001082 case Expand:
1083 SDOperand Lo, Hi;
1084 ExpandOp(Node->getOperand(1), Lo, Hi);
1085
1086 if (!TLI.isLittleEndian())
1087 std::swap(Lo, Hi);
1088
Chris Lattneredb1add2005-05-11 04:51:16 +00001089 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1090 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001091 // If this is a vector type, then we have to calculate the increment as
1092 // the product of the element size in bytes, and the number of elements
1093 // in the high half of the vector.
Chris Lattner456a93a2006-01-28 07:39:30 +00001094 unsigned IncrementSize;
Nate Begemanab48be32005-11-22 18:16:00 +00001095 if (MVT::Vector == Hi.getValueType()) {
1096 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1097 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1098 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1099 } else {
1100 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1101 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001102 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1103 getIntPtrConstant(IncrementSize));
1104 assert(isTypeLegal(Tmp2.getValueType()) &&
1105 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001106 // FIXME: This sets the srcvalue of both halves to be the same, which is
1107 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001108 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1109 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001110 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1111 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001112 }
1113 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001114 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001115 case ISD::PCMARKER:
1116 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001117 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001118 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001119 case ISD::STACKSAVE:
1120 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001121 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1122 Tmp1 = Result.getValue(0);
1123 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001124
Chris Lattner140d53c2006-01-13 02:50:02 +00001125 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1126 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001127 case TargetLowering::Legal: break;
1128 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001129 Tmp3 = TLI.LowerOperation(Result, DAG);
1130 if (Tmp3.Val) {
1131 Tmp1 = LegalizeOp(Tmp3);
1132 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001133 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001134 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001135 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001136 // Expand to CopyFromReg if the target set
1137 // StackPointerRegisterToSaveRestore.
1138 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001139 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001140 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001141 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001142 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001143 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1144 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001145 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001146 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001147 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001148
1149 // Since stacksave produce two values, make sure to remember that we
1150 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001151 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1152 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1153 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001154
Chris Lattner140d53c2006-01-13 02:50:02 +00001155 case ISD::STACKRESTORE:
1156 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1157 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001159
1160 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1161 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001162 case TargetLowering::Legal: break;
1163 case TargetLowering::Custom:
1164 Tmp1 = TLI.LowerOperation(Result, DAG);
1165 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001166 break;
1167 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001168 // Expand to CopyToReg if the target set
1169 // StackPointerRegisterToSaveRestore.
1170 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1171 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1172 } else {
1173 Result = Tmp1;
1174 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001175 break;
1176 }
1177 break;
1178
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001179 case ISD::READCYCLECOUNTER:
1180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001182
1183 // Since rdcc produce two values, make sure to remember that we legalized
1184 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001185 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001186 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001187 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001188
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001189 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1192
Chris Lattner456a93a2006-01-28 07:39:30 +00001193 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1194 "Cannot handle illegal TRUNCSTORE yet!");
1195 Tmp2 = LegalizeOp(Node->getOperand(1));
1196
1197 // The only promote case we handle is TRUNCSTORE:i1 X into
1198 // -> TRUNCSTORE:i8 (and X, 1)
1199 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1200 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1201 TargetLowering::Promote) {
1202 // Promote the bool to a mask then store.
1203 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1204 DAG.getConstant(1, Tmp2.getValueType()));
1205 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1206 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001207
Chris Lattner456a93a2006-01-28 07:39:30 +00001208 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1209 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1211 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001212 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001213
Chris Lattner456a93a2006-01-28 07:39:30 +00001214 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1215 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1216 default: assert(0 && "This action is not supported yet!");
1217 case TargetLowering::Legal: break;
1218 case TargetLowering::Custom:
1219 Tmp1 = TLI.LowerOperation(Result, DAG);
1220 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001221 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001222 }
1223 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001224 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001225 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001226 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1227 case Expand: assert(0 && "It's impossible to expand bools");
1228 case Legal:
1229 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1230 break;
1231 case Promote:
1232 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1233 break;
1234 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001235 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001236 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001237
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001239
Nate Begemanb942a3d2005-08-23 04:29:48 +00001240 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001241 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001242 case TargetLowering::Legal: break;
1243 case TargetLowering::Custom: {
1244 Tmp1 = TLI.LowerOperation(Result, DAG);
1245 if (Tmp1.Val) Result = Tmp1;
1246 break;
1247 }
Nate Begeman9373a812005-08-10 20:51:12 +00001248 case TargetLowering::Expand:
1249 if (Tmp1.getOpcode() == ISD::SETCC) {
1250 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1251 Tmp2, Tmp3,
1252 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1253 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001254 // Make sure the condition is either zero or one. It may have been
1255 // promoted from something else.
1256 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001257 Result = DAG.getSelectCC(Tmp1,
1258 DAG.getConstant(0, Tmp1.getValueType()),
1259 Tmp2, Tmp3, ISD::SETNE);
1260 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001261 break;
1262 case TargetLowering::Promote: {
1263 MVT::ValueType NVT =
1264 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1265 unsigned ExtOp, TruncOp;
1266 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001267 ExtOp = ISD::ANY_EXTEND;
1268 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001269 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001270 ExtOp = ISD::FP_EXTEND;
1271 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001272 }
1273 // Promote each of the values to the new type.
1274 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1275 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1276 // Perform the larger operation, then round down.
1277 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1278 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1279 break;
1280 }
1281 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001282 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001283 case ISD::SELECT_CC:
1284 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1285 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1286
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001287 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001288 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1289 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001290
1291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4,
1292 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001293
Chris Lattner23004e52005-08-26 00:23:59 +00001294 // Everything is legal, see if we should expand this op or something.
1295 switch (TLI.getOperationAction(ISD::SELECT_CC,
1296 Node->getOperand(0).getValueType())) {
1297 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001298 case TargetLowering::Legal: break;
1299 case TargetLowering::Custom:
1300 Tmp1 = TLI.LowerOperation(Result, DAG);
1301 if (Tmp1.Val) Result = Tmp1;
Chris Lattner23004e52005-08-26 00:23:59 +00001302 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001303 }
1304 break;
1305 } else {
1306 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1307 Node->getOperand(0), // LHS
1308 Node->getOperand(1), // RHS
1309 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001310 // If we get a SETCC back from legalizing the SETCC node we just
1311 // created, then use its LHS, RHS, and CC directly in creating a new
1312 // node. Otherwise, select between the true and false value based on
1313 // comparing the result of the legalized with zero.
1314 if (Tmp1.getOpcode() == ISD::SETCC) {
1315 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1316 Tmp1.getOperand(0), Tmp1.getOperand(1),
1317 Tmp3, Tmp4, Tmp1.getOperand(2));
1318 } else {
1319 Result = DAG.getSelectCC(Tmp1,
1320 DAG.getConstant(0, Tmp1.getValueType()),
1321 Tmp3, Tmp4, ISD::SETNE);
1322 }
Nate Begeman9373a812005-08-10 20:51:12 +00001323 }
1324 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001325 case ISD::SETCC:
1326 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1327 case Legal:
1328 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1329 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001330 break;
1331 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001332 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1333 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1334
1335 // If this is an FP compare, the operands have already been extended.
1336 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1337 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001338 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001339
1340 // Otherwise, we have to insert explicit sign or zero extends. Note
1341 // that we could insert sign extends for ALL conditions, but zero extend
1342 // is cheaper on many machines (an AND instead of two shifts), so prefer
1343 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001344 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001345 default: assert(0 && "Unknown integer comparison!");
1346 case ISD::SETEQ:
1347 case ISD::SETNE:
1348 case ISD::SETUGE:
1349 case ISD::SETUGT:
1350 case ISD::SETULE:
1351 case ISD::SETULT:
1352 // ALL of these operations will work if we either sign or zero extend
1353 // the operands (including the unsigned comparisons!). Zero extend is
1354 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001355 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1356 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001357 break;
1358 case ISD::SETGE:
1359 case ISD::SETGT:
1360 case ISD::SETLT:
1361 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001362 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1363 DAG.getValueType(VT));
1364 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1365 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001366 break;
1367 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001368 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001369 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001370 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001371 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1372 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1373 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001374 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001375 case ISD::SETEQ:
1376 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001377 if (RHSLo == RHSHi)
1378 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1379 if (RHSCST->isAllOnesValue()) {
1380 // Comparison to -1.
1381 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001382 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001383 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001384 }
1385
Chris Lattner3e928bb2005-01-07 07:47:09 +00001386 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1387 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1388 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001389 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001390 break;
1391 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001392 // If this is a comparison of the sign bit, just look at the top part.
1393 // X > -1, x < 0
1394 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001395 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001396 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001397 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001398 (CST->isAllOnesValue()))) { // X > -1
1399 Tmp1 = LHSHi;
1400 Tmp2 = RHSHi;
1401 break;
1402 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001403
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 // FIXME: This generated code sucks.
1405 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001406 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001407 default: assert(0 && "Unknown integer setcc!");
1408 case ISD::SETLT:
1409 case ISD::SETULT: LowCC = ISD::SETULT; break;
1410 case ISD::SETGT:
1411 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1412 case ISD::SETLE:
1413 case ISD::SETULE: LowCC = ISD::SETULE; break;
1414 case ISD::SETGE:
1415 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1416 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001417
Chris Lattner3e928bb2005-01-07 07:47:09 +00001418 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1419 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1420 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1421
1422 // NOTE: on targets without efficient SELECT of bools, we can always use
1423 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001424 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1425 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1426 Node->getOperand(2));
1427 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001428 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1429 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001430 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001431 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001432 }
1433 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001434
Chris Lattner456a93a2006-01-28 07:39:30 +00001435 switch (TLI.getOperationAction(ISD::SETCC,
1436 Node->getOperand(0).getValueType())) {
1437 default: assert(0 && "Cannot handle this action for SETCC yet!");
1438 case TargetLowering::Custom:
1439 isCustom = true;
1440 // FALLTHROUGH.
1441 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001443 if (isCustom) {
1444 Tmp3 = TLI.LowerOperation(Result, DAG);
1445 if (Tmp3.Val) Result = Tmp3;
1446 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001447 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001448 case TargetLowering::Promote: {
1449 // First step, figure out the appropriate operation to use.
1450 // Allow SETCC to not be supported for all legal data types
1451 // Mostly this targets FP
1452 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1453 MVT::ValueType OldVT = NewInTy;
1454
1455 // Scan for the appropriate larger type to use.
1456 while (1) {
1457 NewInTy = (MVT::ValueType)(NewInTy+1);
1458
1459 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1460 "Fell off of the edge of the integer world");
1461 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1462 "Fell off of the edge of the floating point world");
1463
1464 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001465 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001466 break;
1467 }
1468 if (MVT::isInteger(NewInTy))
1469 assert(0 && "Cannot promote Legal Integer SETCC yet");
1470 else {
1471 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1472 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1473 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001474 Tmp1 = LegalizeOp(Tmp1);
1475 Tmp2 = LegalizeOp(Tmp2);
1476 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001477 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001478 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001479 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001480 case TargetLowering::Expand:
1481 // Expand a setcc node into a select_cc of the same condition, lhs, and
1482 // rhs that selects between const 1 (true) and const 0 (false).
1483 MVT::ValueType VT = Node->getValueType(0);
1484 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1485 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1486 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001487 break;
1488 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001489 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001490 case ISD::MEMSET:
1491 case ISD::MEMCPY:
1492 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001493 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001494 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1495
1496 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1497 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1498 case Expand: assert(0 && "Cannot expand a byte!");
1499 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001500 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001501 break;
1502 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001503 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001504 break;
1505 }
1506 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001507 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001508 }
Chris Lattner272455b2005-02-02 03:44:41 +00001509
1510 SDOperand Tmp4;
1511 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001512 case Expand: {
1513 // Length is too big, just take the lo-part of the length.
1514 SDOperand HiPart;
1515 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1516 break;
1517 }
Chris Lattnere5605212005-01-28 22:29:18 +00001518 case Legal:
1519 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001520 break;
1521 case Promote:
1522 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001523 break;
1524 }
1525
1526 SDOperand Tmp5;
1527 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1528 case Expand: assert(0 && "Cannot expand this yet!");
1529 case Legal:
1530 Tmp5 = LegalizeOp(Node->getOperand(4));
1531 break;
1532 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001533 Tmp5 = PromoteOp(Node->getOperand(4));
1534 break;
1535 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001536
1537 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1538 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001539 case TargetLowering::Custom:
1540 isCustom = true;
1541 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001542 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001544 if (isCustom) {
1545 Tmp1 = TLI.LowerOperation(Result, DAG);
1546 if (Tmp1.Val) Result = Tmp1;
1547 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001548 break;
1549 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001550 // Otherwise, the target does not support this operation. Lower the
1551 // operation to an explicit libcall as appropriate.
1552 MVT::ValueType IntPtr = TLI.getPointerTy();
1553 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1554 std::vector<std::pair<SDOperand, const Type*> > Args;
1555
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001556 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001557 if (Node->getOpcode() == ISD::MEMSET) {
1558 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1559 // Extend the ubyte argument to be an int value for the call.
1560 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1561 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1562 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1563
1564 FnName = "memset";
1565 } else if (Node->getOpcode() == ISD::MEMCPY ||
1566 Node->getOpcode() == ISD::MEMMOVE) {
1567 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1568 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1569 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1570 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1571 } else {
1572 assert(0 && "Unknown op!");
1573 }
Chris Lattner45982da2005-05-12 16:53:42 +00001574
Chris Lattnere1bd8222005-01-11 05:57:22 +00001575 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001576 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001577 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001578 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001579 break;
1580 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001581 }
1582 break;
1583 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001584
1585 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001586 Tmp1 = LegalizeOp(Node->getOperand(0));
1587 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001589
Chris Lattner52d08bd2005-05-09 20:23:03 +00001590 // Since these produce two values, make sure to remember that we legalized
1591 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001592 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner52d08bd2005-05-09 20:23:03 +00001593 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001594 return Result;
Chris Lattner52d08bd2005-05-09 20:23:03 +00001595 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001596 Tmp1 = LegalizeOp(Node->getOperand(0));
1597 Tmp2 = LegalizeOp(Node->getOperand(1));
1598 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001600 break;
1601
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001602 case ISD::READIO:
1603 Tmp1 = LegalizeOp(Node->getOperand(0));
1604 Tmp2 = LegalizeOp(Node->getOperand(1));
1605
1606 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1607 case TargetLowering::Custom:
1608 default: assert(0 && "This action not implemented for this operation!");
1609 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001611 break;
1612 case TargetLowering::Expand:
1613 // Replace this with a load from memory.
1614 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1615 Node->getOperand(1), DAG.getSrcValue(NULL));
1616 Result = LegalizeOp(Result);
1617 break;
1618 }
1619
1620 // Since these produce two values, make sure to remember that we legalized
1621 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001622 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001623 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1624 return Result.getValue(Op.ResNo);
1625
1626 case ISD::WRITEIO:
1627 Tmp1 = LegalizeOp(Node->getOperand(0));
1628 Tmp2 = LegalizeOp(Node->getOperand(1));
1629 Tmp3 = LegalizeOp(Node->getOperand(2));
1630
1631 switch (TLI.getOperationAction(Node->getOpcode(),
1632 Node->getOperand(1).getValueType())) {
1633 case TargetLowering::Custom:
1634 default: assert(0 && "This action not implemented for this operation!");
1635 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001637 break;
1638 case TargetLowering::Expand:
1639 // Replace this with a store to memory.
1640 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1641 Node->getOperand(1), Node->getOperand(2),
1642 DAG.getSrcValue(NULL));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001643 break;
1644 }
1645 break;
1646
Chris Lattner84f67882005-01-20 18:52:28 +00001647 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001648 case ISD::SUB_PARTS:
1649 case ISD::SHL_PARTS:
1650 case ISD::SRA_PARTS:
1651 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001652 std::vector<SDOperand> Ops;
1653 bool Changed = false;
1654 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1655 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1656 Changed |= Ops.back() != Node->getOperand(i);
1657 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001658 if (Changed)
1659 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001660
Evan Cheng05a2d562006-01-09 18:31:59 +00001661 switch (TLI.getOperationAction(Node->getOpcode(),
1662 Node->getValueType(0))) {
1663 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001664 case TargetLowering::Legal: break;
1665 case TargetLowering::Custom:
1666 Tmp1 = TLI.LowerOperation(Result, DAG);
1667 if (Tmp1.Val) {
1668 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001669 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001670 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001671 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1672 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001673 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001674 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001675 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001676 return RetVal;
1677 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001678 break;
1679 }
1680
Chris Lattner2c8086f2005-04-02 05:00:07 +00001681 // Since these produce multiple values, make sure to remember that we
1682 // legalized all of them.
1683 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1684 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1685 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001686 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001687
1688 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001689 case ISD::ADD:
1690 case ISD::SUB:
1691 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001692 case ISD::MULHS:
1693 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001694 case ISD::UDIV:
1695 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001696 case ISD::AND:
1697 case ISD::OR:
1698 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001699 case ISD::SHL:
1700 case ISD::SRL:
1701 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001702 case ISD::FADD:
1703 case ISD::FSUB:
1704 case ISD::FMUL:
1705 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001706 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001707 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1708 case Expand: assert(0 && "Not possible");
1709 case Legal:
1710 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1711 break;
1712 case Promote:
1713 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1714 break;
1715 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001716
1717 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001718
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001720 default: assert(0 && "Operation not supported");
1721 case TargetLowering::Legal: break;
1722 case TargetLowering::Custom:
1723 Tmp1 = TLI.LowerOperation(Result, DAG);
1724 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001725 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001726 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001727 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001728
Nate Begeman419f8b62005-10-18 00:27:41 +00001729 case ISD::BUILD_PAIR: {
1730 MVT::ValueType PairTy = Node->getValueType(0);
1731 // TODO: handle the case where the Lo and Hi operands are not of legal type
1732 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1733 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1734 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001735 case TargetLowering::Promote:
1736 case TargetLowering::Custom:
1737 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001738 case TargetLowering::Legal:
1739 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1740 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1741 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001742 case TargetLowering::Expand:
1743 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1744 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1745 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1746 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1747 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001748 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001749 break;
1750 }
1751 break;
1752 }
1753
Nate Begemanc105e192005-04-06 00:23:54 +00001754 case ISD::UREM:
1755 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001756 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001757 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1758 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001759
Nate Begemanc105e192005-04-06 00:23:54 +00001760 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001761 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1762 case TargetLowering::Custom:
1763 isCustom = true;
1764 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001765 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001767 if (isCustom) {
1768 Tmp1 = TLI.LowerOperation(Result, DAG);
1769 if (Tmp1.Val) Result = Tmp1;
1770 }
Nate Begemanc105e192005-04-06 00:23:54 +00001771 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001772 case TargetLowering::Expand:
1773 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001774 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001775 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001776 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001777 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1778 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1779 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1780 } else {
1781 // Floating point mod -> fmod libcall.
1782 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1783 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001784 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001785 }
1786 break;
1787 }
1788 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001789 case ISD::VAARG: {
1790 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1791 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1792
Chris Lattner5c62f332006-01-28 07:42:08 +00001793 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001794 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1795 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001796 case TargetLowering::Custom:
1797 isCustom = true;
1798 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001799 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001800 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1801 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001802 Tmp1 = Result.getValue(1);
1803
1804 if (isCustom) {
1805 Tmp2 = TLI.LowerOperation(Result, DAG);
1806 if (Tmp2.Val) {
1807 Result = LegalizeOp(Tmp2);
1808 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1809 }
1810 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001811 break;
1812 case TargetLowering::Expand: {
1813 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1814 Node->getOperand(2));
1815 // Increment the pointer, VAList, to the next vaarg
1816 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1817 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1818 TLI.getPointerTy()));
1819 // Store the incremented VAList to the legalized pointer
1820 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1821 Node->getOperand(2));
1822 // Load the actual argument out of the pointer VAList
1823 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001824 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001825 Result = LegalizeOp(Result);
1826 break;
1827 }
1828 }
1829 // Since VAARG produces two values, make sure to remember that we
1830 // legalized both of them.
1831 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001832 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1833 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001834 }
1835
1836 case ISD::VACOPY:
1837 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1838 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1839 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1840
1841 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1842 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001843 case TargetLowering::Custom:
1844 isCustom = true;
1845 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001846 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001847 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1848 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001849 if (isCustom) {
1850 Tmp1 = TLI.LowerOperation(Result, DAG);
1851 if (Tmp1.Val) Result = Tmp1;
1852 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001853 break;
1854 case TargetLowering::Expand:
1855 // This defaults to loading a pointer from the input and storing it to the
1856 // output, returning the chain.
1857 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1858 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1859 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00001860 break;
1861 }
1862 break;
1863
1864 case ISD::VAEND:
1865 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1866 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1867
1868 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1869 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001870 case TargetLowering::Custom:
1871 isCustom = true;
1872 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001873 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001875 if (isCustom) {
1876 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1877 if (Tmp1.Val) Result = Tmp1;
1878 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001879 break;
1880 case TargetLowering::Expand:
1881 Result = Tmp1; // Default to a no-op, return the chain
1882 break;
1883 }
1884 break;
1885
1886 case ISD::VASTART:
1887 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1888 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1889
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001890 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1891
Nate Begemanacc398c2006-01-25 18:21:52 +00001892 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1893 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001894 case TargetLowering::Legal: break;
1895 case TargetLowering::Custom:
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00001898 break;
1899 }
1900 break;
1901
Nate Begeman35ef9132006-01-11 21:21:00 +00001902 case ISD::ROTL:
1903 case ISD::ROTR:
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1905 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001906
1907 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1908 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00001910 break;
1911
Nate Begemand88fc032006-01-14 03:14:10 +00001912 case ISD::BSWAP:
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1914 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001915 case TargetLowering::Custom:
1916 assert(0 && "Cannot custom legalize this yet!");
1917 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001918 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001919 break;
1920 case TargetLowering::Promote: {
1921 MVT::ValueType OVT = Tmp1.getValueType();
1922 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1923 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00001924
Chris Lattner456a93a2006-01-28 07:39:30 +00001925 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1926 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1927 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1928 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1929 break;
1930 }
1931 case TargetLowering::Expand:
1932 Result = ExpandBSWAP(Tmp1);
1933 break;
Nate Begemand88fc032006-01-14 03:14:10 +00001934 }
1935 break;
1936
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001937 case ISD::CTPOP:
1938 case ISD::CTTZ:
1939 case ISD::CTLZ:
1940 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1941 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001942 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001943 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001944 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001945 break;
1946 case TargetLowering::Promote: {
1947 MVT::ValueType OVT = Tmp1.getValueType();
1948 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001949
1950 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001951 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1952 // Perform the larger operation, then subtract if needed.
1953 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001954 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001955 case ISD::CTPOP:
1956 Result = Tmp1;
1957 break;
1958 case ISD::CTTZ:
1959 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001960 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1961 DAG.getConstant(getSizeInBits(NVT), NVT),
1962 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001963 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001964 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1965 break;
1966 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00001967 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001968 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1969 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001970 getSizeInBits(OVT), NVT));
1971 break;
1972 }
1973 break;
1974 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001975 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00001976 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001977 break;
1978 }
1979 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001980
Chris Lattner2c8086f2005-04-02 05:00:07 +00001981 // Unary operators
1982 case ISD::FABS:
1983 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001984 case ISD::FSQRT:
1985 case ISD::FSIN:
1986 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001987 Tmp1 = LegalizeOp(Node->getOperand(0));
1988 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001989 case TargetLowering::Promote:
1990 case TargetLowering::Custom:
1991 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001992 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001993 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001994 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001995 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00001996 switch (Node->getOpcode()) {
1997 default: assert(0 && "Unreachable!");
1998 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001999 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2000 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002001 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002002 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002003 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002004 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2005 MVT::ValueType VT = Node->getValueType(0);
2006 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002007 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002008 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2009 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002010 break;
2011 }
2012 case ISD::FSQRT:
2013 case ISD::FSIN:
2014 case ISD::FCOS: {
2015 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002016 const char *FnName = 0;
2017 switch(Node->getOpcode()) {
2018 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2019 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2020 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2021 default: assert(0 && "Unreachable!");
2022 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002023 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002024 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002025 break;
2026 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002027 }
2028 break;
2029 }
2030 break;
Chris Lattner35481892005-12-23 00:16:34 +00002031
2032 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002033 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002034 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002035 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002036 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2037 Node->getOperand(0).getValueType())) {
2038 default: assert(0 && "Unknown operation action!");
2039 case TargetLowering::Expand:
2040 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2041 break;
2042 case TargetLowering::Legal:
2043 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002044 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002045 break;
2046 }
2047 }
2048 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002049 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002050 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002051 case ISD::UINT_TO_FP: {
2052 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002053 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2054 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002055 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002056 Node->getOperand(0).getValueType())) {
2057 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002058 case TargetLowering::Custom:
2059 isCustom = true;
2060 // FALLTHROUGH
2061 case TargetLowering::Legal:
2062 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002063 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002064 if (isCustom) {
2065 Tmp1 = TLI.LowerOperation(Result, DAG);
2066 if (Tmp1.Val) Result = Tmp1;
2067 }
2068 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002069 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002070 Result = ExpandLegalINT_TO_FP(isSigned,
2071 LegalizeOp(Node->getOperand(0)),
2072 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002073 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002074 case TargetLowering::Promote:
2075 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2076 Node->getValueType(0),
2077 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002078 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002079 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002080 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002081 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002082 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2083 Node->getValueType(0), Node->getOperand(0));
2084 break;
2085 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002086 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002087 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002088 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2089 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002090 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002091 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2092 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002093 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002094 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2095 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002096 break;
2097 }
2098 break;
2099 }
2100 case ISD::TRUNCATE:
2101 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2102 case Legal:
2103 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002104 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002105 break;
2106 case Expand:
2107 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2108
2109 // Since the result is legal, we should just be able to truncate the low
2110 // part of the source.
2111 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2112 break;
2113 case Promote:
2114 Result = PromoteOp(Node->getOperand(0));
2115 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2116 break;
2117 }
2118 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002119
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002120 case ISD::FP_TO_SINT:
2121 case ISD::FP_TO_UINT:
2122 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2123 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002124 Tmp1 = LegalizeOp(Node->getOperand(0));
2125
Chris Lattner1618beb2005-07-29 00:11:56 +00002126 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2127 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002128 case TargetLowering::Custom:
2129 isCustom = true;
2130 // FALLTHROUGH
2131 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002132 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002133 if (isCustom) {
2134 Tmp1 = TLI.LowerOperation(Result, DAG);
2135 if (Tmp1.Val) Result = Tmp1;
2136 }
2137 break;
2138 case TargetLowering::Promote:
2139 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2140 Node->getOpcode() == ISD::FP_TO_SINT);
2141 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002142 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002143 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2144 SDOperand True, False;
2145 MVT::ValueType VT = Node->getOperand(0).getValueType();
2146 MVT::ValueType NVT = Node->getValueType(0);
2147 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2148 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2149 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2150 Node->getOperand(0), Tmp2, ISD::SETLT);
2151 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2152 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002153 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002154 Tmp2));
2155 False = DAG.getNode(ISD::XOR, NVT, False,
2156 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002157 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2158 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002159 } else {
2160 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2161 }
2162 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002163 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002164 break;
2165 case Expand:
2166 assert(0 && "Shouldn't need to expand other operators here!");
2167 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002168 Tmp1 = PromoteOp(Node->getOperand(0));
2169 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2170 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002171 break;
2172 }
2173 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002174
Chris Lattner13c78e22005-09-02 00:18:10 +00002175 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002176 case ISD::ZERO_EXTEND:
2177 case ISD::SIGN_EXTEND:
2178 case ISD::FP_EXTEND:
2179 case ISD::FP_ROUND:
2180 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002181 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002182 case Legal:
2183 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002184 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002185 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002186 case Promote:
2187 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002188 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002189 Tmp1 = PromoteOp(Node->getOperand(0));
2190 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002191 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002192 case ISD::ZERO_EXTEND:
2193 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002194 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002195 Result = DAG.getZeroExtendInReg(Result,
2196 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002197 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002198 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002199 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002200 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002201 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002202 Result,
2203 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002204 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002205 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002206 Result = PromoteOp(Node->getOperand(0));
2207 if (Result.getValueType() != Op.getValueType())
2208 // Dynamically dead while we have only 2 FP types.
2209 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2210 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002211 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002212 Result = PromoteOp(Node->getOperand(0));
2213 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2214 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002215 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002216 }
2217 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002218 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002219 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002220 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002221 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002222
2223 // If this operation is not supported, convert it to a shl/shr or load/store
2224 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002225 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2226 default: assert(0 && "This action not supported for this op yet!");
2227 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002228 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002229 break;
2230 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002231 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002232 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002233 // NOTE: we could fall back on load/store here too for targets without
2234 // SAR. However, it is doubtful that any exist.
2235 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2236 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002237 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002238 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2239 Node->getOperand(0), ShiftCst);
2240 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2241 Result, ShiftCst);
2242 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2243 // The only way we can lower this is to turn it into a STORETRUNC,
2244 // EXTLOAD pair, targetting a temporary location (a stack slot).
2245
2246 // NOTE: there is a choice here between constantly creating new stack
2247 // slots and always reusing the same one. We currently always create
2248 // new ones, as reuse may inhibit scheduling.
2249 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2250 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2251 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2252 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002253 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002254 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2255 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2256 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002257 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002258 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002259 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2260 Result, StackSlot, DAG.getSrcValue(NULL),
2261 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002262 } else {
2263 assert(0 && "Unknown op");
2264 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002265 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002266 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002267 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002268 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002269 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002270
2271 // Make sure that the generated code is itself legal.
2272 if (Result != Op)
2273 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002274
Chris Lattner45982da2005-05-12 16:53:42 +00002275 // Note that LegalizeOp may be reentered even from single-use nodes, which
2276 // means that we always must cache transformed nodes.
2277 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002278 return Result;
2279}
2280
Chris Lattner8b6fa222005-01-15 22:16:26 +00002281/// PromoteOp - Given an operation that produces a value in an invalid type,
2282/// promote it to compute the value into a larger type. The produced value will
2283/// have the correct bits for the low portion of the register, but no guarantee
2284/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002285SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2286 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002287 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002288 assert(getTypeAction(VT) == Promote &&
2289 "Caller should expand or legalize operands that are not promotable!");
2290 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2291 "Cannot promote to smaller type!");
2292
Chris Lattner03c85462005-01-15 05:21:40 +00002293 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002294 SDOperand Result;
2295 SDNode *Node = Op.Val;
2296
Chris Lattner6fdcb252005-09-02 20:32:45 +00002297 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2298 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002299
Chris Lattner03c85462005-01-15 05:21:40 +00002300 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002301 case ISD::CopyFromReg:
2302 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002303 default:
2304 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2305 assert(0 && "Do not know how to promote this operator!");
2306 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002307 case ISD::UNDEF:
2308 Result = DAG.getNode(ISD::UNDEF, NVT);
2309 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002310 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002311 if (VT != MVT::i1)
2312 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2313 else
2314 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002315 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2316 break;
2317 case ISD::ConstantFP:
2318 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2319 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2320 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002321
Chris Lattner82fbfb62005-01-18 02:59:52 +00002322 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002323 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002324 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2325 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002326 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002327
2328 case ISD::TRUNCATE:
2329 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2330 case Legal:
2331 Result = LegalizeOp(Node->getOperand(0));
2332 assert(Result.getValueType() >= NVT &&
2333 "This truncation doesn't make sense!");
2334 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2335 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2336 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002337 case Promote:
2338 // The truncation is not required, because we don't guarantee anything
2339 // about high bits anyway.
2340 Result = PromoteOp(Node->getOperand(0));
2341 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002342 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002343 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2344 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002345 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002346 }
2347 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002348 case ISD::SIGN_EXTEND:
2349 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002350 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002351 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2352 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2353 case Legal:
2354 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002355 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002356 break;
2357 case Promote:
2358 // Promote the reg if it's smaller.
2359 Result = PromoteOp(Node->getOperand(0));
2360 // The high bits are not guaranteed to be anything. Insert an extend.
2361 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002362 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002363 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002364 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002365 Result = DAG.getZeroExtendInReg(Result,
2366 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002367 break;
2368 }
2369 break;
Chris Lattner35481892005-12-23 00:16:34 +00002370 case ISD::BIT_CONVERT:
2371 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2372 Result = PromoteOp(Result);
2373 break;
2374
Chris Lattner8b6fa222005-01-15 22:16:26 +00002375 case ISD::FP_EXTEND:
2376 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2377 case ISD::FP_ROUND:
2378 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2379 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2380 case Promote: assert(0 && "Unreachable with 2 FP types!");
2381 case Legal:
2382 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002383 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002384 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002385 break;
2386 }
2387 break;
2388
2389 case ISD::SINT_TO_FP:
2390 case ISD::UINT_TO_FP:
2391 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2392 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002393 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002394 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002395 break;
2396
2397 case Promote:
2398 Result = PromoteOp(Node->getOperand(0));
2399 if (Node->getOpcode() == ISD::SINT_TO_FP)
2400 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002401 Result,
2402 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002403 else
Chris Lattner23993562005-04-13 02:38:47 +00002404 Result = DAG.getZeroExtendInReg(Result,
2405 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002406 // No extra round required here.
2407 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002408 break;
2409 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002410 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2411 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002412 // Round if we cannot tolerate excess precision.
2413 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002414 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2415 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002416 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002417 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002418 break;
2419
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002420 case ISD::SIGN_EXTEND_INREG:
2421 Result = PromoteOp(Node->getOperand(0));
2422 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2423 Node->getOperand(1));
2424 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002425 case ISD::FP_TO_SINT:
2426 case ISD::FP_TO_UINT:
2427 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2428 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002429 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002430 break;
2431 case Promote:
2432 // The input result is prerounded, so we don't have to do anything
2433 // special.
2434 Tmp1 = PromoteOp(Node->getOperand(0));
2435 break;
2436 case Expand:
2437 assert(0 && "not implemented");
2438 }
Nate Begemand2558e32005-08-14 01:20:53 +00002439 // If we're promoting a UINT to a larger size, check to see if the new node
2440 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2441 // we can use that instead. This allows us to generate better code for
2442 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2443 // legal, such as PowerPC.
2444 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002445 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002446 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2447 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002448 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2449 } else {
2450 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2451 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002452 break;
2453
Chris Lattner2c8086f2005-04-02 05:00:07 +00002454 case ISD::FABS:
2455 case ISD::FNEG:
2456 Tmp1 = PromoteOp(Node->getOperand(0));
2457 assert(Tmp1.getValueType() == NVT);
2458 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2459 // NOTE: we do not have to do any extra rounding here for
2460 // NoExcessFPPrecision, because we know the input will have the appropriate
2461 // precision, and these operations don't modify precision at all.
2462 break;
2463
Chris Lattnerda6ba872005-04-28 21:44:33 +00002464 case ISD::FSQRT:
2465 case ISD::FSIN:
2466 case ISD::FCOS:
2467 Tmp1 = PromoteOp(Node->getOperand(0));
2468 assert(Tmp1.getValueType() == NVT);
2469 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002470 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002471 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2472 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002473 break;
2474
Chris Lattner03c85462005-01-15 05:21:40 +00002475 case ISD::AND:
2476 case ISD::OR:
2477 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002478 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002479 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002480 case ISD::MUL:
2481 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002482 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002483 // that too is okay if they are integer operations.
2484 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);
Chris Lattner01b3d732005-09-28 22:28:18 +00002488 break;
2489 case ISD::FADD:
2490 case ISD::FSUB:
2491 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002492 Tmp1 = PromoteOp(Node->getOperand(0));
2493 Tmp2 = PromoteOp(Node->getOperand(1));
2494 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2495 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2496
2497 // Floating point operations will give excess precision that we may not be
2498 // able to tolerate. If we DO allow excess precision, just leave it,
2499 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002500 // FIXME: Why would we need to round FP ops more than integer ones?
2501 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002502 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002503 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2504 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002505 break;
2506
Chris Lattner8b6fa222005-01-15 22:16:26 +00002507 case ISD::SDIV:
2508 case ISD::SREM:
2509 // These operators require that their input be sign extended.
2510 Tmp1 = PromoteOp(Node->getOperand(0));
2511 Tmp2 = PromoteOp(Node->getOperand(1));
2512 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002513 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2514 DAG.getValueType(VT));
2515 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2516 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002517 }
2518 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2519
2520 // Perform FP_ROUND: this is probably overly pessimistic.
2521 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002522 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2523 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002524 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002525 case ISD::FDIV:
2526 case ISD::FREM:
2527 // These operators require that their input be fp extended.
2528 Tmp1 = PromoteOp(Node->getOperand(0));
2529 Tmp2 = PromoteOp(Node->getOperand(1));
2530 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2531
2532 // Perform FP_ROUND: this is probably overly pessimistic.
2533 if (NoExcessFPPrecision)
2534 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2535 DAG.getValueType(VT));
2536 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002537
2538 case ISD::UDIV:
2539 case ISD::UREM:
2540 // These operators require that their input be zero extended.
2541 Tmp1 = PromoteOp(Node->getOperand(0));
2542 Tmp2 = PromoteOp(Node->getOperand(1));
2543 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002544 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2545 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002546 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2547 break;
2548
2549 case ISD::SHL:
2550 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002551 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002552 break;
2553 case ISD::SRA:
2554 // The input value must be properly sign extended.
2555 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002556 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2557 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002558 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002559 break;
2560 case ISD::SRL:
2561 // The input value must be properly zero extended.
2562 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002563 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002564 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002565 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002566
2567 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002568 Tmp1 = Node->getOperand(0); // Get the chain.
2569 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002570 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2571 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2572 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2573 } else {
2574 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2575 Node->getOperand(2));
2576 // Increment the pointer, VAList, to the next vaarg
2577 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2578 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2579 TLI.getPointerTy()));
2580 // Store the incremented VAList to the legalized pointer
2581 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2582 Node->getOperand(2));
2583 // Load the actual argument out of the pointer VAList
2584 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2585 DAG.getSrcValue(0), VT);
2586 }
2587 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002588 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002589 break;
2590
Chris Lattner03c85462005-01-15 05:21:40 +00002591 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002592 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2593 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002594 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002595 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002596 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002597 case ISD::SEXTLOAD:
2598 case ISD::ZEXTLOAD:
2599 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002600 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2601 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002602 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002603 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002604 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002605 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002606 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002607 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2608 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002609 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002610 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002611 case ISD::SELECT_CC:
2612 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2613 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2614 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002615 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002616 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002617 case ISD::BSWAP:
2618 Tmp1 = Node->getOperand(0);
2619 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2620 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2621 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2622 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2623 TLI.getShiftAmountTy()));
2624 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002625 case ISD::CTPOP:
2626 case ISD::CTTZ:
2627 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002628 // Zero extend the argument
2629 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002630 // Perform the larger operation, then subtract if needed.
2631 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002632 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002633 case ISD::CTPOP:
2634 Result = Tmp1;
2635 break;
2636 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002637 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002638 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002639 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002640 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002641 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002642 break;
2643 case ISD::CTLZ:
2644 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002645 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2646 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002647 getSizeInBits(VT), NVT));
2648 break;
2649 }
2650 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002651 }
2652
2653 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002654
2655 // Make sure the result is itself legal.
2656 Result = LegalizeOp(Result);
2657
2658 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002659 AddPromotedOperand(Op, Result);
2660 return Result;
2661}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002662
Chris Lattner35481892005-12-23 00:16:34 +00002663/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002664/// The resultant code need not be legal. Note that SrcOp is the input operand
2665/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002666SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2667 SDOperand SrcOp) {
2668 // Create the stack frame object.
2669 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2670 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002671 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002672 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2673
2674 // Emit a store to the stack slot.
2675 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002676 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002677 // Result is a load from the stack slot.
2678 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2679}
2680
Chris Lattner5b359c62005-04-02 04:00:59 +00002681void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2682 SDOperand Op, SDOperand Amt,
2683 SDOperand &Lo, SDOperand &Hi) {
2684 // Expand the subcomponents.
2685 SDOperand LHSL, LHSH;
2686 ExpandOp(Op, LHSL, LHSH);
2687
2688 std::vector<SDOperand> Ops;
2689 Ops.push_back(LHSL);
2690 Ops.push_back(LHSH);
2691 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002692 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002693 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002694 Hi = Lo.getValue(1);
2695}
2696
2697
Chris Lattnere34b3962005-01-19 04:19:40 +00002698/// ExpandShift - Try to find a clever way to expand this shift operation out to
2699/// smaller elements. If we can't find a way that is more efficient than a
2700/// libcall on this target, return false. Otherwise, return true with the
2701/// low-parts expanded into Lo and Hi.
2702bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2703 SDOperand &Lo, SDOperand &Hi) {
2704 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2705 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002706
Chris Lattnere34b3962005-01-19 04:19:40 +00002707 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002708 SDOperand ShAmt = LegalizeOp(Amt);
2709 MVT::ValueType ShTy = ShAmt.getValueType();
2710 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2711 unsigned NVTBits = MVT::getSizeInBits(NVT);
2712
2713 // Handle the case when Amt is an immediate. Other cases are currently broken
2714 // and are disabled.
2715 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2716 unsigned Cst = CN->getValue();
2717 // Expand the incoming operand to be shifted, so that we have its parts
2718 SDOperand InL, InH;
2719 ExpandOp(Op, InL, InH);
2720 switch(Opc) {
2721 case ISD::SHL:
2722 if (Cst > VTBits) {
2723 Lo = DAG.getConstant(0, NVT);
2724 Hi = DAG.getConstant(0, NVT);
2725 } else if (Cst > NVTBits) {
2726 Lo = DAG.getConstant(0, NVT);
2727 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002728 } else if (Cst == NVTBits) {
2729 Lo = DAG.getConstant(0, NVT);
2730 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002731 } else {
2732 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2733 Hi = DAG.getNode(ISD::OR, NVT,
2734 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2735 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2736 }
2737 return true;
2738 case ISD::SRL:
2739 if (Cst > VTBits) {
2740 Lo = DAG.getConstant(0, NVT);
2741 Hi = DAG.getConstant(0, NVT);
2742 } else if (Cst > NVTBits) {
2743 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2744 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002745 } else if (Cst == NVTBits) {
2746 Lo = InH;
2747 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002748 } else {
2749 Lo = DAG.getNode(ISD::OR, NVT,
2750 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2751 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2752 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2753 }
2754 return true;
2755 case ISD::SRA:
2756 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002757 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002758 DAG.getConstant(NVTBits-1, ShTy));
2759 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002760 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002761 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002762 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002763 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002764 } else if (Cst == NVTBits) {
2765 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002766 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002767 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002768 } else {
2769 Lo = DAG.getNode(ISD::OR, NVT,
2770 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2771 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2772 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2773 }
2774 return true;
2775 }
2776 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002777 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002778}
Chris Lattner77e77a62005-01-21 06:05:23 +00002779
Chris Lattner9530ddc2005-05-13 05:09:11 +00002780/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2781/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002782/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00002783static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
2784 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00002785 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattnerde387ce2006-01-09 23:21:49 +00002786 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002787
Chris Lattner16cd04d2005-05-12 23:24:06 +00002788 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002789 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002790 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002791 Found = Node;
2792 return;
2793 }
2794
2795 // Otherwise, scan the operands of Node to see if any of them is a call.
2796 assert(Node->getNumOperands() != 0 &&
2797 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002798 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00002799 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002800
2801 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002802 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00002803 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002804}
2805
2806
Chris Lattner9530ddc2005-05-13 05:09:11 +00002807/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2808/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002809/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002810static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2811 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00002812 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner82299e72005-08-05 18:10:27 +00002813 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002814
Chris Lattner16cd04d2005-05-12 23:24:06 +00002815 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002816 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002817 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002818 Found = Node;
2819 return;
2820 }
2821
2822 // Otherwise, scan the operands of Node to see if any of them is a call.
2823 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2824 if (UI == E) return;
2825 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002826 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002827
2828 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002829 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002830}
2831
Chris Lattner9530ddc2005-05-13 05:09:11 +00002832/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002833/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002834static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002835 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002836 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002837 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002838 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002839
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002840 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002841 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002842 if (TheChain.getValueType() != MVT::Other) {
2843 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00002844 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00002845 if (TheChain.getValueType() != MVT::Other) {
2846 // Otherwise, hunt for it.
2847 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
2848 if (Node->getValueType(i) == MVT::Other) {
2849 TheChain = SDOperand(Node, i);
2850 break;
2851 }
2852
2853 // Otherwise, we walked into a node without a chain.
2854 if (TheChain.getValueType() != MVT::Other)
2855 return 0;
2856 }
2857 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002858
2859 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002860 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002861
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002862 // Make sure to only follow users of our token chain.
2863 SDNode *User = *UI;
2864 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2865 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002866 if (SDNode *Result = FindCallSeqEnd(User))
2867 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002868 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002869 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002870}
2871
Chris Lattner9530ddc2005-05-13 05:09:11 +00002872/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002873/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002874static SDNode *FindCallSeqStart(SDNode *Node) {
2875 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002876 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002877
2878 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2879 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002880 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002881}
2882
2883
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002884/// FindInputOutputChains - If we are replacing an operation with a call we need
2885/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002886/// properly serialize the calls in the block. The returned operand is the
2887/// input chain value for the new call (e.g. the entry node or the previous
2888/// call), and OutChain is set to be the chain node to update to point to the
2889/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002890static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2891 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002892 SDNode *LatestCallSeqStart = Entry.Val;
2893 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00002894 std::set<SDNode*> Visited;
2895 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
2896 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00002897 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002898
Chris Lattner16cd04d2005-05-12 23:24:06 +00002899 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002900 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002901 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2902 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002903 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002904 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002905 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2906 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002907 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002908 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002909 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002910
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002911 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002912 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002913 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002914 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00002915 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002916
Chris Lattner9530ddc2005-05-13 05:09:11 +00002917 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002918 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002919 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002920
Chris Lattner9530ddc2005-05-13 05:09:11 +00002921 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002922}
2923
Jeff Cohen00b168892005-07-27 06:12:32 +00002924/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002925void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2926 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002927 // Nothing to splice it into?
2928 if (OutChain == 0) return;
2929
2930 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2931 //OutChain->dump();
2932
2933 // Form a token factor node merging the old inval and the new inval.
2934 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2935 OutChain->getOperand(0));
2936 // Change the node to refer to the new token.
2937 OutChain->setAdjCallChain(InToken);
2938}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002939
2940
Chris Lattner77e77a62005-01-21 06:05:23 +00002941// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2942// does not fit into a register, return the lo part and set the hi part to the
2943// by-reg argument. If it does fit into a single register, return the result
2944// and leave the Hi part unset.
2945SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2946 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002947 SDNode *OutChain;
2948 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2949 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002950 if (InChain.Val == 0)
2951 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002952
Chris Lattner77e77a62005-01-21 06:05:23 +00002953 TargetLowering::ArgListTy Args;
2954 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2955 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2956 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2957 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2958 }
2959 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002960
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002961 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002962 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002963 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002964 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2965 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002966
Chris Lattner99c25b82005-09-02 20:26:58 +00002967 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002968 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002969 default: assert(0 && "Unknown thing");
2970 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002971 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00002972 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002973 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002974 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00002975 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002976 }
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002977
2978 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00002979 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner99c25b82005-09-02 20:26:58 +00002980 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002981}
2982
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002983
Chris Lattner77e77a62005-01-21 06:05:23 +00002984/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2985/// destination type is legal.
2986SDOperand SelectionDAGLegalize::
2987ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002988 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002989 assert(getTypeAction(Source.getValueType()) == Expand &&
2990 "This is not an expansion!");
2991 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2992
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002993 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002994 assert(Source.getValueType() == MVT::i64 &&
2995 "This only works for 64-bit -> FP");
2996 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2997 // incoming integer is set. To handle this, we dynamically test to see if
2998 // it is set, and, if so, add a fudge factor.
2999 SDOperand Lo, Hi;
3000 ExpandOp(Source, Lo, Hi);
3001
Chris Lattner66de05b2005-05-13 04:45:13 +00003002 // If this is unsigned, and not supported, first perform the conversion to
3003 // signed, then adjust the result if the sign bit is set.
3004 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3005 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3006
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003007 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3008 DAG.getConstant(0, Hi.getValueType()),
3009 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003010 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3011 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3012 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003013 uint64_t FF = 0x5f800000ULL;
3014 if (TLI.isLittleEndian()) FF <<= 32;
3015 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003016
Chris Lattner5839bf22005-08-26 17:15:30 +00003017 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003018 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3019 SDOperand FudgeInReg;
3020 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003021 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3022 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003023 else {
3024 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003025 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3026 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003027 }
Chris Lattner473a9902005-09-29 06:44:39 +00003028 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003029 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003030
Chris Lattnera88a2602005-05-14 05:33:54 +00003031 // Check to see if the target has a custom way to lower this. If so, use it.
3032 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3033 default: assert(0 && "This action not implemented for this operation!");
3034 case TargetLowering::Legal:
3035 case TargetLowering::Expand:
3036 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003037 case TargetLowering::Custom: {
3038 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3039 Source), DAG);
3040 if (NV.Val)
3041 return LegalizeOp(NV);
3042 break; // The target decided this was legal after all
3043 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003044 }
3045
Chris Lattner13689e22005-05-12 07:00:44 +00003046 // Expand the source, then glue it back together for the call. We must expand
3047 // the source in case it is shared (this pass of legalize must traverse it).
3048 SDOperand SrcLo, SrcHi;
3049 ExpandOp(Source, SrcLo, SrcHi);
3050 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3051
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003052 SDNode *OutChain = 0;
3053 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3054 DAG.getEntryNode());
3055 const char *FnName = 0;
3056 if (DestTy == MVT::f32)
3057 FnName = "__floatdisf";
3058 else {
3059 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3060 FnName = "__floatdidf";
3061 }
3062
Chris Lattner77e77a62005-01-21 06:05:23 +00003063 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3064
3065 TargetLowering::ArgListTy Args;
3066 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003067
Chris Lattner77e77a62005-01-21 06:05:23 +00003068 Args.push_back(std::make_pair(Source, ArgTy));
3069
3070 // We don't care about token chains for libcalls. We just use the entry
3071 // node as our input and ignore the output chain. This allows us to place
3072 // calls wherever we need them to satisfy data dependences.
3073 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003074
3075 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003076 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3077 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003078
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003079 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003080 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003081}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003082
Chris Lattner22cde6a2006-01-28 08:25:58 +00003083/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3084/// INT_TO_FP operation of the specified operand when the target requests that
3085/// we expand it. At this point, we know that the result and operand types are
3086/// legal for the target.
3087SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3088 SDOperand Op0,
3089 MVT::ValueType DestVT) {
3090 if (Op0.getValueType() == MVT::i32) {
3091 // simple 32-bit [signed|unsigned] integer to float/double expansion
3092
3093 // get the stack frame index of a 8 byte buffer
3094 MachineFunction &MF = DAG.getMachineFunction();
3095 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3096 // get address of 8 byte buffer
3097 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3098 // word offset constant for Hi/Lo address computation
3099 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3100 // set up Hi and Lo (into buffer) address based on endian
3101 SDOperand Hi, Lo;
3102 if (TLI.isLittleEndian()) {
3103 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3104 Lo = StackSlot;
3105 } else {
3106 Hi = StackSlot;
3107 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3108 }
3109 // if signed map to unsigned space
3110 SDOperand Op0Mapped;
3111 if (isSigned) {
3112 // constant used to invert sign bit (signed to unsigned mapping)
3113 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3114 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3115 } else {
3116 Op0Mapped = Op0;
3117 }
3118 // store the lo of the constructed double - based on integer input
3119 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3120 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3121 // initial hi portion of constructed double
3122 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3123 // store the hi of the constructed double - biased exponent
3124 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3125 InitialHi, Hi, DAG.getSrcValue(NULL));
3126 // load the constructed double
3127 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3128 DAG.getSrcValue(NULL));
3129 // FP constant to bias correct the final result
3130 SDOperand Bias = DAG.getConstantFP(isSigned ?
3131 BitsToDouble(0x4330000080000000ULL)
3132 : BitsToDouble(0x4330000000000000ULL),
3133 MVT::f64);
3134 // subtract the bias
3135 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3136 // final result
3137 SDOperand Result;
3138 // handle final rounding
3139 if (DestVT == MVT::f64) {
3140 // do nothing
3141 Result = Sub;
3142 } else {
3143 // if f32 then cast to f32
3144 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3145 }
3146 return Result;
3147 }
3148 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3149 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3150
3151 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3152 DAG.getConstant(0, Op0.getValueType()),
3153 ISD::SETLT);
3154 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3155 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3156 SignSet, Four, Zero);
3157
3158 // If the sign bit of the integer is set, the large number will be treated
3159 // as a negative number. To counteract this, the dynamic code adds an
3160 // offset depending on the data type.
3161 uint64_t FF;
3162 switch (Op0.getValueType()) {
3163 default: assert(0 && "Unsupported integer type!");
3164 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3165 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3166 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3167 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3168 }
3169 if (TLI.isLittleEndian()) FF <<= 32;
3170 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3171
3172 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3173 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3174 SDOperand FudgeInReg;
3175 if (DestVT == MVT::f32)
3176 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3177 DAG.getSrcValue(NULL));
3178 else {
3179 assert(DestVT == MVT::f64 && "Unexpected conversion");
3180 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3181 DAG.getEntryNode(), CPIdx,
3182 DAG.getSrcValue(NULL), MVT::f32));
3183 }
3184
3185 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3186}
3187
3188/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3189/// *INT_TO_FP operation of the specified operand when the target requests that
3190/// we promote it. At this point, we know that the result and operand types are
3191/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3192/// operation that takes a larger input.
3193SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3194 MVT::ValueType DestVT,
3195 bool isSigned) {
3196 // First step, figure out the appropriate *INT_TO_FP operation to use.
3197 MVT::ValueType NewInTy = LegalOp.getValueType();
3198
3199 unsigned OpToUse = 0;
3200
3201 // Scan for the appropriate larger type to use.
3202 while (1) {
3203 NewInTy = (MVT::ValueType)(NewInTy+1);
3204 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3205
3206 // If the target supports SINT_TO_FP of this type, use it.
3207 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3208 default: break;
3209 case TargetLowering::Legal:
3210 if (!TLI.isTypeLegal(NewInTy))
3211 break; // Can't use this datatype.
3212 // FALL THROUGH.
3213 case TargetLowering::Custom:
3214 OpToUse = ISD::SINT_TO_FP;
3215 break;
3216 }
3217 if (OpToUse) break;
3218 if (isSigned) continue;
3219
3220 // If the target supports UINT_TO_FP of this type, use it.
3221 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3222 default: break;
3223 case TargetLowering::Legal:
3224 if (!TLI.isTypeLegal(NewInTy))
3225 break; // Can't use this datatype.
3226 // FALL THROUGH.
3227 case TargetLowering::Custom:
3228 OpToUse = ISD::UINT_TO_FP;
3229 break;
3230 }
3231 if (OpToUse) break;
3232
3233 // Otherwise, try a larger type.
3234 }
3235
3236 // Okay, we found the operation and type to use. Zero extend our input to the
3237 // desired type then run the operation on it.
3238 return DAG.getNode(OpToUse, DestVT,
3239 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3240 NewInTy, LegalOp));
3241}
3242
3243/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3244/// FP_TO_*INT operation of the specified operand when the target requests that
3245/// we promote it. At this point, we know that the result and operand types are
3246/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3247/// operation that returns a larger result.
3248SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3249 MVT::ValueType DestVT,
3250 bool isSigned) {
3251 // First step, figure out the appropriate FP_TO*INT operation to use.
3252 MVT::ValueType NewOutTy = DestVT;
3253
3254 unsigned OpToUse = 0;
3255
3256 // Scan for the appropriate larger type to use.
3257 while (1) {
3258 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3259 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3260
3261 // If the target supports FP_TO_SINT returning this type, use it.
3262 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3263 default: break;
3264 case TargetLowering::Legal:
3265 if (!TLI.isTypeLegal(NewOutTy))
3266 break; // Can't use this datatype.
3267 // FALL THROUGH.
3268 case TargetLowering::Custom:
3269 OpToUse = ISD::FP_TO_SINT;
3270 break;
3271 }
3272 if (OpToUse) break;
3273
3274 // If the target supports FP_TO_UINT of this type, use it.
3275 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3276 default: break;
3277 case TargetLowering::Legal:
3278 if (!TLI.isTypeLegal(NewOutTy))
3279 break; // Can't use this datatype.
3280 // FALL THROUGH.
3281 case TargetLowering::Custom:
3282 OpToUse = ISD::FP_TO_UINT;
3283 break;
3284 }
3285 if (OpToUse) break;
3286
3287 // Otherwise, try a larger type.
3288 }
3289
3290 // Okay, we found the operation and type to use. Truncate the result of the
3291 // extended FP_TO_*INT operation to the desired size.
3292 return DAG.getNode(ISD::TRUNCATE, DestVT,
3293 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3294}
3295
3296/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3297///
3298SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3299 MVT::ValueType VT = Op.getValueType();
3300 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3301 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3302 switch (VT) {
3303 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3304 case MVT::i16:
3305 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3306 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3307 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3308 case MVT::i32:
3309 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3310 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3311 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3312 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3313 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3314 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3315 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3316 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3317 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3318 case MVT::i64:
3319 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3320 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3321 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3322 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3323 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3324 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3325 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3326 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3327 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3328 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3329 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3330 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3331 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3332 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3333 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3334 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3335 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3336 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3337 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3338 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3339 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3340 }
3341}
3342
3343/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3344///
3345SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3346 switch (Opc) {
3347 default: assert(0 && "Cannot expand this yet!");
3348 case ISD::CTPOP: {
3349 static const uint64_t mask[6] = {
3350 0x5555555555555555ULL, 0x3333333333333333ULL,
3351 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3352 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3353 };
3354 MVT::ValueType VT = Op.getValueType();
3355 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3356 unsigned len = getSizeInBits(VT);
3357 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3358 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3359 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3360 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3361 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3362 DAG.getNode(ISD::AND, VT,
3363 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3364 }
3365 return Op;
3366 }
3367 case ISD::CTLZ: {
3368 // for now, we do this:
3369 // x = x | (x >> 1);
3370 // x = x | (x >> 2);
3371 // ...
3372 // x = x | (x >>16);
3373 // x = x | (x >>32); // for 64-bit input
3374 // return popcount(~x);
3375 //
3376 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3377 MVT::ValueType VT = Op.getValueType();
3378 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3379 unsigned len = getSizeInBits(VT);
3380 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3381 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3382 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3383 }
3384 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3385 return DAG.getNode(ISD::CTPOP, VT, Op);
3386 }
3387 case ISD::CTTZ: {
3388 // for now, we use: { return popcount(~x & (x - 1)); }
3389 // unless the target has ctlz but not ctpop, in which case we use:
3390 // { return 32 - nlz(~x & (x-1)); }
3391 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3392 MVT::ValueType VT = Op.getValueType();
3393 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3394 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3395 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3396 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3397 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3398 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3399 TLI.isOperationLegal(ISD::CTLZ, VT))
3400 return DAG.getNode(ISD::SUB, VT,
3401 DAG.getConstant(getSizeInBits(VT), VT),
3402 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3403 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3404 }
3405 }
3406}
Chris Lattnere34b3962005-01-19 04:19:40 +00003407
3408
Chris Lattner3e928bb2005-01-07 07:47:09 +00003409/// ExpandOp - Expand the specified SDOperand into its two component pieces
3410/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3411/// LegalizeNodes map is filled in for any results that are not expanded, the
3412/// ExpandedNodes map is filled in for any results that are expanded, and the
3413/// Lo/Hi values are returned.
3414void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3415 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003416 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003417 SDNode *Node = Op.Val;
3418 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003419 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3420 "Cannot expand FP values!");
3421 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003422 "Cannot expand to FP value or to larger int value!");
3423
Chris Lattner6fdcb252005-09-02 20:32:45 +00003424 // See if we already expanded it.
3425 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3426 = ExpandedNodes.find(Op);
3427 if (I != ExpandedNodes.end()) {
3428 Lo = I->second.first;
3429 Hi = I->second.second;
3430 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003431 }
3432
Chris Lattner3e928bb2005-01-07 07:47:09 +00003433 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003434 case ISD::CopyFromReg:
3435 assert(0 && "CopyFromReg must be legal!");
3436 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003437 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3438 assert(0 && "Do not know how to expand this operator!");
3439 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003440 case ISD::UNDEF:
3441 Lo = DAG.getNode(ISD::UNDEF, NVT);
3442 Hi = DAG.getNode(ISD::UNDEF, NVT);
3443 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003444 case ISD::Constant: {
3445 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3446 Lo = DAG.getConstant(Cst, NVT);
3447 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3448 break;
3449 }
Nate Begemancc827e62005-12-07 19:48:11 +00003450 case ISD::ConstantVec: {
3451 unsigned NumElements = Node->getNumOperands();
3452 // If we only have two elements left in the constant vector, just break it
3453 // apart into the two scalar constants it contains. Otherwise, bisect the
3454 // ConstantVec, and return each half as a new ConstantVec.
3455 // FIXME: this is hard coded as big endian, it may have to change to support
3456 // SSE and Alpha MVI
3457 if (NumElements == 2) {
3458 Hi = Node->getOperand(0);
3459 Lo = Node->getOperand(1);
3460 } else {
3461 NumElements /= 2;
3462 std::vector<SDOperand> LoOps, HiOps;
3463 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3464 HiOps.push_back(Node->getOperand(I));
3465 LoOps.push_back(Node->getOperand(I+NumElements));
3466 }
3467 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3468 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3469 }
3470 break;
3471 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003472
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003473 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003474 // Return the operands.
3475 Lo = Node->getOperand(0);
3476 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003477 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003478
3479 case ISD::SIGN_EXTEND_INREG:
3480 ExpandOp(Node->getOperand(0), Lo, Hi);
3481 // Sign extend the lo-part.
3482 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3483 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3484 TLI.getShiftAmountTy()));
3485 // sext_inreg the low part if needed.
3486 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3487 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003488
Nate Begemand88fc032006-01-14 03:14:10 +00003489 case ISD::BSWAP: {
3490 ExpandOp(Node->getOperand(0), Lo, Hi);
3491 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3492 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3493 Lo = TempLo;
3494 break;
3495 }
3496
Chris Lattneredb1add2005-05-11 04:51:16 +00003497 case ISD::CTPOP:
3498 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003499 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3500 DAG.getNode(ISD::CTPOP, NVT, Lo),
3501 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003502 Hi = DAG.getConstant(0, NVT);
3503 break;
3504
Chris Lattner39a8f332005-05-12 19:05:01 +00003505 case ISD::CTLZ: {
3506 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003507 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003508 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3509 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003510 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3511 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003512 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3513 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3514
3515 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3516 Hi = DAG.getConstant(0, NVT);
3517 break;
3518 }
3519
3520 case ISD::CTTZ: {
3521 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003522 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003523 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3524 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003525 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3526 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003527 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3528 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3529
3530 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3531 Hi = DAG.getConstant(0, NVT);
3532 break;
3533 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003534
Nate Begemanacc398c2006-01-25 18:21:52 +00003535 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003536 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3537 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003538 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3539 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3540
3541 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003542 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003543 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3544 if (!TLI.isLittleEndian())
3545 std::swap(Lo, Hi);
3546 break;
3547 }
3548
Chris Lattner3e928bb2005-01-07 07:47:09 +00003549 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003550 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3551 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003552 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003553
3554 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003555 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003556 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3557 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003558 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003559 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003560
3561 // Build a factor node to remember that this load is independent of the
3562 // other one.
3563 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3564 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003565
Chris Lattner3e928bb2005-01-07 07:47:09 +00003566 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003567 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003568 if (!TLI.isLittleEndian())
3569 std::swap(Lo, Hi);
3570 break;
3571 }
Nate Begemanab48be32005-11-22 18:16:00 +00003572 case ISD::VLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003573 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3574 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanab48be32005-11-22 18:16:00 +00003575 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3576 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3577
3578 // If we only have two elements, turn into a pair of scalar loads.
3579 // FIXME: handle case where a vector of two elements is fine, such as
3580 // 2 x double on SSE2.
3581 if (NumElements == 2) {
3582 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3583 // Increment the pointer to the other half.
3584 unsigned IncrementSize = 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.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3589 } else {
3590 NumElements /= 2; // Split the vector in half
3591 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3592 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3593 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3594 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003595 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003596 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3597 }
3598
3599 // Build a factor node to remember that this load is independent of the
3600 // other one.
3601 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3602 Hi.getValue(1));
3603
3604 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003605 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemanab48be32005-11-22 18:16:00 +00003606 if (!TLI.isLittleEndian())
3607 std::swap(Lo, Hi);
3608 break;
3609 }
3610 case ISD::VADD:
3611 case ISD::VSUB:
3612 case ISD::VMUL: {
3613 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3614 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3615 SDOperand LL, LH, RL, RH;
3616
3617 ExpandOp(Node->getOperand(0), LL, LH);
3618 ExpandOp(Node->getOperand(1), RL, RH);
3619
3620 // If we only have two elements, turn into a pair of scalar loads.
3621 // FIXME: handle case where a vector of two elements is fine, such as
3622 // 2 x double on SSE2.
3623 if (NumElements == 2) {
3624 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3625 Lo = DAG.getNode(Opc, EVT, LL, RL);
3626 Hi = DAG.getNode(Opc, EVT, LH, RH);
3627 } else {
3628 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3629 LL.getOperand(3));
3630 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3631 LH.getOperand(3));
3632 }
3633 break;
3634 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003635 case ISD::AND:
3636 case ISD::OR:
3637 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3638 SDOperand LL, LH, RL, RH;
3639 ExpandOp(Node->getOperand(0), LL, LH);
3640 ExpandOp(Node->getOperand(1), RL, RH);
3641 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3642 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3643 break;
3644 }
3645 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003646 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003647 ExpandOp(Node->getOperand(1), LL, LH);
3648 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003649 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3650 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003651 break;
3652 }
Nate Begeman9373a812005-08-10 20:51:12 +00003653 case ISD::SELECT_CC: {
3654 SDOperand TL, TH, FL, FH;
3655 ExpandOp(Node->getOperand(2), TL, TH);
3656 ExpandOp(Node->getOperand(3), FL, FH);
3657 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3658 Node->getOperand(1), TL, FL, Node->getOperand(4));
3659 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3660 Node->getOperand(1), TH, FH, Node->getOperand(4));
3661 break;
3662 }
Nate Begeman144ff662005-10-13 17:15:37 +00003663 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003664 SDOperand Chain = Node->getOperand(0);
3665 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003666 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3667
3668 if (EVT == NVT)
3669 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3670 else
3671 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3672 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003673
3674 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003675 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003676
Nate Begeman144ff662005-10-13 17:15:37 +00003677 // The high part is obtained by SRA'ing all but one of the bits of the lo
3678 // part.
3679 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3680 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3681 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003682 break;
3683 }
3684 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003685 SDOperand Chain = Node->getOperand(0);
3686 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003687 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3688
3689 if (EVT == NVT)
3690 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3691 else
3692 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3693 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003694
3695 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003696 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003697
Nate Begeman144ff662005-10-13 17:15:37 +00003698 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003699 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003700 break;
3701 }
3702 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003703 SDOperand Chain = Node->getOperand(0);
3704 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003705 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3706
3707 if (EVT == NVT)
3708 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3709 else
3710 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3711 EVT);
3712
3713 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003714 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003715
3716 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003717 Hi = DAG.getNode(ISD::UNDEF, NVT);
3718 break;
3719 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003720 case ISD::ANY_EXTEND:
3721 // The low part is any extension of the input (which degenerates to a copy).
3722 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3723 // The high part is undefined.
3724 Hi = DAG.getNode(ISD::UNDEF, NVT);
3725 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003726 case ISD::SIGN_EXTEND: {
3727 // The low part is just a sign extension of the input (which degenerates to
3728 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003729 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003730
Chris Lattner3e928bb2005-01-07 07:47:09 +00003731 // The high part is obtained by SRA'ing all but one of the bits of the lo
3732 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003733 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003734 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3735 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003736 break;
3737 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003738 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003739 // The low part is just a zero extension of the input (which degenerates to
3740 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003741 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003742
Chris Lattner3e928bb2005-01-07 07:47:09 +00003743 // The high part is just a zero.
3744 Hi = DAG.getConstant(0, NVT);
3745 break;
Chris Lattner35481892005-12-23 00:16:34 +00003746
3747 case ISD::BIT_CONVERT: {
3748 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3749 Node->getOperand(0));
3750 ExpandOp(Tmp, Lo, Hi);
3751 break;
3752 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003753
Chris Lattner8137c9e2006-01-28 05:07:51 +00003754 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003755 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3756 TargetLowering::Custom &&
3757 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003758 Lo = TLI.LowerOperation(Op, DAG);
3759 assert(Lo.Val && "Node must be custom expanded!");
3760 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003761 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003762 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003763 break;
3764
Chris Lattner4e6c7462005-01-08 19:27:05 +00003765 // These operators cannot be expanded directly, emit them as calls to
3766 // library functions.
3767 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003768 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003769 SDOperand Op;
3770 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3771 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003772 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3773 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003774 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003775
Chris Lattnerf20d1832005-07-30 01:40:57 +00003776 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3777
Chris Lattner80a3e942005-07-29 00:33:32 +00003778 // Now that the custom expander is done, expand the result, which is still
3779 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003780 if (Op.Val) {
3781 ExpandOp(Op, Lo, Hi);
3782 break;
3783 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003784 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003785
Chris Lattner4e6c7462005-01-08 19:27:05 +00003786 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003787 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003788 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003789 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003790 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003791
Chris Lattner4e6c7462005-01-08 19:27:05 +00003792 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003793 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003794 SDOperand Op;
3795 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3796 case Expand: assert(0 && "cannot expand FP!");
3797 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3798 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3799 }
3800
3801 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3802
3803 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003804 if (Op.Val) {
3805 ExpandOp(Op, Lo, Hi);
3806 break;
3807 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003808 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003809
Chris Lattner4e6c7462005-01-08 19:27:05 +00003810 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003811 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003812 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003813 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003814 break;
3815
Evan Cheng05a2d562006-01-09 18:31:59 +00003816 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003817 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003818 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003819 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003820 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003821 Op = TLI.LowerOperation(Op, DAG);
3822 if (Op.Val) {
3823 // Now that the custom expander is done, expand the result, which is
3824 // still VT.
3825 ExpandOp(Op, Lo, Hi);
3826 break;
3827 }
3828 }
3829
Chris Lattnere34b3962005-01-19 04:19:40 +00003830 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003831 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003832 break;
Chris Lattner47599822005-04-02 03:38:53 +00003833
3834 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003835 TargetLowering::LegalizeAction Action =
3836 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3837 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3838 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003839 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003840 break;
3841 }
3842
Chris Lattnere34b3962005-01-19 04:19:40 +00003843 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003844 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003845 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003846 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003847
Evan Cheng05a2d562006-01-09 18:31:59 +00003848 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003849 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003850 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003851 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003852 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003853 Op = TLI.LowerOperation(Op, DAG);
3854 if (Op.Val) {
3855 // Now that the custom expander is done, expand the result, which is
3856 // still VT.
3857 ExpandOp(Op, Lo, Hi);
3858 break;
3859 }
3860 }
3861
Chris Lattnere34b3962005-01-19 04:19:40 +00003862 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003863 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003864 break;
Chris Lattner47599822005-04-02 03:38:53 +00003865
3866 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003867 TargetLowering::LegalizeAction Action =
3868 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3869 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3870 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003871 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003872 break;
3873 }
3874
Chris Lattnere34b3962005-01-19 04:19:40 +00003875 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003876 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003877 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003878 }
3879
3880 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003881 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003882 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003883 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003884 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003885 Op = TLI.LowerOperation(Op, DAG);
3886 if (Op.Val) {
3887 // Now that the custom expander is done, expand the result, which is
3888 // still VT.
3889 ExpandOp(Op, Lo, Hi);
3890 break;
3891 }
3892 }
3893
Chris Lattnere34b3962005-01-19 04:19:40 +00003894 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003895 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003896 break;
Chris Lattner47599822005-04-02 03:38:53 +00003897
3898 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003899 TargetLowering::LegalizeAction Action =
3900 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3901 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3902 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003903 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003904 break;
3905 }
3906
Chris Lattnere34b3962005-01-19 04:19:40 +00003907 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003908 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003909 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003910 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003911
Misha Brukmanedf128a2005-04-21 22:36:52 +00003912 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003913 case ISD::SUB: {
3914 // If the target wants to custom expand this, let them.
3915 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3916 TargetLowering::Custom) {
3917 Op = TLI.LowerOperation(Op, DAG);
3918 if (Op.Val) {
3919 ExpandOp(Op, Lo, Hi);
3920 break;
3921 }
3922 }
3923
3924 // Expand the subcomponents.
3925 SDOperand LHSL, LHSH, RHSL, RHSH;
3926 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3927 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3928
3929 std::vector<SDOperand> Ops;
3930 Ops.push_back(LHSL);
3931 Ops.push_back(LHSH);
3932 Ops.push_back(RHSL);
3933 Ops.push_back(RHSH);
3934 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3935 unsigned Opc =
3936 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3937 Lo = DAG.getNode(Opc, VTs, Ops);
3938 Hi = Lo.getValue(1);
Chris Lattner84f67882005-01-20 18:52:28 +00003939 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003940 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003941 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003942 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003943 SDOperand LL, LH, RL, RH;
3944 ExpandOp(Node->getOperand(0), LL, LH);
3945 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003946 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3947 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3948 // extended the sign bit of the low half through the upper half, and if so
3949 // emit a MULHS instead of the alternate sequence that is valid for any
3950 // i64 x i64 multiply.
3951 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3952 // is RH an extension of the sign bit of RL?
3953 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3954 RH.getOperand(1).getOpcode() == ISD::Constant &&
3955 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3956 // is LH an extension of the sign bit of LL?
3957 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3958 LH.getOperand(1).getOpcode() == ISD::Constant &&
3959 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3960 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3961 } else {
3962 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3963 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3964 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3965 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3966 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3967 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003968 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3969 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003970 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00003971 }
3972 break;
3973 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003974 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3975 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3976 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3977 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003978 }
3979
Chris Lattner83397362005-12-21 18:02:52 +00003980 // Make sure the resultant values have been legalized themselves, unless this
3981 // is a type that requires multi-step expansion.
3982 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3983 Lo = LegalizeOp(Lo);
3984 Hi = LegalizeOp(Hi);
3985 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003986
3987 // Remember in a map if the values will be reused later.
3988 bool isNew =
3989 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
3990 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003991}
3992
3993
3994// SelectionDAG::Legalize - This is the entry point for the file.
3995//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003996void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003997 /// run - This is the main entry point to this class.
3998 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00003999 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004000}
4001