blob: 19e70f59279c29e96276177c19067fa478d245d2 [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"
18#include "llvm/Constants.h"
19#include <iostream>
20using namespace llvm;
21
Chris Lattner4e6c7462005-01-08 19:27:05 +000022static const Type *getTypeFor(MVT::ValueType VT) {
23 switch (VT) {
24 default: assert(0 && "Unknown MVT!");
25 case MVT::i1: return Type::BoolTy;
26 case MVT::i8: return Type::UByteTy;
27 case MVT::i16: return Type::UShortTy;
28 case MVT::i32: return Type::UIntTy;
29 case MVT::i64: return Type::ULongTy;
30 case MVT::f32: return Type::FloatTy;
31 case MVT::f64: return Type::DoubleTy;
32 }
33}
34
35
Chris Lattner3e928bb2005-01-07 07:47:09 +000036//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
49class SelectionDAGLegalize {
50 TargetLowering &TLI;
51 SelectionDAG &DAG;
52
53 /// LegalizeAction - This enum indicates what action we should take for each
54 /// value type the can occur in the program.
55 enum LegalizeAction {
56 Legal, // The target natively supports this value type.
57 Promote, // This should be promoted to the next larger type.
58 Expand, // This integer type should be broken into smaller pieces.
59 };
60
61 /// TransformToType - For any value types we are promoting or expanding, this
62 /// contains the value type that we are changing to. For Expanded types, this
63 /// contains one step of the expand (e.g. i64 -> i32), even if there are
64 /// multiple steps required (e.g. i64 -> i16)
65 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
66
67 /// ValueTypeActions - This is a bitvector that contains two bits for each
68 /// value type, where the two bits correspond to the LegalizeAction enum.
69 /// This can be queried with "getTypeAction(VT)".
70 unsigned ValueTypeActions;
71
72 /// NeedsAnotherIteration - This is set when we expand a large integer
73 /// operation into smaller integer operations, but the smaller operations are
74 /// not set. This occurs only rarely in practice, for targets that don't have
75 /// 32-bit or larger integer registers.
76 bool NeedsAnotherIteration;
77
78 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
81 std::map<SDOperand, SDOperand> LegalizedNodes;
82
83 /// ExpandedNodes - For nodes that need to be expanded, and which have more
84 /// than one use, this map indicates which which operands are the expanded
85 /// version of the input. This allows us to avoid expanding the same node
86 /// more than once.
87 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
88
Chris Lattner8afc48e2005-01-07 22:28:47 +000089 void AddLegalizedOperand(SDOperand From, SDOperand To) {
90 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
91 assert(isNew && "Got into the map somehow?");
92 }
93
Chris Lattner3e928bb2005-01-07 07:47:09 +000094 /// setValueTypeAction - Set the action for a particular value type. This
95 /// assumes an action has not already been set for this value type.
96 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
97 ValueTypeActions |= A << (VT*2);
98 if (A == Promote) {
99 MVT::ValueType PromoteTo;
100 if (VT == MVT::f32)
101 PromoteTo = MVT::f64;
102 else {
103 unsigned LargerReg = VT+1;
104 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
105 ++LargerReg;
106 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
107 "Nothing to promote to??");
108 }
109 PromoteTo = (MVT::ValueType)LargerReg;
110 }
111
112 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
113 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
114 "Can only promote from int->int or fp->fp!");
115 assert(VT < PromoteTo && "Must promote to a larger type!");
116 TransformToType[VT] = PromoteTo;
117 } else if (A == Expand) {
118 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
119 "Cannot expand this type: target must support SOME integer reg!");
120 // Expand to the next smaller integer type!
121 TransformToType[VT] = (MVT::ValueType)(VT-1);
122 }
123 }
124
125public:
126
127 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
128
129 /// Run - While there is still lowering to do, perform a pass over the DAG.
130 /// Most regularization can be done in a single pass, but targets that require
131 /// large values to be split into registers multiple times (e.g. i64 -> 4x
132 /// i16) require iteration for these values (the first iteration will demote
133 /// to i32, the second will demote to i16).
134 void Run() {
135 do {
136 NeedsAnotherIteration = false;
137 LegalizeDAG();
138 } while (NeedsAnotherIteration);
139 }
140
141 /// getTypeAction - Return how we should legalize values of this type, either
142 /// it is already legal or we need to expand it into multiple registers of
143 /// smaller integer type, or we need to promote it to a larger type.
144 LegalizeAction getTypeAction(MVT::ValueType VT) const {
145 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
146 }
147
148 /// isTypeLegal - Return true if this type is legal on this target.
149 ///
150 bool isTypeLegal(MVT::ValueType VT) const {
151 return getTypeAction(VT) == Legal;
152 }
153
154private:
155 void LegalizeDAG();
156
157 SDOperand LegalizeOp(SDOperand O);
158 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
159
160 SDOperand getIntPtrConstant(uint64_t Val) {
161 return DAG.getConstant(Val, TLI.getPointerTy());
162 }
163};
164}
165
166
167SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
168 SelectionDAG &dag)
169 : TLI(tli), DAG(dag), ValueTypeActions(0) {
170
171 assert(MVT::LAST_VALUETYPE <= 16 &&
172 "Too many value types for ValueTypeActions to hold!");
173
174 // Inspect all of the ValueType's possible, deciding how to process them.
175 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
176 // If TLI says we are expanding this type, expand it!
177 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
178 setValueTypeAction((MVT::ValueType)IntReg, Expand);
179 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
180 // Otherwise, if we don't have native support, we must promote to a
181 // larger type.
182 setValueTypeAction((MVT::ValueType)IntReg, Promote);
183
184 // If the target does not have native support for F32, promote it to F64.
185 if (!TLI.hasNativeSupportFor(MVT::f32))
186 setValueTypeAction(MVT::f32, Promote);
187}
188
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189void SelectionDAGLegalize::LegalizeDAG() {
190 SDOperand OldRoot = DAG.getRoot();
191 SDOperand NewRoot = LegalizeOp(OldRoot);
192 DAG.setRoot(NewRoot);
193
194 ExpandedNodes.clear();
195 LegalizedNodes.clear();
196
197 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000198 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000199}
200
201SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000202 assert(getTypeAction(Op.getValueType()) == Legal &&
203 "Caller should expand or promote operands that are not legal!");
204
Chris Lattner3e928bb2005-01-07 07:47:09 +0000205 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000206 // register on this target, make sure to expand or promote them.
207 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000208 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
209 switch (getTypeAction(Op.Val->getValueType(i))) {
210 case Legal: break; // Nothing to do.
211 case Expand: {
212 SDOperand T1, T2;
213 ExpandOp(Op.getValue(i), T1, T2);
214 assert(LegalizedNodes.count(Op) &&
215 "Expansion didn't add legal operands!");
216 return LegalizedNodes[Op];
217 }
218 case Promote:
219 // FIXME: Implement promotion!
220 assert(0 && "Promotion not implemented at all yet!");
221 }
222 }
223
224 // If there is more than one use of this, see if we already legalized it.
225 // There is no use remembering values that only have a single use, as the map
226 // entries will never be reused.
227 if (!Op.Val->hasOneUse()) {
228 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
229 if (I != LegalizedNodes.end()) return I->second;
230 }
231
232 SDOperand Tmp1, Tmp2;
233
234 SDOperand Result = Op;
235 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000236
237 switch (Node->getOpcode()) {
238 default:
239 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
240 assert(0 && "Do not know how to legalize this operator!");
241 abort();
242 case ISD::EntryToken:
243 case ISD::FrameIndex:
244 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000245 case ISD::ExternalSymbol:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000246 case ISD::ConstantPool:
247 case ISD::CopyFromReg: // Nothing to do.
248 assert(getTypeAction(Node->getValueType(0)) == Legal &&
249 "This must be legal!");
250 break;
251 case ISD::Constant:
252 // We know we don't need to expand constants here, constants only have one
253 // value and we check that it is fine above.
254
255 // FIXME: Maybe we should handle things like targets that don't support full
256 // 32-bit immediates?
257 break;
258 case ISD::ConstantFP: {
259 // Spill FP immediates to the constant pool if the target cannot directly
260 // codegen them. Targets often have some immediate values that can be
261 // efficiently generated into an FP register without a load. We explicitly
262 // leave these constants as ConstantFP nodes for the target to deal with.
263
264 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
265
266 // Check to see if this FP immediate is already legal.
267 bool isLegal = false;
268 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
269 E = TLI.legal_fpimm_end(); I != E; ++I)
270 if (CFP->isExactlyValue(*I)) {
271 isLegal = true;
272 break;
273 }
274
275 if (!isLegal) {
276 // Otherwise we need to spill the constant to memory.
277 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
278
279 bool Extend = false;
280
281 // If a FP immediate is precise when represented as a float, we put it
282 // into the constant pool as a float, even if it's is statically typed
283 // as a double.
284 MVT::ValueType VT = CFP->getValueType(0);
285 bool isDouble = VT == MVT::f64;
286 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
287 Type::FloatTy, CFP->getValue());
288 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
289 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
290 VT = MVT::f32;
291 Extend = true;
292 }
293
294 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
295 TLI.getPointerTy());
296 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
297
298 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
299 }
300 break;
301 }
302 case ISD::ADJCALLSTACKDOWN:
303 case ISD::ADJCALLSTACKUP:
304 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
305 // There is no need to legalize the size argument (Operand #1)
306 if (Tmp1 != Node->getOperand(0))
307 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
308 Node->getOperand(1));
309 break;
310 case ISD::CALL:
311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
312 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfad71eb2005-01-07 21:35:32 +0000313 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000314 std::vector<MVT::ValueType> RetTyVTs;
315 RetTyVTs.reserve(Node->getNumValues());
316 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000317 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000318 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo);
319 }
320 break;
321
Chris Lattnerc7af1792005-01-07 22:12:08 +0000322 case ISD::BR:
323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
326 break;
327
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000328 case ISD::BRCOND:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 // FIXME: booleans might not be legal!
331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
332 // Basic block destination (Op#2) is always legal.
333 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
334 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
335 Node->getOperand(2));
336 break;
337
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338 case ISD::LOAD:
339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
341 if (Tmp1 != Node->getOperand(0) ||
342 Tmp2 != Node->getOperand(1))
343 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner8afc48e2005-01-07 22:28:47 +0000344 else
345 Result = SDOperand(Node, 0);
346
347 // Since loads produce two values, make sure to remember that we legalized
348 // both of them.
349 AddLegalizedOperand(SDOperand(Node, 0), Result);
350 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
351 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352
353 case ISD::EXTRACT_ELEMENT:
354 // Get both the low and high parts.
355 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
356 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
357 Result = Tmp2; // 1 -> Hi
358 else
359 Result = Tmp1; // 0 -> Lo
360 break;
361
362 case ISD::CopyToReg:
363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
364
365 switch (getTypeAction(Node->getOperand(1).getValueType())) {
366 case Legal:
367 // Legalize the incoming value (must be legal).
368 Tmp2 = LegalizeOp(Node->getOperand(1));
369 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
370 Result = DAG.getCopyToReg(Tmp1, Tmp2,
371 cast<CopyRegSDNode>(Node)->getReg());
372 break;
373 case Expand: {
374 SDOperand Lo, Hi;
375 ExpandOp(Node->getOperand(1), Lo, Hi);
376 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
377 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
378 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
379 assert(isTypeLegal(Result.getValueType()) &&
380 "Cannot expand multiple times yet (i64 -> i16)");
381 break;
382 }
383 case Promote:
384 assert(0 && "Don't know what it means to promote this!");
385 abort();
386 }
387 break;
388
389 case ISD::RET:
390 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
391 switch (Node->getNumOperands()) {
392 case 2: // ret val
393 switch (getTypeAction(Node->getOperand(1).getValueType())) {
394 case Legal:
395 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000396 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000397 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
398 break;
399 case Expand: {
400 SDOperand Lo, Hi;
401 ExpandOp(Node->getOperand(1), Lo, Hi);
402 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
403 break;
404 }
405 case Promote:
406 assert(0 && "Can't promote return value!");
407 }
408 break;
409 case 1: // ret void
410 if (Tmp1 != Node->getOperand(0))
411 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
412 break;
413 default: { // ret <values>
414 std::vector<SDOperand> NewValues;
415 NewValues.push_back(Tmp1);
416 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
417 switch (getTypeAction(Node->getOperand(i).getValueType())) {
418 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000419 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000420 break;
421 case Expand: {
422 SDOperand Lo, Hi;
423 ExpandOp(Node->getOperand(i), Lo, Hi);
424 NewValues.push_back(Lo);
425 NewValues.push_back(Hi);
426 break;
427 }
428 case Promote:
429 assert(0 && "Can't promote return value!");
430 }
431 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
432 break;
433 }
434 }
435 break;
436 case ISD::STORE:
437 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
438 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
439
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000440 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
441 if (ConstantFPSDNode *CFP =
442 dyn_cast<ConstantFPSDNode>(Node->getOperand(1))) {
443 if (CFP->getValueType(0) == MVT::f32) {
444 union {
445 unsigned I;
446 float F;
447 } V;
448 V.F = CFP->getValue();
449 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
450 DAG.getConstant(V.I, MVT::i32), Tmp2);
451 } else {
452 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
453 union {
454 uint64_t I;
455 double F;
456 } V;
457 V.F = CFP->getValue();
458 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
459 DAG.getConstant(V.I, MVT::i64), Tmp2);
460 }
461 Op = Result;
462 Node = Op.Val;
463 }
464
Chris Lattner3e928bb2005-01-07 07:47:09 +0000465 switch (getTypeAction(Node->getOperand(1).getValueType())) {
466 case Legal: {
467 SDOperand Val = LegalizeOp(Node->getOperand(1));
468 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
469 Tmp2 != Node->getOperand(2))
470 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
471 break;
472 }
473 case Promote:
474 assert(0 && "FIXME: promote for stores not implemented!");
475 case Expand:
476 SDOperand Lo, Hi;
477 ExpandOp(Node->getOperand(1), Lo, Hi);
478
479 if (!TLI.isLittleEndian())
480 std::swap(Lo, Hi);
481
482 // FIXME: These two stores are independent of each other!
483 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
484
485 unsigned IncrementSize;
486 switch (Lo.getValueType()) {
487 default: assert(0 && "Unknown ValueType to expand to!");
488 case MVT::i32: IncrementSize = 4; break;
489 case MVT::i16: IncrementSize = 2; break;
490 case MVT::i8: IncrementSize = 1; break;
491 }
492 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
493 getIntPtrConstant(IncrementSize));
494 assert(isTypeLegal(Tmp2.getValueType()) &&
495 "Pointers must be legal!");
496 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
497 }
498 break;
499 case ISD::SELECT: {
500 // FIXME: BOOLS MAY REQUIRE PROMOTION!
501 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
502 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
503 SDOperand Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
504
505 if (Tmp1 != Node->getOperand(0) ||
506 Tmp2 != Node->getOperand(1) ||
507 Tmp3 != Node->getOperand(2))
508 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
509 break;
510 }
511 case ISD::SETCC:
512 switch (getTypeAction(Node->getOperand(0).getValueType())) {
513 case Legal:
514 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
515 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
516 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
517 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
518 Tmp1, Tmp2);
519 break;
520 case Promote:
521 assert(0 && "Can't promote setcc operands yet!");
522 break;
523 case Expand:
524 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
525 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
526 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
527 switch (cast<SetCCSDNode>(Node)->getCondition()) {
528 case ISD::SETEQ:
529 case ISD::SETNE:
530 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
531 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
532 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
533 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
534 DAG.getConstant(0, Tmp1.getValueType()));
535 break;
536 default:
537 // FIXME: This generated code sucks.
538 ISD::CondCode LowCC;
539 switch (cast<SetCCSDNode>(Node)->getCondition()) {
540 default: assert(0 && "Unknown integer setcc!");
541 case ISD::SETLT:
542 case ISD::SETULT: LowCC = ISD::SETULT; break;
543 case ISD::SETGT:
544 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
545 case ISD::SETLE:
546 case ISD::SETULE: LowCC = ISD::SETULE; break;
547 case ISD::SETGE:
548 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
549 }
550
551 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
552 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
553 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
554
555 // NOTE: on targets without efficient SELECT of bools, we can always use
556 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
557 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
558 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
559 LHSHi, RHSHi);
560 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
561 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
562 break;
563 }
564 }
565 break;
566
567 case ISD::ADD:
568 case ISD::SUB:
569 case ISD::MUL:
570 case ISD::UDIV:
571 case ISD::SDIV:
572 case ISD::UREM:
573 case ISD::SREM:
574 case ISD::AND:
575 case ISD::OR:
576 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000577 case ISD::SHL:
578 case ISD::SRL:
579 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
581 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
582 if (Tmp1 != Node->getOperand(0) ||
583 Tmp2 != Node->getOperand(1))
584 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
585 break;
586 case ISD::ZERO_EXTEND:
587 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +0000588 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000589 case ISD::FP_EXTEND:
590 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000591 case ISD::FP_TO_SINT:
592 case ISD::FP_TO_UINT:
593 case ISD::SINT_TO_FP:
594 case ISD::UINT_TO_FP:
595
Chris Lattner3e928bb2005-01-07 07:47:09 +0000596 switch (getTypeAction(Node->getOperand(0).getValueType())) {
597 case Legal:
598 Tmp1 = LegalizeOp(Node->getOperand(0));
599 if (Tmp1 != Node->getOperand(0))
600 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
601 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +0000602 case Expand:
603 // In the expand case, we must be dealing with a truncate, because
604 // otherwise the result would be larger than the source.
605 assert(Node->getOpcode() == ISD::TRUNCATE &&
606 "Shouldn't need to expand other operators here!");
607 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
608
609 // Since the result is legal, we should just be able to truncate the low
610 // part of the source.
611 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
612 break;
613
Chris Lattner3e928bb2005-01-07 07:47:09 +0000614 default:
Chris Lattnerb00a6422005-01-07 22:37:48 +0000615 assert(0 && "Do not know how to promote this yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000616 }
617 break;
618 }
619
Chris Lattner8afc48e2005-01-07 22:28:47 +0000620 if (!Op.Val->hasOneUse())
621 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000622
623 return Result;
624}
625
626
627/// ExpandOp - Expand the specified SDOperand into its two component pieces
628/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
629/// LegalizeNodes map is filled in for any results that are not expanded, the
630/// ExpandedNodes map is filled in for any results that are expanded, and the
631/// Lo/Hi values are returned.
632void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
633 MVT::ValueType VT = Op.getValueType();
634 MVT::ValueType NVT = TransformToType[VT];
635 SDNode *Node = Op.Val;
636 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
637 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
638 assert(MVT::isInteger(NVT) && NVT < VT &&
639 "Cannot expand to FP value or to larger int value!");
640
641 // If there is more than one use of this, see if we already expanded it.
642 // There is no use remembering values that only have a single use, as the map
643 // entries will never be reused.
644 if (!Node->hasOneUse()) {
645 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
646 = ExpandedNodes.find(Op);
647 if (I != ExpandedNodes.end()) {
648 Lo = I->second.first;
649 Hi = I->second.second;
650 return;
651 }
652 }
653
Chris Lattner4e6c7462005-01-08 19:27:05 +0000654 // Expanding to multiple registers needs to perform an optimization step, and
655 // is not careful to avoid operations the target does not support. Make sure
656 // that all generated operations are legalized in the next iteration.
657 NeedsAnotherIteration = true;
658 const char *LibCallName = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000659
Chris Lattner3e928bb2005-01-07 07:47:09 +0000660 switch (Node->getOpcode()) {
661 default:
662 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
663 assert(0 && "Do not know how to expand this operator!");
664 abort();
665 case ISD::Constant: {
666 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
667 Lo = DAG.getConstant(Cst, NVT);
668 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
669 break;
670 }
671
672 case ISD::CopyFromReg: {
673 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
674 // Aggregate register values are always in consequtive pairs.
675 Lo = DAG.getCopyFromReg(Reg, NVT);
676 Hi = DAG.getCopyFromReg(Reg+1, NVT);
677 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
678 break;
679 }
680
681 case ISD::LOAD: {
682 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
683 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
684 Lo = DAG.getLoad(NVT, Ch, Ptr);
685
686 // Increment the pointer to the other half.
687 unsigned IncrementSize;
688 switch (Lo.getValueType()) {
689 default: assert(0 && "Unknown ValueType to expand to!");
690 case MVT::i32: IncrementSize = 4; break;
691 case MVT::i16: IncrementSize = 2; break;
692 case MVT::i8: IncrementSize = 1; break;
693 }
694 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
695 getIntPtrConstant(IncrementSize));
696 // FIXME: This load is independent of the first one.
697 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
698
699 // Remember that we legalized the chain.
Chris Lattner8afc48e2005-01-07 22:28:47 +0000700 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000701 if (!TLI.isLittleEndian())
702 std::swap(Lo, Hi);
703 break;
704 }
705 case ISD::CALL: {
706 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
707 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
708
709 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
710 "Can only expand a call once so far, not i64 -> i16!");
711
712 std::vector<MVT::ValueType> RetTyVTs;
713 RetTyVTs.reserve(3);
714 RetTyVTs.push_back(NVT);
715 RetTyVTs.push_back(NVT);
716 RetTyVTs.push_back(MVT::Other);
717 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
718 Lo = SDOperand(NC, 0);
719 Hi = SDOperand(NC, 1);
720
721 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +0000722 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000723 break;
724 }
725 case ISD::AND:
726 case ISD::OR:
727 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
728 SDOperand LL, LH, RL, RH;
729 ExpandOp(Node->getOperand(0), LL, LH);
730 ExpandOp(Node->getOperand(1), RL, RH);
731 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
732 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
733 break;
734 }
735 case ISD::SELECT: {
736 SDOperand C, LL, LH, RL, RH;
737 // FIXME: BOOLS MAY REQUIRE PROMOTION!
738 C = LegalizeOp(Node->getOperand(0));
739 ExpandOp(Node->getOperand(1), LL, LH);
740 ExpandOp(Node->getOperand(2), RL, RH);
741 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
742 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
743 break;
744 }
745 case ISD::SIGN_EXTEND: {
746 // The low part is just a sign extension of the input (which degenerates to
747 // a copy).
748 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
749
750 // The high part is obtained by SRA'ing all but one of the bits of the lo
751 // part.
752 unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType());
753 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8));
754 break;
755 }
756 case ISD::ZERO_EXTEND:
757 // The low part is just a zero extension of the input (which degenerates to
758 // a copy).
759 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
760
761 // The high part is just a zero.
762 Hi = DAG.getConstant(0, NVT);
763 break;
Chris Lattner4e6c7462005-01-08 19:27:05 +0000764
765 // These operators cannot be expanded directly, emit them as calls to
766 // library functions.
767 case ISD::FP_TO_SINT:
768 if (Node->getOperand(0).getValueType() == MVT::f32)
769 LibCallName = "__fixsfdi";
770 else
771 LibCallName = "__fixdfdi";
772 break;
773 case ISD::FP_TO_UINT:
774 if (Node->getOperand(0).getValueType() == MVT::f32)
775 LibCallName = "__fixunssfdi";
776 else
777 LibCallName = "__fixunsdfdi";
778 break;
779
780 case ISD::ADD: LibCallName = "__adddi3"; break;
781 case ISD::SUB: LibCallName = "__subdi3"; break;
782 case ISD::MUL: LibCallName = "__muldi3"; break;
783 case ISD::SDIV: LibCallName = "__divdi3"; break;
784 case ISD::UDIV: LibCallName = "__udivdi3"; break;
785 case ISD::SREM: LibCallName = "__moddi3"; break;
786 case ISD::UREM: LibCallName = "__umoddi3"; break;
787 case ISD::SHL: LibCallName = "__lshrdi3"; break;
788 case ISD::SRA: LibCallName = "__ashrdi3"; break;
789 case ISD::SRL: LibCallName = "__ashldi3"; break;
790 }
791
792 // Int2FP -> __floatdisf/__floatdidf
793
794 // If this is to be expanded into a libcall... do so now.
795 if (LibCallName) {
796 TargetLowering::ArgListTy Args;
797 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
798 Args.push_back(std::make_pair(Node->getOperand(i),
799 getTypeFor(Node->getOperand(i).getValueType())));
800 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
801
802 // We don't care about token chains for libcalls. We just use the entry
803 // node as our input and ignore the output chain. This allows us to place
804 // calls wherever we need them to satisfy data dependences.
805 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
806 getTypeFor(Op.getValueType()), Callee,
807 Args, DAG).first;
808 ExpandOp(Result, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000809 }
810
811 // Remember in a map if the values will be reused later.
812 if (!Node->hasOneUse()) {
813 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
814 std::make_pair(Lo, Hi))).second;
815 assert(isNew && "Value already expanded?!?");
816 }
817}
818
819
820// SelectionDAG::Legalize - This is the entry point for the file.
821//
822void SelectionDAG::Legalize(TargetLowering &TLI) {
823 /// run - This is the main entry point to this class.
824 ///
825 SelectionDAGLegalize(TLI, *this).Run();
826}
827