1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SelectionDAG::Legalize method.
12 //===----------------------------------------------------------------------===//
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/Target/TargetData.h"
19 #include "llvm/Constants.h"
23 static const Type *getTypeFor(MVT::ValueType VT) {
25 default: assert(0 && "Unknown MVT!");
26 case MVT::i1: return Type::BoolTy;
27 case MVT::i8: return Type::UByteTy;
28 case MVT::i16: return Type::UShortTy;
29 case MVT::i32: return Type::UIntTy;
30 case MVT::i64: return Type::ULongTy;
31 case MVT::f32: return Type::FloatTy;
32 case MVT::f64: return Type::DoubleTy;
37 //===----------------------------------------------------------------------===//
38 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
39 /// hacks on it until the target machine can handle it. This involves
40 /// eliminating value sizes the machine cannot handle (promoting small sizes to
41 /// large sizes or splitting up large values into small values) as well as
42 /// eliminating operations the machine cannot handle.
44 /// This code also does a small amount of optimization and recognition of idioms
45 /// as part of its processing. For example, if a target does not support a
46 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
47 /// will attempt merge setcc and brc instructions into brcc's.
50 class SelectionDAGLegalize {
54 /// LegalizeAction - This enum indicates what action we should take for each
55 /// value type the can occur in the program.
57 Legal, // The target natively supports this value type.
58 Promote, // This should be promoted to the next larger type.
59 Expand, // This integer type should be broken into smaller pieces.
62 /// TransformToType - For any value types we are promoting or expanding, this
63 /// contains the value type that we are changing to. For Expanded types, this
64 /// contains one step of the expand (e.g. i64 -> i32), even if there are
65 /// multiple steps required (e.g. i64 -> i16)
66 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
68 /// ValueTypeActions - This is a bitvector that contains two bits for each
69 /// value type, where the two bits correspond to the LegalizeAction enum.
70 /// This can be queried with "getTypeAction(VT)".
71 unsigned ValueTypeActions;
73 /// NeedsAnotherIteration - This is set when we expand a large integer
74 /// operation into smaller integer operations, but the smaller operations are
75 /// not set. This occurs only rarely in practice, for targets that don't have
76 /// 32-bit or larger integer registers.
77 bool NeedsAnotherIteration;
79 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
82 std::map<SDOperand, SDOperand> LegalizedNodes;
84 /// ExpandedNodes - For nodes that need to be expanded, and which have more
85 /// than one use, this map indicates which which operands are the expanded
86 /// version of the input. This allows us to avoid expanding the same node
88 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
90 void AddLegalizedOperand(SDOperand From, SDOperand To) {
91 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
92 assert(isNew && "Got into the map somehow?");
95 /// setValueTypeAction - Set the action for a particular value type. This
96 /// assumes an action has not already been set for this value type.
97 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
98 ValueTypeActions |= A << (VT*2);
100 MVT::ValueType PromoteTo;
102 PromoteTo = MVT::f64;
104 unsigned LargerReg = VT+1;
105 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
107 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
108 "Nothing to promote to??");
110 PromoteTo = (MVT::ValueType)LargerReg;
113 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
114 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
115 "Can only promote from int->int or fp->fp!");
116 assert(VT < PromoteTo && "Must promote to a larger type!");
117 TransformToType[VT] = PromoteTo;
118 } else if (A == Expand) {
119 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
120 "Cannot expand this type: target must support SOME integer reg!");
121 // Expand to the next smaller integer type!
122 TransformToType[VT] = (MVT::ValueType)(VT-1);
128 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
130 /// Run - While there is still lowering to do, perform a pass over the DAG.
131 /// Most regularization can be done in a single pass, but targets that require
132 /// large values to be split into registers multiple times (e.g. i64 -> 4x
133 /// i16) require iteration for these values (the first iteration will demote
134 /// to i32, the second will demote to i16).
137 NeedsAnotherIteration = false;
139 } while (NeedsAnotherIteration);
142 /// getTypeAction - Return how we should legalize values of this type, either
143 /// it is already legal or we need to expand it into multiple registers of
144 /// smaller integer type, or we need to promote it to a larger type.
145 LegalizeAction getTypeAction(MVT::ValueType VT) const {
146 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
149 /// isTypeLegal - Return true if this type is legal on this target.
151 bool isTypeLegal(MVT::ValueType VT) const {
152 return getTypeAction(VT) == Legal;
158 SDOperand LegalizeOp(SDOperand O);
159 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
161 SDOperand getIntPtrConstant(uint64_t Val) {
162 return DAG.getConstant(Val, TLI.getPointerTy());
168 SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
170 : TLI(tli), DAG(dag), ValueTypeActions(0) {
172 assert(MVT::LAST_VALUETYPE <= 16 &&
173 "Too many value types for ValueTypeActions to hold!");
175 // Inspect all of the ValueType's possible, deciding how to process them.
176 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
177 // If TLI says we are expanding this type, expand it!
178 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
179 setValueTypeAction((MVT::ValueType)IntReg, Expand);
180 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
181 // Otherwise, if we don't have native support, we must promote to a
183 setValueTypeAction((MVT::ValueType)IntReg, Promote);
185 // If the target does not have native support for F32, promote it to F64.
186 if (!TLI.hasNativeSupportFor(MVT::f32))
187 setValueTypeAction(MVT::f32, Promote);
190 void SelectionDAGLegalize::LegalizeDAG() {
191 SDOperand OldRoot = DAG.getRoot();
192 SDOperand NewRoot = LegalizeOp(OldRoot);
193 DAG.setRoot(NewRoot);
195 ExpandedNodes.clear();
196 LegalizedNodes.clear();
198 // Remove dead nodes now.
199 DAG.RemoveDeadNodes(OldRoot.Val);
202 SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
203 assert(getTypeAction(Op.getValueType()) == Legal &&
204 "Caller should expand or promote operands that are not legal!");
206 // If this operation defines any values that cannot be represented in a
207 // register on this target, make sure to expand or promote them.
208 if (Op.Val->getNumValues() > 1) {
209 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
210 switch (getTypeAction(Op.Val->getValueType(i))) {
211 case Legal: break; // Nothing to do.
214 ExpandOp(Op.getValue(i), T1, T2);
215 assert(LegalizedNodes.count(Op) &&
216 "Expansion didn't add legal operands!");
217 return LegalizedNodes[Op];
220 // FIXME: Implement promotion!
221 assert(0 && "Promotion not implemented at all yet!");
225 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
226 if (I != LegalizedNodes.end()) return I->second;
228 SDOperand Tmp1, Tmp2, Tmp3;
230 SDOperand Result = Op;
231 SDNode *Node = Op.Val;
233 switch (Node->getOpcode()) {
235 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
236 assert(0 && "Do not know how to legalize this operator!");
238 case ISD::EntryToken:
239 case ISD::FrameIndex:
240 case ISD::GlobalAddress:
241 case ISD::ExternalSymbol:
242 case ISD::ConstantPool:
243 case ISD::CopyFromReg: // Nothing to do.
244 assert(getTypeAction(Node->getValueType(0)) == Legal &&
245 "This must be legal!");
248 // We know we don't need to expand constants here, constants only have one
249 // value and we check that it is fine above.
251 // FIXME: Maybe we should handle things like targets that don't support full
252 // 32-bit immediates?
254 case ISD::ConstantFP: {
255 // Spill FP immediates to the constant pool if the target cannot directly
256 // codegen them. Targets often have some immediate values that can be
257 // efficiently generated into an FP register without a load. We explicitly
258 // leave these constants as ConstantFP nodes for the target to deal with.
260 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
262 // Check to see if this FP immediate is already legal.
263 bool isLegal = false;
264 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
265 E = TLI.legal_fpimm_end(); I != E; ++I)
266 if (CFP->isExactlyValue(*I)) {
272 // Otherwise we need to spill the constant to memory.
273 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
277 // If a FP immediate is precise when represented as a float, we put it
278 // into the constant pool as a float, even if it's is statically typed
280 MVT::ValueType VT = CFP->getValueType(0);
281 bool isDouble = VT == MVT::f64;
282 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
283 Type::FloatTy, CFP->getValue());
284 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
285 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
290 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
292 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
294 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
298 case ISD::ADJCALLSTACKDOWN:
299 case ISD::ADJCALLSTACKUP:
300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
301 // There is no need to legalize the size argument (Operand #1)
302 if (Tmp1 != Node->getOperand(0))
303 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
304 Node->getOperand(1));
306 case ISD::DYNAMIC_STACKALLOC:
307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
308 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
309 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
310 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
311 Tmp3 != Node->getOperand(2))
312 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
315 Result = Op.getValue(0);
317 // Since this op produces two values, make sure to remember that we
318 // legalized both of them.
319 AddLegalizedOperand(SDOperand(Node, 0), Result);
320 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
321 return Result.getValue(Op.ResNo);
324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
325 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
326 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
327 std::vector<MVT::ValueType> RetTyVTs;
328 RetTyVTs.reserve(Node->getNumValues());
329 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
330 RetTyVTs.push_back(Node->getValueType(i));
331 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0);
333 Result = Result.getValue(0);
335 // Since calls produce multiple values, make sure to remember that we
336 // legalized all of them.
337 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
338 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
339 return Result.getValue(Op.ResNo);
342 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
343 if (Tmp1 != Node->getOperand(0))
344 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
349 // FIXME: booleans might not be legal!
350 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
351 // Basic block destination (Op#2) is always legal.
352 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
353 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
354 Node->getOperand(2));
358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
360 if (Tmp1 != Node->getOperand(0) ||
361 Tmp2 != Node->getOperand(1))
362 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
364 Result = SDOperand(Node, 0);
366 // Since loads produce two values, make sure to remember that we legalized
368 AddLegalizedOperand(SDOperand(Node, 0), Result);
369 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
370 return Result.getValue(Op.ResNo);
372 case ISD::EXTRACT_ELEMENT:
373 // Get both the low and high parts.
374 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
375 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
376 Result = Tmp2; // 1 -> Hi
378 Result = Tmp1; // 0 -> Lo
382 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
384 switch (getTypeAction(Node->getOperand(1).getValueType())) {
386 // Legalize the incoming value (must be legal).
387 Tmp2 = LegalizeOp(Node->getOperand(1));
388 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
389 Result = DAG.getCopyToReg(Tmp1, Tmp2,
390 cast<CopyRegSDNode>(Node)->getReg());
394 ExpandOp(Node->getOperand(1), Lo, Hi);
395 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
396 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
397 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
398 assert(isTypeLegal(Result.getValueType()) &&
399 "Cannot expand multiple times yet (i64 -> i16)");
403 assert(0 && "Don't know what it means to promote this!");
409 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
410 switch (Node->getNumOperands()) {
412 switch (getTypeAction(Node->getOperand(1).getValueType())) {
414 Tmp2 = LegalizeOp(Node->getOperand(1));
415 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
416 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
420 ExpandOp(Node->getOperand(1), Lo, Hi);
421 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
425 assert(0 && "Can't promote return value!");
429 if (Tmp1 != Node->getOperand(0))
430 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
432 default: { // ret <values>
433 std::vector<SDOperand> NewValues;
434 NewValues.push_back(Tmp1);
435 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
436 switch (getTypeAction(Node->getOperand(i).getValueType())) {
438 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
442 ExpandOp(Node->getOperand(i), Lo, Hi);
443 NewValues.push_back(Lo);
444 NewValues.push_back(Hi);
448 assert(0 && "Can't promote return value!");
450 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
456 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
457 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
459 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
460 if (ConstantFPSDNode *CFP =
461 dyn_cast<ConstantFPSDNode>(Node->getOperand(1))) {
462 if (CFP->getValueType(0) == MVT::f32) {
467 V.F = CFP->getValue();
468 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
469 DAG.getConstant(V.I, MVT::i32), Tmp2);
471 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
476 V.F = CFP->getValue();
477 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
478 DAG.getConstant(V.I, MVT::i64), Tmp2);
484 switch (getTypeAction(Node->getOperand(1).getValueType())) {
486 SDOperand Val = LegalizeOp(Node->getOperand(1));
487 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
488 Tmp2 != Node->getOperand(2))
489 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
493 assert(0 && "FIXME: promote for stores not implemented!");
496 ExpandOp(Node->getOperand(1), Lo, Hi);
498 if (!TLI.isLittleEndian())
501 // FIXME: These two stores are independent of each other!
502 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
504 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
505 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
506 getIntPtrConstant(IncrementSize));
507 assert(isTypeLegal(Tmp2.getValueType()) &&
508 "Pointers must be legal!");
509 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
513 // FIXME: BOOLS MAY REQUIRE PROMOTION!
514 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
515 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
516 SDOperand Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
518 if (Tmp1 != Node->getOperand(0) ||
519 Tmp2 != Node->getOperand(1) ||
520 Tmp3 != Node->getOperand(2))
521 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
525 switch (getTypeAction(Node->getOperand(0).getValueType())) {
527 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
528 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
529 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
530 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
534 assert(0 && "Can't promote setcc operands yet!");
537 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
538 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
539 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
540 switch (cast<SetCCSDNode>(Node)->getCondition()) {
543 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
544 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
545 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
546 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
547 DAG.getConstant(0, Tmp1.getValueType()));
550 // FIXME: This generated code sucks.
552 switch (cast<SetCCSDNode>(Node)->getCondition()) {
553 default: assert(0 && "Unknown integer setcc!");
555 case ISD::SETULT: LowCC = ISD::SETULT; break;
557 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
559 case ISD::SETULE: LowCC = ISD::SETULE; break;
561 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
564 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
565 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
566 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
568 // NOTE: on targets without efficient SELECT of bools, we can always use
569 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
570 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
571 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
573 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
574 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
583 Tmp1 = LegalizeOp(Node->getOperand(0));
584 Tmp2 = LegalizeOp(Node->getOperand(1));
585 Tmp3 = LegalizeOp(Node->getOperand(2));
586 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
587 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
588 if (TLI.isOperationSupported(Node->getOpcode(), MVT::Other)) {
589 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
590 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
591 Tmp5 != Node->getOperand(4)) {
592 std::vector<SDOperand> Ops;
593 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
594 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
595 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
598 // Otherwise, the target does not support this operation. Lower the
599 // operation to an explicit libcall as appropriate.
600 MVT::ValueType IntPtr = TLI.getPointerTy();
601 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
602 std::vector<std::pair<SDOperand, const Type*> > Args;
605 if (Node->getOpcode() == ISD::MEMSET) {
606 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
607 // Extend the ubyte argument to be an int value for the call.
608 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
609 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
610 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
613 } else if (Node->getOpcode() == ISD::MEMCPY ||
614 Node->getOpcode() == ISD::MEMMOVE) {
615 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
616 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
617 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
618 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
620 assert(0 && "Unknown op!");
622 std::pair<SDOperand,SDOperand> CallResult =
623 TLI.LowerCallTo(Tmp1, Type::VoidTy,
624 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
625 Result = LegalizeOp(CallResult.second);
642 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
643 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
644 if (Tmp1 != Node->getOperand(0) ||
645 Tmp2 != Node->getOperand(1))
646 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
648 case ISD::ZERO_EXTEND:
649 case ISD::SIGN_EXTEND:
653 case ISD::FP_TO_SINT:
654 case ISD::FP_TO_UINT:
655 case ISD::SINT_TO_FP:
656 case ISD::UINT_TO_FP:
658 switch (getTypeAction(Node->getOperand(0).getValueType())) {
660 Tmp1 = LegalizeOp(Node->getOperand(0));
661 if (Tmp1 != Node->getOperand(0))
662 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
665 // In the expand case, we must be dealing with a truncate, because
666 // otherwise the result would be larger than the source.
667 assert(Node->getOpcode() == ISD::TRUNCATE &&
668 "Shouldn't need to expand other operators here!");
669 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
671 // Since the result is legal, we should just be able to truncate the low
672 // part of the source.
673 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
677 assert(0 && "Do not know how to promote this yet!");
682 if (!Op.Val->hasOneUse())
683 AddLegalizedOperand(Op, Result);
689 /// ExpandOp - Expand the specified SDOperand into its two component pieces
690 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
691 /// LegalizeNodes map is filled in for any results that are not expanded, the
692 /// ExpandedNodes map is filled in for any results that are expanded, and the
693 /// Lo/Hi values are returned.
694 void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
695 MVT::ValueType VT = Op.getValueType();
696 MVT::ValueType NVT = TransformToType[VT];
697 SDNode *Node = Op.Val;
698 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
699 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
700 assert(MVT::isInteger(NVT) && NVT < VT &&
701 "Cannot expand to FP value or to larger int value!");
703 // If there is more than one use of this, see if we already expanded it.
704 // There is no use remembering values that only have a single use, as the map
705 // entries will never be reused.
706 if (!Node->hasOneUse()) {
707 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
708 = ExpandedNodes.find(Op);
709 if (I != ExpandedNodes.end()) {
710 Lo = I->second.first;
711 Hi = I->second.second;
716 // Expanding to multiple registers needs to perform an optimization step, and
717 // is not careful to avoid operations the target does not support. Make sure
718 // that all generated operations are legalized in the next iteration.
719 NeedsAnotherIteration = true;
720 const char *LibCallName = 0;
722 switch (Node->getOpcode()) {
724 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
725 assert(0 && "Do not know how to expand this operator!");
727 case ISD::Constant: {
728 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
729 Lo = DAG.getConstant(Cst, NVT);
730 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
734 case ISD::CopyFromReg: {
735 unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
736 // Aggregate register values are always in consequtive pairs.
737 Lo = DAG.getCopyFromReg(Reg, NVT);
738 Hi = DAG.getCopyFromReg(Reg+1, NVT);
739 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
744 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
745 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
746 Lo = DAG.getLoad(NVT, Ch, Ptr);
748 // Increment the pointer to the other half.
749 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
750 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
751 getIntPtrConstant(IncrementSize));
752 // FIXME: This load is independent of the first one.
753 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
755 // Remember that we legalized the chain.
756 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
757 if (!TLI.isLittleEndian())
762 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
763 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
765 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
766 "Can only expand a call once so far, not i64 -> i16!");
768 std::vector<MVT::ValueType> RetTyVTs;
770 RetTyVTs.push_back(NVT);
771 RetTyVTs.push_back(NVT);
772 RetTyVTs.push_back(MVT::Other);
773 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
774 Lo = SDOperand(NC, 0);
775 Hi = SDOperand(NC, 1);
777 // Insert the new chain mapping.
778 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
783 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
784 SDOperand LL, LH, RL, RH;
785 ExpandOp(Node->getOperand(0), LL, LH);
786 ExpandOp(Node->getOperand(1), RL, RH);
787 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
788 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
792 SDOperand C, LL, LH, RL, RH;
793 // FIXME: BOOLS MAY REQUIRE PROMOTION!
794 C = LegalizeOp(Node->getOperand(0));
795 ExpandOp(Node->getOperand(1), LL, LH);
796 ExpandOp(Node->getOperand(2), RL, RH);
797 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
798 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
801 case ISD::SIGN_EXTEND: {
802 // The low part is just a sign extension of the input (which degenerates to
804 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
806 // The high part is obtained by SRA'ing all but one of the bits of the lo
808 unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType());
809 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8));
812 case ISD::ZERO_EXTEND:
813 // The low part is just a zero extension of the input (which degenerates to
815 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
817 // The high part is just a zero.
818 Hi = DAG.getConstant(0, NVT);
821 // These operators cannot be expanded directly, emit them as calls to
822 // library functions.
823 case ISD::FP_TO_SINT:
824 if (Node->getOperand(0).getValueType() == MVT::f32)
825 LibCallName = "__fixsfdi";
827 LibCallName = "__fixdfdi";
829 case ISD::FP_TO_UINT:
830 if (Node->getOperand(0).getValueType() == MVT::f32)
831 LibCallName = "__fixunssfdi";
833 LibCallName = "__fixunsdfdi";
836 case ISD::ADD: LibCallName = "__adddi3"; break;
837 case ISD::SUB: LibCallName = "__subdi3"; break;
838 case ISD::MUL: LibCallName = "__muldi3"; break;
839 case ISD::SDIV: LibCallName = "__divdi3"; break;
840 case ISD::UDIV: LibCallName = "__udivdi3"; break;
841 case ISD::SREM: LibCallName = "__moddi3"; break;
842 case ISD::UREM: LibCallName = "__umoddi3"; break;
843 case ISD::SHL: LibCallName = "__ashldi3"; break;
844 case ISD::SRA: LibCallName = "__ashrdi3"; break;
845 case ISD::SRL: LibCallName = "__lshrdi3"; break;
848 // Int2FP -> __floatdisf/__floatdidf
850 // If this is to be expanded into a libcall... do so now.
852 TargetLowering::ArgListTy Args;
853 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
854 Args.push_back(std::make_pair(Node->getOperand(i),
855 getTypeFor(Node->getOperand(i).getValueType())));
856 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
858 // We don't care about token chains for libcalls. We just use the entry
859 // node as our input and ignore the output chain. This allows us to place
860 // calls wherever we need them to satisfy data dependences.
861 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
862 getTypeFor(Op.getValueType()), Callee,
864 ExpandOp(Result, Lo, Hi);
867 // Remember in a map if the values will be reused later.
868 if (!Node->hasOneUse()) {
869 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
870 std::make_pair(Lo, Hi))).second;
871 assert(isNew && "Value already expanded?!?");
876 // SelectionDAG::Legalize - This is the entry point for the file.
878 void SelectionDAG::Legalize(TargetLowering &TLI) {
879 /// run - This is the main entry point to this class.
881 SelectionDAGLegalize(TLI, *this).Run();