blob: 9312a5c87d7f9a58ea6a3f16faca0744ae1a6766 [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"
17#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000018#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000019#include "llvm/Target/TargetOptions.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/Constants.h"
21#include <iostream>
22using namespace llvm;
23
Chris Lattner4e6c7462005-01-08 19:27:05 +000024static const Type *getTypeFor(MVT::ValueType VT) {
25 switch (VT) {
26 default: assert(0 && "Unknown MVT!");
27 case MVT::i1: return Type::BoolTy;
28 case MVT::i8: return Type::UByteTy;
29 case MVT::i16: return Type::UShortTy;
30 case MVT::i32: return Type::UIntTy;
31 case MVT::i64: return Type::ULongTy;
32 case MVT::f32: return Type::FloatTy;
33 case MVT::f64: return Type::DoubleTy;
34 }
35}
36
37
Chris Lattner3e928bb2005-01-07 07:47:09 +000038//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
51class SelectionDAGLegalize {
52 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
55 /// LegalizeAction - This enum indicates what action we should take for each
56 /// value type the can occur in the program.
57 enum LegalizeAction {
58 Legal, // The target natively supports this value type.
59 Promote, // This should be promoted to the next larger type.
60 Expand, // This integer type should be broken into smaller pieces.
61 };
62
63 /// TransformToType - For any value types we are promoting or expanding, this
64 /// contains the value type that we are changing to. For Expanded types, this
65 /// contains one step of the expand (e.g. i64 -> i32), even if there are
66 /// multiple steps required (e.g. i64 -> i16)
67 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
68
69 /// ValueTypeActions - This is a bitvector that contains two bits for each
70 /// value type, where the two bits correspond to the LegalizeAction enum.
71 /// This can be queried with "getTypeAction(VT)".
72 unsigned ValueTypeActions;
73
74 /// NeedsAnotherIteration - This is set when we expand a large integer
75 /// operation into smaller integer operations, but the smaller operations are
76 /// not set. This occurs only rarely in practice, for targets that don't have
77 /// 32-bit or larger integer registers.
78 bool NeedsAnotherIteration;
79
80 /// LegalizedNodes - For nodes that are of legal width, and that have more
81 /// than one use, this map indicates what regularized operand to use. This
82 /// allows us to avoid legalizing the same thing more than once.
83 std::map<SDOperand, SDOperand> LegalizedNodes;
84
Chris Lattner03c85462005-01-15 05:21:40 +000085 /// PromotedNodes - For nodes that are below legal width, and that have more
86 /// than one use, this map indicates what promoted value to use. This allows
87 /// us to avoid promoting the same thing more than once.
88 std::map<SDOperand, SDOperand> PromotedNodes;
89
Chris Lattner3e928bb2005-01-07 07:47:09 +000090 /// ExpandedNodes - For nodes that need to be expanded, and which have more
91 /// than one use, this map indicates which which operands are the expanded
92 /// version of the input. This allows us to avoid expanding the same node
93 /// more than once.
94 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
95
Chris Lattner8afc48e2005-01-07 22:28:47 +000096 void AddLegalizedOperand(SDOperand From, SDOperand To) {
97 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
98 assert(isNew && "Got into the map somehow?");
99 }
Chris Lattner03c85462005-01-15 05:21:40 +0000100 void AddPromotedOperand(SDOperand From, SDOperand To) {
101 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
102 assert(isNew && "Got into the map somehow?");
103 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105 /// setValueTypeAction - Set the action for a particular value type. This
106 /// assumes an action has not already been set for this value type.
107 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
108 ValueTypeActions |= A << (VT*2);
109 if (A == Promote) {
110 MVT::ValueType PromoteTo;
111 if (VT == MVT::f32)
112 PromoteTo = MVT::f64;
113 else {
114 unsigned LargerReg = VT+1;
115 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
116 ++LargerReg;
117 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
118 "Nothing to promote to??");
119 }
120 PromoteTo = (MVT::ValueType)LargerReg;
121 }
122
123 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
124 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
125 "Can only promote from int->int or fp->fp!");
126 assert(VT < PromoteTo && "Must promote to a larger type!");
127 TransformToType[VT] = PromoteTo;
128 } else if (A == Expand) {
129 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
130 "Cannot expand this type: target must support SOME integer reg!");
131 // Expand to the next smaller integer type!
132 TransformToType[VT] = (MVT::ValueType)(VT-1);
133 }
134 }
135
136public:
137
138 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
139
140 /// Run - While there is still lowering to do, perform a pass over the DAG.
141 /// Most regularization can be done in a single pass, but targets that require
142 /// large values to be split into registers multiple times (e.g. i64 -> 4x
143 /// i16) require iteration for these values (the first iteration will demote
144 /// to i32, the second will demote to i16).
145 void Run() {
146 do {
147 NeedsAnotherIteration = false;
148 LegalizeDAG();
149 } while (NeedsAnotherIteration);
150 }
151
152 /// getTypeAction - Return how we should legalize values of this type, either
153 /// it is already legal or we need to expand it into multiple registers of
154 /// smaller integer type, or we need to promote it to a larger type.
155 LegalizeAction getTypeAction(MVT::ValueType VT) const {
156 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
157 }
158
159 /// isTypeLegal - Return true if this type is legal on this target.
160 ///
161 bool isTypeLegal(MVT::ValueType VT) const {
162 return getTypeAction(VT) == Legal;
163 }
164
165private:
166 void LegalizeDAG();
167
168 SDOperand LegalizeOp(SDOperand O);
169 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000170 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000171
172 SDOperand getIntPtrConstant(uint64_t Val) {
173 return DAG.getConstant(Val, TLI.getPointerTy());
174 }
175};
176}
177
178
179SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
180 SelectionDAG &dag)
181 : TLI(tli), DAG(dag), ValueTypeActions(0) {
182
183 assert(MVT::LAST_VALUETYPE <= 16 &&
184 "Too many value types for ValueTypeActions to hold!");
185
186 // Inspect all of the ValueType's possible, deciding how to process them.
187 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
188 // If TLI says we are expanding this type, expand it!
189 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
190 setValueTypeAction((MVT::ValueType)IntReg, Expand);
191 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
192 // Otherwise, if we don't have native support, we must promote to a
193 // larger type.
194 setValueTypeAction((MVT::ValueType)IntReg, Promote);
195
196 // If the target does not have native support for F32, promote it to F64.
197 if (!TLI.hasNativeSupportFor(MVT::f32))
198 setValueTypeAction(MVT::f32, Promote);
199}
200
Chris Lattner3e928bb2005-01-07 07:47:09 +0000201void SelectionDAGLegalize::LegalizeDAG() {
202 SDOperand OldRoot = DAG.getRoot();
203 SDOperand NewRoot = LegalizeOp(OldRoot);
204 DAG.setRoot(NewRoot);
205
206 ExpandedNodes.clear();
207 LegalizedNodes.clear();
208
209 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000210 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211}
212
213SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000214 assert(getTypeAction(Op.getValueType()) == Legal &&
215 "Caller should expand or promote operands that are not legal!");
216
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000218 // register on this target, make sure to expand or promote them.
219 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000220 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
221 switch (getTypeAction(Op.Val->getValueType(i))) {
222 case Legal: break; // Nothing to do.
223 case Expand: {
224 SDOperand T1, T2;
225 ExpandOp(Op.getValue(i), T1, T2);
226 assert(LegalizedNodes.count(Op) &&
227 "Expansion didn't add legal operands!");
228 return LegalizedNodes[Op];
229 }
230 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000231 PromoteOp(Op.getValue(i));
232 assert(LegalizedNodes.count(Op) &&
233 "Expansion didn't add legal operands!");
234 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000235 }
236 }
237
Chris Lattnere1bd8222005-01-11 05:57:22 +0000238 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
239 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000240
Chris Lattnerfa404e82005-01-09 19:03:49 +0000241 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242
243 SDOperand Result = Op;
244 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245
246 switch (Node->getOpcode()) {
247 default:
248 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
249 assert(0 && "Do not know how to legalize this operator!");
250 abort();
251 case ISD::EntryToken:
252 case ISD::FrameIndex:
253 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000254 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000255 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000256 assert(getTypeAction(Node->getValueType(0)) == Legal &&
257 "This must be legal!");
258 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000259 case ISD::CopyFromReg:
260 Tmp1 = LegalizeOp(Node->getOperand(0));
261 if (Tmp1 != Node->getOperand(0))
262 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
263 Node->getValueType(0), Tmp1);
264 break;
Chris Lattner18c2f132005-01-13 20:50:02 +0000265 case ISD::ImplicitDef:
266 Tmp1 = LegalizeOp(Node->getOperand(0));
267 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000268 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000269 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270 case ISD::Constant:
271 // We know we don't need to expand constants here, constants only have one
272 // value and we check that it is fine above.
273
274 // FIXME: Maybe we should handle things like targets that don't support full
275 // 32-bit immediates?
276 break;
277 case ISD::ConstantFP: {
278 // Spill FP immediates to the constant pool if the target cannot directly
279 // codegen them. Targets often have some immediate values that can be
280 // efficiently generated into an FP register without a load. We explicitly
281 // leave these constants as ConstantFP nodes for the target to deal with.
282
283 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
284
285 // Check to see if this FP immediate is already legal.
286 bool isLegal = false;
287 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
288 E = TLI.legal_fpimm_end(); I != E; ++I)
289 if (CFP->isExactlyValue(*I)) {
290 isLegal = true;
291 break;
292 }
293
294 if (!isLegal) {
295 // Otherwise we need to spill the constant to memory.
296 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
297
298 bool Extend = false;
299
300 // If a FP immediate is precise when represented as a float, we put it
301 // into the constant pool as a float, even if it's is statically typed
302 // as a double.
303 MVT::ValueType VT = CFP->getValueType(0);
304 bool isDouble = VT == MVT::f64;
305 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
306 Type::FloatTy, CFP->getValue());
307 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
308 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
309 VT = MVT::f32;
310 Extend = true;
311 }
312
313 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
314 TLI.getPointerTy());
315 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
316
317 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
318 }
319 break;
320 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000321 case ISD::TokenFactor: {
322 std::vector<SDOperand> Ops;
323 bool Changed = false;
324 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
325 Ops.push_back(LegalizeOp(Node->getOperand(i))); // Legalize the operands
326 Changed |= Ops[i] != Node->getOperand(i);
327 }
328 if (Changed)
329 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
330 break;
331 }
332
Chris Lattner3e928bb2005-01-07 07:47:09 +0000333 case ISD::ADJCALLSTACKDOWN:
334 case ISD::ADJCALLSTACKUP:
335 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
336 // There is no need to legalize the size argument (Operand #1)
337 if (Tmp1 != Node->getOperand(0))
338 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
339 Node->getOperand(1));
340 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000341 case ISD::DYNAMIC_STACKALLOC:
342 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
343 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
344 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
345 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
346 Tmp3 != Node->getOperand(2))
347 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
348 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000349 else
350 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000351
352 // Since this op produces two values, make sure to remember that we
353 // legalized both of them.
354 AddLegalizedOperand(SDOperand(Node, 0), Result);
355 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
356 return Result.getValue(Op.ResNo);
357
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358 case ISD::CALL:
359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
360 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfad71eb2005-01-07 21:35:32 +0000361 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000362 std::vector<MVT::ValueType> RetTyVTs;
363 RetTyVTs.reserve(Node->getNumValues());
364 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000365 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner38d6be52005-01-09 19:43:23 +0000366 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0);
367 } else {
368 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000369 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000370 // Since calls produce multiple values, make sure to remember that we
371 // legalized all of them.
372 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
373 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
374 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000375
Chris Lattnerc7af1792005-01-07 22:12:08 +0000376 case ISD::BR:
377 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
378 if (Tmp1 != Node->getOperand(0))
379 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
380 break;
381
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000382 case ISD::BRCOND:
383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
384 // FIXME: booleans might not be legal!
385 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
386 // Basic block destination (Op#2) is always legal.
387 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
388 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
389 Node->getOperand(2));
390 break;
391
Chris Lattner3e928bb2005-01-07 07:47:09 +0000392 case ISD::LOAD:
393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
395 if (Tmp1 != Node->getOperand(0) ||
396 Tmp2 != Node->getOperand(1))
397 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner8afc48e2005-01-07 22:28:47 +0000398 else
399 Result = SDOperand(Node, 0);
400
401 // Since loads produce two values, make sure to remember that we legalized
402 // both of them.
403 AddLegalizedOperand(SDOperand(Node, 0), Result);
404 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
405 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000406
Chris Lattner0f69b292005-01-15 06:18:18 +0000407 case ISD::EXTLOAD:
408 case ISD::SEXTLOAD:
409 case ISD::ZEXTLOAD:
410 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
411 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
412 if (Tmp1 != Node->getOperand(0) ||
413 Tmp2 != Node->getOperand(1))
414 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
415 cast<MVTSDNode>(Node)->getExtraValueType());
416 else
417 Result = SDOperand(Node, 0);
418
419 // Since loads produce two values, make sure to remember that we legalized
420 // both of them.
421 AddLegalizedOperand(SDOperand(Node, 0), Result);
422 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
423 return Result.getValue(Op.ResNo);
424
Chris Lattner3e928bb2005-01-07 07:47:09 +0000425 case ISD::EXTRACT_ELEMENT:
426 // Get both the low and high parts.
427 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
428 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
429 Result = Tmp2; // 1 -> Hi
430 else
431 Result = Tmp1; // 0 -> Lo
432 break;
433
434 case ISD::CopyToReg:
435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
436
437 switch (getTypeAction(Node->getOperand(1).getValueType())) {
438 case Legal:
439 // Legalize the incoming value (must be legal).
440 Tmp2 = LegalizeOp(Node->getOperand(1));
441 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000442 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000443 break;
444 case Expand: {
445 SDOperand Lo, Hi;
446 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000447 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000448 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
449 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
450 assert(isTypeLegal(Result.getValueType()) &&
451 "Cannot expand multiple times yet (i64 -> i16)");
452 break;
453 }
454 case Promote:
455 assert(0 && "Don't know what it means to promote this!");
456 abort();
457 }
458 break;
459
460 case ISD::RET:
461 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
462 switch (Node->getNumOperands()) {
463 case 2: // ret val
464 switch (getTypeAction(Node->getOperand(1).getValueType())) {
465 case Legal:
466 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000467 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000468 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
469 break;
470 case Expand: {
471 SDOperand Lo, Hi;
472 ExpandOp(Node->getOperand(1), Lo, Hi);
473 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
474 break;
475 }
476 case Promote:
477 assert(0 && "Can't promote return value!");
478 }
479 break;
480 case 1: // ret void
481 if (Tmp1 != Node->getOperand(0))
482 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
483 break;
484 default: { // ret <values>
485 std::vector<SDOperand> NewValues;
486 NewValues.push_back(Tmp1);
487 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
488 switch (getTypeAction(Node->getOperand(i).getValueType())) {
489 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000490 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491 break;
492 case Expand: {
493 SDOperand Lo, Hi;
494 ExpandOp(Node->getOperand(i), Lo, Hi);
495 NewValues.push_back(Lo);
496 NewValues.push_back(Hi);
497 break;
498 }
499 case Promote:
500 assert(0 && "Can't promote return value!");
501 }
502 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
503 break;
504 }
505 }
506 break;
507 case ISD::STORE:
508 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
509 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
510
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000511 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000512 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000513 if (CFP->getValueType(0) == MVT::f32) {
514 union {
515 unsigned I;
516 float F;
517 } V;
518 V.F = CFP->getValue();
519 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
520 DAG.getConstant(V.I, MVT::i32), Tmp2);
521 } else {
522 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
523 union {
524 uint64_t I;
525 double F;
526 } V;
527 V.F = CFP->getValue();
528 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
529 DAG.getConstant(V.I, MVT::i64), Tmp2);
530 }
531 Op = Result;
532 Node = Op.Val;
533 }
534
Chris Lattner3e928bb2005-01-07 07:47:09 +0000535 switch (getTypeAction(Node->getOperand(1).getValueType())) {
536 case Legal: {
537 SDOperand Val = LegalizeOp(Node->getOperand(1));
538 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
539 Tmp2 != Node->getOperand(2))
540 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
541 break;
542 }
543 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000544 // Truncate the value and store the result.
545 Tmp3 = PromoteOp(Node->getOperand(1));
546 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
547 Node->getOperand(1).getValueType());
548 break;
549
Chris Lattner3e928bb2005-01-07 07:47:09 +0000550 case Expand:
551 SDOperand Lo, Hi;
552 ExpandOp(Node->getOperand(1), Lo, Hi);
553
554 if (!TLI.isLittleEndian())
555 std::swap(Lo, Hi);
556
557 // FIXME: These two stores are independent of each other!
558 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
559
Chris Lattner38d6be52005-01-09 19:43:23 +0000560 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000561 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
562 getIntPtrConstant(IncrementSize));
563 assert(isTypeLegal(Tmp2.getValueType()) &&
564 "Pointers must be legal!");
565 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
566 }
567 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000568 case ISD::TRUNCSTORE:
569 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
570 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
571
572 switch (getTypeAction(Node->getOperand(1).getValueType())) {
573 case Legal:
574 Tmp2 = LegalizeOp(Node->getOperand(1));
575 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
576 Tmp3 != Node->getOperand(2))
577 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
578 cast<MVTSDNode>(Node)->getExtraValueType());
579 break;
580 case Promote:
581 case Expand:
582 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
583 }
584 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000585 case ISD::SELECT:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000586 // FIXME: BOOLS MAY REQUIRE PROMOTION!
587 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
588 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000589 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
590
591 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner3e928bb2005-01-07 07:47:09 +0000592 Tmp3 != Node->getOperand(2))
593 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
594 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000595 case ISD::SETCC:
596 switch (getTypeAction(Node->getOperand(0).getValueType())) {
597 case Legal:
598 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
599 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
600 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
601 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
602 Tmp1, Tmp2);
603 break;
604 case Promote:
605 assert(0 && "Can't promote setcc operands yet!");
606 break;
607 case Expand:
608 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
609 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
610 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
611 switch (cast<SetCCSDNode>(Node)->getCondition()) {
612 case ISD::SETEQ:
613 case ISD::SETNE:
614 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
615 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
616 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
617 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
618 DAG.getConstant(0, Tmp1.getValueType()));
619 break;
620 default:
621 // FIXME: This generated code sucks.
622 ISD::CondCode LowCC;
623 switch (cast<SetCCSDNode>(Node)->getCondition()) {
624 default: assert(0 && "Unknown integer setcc!");
625 case ISD::SETLT:
626 case ISD::SETULT: LowCC = ISD::SETULT; break;
627 case ISD::SETGT:
628 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
629 case ISD::SETLE:
630 case ISD::SETULE: LowCC = ISD::SETULE; break;
631 case ISD::SETGE:
632 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
633 }
634
635 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
636 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
637 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
638
639 // NOTE: on targets without efficient SELECT of bools, we can always use
640 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
641 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
642 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
643 LHSHi, RHSHi);
644 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
645 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
646 break;
647 }
648 }
649 break;
650
Chris Lattnere1bd8222005-01-11 05:57:22 +0000651 case ISD::MEMSET:
652 case ISD::MEMCPY:
653 case ISD::MEMMOVE: {
654 Tmp1 = LegalizeOp(Node->getOperand(0));
655 Tmp2 = LegalizeOp(Node->getOperand(1));
656 Tmp3 = LegalizeOp(Node->getOperand(2));
657 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
658 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
659 if (TLI.isOperationSupported(Node->getOpcode(), MVT::Other)) {
660 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
661 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
662 Tmp5 != Node->getOperand(4)) {
663 std::vector<SDOperand> Ops;
664 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
665 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
666 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
667 }
668 } else {
669 // Otherwise, the target does not support this operation. Lower the
670 // operation to an explicit libcall as appropriate.
671 MVT::ValueType IntPtr = TLI.getPointerTy();
672 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
673 std::vector<std::pair<SDOperand, const Type*> > Args;
674
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000675 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000676 if (Node->getOpcode() == ISD::MEMSET) {
677 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
678 // Extend the ubyte argument to be an int value for the call.
679 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
680 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
681 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
682
683 FnName = "memset";
684 } else if (Node->getOpcode() == ISD::MEMCPY ||
685 Node->getOpcode() == ISD::MEMMOVE) {
686 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
687 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
688 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
689 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
690 } else {
691 assert(0 && "Unknown op!");
692 }
693 std::pair<SDOperand,SDOperand> CallResult =
694 TLI.LowerCallTo(Tmp1, Type::VoidTy,
695 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
696 Result = LegalizeOp(CallResult.second);
697 }
698 break;
699 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000700 case ISD::ADD:
701 case ISD::SUB:
702 case ISD::MUL:
703 case ISD::UDIV:
704 case ISD::SDIV:
705 case ISD::UREM:
706 case ISD::SREM:
707 case ISD::AND:
708 case ISD::OR:
709 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000710 case ISD::SHL:
711 case ISD::SRL:
712 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000713 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
714 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
715 if (Tmp1 != Node->getOperand(0) ||
716 Tmp2 != Node->getOperand(1))
717 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
718 break;
719 case ISD::ZERO_EXTEND:
720 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +0000721 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000722 case ISD::FP_EXTEND:
723 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000724 case ISD::FP_TO_SINT:
725 case ISD::FP_TO_UINT:
726 case ISD::SINT_TO_FP:
727 case ISD::UINT_TO_FP:
728
Chris Lattner3e928bb2005-01-07 07:47:09 +0000729 switch (getTypeAction(Node->getOperand(0).getValueType())) {
730 case Legal:
731 Tmp1 = LegalizeOp(Node->getOperand(0));
732 if (Tmp1 != Node->getOperand(0))
733 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
734 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +0000735 case Expand:
Chris Lattnera385e9b2005-01-13 17:59:25 +0000736 assert(Node->getOpcode() != ISD::SINT_TO_FP &&
737 Node->getOpcode() != ISD::UINT_TO_FP &&
738 "Cannot lower Xint_to_fp to a call yet!");
739
Chris Lattnerb00a6422005-01-07 22:37:48 +0000740 // In the expand case, we must be dealing with a truncate, because
741 // otherwise the result would be larger than the source.
742 assert(Node->getOpcode() == ISD::TRUNCATE &&
743 "Shouldn't need to expand other operators here!");
744 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
745
746 // Since the result is legal, we should just be able to truncate the low
747 // part of the source.
748 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
749 break;
750
Chris Lattner03c85462005-01-15 05:21:40 +0000751 case Promote:
752 switch (Node->getOpcode()) {
753 case ISD::ZERO_EXTEND: {
754 // Mask out the high bits.
755 uint64_t MaskCst =
756 1ULL << (MVT::getSizeInBits(Node->getOperand(0).getValueType()))-1;
757 Tmp1 = PromoteOp(Node->getOperand(0));
758 Result = DAG.getNode(ISD::AND, Node->getValueType(0), Tmp1,
759 DAG.getConstant(MaskCst, Node->getValueType(0)));
760 break;
761 }
762 case ISD::SIGN_EXTEND:
763 case ISD::TRUNCATE:
764 case ISD::FP_EXTEND:
765 case ISD::FP_ROUND:
766 case ISD::FP_TO_SINT:
767 case ISD::FP_TO_UINT:
768 case ISD::SINT_TO_FP:
769 case ISD::UINT_TO_FP:
770 Node->dump();
771 assert(0 && "Do not know how to promote this yet!");
772 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 }
774 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000775 case ISD::FP_ROUND_INREG:
776 case ISD::SIGN_EXTEND_INREG:
777 case ISD::ZERO_EXTEND_INREG:
778 Tmp1 = LegalizeOp(Node->getOperand(0));
779 if (Tmp1 != Node->getOperand(0))
780 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
781 cast<MVTSDNode>(Node)->getExtraValueType());
782 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783 }
784
Chris Lattner8afc48e2005-01-07 22:28:47 +0000785 if (!Op.Val->hasOneUse())
786 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787
788 return Result;
789}
790
Chris Lattner03c85462005-01-15 05:21:40 +0000791SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
792 MVT::ValueType VT = Op.getValueType();
793 MVT::ValueType NVT = TransformToType[VT];
794 assert(getTypeAction(VT) == Promote &&
795 "Caller should expand or legalize operands that are not promotable!");
796 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
797 "Cannot promote to smaller type!");
798
799 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
800 if (I != PromotedNodes.end()) return I->second;
801
802 SDOperand Tmp1, Tmp2, Tmp3;
803
804 SDOperand Result;
805 SDNode *Node = Op.Val;
806
Chris Lattner0f69b292005-01-15 06:18:18 +0000807 // Promotion needs an optimization step to clean up after it, and is not
808 // careful to avoid operations the target does not support. Make sure that
809 // all generated operations are legalized in the next iteration.
810 NeedsAnotherIteration = true;
811
Chris Lattner03c85462005-01-15 05:21:40 +0000812 switch (Node->getOpcode()) {
813 default:
814 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
815 assert(0 && "Do not know how to promote this operator!");
816 abort();
817 case ISD::Constant:
818 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
819 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
820 break;
821 case ISD::ConstantFP:
822 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
823 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
824 break;
825
826 case ISD::TRUNCATE:
827 switch (getTypeAction(Node->getOperand(0).getValueType())) {
828 case Legal:
829 Result = LegalizeOp(Node->getOperand(0));
830 assert(Result.getValueType() >= NVT &&
831 "This truncation doesn't make sense!");
832 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
833 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
834 break;
835 case Expand:
836 assert(0 && "Cannot handle expand yet");
837 case Promote:
838 assert(0 && "Cannot handle promote-promote yet");
839 }
840 break;
841 case ISD::AND:
842 case ISD::OR:
843 case ISD::XOR:
844 // The logical ops can just execute, they don't care what the top bits
845 // coming in are.
846 Tmp1 = PromoteOp(Node->getOperand(0));
847 Tmp2 = PromoteOp(Node->getOperand(1));
848 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
849 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
850 break;
851
Chris Lattner0f69b292005-01-15 06:18:18 +0000852 case ISD::ADD:
853 case ISD::MUL:
854 // The input may have strange things in the top bits of the registers, but
855 // these operations don't care. They may have wierd bits going out, but
856 // that too is okay if they are integer operations.
857 Tmp1 = PromoteOp(Node->getOperand(0));
858 Tmp2 = PromoteOp(Node->getOperand(1));
859 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
860 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
861
862 // However, if this is a floating point operation, they will give excess
863 // precision that we may not be able to tolerate. If we DO allow excess
864 // precision, just leave it, otherwise excise it.
865 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
866 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
867 break;
868
Chris Lattner03c85462005-01-15 05:21:40 +0000869 case ISD::LOAD:
870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
871 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
872 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
873
874 // Remember that we legalized the chain.
875 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
876 break;
877 case ISD::SELECT:
878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition
879 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
880 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
881 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
882 break;
883 }
884
885 assert(Result.Val && "Didn't set a result!");
886 AddPromotedOperand(Op, Result);
887 return Result;
888}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000889
890/// ExpandOp - Expand the specified SDOperand into its two component pieces
891/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
892/// LegalizeNodes map is filled in for any results that are not expanded, the
893/// ExpandedNodes map is filled in for any results that are expanded, and the
894/// Lo/Hi values are returned.
895void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
896 MVT::ValueType VT = Op.getValueType();
897 MVT::ValueType NVT = TransformToType[VT];
898 SDNode *Node = Op.Val;
899 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
900 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
901 assert(MVT::isInteger(NVT) && NVT < VT &&
902 "Cannot expand to FP value or to larger int value!");
903
904 // If there is more than one use of this, see if we already expanded it.
905 // There is no use remembering values that only have a single use, as the map
906 // entries will never be reused.
907 if (!Node->hasOneUse()) {
908 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
909 = ExpandedNodes.find(Op);
910 if (I != ExpandedNodes.end()) {
911 Lo = I->second.first;
912 Hi = I->second.second;
913 return;
914 }
915 }
916
Chris Lattner4e6c7462005-01-08 19:27:05 +0000917 // Expanding to multiple registers needs to perform an optimization step, and
918 // is not careful to avoid operations the target does not support. Make sure
919 // that all generated operations are legalized in the next iteration.
920 NeedsAnotherIteration = true;
921 const char *LibCallName = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000922
Chris Lattner3e928bb2005-01-07 07:47:09 +0000923 switch (Node->getOpcode()) {
924 default:
925 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
926 assert(0 && "Do not know how to expand this operator!");
927 abort();
928 case ISD::Constant: {
929 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
930 Lo = DAG.getConstant(Cst, NVT);
931 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
932 break;
933 }
934
935 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +0000936 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000937 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +0000938 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
939 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
940
941 // Remember that we legalized the chain.
942 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
943
Chris Lattner3e928bb2005-01-07 07:47:09 +0000944 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
945 break;
946 }
947
948 case ISD::LOAD: {
949 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
950 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
951 Lo = DAG.getLoad(NVT, Ch, Ptr);
952
953 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +0000954 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000955 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
956 getIntPtrConstant(IncrementSize));
957 // FIXME: This load is independent of the first one.
958 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
959
960 // Remember that we legalized the chain.
Chris Lattner8afc48e2005-01-07 22:28:47 +0000961 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000962 if (!TLI.isLittleEndian())
963 std::swap(Lo, Hi);
964 break;
965 }
966 case ISD::CALL: {
967 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
968 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
969
970 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
971 "Can only expand a call once so far, not i64 -> i16!");
972
973 std::vector<MVT::ValueType> RetTyVTs;
974 RetTyVTs.reserve(3);
975 RetTyVTs.push_back(NVT);
976 RetTyVTs.push_back(NVT);
977 RetTyVTs.push_back(MVT::Other);
978 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
979 Lo = SDOperand(NC, 0);
980 Hi = SDOperand(NC, 1);
981
982 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +0000983 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000984 break;
985 }
986 case ISD::AND:
987 case ISD::OR:
988 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
989 SDOperand LL, LH, RL, RH;
990 ExpandOp(Node->getOperand(0), LL, LH);
991 ExpandOp(Node->getOperand(1), RL, RH);
992 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
993 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
994 break;
995 }
996 case ISD::SELECT: {
997 SDOperand C, LL, LH, RL, RH;
998 // FIXME: BOOLS MAY REQUIRE PROMOTION!
999 C = LegalizeOp(Node->getOperand(0));
1000 ExpandOp(Node->getOperand(1), LL, LH);
1001 ExpandOp(Node->getOperand(2), RL, RH);
1002 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1003 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1004 break;
1005 }
1006 case ISD::SIGN_EXTEND: {
1007 // The low part is just a sign extension of the input (which degenerates to
1008 // a copy).
1009 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1010
1011 // The high part is obtained by SRA'ing all but one of the bits of the lo
1012 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00001013 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
1014 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001015 break;
1016 }
1017 case ISD::ZERO_EXTEND:
1018 // The low part is just a zero extension of the input (which degenerates to
1019 // a copy).
1020 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1021
1022 // The high part is just a zero.
1023 Hi = DAG.getConstant(0, NVT);
1024 break;
Chris Lattner4e6c7462005-01-08 19:27:05 +00001025
1026 // These operators cannot be expanded directly, emit them as calls to
1027 // library functions.
1028 case ISD::FP_TO_SINT:
1029 if (Node->getOperand(0).getValueType() == MVT::f32)
1030 LibCallName = "__fixsfdi";
1031 else
1032 LibCallName = "__fixdfdi";
1033 break;
1034 case ISD::FP_TO_UINT:
1035 if (Node->getOperand(0).getValueType() == MVT::f32)
1036 LibCallName = "__fixunssfdi";
1037 else
1038 LibCallName = "__fixunsdfdi";
1039 break;
1040
1041 case ISD::ADD: LibCallName = "__adddi3"; break;
1042 case ISD::SUB: LibCallName = "__subdi3"; break;
1043 case ISD::MUL: LibCallName = "__muldi3"; break;
1044 case ISD::SDIV: LibCallName = "__divdi3"; break;
1045 case ISD::UDIV: LibCallName = "__udivdi3"; break;
1046 case ISD::SREM: LibCallName = "__moddi3"; break;
1047 case ISD::UREM: LibCallName = "__umoddi3"; break;
Chris Lattner6b7598b2005-01-10 21:02:37 +00001048 case ISD::SHL: LibCallName = "__ashldi3"; break;
Chris Lattner4e6c7462005-01-08 19:27:05 +00001049 case ISD::SRA: LibCallName = "__ashrdi3"; break;
Chris Lattner6b7598b2005-01-10 21:02:37 +00001050 case ISD::SRL: LibCallName = "__lshrdi3"; break;
Chris Lattner4e6c7462005-01-08 19:27:05 +00001051 }
1052
1053 // Int2FP -> __floatdisf/__floatdidf
1054
1055 // If this is to be expanded into a libcall... do so now.
1056 if (LibCallName) {
1057 TargetLowering::ArgListTy Args;
1058 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1059 Args.push_back(std::make_pair(Node->getOperand(i),
1060 getTypeFor(Node->getOperand(i).getValueType())));
1061 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
1062
1063 // We don't care about token chains for libcalls. We just use the entry
1064 // node as our input and ignore the output chain. This allows us to place
1065 // calls wherever we need them to satisfy data dependences.
1066 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
1067 getTypeFor(Op.getValueType()), Callee,
1068 Args, DAG).first;
1069 ExpandOp(Result, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001070 }
1071
1072 // Remember in a map if the values will be reused later.
1073 if (!Node->hasOneUse()) {
1074 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1075 std::make_pair(Lo, Hi))).second;
1076 assert(isNew && "Value already expanded?!?");
1077 }
1078}
1079
1080
1081// SelectionDAG::Legalize - This is the entry point for the file.
1082//
1083void SelectionDAG::Legalize(TargetLowering &TLI) {
1084 /// run - This is the main entry point to this class.
1085 ///
1086 SelectionDAGLegalize(TLI, *this).Run();
1087}
1088