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 "AlphaISelLowering.h"
17 #include "llvm/Constants.h" // FIXME: REMOVE
18 #include "llvm/Function.h"
19 #include "llvm/Module.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/SSARegMap.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/CommandLine.h"
38 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
39 cl::desc("Use the FP div instruction for integer div when possible"),
41 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
42 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
44 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
45 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
47 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
48 cl::desc("Print estimates on live ins and outs"),
50 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
51 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
57 //===--------------------------------------------------------------------===//
58 /// ISel - Alpha specific code to select Alpha machine instructions for
59 /// SelectionDAG operations.
60 //===--------------------------------------------------------------------===//
61 class AlphaISel : public SelectionDAGISel {
63 /// AlphaLowering - This object fully describes how to lower LLVM code to an
64 /// Alpha-specific SelectionDAG.
65 AlphaTargetLowering AlphaLowering;
67 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
68 // for sdiv and udiv until it is put into the future
71 /// ExprMap - As shared expressions are codegen'd, we keep track of which
72 /// vreg the value is produced in, so we only emit one copy of each compiled
74 static const unsigned notIn = (unsigned)(-1);
75 std::map<SDOperand, unsigned> ExprMap;
77 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
78 std::map<SDOperand, unsigned> CCInvMap;
86 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
90 /// InstructionSelectBasicBlock - This callback is invoked by
91 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
92 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
99 // Codegen the basic block.
101 max_depth = DAG.getRoot().getNodeDepth();
102 Select(DAG.getRoot());
107 std::cerr << "COUNT: "
108 << BB->getParent()->getFunction ()->getName() << " "
109 << BB->getNumber() << " "
112 << count_outs << "\n";
114 // Clear state used for selection.
119 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
121 unsigned SelectExpr(SDOperand N);
122 void Select(SDOperand N);
124 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
125 void SelectBranchCC(SDOperand N);
126 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
127 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
128 //returns whether the sense of the comparison was inverted
129 bool SelectFPSetCC(SDOperand N, unsigned dst);
131 // dag -> dag expanders for integer divide by constant
132 SDOperand BuildSDIVSequence(SDOperand N);
133 SDOperand BuildUDIVSequence(SDOperand N);
138 void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
139 // If this function has live-in values, emit the copies from pregs to vregs at
140 // the top of the function, before anything else.
141 MachineBasicBlock *BB = MF.begin();
142 if (MF.livein_begin() != MF.livein_end()) {
143 SSARegMap *RegMap = MF.getSSARegMap();
144 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
145 E = MF.livein_end(); LI != E; ++LI) {
146 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
147 if (RC == Alpha::GPRCRegisterClass) {
148 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
150 } else if (RC == Alpha::FPRCRegisterClass) {
151 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
154 assert(0 && "Unknown regclass!");
160 static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
162 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
164 Imm = CN->getSignExtended();
172 // isSIntImmediateBounded - This method tests to see if a constant operand
173 // bounded s.t. low <= Imm <= high
174 // If so Imm will receive the 64 bit value.
175 static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
176 int64_t low, int64_t high) {
177 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
181 static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
183 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
185 Imm = (uint64_t)CN->getValue();
193 static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
194 uint64_t low, uint64_t high) {
195 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
200 static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
202 fun = type = offset = 0;
205 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
207 const Module* M = GV->getParent();
208 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
210 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
212 const Function* F = Arg->getParent();
213 const Module* M = F->getParent();
214 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
216 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
218 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
219 assert(dyn_cast<PointerType>(I->getType()));
221 const BasicBlock* bb = I->getParent();
222 const Function* F = bb->getParent();
223 const Module* M = F->getParent();
224 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
226 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
227 offset += ii->size();
228 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
230 } else if (const Constant* C = dyn_cast<Constant>(v)) {
231 //Don't know how to look these up yet
234 assert(0 && "Error in value marking");
236 //type = 4: register spilling
237 //type = 5: global address loading or constant loading
246 //Factorize a number using the list of constants
247 static bool factorize(int v[], int res[], int size, uint64_t c)
250 while (c != 1 && cont)
253 for(int i = 0; i < size; ++i)
267 //Shamelessly adapted from PPC32
268 // Structure used to return the necessary information to codegen an SDIV as
271 int64_t m; // magic number
272 int64_t s; // shift amount
276 uint64_t m; // magic number
277 int64_t a; // add indicator
278 int64_t s; // shift amount
281 /// magic - calculate the magic numbers required to codegen an integer sdiv as
282 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
284 static struct ms magic(int64_t d) {
286 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
287 const uint64_t two63 = 9223372036854775808ULL; // 2^63
291 t = two63 + ((uint64_t)d >> 63);
292 anc = t - 1 - t%ad; // absolute value of nc
293 p = 63; // initialize p
294 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
295 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
296 q2 = two63/ad; // initialize q2 = 2p/abs(d)
297 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
300 q1 = 2*q1; // update q1 = 2p/abs(nc)
301 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
302 if (r1 >= anc) { // must be unsigned comparison
306 q2 = 2*q2; // update q2 = 2p/abs(d)
307 r2 = 2*r2; // update r2 = rem(2p/abs(d))
308 if (r2 >= ad) { // must be unsigned comparison
313 } while (q1 < delta || (q1 == delta && r1 == 0));
316 if (d < 0) mag.m = -mag.m; // resulting magic number
317 mag.s = p - 64; // resulting shift
321 /// magicu - calculate the magic numbers required to codegen an integer udiv as
322 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
323 static struct mu magicu(uint64_t d)
326 uint64_t nc, delta, q1, r1, q2, r2;
328 magu.a = 0; // initialize "add" indicator
330 p = 63; // initialize p
331 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
332 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
333 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
334 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
337 if (r1 >= nc - r1 ) {
338 q1 = 2*q1 + 1; // update q1
339 r1 = 2*r1 - nc; // update r1
342 q1 = 2*q1; // update q1
343 r1 = 2*r1; // update r1
345 if (r2 + 1 >= d - r2) {
346 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
347 q2 = 2*q2 + 1; // update q2
348 r2 = 2*r2 + 1 - d; // update r2
351 if (q2 >= 0x8000000000000000ull) magu.a = 1;
352 q2 = 2*q2; // update q2
353 r2 = 2*r2 + 1; // update r2
356 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
357 magu.m = q2 + 1; // resulting magic number
358 magu.s = p - 64; // resulting shift
362 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
363 /// return a DAG expression to select that will generate the same value by
364 /// multiplying by a magic number. See:
365 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
366 SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
367 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
368 ms magics = magic(d);
369 // Multiply the numerator (operand 0) by the magic value
370 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
371 ISelDAG->getConstant(magics.m, MVT::i64));
372 // If d > 0 and m < 0, add the numerator
373 if (d > 0 && magics.m < 0)
374 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
375 // If d < 0 and m > 0, subtract the numerator.
376 if (d < 0 && magics.m > 0)
377 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
378 // Shift right algebraic if shift value is nonzero
380 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
381 ISelDAG->getConstant(magics.s, MVT::i64));
382 // Extract the sign bit and add it to the quotient
384 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
385 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
388 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
389 /// return a DAG expression to select that will generate the same value by
390 /// multiplying by a magic number. See:
391 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
392 SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
394 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
395 mu magics = magicu(d);
396 // Multiply the numerator (operand 0) by the magic value
397 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
398 ISelDAG->getConstant(magics.m, MVT::i64));
400 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
401 ISelDAG->getConstant(magics.s, MVT::i64));
403 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
404 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
405 ISelDAG->getConstant(1, MVT::i64));
406 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
407 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
408 ISelDAG->getConstant(magics.s-1, MVT::i64));
413 //These describe LDAx
414 static const int IMM_LOW = -32768;
415 static const int IMM_HIGH = 32767;
416 static const int IMM_MULT = 65536;
418 static long getUpper16(long l)
420 long y = l / IMM_MULT;
421 if (l % IMM_MULT > IMM_HIGH)
426 static long getLower16(long l)
428 long h = getUpper16(l);
429 return l - h * IMM_MULT;
432 static unsigned GetRelVersion(unsigned opcode)
435 default: assert(0 && "unknown load or store"); return 0;
436 case Alpha::LDQ: return Alpha::LDQr;
437 case Alpha::LDS: return Alpha::LDSr;
438 case Alpha::LDT: return Alpha::LDTr;
439 case Alpha::LDL: return Alpha::LDLr;
440 case Alpha::LDBU: return Alpha::LDBUr;
441 case Alpha::LDWU: return Alpha::LDWUr;
442 case Alpha::STB: return Alpha::STBr;
443 case Alpha::STW: return Alpha::STWr;
444 case Alpha::STL: return Alpha::STLr;
445 case Alpha::STQ: return Alpha::STQr;
446 case Alpha::STS: return Alpha::STSr;
447 case Alpha::STT: return Alpha::STTr;
452 void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
455 if (EnableAlphaFTOI) {
456 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
457 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
460 // Spill the integer to memory and reload it from there.
461 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
462 MachineFunction *F = BB->getParent();
463 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
465 if (EnableAlphaLSMark)
466 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
468 Opc = isDouble ? Alpha::STT : Alpha::STS;
469 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
471 if (EnableAlphaLSMark)
472 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
474 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
475 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
479 void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
482 if (EnableAlphaFTOI) {
483 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
484 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
487 // Spill the integer to memory and reload it from there.
488 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
489 MachineFunction *F = BB->getParent();
490 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
492 if (EnableAlphaLSMark)
493 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
495 Opc = isDouble ? Alpha::STQ : Alpha::STL;
496 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
498 if (EnableAlphaLSMark)
499 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
501 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
502 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
506 bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
508 SDNode *SetCC = N.Val;
509 unsigned Opc, Tmp1, Tmp2, Tmp3;
510 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
515 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
516 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
517 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
518 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
519 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
520 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
521 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
524 ConstantFPSDNode *CN;
525 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
526 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
529 Tmp1 = SelectExpr(N.getOperand(0));
531 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
532 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
535 Tmp2 = SelectExpr(N.getOperand(1));
537 //Can only compare doubles, and dag won't promote for me
538 if (SetCC->getOperand(0).getValueType() == MVT::f32)
540 //assert(0 && "Setcc On float?\n");
541 std::cerr << "Setcc on float!\n";
542 Tmp3 = MakeReg(MVT::f64);
543 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
546 if (SetCC->getOperand(1).getValueType() == MVT::f32)
548 //assert (0 && "Setcc On float?\n");
549 std::cerr << "Setcc on float!\n";
550 Tmp3 = MakeReg(MVT::f64);
551 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
555 if (rev) std::swap(Tmp1, Tmp2);
557 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
561 //Check to see if the load is a constant offset from a base register
562 void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
564 unsigned opcode = N.getOpcode();
565 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
566 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
568 Reg = SelectExpr(N.getOperand(0));
569 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
577 void AlphaISel::SelectBranchCC(SDOperand N)
579 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
580 MachineBasicBlock *Dest =
581 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
582 unsigned Opc = Alpha::WTF;
584 Select(N.getOperand(0)); //chain
585 SDOperand CC = N.getOperand(1);
587 if (CC.getOpcode() == ISD::SETCC)
589 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
590 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
591 //Dropping the CC is only useful if we are comparing to 0
592 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
593 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
597 if(cCode == ISD::SETNE)
602 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
603 case ISD::SETEQ: Opc = Alpha::BEQ; break;
604 case ISD::SETLT: Opc = Alpha::BLT; break;
605 case ISD::SETLE: Opc = Alpha::BLE; break;
606 case ISD::SETGT: Opc = Alpha::BGT; break;
607 case ISD::SETGE: Opc = Alpha::BGE; break;
608 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
609 case ISD::SETUGT: Opc = Alpha::BNE; break;
610 //Technically you could have this CC
611 case ISD::SETULE: Opc = Alpha::BEQ; break;
612 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
613 case ISD::SETNE: Opc = Alpha::BNE; break;
615 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
616 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
619 unsigned Tmp1 = SelectExpr(CC);
621 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
623 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
627 //Any comparison between 2 values should be codegened as an folded
628 //branch, as moving CC to the integer register is very expensive
629 //for a cmp b: c = a - b;
634 bool invTest = false;
637 ConstantFPSDNode *CN;
638 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
639 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
640 Tmp3 = SelectExpr(CC.getOperand(0));
641 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
642 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
644 Tmp3 = SelectExpr(CC.getOperand(1));
649 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
650 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
651 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
652 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
653 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
654 .addReg(Tmp1).addReg(Tmp2);
658 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
659 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
660 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
661 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
662 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
663 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
664 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
666 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
669 abort(); //Should never be reached
671 //Giveup and do the stupid thing
672 unsigned Tmp1 = SelectExpr(CC);
673 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
676 abort(); //Should never be reached
679 unsigned AlphaISel::SelectExpr(SDOperand N) {
681 unsigned Tmp1, Tmp2 = 0, Tmp3;
683 unsigned opcode = N.getOpcode();
687 SDNode *Node = N.Val;
688 MVT::ValueType DestType = N.getValueType();
689 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
691 unsigned &Reg = ExprMap[N];
694 switch(N.getOpcode()) {
696 Reg = Result = (N.getValueType() != MVT::Other) ?
697 MakeReg(N.getValueType()) : notIn;
699 case ISD::AssertSext:
700 case ISD::AssertZext:
701 return Reg = SelectExpr(N.getOperand(0));
704 // If this is a call instruction, make sure to prepare ALL of the result
705 // values as well as the chain.
706 if (Node->getNumValues() == 1)
707 Reg = Result = notIn; // Void call, just a chain.
709 Result = MakeReg(Node->getValueType(0));
710 ExprMap[N.getValue(0)] = Result;
711 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
712 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
713 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
721 assert(0 && "Node not handled!\n");
726 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
727 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
728 Tmp1 = SelectExpr(N.getOperand(0));
729 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
733 Tmp1 = SelectExpr(N.getOperand(0));
734 Tmp2 = SelectExpr(N.getOperand(1));
735 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
739 //MULHU - Ra<63>*Rb - Rb<63>*Ra
740 Tmp1 = SelectExpr(N.getOperand(0));
741 Tmp2 = SelectExpr(N.getOperand(1));
742 Tmp3 = MakeReg(MVT::i64);
743 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
744 unsigned V1 = MakeReg(MVT::i64);
745 unsigned V2 = MakeReg(MVT::i64);
746 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
748 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
750 unsigned IRes = MakeReg(MVT::i64);
751 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
752 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
756 BuildMI(BB, Alpha::IDEF, 0, Result);
760 case ISD::DYNAMIC_STACKALLOC:
761 // Generate both result values.
763 ExprMap[N.getValue(1)] = notIn; // Generate the token
765 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
767 // FIXME: We are currently ignoring the requested alignment for handling
768 // greater than the stack alignment. This will need to be revisited at some
769 // point. Align = N.getOperand(2);
771 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
772 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
773 std::cerr << "Cannot allocate stack object with greater alignment than"
774 << " the stack alignment yet!";
778 Select(N.getOperand(0));
779 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
780 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
782 Tmp1 = SelectExpr(N.getOperand(1));
783 // Subtract size from stack pointer, thereby allocating some space.
784 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
787 // Put a pointer to the space into the result register, by copying the stack
789 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
792 case ISD::ConstantPool:
793 Tmp1 = BB->getParent()->getConstantPool()->
794 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
795 AlphaLowering.restoreGP(BB);
796 Tmp2 = MakeReg(MVT::i64);
797 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
799 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
803 case ISD::FrameIndex:
804 BuildMI(BB, Alpha::LDA, 2, Result)
805 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
814 // Make sure we generate both values.
816 ExprMap[N.getValue(1)] = notIn; // Generate the token
818 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
820 SDOperand Chain = N.getOperand(0);
821 SDOperand Address = N.getOperand(1);
826 if (opcode == ISD::LOAD)
827 switch (Node->getValueType(0)) {
828 default: Node->dump(); assert(0 && "Bad load!");
829 case MVT::i64: Opc = Alpha::LDQ; break;
830 case MVT::f64: Opc = Alpha::LDT; break;
831 case MVT::f32: Opc = Alpha::LDS; break;
834 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
835 default: Node->dump(); assert(0 && "Bad sign extend!");
836 case MVT::i32: Opc = Alpha::LDL;
837 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
838 case MVT::i16: Opc = Alpha::LDWU;
839 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
840 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
841 case MVT::i8: Opc = Alpha::LDBU;
842 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
846 if (EnableAlphaLSMark)
847 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
850 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
851 if (GASD && !GASD->getGlobal()->isExternal()) {
852 Tmp1 = MakeReg(MVT::i64);
853 AlphaLowering.restoreGP(BB);
854 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
855 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
856 if (EnableAlphaLSMark)
857 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
859 BuildMI(BB, GetRelVersion(Opc), 2, Result)
860 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
861 } else if (ConstantPoolSDNode *CP =
862 dyn_cast<ConstantPoolSDNode>(Address)) {
863 unsigned CPIdx = BB->getParent()->getConstantPool()->
864 getConstantPoolIndex(CP->get());
865 AlphaLowering.restoreGP(BB);
867 Tmp1 = MakeReg(MVT::i64);
868 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
870 if (EnableAlphaLSMark)
871 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
873 BuildMI(BB, GetRelVersion(Opc), 2, Result)
874 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
875 } else if(Address.getOpcode() == ISD::FrameIndex) {
876 if (EnableAlphaLSMark)
877 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
879 BuildMI(BB, Opc, 2, Result)
880 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
884 SelectAddr(Address, Tmp1, offset);
885 if (EnableAlphaLSMark)
886 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
888 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
893 case ISD::GlobalAddress:
894 AlphaLowering.restoreGP(BB);
897 Reg = Result = MakeReg(MVT::i64);
899 if (EnableAlphaLSMark)
900 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
903 BuildMI(BB, Alpha::LDQl, 2, Result)
904 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
908 case ISD::ExternalSymbol:
909 AlphaLowering.restoreGP(BB);
912 Reg = Result = MakeReg(MVT::i64);
914 if (EnableAlphaLSMark)
915 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
918 BuildMI(BB, Alpha::LDQl, 2, Result)
919 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
926 Select(N.getOperand(0));
928 // The chain for this call is now lowered.
929 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
932 std::vector<unsigned> argvregs;
933 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
934 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
935 argvregs.push_back(SelectExpr(N.getOperand(i)));
938 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
940 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
941 Alpha::R19, Alpha::R20, Alpha::R21};
942 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
943 Alpha::F19, Alpha::F20, Alpha::F21};
944 switch(N.getOperand(i+2).getValueType()) {
947 N.getOperand(i).Val->dump();
948 std::cerr << "Type for " << i << " is: " <<
949 N.getOperand(i+2).getValueType() << "\n";
950 assert(0 && "Unknown value type for call");
956 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
957 .addReg(argvregs[i]);
961 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
962 .addReg(argvregs[i]);
967 for (int i = 6, e = argvregs.size(); i < e; ++i)
969 switch(N.getOperand(i+2).getValueType()) {
972 N.getOperand(i).Val->dump();
973 std::cerr << "Type for " << i << " is: " <<
974 N.getOperand(i+2).getValueType() << "\n";
975 assert(0 && "Unknown value type for call");
981 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
985 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
989 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
994 //build the right kind of call
995 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
996 if (GASD && !GASD->getGlobal()->isExternal()) {
997 //use PC relative branch call
998 AlphaLowering.restoreGP(BB);
999 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1000 .addGlobalAddress(GASD->getGlobal(),true);
1002 //no need to restore GP as we are doing an indirect call
1003 Tmp1 = SelectExpr(N.getOperand(1));
1004 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1005 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1008 //push the result into a virtual register
1010 switch (Node->getValueType(0)) {
1011 default: Node->dump(); assert(0 && "Unknown value type for call result!");
1012 case MVT::Other: return notIn;
1014 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1018 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1021 return Result+N.ResNo;
1024 case ISD::SIGN_EXTEND_INREG:
1026 //do SDIV opt for all levels of ints if not dividing by a constant
1027 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1028 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
1030 unsigned Tmp4 = MakeReg(MVT::f64);
1031 unsigned Tmp5 = MakeReg(MVT::f64);
1032 unsigned Tmp6 = MakeReg(MVT::f64);
1033 unsigned Tmp7 = MakeReg(MVT::f64);
1034 unsigned Tmp8 = MakeReg(MVT::f64);
1035 unsigned Tmp9 = MakeReg(MVT::f64);
1037 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1038 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1039 MoveInt2FP(Tmp1, Tmp4, true);
1040 MoveInt2FP(Tmp2, Tmp5, true);
1041 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1042 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
1043 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1044 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
1045 MoveFP2Int(Tmp9, Result, true);
1049 //Alpha has instructions for a bunch of signed 32 bit stuff
1050 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
1051 switch (N.getOperand(0).getOpcode()) {
1056 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1057 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1058 //FIXME: first check for Scaled Adds and Subs!
1059 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1060 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
1062 bool use4 = SImm == 2;
1063 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1064 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1065 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1066 2,Result).addReg(Tmp1).addReg(Tmp2);
1068 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1069 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
1071 bool use4 = SImm == 2;
1072 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1073 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1074 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1076 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
1077 { //Normal imm add/sub
1078 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
1079 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1080 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1082 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1083 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
1084 { //handle canonicalization
1085 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1086 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1087 SImm = 0 - ((SImm << 32) >> 32);
1088 assert(SImm >= 0 && SImm <= 255);
1089 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1093 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
1094 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1095 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1096 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1100 default: break; //Fall Though;
1102 } //Every thing else fall though too, including unhandled opcodes above
1103 Tmp1 = SelectExpr(N.getOperand(0));
1104 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
1105 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
1108 assert(0 && "Sign Extend InReg not there yet");
1112 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
1116 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
1119 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
1122 Tmp2 = MakeReg(MVT::i64);
1123 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
1124 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
1132 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1133 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1134 bool isConst = false;
1137 //Tmp1 = SelectExpr(N.getOperand(0));
1138 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
1142 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1144 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1146 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1148 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1149 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1150 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1152 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1153 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1155 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1156 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1157 case ISD::SETNE: {//Handle this one special
1158 //std::cerr << "Alpha does not have a setne.\n";
1160 Tmp1 = SelectExpr(N.getOperand(0));
1161 Tmp2 = SelectExpr(N.getOperand(1));
1162 Tmp3 = MakeReg(MVT::i64);
1163 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1164 //Remeber we have the Inv for this CC
1167 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1172 Tmp1 = SelectExpr(N.getOperand(0));
1174 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1176 Tmp2 = SelectExpr(N.getOperand(1));
1177 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1179 } else { //if (dir == 2) {
1180 Tmp1 = SelectExpr(N.getOperand(1));
1181 Tmp2 = SelectExpr(N.getOperand(0));
1182 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1186 Tmp1 = MakeReg(MVT::f64);
1187 bool inv = SelectFPSetCC(N, Tmp1);
1189 //now arrange for Result (int) to have a 1 or 0
1190 Tmp2 = MakeReg(MVT::i64);
1191 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1192 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1193 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
1198 case ISD::CopyFromReg:
1202 // Make sure we generate both values.
1203 if (Result != notIn)
1204 ExprMap[N.getValue(1)] = notIn; // Generate the token
1206 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1208 SDOperand Chain = N.getOperand(0);
1211 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1212 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1213 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
1214 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1216 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1220 //Most of the plain arithmetic and logic share the same form, and the same
1221 //constant immediate test
1224 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1232 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
1234 unsigned int build = 0;
1235 for(int i = 0; i < 8; ++i)
1237 if ((UImm & 0x00FF) == 0x00FF)
1239 else if ((UImm & 0x00FF) != 0)
1240 { build = 0; break; }
1245 Tmp1 = SelectExpr(N.getOperand(0));
1246 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1251 //Check operand(0) == Not
1252 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1253 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
1255 case ISD::AND: Opc = Alpha::BIC; break;
1256 case ISD::OR: Opc = Alpha::ORNOT; break;
1257 case ISD::XOR: Opc = Alpha::EQV; break;
1259 Tmp1 = SelectExpr(N.getOperand(1));
1260 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1261 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1264 //Check operand(1) == Not
1265 if (N.getOperand(1).getOpcode() == ISD::XOR &&
1266 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
1268 case ISD::AND: Opc = Alpha::BIC; break;
1269 case ISD::OR: Opc = Alpha::ORNOT; break;
1270 case ISD::XOR: Opc = Alpha::EQV; break;
1272 Tmp1 = SelectExpr(N.getOperand(0));
1273 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1274 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1282 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
1284 case ISD::AND: Opc = Alpha::ANDi; break;
1285 case ISD::OR: Opc = Alpha::BISi; break;
1286 case ISD::XOR: Opc = Alpha::XORi; break;
1287 case ISD::SHL: Opc = Alpha::SLi; break;
1288 case ISD::SRL: Opc = Alpha::SRLi; break;
1289 case ISD::SRA: Opc = Alpha::SRAi; break;
1290 case ISD::MUL: Opc = Alpha::MULQi; break;
1292 Tmp1 = SelectExpr(N.getOperand(0));
1293 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1296 case ISD::AND: Opc = Alpha::AND; break;
1297 case ISD::OR: Opc = Alpha::BIS; break;
1298 case ISD::XOR: Opc = Alpha::XOR; break;
1299 case ISD::SHL: Opc = Alpha::SL; break;
1300 case ISD::SRL: Opc = Alpha::SRL; break;
1301 case ISD::SRA: Opc = Alpha::SRA; break;
1303 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
1307 Tmp1 = SelectExpr(N.getOperand(0));
1308 Tmp2 = SelectExpr(N.getOperand(1));
1309 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1316 ConstantFPSDNode *CN;
1317 if (opcode == ISD::ADD)
1318 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1320 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1321 if (opcode == ISD::SUB
1322 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1323 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1328 Tmp1 = SelectExpr(N.getOperand(0));
1329 Tmp2 = SelectExpr(N.getOperand(1));
1330 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1334 bool isAdd = opcode == ISD::ADD;
1336 //first check for Scaled Adds and Subs!
1337 //Valid for add and sub
1338 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1339 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1340 (SImm == 2 || SImm == 3)) {
1341 bool use4 = SImm == 2;
1342 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1343 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1344 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1345 2, Result).addReg(Tmp2).addImm(SImm);
1347 Tmp1 = SelectExpr(N.getOperand(1));
1348 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1349 2, Result).addReg(Tmp2).addReg(Tmp1);
1352 //Position prevents subs
1353 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
1354 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1355 (SImm == 2 || SImm == 3)) {
1356 bool use4 = SImm == 2;
1357 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1358 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1359 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
1361 Tmp1 = SelectExpr(N.getOperand(0));
1362 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1366 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1367 { //Normal imm add/sub
1368 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1369 Tmp1 = SelectExpr(N.getOperand(0));
1370 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1372 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
1373 { //inverted imm add/sub
1374 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1375 Tmp1 = SelectExpr(N.getOperand(0));
1376 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
1379 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
1381 Tmp1 = SelectExpr(N.getOperand(0));
1384 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
1386 //give up and do the operation
1389 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1390 Tmp1 = SelectExpr(N.getOperand(0));
1391 Tmp2 = SelectExpr(N.getOperand(1));
1392 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1399 Tmp1 = SelectExpr(N.getOperand(0));
1400 Tmp2 = SelectExpr(N.getOperand(1));
1401 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1402 .addReg(Tmp1).addReg(Tmp2);
1405 //check if we can convert into a shift!
1406 if (isSIntImmediate(N.getOperand(1), SImm) &&
1407 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1408 unsigned k = Log2_64(llabs(SImm));
1409 Tmp1 = SelectExpr(N.getOperand(0));
1414 Tmp2 = MakeReg(MVT::i64);
1415 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1417 Tmp3 = MakeReg(MVT::i64);
1418 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1419 unsigned Tmp4 = MakeReg(MVT::i64);
1420 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1422 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1425 unsigned Tmp5 = MakeReg(MVT::i64);
1426 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1427 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1436 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
1438 // If this is a divide by constant, we can emit code using some magic
1439 // constants to implement it as a multiply instead.
1441 if (opcode == ISD::SDIV)
1442 return SelectExpr(BuildSDIVSequence(N));
1444 return SelectExpr(BuildUDIVSequence(N));
1450 const char* opstr = 0;
1452 case ISD::UREM: opstr = "__remqu"; break;
1453 case ISD::SREM: opstr = "__remq"; break;
1454 case ISD::UDIV: opstr = "__divqu"; break;
1455 case ISD::SDIV: opstr = "__divq"; break;
1457 Tmp1 = SelectExpr(N.getOperand(0));
1458 Tmp2 = SelectExpr(N.getOperand(1));
1460 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1461 Tmp3 = SelectExpr(Addr);
1462 //set up regs explicitly (helps Reg alloc)
1463 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1464 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
1465 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1466 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
1467 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
1471 case ISD::FP_TO_UINT:
1472 case ISD::FP_TO_SINT:
1474 assert (DestType == MVT::i64 && "only quads can be loaded to");
1475 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1476 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
1477 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1478 if (SrcType == MVT::f32)
1480 Tmp2 = MakeReg(MVT::f64);
1481 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
1484 Tmp2 = MakeReg(MVT::f64);
1485 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
1486 MoveFP2Int(Tmp2, Result, true);
1493 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1494 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1495 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1497 SDOperand CC = N.getOperand(0);
1499 if (CC.getOpcode() == ISD::SETCC &&
1500 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1501 //FP Setcc -> Select yay!
1504 //for a cmp b: c = a - b;
1509 bool invTest = false;
1512 ConstantFPSDNode *CN;
1513 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
1514 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1515 Tmp3 = SelectExpr(CC.getOperand(0));
1516 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
1517 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1519 Tmp3 = SelectExpr(CC.getOperand(1));
1524 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1525 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1526 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
1527 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1528 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1529 .addReg(Tmp1).addReg(Tmp2);
1532 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1533 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1534 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1535 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1536 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1537 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1538 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1539 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1541 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1546 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1547 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1549 // // Spill the cond to memory and reload it from there.
1550 // unsigned Tmp4 = MakeReg(MVT::f64);
1551 // MoveIntFP(Tmp1, Tmp4, true);
1552 // //now ideally, we don't have to do anything to the flag...
1553 // // Get the condition into the zero flag.
1554 // BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1558 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1559 //and can save stack use
1560 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1561 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1562 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1563 // Get the condition into the zero flag.
1564 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1566 SDOperand CC = N.getOperand(0);
1568 if (CC.getOpcode() == ISD::SETCC &&
1569 !MVT::isInteger(CC.getOperand(0).getValueType()))
1570 { //FP Setcc -> Int Select
1571 Tmp1 = MakeReg(MVT::f64);
1572 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1573 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1574 bool inv = SelectFPSetCC(CC, Tmp1);
1575 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1576 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1579 if (CC.getOpcode() == ISD::SETCC) {
1580 //Int SetCC -> Select
1581 //Dropping the CC is only useful if we are comparing to 0
1582 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
1583 //figure out a few things
1584 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
1587 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1588 if (useImm) //Invert sense to get Imm field right
1589 cCode = ISD::getSetCCInverse(cCode, true);
1593 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1594 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1595 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1596 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1597 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1598 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1599 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1600 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1601 //Technically you could have this CC
1602 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1603 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1604 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1606 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
1609 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1610 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
1612 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1613 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1614 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1618 //Otherwise, fall though
1620 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1621 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1622 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1623 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1631 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1632 int zero_extend_top = 0;
1633 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
1634 ((int32_t)val < 0)) {
1635 //try a small load and zero extend
1637 zero_extend_top = 15;
1640 if (val <= IMM_HIGH && val >= IMM_LOW) {
1641 if(!zero_extend_top)
1642 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1644 Tmp1 = MakeReg(MVT::i64);
1645 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1646 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1649 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1650 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1651 Tmp1 = MakeReg(MVT::i64);
1652 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1653 .addReg(Alpha::R31);
1654 if (!zero_extend_top)
1655 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1657 Tmp3 = MakeReg(MVT::i64);
1658 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1659 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1663 //re-get the val since we are going to mem anyway
1664 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1665 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1667 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1668 unsigned CPI = CP->getConstantPoolIndex(C);
1669 AlphaLowering.restoreGP(BB);
1671 Tmp1 = MakeReg(MVT::i64);
1672 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1673 .addReg(Alpha::R29);
1674 if (EnableAlphaLSMark)
1675 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1677 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1683 if(ISD::FABS == N.getOperand(0).getOpcode())
1685 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1686 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1688 Tmp1 = SelectExpr(N.getOperand(0));
1689 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1694 Tmp1 = SelectExpr(N.getOperand(0));
1695 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1699 assert (DestType == MVT::f32 &&
1700 N.getOperand(0).getValueType() == MVT::f64 &&
1701 "only f64 to f32 conversion supported here");
1702 Tmp1 = SelectExpr(N.getOperand(0));
1703 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
1706 case ISD::FP_EXTEND:
1707 assert (DestType == MVT::f64 &&
1708 N.getOperand(0).getValueType() == MVT::f32 &&
1709 "only f32 to f64 conversion supported here");
1710 Tmp1 = SelectExpr(N.getOperand(0));
1711 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
1714 case ISD::ConstantFP:
1715 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1716 if (CN->isExactlyValue(+0.0)) {
1717 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1718 .addReg(Alpha::F31);
1719 } else if ( CN->isExactlyValue(-0.0)) {
1720 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1721 .addReg(Alpha::F31);
1728 case ISD::SINT_TO_FP:
1730 assert (N.getOperand(0).getValueType() == MVT::i64
1731 && "only quads can be loaded from");
1732 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1733 Tmp2 = MakeReg(MVT::f64);
1734 MoveInt2FP(Tmp1, Tmp2, true);
1735 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1736 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
1740 case ISD::AssertSext:
1741 case ISD::AssertZext:
1742 return SelectExpr(N.getOperand(0));
1749 void AlphaISel::Select(SDOperand N) {
1750 unsigned Tmp1, Tmp2, Opc;
1751 unsigned opcode = N.getOpcode();
1753 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
1754 return; // Already selected.
1756 SDNode *Node = N.Val;
1761 Node->dump(); std::cerr << "\n";
1762 assert(0 && "Node not handled yet!");
1770 MachineBasicBlock *Dest =
1771 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1773 Select(N.getOperand(0));
1774 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1778 case ISD::ImplicitDef:
1780 Select(N.getOperand(0));
1781 BuildMI(BB, Alpha::IDEF, 0,
1782 cast<RegisterSDNode>(N.getOperand(1))->getReg());
1785 case ISD::EntryToken: return; // Noop
1787 case ISD::TokenFactor:
1788 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1789 Select(Node->getOperand(i));
1791 //N.Val->dump(); std::cerr << "\n";
1792 //assert(0 && "Node not handled yet!");
1796 case ISD::CopyToReg:
1798 Select(N.getOperand(0));
1799 Tmp1 = SelectExpr(N.getOperand(2));
1800 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1803 if (N.getOperand(2).getValueType() == MVT::f64 ||
1804 N.getOperand(2).getValueType() == MVT::f32)
1805 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1807 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1813 switch (N.getNumOperands()) {
1815 std::cerr << N.getNumOperands() << "\n";
1816 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1817 std::cerr << N.getOperand(i).getValueType() << "\n";
1819 assert(0 && "Unknown return instruction!");
1821 Select(N.getOperand(0));
1822 Tmp1 = SelectExpr(N.getOperand(1));
1823 switch (N.getOperand(1).getValueType()) {
1824 default: Node->dump();
1825 assert(0 && "All other types should have been promoted!!");
1828 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1832 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1837 Select(N.getOperand(0));
1840 // Just emit a 'ret' instruction
1841 AlphaLowering.restoreRA(BB);
1842 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
1845 case ISD::TRUNCSTORE:
1848 SDOperand Chain = N.getOperand(0);
1849 SDOperand Value = N.getOperand(1);
1850 SDOperand Address = N.getOperand(2);
1853 Tmp1 = SelectExpr(Value); //value
1855 if (opcode == ISD::STORE) {
1856 switch(Value.getValueType()) {
1857 default: assert(0 && "unknown Type in store");
1858 case MVT::i64: Opc = Alpha::STQ; break;
1859 case MVT::f64: Opc = Alpha::STT; break;
1860 case MVT::f32: Opc = Alpha::STS; break;
1862 } else { //ISD::TRUNCSTORE
1863 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1864 default: assert(0 && "unknown Type in store");
1865 case MVT::i1: //FIXME: DAG does not promote this load
1866 case MVT::i8: Opc = Alpha::STB; break;
1867 case MVT::i16: Opc = Alpha::STW; break;
1868 case MVT::i32: Opc = Alpha::STL; break;
1873 if (EnableAlphaLSMark)
1874 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
1877 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1878 if (GASD && !GASD->getGlobal()->isExternal()) {
1879 Tmp2 = MakeReg(MVT::i64);
1880 AlphaLowering.restoreGP(BB);
1881 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1882 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1883 if (EnableAlphaLSMark)
1884 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1886 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1887 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
1888 } else if(Address.getOpcode() == ISD::FrameIndex) {
1889 if (EnableAlphaLSMark)
1890 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1892 BuildMI(BB, Opc, 3).addReg(Tmp1)
1893 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1894 .addReg(Alpha::F31);
1897 SelectAddr(Address, Tmp2, offset);
1898 if (EnableAlphaLSMark)
1899 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1901 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1910 case ISD::CopyFromReg:
1913 case ISD::DYNAMIC_STACKALLOC:
1918 case ISD::CALLSEQ_START:
1919 case ISD::CALLSEQ_END:
1920 Select(N.getOperand(0));
1921 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1923 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
1924 Alpha::ADJUSTSTACKUP;
1925 BuildMI(BB, Opc, 1).addImm(Tmp1);
1929 Select(N.getOperand(0)); //Chain
1930 BuildMI(BB, Alpha::PCLABEL, 2)
1931 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
1934 assert(0 && "Should not be reached!");
1938 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1939 /// into a machine code representation using pattern matching and a machine
1940 /// description file.
1942 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1943 return new AlphaISel(TM);