blob: 964c5116c7700710f9971e6571a3d897f0e41405 [file] [log] [blame]
Chris Lattnerdc750592005-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 Lattner7e6eeba2005-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 Lattnerdc750592005-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 Lattnerea4ca942005-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 Lattnerdc750592005-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 Lattnerdc750592005-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 Lattner473825c2005-01-07 21:09:37 +0000198 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000199}
200
201SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
202 // If this operation defines any values that cannot be represented in a
203 // register on this target, make sure to expand it.
204 if (Op.Val->getNumValues() == 1) {// Fast path == assertion only
205 assert(getTypeAction(Op.Val->getValueType(0)) == Legal &&
206 "For a single use value, caller should check for legality!");
207 } else {
208 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;
236 LegalizeAction Action;
237
238 switch (Node->getOpcode()) {
239 default:
240 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
241 assert(0 && "Do not know how to legalize this operator!");
242 abort();
243 case ISD::EntryToken:
244 case ISD::FrameIndex:
245 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000246 case ISD::ExternalSymbol:
Chris Lattnerdc750592005-01-07 07:47:09 +0000247 case ISD::ConstantPool:
248 case ISD::CopyFromReg: // Nothing to do.
249 assert(getTypeAction(Node->getValueType(0)) == Legal &&
250 "This must be legal!");
251 break;
252 case ISD::Constant:
253 // We know we don't need to expand constants here, constants only have one
254 // value and we check that it is fine above.
255
256 // FIXME: Maybe we should handle things like targets that don't support full
257 // 32-bit immediates?
258 break;
259 case ISD::ConstantFP: {
260 // Spill FP immediates to the constant pool if the target cannot directly
261 // codegen them. Targets often have some immediate values that can be
262 // efficiently generated into an FP register without a load. We explicitly
263 // leave these constants as ConstantFP nodes for the target to deal with.
264
265 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
266
267 // Check to see if this FP immediate is already legal.
268 bool isLegal = false;
269 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
270 E = TLI.legal_fpimm_end(); I != E; ++I)
271 if (CFP->isExactlyValue(*I)) {
272 isLegal = true;
273 break;
274 }
275
276 if (!isLegal) {
277 // Otherwise we need to spill the constant to memory.
278 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
279
280 bool Extend = false;
281
282 // If a FP immediate is precise when represented as a float, we put it
283 // into the constant pool as a float, even if it's is statically typed
284 // as a double.
285 MVT::ValueType VT = CFP->getValueType(0);
286 bool isDouble = VT == MVT::f64;
287 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
288 Type::FloatTy, CFP->getValue());
289 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
290 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
291 VT = MVT::f32;
292 Extend = true;
293 }
294
295 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
296 TLI.getPointerTy());
297 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
298
299 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
300 }
301 break;
302 }
303 case ISD::ADJCALLSTACKDOWN:
304 case ISD::ADJCALLSTACKUP:
305 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
306 // There is no need to legalize the size argument (Operand #1)
307 if (Tmp1 != Node->getOperand(0))
308 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
309 Node->getOperand(1));
310 break;
311 case ISD::CALL:
312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
313 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfa854eb2005-01-07 21:35:32 +0000314 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000315 std::vector<MVT::ValueType> RetTyVTs;
316 RetTyVTs.reserve(Node->getNumValues());
317 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000318 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000319 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo);
320 }
321 break;
322
Chris Lattner68a12142005-01-07 22:12:08 +0000323 case ISD::BR:
324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
325 if (Tmp1 != Node->getOperand(0))
326 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
327 break;
328
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000329 case ISD::BRCOND:
330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
331 // FIXME: booleans might not be legal!
332 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
333 // Basic block destination (Op#2) is always legal.
334 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
335 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
336 Node->getOperand(2));
337 break;
338
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 case ISD::LOAD:
340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
341 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
342 if (Tmp1 != Node->getOperand(0) ||
343 Tmp2 != Node->getOperand(1))
344 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000345 else
346 Result = SDOperand(Node, 0);
347
348 // Since loads produce two values, make sure to remember that we legalized
349 // both of them.
350 AddLegalizedOperand(SDOperand(Node, 0), Result);
351 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
352 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000353
354 case ISD::EXTRACT_ELEMENT:
355 // Get both the low and high parts.
356 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
357 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
358 Result = Tmp2; // 1 -> Hi
359 else
360 Result = Tmp1; // 0 -> Lo
361 break;
362
363 case ISD::CopyToReg:
364 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
365
366 switch (getTypeAction(Node->getOperand(1).getValueType())) {
367 case Legal:
368 // Legalize the incoming value (must be legal).
369 Tmp2 = LegalizeOp(Node->getOperand(1));
370 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
371 Result = DAG.getCopyToReg(Tmp1, Tmp2,
372 cast<CopyRegSDNode>(Node)->getReg());
373 break;
374 case Expand: {
375 SDOperand Lo, Hi;
376 ExpandOp(Node->getOperand(1), Lo, Hi);
377 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
378 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
379 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
380 assert(isTypeLegal(Result.getValueType()) &&
381 "Cannot expand multiple times yet (i64 -> i16)");
382 break;
383 }
384 case Promote:
385 assert(0 && "Don't know what it means to promote this!");
386 abort();
387 }
388 break;
389
390 case ISD::RET:
391 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
392 switch (Node->getNumOperands()) {
393 case 2: // ret val
394 switch (getTypeAction(Node->getOperand(1).getValueType())) {
395 case Legal:
396 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000397 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000398 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
399 break;
400 case Expand: {
401 SDOperand Lo, Hi;
402 ExpandOp(Node->getOperand(1), Lo, Hi);
403 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
404 break;
405 }
406 case Promote:
407 assert(0 && "Can't promote return value!");
408 }
409 break;
410 case 1: // ret void
411 if (Tmp1 != Node->getOperand(0))
412 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
413 break;
414 default: { // ret <values>
415 std::vector<SDOperand> NewValues;
416 NewValues.push_back(Tmp1);
417 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
418 switch (getTypeAction(Node->getOperand(i).getValueType())) {
419 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000420 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000421 break;
422 case Expand: {
423 SDOperand Lo, Hi;
424 ExpandOp(Node->getOperand(i), Lo, Hi);
425 NewValues.push_back(Lo);
426 NewValues.push_back(Hi);
427 break;
428 }
429 case Promote:
430 assert(0 && "Can't promote return value!");
431 }
432 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
433 break;
434 }
435 }
436 break;
437 case ISD::STORE:
438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
439 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
440
Chris Lattnere69daaf2005-01-08 06:25:56 +0000441 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
442 if (ConstantFPSDNode *CFP =
443 dyn_cast<ConstantFPSDNode>(Node->getOperand(1))) {
444 if (CFP->getValueType(0) == MVT::f32) {
445 union {
446 unsigned I;
447 float F;
448 } V;
449 V.F = CFP->getValue();
450 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
451 DAG.getConstant(V.I, MVT::i32), Tmp2);
452 } else {
453 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
454 union {
455 uint64_t I;
456 double F;
457 } V;
458 V.F = CFP->getValue();
459 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
460 DAG.getConstant(V.I, MVT::i64), Tmp2);
461 }
462 Op = Result;
463 Node = Op.Val;
464 }
465
Chris Lattnerdc750592005-01-07 07:47:09 +0000466 switch (getTypeAction(Node->getOperand(1).getValueType())) {
467 case Legal: {
468 SDOperand Val = LegalizeOp(Node->getOperand(1));
469 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
470 Tmp2 != Node->getOperand(2))
471 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
472 break;
473 }
474 case Promote:
475 assert(0 && "FIXME: promote for stores not implemented!");
476 case Expand:
477 SDOperand Lo, Hi;
478 ExpandOp(Node->getOperand(1), Lo, Hi);
479
480 if (!TLI.isLittleEndian())
481 std::swap(Lo, Hi);
482
483 // FIXME: These two stores are independent of each other!
484 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
485
486 unsigned IncrementSize;
487 switch (Lo.getValueType()) {
488 default: assert(0 && "Unknown ValueType to expand to!");
489 case MVT::i32: IncrementSize = 4; break;
490 case MVT::i16: IncrementSize = 2; break;
491 case MVT::i8: IncrementSize = 1; break;
492 }
493 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
494 getIntPtrConstant(IncrementSize));
495 assert(isTypeLegal(Tmp2.getValueType()) &&
496 "Pointers must be legal!");
497 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
498 }
499 break;
500 case ISD::SELECT: {
501 // FIXME: BOOLS MAY REQUIRE PROMOTION!
502 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
503 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
504 SDOperand Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
505
506 if (Tmp1 != Node->getOperand(0) ||
507 Tmp2 != Node->getOperand(1) ||
508 Tmp3 != Node->getOperand(2))
509 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
510 break;
511 }
512 case ISD::SETCC:
513 switch (getTypeAction(Node->getOperand(0).getValueType())) {
514 case Legal:
515 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
516 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
517 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
518 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
519 Tmp1, Tmp2);
520 break;
521 case Promote:
522 assert(0 && "Can't promote setcc operands yet!");
523 break;
524 case Expand:
525 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
526 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
527 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
528 switch (cast<SetCCSDNode>(Node)->getCondition()) {
529 case ISD::SETEQ:
530 case ISD::SETNE:
531 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
532 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
533 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
534 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
535 DAG.getConstant(0, Tmp1.getValueType()));
536 break;
537 default:
538 // FIXME: This generated code sucks.
539 ISD::CondCode LowCC;
540 switch (cast<SetCCSDNode>(Node)->getCondition()) {
541 default: assert(0 && "Unknown integer setcc!");
542 case ISD::SETLT:
543 case ISD::SETULT: LowCC = ISD::SETULT; break;
544 case ISD::SETGT:
545 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
546 case ISD::SETLE:
547 case ISD::SETULE: LowCC = ISD::SETULE; break;
548 case ISD::SETGE:
549 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
550 }
551
552 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
553 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
554 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
555
556 // NOTE: on targets without efficient SELECT of bools, we can always use
557 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
558 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
559 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
560 LHSHi, RHSHi);
561 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
562 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
563 break;
564 }
565 }
566 break;
567
568 case ISD::ADD:
569 case ISD::SUB:
570 case ISD::MUL:
571 case ISD::UDIV:
572 case ISD::SDIV:
573 case ISD::UREM:
574 case ISD::SREM:
575 case ISD::AND:
576 case ISD::OR:
577 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000578 case ISD::SHL:
579 case ISD::SRL:
580 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000581 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
582 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
583 if (Tmp1 != Node->getOperand(0) ||
584 Tmp2 != Node->getOperand(1))
585 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
586 break;
587 case ISD::ZERO_EXTEND:
588 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000589 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000590 case ISD::FP_EXTEND:
591 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000592 case ISD::FP_TO_SINT:
593 case ISD::FP_TO_UINT:
594 case ISD::SINT_TO_FP:
595 case ISD::UINT_TO_FP:
596
Chris Lattnerdc750592005-01-07 07:47:09 +0000597 switch (getTypeAction(Node->getOperand(0).getValueType())) {
598 case Legal:
599 Tmp1 = LegalizeOp(Node->getOperand(0));
600 if (Tmp1 != Node->getOperand(0))
601 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
602 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000603 case Expand:
604 // In the expand case, we must be dealing with a truncate, because
605 // otherwise the result would be larger than the source.
606 assert(Node->getOpcode() == ISD::TRUNCATE &&
607 "Shouldn't need to expand other operators here!");
608 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
609
610 // Since the result is legal, we should just be able to truncate the low
611 // part of the source.
612 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
613 break;
614
Chris Lattnerdc750592005-01-07 07:47:09 +0000615 default:
Chris Lattnera65a2f02005-01-07 22:37:48 +0000616 assert(0 && "Do not know how to promote this yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000617 }
618 break;
619 }
620
Chris Lattnerea4ca942005-01-07 22:28:47 +0000621 if (!Op.Val->hasOneUse())
622 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000623
624 return Result;
625}
626
627
628/// ExpandOp - Expand the specified SDOperand into its two component pieces
629/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
630/// LegalizeNodes map is filled in for any results that are not expanded, the
631/// ExpandedNodes map is filled in for any results that are expanded, and the
632/// Lo/Hi values are returned.
633void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
634 MVT::ValueType VT = Op.getValueType();
635 MVT::ValueType NVT = TransformToType[VT];
636 SDNode *Node = Op.Val;
637 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
638 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
639 assert(MVT::isInteger(NVT) && NVT < VT &&
640 "Cannot expand to FP value or to larger int value!");
641
642 // If there is more than one use of this, see if we already expanded it.
643 // There is no use remembering values that only have a single use, as the map
644 // entries will never be reused.
645 if (!Node->hasOneUse()) {
646 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
647 = ExpandedNodes.find(Op);
648 if (I != ExpandedNodes.end()) {
649 Lo = I->second.first;
650 Hi = I->second.second;
651 return;
652 }
653 }
654
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000655 // Expanding to multiple registers needs to perform an optimization step, and
656 // is not careful to avoid operations the target does not support. Make sure
657 // that all generated operations are legalized in the next iteration.
658 NeedsAnotherIteration = true;
659 const char *LibCallName = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +0000660
661 LegalizeAction Action;
662 switch (Node->getOpcode()) {
663 default:
664 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
665 assert(0 && "Do not know how to expand this operator!");
666 abort();
667 case ISD::Constant: {
668 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
669 Lo = DAG.getConstant(Cst, NVT);
670 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
671 break;
672 }
673
674 case ISD::CopyFromReg: {
675 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
676 // Aggregate register values are always in consequtive pairs.
677 Lo = DAG.getCopyFromReg(Reg, NVT);
678 Hi = DAG.getCopyFromReg(Reg+1, NVT);
679 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
680 break;
681 }
682
683 case ISD::LOAD: {
684 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
685 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
686 Lo = DAG.getLoad(NVT, Ch, Ptr);
687
688 // Increment the pointer to the other half.
689 unsigned IncrementSize;
690 switch (Lo.getValueType()) {
691 default: assert(0 && "Unknown ValueType to expand to!");
692 case MVT::i32: IncrementSize = 4; break;
693 case MVT::i16: IncrementSize = 2; break;
694 case MVT::i8: IncrementSize = 1; break;
695 }
696 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
697 getIntPtrConstant(IncrementSize));
698 // FIXME: This load is independent of the first one.
699 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
700
701 // Remember that we legalized the chain.
Chris Lattnerea4ca942005-01-07 22:28:47 +0000702 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +0000703 if (!TLI.isLittleEndian())
704 std::swap(Lo, Hi);
705 break;
706 }
707 case ISD::CALL: {
708 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
709 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
710
711 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
712 "Can only expand a call once so far, not i64 -> i16!");
713
714 std::vector<MVT::ValueType> RetTyVTs;
715 RetTyVTs.reserve(3);
716 RetTyVTs.push_back(NVT);
717 RetTyVTs.push_back(NVT);
718 RetTyVTs.push_back(MVT::Other);
719 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
720 Lo = SDOperand(NC, 0);
721 Hi = SDOperand(NC, 1);
722
723 // Insert the new chain mapping.
724 bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1),
725 Hi.getValue(2))).second;
726 assert(isNew && "This node was already legalized!");
727 break;
728 }
729 case ISD::AND:
730 case ISD::OR:
731 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
732 SDOperand LL, LH, RL, RH;
733 ExpandOp(Node->getOperand(0), LL, LH);
734 ExpandOp(Node->getOperand(1), RL, RH);
735 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
736 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
737 break;
738 }
739 case ISD::SELECT: {
740 SDOperand C, LL, LH, RL, RH;
741 // FIXME: BOOLS MAY REQUIRE PROMOTION!
742 C = LegalizeOp(Node->getOperand(0));
743 ExpandOp(Node->getOperand(1), LL, LH);
744 ExpandOp(Node->getOperand(2), RL, RH);
745 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
746 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
747 break;
748 }
749 case ISD::SIGN_EXTEND: {
750 // The low part is just a sign extension of the input (which degenerates to
751 // a copy).
752 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
753
754 // The high part is obtained by SRA'ing all but one of the bits of the lo
755 // part.
756 unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType());
757 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8));
758 break;
759 }
760 case ISD::ZERO_EXTEND:
761 // The low part is just a zero extension of the input (which degenerates to
762 // a copy).
763 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
764
765 // The high part is just a zero.
766 Hi = DAG.getConstant(0, NVT);
767 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000768
769 // These operators cannot be expanded directly, emit them as calls to
770 // library functions.
771 case ISD::FP_TO_SINT:
772 if (Node->getOperand(0).getValueType() == MVT::f32)
773 LibCallName = "__fixsfdi";
774 else
775 LibCallName = "__fixdfdi";
776 break;
777 case ISD::FP_TO_UINT:
778 if (Node->getOperand(0).getValueType() == MVT::f32)
779 LibCallName = "__fixunssfdi";
780 else
781 LibCallName = "__fixunsdfdi";
782 break;
783
784 case ISD::ADD: LibCallName = "__adddi3"; break;
785 case ISD::SUB: LibCallName = "__subdi3"; break;
786 case ISD::MUL: LibCallName = "__muldi3"; break;
787 case ISD::SDIV: LibCallName = "__divdi3"; break;
788 case ISD::UDIV: LibCallName = "__udivdi3"; break;
789 case ISD::SREM: LibCallName = "__moddi3"; break;
790 case ISD::UREM: LibCallName = "__umoddi3"; break;
791 case ISD::SHL: LibCallName = "__lshrdi3"; break;
792 case ISD::SRA: LibCallName = "__ashrdi3"; break;
793 case ISD::SRL: LibCallName = "__ashldi3"; break;
794 }
795
796 // Int2FP -> __floatdisf/__floatdidf
797
798 // If this is to be expanded into a libcall... do so now.
799 if (LibCallName) {
800 TargetLowering::ArgListTy Args;
801 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
802 Args.push_back(std::make_pair(Node->getOperand(i),
803 getTypeFor(Node->getOperand(i).getValueType())));
804 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
805
806 // We don't care about token chains for libcalls. We just use the entry
807 // node as our input and ignore the output chain. This allows us to place
808 // calls wherever we need them to satisfy data dependences.
809 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
810 getTypeFor(Op.getValueType()), Callee,
811 Args, DAG).first;
812 ExpandOp(Result, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000813 }
814
815 // Remember in a map if the values will be reused later.
816 if (!Node->hasOneUse()) {
817 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
818 std::make_pair(Lo, Hi))).second;
819 assert(isNew && "Value already expanded?!?");
820 }
821}
822
823
824// SelectionDAG::Legalize - This is the entry point for the file.
825//
826void SelectionDAG::Legalize(TargetLowering &TLI) {
827 /// run - This is the main entry point to this class.
828 ///
829 SelectionDAGLegalize(TLI, *this).Run();
830}
831