blob: 248c7bafd5d4145f40ac80b852701344eff73c81 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.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 Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattner3e928bb2005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner03c85462005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattner8afc48e2005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner03c85462005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
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
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner77e77a62005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145}
146
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattnere1bd8222005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187
Chris Lattnerfa404e82005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000223 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
288
289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
293 MVT::f32);
294 } else {
295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
296 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000297 }
298 break;
299 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
307 Changed = true;
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
310 } else {
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
313 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000314 }
315 if (Changed)
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
317 break;
318 }
319
Chris Lattner3e928bb2005-01-07 07:47:09 +0000320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
327 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
335 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000336 else
337 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000338
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
344
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000345 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000348
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
354 }
355
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000360 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000362 } else {
363 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000364 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000370 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000371 case ISD::BR:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
375 break;
376
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000377 case ISD::BRCOND:
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000379
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
382 case Legal:
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
384 break;
385 case Promote:
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
387 break;
388 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
393 break;
394
Chris Lattner3e928bb2005-01-07 07:47:09 +0000395 case ISD::LOAD:
396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
397 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
398 if (Tmp1 != Node->getOperand(0) ||
399 Tmp2 != Node->getOperand(1))
400 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner8afc48e2005-01-07 22:28:47 +0000401 else
402 Result = SDOperand(Node, 0);
403
404 // Since loads produce two values, make sure to remember that we legalized
405 // both of them.
406 AddLegalizedOperand(SDOperand(Node, 0), Result);
407 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
408 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000409
Chris Lattner0f69b292005-01-15 06:18:18 +0000410 case ISD::EXTLOAD:
411 case ISD::SEXTLOAD:
412 case ISD::ZEXTLOAD:
413 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
414 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
415 if (Tmp1 != Node->getOperand(0) ||
416 Tmp2 != Node->getOperand(1))
417 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
418 cast<MVTSDNode>(Node)->getExtraValueType());
419 else
420 Result = SDOperand(Node, 0);
421
422 // Since loads produce two values, make sure to remember that we legalized
423 // both of them.
424 AddLegalizedOperand(SDOperand(Node, 0), Result);
425 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
426 return Result.getValue(Op.ResNo);
427
Chris Lattner3e928bb2005-01-07 07:47:09 +0000428 case ISD::EXTRACT_ELEMENT:
429 // Get both the low and high parts.
430 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
431 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
432 Result = Tmp2; // 1 -> Hi
433 else
434 Result = Tmp1; // 0 -> Lo
435 break;
436
437 case ISD::CopyToReg:
438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
439
440 switch (getTypeAction(Node->getOperand(1).getValueType())) {
441 case Legal:
442 // Legalize the incoming value (must be legal).
443 Tmp2 = LegalizeOp(Node->getOperand(1));
444 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000445 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000446 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000447 case Promote:
448 Tmp2 = PromoteOp(Node->getOperand(1));
449 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
450 break;
451 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000452 SDOperand Lo, Hi;
453 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000454 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000455 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
456 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
457 // Note that the copytoreg nodes are independent of each other.
458 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000459 assert(isTypeLegal(Result.getValueType()) &&
460 "Cannot expand multiple times yet (i64 -> i16)");
461 break;
462 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463 break;
464
465 case ISD::RET:
466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
467 switch (Node->getNumOperands()) {
468 case 2: // ret val
469 switch (getTypeAction(Node->getOperand(1).getValueType())) {
470 case Legal:
471 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000472 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
474 break;
475 case Expand: {
476 SDOperand Lo, Hi;
477 ExpandOp(Node->getOperand(1), Lo, Hi);
478 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
479 break;
480 }
481 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000482 Tmp2 = PromoteOp(Node->getOperand(1));
483 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
484 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000485 }
486 break;
487 case 1: // ret void
488 if (Tmp1 != Node->getOperand(0))
489 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
490 break;
491 default: { // ret <values>
492 std::vector<SDOperand> NewValues;
493 NewValues.push_back(Tmp1);
494 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
495 switch (getTypeAction(Node->getOperand(i).getValueType())) {
496 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000497 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000498 break;
499 case Expand: {
500 SDOperand Lo, Hi;
501 ExpandOp(Node->getOperand(i), Lo, Hi);
502 NewValues.push_back(Lo);
503 NewValues.push_back(Hi);
504 break;
505 }
506 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000507 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000508 }
509 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
510 break;
511 }
512 }
513 break;
514 case ISD::STORE:
515 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
516 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
517
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000518 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000519 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000520 if (CFP->getValueType(0) == MVT::f32) {
521 union {
522 unsigned I;
523 float F;
524 } V;
525 V.F = CFP->getValue();
526 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
527 DAG.getConstant(V.I, MVT::i32), Tmp2);
528 } else {
529 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
530 union {
531 uint64_t I;
532 double F;
533 } V;
534 V.F = CFP->getValue();
535 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
536 DAG.getConstant(V.I, MVT::i64), Tmp2);
537 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000538 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000539 }
540
Chris Lattner3e928bb2005-01-07 07:47:09 +0000541 switch (getTypeAction(Node->getOperand(1).getValueType())) {
542 case Legal: {
543 SDOperand Val = LegalizeOp(Node->getOperand(1));
544 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
545 Tmp2 != Node->getOperand(2))
546 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
547 break;
548 }
549 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000550 // Truncate the value and store the result.
551 Tmp3 = PromoteOp(Node->getOperand(1));
552 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
553 Node->getOperand(1).getValueType());
554 break;
555
Chris Lattner3e928bb2005-01-07 07:47:09 +0000556 case Expand:
557 SDOperand Lo, Hi;
558 ExpandOp(Node->getOperand(1), Lo, Hi);
559
560 if (!TLI.isLittleEndian())
561 std::swap(Lo, Hi);
562
Chris Lattnerec39a452005-01-19 18:02:17 +0000563 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000564
Chris Lattnerec39a452005-01-19 18:02:17 +0000565 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000566 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
567 getIntPtrConstant(IncrementSize));
568 assert(isTypeLegal(Tmp2.getValueType()) &&
569 "Pointers must be legal!");
Chris Lattnerec39a452005-01-19 18:02:17 +0000570 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
571 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
572 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000573 }
574 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000575 case ISD::PCMARKER:
576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000577 if (Tmp1 != Node->getOperand(0))
578 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000579 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000580 case ISD::TRUNCSTORE:
581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
582 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
583
584 switch (getTypeAction(Node->getOperand(1).getValueType())) {
585 case Legal:
586 Tmp2 = LegalizeOp(Node->getOperand(1));
587 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
588 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000589 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner0f69b292005-01-15 06:18:18 +0000590 cast<MVTSDNode>(Node)->getExtraValueType());
591 break;
592 case Promote:
593 case Expand:
594 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
595 }
596 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000597 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000598 switch (getTypeAction(Node->getOperand(0).getValueType())) {
599 case Expand: assert(0 && "It's impossible to expand bools");
600 case Legal:
601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
602 break;
603 case Promote:
604 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
605 break;
606 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000607 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000608 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000609
610 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
611 default: assert(0 && "This action is not supported yet!");
612 case TargetLowering::Legal:
613 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
614 Tmp3 != Node->getOperand(2))
615 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
616 Tmp1, Tmp2, Tmp3);
617 break;
618 case TargetLowering::Promote: {
619 MVT::ValueType NVT =
620 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
621 unsigned ExtOp, TruncOp;
622 if (MVT::isInteger(Tmp2.getValueType())) {
623 ExtOp = ISD::ZERO_EXTEND;
624 TruncOp = ISD::TRUNCATE;
625 } else {
626 ExtOp = ISD::FP_EXTEND;
627 TruncOp = ISD::FP_ROUND;
628 }
629 // Promote each of the values to the new type.
630 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
631 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
632 // Perform the larger operation, then round down.
633 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
634 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
635 break;
636 }
637 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000638 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000639 case ISD::SETCC:
640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
641 case Legal:
642 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
643 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
644 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
645 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000646 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000647 break;
648 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000649 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
650 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
651
652 // If this is an FP compare, the operands have already been extended.
653 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
654 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000655 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000656
657 // Otherwise, we have to insert explicit sign or zero extends. Note
658 // that we could insert sign extends for ALL conditions, but zero extend
659 // is cheaper on many machines (an AND instead of two shifts), so prefer
660 // it.
661 switch (cast<SetCCSDNode>(Node)->getCondition()) {
662 default: assert(0 && "Unknown integer comparison!");
663 case ISD::SETEQ:
664 case ISD::SETNE:
665 case ISD::SETUGE:
666 case ISD::SETUGT:
667 case ISD::SETULE:
668 case ISD::SETULT:
669 // ALL of these operations will work if we either sign or zero extend
670 // the operands (including the unsigned comparisons!). Zero extend is
671 // usually a simpler/cheaper operation, so prefer it.
672 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
673 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
674 break;
675 case ISD::SETGE:
676 case ISD::SETGT:
677 case ISD::SETLT:
678 case ISD::SETLE:
679 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
680 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
681 break;
682 }
683
684 }
685 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000686 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000687 break;
688 case Expand:
689 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
690 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
691 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
692 switch (cast<SetCCSDNode>(Node)->getCondition()) {
693 case ISD::SETEQ:
694 case ISD::SETNE:
695 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
696 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
697 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000698 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
699 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000700 DAG.getConstant(0, Tmp1.getValueType()));
701 break;
702 default:
703 // FIXME: This generated code sucks.
704 ISD::CondCode LowCC;
705 switch (cast<SetCCSDNode>(Node)->getCondition()) {
706 default: assert(0 && "Unknown integer setcc!");
707 case ISD::SETLT:
708 case ISD::SETULT: LowCC = ISD::SETULT; break;
709 case ISD::SETGT:
710 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
711 case ISD::SETLE:
712 case ISD::SETULE: LowCC = ISD::SETULE; break;
713 case ISD::SETGE:
714 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
715 }
716
717 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
718 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
719 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
720
721 // NOTE: on targets without efficient SELECT of bools, we can always use
722 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000723 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000724 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000725 Node->getValueType(0), LHSHi, RHSHi);
726 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
727 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
728 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000729 break;
730 }
731 }
732 break;
733
Chris Lattnere1bd8222005-01-11 05:57:22 +0000734 case ISD::MEMSET:
735 case ISD::MEMCPY:
736 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000737 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000738 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
739
740 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
741 switch (getTypeAction(Node->getOperand(2).getValueType())) {
742 case Expand: assert(0 && "Cannot expand a byte!");
743 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000744 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000745 break;
746 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000747 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000748 break;
749 }
750 } else {
751 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
752 }
Chris Lattner272455b2005-02-02 03:44:41 +0000753
754 SDOperand Tmp4;
755 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000756 case Expand: assert(0 && "Cannot expand this yet!");
757 case Legal:
758 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000759 break;
760 case Promote:
761 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000762 break;
763 }
764
765 SDOperand Tmp5;
766 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
767 case Expand: assert(0 && "Cannot expand this yet!");
768 case Legal:
769 Tmp5 = LegalizeOp(Node->getOperand(4));
770 break;
771 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000772 Tmp5 = PromoteOp(Node->getOperand(4));
773 break;
774 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000775
776 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
777 default: assert(0 && "This action not implemented for this operation!");
778 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000779 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
780 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
781 Tmp5 != Node->getOperand(4)) {
782 std::vector<SDOperand> Ops;
783 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
784 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
785 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
786 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000787 break;
788 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000789 // Otherwise, the target does not support this operation. Lower the
790 // operation to an explicit libcall as appropriate.
791 MVT::ValueType IntPtr = TLI.getPointerTy();
792 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
793 std::vector<std::pair<SDOperand, const Type*> > Args;
794
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000795 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000796 if (Node->getOpcode() == ISD::MEMSET) {
797 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
798 // Extend the ubyte argument to be an int value for the call.
799 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
800 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
801 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
802
803 FnName = "memset";
804 } else if (Node->getOpcode() == ISD::MEMCPY ||
805 Node->getOpcode() == ISD::MEMMOVE) {
806 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
807 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
808 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
809 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
810 } else {
811 assert(0 && "Unknown op!");
812 }
813 std::pair<SDOperand,SDOperand> CallResult =
Nate Begeman8e21e712005-03-26 01:29:23 +0000814 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000815 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
816 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000817 break;
818 }
819 case TargetLowering::Custom:
820 std::vector<SDOperand> Ops;
821 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
822 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
823 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
824 Result = TLI.LowerOperation(Result);
825 Result = LegalizeOp(Result);
826 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000827 }
828 break;
829 }
Chris Lattner84f67882005-01-20 18:52:28 +0000830 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +0000831 case ISD::SUB_PARTS:
832 case ISD::SHL_PARTS:
833 case ISD::SRA_PARTS:
834 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +0000835 std::vector<SDOperand> Ops;
836 bool Changed = false;
837 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
838 Ops.push_back(LegalizeOp(Node->getOperand(i)));
839 Changed |= Ops.back() != Node->getOperand(i);
840 }
841 if (Changed)
842 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +0000843
844 // Since these produce multiple values, make sure to remember that we
845 // legalized all of them.
846 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
847 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
848 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +0000849 }
Chris Lattner2c8086f2005-04-02 05:00:07 +0000850
851 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +0000852 case ISD::ADD:
853 case ISD::SUB:
854 case ISD::MUL:
855 case ISD::UDIV:
856 case ISD::SDIV:
857 case ISD::UREM:
858 case ISD::SREM:
859 case ISD::AND:
860 case ISD::OR:
861 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000862 case ISD::SHL:
863 case ISD::SRL:
864 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000865 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
866 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
867 if (Tmp1 != Node->getOperand(0) ||
868 Tmp2 != Node->getOperand(1))
869 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
870 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +0000871
872 // Unary operators
873 case ISD::FABS:
874 case ISD::FNEG:
875 Tmp1 = LegalizeOp(Node->getOperand(0));
876 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
877 case TargetLowering::Legal:
878 if (Tmp1 != Node->getOperand(0))
879 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
880 break;
881 case TargetLowering::Promote:
882 case TargetLowering::Custom:
883 assert(0 && "Cannot promote/custom handle this yet!");
884 case TargetLowering::Expand:
885 if (Node->getOpcode() == ISD::FNEG) {
886 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
887 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
888 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
889 Tmp2, Tmp1));
Chris Lattner4af6e0d2005-04-02 05:26:37 +0000890 } else if (Node->getOpcode() == ISD::FABS) {
891 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
892 MVT::ValueType VT = Node->getValueType(0);
893 Tmp2 = DAG.getConstantFP(0.0, VT);
894 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
895 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
896 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
897 Result = LegalizeOp(Result);
Chris Lattner2c8086f2005-04-02 05:00:07 +0000898 } else {
Chris Lattner4af6e0d2005-04-02 05:26:37 +0000899 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +0000900 }
901 break;
902 }
903 break;
904
905 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000906 case ISD::ZERO_EXTEND:
907 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +0000908 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000909 case ISD::FP_EXTEND:
910 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000911 case ISD::FP_TO_SINT:
912 case ISD::FP_TO_UINT:
913 case ISD::SINT_TO_FP:
914 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000915 switch (getTypeAction(Node->getOperand(0).getValueType())) {
916 case Legal:
917 Tmp1 = LegalizeOp(Node->getOperand(0));
918 if (Tmp1 != Node->getOperand(0))
919 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
920 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +0000921 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +0000922 if (Node->getOpcode() == ISD::SINT_TO_FP ||
923 Node->getOpcode() == ISD::UINT_TO_FP) {
924 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
925 Node->getValueType(0), Node->getOperand(0));
926 Result = LegalizeOp(Result);
927 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +0000928 } else if (Node->getOpcode() == ISD::TRUNCATE) {
929 // In the expand case, we must be dealing with a truncate, because
930 // otherwise the result would be larger than the source.
931 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
932
933 // Since the result is legal, we should just be able to truncate the low
934 // part of the source.
935 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
936 break;
Chris Lattner77e77a62005-01-21 06:05:23 +0000937 }
Chris Lattner2c8086f2005-04-02 05:00:07 +0000938 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +0000939
Chris Lattner03c85462005-01-15 05:21:40 +0000940 case Promote:
941 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +0000942 case ISD::ZERO_EXTEND:
943 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +0000944 // NOTE: Any extend would work here...
945 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
946 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner1713e732005-01-16 00:38:00 +0000947 Result, Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000948 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000949 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +0000950 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +0000951 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +0000952 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +0000953 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
954 Result, Node->getOperand(0).getValueType());
955 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000956 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +0000957 Result = PromoteOp(Node->getOperand(0));
958 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
959 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000960 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +0000961 Result = PromoteOp(Node->getOperand(0));
962 if (Result.getValueType() != Op.getValueType())
963 // Dynamically dead while we have only 2 FP types.
964 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
965 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000966 case ISD::FP_ROUND:
967 case ISD::FP_TO_SINT:
968 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000969 Result = PromoteOp(Node->getOperand(0));
970 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
971 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000972 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000973 Result = PromoteOp(Node->getOperand(0));
974 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
975 Result, Node->getOperand(0).getValueType());
976 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
977 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000978 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000979 Result = PromoteOp(Node->getOperand(0));
980 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
981 Result, Node->getOperand(0).getValueType());
982 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
983 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000984 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000985 }
986 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000987 case ISD::FP_ROUND_INREG:
988 case ISD::SIGN_EXTEND_INREG:
Chris Lattner45b8caf2005-01-15 07:15:18 +0000989 case ISD::ZERO_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000990 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +0000991 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
992
993 // If this operation is not supported, convert it to a shl/shr or load/store
994 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000995 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
996 default: assert(0 && "This action not supported for this op yet!");
997 case TargetLowering::Legal:
998 if (Tmp1 != Node->getOperand(0))
999 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1000 ExtraVT);
1001 break;
1002 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001003 // If this is an integer extend and shifts are supported, do that.
1004 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
1005 // NOTE: we could fall back on load/store here too for targets without
1006 // AND. However, it is doubtful that any exist.
1007 // AND out the appropriate bits.
1008 SDOperand Mask =
1009 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
1010 Node->getValueType(0));
1011 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
1012 Node->getOperand(0), Mask);
1013 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
1014 // NOTE: we could fall back on load/store here too for targets without
1015 // SAR. However, it is doubtful that any exist.
1016 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1017 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001018 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001019 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1020 Node->getOperand(0), ShiftCst);
1021 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1022 Result, ShiftCst);
1023 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1024 // The only way we can lower this is to turn it into a STORETRUNC,
1025 // EXTLOAD pair, targetting a temporary location (a stack slot).
1026
1027 // NOTE: there is a choice here between constantly creating new stack
1028 // slots and always reusing the same one. We currently always create
1029 // new ones, as reuse may inhibit scheduling.
1030 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1031 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1032 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1033 MachineFunction &MF = DAG.getMachineFunction();
1034 int SSFI =
1035 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1036 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1037 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1038 Node->getOperand(0), StackSlot, ExtraVT);
1039 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1040 Result, StackSlot, ExtraVT);
1041 } else {
1042 assert(0 && "Unknown op");
1043 }
1044 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001045 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001046 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001047 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001048 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001049 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050
Chris Lattner8afc48e2005-01-07 22:28:47 +00001051 if (!Op.Val->hasOneUse())
1052 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001053
1054 return Result;
1055}
1056
Chris Lattner8b6fa222005-01-15 22:16:26 +00001057/// PromoteOp - Given an operation that produces a value in an invalid type,
1058/// promote it to compute the value into a larger type. The produced value will
1059/// have the correct bits for the low portion of the register, but no guarantee
1060/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001061SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1062 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001063 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001064 assert(getTypeAction(VT) == Promote &&
1065 "Caller should expand or legalize operands that are not promotable!");
1066 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1067 "Cannot promote to smaller type!");
1068
1069 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1070 if (I != PromotedNodes.end()) return I->second;
1071
1072 SDOperand Tmp1, Tmp2, Tmp3;
1073
1074 SDOperand Result;
1075 SDNode *Node = Op.Val;
1076
Chris Lattner0f69b292005-01-15 06:18:18 +00001077 // Promotion needs an optimization step to clean up after it, and is not
1078 // careful to avoid operations the target does not support. Make sure that
1079 // all generated operations are legalized in the next iteration.
1080 NeedsAnotherIteration = true;
1081
Chris Lattner03c85462005-01-15 05:21:40 +00001082 switch (Node->getOpcode()) {
1083 default:
1084 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1085 assert(0 && "Do not know how to promote this operator!");
1086 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001087 case ISD::UNDEF:
1088 Result = DAG.getNode(ISD::UNDEF, NVT);
1089 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001090 case ISD::Constant:
1091 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1092 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1093 break;
1094 case ISD::ConstantFP:
1095 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1096 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1097 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001098 case ISD::CopyFromReg:
1099 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1100 Node->getOperand(0));
1101 // Remember that we legalized the chain.
1102 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1103 break;
1104
Chris Lattner82fbfb62005-01-18 02:59:52 +00001105 case ISD::SETCC:
1106 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1107 "SetCC type is not legal??");
1108 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1109 TLI.getSetCCResultTy(), Node->getOperand(0),
1110 Node->getOperand(1));
1111 Result = LegalizeOp(Result);
1112 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001113
1114 case ISD::TRUNCATE:
1115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1116 case Legal:
1117 Result = LegalizeOp(Node->getOperand(0));
1118 assert(Result.getValueType() >= NVT &&
1119 "This truncation doesn't make sense!");
1120 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1121 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1122 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001123 case Promote:
1124 // The truncation is not required, because we don't guarantee anything
1125 // about high bits anyway.
1126 Result = PromoteOp(Node->getOperand(0));
1127 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001128 case Expand:
1129 assert(0 && "Cannot handle expand yet");
Chris Lattner03c85462005-01-15 05:21:40 +00001130 }
1131 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001132 case ISD::SIGN_EXTEND:
1133 case ISD::ZERO_EXTEND:
1134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1135 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1136 case Legal:
1137 // Input is legal? Just do extend all the way to the larger type.
1138 Result = LegalizeOp(Node->getOperand(0));
1139 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1140 break;
1141 case Promote:
1142 // Promote the reg if it's smaller.
1143 Result = PromoteOp(Node->getOperand(0));
1144 // The high bits are not guaranteed to be anything. Insert an extend.
1145 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001146 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1147 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001148 else
Chris Lattner595dc542005-02-04 18:39:19 +00001149 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1150 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001151 break;
1152 }
1153 break;
1154
1155 case ISD::FP_EXTEND:
1156 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1157 case ISD::FP_ROUND:
1158 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1159 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1160 case Promote: assert(0 && "Unreachable with 2 FP types!");
1161 case Legal:
1162 // Input is legal? Do an FP_ROUND_INREG.
1163 Result = LegalizeOp(Node->getOperand(0));
1164 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1165 break;
1166 }
1167 break;
1168
1169 case ISD::SINT_TO_FP:
1170 case ISD::UINT_TO_FP:
1171 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1172 case Legal:
1173 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001174 // No extra round required here.
1175 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001176 break;
1177
1178 case Promote:
1179 Result = PromoteOp(Node->getOperand(0));
1180 if (Node->getOpcode() == ISD::SINT_TO_FP)
1181 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1182 Result, Node->getOperand(0).getValueType());
1183 else
1184 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1185 Result, Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001186 // No extra round required here.
1187 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001188 break;
1189 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001190 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1191 Node->getOperand(0));
1192 Result = LegalizeOp(Result);
1193
1194 // Round if we cannot tolerate excess precision.
1195 if (NoExcessFPPrecision)
1196 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1197 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001198 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001199 break;
1200
1201 case ISD::FP_TO_SINT:
1202 case ISD::FP_TO_UINT:
1203 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1204 case Legal:
1205 Tmp1 = LegalizeOp(Node->getOperand(0));
1206 break;
1207 case Promote:
1208 // The input result is prerounded, so we don't have to do anything
1209 // special.
1210 Tmp1 = PromoteOp(Node->getOperand(0));
1211 break;
1212 case Expand:
1213 assert(0 && "not implemented");
1214 }
1215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1216 break;
1217
Chris Lattner2c8086f2005-04-02 05:00:07 +00001218 case ISD::FABS:
1219 case ISD::FNEG:
1220 Tmp1 = PromoteOp(Node->getOperand(0));
1221 assert(Tmp1.getValueType() == NVT);
1222 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1223 // NOTE: we do not have to do any extra rounding here for
1224 // NoExcessFPPrecision, because we know the input will have the appropriate
1225 // precision, and these operations don't modify precision at all.
1226 break;
1227
Chris Lattner03c85462005-01-15 05:21:40 +00001228 case ISD::AND:
1229 case ISD::OR:
1230 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001231 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001232 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001233 case ISD::MUL:
1234 // The input may have strange things in the top bits of the registers, but
1235 // these operations don't care. They may have wierd bits going out, but
1236 // that too is okay if they are integer operations.
1237 Tmp1 = PromoteOp(Node->getOperand(0));
1238 Tmp2 = PromoteOp(Node->getOperand(1));
1239 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1240 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1241
1242 // However, if this is a floating point operation, they will give excess
1243 // precision that we may not be able to tolerate. If we DO allow excess
1244 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001245 // FIXME: Why would we need to round FP ops more than integer ones?
1246 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001247 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1248 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1249 break;
1250
Chris Lattner8b6fa222005-01-15 22:16:26 +00001251 case ISD::SDIV:
1252 case ISD::SREM:
1253 // These operators require that their input be sign extended.
1254 Tmp1 = PromoteOp(Node->getOperand(0));
1255 Tmp2 = PromoteOp(Node->getOperand(1));
1256 if (MVT::isInteger(NVT)) {
1257 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001258 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001259 }
1260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1261
1262 // Perform FP_ROUND: this is probably overly pessimistic.
1263 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1264 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1265 break;
1266
1267 case ISD::UDIV:
1268 case ISD::UREM:
1269 // These operators require that their input be zero extended.
1270 Tmp1 = PromoteOp(Node->getOperand(0));
1271 Tmp2 = PromoteOp(Node->getOperand(1));
1272 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1273 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001274 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001275 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1276 break;
1277
1278 case ISD::SHL:
1279 Tmp1 = PromoteOp(Node->getOperand(0));
1280 Tmp2 = LegalizeOp(Node->getOperand(1));
1281 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1282 break;
1283 case ISD::SRA:
1284 // The input value must be properly sign extended.
1285 Tmp1 = PromoteOp(Node->getOperand(0));
1286 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1287 Tmp2 = LegalizeOp(Node->getOperand(1));
1288 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1289 break;
1290 case ISD::SRL:
1291 // The input value must be properly zero extended.
1292 Tmp1 = PromoteOp(Node->getOperand(0));
1293 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1294 Tmp2 = LegalizeOp(Node->getOperand(1));
1295 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1296 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001297 case ISD::LOAD:
1298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1299 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1300 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1301
1302 // Remember that we legalized the chain.
1303 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1304 break;
1305 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001306 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1307 case Expand: assert(0 && "It's impossible to expand bools");
1308 case Legal:
1309 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1310 break;
1311 case Promote:
1312 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1313 break;
1314 }
Chris Lattner03c85462005-01-15 05:21:40 +00001315 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1316 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1317 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1318 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001319 case ISD::CALL: {
1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1321 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1322
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001323 std::vector<SDOperand> Ops;
1324 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1325 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1326
Chris Lattner8ac532c2005-01-16 19:46:48 +00001327 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1328 "Can only promote single result calls");
1329 std::vector<MVT::ValueType> RetTyVTs;
1330 RetTyVTs.reserve(2);
1331 RetTyVTs.push_back(NVT);
1332 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001333 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001334 Result = SDOperand(NC, 0);
1335
1336 // Insert the new chain mapping.
1337 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1338 break;
1339 }
Chris Lattner03c85462005-01-15 05:21:40 +00001340 }
1341
1342 assert(Result.Val && "Didn't set a result!");
1343 AddPromotedOperand(Op, Result);
1344 return Result;
1345}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001346
Chris Lattner84f67882005-01-20 18:52:28 +00001347/// ExpandAddSub - Find a clever way to expand this add operation into
1348/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001349void SelectionDAGLegalize::
1350ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1351 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001352 // Expand the subcomponents.
1353 SDOperand LHSL, LHSH, RHSL, RHSH;
1354 ExpandOp(LHS, LHSL, LHSH);
1355 ExpandOp(RHS, RHSL, RHSH);
1356
1357 // Convert this add to the appropriate ADDC pair. The low part has no carry
1358 // in.
Chris Lattner84f67882005-01-20 18:52:28 +00001359 std::vector<SDOperand> Ops;
1360 Ops.push_back(LHSL);
1361 Ops.push_back(LHSH);
1362 Ops.push_back(RHSL);
1363 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001364 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001365 Hi = Lo.getValue(1);
1366}
1367
Chris Lattner5b359c62005-04-02 04:00:59 +00001368void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1369 SDOperand Op, SDOperand Amt,
1370 SDOperand &Lo, SDOperand &Hi) {
1371 // Expand the subcomponents.
1372 SDOperand LHSL, LHSH;
1373 ExpandOp(Op, LHSL, LHSH);
1374
1375 std::vector<SDOperand> Ops;
1376 Ops.push_back(LHSL);
1377 Ops.push_back(LHSH);
1378 Ops.push_back(Amt);
1379 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1380 Hi = Lo.getValue(1);
1381}
1382
1383
Chris Lattnere34b3962005-01-19 04:19:40 +00001384/// ExpandShift - Try to find a clever way to expand this shift operation out to
1385/// smaller elements. If we can't find a way that is more efficient than a
1386/// libcall on this target, return false. Otherwise, return true with the
1387/// low-parts expanded into Lo and Hi.
1388bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1389 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner47599822005-04-02 03:38:53 +00001390 // FIXME: This code is buggy, disable it for now. Note that we should at
1391 // least handle the case when Amt is an immediate here.
1392 return false;
1393
Chris Lattnere34b3962005-01-19 04:19:40 +00001394 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1395 "This is not a shift!");
1396 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1397
1398 // If we have an efficient select operation (or if the selects will all fold
1399 // away), lower to some complex code, otherwise just emit the libcall.
1400 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1401 !isa<ConstantSDNode>(Amt))
1402 return false;
1403
1404 SDOperand InL, InH;
1405 ExpandOp(Op, InL, InH);
1406 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattnere34b3962005-01-19 04:19:40 +00001407 MVT::ValueType ShTy = ShAmt.getValueType();
1408
1409 unsigned NVTBits = MVT::getSizeInBits(NVT);
1410 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1411 DAG.getConstant(NVTBits, ShTy), ShAmt);
1412
Chris Lattnere5544f82005-01-20 20:29:23 +00001413 // Compare the unmasked shift amount against 32.
1414 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1415 DAG.getConstant(NVTBits, ShTy));
1416
Chris Lattnere34b3962005-01-19 04:19:40 +00001417 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1418 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1419 DAG.getConstant(NVTBits-1, ShTy));
1420 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1421 DAG.getConstant(NVTBits-1, ShTy));
1422 }
1423
1424 if (Opc == ISD::SHL) {
1425 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1426 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1427 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001428 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001429
Chris Lattnere34b3962005-01-19 04:19:40 +00001430 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1431 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1432 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001433 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1434 DAG.getSetCC(ISD::SETEQ,
1435 TLI.getSetCCResultTy(), NAmt,
1436 DAG.getConstant(32, ShTy)),
1437 DAG.getConstant(0, NVT),
1438 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001439 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001440 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001441 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001442 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001443
1444 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001445 if (Opc == ISD::SRA)
1446 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1447 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001448 else
1449 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001450 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001451 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001452 }
1453 return true;
1454}
Chris Lattner77e77a62005-01-21 06:05:23 +00001455
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001456/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1457/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1458/// Found.
1459static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1460 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1461
1462 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1463 // than the Found node. Just remember this node and return.
1464 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1465 Found = Node;
1466 return;
1467 }
1468
1469 // Otherwise, scan the operands of Node to see if any of them is a call.
1470 assert(Node->getNumOperands() != 0 &&
1471 "All leaves should have depth equal to the entry node!");
1472 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1473 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1474
1475 // Tail recurse for the last iteration.
1476 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1477 Found);
1478}
1479
1480
1481/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1482/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1483/// than Found.
1484static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1485 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1486
1487 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1488 // than the Found node. Just remember this node and return.
1489 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1490 Found = Node;
1491 return;
1492 }
1493
1494 // Otherwise, scan the operands of Node to see if any of them is a call.
1495 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1496 if (UI == E) return;
1497 for (--E; UI != E; ++UI)
1498 FindEarliestAdjCallStackUp(*UI, Found);
1499
1500 // Tail recurse for the last iteration.
1501 FindEarliestAdjCallStackUp(*UI, Found);
1502}
1503
1504/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1505/// find the ADJCALLSTACKUP node that terminates the call sequence.
1506static SDNode *FindAdjCallStackUp(SDNode *Node) {
1507 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1508 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00001509 if (Node->use_empty())
1510 return 0; // No adjcallstackup
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001511
1512 if (Node->hasOneUse()) // Simple case, only has one user to check.
1513 return FindAdjCallStackUp(*Node->use_begin());
1514
1515 SDOperand TheChain(Node, Node->getNumValues()-1);
1516 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1517
1518 for (SDNode::use_iterator UI = Node->use_begin(),
1519 E = Node->use_end(); ; ++UI) {
1520 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1521
1522 // Make sure to only follow users of our token chain.
1523 SDNode *User = *UI;
1524 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1525 if (User->getOperand(i) == TheChain)
1526 return FindAdjCallStackUp(User);
1527 }
1528 assert(0 && "Unreachable");
1529 abort();
1530}
1531
1532/// FindInputOutputChains - If we are replacing an operation with a call we need
1533/// to find the call that occurs before and the call that occurs after it to
1534/// properly serialize the calls in the block.
1535static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1536 SDOperand Entry) {
1537 SDNode *LatestAdjCallStackDown = Entry.Val;
1538 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1539 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1540
1541 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1542
1543
1544 SDNode *EarliestAdjCallStackUp = 0;
1545 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1546
1547 if (EarliestAdjCallStackUp) {
1548 //std::cerr << "Found node: ";
1549 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1550 }
1551
1552 return SDOperand(LatestAdjCallStackUp, 0);
1553}
1554
1555
1556
Chris Lattner77e77a62005-01-21 06:05:23 +00001557// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1558// does not fit into a register, return the lo part and set the hi part to the
1559// by-reg argument. If it does fit into a single register, return the result
1560// and leave the Hi part unset.
1561SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1562 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001563 SDNode *OutChain;
1564 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1565 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00001566 if (InChain.Val == 0)
1567 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001568
Chris Lattner77e77a62005-01-21 06:05:23 +00001569 TargetLowering::ArgListTy Args;
1570 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1571 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1572 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1573 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1574 }
1575 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1576
1577 // We don't care about token chains for libcalls. We just use the entry
1578 // node as our input and ignore the output chain. This allows us to place
1579 // calls wherever we need them to satisfy data dependences.
1580 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begeman8e21e712005-03-26 01:29:23 +00001581 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattner77e77a62005-01-21 06:05:23 +00001582 Args, DAG).first;
1583 switch (getTypeAction(Result.getValueType())) {
1584 default: assert(0 && "Unknown thing");
1585 case Legal:
1586 return Result;
1587 case Promote:
1588 assert(0 && "Cannot promote this yet!");
1589 case Expand:
1590 SDOperand Lo;
1591 ExpandOp(Result, Lo, Hi);
1592 return Lo;
1593 }
1594}
1595
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001596
Chris Lattner77e77a62005-01-21 06:05:23 +00001597/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1598/// destination type is legal.
1599SDOperand SelectionDAGLegalize::
1600ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1601 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1602 assert(getTypeAction(Source.getValueType()) == Expand &&
1603 "This is not an expansion!");
1604 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1605
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001606 SDNode *OutChain;
1607 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1608 DAG.getEntryNode());
1609
Chris Lattnerfed55772005-01-23 23:19:44 +00001610 const char *FnName = 0;
Chris Lattner77e77a62005-01-21 06:05:23 +00001611 if (isSigned) {
1612 if (DestTy == MVT::f32)
1613 FnName = "__floatdisf";
1614 else {
1615 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1616 FnName = "__floatdidf";
1617 }
1618 } else {
1619 // If this is unsigned, and not supported, first perform the conversion to
1620 // signed, then adjust the result if the sign bit is set.
1621 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1622
1623 assert(0 && "Unsigned casts not supported yet!");
1624 }
1625 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1626
1627 TargetLowering::ArgListTy Args;
1628 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1629 Args.push_back(std::make_pair(Source, ArgTy));
1630
1631 // We don't care about token chains for libcalls. We just use the entry
1632 // node as our input and ignore the output chain. This allows us to place
1633 // calls wherever we need them to satisfy data dependences.
1634 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begeman8e21e712005-03-26 01:29:23 +00001635 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001636
Chris Lattner77e77a62005-01-21 06:05:23 +00001637}
1638
Chris Lattnere34b3962005-01-19 04:19:40 +00001639
1640
Chris Lattner3e928bb2005-01-07 07:47:09 +00001641/// ExpandOp - Expand the specified SDOperand into its two component pieces
1642/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1643/// LegalizeNodes map is filled in for any results that are not expanded, the
1644/// ExpandedNodes map is filled in for any results that are expanded, and the
1645/// Lo/Hi values are returned.
1646void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1647 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001648 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001649 SDNode *Node = Op.Val;
1650 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1651 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1652 assert(MVT::isInteger(NVT) && NVT < VT &&
1653 "Cannot expand to FP value or to larger int value!");
1654
1655 // If there is more than one use of this, see if we already expanded it.
1656 // There is no use remembering values that only have a single use, as the map
1657 // entries will never be reused.
1658 if (!Node->hasOneUse()) {
1659 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1660 = ExpandedNodes.find(Op);
1661 if (I != ExpandedNodes.end()) {
1662 Lo = I->second.first;
1663 Hi = I->second.second;
1664 return;
1665 }
1666 }
1667
Chris Lattner4e6c7462005-01-08 19:27:05 +00001668 // Expanding to multiple registers needs to perform an optimization step, and
1669 // is not careful to avoid operations the target does not support. Make sure
1670 // that all generated operations are legalized in the next iteration.
1671 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001672
Chris Lattner3e928bb2005-01-07 07:47:09 +00001673 switch (Node->getOpcode()) {
1674 default:
1675 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1676 assert(0 && "Do not know how to expand this operator!");
1677 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001678 case ISD::UNDEF:
1679 Lo = DAG.getNode(ISD::UNDEF, NVT);
1680 Hi = DAG.getNode(ISD::UNDEF, NVT);
1681 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001682 case ISD::Constant: {
1683 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1684 Lo = DAG.getConstant(Cst, NVT);
1685 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1686 break;
1687 }
1688
1689 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00001690 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001691 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00001692 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1693 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1694
1695 // Remember that we legalized the chain.
1696 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1697
Chris Lattner3e928bb2005-01-07 07:47:09 +00001698 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1699 break;
1700 }
1701
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00001702 case ISD::BUILD_PAIR:
1703 // Legalize both operands. FIXME: in the future we should handle the case
1704 // where the two elements are not legal.
1705 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1706 Lo = LegalizeOp(Node->getOperand(0));
1707 Hi = LegalizeOp(Node->getOperand(1));
1708 break;
1709
Chris Lattner3e928bb2005-01-07 07:47:09 +00001710 case ISD::LOAD: {
1711 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1712 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1713 Lo = DAG.getLoad(NVT, Ch, Ptr);
1714
1715 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00001716 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001717 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1718 getIntPtrConstant(IncrementSize));
Chris Lattnerec39a452005-01-19 18:02:17 +00001719 Hi = DAG.getLoad(NVT, Ch, Ptr);
1720
1721 // Build a factor node to remember that this load is independent of the
1722 // other one.
1723 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1724 Hi.getValue(1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001725
1726 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00001727 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001728 if (!TLI.isLittleEndian())
1729 std::swap(Lo, Hi);
1730 break;
1731 }
1732 case ISD::CALL: {
1733 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1734 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1735
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001736 bool Changed = false;
1737 std::vector<SDOperand> Ops;
1738 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1739 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1740 Changed |= Ops.back() != Node->getOperand(i);
1741 }
1742
Chris Lattner3e928bb2005-01-07 07:47:09 +00001743 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1744 "Can only expand a call once so far, not i64 -> i16!");
1745
1746 std::vector<MVT::ValueType> RetTyVTs;
1747 RetTyVTs.reserve(3);
1748 RetTyVTs.push_back(NVT);
1749 RetTyVTs.push_back(NVT);
1750 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001751 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001752 Lo = SDOperand(NC, 0);
1753 Hi = SDOperand(NC, 1);
1754
1755 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00001756 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001757 break;
1758 }
1759 case ISD::AND:
1760 case ISD::OR:
1761 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1762 SDOperand LL, LH, RL, RH;
1763 ExpandOp(Node->getOperand(0), LL, LH);
1764 ExpandOp(Node->getOperand(1), RL, RH);
1765 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1766 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1767 break;
1768 }
1769 case ISD::SELECT: {
1770 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00001771
1772 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1773 case Expand: assert(0 && "It's impossible to expand bools");
1774 case Legal:
1775 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1776 break;
1777 case Promote:
1778 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1779 break;
1780 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001781 ExpandOp(Node->getOperand(1), LL, LH);
1782 ExpandOp(Node->getOperand(2), RL, RH);
1783 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1784 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1785 break;
1786 }
1787 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00001788 SDOperand In;
1789 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1790 case Expand: assert(0 && "expand-expand not implemented yet!");
1791 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1792 case Promote:
1793 In = PromoteOp(Node->getOperand(0));
1794 // Emit the appropriate sign_extend_inreg to get the value we want.
1795 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
1796 Node->getOperand(0).getValueType());
1797 break;
1798 }
1799
Chris Lattner3e928bb2005-01-07 07:47:09 +00001800 // The low part is just a sign extension of the input (which degenerates to
1801 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00001802 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001803
1804 // The high part is obtained by SRA'ing all but one of the bits of the lo
1805 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00001806 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00001807 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1808 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001809 break;
1810 }
Chris Lattner06098e02005-04-03 23:41:52 +00001811 case ISD::ZERO_EXTEND: {
1812 SDOperand In;
1813 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1814 case Expand: assert(0 && "expand-expand not implemented yet!");
1815 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1816 case Promote:
1817 In = PromoteOp(Node->getOperand(0));
1818 // Emit the appropriate zero_extend_inreg to get the value we want.
1819 In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In,
1820 Node->getOperand(0).getValueType());
1821 break;
1822 }
1823
Chris Lattner3e928bb2005-01-07 07:47:09 +00001824 // The low part is just a zero extension of the input (which degenerates to
1825 // a copy).
1826 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1827
1828 // The high part is just a zero.
1829 Hi = DAG.getConstant(0, NVT);
1830 break;
Chris Lattner06098e02005-04-03 23:41:52 +00001831 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00001832 // These operators cannot be expanded directly, emit them as calls to
1833 // library functions.
1834 case ISD::FP_TO_SINT:
1835 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00001836 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001837 else
Chris Lattner77e77a62005-01-21 06:05:23 +00001838 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001839 break;
1840 case ISD::FP_TO_UINT:
1841 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00001842 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001843 else
Chris Lattner77e77a62005-01-21 06:05:23 +00001844 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001845 break;
1846
Chris Lattnere34b3962005-01-19 04:19:40 +00001847 case ISD::SHL:
1848 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001849 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001850 break;
Chris Lattner47599822005-04-02 03:38:53 +00001851
1852 // If this target supports SHL_PARTS, use it.
1853 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00001854 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
1855 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00001856 break;
1857 }
1858
Chris Lattnere34b3962005-01-19 04:19:40 +00001859 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001860 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001861 break;
1862
1863 case ISD::SRA:
1864 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001865 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001866 break;
Chris Lattner47599822005-04-02 03:38:53 +00001867
1868 // If this target supports SRA_PARTS, use it.
1869 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00001870 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
1871 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00001872 break;
1873 }
1874
Chris Lattnere34b3962005-01-19 04:19:40 +00001875 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001876 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001877 break;
1878 case ISD::SRL:
1879 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001880 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001881 break;
Chris Lattner47599822005-04-02 03:38:53 +00001882
1883 // If this target supports SRL_PARTS, use it.
1884 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00001885 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
1886 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00001887 break;
1888 }
1889
Chris Lattnere34b3962005-01-19 04:19:40 +00001890 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001891 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001892 break;
1893
Chris Lattner47599822005-04-02 03:38:53 +00001894 case ISD::ADD:
1895 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
1896 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00001897 break;
1898 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00001899 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
1900 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00001901 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001902 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1903 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1904 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1905 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1906 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001907 }
1908
1909 // Remember in a map if the values will be reused later.
1910 if (!Node->hasOneUse()) {
1911 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1912 std::make_pair(Lo, Hi))).second;
1913 assert(isNew && "Value already expanded?!?");
1914 }
1915}
1916
1917
1918// SelectionDAG::Legalize - This is the entry point for the file.
1919//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001920void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001921 /// run - This is the main entry point to this class.
1922 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001923 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001924}
1925