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"
33 //===----------------------------------------------------------------------===//
34 // AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
36 class AlphaTargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 unsigned GP; //GOT vreg
40 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
42 //I am having problems with shr n ubyte 1
43 setShiftAmountType(MVT::i64);
44 setSetCCResultType(MVT::i64);
46 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
48 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
50 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
52 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
53 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
55 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
56 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
59 setOperationAction(ISD::SREM , MVT::f32 , Expand);
60 setOperationAction(ISD::SREM , MVT::f64 , Expand);
62 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
63 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
64 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
66 computeRegisterProperties();
68 addLegalFPImmediate(+0.0); //F31
69 addLegalFPImmediate(-0.0); //-F31
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
77 /// LowerCallTo - This hook lowers an abstract call to a function into an
79 virtual std::pair<SDOperand, SDOperand>
80 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
81 ArgListTy &Args, SelectionDAG &DAG);
83 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
86 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
94 void restoreGP(MachineBasicBlock* BB)
96 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
101 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
103 //For now, just use variable size stack frame format
105 //In a standard call, the first six items are passed in registers $16
106 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
107 //of argument-to-register correspondence.) The remaining items are
108 //collected in a memory argument list that is a naturally aligned
109 //array of quadwords. In a standard call, this list, if present, must
110 //be passed at 0(SP).
111 //7 ... n 0(SP) ... (n-7)*8(SP)
113 std::vector<SDOperand>
114 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
116 std::vector<SDOperand> ArgValues;
124 // assert(0 && "TODO");
125 MachineFunction &MF = DAG.getMachineFunction();
126 MachineFrameInfo*MFI = MF.getFrameInfo();
128 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
129 MachineBasicBlock& BB = MF.front();
131 //Handle the return address
132 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
134 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
135 Alpha::R19, Alpha::R20, Alpha::R21};
136 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
137 Alpha::F19, Alpha::F20, Alpha::F21};
144 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
146 SDOperand newroot, argt;
148 switch (getValueType(I->getType())) {
150 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
154 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
156 MF.getSSARegMap()->createVirtualRegister(
157 getRegClassFor(getValueType(I->getType())));
158 argPreg[count] = args_float[count];
159 argOpc[count] = Alpha::CPYS;
160 argt = newroot = DAG.getCopyFromReg(argVreg[count],
161 getValueType(I->getType()),
169 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
171 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172 argPreg[count] = args_int[count];
173 argOpc[count] = Alpha::BIS;
175 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
176 if (getValueType(I->getType()) != MVT::i64)
178 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
183 // Create the frame index object for this incoming parameter...
184 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
186 // Create the SelectionDAG nodes corresponding to a load
187 //from this parameter
188 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
189 argt = newroot = DAG.getLoad(getValueType(I->getType()),
190 DAG.getEntryNode(), FIN);
192 DAG.setRoot(newroot.getValue(1));
193 ArgValues.push_back(argt);
196 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
197 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
198 for (int i = 0; i < count; ++i) {
199 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 ||
200 argPreg[i] == Alpha::F18 || argPreg[i] == Alpha::F19 ||
201 argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
203 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
205 BuildMI(&BB, argOpc[i], 2,
206 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
212 std::pair<SDOperand, SDOperand>
213 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
214 const Type *RetTy, SDOperand Callee,
215 ArgListTy &Args, SelectionDAG &DAG) {
218 NumBytes = (Args.size() - 6) * 8;
220 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
221 DAG.getConstant(NumBytes, getPointerTy()));
222 std::vector<SDOperand> args_to_use;
223 for (unsigned i = 0, e = Args.size(); i != e; ++i)
225 switch (getValueType(Args[i].second)) {
226 default: assert(0 && "Unexpected ValueType for argument!");
231 // Promote the integer to 64 bits. If the input type is signed use a
232 // sign extend, otherwise use a zero extend.
233 if (Args[i].second->isSigned())
234 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
236 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
243 args_to_use.push_back(Args[i].first);
246 std::vector<MVT::ValueType> RetVals;
247 MVT::ValueType RetTyVT = getValueType(RetTy);
248 if (RetTyVT != MVT::isVoid)
249 RetVals.push_back(RetTyVT);
250 RetVals.push_back(MVT::Other);
252 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
253 Chain, Callee, args_to_use), 0);
254 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
255 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
256 DAG.getConstant(NumBytes, getPointerTy()));
257 return std::make_pair(TheCall, Chain);
260 std::pair<SDOperand, SDOperand>
261 AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
262 //vastart just returns the address of the VarArgsFrameIndex slot.
263 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
266 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
267 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
268 const Type *ArgTy, SelectionDAG &DAG) {
273 std::pair<SDOperand, SDOperand> AlphaTargetLowering::
274 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
285 //===--------------------------------------------------------------------===//
286 /// ISel - Alpha specific code to select Alpha machine instructions for
287 /// SelectionDAG operations.
288 //===--------------------------------------------------------------------===//
289 class ISel : public SelectionDAGISel {
291 /// AlphaLowering - This object fully describes how to lower LLVM code to an
292 /// Alpha-specific SelectionDAG.
293 AlphaTargetLowering AlphaLowering;
296 /// ExprMap - As shared expressions are codegen'd, we keep track of which
297 /// vreg the value is produced in, so we only emit one copy of each compiled
299 static const unsigned notIn = (unsigned)(-1);
300 std::map<SDOperand, unsigned> ExprMap;
302 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
303 std::map<SDOperand, unsigned> CCInvMap;
306 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
309 /// InstructionSelectBasicBlock - This callback is invoked by
310 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
311 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
312 // Codegen the basic block.
313 Select(DAG.getRoot());
315 // Clear state used for selection.
320 unsigned SelectExpr(SDOperand N);
321 unsigned SelectExprFP(SDOperand N, unsigned Result);
322 void Select(SDOperand N);
324 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
325 void SelectBranchCC(SDOperand N);
329 static unsigned GetSymVersion(unsigned opcode)
332 default: assert(0 && "unknown load or store"); return 0;
333 case Alpha::LDQ: return Alpha::LDQ_SYM;
334 case Alpha::LDS: return Alpha::LDS_SYM;
335 case Alpha::LDT: return Alpha::LDT_SYM;
336 case Alpha::LDL: return Alpha::LDL_SYM;
337 case Alpha::LDBU: return Alpha::LDBU_SYM;
338 case Alpha::LDWU: return Alpha::LDWU_SYM;
339 case Alpha::LDW: return Alpha::LDW_SYM;
340 case Alpha::LDB: return Alpha::LDB_SYM;
341 case Alpha::STQ: return Alpha::STQ_SYM;
342 case Alpha::STS: return Alpha::STS_SYM;
343 case Alpha::STT: return Alpha::STT_SYM;
344 case Alpha::STL: return Alpha::STL_SYM;
345 case Alpha::STW: return Alpha::STW_SYM;
346 case Alpha::STB: return Alpha::STB_SYM;
350 //Check to see if the load is a constant offset from a base register
351 void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
353 unsigned opcode = N.getOpcode();
354 if (opcode == ISD::ADD) {
355 if(N.getOperand(1).getOpcode() == ISD::Constant &&
356 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
358 Reg = SelectExpr(N.getOperand(0));
359 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
362 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
363 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
365 Reg = SelectExpr(N.getOperand(1));
366 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
375 void ISel::SelectBranchCC(SDOperand N)
377 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
378 MachineBasicBlock *Dest =
379 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
380 unsigned Opc = Alpha::WTF;
382 Select(N.getOperand(0)); //chain
383 SDOperand CC = N.getOperand(1);
385 if (CC.getOpcode() == ISD::SETCC)
387 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
388 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
389 //Dropping the CC is only useful if we are comparing to 0
390 bool isZero0 = false;
391 bool isZero1 = false;
394 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
395 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
397 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
398 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
400 if(SetCC->getCondition() == ISD::SETNE)
404 switch (SetCC->getCondition()) {
405 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
406 case ISD::SETEQ: Opc = Alpha::BEQ; break;
407 case ISD::SETLT: Opc = Alpha::BGT; break;
408 case ISD::SETLE: Opc = Alpha::BGE; break;
409 case ISD::SETGT: Opc = Alpha::BLT; break;
410 case ISD::SETGE: Opc = Alpha::BLE; break;
411 case ISD::SETULT: Opc = Alpha::BNE; break;
412 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
413 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
414 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
415 case ISD::SETNE: Opc = Alpha::BNE; break;
417 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
418 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
420 } else if (isZero1) {
421 switch (SetCC->getCondition()) {
422 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
423 case ISD::SETEQ: Opc = Alpha::BEQ; break;
424 case ISD::SETLT: Opc = Alpha::BLT; break;
425 case ISD::SETLE: Opc = Alpha::BLE; break;
426 case ISD::SETGT: Opc = Alpha::BGT; break;
427 case ISD::SETGE: Opc = Alpha::BGE; break;
428 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
429 case ISD::SETUGT: Opc = Alpha::BNE; break;
430 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
431 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
432 case ISD::SETNE: Opc = Alpha::BNE; break;
434 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
435 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
438 unsigned Tmp1 = SelectExpr(CC);
440 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
442 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
446 //Any comparison between 2 values should be codegened as an folded branch, as moving
447 //CC to the integer register is very expensive
448 //for a cmp b: c = a - b;
452 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
453 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
454 unsigned Tmp3 = MakeReg(MVT::f64);
455 BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
457 switch (SetCC->getCondition()) {
458 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
459 case ISD::SETEQ: Opc = Alpha::FBEQ; break;
460 case ISD::SETLT: Opc = Alpha::FBLT; break;
461 case ISD::SETLE: Opc = Alpha::FBLE; break;
462 case ISD::SETGT: Opc = Alpha::FBGT; break;
463 case ISD::SETGE: Opc = Alpha::FBGE; break;
464 case ISD::SETNE: Opc = Alpha::FBNE; break;
466 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
469 abort(); //Should never be reached
471 //Giveup and do the stupid thing
472 unsigned Tmp1 = SelectExpr(CC);
473 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
476 abort(); //Should never be reached
479 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
481 unsigned Tmp1, Tmp2, Tmp3;
483 SDNode *Node = N.Val;
484 MVT::ValueType DestType = N.getValueType();
485 unsigned opcode = N.getOpcode();
490 assert(0 && "Node not handled!\n");
494 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
495 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
496 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
499 // Spill the cond to memory and reload it from there.
500 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
501 MachineFunction *F = BB->getParent();
502 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
503 unsigned Tmp4 = MakeReg(MVT::f64);
504 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
505 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
506 //now ideally, we don't have to do anything to the flag...
507 // Get the condition into the zero flag.
508 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
513 assert (DestType == MVT::f32 &&
514 N.getOperand(0).getValueType() == MVT::f64 &&
515 "only f64 to f32 conversion supported here");
516 Tmp1 = SelectExpr(N.getOperand(0));
517 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
521 assert (DestType == MVT::f64 &&
522 N.getOperand(0).getValueType() == MVT::f32 &&
523 "only f32 to f64 conversion supported here");
524 Tmp1 = SelectExpr(N.getOperand(0));
525 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
528 case ISD::CopyFromReg:
530 // Make sure we generate both values.
532 ExprMap[N.getValue(1)] = notIn; // Generate the token
534 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
536 SDOperand Chain = N.getOperand(0);
539 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
540 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
541 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
547 // Make sure we generate both values.
549 ExprMap[N.getValue(1)] = notIn; // Generate the token
551 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
553 DestType = N.getValue(0).getValueType();
555 SDOperand Chain = N.getOperand(0);
556 SDOperand Address = N.getOperand(1);
558 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
560 if (Address.getOpcode() == ISD::GlobalAddress) {
561 AlphaLowering.restoreGP(BB);
562 Opc = GetSymVersion(Opc);
563 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
565 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
566 AlphaLowering.restoreGP(BB);
567 Opc = GetSymVersion(Opc);
568 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
570 else if(Address.getOpcode() == ISD::FrameIndex) {
571 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
572 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
575 SelectAddr(Address, Tmp1, offset);
576 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
580 case ISD::ConstantFP:
581 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
582 if (CN->isExactlyValue(+0.0)) {
583 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
584 } else if ( CN->isExactlyValue(-0.0)) {
585 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
597 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
598 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
599 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
600 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
602 Tmp1 = SelectExpr(N.getOperand(0));
603 Tmp2 = SelectExpr(N.getOperand(1));
604 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
609 //include a conversion sequence for float loads to double
611 ExprMap[N.getValue(1)] = notIn; // Generate the token
613 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
615 Tmp1 = MakeReg(MVT::f32);
617 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
618 "EXTLOAD not from f32");
619 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
621 SDOperand Chain = N.getOperand(0);
622 SDOperand Address = N.getOperand(1);
625 if (Address.getOpcode() == ISD::GlobalAddress) {
626 AlphaLowering.restoreGP(BB);
627 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
629 else if (ConstantPoolSDNode *CP =
630 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
632 AlphaLowering.restoreGP(BB);
633 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
635 else if(Address.getOpcode() == ISD::FrameIndex) {
636 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
637 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
640 SelectAddr(Address, Tmp2, offset);
641 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
643 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
647 case ISD::UINT_TO_FP:
648 case ISD::SINT_TO_FP:
650 assert (N.getOperand(0).getValueType() == MVT::i64
651 && "only quads can be loaded from");
652 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
653 Tmp2 = MakeReg(MVT::f64);
656 // Spill the integer to memory and reload it from there.
657 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
658 MachineFunction *F = BB->getParent();
659 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
661 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
662 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
663 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
664 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
666 //The easy way: doesn't work
667 // //so these instructions are not supported on ev56
668 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
669 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
670 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
671 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
676 assert(0 && "should not get here");
680 unsigned ISel::SelectExpr(SDOperand N) {
682 unsigned Tmp1, Tmp2, Tmp3;
684 unsigned opcode = N.getOpcode();
686 SDNode *Node = N.Val;
687 MVT::ValueType DestType = N.getValueType();
689 unsigned &Reg = ExprMap[N];
692 if (N.getOpcode() != ISD::CALL)
693 Reg = Result = (N.getValueType() != MVT::Other) ?
694 MakeReg(N.getValueType()) : notIn;
696 // If this is a call instruction, make sure to prepare ALL of the result
697 // values as well as the chain.
698 if (Node->getNumValues() == 1)
699 Reg = Result = notIn; // Void call, just a chain.
701 Result = MakeReg(Node->getValueType(0));
702 ExprMap[N.getValue(0)] = Result;
703 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
704 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
705 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
709 if (DestType == MVT::f64 || DestType == MVT::f32 ||
711 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
712 opcode == ISD::EXTLOAD) &&
713 (N.getValue(0).getValueType() == MVT::f32 ||
714 N.getValue(0).getValueType() == MVT::f64)
717 return SelectExprFP(N, Result);
722 assert(0 && "Node not handled!\n");
724 case ISD::ConstantPool:
725 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
726 AlphaLowering.restoreGP(BB);
727 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
730 case ISD::FrameIndex:
731 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
732 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
740 // Make sure we generate both values.
742 ExprMap[N.getValue(1)] = notIn; // Generate the token
744 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
746 SDOperand Chain = N.getOperand(0);
747 SDOperand Address = N.getOperand(1);
750 assert(Node->getValueType(0) == MVT::i64 &&
751 "Unknown type to sign extend to.");
752 if (opcode == ISD::LOAD)
755 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
756 default: Node->dump(); assert(0 && "Bad sign extend!");
757 case MVT::i32: Opc = Alpha::LDL;
758 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
759 case MVT::i16: Opc = Alpha::LDWU;
760 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
761 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
762 case MVT::i8: Opc = Alpha::LDBU;
763 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
766 if (Address.getOpcode() == ISD::GlobalAddress) {
767 AlphaLowering.restoreGP(BB);
768 Opc = GetSymVersion(Opc);
769 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
771 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
772 AlphaLowering.restoreGP(BB);
773 Opc = GetSymVersion(Opc);
774 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
776 else if(Address.getOpcode() == ISD::FrameIndex) {
777 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
778 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
781 SelectAddr(Address, Tmp1, offset);
782 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
787 case ISD::GlobalAddress:
788 AlphaLowering.restoreGP(BB);
789 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
790 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
795 Select(N.getOperand(0));
797 // The chain for this call is now lowered.
798 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
801 std::vector<unsigned> argvregs;
802 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
803 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
804 argvregs.push_back(SelectExpr(N.getOperand(i)));
807 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
809 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
810 Alpha::R19, Alpha::R20, Alpha::R21};
811 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
812 Alpha::F19, Alpha::F20, Alpha::F21};
813 switch(N.getOperand(i+2).getValueType()) {
816 N.getOperand(i).Val->dump();
817 std::cerr << "Type for " << i << " is: " <<
818 N.getOperand(i+2).getValueType() << "\n";
819 assert(0 && "Unknown value type for call");
825 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
829 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
834 for (int i = 6, e = argvregs.size(); i < e; ++i)
836 switch(N.getOperand(i+2).getValueType()) {
839 N.getOperand(i).Val->dump();
840 std::cerr << "Type for " << i << " is: " <<
841 N.getOperand(i+2).getValueType() << "\n";
842 assert(0 && "Unknown value type for call");
848 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
851 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
854 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
858 //build the right kind of call
859 if (GlobalAddressSDNode *GASD =
860 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
862 //if (GASD->getGlobal()->isExternal()) {
863 //use safe calling convention
864 AlphaLowering.restoreGP(BB);
865 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
867 //use PC relative branch call
868 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
871 else if (ExternalSymbolSDNode *ESSDN =
872 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
874 AlphaLowering.restoreGP(BB);
875 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
877 //no need to restore GP as we are doing an indirect call
878 Tmp1 = SelectExpr(N.getOperand(1));
879 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
880 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
883 //push the result into a virtual register
885 switch (Node->getValueType(0)) {
886 default: Node->dump(); assert(0 && "Unknown value type for call result!");
887 case MVT::Other: return notIn;
893 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
897 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
900 return Result+N.ResNo;
903 case ISD::SIGN_EXTEND:
906 case ISD::SIGN_EXTEND_INREG:
908 //Alpha has instructions for a bunch of signed 32 bit stuff
909 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
911 switch (N.getOperand(0).getOpcode()) {
916 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
917 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
918 //FIXME: first check for Scaled Adds and Subs!
919 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
920 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
921 { //Normal imm add/sub
922 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
923 //if the value was really originally a i32, skip the up conversion
924 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
925 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
926 ->getExtraValueType() == MVT::i32)
927 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
929 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
930 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
931 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
935 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
936 //if the value was really originally a i32, skip the up conversion
937 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
938 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
939 ->getExtraValueType() == MVT::i32)
940 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
942 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
943 //if the value was really originally a i32, skip the up conversion
944 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
945 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
946 ->getExtraValueType() == MVT::i32)
947 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
949 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
951 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
952 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
953 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
957 default: break; //Fall Though;
959 } //Every thing else fall though too, including unhandled opcodes above
960 Tmp1 = SelectExpr(N.getOperand(0));
961 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
962 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
963 switch(MVN->getExtraValueType())
967 assert(0 && "Sign Extend InReg not there yet");
971 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
975 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
978 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
981 Tmp2 = MakeReg(MVT::i64);
982 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
983 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
988 case ISD::ZERO_EXTEND_INREG:
990 Tmp1 = SelectExpr(N.getOperand(0));
991 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
992 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
993 switch(MVN->getExtraValueType())
997 assert(0 && "Zero Extend InReg not there yet");
999 case MVT::i32: Tmp2 = 0xf0; break;
1000 case MVT::i16: Tmp2 = 0xfc; break;
1001 case MVT::i8: Tmp2 = 0xfe; break;
1002 case MVT::i1: //handle this one special
1003 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1006 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1012 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1013 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1014 bool isConst1 = false;
1015 bool isConst2 = false;
1018 //Tmp1 = SelectExpr(N.getOperand(0));
1019 if(N.getOperand(0).getOpcode() == ISD::Constant &&
1020 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1022 if(N.getOperand(1).getOpcode() == ISD::Constant &&
1023 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1026 switch (SetCC->getCondition()) {
1027 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1028 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
1030 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1032 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1034 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1036 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1038 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1040 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1042 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1044 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
1045 case ISD::SETNE: {//Handle this one special
1046 //std::cerr << "Alpha does not have a setne.\n";
1048 Tmp1 = SelectExpr(N.getOperand(0));
1049 Tmp2 = SelectExpr(N.getOperand(1));
1050 Tmp3 = MakeReg(MVT::i64);
1051 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1052 //Remeber we have the Inv for this CC
1055 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1060 Tmp1 = SelectExpr(N.getOperand(0));
1062 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1063 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1065 Tmp2 = SelectExpr(N.getOperand(1));
1066 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1068 } else if (dir == 2) {
1069 Tmp1 = SelectExpr(N.getOperand(1));
1071 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1072 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1074 Tmp2 = SelectExpr(N.getOperand(0));
1075 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1079 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1080 Tmp2 = SelectExpr(N.getOperand(1));
1081 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1082 } else if (isConst2) {
1083 Tmp1 = SelectExpr(N.getOperand(0));
1084 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1085 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1087 Tmp1 = SelectExpr(N.getOperand(0));
1088 Tmp2 = SelectExpr(N.getOperand(1));
1089 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1093 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1097 switch (SetCC->getCondition()) {
1098 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1099 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1100 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1101 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1102 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1103 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1104 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1107 Tmp1 = SelectExpr(N.getOperand(0));
1108 Tmp2 = SelectExpr(N.getOperand(1));
1109 //Can only compare doubles, and dag won't promote for me
1110 if (SetCC->getOperand(0).getValueType() == MVT::f32)
1112 Tmp3 = MakeReg(MVT::f64);
1113 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1116 if (SetCC->getOperand(1).getValueType() == MVT::f32)
1118 Tmp3 = MakeReg(MVT::f64);
1119 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1123 if (rev) std::swap(Tmp1, Tmp2);
1124 Tmp3 = MakeReg(MVT::f64);
1126 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1128 //now arrange for Result (int) to have a 1 or 0
1130 // Spill the FP to memory and reload it from there.
1131 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1132 MachineFunction *F = BB->getParent();
1133 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1134 unsigned Tmp4 = MakeReg(MVT::f64);
1135 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1136 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1137 unsigned Tmp5 = MakeReg(MVT::i64);
1138 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1140 //now, set result based on Tmp5
1141 //Set Tmp6 if fp cmp was false
1142 unsigned Tmp6 = MakeReg(MVT::i64);
1143 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1145 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1151 // assert(0 && "Not a setcc in setcc");
1157 case ISD::CopyFromReg:
1159 // Make sure we generate both values.
1160 if (Result != notIn)
1161 ExprMap[N.getValue(1)] = notIn; // Generate the token
1163 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1165 SDOperand Chain = N.getOperand(0);
1168 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1169 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1170 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1174 //Most of the plain arithmetic and logic share the same form, and the same
1175 //constant immediate test
1183 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1184 if(N.getOperand(1).getOpcode() == ISD::Constant &&
1185 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1188 case ISD::AND: Opc = Alpha::ANDi; break;
1189 case ISD::OR: Opc = Alpha::BISi; break;
1190 case ISD::XOR: Opc = Alpha::XORi; break;
1191 case ISD::SHL: Opc = Alpha::SLi; break;
1192 case ISD::SRL: Opc = Alpha::SRLi; break;
1193 case ISD::SRA: Opc = Alpha::SRAi; break;
1194 case ISD::MUL: Opc = Alpha::MULQi; break;
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1198 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1201 case ISD::AND: Opc = Alpha::AND; break;
1202 case ISD::OR: Opc = Alpha::BIS; break;
1203 case ISD::XOR: Opc = Alpha::XOR; break;
1204 case ISD::SHL: Opc = Alpha::SL; break;
1205 case ISD::SRL: Opc = Alpha::SRL; break;
1206 case ISD::SRA: Opc = Alpha::SRA; break;
1207 case ISD::MUL: Opc = Alpha::MULQ; break;
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 Tmp2 = SelectExpr(N.getOperand(1));
1211 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1218 bool isAdd = opcode == ISD::ADD;
1220 //FIXME: first check for Scaled Adds and Subs!
1221 if(N.getOperand(1).getOpcode() == ISD::Constant &&
1222 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1223 { //Normal imm add/sub
1224 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1227 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1229 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
1230 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1231 { //LDA //FIXME: expand the above condition a bit
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1236 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1239 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1251 //FIXME: alpha really doesn't support any of these operations,
1252 // the ops are expanded into special library calls with
1253 // special calling conventions
1254 //Restore GP because it is a call after all...
1256 case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1257 case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1258 case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1259 case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
1261 Tmp1 = SelectExpr(N.getOperand(0));
1262 Tmp2 = SelectExpr(N.getOperand(1));
1263 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1266 case ISD::FP_TO_UINT:
1267 case ISD::FP_TO_SINT:
1269 assert (DestType == MVT::i64 && "only quads can be loaded to");
1270 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1271 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
1272 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1275 // Spill the integer to memory and reload it from there.
1276 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1277 MachineFunction *F = BB->getParent();
1278 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1281 //CVTST CVTTQ STT LDQ
1282 if (SrcType == MVT::f32)
1284 Tmp2 = MakeReg(MVT::f64);
1285 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1288 Tmp2 = MakeReg(MVT::f64);
1289 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1290 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1291 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1296 // // case ISD::FP_TO_UINT:
1300 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1301 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1302 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1303 // Get the condition into the zero flag.
1304 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1310 unsigned long val = cast<ConstantSDNode>(N)->getValue();
1311 if (val < 32000 && (long)val > -32000)
1312 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1314 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1315 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1316 unsigned CPI = CP->getConstantPoolIndex(C);
1317 AlphaLowering.restoreGP(BB);
1318 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1327 void ISel::Select(SDOperand N) {
1328 unsigned Tmp1, Tmp2, Opc;
1329 unsigned opcode = N.getOpcode();
1331 // FIXME: Disable for our current expansion model!
1332 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
1333 return; // Already selected.
1335 SDNode *Node = N.Val;
1341 Node->dump(); std::cerr << "\n";
1342 assert(0 && "Node not handled yet!");
1350 MachineBasicBlock *Dest =
1351 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1353 Select(N.getOperand(0));
1354 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1358 case ISD::ImplicitDef:
1359 Select(N.getOperand(0));
1360 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1363 case ISD::EntryToken: return; // Noop
1365 case ISD::TokenFactor:
1366 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1367 Select(Node->getOperand(i));
1369 //N.Val->dump(); std::cerr << "\n";
1370 //assert(0 && "Node not handled yet!");
1374 case ISD::CopyToReg:
1375 Select(N.getOperand(0));
1376 Tmp1 = SelectExpr(N.getOperand(1));
1377 Tmp2 = cast<RegSDNode>(N)->getReg();
1380 if (N.getOperand(1).getValueType() == MVT::f64 ||
1381 N.getOperand(1).getValueType() == MVT::f32)
1382 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1384 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1389 switch (N.getNumOperands()) {
1391 std::cerr << N.getNumOperands() << "\n";
1392 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1393 std::cerr << N.getOperand(i).getValueType() << "\n";
1395 assert(0 && "Unknown return instruction!");
1397 Select(N.getOperand(0));
1398 Tmp1 = SelectExpr(N.getOperand(1));
1399 switch (N.getOperand(1).getValueType()) {
1400 default: Node->dump();
1401 assert(0 && "All other types should have been promoted!!");
1404 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1408 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1413 Select(N.getOperand(0));
1416 //Tmp2 = AlphaLowering.getRetAddr();
1417 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1418 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1421 case ISD::TRUNCSTORE:
1424 SDOperand Chain = N.getOperand(0);
1425 SDOperand Value = N.getOperand(1);
1426 SDOperand Address = N.getOperand(2);
1429 Tmp1 = SelectExpr(Value); //value
1431 if (opcode == ISD::STORE) {
1432 switch(Value.getValueType()) {
1433 default: assert(0 && "unknown Type in store");
1434 case MVT::i64: Opc = Alpha::STQ; break;
1435 case MVT::f64: Opc = Alpha::STT; break;
1436 case MVT::f32: Opc = Alpha::STS; break;
1438 } else { //ISD::TRUNCSTORE
1439 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1440 default: assert(0 && "unknown Type in store");
1441 case MVT::i1: //FIXME: DAG does not promote this load
1442 case MVT::i8: Opc = Alpha::STB; break;
1443 case MVT::i16: Opc = Alpha::STW; break;
1444 case MVT::i32: Opc = Alpha::STL; break;
1448 if (Address.getOpcode() == ISD::GlobalAddress)
1450 AlphaLowering.restoreGP(BB);
1451 Opc = GetSymVersion(Opc);
1452 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1454 else if(Address.getOpcode() == ISD::FrameIndex)
1456 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1457 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1462 SelectAddr(Address, Tmp2, offset);
1463 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1472 case ISD::CopyFromReg:
1474 // case ISD::DYNAMIC_STACKALLOC:
1479 case ISD::ADJCALLSTACKDOWN:
1480 case ISD::ADJCALLSTACKUP:
1481 Select(N.getOperand(0));
1482 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1484 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1485 Alpha::ADJUSTSTACKUP;
1486 BuildMI(BB, Opc, 1).addImm(Tmp1);
1489 assert(0 && "Should not be reached!");
1493 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1494 /// into a machine code representation using pattern matching and a machine
1495 /// description file.
1497 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1498 return new ISel(TM);