1 //===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha -===//
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 defines a pattern matching instruction selector for Alpha.
12 //===----------------------------------------------------------------------===//
15 #include "AlphaRegisterInfo.h"
16 #include "llvm/Constants.h" // FIXME: REMOVE
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/ADT/Statistic.h"
32 //===----------------------------------------------------------------------===//
33 // AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35 class AlphaTargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
37 unsigned GP; //GOT vreg
39 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 //I am having problems with shr n ubyte 1
42 setShiftAmountType(MVT::i64); //are these needed?
43 setSetCCResultType(MVT::i64); //are these needed?
45 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
46 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
47 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
49 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
51 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
52 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
54 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
55 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
56 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
60 setOperationAction(ISD::SREM, MVT::f32, Expand);
61 setOperationAction(ISD::SREM, MVT::f64, Expand);
63 computeRegisterProperties();
65 addLegalFPImmediate(+0.0); //F31
68 /// LowerArguments - This hook must be implemented to indicate how we should
69 /// lower the arguments for the specified function, into the specified DAG.
70 virtual std::vector<SDOperand>
71 LowerArguments(Function &F, SelectionDAG &DAG);
73 /// LowerCallTo - This hook lowers an abstract call to a function into an
75 virtual std::pair<SDOperand, SDOperand>
76 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
77 ArgListTy &Args, SelectionDAG &DAG);
79 virtual std::pair<SDOperand, SDOperand>
80 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
82 virtual std::pair<SDOperand,SDOperand>
83 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
84 const Type *ArgTy, SelectionDAG &DAG);
86 virtual std::pair<SDOperand, SDOperand>
87 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
90 void restoreGP(MachineBasicBlock* BB)
92 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
97 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
99 //For now, just use variable size stack frame format
101 //In a standard call, the first six items are passed in registers $16
102 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
103 //of argument-to-register correspondence.) The remaining items are
104 //collected in a memory argument list that is a naturally aligned
105 //array of quadwords. In a standard call, this list, if present, must
106 //be passed at 0(SP).
107 //7 ... n 0(SP) ... (n-7)*8(SP)
109 std::vector<SDOperand>
110 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
112 std::vector<SDOperand> ArgValues;
120 // assert(0 && "TODO");
121 MachineFunction &MF = DAG.getMachineFunction();
122 MachineFrameInfo *MFI = MF.getFrameInfo();
124 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
125 MachineBasicBlock& BB = MF.front();
127 //Handle the return address
128 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
130 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
131 Alpha::R19, Alpha::R20, Alpha::R21};
132 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
133 Alpha::F19, Alpha::F20, Alpha::F21};
134 std::vector<unsigned> argVreg;
135 std::vector<unsigned> argPreg;
136 std::vector<unsigned> argOpc;
138 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
140 SDOperand newroot, argt;
142 assert(count <= 6 && "More than 6 args not supported");
143 switch (getValueType(I->getType())) {
144 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
147 BuildMI(&BB, Alpha::IDEF, 0, args_float[count - 1]);
148 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType()))));
149 argPreg.push_back(args_float[count - 1]);
150 argOpc.push_back(Alpha::CPYS);
151 newroot = DAG.getCopyFromReg(argVreg[count-1], getValueType(I->getType()), DAG.getRoot());
158 BuildMI(&BB, Alpha::IDEF, 0, args_int[count - 1]);
159 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
160 argPreg.push_back(args_int[count - 1]);
161 argOpc.push_back(Alpha::BIS);
162 argt = newroot = DAG.getCopyFromReg(argVreg[count-1], MVT::i64, DAG.getRoot());
163 if (getValueType(I->getType()) != MVT::i64)
164 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
167 DAG.setRoot(newroot.getValue(1));
168 ArgValues.push_back(argt);
171 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
172 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
173 for (int i = 0; i < count; ++i)
174 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
179 std::pair<SDOperand, SDOperand>
180 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
181 const Type *RetTy, SDOperand Callee,
182 ArgListTy &Args, SelectionDAG &DAG) {
184 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
185 DAG.getConstant(NumBytes, getPointerTy()));
186 std::vector<SDOperand> args_to_use;
187 for (unsigned i = 0, e = Args.size(); i != e; ++i)
189 switch (getValueType(Args[i].second)) {
190 default: assert(0 && "Unexpected ValueType for argument!");
195 // Promote the integer to 64 bits. If the input type is signed use a
196 // sign extend, otherwise use a zero extend.
197 if (Args[i].second->isSigned())
198 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
200 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
207 args_to_use.push_back(Args[i].first);
210 std::vector<MVT::ValueType> RetVals;
211 MVT::ValueType RetTyVT = getValueType(RetTy);
212 if (RetTyVT != MVT::isVoid)
213 RetVals.push_back(RetTyVT);
214 RetVals.push_back(MVT::Other);
216 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
217 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
218 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
219 DAG.getConstant(NumBytes, getPointerTy()));
220 return std::make_pair(TheCall, Chain);
223 std::pair<SDOperand, SDOperand>
224 AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
225 //vastart just returns the address of the VarArgsFrameIndex slot.
226 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
229 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
230 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
231 const Type *ArgTy, SelectionDAG &DAG) {
236 std::pair<SDOperand, SDOperand> AlphaTargetLowering::
237 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
248 //===--------------------------------------------------------------------===//
249 /// ISel - Alpha specific code to select Alpha machine instructions for
250 /// SelectionDAG operations.
252 class ISel : public SelectionDAGISel {
254 /// AlphaLowering - This object fully describes how to lower LLVM code to an
255 /// Alpha-specific SelectionDAG.
256 AlphaTargetLowering AlphaLowering;
259 /// ExprMap - As shared expressions are codegen'd, we keep track of which
260 /// vreg the value is produced in, so we only emit one copy of each compiled
262 static const unsigned notIn = (unsigned)(-1);
263 std::map<SDOperand, unsigned> ExprMap;
266 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
269 /// InstructionSelectBasicBlock - This callback is invoked by
270 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
271 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
272 // Codegen the basic block.
273 Select(DAG.getRoot());
275 // Clear state used for selection.
279 unsigned SelectExpr(SDOperand N);
280 unsigned SelectExprFP(SDOperand N, unsigned Result);
281 void Select(SDOperand N);
285 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
287 unsigned Tmp1, Tmp2, Tmp3;
289 SDNode *Node = N.Val;
290 MVT::ValueType DestType = N.getValueType();
291 unsigned opcode = N.getOpcode();
296 assert(0 && "Node not handled!\n");
299 // Make sure we generate both values.
301 ExprMap[N.getValue(1)] = notIn; // Generate the token
303 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
305 SDOperand Chain = N.getOperand(0);
306 SDOperand Address = N.getOperand(1);
308 if (Address.getOpcode() == ISD::GlobalAddress)
311 AlphaLowering.restoreGP(BB);
312 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
313 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
318 Tmp2 = SelectExpr(Address);
319 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
320 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
324 case ISD::ConstantFP:
325 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
326 if (CN->isExactlyValue(+0.0)) {
327 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
339 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
340 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
341 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
342 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
344 Tmp1 = SelectExpr(N.getOperand(0));
345 Tmp2 = SelectExpr(N.getOperand(1));
346 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
349 case ISD::SINT_TO_FP:
351 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
352 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
353 Tmp2 = MakeReg(DestType);
354 //so these instructions are not supported on ev56
355 Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
356 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
357 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
358 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
362 assert(0 && "should not get here");
366 unsigned ISel::SelectExpr(SDOperand N) {
368 unsigned Tmp1, Tmp2, Tmp3;
370 unsigned opcode = N.getOpcode();
372 SDNode *Node = N.Val;
373 MVT::ValueType DestType = N.getValueType();
375 unsigned &Reg = ExprMap[N];
378 if (N.getOpcode() != ISD::CALL)
379 Reg = Result = (N.getValueType() != MVT::Other) ?
380 MakeReg(N.getValueType()) : notIn;
382 // If this is a call instruction, make sure to prepare ALL of the result
383 // values as well as the chain.
384 if (Node->getNumValues() == 1)
385 Reg = Result = notIn; // Void call, just a chain.
387 Result = MakeReg(Node->getValueType(0));
388 ExprMap[N.getValue(0)] = Result;
389 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
390 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
391 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
395 if (DestType == MVT::f64 || DestType == MVT::f32)
396 return SelectExprFP(N, Result);
401 assert(0 && "Node not handled!\n");
403 case ISD::FrameIndex:
404 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
405 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp1 * 8).addReg(Alpha::R30);
409 // Make sure we generate both values.
411 ExprMap[N.getValue(1)] = notIn; // Generate the token
413 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
415 Select(Node->getOperand(0)); // chain
416 Tmp1 = SelectExpr(Node->getOperand(1));
418 switch(Node->getValueType(0)) {
419 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
421 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
424 assert(0 && "Bad extend load!");
426 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
429 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
432 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
434 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
436 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
444 // Make sure we generate both values.
446 ExprMap[N.getValue(1)] = notIn; // Generate the token
448 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
450 Select(Node->getOperand(0)); // chain
451 Tmp1 = SelectExpr(Node->getOperand(1));
452 switch(Node->getValueType(0)) {
453 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
455 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
458 assert(0 && "Bad sign extend!");
460 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
468 // Make sure we generate both values.
470 ExprMap[N.getValue(1)] = notIn; // Generate the token
472 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
474 Select(Node->getOperand(0)); // chain
475 Tmp1 = SelectExpr(Node->getOperand(1));
476 switch(Node->getValueType(0)) {
477 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
479 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
482 assert(0 && "Bad sign extend!");
484 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
487 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
495 case ISD::GlobalAddress:
496 AlphaLowering.restoreGP(BB);
497 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
498 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
503 Select(N.getOperand(0));
505 // The chain for this call is now lowered.
506 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
509 std::vector<unsigned> argvregs;
510 assert(Node->getNumOperands() < 8 && "Only 6 args supported");
511 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
512 argvregs.push_back(SelectExpr(N.getOperand(i)));
514 for(int i = 0, e = argvregs.size(); i < e; ++i)
516 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
517 Alpha::R19, Alpha::R20, Alpha::R21};
518 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
519 Alpha::F19, Alpha::F20, Alpha::F21};
520 switch(N.getOperand(i+2).getValueType()) {
523 N.getOperand(i).Val->dump();
524 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
525 assert(0 && "Unknown value type for call");
531 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
535 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
540 //build the right kind of call
541 if (GlobalAddressSDNode *GASD =
542 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
544 AlphaLowering.restoreGP(BB);
545 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
547 else if (ExternalSymbolSDNode *ESSDN =
548 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
550 AlphaLowering.restoreGP(BB);
551 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
555 Tmp1 = SelectExpr(N.getOperand(1));
556 AlphaLowering.restoreGP(BB);
557 BuildMI(BB, Alpha::CALL, 1).addReg(Tmp1);
560 //push the result into a virtual register
562 switch (Node->getValueType(0)) {
563 default: Node->dump(); assert(0 && "Unknown value type for call result!");
564 case MVT::Other: return notIn;
570 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
574 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
577 return Result+N.ResNo;
580 case ISD::SIGN_EXTEND:
583 case ISD::SIGN_EXTEND_INREG:
585 //Alpha has instructions for a bunch of signed 32 bit stuff
586 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
588 switch (N.getOperand(0).getOpcode()) {
593 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
594 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
595 //FIXME: first check for Scaled Adds and Subs!
596 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
597 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
598 { //Normal imm add/sub
599 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
600 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
601 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
602 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
606 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
607 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
608 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
609 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
613 default: break; //Fall Though;
615 } //Every thing else fall though too, including unhandled opcodes above
616 Tmp1 = SelectExpr(N.getOperand(0));
617 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
618 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
619 switch(MVN->getExtraValueType())
623 assert(0 && "Sign Extend InReg not there yet");
627 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
631 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
634 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
639 case ISD::ZERO_EXTEND_INREG:
641 Tmp1 = SelectExpr(N.getOperand(0));
642 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
643 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
644 switch(MVN->getExtraValueType())
648 assert(0 && "Zero Extend InReg not there yet");
650 case MVT::i32: Tmp2 = 0xf0; break;
651 case MVT::i16: Tmp2 = 0xfc; break;
652 case MVT::i8: Tmp2 = 0xfe; break;
653 case MVT::i1: //handle this one special
654 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
657 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
663 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
664 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
665 bool isConst1 = false;
666 bool isConst2 = false;
669 //Tmp1 = SelectExpr(N.getOperand(0));
670 if(N.getOperand(0).getOpcode() == ISD::Constant &&
671 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
673 if(N.getOperand(1).getOpcode() == ISD::Constant &&
674 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
677 switch (SetCC->getCondition()) {
678 default: Node->dump(); assert(0 && "Unknown integer comparison!");
679 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
680 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
681 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
682 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
683 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
684 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
685 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
686 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
687 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
688 case ISD::SETNE: {//Handle this one special
689 //std::cerr << "Alpha does not have a setne.\n";
691 Tmp1 = SelectExpr(N.getOperand(0));
692 Tmp2 = SelectExpr(N.getOperand(1));
693 Tmp3 = MakeReg(MVT::i64);
694 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
696 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
697 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
702 Tmp1 = SelectExpr(N.getOperand(0));
704 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
705 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
707 Tmp2 = SelectExpr(N.getOperand(1));
708 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
710 } else if (dir == 2) {
711 Tmp1 = SelectExpr(N.getOperand(1));
713 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
714 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
716 Tmp2 = SelectExpr(N.getOperand(0));
717 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
721 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
722 Tmp2 = SelectExpr(N.getOperand(1));
723 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
724 } else if (isConst2) {
725 Tmp1 = SelectExpr(N.getOperand(0));
726 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
727 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
729 Tmp1 = SelectExpr(N.getOperand(0));
730 Tmp2 = SelectExpr(N.getOperand(1));
731 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
738 assert(0 && "only integer");
744 assert(0 && "Not a setcc in setcc");
749 case ISD::CopyFromReg:
751 // Make sure we generate both values.
753 ExprMap[N.getValue(1)] = notIn; // Generate the token
755 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
757 SDOperand Chain = N.getOperand(0);
760 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
761 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
762 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
766 //Most of the plain arithmetic and logic share the same form, and the same
767 //constant immediate test
774 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
775 if(N.getOperand(1).getOpcode() == ISD::Constant &&
776 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
779 case ISD::AND: Opc = Alpha::ANDi; break;
780 case ISD::OR: Opc = Alpha::BISi; break;
781 case ISD::XOR: Opc = Alpha::XORi; break;
782 case ISD::SHL: Opc = Alpha::SLi; break;
783 case ISD::SRL: Opc = Alpha::SRLi; break;
784 case ISD::SRA: Opc = Alpha::SRAi; break;
785 case ISD::MUL: Opc = Alpha::MULQi; break;
787 Tmp1 = SelectExpr(N.getOperand(0));
788 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
789 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
794 case ISD::AND: Opc = Alpha::AND; break;
795 case ISD::OR: Opc = Alpha::BIS; break;
796 case ISD::XOR: Opc = Alpha::XOR; break;
797 case ISD::SHL: Opc = Alpha::SL; break;
798 case ISD::SRL: Opc = Alpha::SRL; break;
799 case ISD::SRA: Opc = Alpha::SRA; break;
800 case ISD::MUL: Opc = Alpha::MULQ; break;
802 Tmp1 = SelectExpr(N.getOperand(0));
803 Tmp2 = SelectExpr(N.getOperand(1));
804 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
811 bool isAdd = opcode == ISD::ADD;
813 //FIXME: first check for Scaled Adds and Subs!
814 if(N.getOperand(1).getOpcode() == ISD::Constant &&
815 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
816 { //Normal imm add/sub
817 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
818 Tmp1 = SelectExpr(N.getOperand(0));
819 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
820 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
822 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
823 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
824 { //LDA //FIXME: expand the above condition a bit
825 Tmp1 = SelectExpr(N.getOperand(0));
826 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
829 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
833 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
834 Tmp1 = SelectExpr(N.getOperand(0));
835 Tmp2 = SelectExpr(N.getOperand(1));
836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
845 //FIXME: alpha really doesn't support any of these operations,
846 // the ops are expanded into special library calls with
847 // special calling conventions
849 case ISD::UREM: Opc = Alpha::REMQU; break;
850 case ISD::SREM: Opc = Alpha::REMQ; break;
851 case ISD::UDIV: Opc = Alpha::DIVQU; break;
852 case ISD::SDIV: Opc = Alpha::DIVQ; break;
854 Tmp1 = SelectExpr(N.getOperand(0));
855 Tmp2 = SelectExpr(N.getOperand(1));
856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
858 // // case ISD::UINT_TO_FP:
860 // case ISD::FP_TO_SINT:
861 // assert (N.getValueType() == MVT::f64 && "Only can convert for doubles");
862 // Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
863 // Tmp2 = MakeReg(SrcTy);
864 // BuildMI(BB, CVTTQ, 1, Tmp2).addReg(Tmp1);
865 // BuildMI(BB, FTOIT, 1, Result).addReg(Tmp2);
868 // // case ISD::FP_TO_UINT:
872 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
873 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
874 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
875 // Get the condition into the zero flag.
876 unsigned dummy = MakeReg(MVT::i64);
877 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
878 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
884 long val = cast<ConstantSDNode>(N)->getValue();
885 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
891 // Make sure we generate both values.
893 ExprMap[N.getValue(1)] = notIn; // Generate the token
895 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
897 SDOperand Chain = N.getOperand(0);
898 SDOperand Address = N.getOperand(1);
900 if (Address.getOpcode() == ISD::GlobalAddress)
903 AlphaLowering.restoreGP(BB);
904 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
909 Tmp2 = SelectExpr(Address);
910 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
919 void ISel::Select(SDOperand N) {
920 unsigned Tmp1, Tmp2, Opc;
922 // FIXME: Disable for our current expansion model!
923 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
924 return; // Already selected.
926 SDNode *Node = N.Val;
928 switch (N.getOpcode()) {
931 Node->dump(); std::cerr << "\n";
932 assert(0 && "Node not handled yet!");
935 MachineBasicBlock *Dest =
936 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
938 Select(N.getOperand(0));
939 Tmp1 = SelectExpr(N.getOperand(1));
940 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
945 MachineBasicBlock *Dest =
946 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
948 Select(N.getOperand(0));
949 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
953 case ISD::ImplicitDef:
954 Select(N.getOperand(0));
955 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
958 case ISD::EntryToken: return; // Noop
960 case ISD::TokenFactor:
961 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
962 Select(Node->getOperand(i));
964 //N.Val->dump(); std::cerr << "\n";
965 //assert(0 && "Node not handled yet!");
970 Select(N.getOperand(0));
971 Tmp1 = SelectExpr(N.getOperand(1));
972 Tmp2 = cast<RegSDNode>(N)->getReg();
975 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
980 switch (N.getNumOperands()) {
982 std::cerr << N.getNumOperands() << "\n";
983 for (unsigned i = 0; i < N.getNumOperands(); ++i)
984 std::cerr << N.getOperand(i).getValueType() << "\n";
986 assert(0 && "Unknown return instruction!");
988 Select(N.getOperand(0));
989 Tmp1 = SelectExpr(N.getOperand(1));
990 switch (N.getOperand(1).getValueType()) {
991 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
994 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
998 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1003 Select(N.getOperand(0));
1006 //Tmp2 = AlphaLowering.getRetAddr();
1007 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1008 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1012 Select(N.getOperand(0));
1013 Tmp1 = SelectExpr(N.getOperand(1)); //value
1014 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1016 AlphaLowering.restoreGP(BB);
1017 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1021 Tmp2 = SelectExpr(N.getOperand(2)); //address
1022 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1030 case ISD::CopyFromReg:
1032 // case ISD::DYNAMIC_STACKALLOC:
1038 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1039 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
1040 if (StoredTy == MVT::i64) {
1042 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1045 Select(N.getOperand(0));
1046 Tmp1 = SelectExpr(N.getOperand(1));
1047 Tmp2 = SelectExpr(N.getOperand(2));
1050 default: Node->dump(); assert(0 && "Unhandled Type"); break;
1051 case MVT::i1: //FIXME: DAG does not promote this load
1052 case MVT::i8: Opc = Alpha::STB; break;
1053 case MVT::i16: Opc = Alpha::STW; break;
1054 case MVT::i32: Opc = Alpha::STL; break;
1057 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1061 case ISD::ADJCALLSTACKDOWN:
1062 case ISD::ADJCALLSTACKUP:
1063 Select(N.getOperand(0));
1064 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1066 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1067 Alpha::ADJUSTSTACKUP;
1068 BuildMI(BB, Opc, 1).addImm(Tmp1);
1071 assert(0 && "Should not be reached!");
1075 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1076 /// into a machine code representation using pattern matching and a machine
1077 /// description file.
1079 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1080 return new ISel(TM);