blob: 52389ec8ad2b7f4b929f5209dae04506710b861d [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
22//===----------------------------------------------------------------------===//
23/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
24/// hacks on it until the target machine can handle it. This involves
25/// eliminating value sizes the machine cannot handle (promoting small sizes to
26/// large sizes or splitting up large values into small values) as well as
27/// eliminating operations the machine cannot handle.
28///
29/// This code also does a small amount of optimization and recognition of idioms
30/// as part of its processing. For example, if a target does not support a
31/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
32/// will attempt merge setcc and brc instructions into brcc's.
33///
34namespace {
35class SelectionDAGLegalize {
36 TargetLowering &TLI;
37 SelectionDAG &DAG;
38
39 /// LegalizeAction - This enum indicates what action we should take for each
40 /// value type the can occur in the program.
41 enum LegalizeAction {
42 Legal, // The target natively supports this value type.
43 Promote, // This should be promoted to the next larger type.
44 Expand, // This integer type should be broken into smaller pieces.
45 };
46
47 /// TransformToType - For any value types we are promoting or expanding, this
48 /// contains the value type that we are changing to. For Expanded types, this
49 /// contains one step of the expand (e.g. i64 -> i32), even if there are
50 /// multiple steps required (e.g. i64 -> i16)
51 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
52
53 /// ValueTypeActions - This is a bitvector that contains two bits for each
54 /// value type, where the two bits correspond to the LegalizeAction enum.
55 /// This can be queried with "getTypeAction(VT)".
56 unsigned ValueTypeActions;
57
58 /// NeedsAnotherIteration - This is set when we expand a large integer
59 /// operation into smaller integer operations, but the smaller operations are
60 /// not set. This occurs only rarely in practice, for targets that don't have
61 /// 32-bit or larger integer registers.
62 bool NeedsAnotherIteration;
63
64 /// LegalizedNodes - For nodes that are of legal width, and that have more
65 /// than one use, this map indicates what regularized operand to use. This
66 /// allows us to avoid legalizing the same thing more than once.
67 std::map<SDOperand, SDOperand> LegalizedNodes;
68
69 /// ExpandedNodes - For nodes that need to be expanded, and which have more
70 /// than one use, this map indicates which which operands are the expanded
71 /// version of the input. This allows us to avoid expanding the same node
72 /// more than once.
73 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
74
75 /// setValueTypeAction - Set the action for a particular value type. This
76 /// assumes an action has not already been set for this value type.
77 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
78 ValueTypeActions |= A << (VT*2);
79 if (A == Promote) {
80 MVT::ValueType PromoteTo;
81 if (VT == MVT::f32)
82 PromoteTo = MVT::f64;
83 else {
84 unsigned LargerReg = VT+1;
85 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
86 ++LargerReg;
87 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
88 "Nothing to promote to??");
89 }
90 PromoteTo = (MVT::ValueType)LargerReg;
91 }
92
93 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
94 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
95 "Can only promote from int->int or fp->fp!");
96 assert(VT < PromoteTo && "Must promote to a larger type!");
97 TransformToType[VT] = PromoteTo;
98 } else if (A == Expand) {
99 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
100 "Cannot expand this type: target must support SOME integer reg!");
101 // Expand to the next smaller integer type!
102 TransformToType[VT] = (MVT::ValueType)(VT-1);
103 }
104 }
105
106public:
107
108 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
109
110 /// Run - While there is still lowering to do, perform a pass over the DAG.
111 /// Most regularization can be done in a single pass, but targets that require
112 /// large values to be split into registers multiple times (e.g. i64 -> 4x
113 /// i16) require iteration for these values (the first iteration will demote
114 /// to i32, the second will demote to i16).
115 void Run() {
116 do {
117 NeedsAnotherIteration = false;
118 LegalizeDAG();
119 } while (NeedsAnotherIteration);
120 }
121
122 /// getTypeAction - Return how we should legalize values of this type, either
123 /// it is already legal or we need to expand it into multiple registers of
124 /// smaller integer type, or we need to promote it to a larger type.
125 LegalizeAction getTypeAction(MVT::ValueType VT) const {
126 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
127 }
128
129 /// isTypeLegal - Return true if this type is legal on this target.
130 ///
131 bool isTypeLegal(MVT::ValueType VT) const {
132 return getTypeAction(VT) == Legal;
133 }
134
135private:
136 void LegalizeDAG();
137
138 SDOperand LegalizeOp(SDOperand O);
139 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
140
141 SDOperand getIntPtrConstant(uint64_t Val) {
142 return DAG.getConstant(Val, TLI.getPointerTy());
143 }
144};
145}
146
147
148SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
149 SelectionDAG &dag)
150 : TLI(tli), DAG(dag), ValueTypeActions(0) {
151
152 assert(MVT::LAST_VALUETYPE <= 16 &&
153 "Too many value types for ValueTypeActions to hold!");
154
155 // Inspect all of the ValueType's possible, deciding how to process them.
156 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
157 // If TLI says we are expanding this type, expand it!
158 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
159 setValueTypeAction((MVT::ValueType)IntReg, Expand);
160 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
161 // Otherwise, if we don't have native support, we must promote to a
162 // larger type.
163 setValueTypeAction((MVT::ValueType)IntReg, Promote);
164
165 // If the target does not have native support for F32, promote it to F64.
166 if (!TLI.hasNativeSupportFor(MVT::f32))
167 setValueTypeAction(MVT::f32, Promote);
168}
169
Chris Lattnerdc750592005-01-07 07:47:09 +0000170void SelectionDAGLegalize::LegalizeDAG() {
171 SDOperand OldRoot = DAG.getRoot();
172 SDOperand NewRoot = LegalizeOp(OldRoot);
173 DAG.setRoot(NewRoot);
174
175 ExpandedNodes.clear();
176 LegalizedNodes.clear();
177
178 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000179 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000180}
181
182SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
183 // If this operation defines any values that cannot be represented in a
184 // register on this target, make sure to expand it.
185 if (Op.Val->getNumValues() == 1) {// Fast path == assertion only
186 assert(getTypeAction(Op.Val->getValueType(0)) == Legal &&
187 "For a single use value, caller should check for legality!");
188 } else {
189 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
190 switch (getTypeAction(Op.Val->getValueType(i))) {
191 case Legal: break; // Nothing to do.
192 case Expand: {
193 SDOperand T1, T2;
194 ExpandOp(Op.getValue(i), T1, T2);
195 assert(LegalizedNodes.count(Op) &&
196 "Expansion didn't add legal operands!");
197 return LegalizedNodes[Op];
198 }
199 case Promote:
200 // FIXME: Implement promotion!
201 assert(0 && "Promotion not implemented at all yet!");
202 }
203 }
204
205 // If there is more than one use of this, see if we already legalized it.
206 // There is no use remembering values that only have a single use, as the map
207 // entries will never be reused.
208 if (!Op.Val->hasOneUse()) {
209 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
210 if (I != LegalizedNodes.end()) return I->second;
211 }
212
213 SDOperand Tmp1, Tmp2;
214
215 SDOperand Result = Op;
216 SDNode *Node = Op.Val;
217 LegalizeAction Action;
218
219 switch (Node->getOpcode()) {
220 default:
221 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
222 assert(0 && "Do not know how to legalize this operator!");
223 abort();
224 case ISD::EntryToken:
225 case ISD::FrameIndex:
226 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000227 case ISD::ExternalSymbol:
Chris Lattnerdc750592005-01-07 07:47:09 +0000228 case ISD::ConstantPool:
229 case ISD::CopyFromReg: // Nothing to do.
230 assert(getTypeAction(Node->getValueType(0)) == Legal &&
231 "This must be legal!");
232 break;
233 case ISD::Constant:
234 // We know we don't need to expand constants here, constants only have one
235 // value and we check that it is fine above.
236
237 // FIXME: Maybe we should handle things like targets that don't support full
238 // 32-bit immediates?
239 break;
240 case ISD::ConstantFP: {
241 // Spill FP immediates to the constant pool if the target cannot directly
242 // codegen them. Targets often have some immediate values that can be
243 // efficiently generated into an FP register without a load. We explicitly
244 // leave these constants as ConstantFP nodes for the target to deal with.
245
246 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
247
248 // Check to see if this FP immediate is already legal.
249 bool isLegal = false;
250 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
251 E = TLI.legal_fpimm_end(); I != E; ++I)
252 if (CFP->isExactlyValue(*I)) {
253 isLegal = true;
254 break;
255 }
256
257 if (!isLegal) {
258 // Otherwise we need to spill the constant to memory.
259 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
260
261 bool Extend = false;
262
263 // If a FP immediate is precise when represented as a float, we put it
264 // into the constant pool as a float, even if it's is statically typed
265 // as a double.
266 MVT::ValueType VT = CFP->getValueType(0);
267 bool isDouble = VT == MVT::f64;
268 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
269 Type::FloatTy, CFP->getValue());
270 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
271 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
272 VT = MVT::f32;
273 Extend = true;
274 }
275
276 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
277 TLI.getPointerTy());
278 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
279
280 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
281 }
282 break;
283 }
284 case ISD::ADJCALLSTACKDOWN:
285 case ISD::ADJCALLSTACKUP:
286 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
287 // There is no need to legalize the size argument (Operand #1)
288 if (Tmp1 != Node->getOperand(0))
289 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
290 Node->getOperand(1));
291 break;
292 case ISD::CALL:
293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
294 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfa854eb2005-01-07 21:35:32 +0000295 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000296 std::vector<MVT::ValueType> RetTyVTs;
297 RetTyVTs.reserve(Node->getNumValues());
298 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000299 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000300 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo);
301 }
302 break;
303
Chris Lattner68a12142005-01-07 22:12:08 +0000304 case ISD::BR:
305 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
306 if (Tmp1 != Node->getOperand(0))
307 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
308 break;
309
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000310 case ISD::BRCOND:
311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
312 // FIXME: booleans might not be legal!
313 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
314 // Basic block destination (Op#2) is always legal.
315 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
316 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
317 Node->getOperand(2));
318 break;
319
Chris Lattnerdc750592005-01-07 07:47:09 +0000320 case ISD::LOAD:
321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
323 if (Tmp1 != Node->getOperand(0) ||
324 Tmp2 != Node->getOperand(1))
325 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
326 break;
327
328 case ISD::EXTRACT_ELEMENT:
329 // Get both the low and high parts.
330 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
331 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
332 Result = Tmp2; // 1 -> Hi
333 else
334 Result = Tmp1; // 0 -> Lo
335 break;
336
337 case ISD::CopyToReg:
338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
339
340 switch (getTypeAction(Node->getOperand(1).getValueType())) {
341 case Legal:
342 // Legalize the incoming value (must be legal).
343 Tmp2 = LegalizeOp(Node->getOperand(1));
344 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
345 Result = DAG.getCopyToReg(Tmp1, Tmp2,
346 cast<CopyRegSDNode>(Node)->getReg());
347 break;
348 case Expand: {
349 SDOperand Lo, Hi;
350 ExpandOp(Node->getOperand(1), Lo, Hi);
351 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
352 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
353 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
354 assert(isTypeLegal(Result.getValueType()) &&
355 "Cannot expand multiple times yet (i64 -> i16)");
356 break;
357 }
358 case Promote:
359 assert(0 && "Don't know what it means to promote this!");
360 abort();
361 }
362 break;
363
364 case ISD::RET:
365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
366 switch (Node->getNumOperands()) {
367 case 2: // ret val
368 switch (getTypeAction(Node->getOperand(1).getValueType())) {
369 case Legal:
370 Tmp2 = LegalizeOp(Node->getOperand(1));
371 if (Tmp2 != Node->getOperand(1))
372 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
373 break;
374 case Expand: {
375 SDOperand Lo, Hi;
376 ExpandOp(Node->getOperand(1), Lo, Hi);
377 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
378 break;
379 }
380 case Promote:
381 assert(0 && "Can't promote return value!");
382 }
383 break;
384 case 1: // ret void
385 if (Tmp1 != Node->getOperand(0))
386 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
387 break;
388 default: { // ret <values>
389 std::vector<SDOperand> NewValues;
390 NewValues.push_back(Tmp1);
391 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
392 switch (getTypeAction(Node->getOperand(i).getValueType())) {
393 case Legal:
394 NewValues.push_back(LegalizeOp(Node->getOperand(1)));
395 break;
396 case Expand: {
397 SDOperand Lo, Hi;
398 ExpandOp(Node->getOperand(i), Lo, Hi);
399 NewValues.push_back(Lo);
400 NewValues.push_back(Hi);
401 break;
402 }
403 case Promote:
404 assert(0 && "Can't promote return value!");
405 }
406 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
407 break;
408 }
409 }
410 break;
411 case ISD::STORE:
412 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
413 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
414
415 switch (getTypeAction(Node->getOperand(1).getValueType())) {
416 case Legal: {
417 SDOperand Val = LegalizeOp(Node->getOperand(1));
418 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
419 Tmp2 != Node->getOperand(2))
420 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
421 break;
422 }
423 case Promote:
424 assert(0 && "FIXME: promote for stores not implemented!");
425 case Expand:
426 SDOperand Lo, Hi;
427 ExpandOp(Node->getOperand(1), Lo, Hi);
428
429 if (!TLI.isLittleEndian())
430 std::swap(Lo, Hi);
431
432 // FIXME: These two stores are independent of each other!
433 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
434
435 unsigned IncrementSize;
436 switch (Lo.getValueType()) {
437 default: assert(0 && "Unknown ValueType to expand to!");
438 case MVT::i32: IncrementSize = 4; break;
439 case MVT::i16: IncrementSize = 2; break;
440 case MVT::i8: IncrementSize = 1; break;
441 }
442 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
443 getIntPtrConstant(IncrementSize));
444 assert(isTypeLegal(Tmp2.getValueType()) &&
445 "Pointers must be legal!");
446 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
447 }
448 break;
449 case ISD::SELECT: {
450 // FIXME: BOOLS MAY REQUIRE PROMOTION!
451 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
452 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
453 SDOperand Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
454
455 if (Tmp1 != Node->getOperand(0) ||
456 Tmp2 != Node->getOperand(1) ||
457 Tmp3 != Node->getOperand(2))
458 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
459 break;
460 }
461 case ISD::SETCC:
462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
463 case Legal:
464 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
465 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
466 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
467 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
468 Tmp1, Tmp2);
469 break;
470 case Promote:
471 assert(0 && "Can't promote setcc operands yet!");
472 break;
473 case Expand:
474 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
475 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
476 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
477 switch (cast<SetCCSDNode>(Node)->getCondition()) {
478 case ISD::SETEQ:
479 case ISD::SETNE:
480 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
481 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
482 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
483 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
484 DAG.getConstant(0, Tmp1.getValueType()));
485 break;
486 default:
487 // FIXME: This generated code sucks.
488 ISD::CondCode LowCC;
489 switch (cast<SetCCSDNode>(Node)->getCondition()) {
490 default: assert(0 && "Unknown integer setcc!");
491 case ISD::SETLT:
492 case ISD::SETULT: LowCC = ISD::SETULT; break;
493 case ISD::SETGT:
494 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
495 case ISD::SETLE:
496 case ISD::SETULE: LowCC = ISD::SETULE; break;
497 case ISD::SETGE:
498 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
499 }
500
501 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
502 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
503 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
504
505 // NOTE: on targets without efficient SELECT of bools, we can always use
506 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
507 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
508 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
509 LHSHi, RHSHi);
510 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
511 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
512 break;
513 }
514 }
515 break;
516
517 case ISD::ADD:
518 case ISD::SUB:
519 case ISD::MUL:
520 case ISD::UDIV:
521 case ISD::SDIV:
522 case ISD::UREM:
523 case ISD::SREM:
524 case ISD::AND:
525 case ISD::OR:
526 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000527 case ISD::SHL:
528 case ISD::SRL:
529 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000530 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
531 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
532 if (Tmp1 != Node->getOperand(0) ||
533 Tmp2 != Node->getOperand(1))
534 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
535 break;
536 case ISD::ZERO_EXTEND:
537 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000538 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000539 case ISD::FP_EXTEND:
540 case ISD::FP_ROUND:
Chris Lattnerdc750592005-01-07 07:47:09 +0000541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
542 case Legal:
543 Tmp1 = LegalizeOp(Node->getOperand(0));
544 if (Tmp1 != Node->getOperand(0))
545 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
546 break;
547 default:
548 assert(0 && "Do not know how to expand or promote this yet!");
549 }
550 break;
551 }
552
553 if (!Op.Val->hasOneUse()) {
554 bool isNew = LegalizedNodes.insert(std::make_pair(Op, Result)).second;
555 assert(isNew && "Got into the map somehow?");
556 }
557
558 return Result;
559}
560
561
562/// ExpandOp - Expand the specified SDOperand into its two component pieces
563/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
564/// LegalizeNodes map is filled in for any results that are not expanded, the
565/// ExpandedNodes map is filled in for any results that are expanded, and the
566/// Lo/Hi values are returned.
567void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
568 MVT::ValueType VT = Op.getValueType();
569 MVT::ValueType NVT = TransformToType[VT];
570 SDNode *Node = Op.Val;
571 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
572 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
573 assert(MVT::isInteger(NVT) && NVT < VT &&
574 "Cannot expand to FP value or to larger int value!");
575
576 // If there is more than one use of this, see if we already expanded it.
577 // There is no use remembering values that only have a single use, as the map
578 // entries will never be reused.
579 if (!Node->hasOneUse()) {
580 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
581 = ExpandedNodes.find(Op);
582 if (I != ExpandedNodes.end()) {
583 Lo = I->second.first;
584 Hi = I->second.second;
585 return;
586 }
587 }
588
589 // If we are lowering to a type that the target doesn't support, we will have
590 // to iterate lowering.
591 if (!isTypeLegal(NVT))
592 NeedsAnotherIteration = true;
593
594 LegalizeAction Action;
595 switch (Node->getOpcode()) {
596 default:
597 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
598 assert(0 && "Do not know how to expand this operator!");
599 abort();
600 case ISD::Constant: {
601 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
602 Lo = DAG.getConstant(Cst, NVT);
603 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
604 break;
605 }
606
607 case ISD::CopyFromReg: {
608 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
609 // Aggregate register values are always in consequtive pairs.
610 Lo = DAG.getCopyFromReg(Reg, NVT);
611 Hi = DAG.getCopyFromReg(Reg+1, NVT);
612 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
613 break;
614 }
615
616 case ISD::LOAD: {
617 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
618 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
619 Lo = DAG.getLoad(NVT, Ch, Ptr);
620
621 // Increment the pointer to the other half.
622 unsigned IncrementSize;
623 switch (Lo.getValueType()) {
624 default: assert(0 && "Unknown ValueType to expand to!");
625 case MVT::i32: IncrementSize = 4; break;
626 case MVT::i16: IncrementSize = 2; break;
627 case MVT::i8: IncrementSize = 1; break;
628 }
629 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
630 getIntPtrConstant(IncrementSize));
631 // FIXME: This load is independent of the first one.
632 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
633
634 // Remember that we legalized the chain.
635 bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1),
636 Hi.getValue(1))).second;
637 assert(isNew && "This node was already legalized!");
638 if (!TLI.isLittleEndian())
639 std::swap(Lo, Hi);
640 break;
641 }
642 case ISD::CALL: {
643 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
644 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
645
646 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
647 "Can only expand a call once so far, not i64 -> i16!");
648
649 std::vector<MVT::ValueType> RetTyVTs;
650 RetTyVTs.reserve(3);
651 RetTyVTs.push_back(NVT);
652 RetTyVTs.push_back(NVT);
653 RetTyVTs.push_back(MVT::Other);
654 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
655 Lo = SDOperand(NC, 0);
656 Hi = SDOperand(NC, 1);
657
658 // Insert the new chain mapping.
659 bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1),
660 Hi.getValue(2))).second;
661 assert(isNew && "This node was already legalized!");
662 break;
663 }
664 case ISD::AND:
665 case ISD::OR:
666 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
667 SDOperand LL, LH, RL, RH;
668 ExpandOp(Node->getOperand(0), LL, LH);
669 ExpandOp(Node->getOperand(1), RL, RH);
670 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
671 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
672 break;
673 }
674 case ISD::SELECT: {
675 SDOperand C, LL, LH, RL, RH;
676 // FIXME: BOOLS MAY REQUIRE PROMOTION!
677 C = LegalizeOp(Node->getOperand(0));
678 ExpandOp(Node->getOperand(1), LL, LH);
679 ExpandOp(Node->getOperand(2), RL, RH);
680 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
681 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
682 break;
683 }
684 case ISD::SIGN_EXTEND: {
685 // The low part is just a sign extension of the input (which degenerates to
686 // a copy).
687 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
688
689 // The high part is obtained by SRA'ing all but one of the bits of the lo
690 // part.
691 unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType());
692 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8));
693 break;
694 }
695 case ISD::ZERO_EXTEND:
696 // The low part is just a zero extension of the input (which degenerates to
697 // a copy).
698 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
699
700 // The high part is just a zero.
701 Hi = DAG.getConstant(0, NVT);
702 break;
703 }
704
705 // Remember in a map if the values will be reused later.
706 if (!Node->hasOneUse()) {
707 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
708 std::make_pair(Lo, Hi))).second;
709 assert(isNew && "Value already expanded?!?");
710 }
711}
712
713
714// SelectionDAG::Legalize - This is the entry point for the file.
715//
716void SelectionDAG::Legalize(TargetLowering &TLI) {
717 /// run - This is the main entry point to this class.
718 ///
719 SelectionDAGLegalize(TLI, *this).Run();
720}
721