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/CodeGen/MachineFrameInfo.h"
18 #include "llvm/Target/TargetLowering.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include "llvm/Constants.h"
25 //===----------------------------------------------------------------------===//
26 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27 /// hacks on it until the target machine can handle it. This involves
28 /// eliminating value sizes the machine cannot handle (promoting small sizes to
29 /// large sizes or splitting up large values into small values) as well as
30 /// eliminating operations the machine cannot handle.
32 /// This code also does a small amount of optimization and recognition of idioms
33 /// as part of its processing. For example, if a target does not support a
34 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35 /// will attempt merge setcc and brc instructions into brcc's.
38 class SelectionDAGLegalize {
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
50 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
66 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
71 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
77 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
81 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
88 SelectionDAGLegalize(SelectionDAG &DAG);
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
97 NeedsAnotherIteration = false;
99 } while (NeedsAnotherIteration);
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 /// isTypeLegal - Return true if this type is legal on this target.
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
120 SDOperand PromoteOp(SDOperand O);
122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
131 SDOperand &Lo, SDOperand &Hi);
133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
140 SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
147 void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
154 PromotedNodes.clear();
156 // Remove dead nodes now.
157 DAG.RemoveDeadNodes(OldRoot.Val);
160 SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
164 // If this operation defines any values that cannot be represented in a
165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
188 SDOperand Tmp1, Tmp2, Tmp3;
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
193 switch (Node->getOpcode()) {
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
201 case ISD::ExternalSymbol:
202 case ISD::ConstantPool: // Nothing to do.
203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
212 Result = Op.getValue(0);
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
235 assert(0 && "Unknown value type!");
237 case TargetLowering::Legal:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
293 DAG.getSrcValue(NULL), MVT::f32);
295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
337 Result = Op.getValue(0);
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
360 RetTyVTs.push_back(Node->getValueType(i));
361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
363 Result = Result.getValue(0);
365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
394 case ISD::BRCONDTWOWAY:
395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
397 case Expand: assert(0 && "It's impossible to expand bools");
399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
405 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
407 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
408 case TargetLowering::Promote:
409 default: assert(0 && "This action is not supported yet!");
410 case TargetLowering::Legal:
411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
412 std::vector<SDOperand> Ops;
415 Ops.push_back(Node->getOperand(2));
416 Ops.push_back(Node->getOperand(3));
417 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
420 case TargetLowering::Expand:
421 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
422 Node->getOperand(2));
423 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
432 if (Tmp1 != Node->getOperand(0) ||
433 Tmp2 != Node->getOperand(1))
434 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2, Node->getOperand(2));
436 Result = SDOperand(Node, 0);
438 // Since loads produce two values, make sure to remember that we legalized
440 AddLegalizedOperand(SDOperand(Node, 0), Result);
441 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
442 return Result.getValue(Op.ResNo);
446 case ISD::ZEXTLOAD: {
447 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
448 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
450 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
451 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
452 default: assert(0 && "This action is not supported yet!");
453 case TargetLowering::Promote:
454 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
455 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
456 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
457 // Since loads produce two values, make sure to remember that we legalized
459 AddLegalizedOperand(SDOperand(Node, 0), Result);
460 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
461 return Result.getValue(Op.ResNo);
463 case TargetLowering::Legal:
464 if (Tmp1 != Node->getOperand(0) ||
465 Tmp2 != Node->getOperand(1))
466 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
467 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
469 Result = SDOperand(Node, 0);
471 // Since loads produce two values, make sure to remember that we legalized
473 AddLegalizedOperand(SDOperand(Node, 0), Result);
474 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
475 return Result.getValue(Op.ResNo);
476 case TargetLowering::Expand:
477 assert(Node->getOpcode() != ISD::EXTLOAD &&
478 "EXTLOAD should always be supported!");
479 // Turn the unsupported load into an EXTLOAD followed by an explicit
480 // zero/sign extend inreg.
481 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
482 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
484 if (Node->getOpcode() == ISD::SEXTLOAD)
485 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
488 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
489 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
490 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
492 return Result.getValue(1);
495 assert(0 && "Unreachable");
497 case ISD::EXTRACT_ELEMENT:
498 // Get both the low and high parts.
499 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
500 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
501 Result = Tmp2; // 1 -> Hi
503 Result = Tmp1; // 0 -> Lo
507 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
509 switch (getTypeAction(Node->getOperand(1).getValueType())) {
511 // Legalize the incoming value (must be legal).
512 Tmp2 = LegalizeOp(Node->getOperand(1));
513 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
514 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
517 Tmp2 = PromoteOp(Node->getOperand(1));
518 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
522 ExpandOp(Node->getOperand(1), Lo, Hi);
523 unsigned Reg = cast<RegSDNode>(Node)->getReg();
524 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
525 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
526 // Note that the copytoreg nodes are independent of each other.
527 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
528 assert(isTypeLegal(Result.getValueType()) &&
529 "Cannot expand multiple times yet (i64 -> i16)");
535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
536 switch (Node->getNumOperands()) {
538 switch (getTypeAction(Node->getOperand(1).getValueType())) {
540 Tmp2 = LegalizeOp(Node->getOperand(1));
541 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
542 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
546 ExpandOp(Node->getOperand(1), Lo, Hi);
547 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
551 Tmp2 = PromoteOp(Node->getOperand(1));
552 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
557 if (Tmp1 != Node->getOperand(0))
558 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
560 default: { // ret <values>
561 std::vector<SDOperand> NewValues;
562 NewValues.push_back(Tmp1);
563 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
564 switch (getTypeAction(Node->getOperand(i).getValueType())) {
566 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
570 ExpandOp(Node->getOperand(i), Lo, Hi);
571 NewValues.push_back(Lo);
572 NewValues.push_back(Hi);
576 assert(0 && "Can't promote multiple return value yet!");
578 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
584 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
585 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
587 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
588 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
589 if (CFP->getValueType(0) == MVT::f32) {
594 V.F = CFP->getValue();
595 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
596 DAG.getConstant(V.I, MVT::i32), Tmp2, Node->getOperand(3));
598 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
603 V.F = CFP->getValue();
604 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
605 DAG.getConstant(V.I, MVT::i64), Tmp2, Node->getOperand(3));
610 switch (getTypeAction(Node->getOperand(1).getValueType())) {
612 SDOperand Val = LegalizeOp(Node->getOperand(1));
613 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
614 Tmp2 != Node->getOperand(2))
615 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2, Node->getOperand(3));
619 // Truncate the value and store the result.
620 Tmp3 = PromoteOp(Node->getOperand(1));
621 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
623 Node->getOperand(1).getValueType());
628 ExpandOp(Node->getOperand(1), Lo, Hi);
630 if (!TLI.isLittleEndian())
633 Lo = DAG.getNode(ISD::STORE, MVT::Other,Tmp1, Lo, Tmp2,Node->getOperand(3));
635 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
636 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
637 getIntPtrConstant(IncrementSize));
638 assert(isTypeLegal(Tmp2.getValueType()) &&
639 "Pointers must be legal!");
640 //Again, claiming both parts of the store came form the same Instr
641 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, Node->getOperand(3));
643 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
649 if (Tmp1 != Node->getOperand(0))
650 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
652 case ISD::TRUNCSTORE:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
654 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
656 switch (getTypeAction(Node->getOperand(1).getValueType())) {
658 Tmp2 = LegalizeOp(Node->getOperand(1));
659 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
660 Tmp3 != Node->getOperand(2))
661 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
663 cast<MVTSDNode>(Node)->getExtraValueType());
667 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
671 switch (getTypeAction(Node->getOperand(0).getValueType())) {
672 case Expand: assert(0 && "It's impossible to expand bools");
674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
677 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
680 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
681 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
683 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
684 default: assert(0 && "This action is not supported yet!");
685 case TargetLowering::Legal:
686 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
687 Tmp3 != Node->getOperand(2))
688 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
691 case TargetLowering::Promote: {
693 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
694 unsigned ExtOp, TruncOp;
695 if (MVT::isInteger(Tmp2.getValueType())) {
696 ExtOp = ISD::ZERO_EXTEND;
697 TruncOp = ISD::TRUNCATE;
699 ExtOp = ISD::FP_EXTEND;
700 TruncOp = ISD::FP_ROUND;
702 // Promote each of the values to the new type.
703 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
704 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
705 // Perform the larger operation, then round down.
706 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
707 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
713 switch (getTypeAction(Node->getOperand(0).getValueType())) {
715 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
716 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
717 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
718 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
719 Node->getValueType(0), Tmp1, Tmp2);
722 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
723 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
725 // If this is an FP compare, the operands have already been extended.
726 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
727 MVT::ValueType VT = Node->getOperand(0).getValueType();
728 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
730 // Otherwise, we have to insert explicit sign or zero extends. Note
731 // that we could insert sign extends for ALL conditions, but zero extend
732 // is cheaper on many machines (an AND instead of two shifts), so prefer
734 switch (cast<SetCCSDNode>(Node)->getCondition()) {
735 default: assert(0 && "Unknown integer comparison!");
742 // ALL of these operations will work if we either sign or zero extend
743 // the operands (including the unsigned comparisons!). Zero extend is
744 // usually a simpler/cheaper operation, so prefer it.
745 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
746 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
752 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
753 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
758 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
759 Node->getValueType(0), Tmp1, Tmp2);
762 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
763 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
764 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
765 switch (cast<SetCCSDNode>(Node)->getCondition()) {
769 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
770 if (RHSCST->isAllOnesValue()) {
772 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
773 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
774 Node->getValueType(0), Tmp1, RHSLo);
778 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
779 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
780 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
781 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
782 Node->getValueType(0), Tmp1,
783 DAG.getConstant(0, Tmp1.getValueType()));
786 // If this is a comparison of the sign bit, just look at the top part.
788 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
789 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
790 CST->getValue() == 0) || // X < 0
791 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
792 (CST->isAllOnesValue()))) // X > -1
793 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
794 Node->getValueType(0), LHSHi, RHSHi);
796 // FIXME: This generated code sucks.
798 switch (cast<SetCCSDNode>(Node)->getCondition()) {
799 default: assert(0 && "Unknown integer setcc!");
801 case ISD::SETULT: LowCC = ISD::SETULT; break;
803 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
805 case ISD::SETULE: LowCC = ISD::SETULE; break;
807 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
810 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
811 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
812 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
814 // NOTE: on targets without efficient SELECT of bools, we can always use
815 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
816 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
817 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
818 Node->getValueType(0), LHSHi, RHSHi);
819 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
820 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
830 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
831 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
833 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
834 switch (getTypeAction(Node->getOperand(2).getValueType())) {
835 case Expand: assert(0 && "Cannot expand a byte!");
837 Tmp3 = LegalizeOp(Node->getOperand(2));
840 Tmp3 = PromoteOp(Node->getOperand(2));
844 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
848 switch (getTypeAction(Node->getOperand(3).getValueType())) {
849 case Expand: assert(0 && "Cannot expand this yet!");
851 Tmp4 = LegalizeOp(Node->getOperand(3));
854 Tmp4 = PromoteOp(Node->getOperand(3));
859 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
860 case Expand: assert(0 && "Cannot expand this yet!");
862 Tmp5 = LegalizeOp(Node->getOperand(4));
865 Tmp5 = PromoteOp(Node->getOperand(4));
869 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
870 default: assert(0 && "This action not implemented for this operation!");
871 case TargetLowering::Legal:
872 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
873 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
874 Tmp5 != Node->getOperand(4)) {
875 std::vector<SDOperand> Ops;
876 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
877 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
878 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
881 case TargetLowering::Expand: {
882 // Otherwise, the target does not support this operation. Lower the
883 // operation to an explicit libcall as appropriate.
884 MVT::ValueType IntPtr = TLI.getPointerTy();
885 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
886 std::vector<std::pair<SDOperand, const Type*> > Args;
888 const char *FnName = 0;
889 if (Node->getOpcode() == ISD::MEMSET) {
890 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
891 // Extend the ubyte argument to be an int value for the call.
892 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
893 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
894 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
897 } else if (Node->getOpcode() == ISD::MEMCPY ||
898 Node->getOpcode() == ISD::MEMMOVE) {
899 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
900 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
901 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
902 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
904 assert(0 && "Unknown op!");
906 std::pair<SDOperand,SDOperand> CallResult =
907 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
908 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
909 Result = LegalizeOp(CallResult.second);
912 case TargetLowering::Custom:
913 std::vector<SDOperand> Ops;
914 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
915 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
916 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
917 Result = TLI.LowerOperation(Result);
918 Result = LegalizeOp(Result);
927 case ISD::SRL_PARTS: {
928 std::vector<SDOperand> Ops;
929 bool Changed = false;
930 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
931 Ops.push_back(LegalizeOp(Node->getOperand(i)));
932 Changed |= Ops.back() != Node->getOperand(i);
935 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
937 // Since these produce multiple values, make sure to remember that we
938 // legalized all of them.
939 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
940 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
941 return Result.getValue(Op.ResNo);
958 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
960 if (Tmp1 != Node->getOperand(0) ||
961 Tmp2 != Node->getOperand(1))
962 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
967 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
968 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
969 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
970 case TargetLowering::Legal:
971 if (Tmp1 != Node->getOperand(0) ||
972 Tmp2 != Node->getOperand(1))
973 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
976 case TargetLowering::Promote:
977 case TargetLowering::Custom:
978 assert(0 && "Cannot promote/custom handle this yet!");
979 case TargetLowering::Expand: {
980 MVT::ValueType VT = Node->getValueType(0);
981 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
982 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
983 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
984 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
996 Tmp1 = LegalizeOp(Node->getOperand(0));
997 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
998 case TargetLowering::Legal:
999 if (Tmp1 != Node->getOperand(0))
1000 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1002 case TargetLowering::Promote:
1003 case TargetLowering::Custom:
1004 assert(0 && "Cannot promote/custom handle this yet!");
1005 case TargetLowering::Expand:
1006 switch(Node->getOpcode()) {
1008 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1009 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1010 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1015 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1016 MVT::ValueType VT = Node->getValueType(0);
1017 Tmp2 = DAG.getConstantFP(0.0, VT);
1018 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1019 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1020 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1021 Result = LegalizeOp(Result);
1027 MVT::ValueType VT = Node->getValueType(0);
1028 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1029 const char *FnName = 0;
1030 switch(Node->getOpcode()) {
1031 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1032 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1033 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1034 default: assert(0 && "Unreachable!");
1036 std::vector<std::pair<SDOperand, const Type*> > Args;
1037 Args.push_back(std::make_pair(Tmp1, T));
1038 std::pair<SDOperand,SDOperand> CallResult =
1039 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1040 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1041 Result = LegalizeOp(CallResult.first);
1045 assert(0 && "Unreachable!");
1051 // Conversion operators. The source and destination have different types.
1052 case ISD::ZERO_EXTEND:
1053 case ISD::SIGN_EXTEND:
1055 case ISD::FP_EXTEND:
1057 case ISD::FP_TO_SINT:
1058 case ISD::FP_TO_UINT:
1059 case ISD::SINT_TO_FP:
1060 case ISD::UINT_TO_FP:
1061 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1063 Tmp1 = LegalizeOp(Node->getOperand(0));
1064 if (Tmp1 != Node->getOperand(0))
1065 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1068 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1069 Node->getOpcode() == ISD::UINT_TO_FP) {
1070 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1071 Node->getValueType(0), Node->getOperand(0));
1072 Result = LegalizeOp(Result);
1074 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1075 // In the expand case, we must be dealing with a truncate, because
1076 // otherwise the result would be larger than the source.
1077 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1079 // Since the result is legal, we should just be able to truncate the low
1080 // part of the source.
1081 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1084 assert(0 && "Shouldn't need to expand other operators here!");
1087 switch (Node->getOpcode()) {
1088 case ISD::ZERO_EXTEND:
1089 Result = PromoteOp(Node->getOperand(0));
1090 // NOTE: Any extend would work here...
1091 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
1092 Result = DAG.getZeroExtendInReg(Result,
1093 Node->getOperand(0).getValueType());
1095 case ISD::SIGN_EXTEND:
1096 Result = PromoteOp(Node->getOperand(0));
1097 // NOTE: Any extend would work here...
1098 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
1099 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1100 Result, Node->getOperand(0).getValueType());
1103 Result = PromoteOp(Node->getOperand(0));
1104 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1106 case ISD::FP_EXTEND:
1107 Result = PromoteOp(Node->getOperand(0));
1108 if (Result.getValueType() != Op.getValueType())
1109 // Dynamically dead while we have only 2 FP types.
1110 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1113 case ISD::FP_TO_SINT:
1114 case ISD::FP_TO_UINT:
1115 Result = PromoteOp(Node->getOperand(0));
1116 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1118 case ISD::SINT_TO_FP:
1119 Result = PromoteOp(Node->getOperand(0));
1120 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1121 Result, Node->getOperand(0).getValueType());
1122 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1124 case ISD::UINT_TO_FP:
1125 Result = PromoteOp(Node->getOperand(0));
1126 Result = DAG.getZeroExtendInReg(Result,
1127 Node->getOperand(0).getValueType());
1128 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1133 case ISD::FP_ROUND_INREG:
1134 case ISD::SIGN_EXTEND_INREG: {
1135 Tmp1 = LegalizeOp(Node->getOperand(0));
1136 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1138 // If this operation is not supported, convert it to a shl/shr or load/store
1140 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1141 default: assert(0 && "This action not supported for this op yet!");
1142 case TargetLowering::Legal:
1143 if (Tmp1 != Node->getOperand(0))
1144 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1147 case TargetLowering::Expand:
1148 // If this is an integer extend and shifts are supported, do that.
1149 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
1150 // NOTE: we could fall back on load/store here too for targets without
1151 // SAR. However, it is doubtful that any exist.
1152 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1153 MVT::getSizeInBits(ExtraVT);
1154 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
1155 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1156 Node->getOperand(0), ShiftCst);
1157 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1159 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1160 // The only way we can lower this is to turn it into a STORETRUNC,
1161 // EXTLOAD pair, targetting a temporary location (a stack slot).
1163 // NOTE: there is a choice here between constantly creating new stack
1164 // slots and always reusing the same one. We currently always create
1165 // new ones, as reuse may inhibit scheduling.
1166 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1167 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1168 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1169 MachineFunction &MF = DAG.getMachineFunction();
1171 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1172 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1173 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1174 Node->getOperand(0), StackSlot, DAG.getSrcValue(NULL), ExtraVT);
1175 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1176 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
1178 assert(0 && "Unknown op");
1180 Result = LegalizeOp(Result);
1187 if (!Op.Val->hasOneUse())
1188 AddLegalizedOperand(Op, Result);
1193 /// PromoteOp - Given an operation that produces a value in an invalid type,
1194 /// promote it to compute the value into a larger type. The produced value will
1195 /// have the correct bits for the low portion of the register, but no guarantee
1196 /// is made about the top bits: it may be zero, sign-extended, or garbage.
1197 SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1198 MVT::ValueType VT = Op.getValueType();
1199 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1200 assert(getTypeAction(VT) == Promote &&
1201 "Caller should expand or legalize operands that are not promotable!");
1202 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1203 "Cannot promote to smaller type!");
1205 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1206 if (I != PromotedNodes.end()) return I->second;
1208 SDOperand Tmp1, Tmp2, Tmp3;
1211 SDNode *Node = Op.Val;
1213 // Promotion needs an optimization step to clean up after it, and is not
1214 // careful to avoid operations the target does not support. Make sure that
1215 // all generated operations are legalized in the next iteration.
1216 NeedsAnotherIteration = true;
1218 switch (Node->getOpcode()) {
1220 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1221 assert(0 && "Do not know how to promote this operator!");
1224 Result = DAG.getNode(ISD::UNDEF, NVT);
1227 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1228 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1230 case ISD::ConstantFP:
1231 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1232 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1234 case ISD::CopyFromReg:
1235 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1236 Node->getOperand(0));
1237 // Remember that we legalized the chain.
1238 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1242 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1243 "SetCC type is not legal??");
1244 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1245 TLI.getSetCCResultTy(), Node->getOperand(0),
1246 Node->getOperand(1));
1247 Result = LegalizeOp(Result);
1251 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1253 Result = LegalizeOp(Node->getOperand(0));
1254 assert(Result.getValueType() >= NVT &&
1255 "This truncation doesn't make sense!");
1256 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1257 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1260 // The truncation is not required, because we don't guarantee anything
1261 // about high bits anyway.
1262 Result = PromoteOp(Node->getOperand(0));
1265 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1266 // Truncate the low part of the expanded value to the result type
1267 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
1270 case ISD::SIGN_EXTEND:
1271 case ISD::ZERO_EXTEND:
1272 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1273 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1275 // Input is legal? Just do extend all the way to the larger type.
1276 Result = LegalizeOp(Node->getOperand(0));
1277 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1280 // Promote the reg if it's smaller.
1281 Result = PromoteOp(Node->getOperand(0));
1282 // The high bits are not guaranteed to be anything. Insert an extend.
1283 if (Node->getOpcode() == ISD::SIGN_EXTEND)
1284 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1285 Node->getOperand(0).getValueType());
1287 Result = DAG.getZeroExtendInReg(Result,
1288 Node->getOperand(0).getValueType());
1293 case ISD::FP_EXTEND:
1294 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1296 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1297 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1298 case Promote: assert(0 && "Unreachable with 2 FP types!");
1300 // Input is legal? Do an FP_ROUND_INREG.
1301 Result = LegalizeOp(Node->getOperand(0));
1302 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1307 case ISD::SINT_TO_FP:
1308 case ISD::UINT_TO_FP:
1309 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1311 Result = LegalizeOp(Node->getOperand(0));
1312 // No extra round required here.
1313 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1317 Result = PromoteOp(Node->getOperand(0));
1318 if (Node->getOpcode() == ISD::SINT_TO_FP)
1319 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1320 Result, Node->getOperand(0).getValueType());
1322 Result = DAG.getZeroExtendInReg(Result,
1323 Node->getOperand(0).getValueType());
1324 // No extra round required here.
1325 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1328 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1329 Node->getOperand(0));
1330 Result = LegalizeOp(Result);
1332 // Round if we cannot tolerate excess precision.
1333 if (NoExcessFPPrecision)
1334 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1339 case ISD::FP_TO_SINT:
1340 case ISD::FP_TO_UINT:
1341 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1343 Tmp1 = LegalizeOp(Node->getOperand(0));
1346 // The input result is prerounded, so we don't have to do anything
1348 Tmp1 = PromoteOp(Node->getOperand(0));
1351 assert(0 && "not implemented");
1353 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1358 Tmp1 = PromoteOp(Node->getOperand(0));
1359 assert(Tmp1.getValueType() == NVT);
1360 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1361 // NOTE: we do not have to do any extra rounding here for
1362 // NoExcessFPPrecision, because we know the input will have the appropriate
1363 // precision, and these operations don't modify precision at all.
1369 Tmp1 = PromoteOp(Node->getOperand(0));
1370 assert(Tmp1.getValueType() == NVT);
1371 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1372 if(NoExcessFPPrecision)
1373 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1382 // The input may have strange things in the top bits of the registers, but
1383 // these operations don't care. They may have wierd bits going out, but
1384 // that too is okay if they are integer operations.
1385 Tmp1 = PromoteOp(Node->getOperand(0));
1386 Tmp2 = PromoteOp(Node->getOperand(1));
1387 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1388 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1390 // However, if this is a floating point operation, they will give excess
1391 // precision that we may not be able to tolerate. If we DO allow excess
1392 // precision, just leave it, otherwise excise it.
1393 // FIXME: Why would we need to round FP ops more than integer ones?
1394 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
1395 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1396 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1401 // These operators require that their input be sign extended.
1402 Tmp1 = PromoteOp(Node->getOperand(0));
1403 Tmp2 = PromoteOp(Node->getOperand(1));
1404 if (MVT::isInteger(NVT)) {
1405 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1406 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
1408 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1410 // Perform FP_ROUND: this is probably overly pessimistic.
1411 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1412 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1417 // These operators require that their input be zero extended.
1418 Tmp1 = PromoteOp(Node->getOperand(0));
1419 Tmp2 = PromoteOp(Node->getOperand(1));
1420 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1421 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1422 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
1423 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1427 Tmp1 = PromoteOp(Node->getOperand(0));
1428 Tmp2 = LegalizeOp(Node->getOperand(1));
1429 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1432 // The input value must be properly sign extended.
1433 Tmp1 = PromoteOp(Node->getOperand(0));
1434 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1435 Tmp2 = LegalizeOp(Node->getOperand(1));
1436 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1439 // The input value must be properly zero extended.
1440 Tmp1 = PromoteOp(Node->getOperand(0));
1441 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1442 Tmp2 = LegalizeOp(Node->getOperand(1));
1443 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1448 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
1449 if (MVT::isInteger(NVT))
1450 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT);
1452 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT);
1454 // Remember that we legalized the chain.
1455 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1458 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1459 case Expand: assert(0 && "It's impossible to expand bools");
1461 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1464 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1467 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1468 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1469 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1472 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1473 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1475 std::vector<SDOperand> Ops;
1476 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1477 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1479 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1480 "Can only promote single result calls");
1481 std::vector<MVT::ValueType> RetTyVTs;
1482 RetTyVTs.reserve(2);
1483 RetTyVTs.push_back(NVT);
1484 RetTyVTs.push_back(MVT::Other);
1485 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
1486 Result = SDOperand(NC, 0);
1488 // Insert the new chain mapping.
1489 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1494 assert(Result.Val && "Didn't set a result!");
1495 AddPromotedOperand(Op, Result);
1499 /// ExpandAddSub - Find a clever way to expand this add operation into
1501 void SelectionDAGLegalize::
1502 ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1503 SDOperand &Lo, SDOperand &Hi) {
1504 // Expand the subcomponents.
1505 SDOperand LHSL, LHSH, RHSL, RHSH;
1506 ExpandOp(LHS, LHSL, LHSH);
1507 ExpandOp(RHS, RHSL, RHSH);
1509 // FIXME: this should be moved to the dag combiner someday.
1510 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1511 if (LHSL.getValueType() == MVT::i32) {
1513 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1514 if (C->getValue() == 0)
1516 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1517 if (C->getValue() == 0)
1520 // Turn this into an add/sub of the high part only.
1522 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1523 LowEl.getValueType(), LHSH, RHSH);
1530 std::vector<SDOperand> Ops;
1531 Ops.push_back(LHSL);
1532 Ops.push_back(LHSH);
1533 Ops.push_back(RHSL);
1534 Ops.push_back(RHSH);
1535 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1536 Hi = Lo.getValue(1);
1539 void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1540 SDOperand Op, SDOperand Amt,
1541 SDOperand &Lo, SDOperand &Hi) {
1542 // Expand the subcomponents.
1543 SDOperand LHSL, LHSH;
1544 ExpandOp(Op, LHSL, LHSH);
1546 std::vector<SDOperand> Ops;
1547 Ops.push_back(LHSL);
1548 Ops.push_back(LHSH);
1550 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1551 Hi = Lo.getValue(1);
1555 /// ExpandShift - Try to find a clever way to expand this shift operation out to
1556 /// smaller elements. If we can't find a way that is more efficient than a
1557 /// libcall on this target, return false. Otherwise, return true with the
1558 /// low-parts expanded into Lo and Hi.
1559 bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1560 SDOperand &Lo, SDOperand &Hi) {
1561 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1562 "This is not a shift!");
1564 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1565 SDOperand ShAmt = LegalizeOp(Amt);
1566 MVT::ValueType ShTy = ShAmt.getValueType();
1567 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1568 unsigned NVTBits = MVT::getSizeInBits(NVT);
1570 // Handle the case when Amt is an immediate. Other cases are currently broken
1571 // and are disabled.
1572 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1573 unsigned Cst = CN->getValue();
1574 // Expand the incoming operand to be shifted, so that we have its parts
1576 ExpandOp(Op, InL, InH);
1580 Lo = DAG.getConstant(0, NVT);
1581 Hi = DAG.getConstant(0, NVT);
1582 } else if (Cst > NVTBits) {
1583 Lo = DAG.getConstant(0, NVT);
1584 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
1585 } else if (Cst == NVTBits) {
1586 Lo = DAG.getConstant(0, NVT);
1589 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1590 Hi = DAG.getNode(ISD::OR, NVT,
1591 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1592 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1597 Lo = DAG.getConstant(0, NVT);
1598 Hi = DAG.getConstant(0, NVT);
1599 } else if (Cst > NVTBits) {
1600 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1601 Hi = DAG.getConstant(0, NVT);
1602 } else if (Cst == NVTBits) {
1604 Hi = DAG.getConstant(0, NVT);
1606 Lo = DAG.getNode(ISD::OR, NVT,
1607 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1608 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1609 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1614 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1615 DAG.getConstant(NVTBits-1, ShTy));
1616 } else if (Cst > NVTBits) {
1617 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1618 DAG.getConstant(Cst-NVTBits, ShTy));
1619 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1620 DAG.getConstant(NVTBits-1, ShTy));
1621 } else if (Cst == NVTBits) {
1623 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1624 DAG.getConstant(NVTBits-1, ShTy));
1626 Lo = DAG.getNode(ISD::OR, NVT,
1627 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1628 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1629 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1634 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1635 // so disable it for now. Currently targets are handling this via SHL_PARTS
1639 // If we have an efficient select operation (or if the selects will all fold
1640 // away), lower to some complex code, otherwise just emit the libcall.
1641 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1642 !isa<ConstantSDNode>(Amt))
1646 ExpandOp(Op, InL, InH);
1647 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1648 DAG.getConstant(NVTBits, ShTy), ShAmt);
1650 // Compare the unmasked shift amount against 32.
1651 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1652 DAG.getConstant(NVTBits, ShTy));
1654 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1655 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1656 DAG.getConstant(NVTBits-1, ShTy));
1657 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1658 DAG.getConstant(NVTBits-1, ShTy));
1661 if (Opc == ISD::SHL) {
1662 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1663 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1664 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
1665 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
1667 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1668 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1670 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1671 DAG.getSetCC(ISD::SETEQ,
1672 TLI.getSetCCResultTy(), NAmt,
1673 DAG.getConstant(32, ShTy)),
1674 DAG.getConstant(0, NVT),
1675 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
1676 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
1678 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
1679 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
1682 if (Opc == ISD::SRA)
1683 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1684 DAG.getConstant(NVTBits-1, ShTy));
1686 HiPart = DAG.getConstant(0, NVT);
1687 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1688 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
1693 /// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1694 /// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1696 static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1697 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1699 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1700 // than the Found node. Just remember this node and return.
1701 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1706 // Otherwise, scan the operands of Node to see if any of them is a call.
1707 assert(Node->getNumOperands() != 0 &&
1708 "All leaves should have depth equal to the entry node!");
1709 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1710 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1712 // Tail recurse for the last iteration.
1713 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1718 /// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1719 /// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1721 static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1722 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1724 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1725 // than the Found node. Just remember this node and return.
1726 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1731 // Otherwise, scan the operands of Node to see if any of them is a call.
1732 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1733 if (UI == E) return;
1734 for (--E; UI != E; ++UI)
1735 FindEarliestAdjCallStackUp(*UI, Found);
1737 // Tail recurse for the last iteration.
1738 FindEarliestAdjCallStackUp(*UI, Found);
1741 /// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1742 /// find the ADJCALLSTACKUP node that terminates the call sequence.
1743 static SDNode *FindAdjCallStackUp(SDNode *Node) {
1744 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1746 if (Node->use_empty())
1747 return 0; // No adjcallstackup
1749 if (Node->hasOneUse()) // Simple case, only has one user to check.
1750 return FindAdjCallStackUp(*Node->use_begin());
1752 SDOperand TheChain(Node, Node->getNumValues()-1);
1753 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1755 for (SDNode::use_iterator UI = Node->use_begin(),
1756 E = Node->use_end(); ; ++UI) {
1757 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1759 // Make sure to only follow users of our token chain.
1761 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1762 if (User->getOperand(i) == TheChain)
1763 return FindAdjCallStackUp(User);
1765 assert(0 && "Unreachable");
1769 /// FindInputOutputChains - If we are replacing an operation with a call we need
1770 /// to find the call that occurs before and the call that occurs after it to
1771 /// properly serialize the calls in the block.
1772 static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1774 SDNode *LatestAdjCallStackDown = Entry.Val;
1775 SDNode *LatestAdjCallStackUp = 0;
1776 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1777 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1779 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1780 // previous call in the function. LatestCallStackDown may in that case be
1781 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1782 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1783 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1784 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1786 LatestAdjCallStackUp = Entry.Val;
1787 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
1789 SDNode *EarliestAdjCallStackUp = 0;
1790 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1792 if (EarliestAdjCallStackUp) {
1793 //std::cerr << "Found node: ";
1794 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1797 return SDOperand(LatestAdjCallStackUp, 0);
1802 // ExpandLibCall - Expand a node into a call to a libcall. If the result value
1803 // does not fit into a register, return the lo part and set the hi part to the
1804 // by-reg argument. If it does fit into a single register, return the result
1805 // and leave the Hi part unset.
1806 SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1809 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1810 DAG.getEntryNode());
1811 if (InChain.Val == 0)
1812 InChain = DAG.getEntryNode();
1814 TargetLowering::ArgListTy Args;
1815 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1816 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1817 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1818 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1820 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1822 // We don't care about token chains for libcalls. We just use the entry
1823 // node as our input and ignore the output chain. This allows us to place
1824 // calls wherever we need them to satisfy data dependences.
1825 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
1826 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
1828 switch (getTypeAction(Result.getValueType())) {
1829 default: assert(0 && "Unknown thing");
1833 assert(0 && "Cannot promote this yet!");
1836 ExpandOp(Result, Lo, Hi);
1842 /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1843 /// destination type is legal.
1844 SDOperand SelectionDAGLegalize::
1845 ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1846 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1847 assert(getTypeAction(Source.getValueType()) == Expand &&
1848 "This is not an expansion!");
1849 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1852 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1853 DAG.getEntryNode());
1855 const char *FnName = 0;
1857 if (DestTy == MVT::f32)
1858 FnName = "__floatdisf";
1860 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1861 FnName = "__floatdidf";
1864 // If this is unsigned, and not supported, first perform the conversion to
1865 // signed, then adjust the result if the sign bit is set.
1866 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
1868 assert(Source.getValueType() == MVT::i64 &&
1869 "This only works for 64-bit -> FP");
1870 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1871 // incoming integer is set. To handle this, we dynamically test to see if
1872 // it is set, and, if so, add a fudge factor.
1874 ExpandOp(Source, Lo, Hi);
1876 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
1877 DAG.getConstant(0, Hi.getValueType()));
1878 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1879 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1880 SignSet, Four, Zero);
1881 // FIXME: This is almost certainly broken for big-endian systems. Should
1882 // this just put the fudge factor in the low bits of the uint64 constant or?
1883 static Constant *FudgeFactor =
1884 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
1886 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
1887 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
1888 TLI.getPointerTy());
1889 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1890 SDOperand FudgeInReg;
1891 if (DestTy == MVT::f32)
1892 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1894 assert(DestTy == MVT::f64 && "Unexpected conversion");
1895 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
1896 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
1898 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
1900 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1902 TargetLowering::ArgListTy Args;
1903 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1904 Args.push_back(std::make_pair(Source, ArgTy));
1906 // We don't care about token chains for libcalls. We just use the entry
1907 // node as our input and ignore the output chain. This allows us to place
1908 // calls wherever we need them to satisfy data dependences.
1909 const Type *RetTy = MVT::getTypeForValueType(DestTy);
1910 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
1915 /// ExpandOp - Expand the specified SDOperand into its two component pieces
1916 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1917 /// LegalizeNodes map is filled in for any results that are not expanded, the
1918 /// ExpandedNodes map is filled in for any results that are expanded, and the
1919 /// Lo/Hi values are returned.
1920 void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1921 MVT::ValueType VT = Op.getValueType();
1922 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1923 SDNode *Node = Op.Val;
1924 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1925 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1926 assert(MVT::isInteger(NVT) && NVT < VT &&
1927 "Cannot expand to FP value or to larger int value!");
1929 // If there is more than one use of this, see if we already expanded it.
1930 // There is no use remembering values that only have a single use, as the map
1931 // entries will never be reused.
1932 if (!Node->hasOneUse()) {
1933 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1934 = ExpandedNodes.find(Op);
1935 if (I != ExpandedNodes.end()) {
1936 Lo = I->second.first;
1937 Hi = I->second.second;
1942 // Expanding to multiple registers needs to perform an optimization step, and
1943 // is not careful to avoid operations the target does not support. Make sure
1944 // that all generated operations are legalized in the next iteration.
1945 NeedsAnotherIteration = true;
1947 switch (Node->getOpcode()) {
1949 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1950 assert(0 && "Do not know how to expand this operator!");
1953 Lo = DAG.getNode(ISD::UNDEF, NVT);
1954 Hi = DAG.getNode(ISD::UNDEF, NVT);
1956 case ISD::Constant: {
1957 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1958 Lo = DAG.getConstant(Cst, NVT);
1959 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1963 case ISD::CopyFromReg: {
1964 unsigned Reg = cast<RegSDNode>(Node)->getReg();
1965 // Aggregate register values are always in consequtive pairs.
1966 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1967 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1969 // Remember that we legalized the chain.
1970 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1972 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1976 case ISD::BUILD_PAIR:
1977 // Legalize both operands. FIXME: in the future we should handle the case
1978 // where the two elements are not legal.
1979 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1980 Lo = LegalizeOp(Node->getOperand(0));
1981 Hi = LegalizeOp(Node->getOperand(1));
1985 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1986 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1987 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
1989 // Increment the pointer to the other half.
1990 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
1991 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1992 getIntPtrConstant(IncrementSize));
1993 //Is this safe? declaring that the two parts of the split load
1994 //are from the same instruction?
1995 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
1997 // Build a factor node to remember that this load is independent of the
1999 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2002 // Remember that we legalized the chain.
2003 AddLegalizedOperand(Op.getValue(1), TF);
2004 if (!TLI.isLittleEndian())
2009 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2010 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2012 bool Changed = false;
2013 std::vector<SDOperand> Ops;
2014 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2015 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2016 Changed |= Ops.back() != Node->getOperand(i);
2019 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2020 "Can only expand a call once so far, not i64 -> i16!");
2022 std::vector<MVT::ValueType> RetTyVTs;
2023 RetTyVTs.reserve(3);
2024 RetTyVTs.push_back(NVT);
2025 RetTyVTs.push_back(NVT);
2026 RetTyVTs.push_back(MVT::Other);
2027 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
2028 Lo = SDOperand(NC, 0);
2029 Hi = SDOperand(NC, 1);
2031 // Insert the new chain mapping.
2032 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
2037 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2038 SDOperand LL, LH, RL, RH;
2039 ExpandOp(Node->getOperand(0), LL, LH);
2040 ExpandOp(Node->getOperand(1), RL, RH);
2041 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2042 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2046 SDOperand C, LL, LH, RL, RH;
2048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2049 case Expand: assert(0 && "It's impossible to expand bools");
2051 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2054 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2057 ExpandOp(Node->getOperand(1), LL, LH);
2058 ExpandOp(Node->getOperand(2), RL, RH);
2059 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2060 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2063 case ISD::SIGN_EXTEND: {
2065 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2066 case Expand: assert(0 && "expand-expand not implemented yet!");
2067 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2069 In = PromoteOp(Node->getOperand(0));
2070 // Emit the appropriate sign_extend_inreg to get the value we want.
2071 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2072 Node->getOperand(0).getValueType());
2076 // The low part is just a sign extension of the input (which degenerates to
2078 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
2080 // The high part is obtained by SRA'ing all but one of the bits of the lo
2082 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
2083 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2084 TLI.getShiftAmountTy()));
2087 case ISD::ZERO_EXTEND: {
2089 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2090 case Expand: assert(0 && "expand-expand not implemented yet!");
2091 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2093 In = PromoteOp(Node->getOperand(0));
2094 // Emit the appropriate zero_extend_inreg to get the value we want.
2095 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
2099 // The low part is just a zero extension of the input (which degenerates to
2101 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
2103 // The high part is just a zero.
2104 Hi = DAG.getConstant(0, NVT);
2107 // These operators cannot be expanded directly, emit them as calls to
2108 // library functions.
2109 case ISD::FP_TO_SINT:
2110 if (Node->getOperand(0).getValueType() == MVT::f32)
2111 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
2113 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
2115 case ISD::FP_TO_UINT:
2116 if (Node->getOperand(0).getValueType() == MVT::f32)
2117 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
2119 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
2123 // If we can emit an efficient shift operation, do so now.
2124 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
2127 // If this target supports SHL_PARTS, use it.
2128 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
2129 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2134 // Otherwise, emit a libcall.
2135 Lo = ExpandLibCall("__ashldi3", Node, Hi);
2139 // If we can emit an efficient shift operation, do so now.
2140 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
2143 // If this target supports SRA_PARTS, use it.
2144 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
2145 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2150 // Otherwise, emit a libcall.
2151 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
2154 // If we can emit an efficient shift operation, do so now.
2155 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
2158 // If this target supports SRL_PARTS, use it.
2159 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
2160 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2165 // Otherwise, emit a libcall.
2166 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
2170 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2174 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2178 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2179 SDOperand LL, LH, RL, RH;
2180 ExpandOp(Node->getOperand(0), LL, LH);
2181 ExpandOp(Node->getOperand(1), RL, RH);
2182 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2183 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2184 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2185 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2186 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2187 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2189 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2193 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2194 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2195 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2196 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
2199 // Remember in a map if the values will be reused later.
2200 if (!Node->hasOneUse()) {
2201 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2202 std::make_pair(Lo, Hi))).second;
2203 assert(isNew && "Value already expanded?!?");
2208 // SelectionDAG::Legalize - This is the entry point for the file.
2210 void SelectionDAG::Legalize() {
2211 /// run - This is the main entry point to this class.
2213 SelectionDAGLegalize(*this).Run();