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 "AlphaTargetMachine.h"
17 #include "AlphaISelLowering.h"
18 #include "llvm/Constants.h" // FIXME: REMOVE
19 #include "llvm/Function.h"
20 #include "llvm/Module.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/SSARegMap.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/CommandLine.h"
39 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
40 cl::desc("Use the FP div instruction for integer div when possible"),
42 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
43 cl::desc("Print estimates on live ins and outs"),
45 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
46 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
52 //===--------------------------------------------------------------------===//
53 /// ISel - Alpha specific code to select Alpha machine instructions for
54 /// SelectionDAG operations.
55 //===--------------------------------------------------------------------===//
56 class AlphaISel : public SelectionDAGISel {
58 /// AlphaLowering - This object fully describes how to lower LLVM code to an
59 /// Alpha-specific SelectionDAG.
60 AlphaTargetLowering AlphaLowering;
62 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
63 // for sdiv and udiv until it is put into the future
66 /// ExprMap - As shared expressions are codegen'd, we keep track of which
67 /// vreg the value is produced in, so we only emit one copy of each compiled
69 static const unsigned notIn = (unsigned)(-1);
70 std::map<SDOperand, unsigned> ExprMap;
72 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
73 std::map<SDOperand, unsigned> CCInvMap;
81 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
85 virtual const char *getPassName() const {
86 return "Alpha Pattern Instruction Selection";
89 /// InstructionSelectBasicBlock - This callback is invoked by
90 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
91 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
98 // Codegen the basic block.
100 max_depth = DAG.getRoot().getNodeDepth();
101 Select(DAG.getRoot());
106 std::cerr << "COUNT: "
107 << BB->getParent()->getFunction ()->getName() << " "
108 << BB->getNumber() << " "
111 << count_outs << "\n";
113 // Clear state used for selection.
118 unsigned SelectExpr(SDOperand N);
119 void Select(SDOperand N);
121 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
122 void SelectBranchCC(SDOperand N);
123 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
124 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
125 //returns whether the sense of the comparison was inverted
126 bool SelectFPSetCC(SDOperand N, unsigned dst);
128 // dag -> dag expanders for integer divide by constant
129 SDOperand BuildSDIVSequence(SDOperand N);
130 SDOperand BuildUDIVSequence(SDOperand N);
135 static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
137 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
139 Imm = CN->getSignExtended();
147 // isSIntImmediateBounded - This method tests to see if a constant operand
148 // bounded s.t. low <= Imm <= high
149 // If so Imm will receive the 64 bit value.
150 static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
151 int64_t low, int64_t high) {
152 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
156 static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
158 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
160 Imm = (uint64_t)CN->getValue();
168 static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
169 uint64_t low, uint64_t high) {
170 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
175 static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
177 fun = type = offset = 0;
180 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
182 const Module* M = GV->getParent();
183 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
185 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
187 const Function* F = Arg->getParent();
188 const Module* M = F->getParent();
189 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
191 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
193 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
194 assert(dyn_cast<PointerType>(I->getType()));
196 const BasicBlock* bb = I->getParent();
197 const Function* F = bb->getParent();
198 const Module* M = F->getParent();
199 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
201 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
202 offset += ii->size();
203 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
205 } else if (const Constant* C = dyn_cast<Constant>(v)) {
206 //Don't know how to look these up yet
209 assert(0 && "Error in value marking");
211 //type = 4: register spilling
212 //type = 5: global address loading or constant loading
221 //Factorize a number using the list of constants
222 static bool factorize(int v[], int res[], int size, uint64_t c)
225 while (c != 1 && cont)
228 for(int i = 0; i < size; ++i)
242 //These describe LDAx
243 static const int IMM_LOW = -32768;
244 static const int IMM_HIGH = 32767;
245 static const int IMM_MULT = 65536;
247 static long getUpper16(long l)
249 long y = l / IMM_MULT;
250 if (l % IMM_MULT > IMM_HIGH)
255 static long getLower16(long l)
257 long h = getUpper16(l);
258 return l - h * IMM_MULT;
261 static unsigned GetRelVersion(unsigned opcode)
264 default: assert(0 && "unknown load or store"); return 0;
265 case Alpha::LDQ: return Alpha::LDQr;
266 case Alpha::LDS: return Alpha::LDSr;
267 case Alpha::LDT: return Alpha::LDTr;
268 case Alpha::LDL: return Alpha::LDLr;
269 case Alpha::LDBU: return Alpha::LDBUr;
270 case Alpha::LDWU: return Alpha::LDWUr;
271 case Alpha::STB: return Alpha::STBr;
272 case Alpha::STW: return Alpha::STWr;
273 case Alpha::STL: return Alpha::STLr;
274 case Alpha::STQ: return Alpha::STQr;
275 case Alpha::STS: return Alpha::STSr;
276 case Alpha::STT: return Alpha::STTr;
281 void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
284 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
285 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
286 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
289 // Spill the integer to memory and reload it from there.
290 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
291 MachineFunction *F = BB->getParent();
292 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
294 if (EnableAlphaLSMark)
295 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
297 Opc = isDouble ? Alpha::STT : Alpha::STS;
298 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
300 if (EnableAlphaLSMark)
301 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
303 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
304 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
308 void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
311 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
312 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
313 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
316 // Spill the integer to memory and reload it from there.
317 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
318 MachineFunction *F = BB->getParent();
319 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
321 if (EnableAlphaLSMark)
322 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
324 Opc = isDouble ? Alpha::STQ : Alpha::STL;
325 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
327 if (EnableAlphaLSMark)
328 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
330 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
331 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
335 bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
337 SDNode *SetCC = N.Val;
338 unsigned Opc, Tmp1, Tmp2, Tmp3;
339 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
344 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
345 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
346 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
347 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
348 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
349 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
350 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
353 ConstantFPSDNode *CN;
354 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
355 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
358 Tmp1 = SelectExpr(N.getOperand(0));
360 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
361 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
364 Tmp2 = SelectExpr(N.getOperand(1));
366 //Can only compare doubles, and dag won't promote for me
367 if (SetCC->getOperand(0).getValueType() == MVT::f32)
369 //assert(0 && "Setcc On float?\n");
370 std::cerr << "Setcc on float!\n";
371 Tmp3 = MakeReg(MVT::f64);
372 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
375 if (SetCC->getOperand(1).getValueType() == MVT::f32)
377 //assert (0 && "Setcc On float?\n");
378 std::cerr << "Setcc on float!\n";
379 Tmp3 = MakeReg(MVT::f64);
380 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
384 if (rev) std::swap(Tmp1, Tmp2);
386 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
390 //Check to see if the load is a constant offset from a base register
391 void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
393 unsigned opcode = N.getOpcode();
394 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
395 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
397 Reg = SelectExpr(N.getOperand(0));
398 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
406 void AlphaISel::SelectBranchCC(SDOperand N)
408 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
409 MachineBasicBlock *Dest =
410 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
411 unsigned Opc = Alpha::WTF;
413 Select(N.getOperand(0)); //chain
414 SDOperand CC = N.getOperand(1);
416 if (CC.getOpcode() == ISD::SETCC)
418 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
419 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
420 //Dropping the CC is only useful if we are comparing to 0
421 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
422 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
426 if(cCode == ISD::SETNE)
431 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
432 case ISD::SETEQ: Opc = Alpha::BEQ; break;
433 case ISD::SETLT: Opc = Alpha::BLT; break;
434 case ISD::SETLE: Opc = Alpha::BLE; break;
435 case ISD::SETGT: Opc = Alpha::BGT; break;
436 case ISD::SETGE: Opc = Alpha::BGE; break;
437 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
438 case ISD::SETUGT: Opc = Alpha::BNE; break;
439 //Technically you could have this CC
440 case ISD::SETULE: Opc = Alpha::BEQ; break;
441 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
442 case ISD::SETNE: Opc = Alpha::BNE; break;
444 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
445 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
448 unsigned Tmp1 = SelectExpr(CC);
450 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
452 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
456 //Any comparison between 2 values should be codegened as an folded
457 //branch, as moving CC to the integer register is very expensive
458 //for a cmp b: c = a - b;
463 bool invTest = false;
466 ConstantFPSDNode *CN;
467 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
468 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
469 Tmp3 = SelectExpr(CC.getOperand(0));
470 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
471 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
473 Tmp3 = SelectExpr(CC.getOperand(1));
478 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
479 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
480 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
481 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
482 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
483 .addReg(Tmp1).addReg(Tmp2);
487 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
488 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
489 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
490 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
491 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
492 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
493 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
495 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
498 abort(); //Should never be reached
500 //Giveup and do the stupid thing
501 unsigned Tmp1 = SelectExpr(CC);
502 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
505 abort(); //Should never be reached
508 unsigned AlphaISel::SelectExpr(SDOperand N) {
510 unsigned Tmp1, Tmp2 = 0, Tmp3;
512 unsigned opcode = N.getOpcode();
516 SDNode *Node = N.Val;
517 MVT::ValueType DestType = N.getValueType();
518 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
520 unsigned &Reg = ExprMap[N];
523 switch(N.getOpcode()) {
525 Reg = Result = (N.getValueType() != MVT::Other) ?
526 MakeReg(N.getValueType()) : notIn;
528 case ISD::AssertSext:
529 case ISD::AssertZext:
530 return Reg = SelectExpr(N.getOperand(0));
533 // If this is a call instruction, make sure to prepare ALL of the result
534 // values as well as the chain.
535 if (Node->getNumValues() == 1)
536 Reg = Result = notIn; // Void call, just a chain.
538 Result = MakeReg(Node->getValueType(0));
539 ExprMap[N.getValue(0)] = Result;
540 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
541 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
542 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
550 assert(0 && "Node not handled!\n");
552 case ISD::READCYCLECOUNTER:
553 Select(N.getOperand(0)); //Select chain
554 BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
560 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
561 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
562 Tmp1 = SelectExpr(N.getOperand(0));
563 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
567 Tmp1 = SelectExpr(N.getOperand(0));
568 Tmp2 = SelectExpr(N.getOperand(1));
569 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
573 //MULHU - Ra<63>*Rb - Rb<63>*Ra
574 Tmp1 = SelectExpr(N.getOperand(0));
575 Tmp2 = SelectExpr(N.getOperand(1));
576 Tmp3 = MakeReg(MVT::i64);
577 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
578 unsigned V1 = MakeReg(MVT::i64);
579 unsigned V2 = MakeReg(MVT::i64);
580 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
582 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
584 unsigned IRes = MakeReg(MVT::i64);
585 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
586 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
590 BuildMI(BB, Alpha::IDEF, 0, Result);
594 case ISD::DYNAMIC_STACKALLOC:
595 // Generate both result values.
597 ExprMap[N.getValue(1)] = notIn; // Generate the token
599 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
601 // FIXME: We are currently ignoring the requested alignment for handling
602 // greater than the stack alignment. This will need to be revisited at some
603 // point. Align = N.getOperand(2);
605 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
606 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
607 std::cerr << "Cannot allocate stack object with greater alignment than"
608 << " the stack alignment yet!";
612 Select(N.getOperand(0));
613 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
614 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
616 Tmp1 = SelectExpr(N.getOperand(1));
617 // Subtract size from stack pointer, thereby allocating some space.
618 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
621 // Put a pointer to the space into the result register, by copying the stack
623 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
626 case ISD::ConstantPool:
627 Tmp1 = BB->getParent()->getConstantPool()->
628 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
629 AlphaLowering.restoreGP(BB);
630 Tmp2 = MakeReg(MVT::i64);
631 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
633 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
637 case ISD::FrameIndex:
638 BuildMI(BB, Alpha::LDA, 2, Result)
639 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
648 // Make sure we generate both values.
650 ExprMap[N.getValue(1)] = notIn; // Generate the token
652 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
654 SDOperand Chain = N.getOperand(0);
655 SDOperand Address = N.getOperand(1);
660 if (opcode == ISD::LOAD)
661 switch (Node->getValueType(0)) {
662 default: Node->dump(); assert(0 && "Bad load!");
663 case MVT::i64: Opc = Alpha::LDQ; break;
664 case MVT::f64: Opc = Alpha::LDT; break;
665 case MVT::f32: Opc = Alpha::LDS; break;
668 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
669 default: Node->dump(); assert(0 && "Bad sign extend!");
670 case MVT::i32: Opc = Alpha::LDL;
671 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
672 case MVT::i16: Opc = Alpha::LDWU;
673 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
674 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
675 case MVT::i8: Opc = Alpha::LDBU;
676 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
680 if (EnableAlphaLSMark)
681 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
684 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
685 if (GASD && !GASD->getGlobal()->isExternal()) {
686 Tmp1 = MakeReg(MVT::i64);
687 AlphaLowering.restoreGP(BB);
688 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
689 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
690 if (EnableAlphaLSMark)
691 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
693 BuildMI(BB, GetRelVersion(Opc), 2, Result)
694 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
695 } else if (ConstantPoolSDNode *CP =
696 dyn_cast<ConstantPoolSDNode>(Address)) {
697 unsigned CPIdx = BB->getParent()->getConstantPool()->
698 getConstantPoolIndex(CP->get());
699 AlphaLowering.restoreGP(BB);
701 Tmp1 = MakeReg(MVT::i64);
702 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
704 if (EnableAlphaLSMark)
705 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
707 BuildMI(BB, GetRelVersion(Opc), 2, Result)
708 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
709 } else if(Address.getOpcode() == ISD::FrameIndex) {
710 if (EnableAlphaLSMark)
711 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
713 BuildMI(BB, Opc, 2, Result)
714 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
718 SelectAddr(Address, Tmp1, offset);
719 if (EnableAlphaLSMark)
720 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
722 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
727 case ISD::GlobalAddress:
728 AlphaLowering.restoreGP(BB);
731 Reg = Result = MakeReg(MVT::i64);
733 if (EnableAlphaLSMark)
734 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
737 BuildMI(BB, Alpha::LDQl, 2, Result)
738 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
742 case ISD::ExternalSymbol:
743 AlphaLowering.restoreGP(BB);
746 Reg = Result = MakeReg(MVT::i64);
748 if (EnableAlphaLSMark)
749 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
752 BuildMI(BB, Alpha::LDQl, 2, Result)
753 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
760 Select(N.getOperand(0));
762 // The chain for this call is now lowered.
763 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
766 std::vector<unsigned> argvregs;
767 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
768 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
769 argvregs.push_back(SelectExpr(N.getOperand(i)));
772 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
774 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
775 Alpha::R19, Alpha::R20, Alpha::R21};
776 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
777 Alpha::F19, Alpha::F20, Alpha::F21};
778 switch(N.getOperand(i+2).getValueType()) {
781 N.getOperand(i).Val->dump();
782 std::cerr << "Type for " << i << " is: " <<
783 N.getOperand(i+2).getValueType() << "\n";
784 assert(0 && "Unknown value type for call");
790 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
791 .addReg(argvregs[i]);
794 BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
795 .addReg(argvregs[i]);
798 BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
799 .addReg(argvregs[i]);
804 for (int i = 6, e = argvregs.size(); i < e; ++i)
806 switch(N.getOperand(i+2).getValueType()) {
809 N.getOperand(i).Val->dump();
810 std::cerr << "Type for " << i << " is: " <<
811 N.getOperand(i+2).getValueType() << "\n";
812 assert(0 && "Unknown value type for call");
818 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
822 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
826 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
831 //build the right kind of call
832 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
833 if (GASD && !GASD->getGlobal()->isExternal()) {
834 //use PC relative branch call
835 AlphaLowering.restoreGP(BB);
836 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
837 .addGlobalAddress(GASD->getGlobal(),true);
839 //no need to restore GP as we are doing an indirect call
840 Tmp1 = SelectExpr(N.getOperand(1));
841 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
842 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
845 //push the result into a virtual register
847 switch (Node->getValueType(0)) {
848 default: Node->dump(); assert(0 && "Unknown value type for call result!");
849 case MVT::Other: return notIn;
851 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
854 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
857 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
860 return Result+N.ResNo;
863 case ISD::SIGN_EXTEND_INREG:
865 //do SDIV opt for all levels of ints if not dividing by a constant
866 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
867 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
869 unsigned Tmp4 = MakeReg(MVT::f64);
870 unsigned Tmp5 = MakeReg(MVT::f64);
871 unsigned Tmp6 = MakeReg(MVT::f64);
872 unsigned Tmp7 = MakeReg(MVT::f64);
873 unsigned Tmp8 = MakeReg(MVT::f64);
874 unsigned Tmp9 = MakeReg(MVT::f64);
876 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
877 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
878 MoveInt2FP(Tmp1, Tmp4, true);
879 MoveInt2FP(Tmp2, Tmp5, true);
880 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
881 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
882 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
883 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
884 MoveFP2Int(Tmp9, Result, true);
888 //Alpha has instructions for a bunch of signed 32 bit stuff
889 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
890 switch (N.getOperand(0).getOpcode()) {
895 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
896 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
897 //FIXME: first check for Scaled Adds and Subs!
898 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
899 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
901 bool use4 = SImm == 2;
902 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
903 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
904 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
905 2,Result).addReg(Tmp1).addReg(Tmp2);
907 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
908 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
910 bool use4 = SImm == 2;
911 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
912 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
913 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
915 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
916 { //Normal imm add/sub
917 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
918 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
919 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
921 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
922 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
923 { //handle canonicalization
924 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
925 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
926 SImm = 0 - ((SImm << 32) >> 32);
927 assert(SImm >= 0 && SImm <= 255);
928 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
932 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
933 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
934 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
939 default: break; //Fall Though;
941 } //Every thing else fall though too, including unhandled opcodes above
942 Tmp1 = SelectExpr(N.getOperand(0));
943 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
944 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
947 assert(0 && "Sign Extend InReg not there yet");
951 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
955 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
958 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
961 Tmp2 = MakeReg(MVT::i64);
962 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
963 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
971 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
972 if (MVT::isInteger(N.getOperand(0).getValueType())) {
973 bool isConst = false;
976 //Tmp1 = SelectExpr(N.getOperand(0));
977 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
981 default: Node->dump(); assert(0 && "Unknown integer comparison!");
983 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
985 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
987 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
988 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
989 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
991 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
992 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
994 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
995 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
996 case ISD::SETNE: {//Handle this one special
997 //std::cerr << "Alpha does not have a setne.\n";
999 Tmp1 = SelectExpr(N.getOperand(0));
1000 Tmp2 = SelectExpr(N.getOperand(1));
1001 Tmp3 = MakeReg(MVT::i64);
1002 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1003 //Remeber we have the Inv for this CC
1006 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1011 Tmp1 = SelectExpr(N.getOperand(0));
1013 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1015 Tmp2 = SelectExpr(N.getOperand(1));
1016 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1018 } else { //if (dir == 2) {
1019 Tmp1 = SelectExpr(N.getOperand(1));
1020 Tmp2 = SelectExpr(N.getOperand(0));
1021 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1025 Tmp1 = MakeReg(MVT::f64);
1026 bool inv = SelectFPSetCC(N, Tmp1);
1028 //now arrange for Result (int) to have a 1 or 0
1029 Tmp2 = MakeReg(MVT::i64);
1030 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1031 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1032 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
1037 case ISD::CopyFromReg:
1041 // Make sure we generate both values.
1042 if (Result != notIn)
1043 ExprMap[N.getValue(1)] = notIn; // Generate the token
1045 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1047 SDOperand Chain = N.getOperand(0);
1050 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1051 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1052 switch(N.getValue(0).getValueType()) {
1054 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1057 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1060 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1066 //Most of the plain arithmetic and logic share the same form, and the same
1067 //constant immediate test
1070 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1071 Tmp1 = SelectExpr(N.getOperand(0));
1072 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1078 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
1080 unsigned int build = 0;
1081 for(int i = 0; i < 8; ++i)
1083 if ((UImm & 0x00FF) == 0x00FF)
1085 else if ((UImm & 0x00FF) != 0)
1086 { build = 0; break; }
1091 Tmp1 = SelectExpr(N.getOperand(0));
1092 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1097 //Check operand(0) == Not
1098 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1099 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
1101 case ISD::AND: Opc = Alpha::BIC; break;
1102 case ISD::OR: Opc = Alpha::ORNOT; break;
1103 case ISD::XOR: Opc = Alpha::EQV; break;
1105 Tmp1 = SelectExpr(N.getOperand(1));
1106 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1107 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1110 //Check operand(1) == Not
1111 if (N.getOperand(1).getOpcode() == ISD::XOR &&
1112 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
1114 case ISD::AND: Opc = Alpha::BIC; break;
1115 case ISD::OR: Opc = Alpha::ORNOT; break;
1116 case ISD::XOR: Opc = Alpha::EQV; break;
1118 Tmp1 = SelectExpr(N.getOperand(0));
1119 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1120 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1128 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
1130 case ISD::AND: Opc = Alpha::ANDi; break;
1131 case ISD::OR: Opc = Alpha::BISi; break;
1132 case ISD::XOR: Opc = Alpha::XORi; break;
1133 case ISD::SHL: Opc = Alpha::SLi; break;
1134 case ISD::SRL: Opc = Alpha::SRLi; break;
1135 case ISD::SRA: Opc = Alpha::SRAi; break;
1136 case ISD::MUL: Opc = Alpha::MULQi; break;
1138 Tmp1 = SelectExpr(N.getOperand(0));
1139 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1142 case ISD::AND: Opc = Alpha::AND; break;
1143 case ISD::OR: Opc = Alpha::BIS; break;
1144 case ISD::XOR: Opc = Alpha::XOR; break;
1145 case ISD::SHL: Opc = Alpha::SL; break;
1146 case ISD::SRL: Opc = Alpha::SRL; break;
1147 case ISD::SRA: Opc = Alpha::SRA; break;
1148 case ISD::MUL: Opc = Alpha::MULQ; break;
1150 Tmp1 = SelectExpr(N.getOperand(0));
1151 Tmp2 = SelectExpr(N.getOperand(1));
1152 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1159 bool isAdd = opcode == ISD::ADD;
1161 //first check for Scaled Adds and Subs!
1162 //Valid for add and sub
1163 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1164 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1165 (SImm == 2 || SImm == 3)) {
1166 bool use4 = SImm == 2;
1167 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1168 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1169 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1170 2, Result).addReg(Tmp2).addImm(SImm);
1172 Tmp1 = SelectExpr(N.getOperand(1));
1173 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1174 2, Result).addReg(Tmp2).addReg(Tmp1);
1177 //Position prevents subs
1178 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
1179 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1180 (SImm == 2 || SImm == 3)) {
1181 bool use4 = SImm == 2;
1182 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1183 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1184 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
1186 Tmp1 = SelectExpr(N.getOperand(0));
1187 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1191 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1192 { //Normal imm add/sub
1193 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1197 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
1198 { //inverted imm add/sub
1199 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1200 Tmp1 = SelectExpr(N.getOperand(0));
1201 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
1204 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
1206 Tmp1 = SelectExpr(N.getOperand(0));
1209 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
1211 //give up and do the operation
1214 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1215 Tmp1 = SelectExpr(N.getOperand(0));
1216 Tmp2 = SelectExpr(N.getOperand(1));
1217 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1225 if (opcode == ISD::FADD)
1226 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1227 else if (opcode == ISD::FSUB)
1228 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1229 else if (opcode == ISD::FMUL)
1230 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1232 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1233 Tmp1 = SelectExpr(N.getOperand(0));
1234 Tmp2 = SelectExpr(N.getOperand(1));
1235 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1240 //check if we can convert into a shift!
1241 if (isSIntImmediate(N.getOperand(1), SImm) &&
1242 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1243 unsigned k = Log2_64(llabs(SImm));
1244 Tmp1 = SelectExpr(N.getOperand(0));
1249 Tmp2 = MakeReg(MVT::i64);
1250 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1252 Tmp3 = MakeReg(MVT::i64);
1253 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1254 unsigned Tmp4 = MakeReg(MVT::i64);
1255 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1257 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1260 unsigned Tmp5 = MakeReg(MVT::i64);
1261 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1262 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1272 const char* opstr = 0;
1274 case ISD::UREM: opstr = "__remqu"; break;
1275 case ISD::SREM: opstr = "__remq"; break;
1276 case ISD::UDIV: opstr = "__divqu"; break;
1277 case ISD::SDIV: opstr = "__divq"; break;
1279 Tmp1 = SelectExpr(N.getOperand(0));
1280 Tmp2 = SelectExpr(N.getOperand(1));
1282 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1283 Tmp3 = SelectExpr(Addr);
1284 //set up regs explicitly (helps Reg alloc)
1285 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1286 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
1287 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1288 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
1289 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
1293 case ISD::FP_TO_UINT:
1294 case ISD::FP_TO_SINT:
1296 assert (DestType == MVT::i64 && "only quads can be loaded to");
1297 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1298 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
1299 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1300 if (SrcType == MVT::f32)
1302 Tmp2 = MakeReg(MVT::f64);
1303 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1306 Tmp2 = MakeReg(MVT::f64);
1307 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1308 MoveFP2Int(Tmp2, Result, true);
1315 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1316 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1317 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1319 SDOperand CC = N.getOperand(0);
1321 if (CC.getOpcode() == ISD::SETCC &&
1322 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1323 //FP Setcc -> Select yay!
1326 //for a cmp b: c = a - b;
1331 bool invTest = false;
1334 ConstantFPSDNode *CN;
1335 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
1336 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1337 Tmp3 = SelectExpr(CC.getOperand(0));
1338 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
1339 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1341 Tmp3 = SelectExpr(CC.getOperand(1));
1346 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1347 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1348 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
1349 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1350 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1351 .addReg(Tmp1).addReg(Tmp2);
1354 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1355 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1356 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1357 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1358 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1359 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1360 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1361 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1363 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1368 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1369 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1371 // // Spill the cond to memory and reload it from there.
1372 // unsigned Tmp4 = MakeReg(MVT::f64);
1373 // MoveIntFP(Tmp1, Tmp4, true);
1374 // //now ideally, we don't have to do anything to the flag...
1375 // // Get the condition into the zero flag.
1376 // BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1380 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1381 //and can save stack use
1382 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1383 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1384 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1385 // Get the condition into the zero flag.
1386 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1388 SDOperand CC = N.getOperand(0);
1390 if (CC.getOpcode() == ISD::SETCC &&
1391 !MVT::isInteger(CC.getOperand(0).getValueType()))
1392 { //FP Setcc -> Int Select
1393 Tmp1 = MakeReg(MVT::f64);
1394 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1395 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1396 bool inv = SelectFPSetCC(CC, Tmp1);
1397 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1398 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1401 if (CC.getOpcode() == ISD::SETCC) {
1402 //Int SetCC -> Select
1403 //Dropping the CC is only useful if we are comparing to 0
1404 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
1405 //figure out a few things
1406 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
1409 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1410 if (useImm) //Invert sense to get Imm field right
1411 cCode = ISD::getSetCCInverse(cCode, true);
1415 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1416 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1417 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1418 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1419 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1420 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1421 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1422 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1423 //Technically you could have this CC
1424 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1425 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1426 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1428 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
1431 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1432 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
1434 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1435 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1436 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1440 //Otherwise, fall though
1442 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1443 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1444 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1445 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1453 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1454 int zero_extend_top = 0;
1455 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
1456 ((int32_t)val < 0)) {
1457 //try a small load and zero extend
1459 zero_extend_top = 15;
1462 if (val <= IMM_HIGH && val >= IMM_LOW) {
1463 if(!zero_extend_top)
1464 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1466 Tmp1 = MakeReg(MVT::i64);
1467 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1468 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1471 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1472 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1473 Tmp1 = MakeReg(MVT::i64);
1474 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1475 .addReg(Alpha::R31);
1476 if (!zero_extend_top)
1477 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1479 Tmp3 = MakeReg(MVT::i64);
1480 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1481 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1485 //re-get the val since we are going to mem anyway
1486 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1487 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1489 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1490 unsigned CPI = CP->getConstantPoolIndex(C);
1491 AlphaLowering.restoreGP(BB);
1493 Tmp1 = MakeReg(MVT::i64);
1494 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1495 .addReg(Alpha::R29);
1496 if (EnableAlphaLSMark)
1497 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1499 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1505 if(ISD::FABS == N.getOperand(0).getOpcode())
1507 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1508 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1509 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1511 Tmp1 = SelectExpr(N.getOperand(0));
1512 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1513 , 2, Result).addReg(Tmp1).addReg(Tmp1);
1518 Tmp1 = SelectExpr(N.getOperand(0));
1519 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1520 .addReg(Alpha::F31).addReg(Tmp1);
1524 assert (DestType == MVT::f32 &&
1525 N.getOperand(0).getValueType() == MVT::f64 &&
1526 "only f64 to f32 conversion supported here");
1527 Tmp1 = SelectExpr(N.getOperand(0));
1528 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
1531 case ISD::FP_EXTEND:
1532 assert (DestType == MVT::f64 &&
1533 N.getOperand(0).getValueType() == MVT::f32 &&
1534 "only f32 to f64 conversion supported here");
1535 Tmp1 = SelectExpr(N.getOperand(0));
1536 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1539 case ISD::ConstantFP:
1540 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1541 if (CN->isExactlyValue(+0.0)) {
1542 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1543 , 2, Result).addReg(Alpha::F31)
1544 .addReg(Alpha::F31);
1545 } else if ( CN->isExactlyValue(-0.0)) {
1546 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1547 2, Result).addReg(Alpha::F31)
1548 .addReg(Alpha::F31);
1555 case ISD::SINT_TO_FP:
1557 assert (N.getOperand(0).getValueType() == MVT::i64
1558 && "only quads can be loaded from");
1559 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1560 Tmp2 = MakeReg(MVT::f64);
1561 MoveInt2FP(Tmp1, Tmp2, true);
1562 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1563 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1567 case ISD::AssertSext:
1568 case ISD::AssertZext:
1569 return SelectExpr(N.getOperand(0));
1576 void AlphaISel::Select(SDOperand N) {
1577 unsigned Tmp1, Tmp2, Opc;
1578 unsigned opcode = N.getOpcode();
1580 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
1581 return; // Already selected.
1583 SDNode *Node = N.Val;
1588 Node->dump(); std::cerr << "\n";
1589 assert(0 && "Node not handled yet!");
1597 MachineBasicBlock *Dest =
1598 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1600 Select(N.getOperand(0));
1601 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1605 case ISD::ImplicitDef:
1607 Select(N.getOperand(0));
1608 BuildMI(BB, Alpha::IDEF, 0,
1609 cast<RegisterSDNode>(N.getOperand(1))->getReg());
1612 case ISD::EntryToken: return; // Noop
1614 case ISD::TokenFactor:
1615 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1616 Select(Node->getOperand(i));
1618 //N.Val->dump(); std::cerr << "\n";
1619 //assert(0 && "Node not handled yet!");
1623 case ISD::CopyToReg:
1625 Select(N.getOperand(0));
1626 Tmp1 = SelectExpr(N.getOperand(2));
1627 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1630 switch(N.getOperand(2).getValueType()) {
1632 BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1635 BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1638 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1646 switch (N.getNumOperands()) {
1648 std::cerr << N.getNumOperands() << "\n";
1649 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1650 std::cerr << N.getOperand(i).getValueType() << "\n";
1652 assert(0 && "Unknown return instruction!");
1654 Select(N.getOperand(0));
1655 Tmp1 = SelectExpr(N.getOperand(1));
1656 switch (N.getOperand(1).getValueType()) {
1657 default: Node->dump();
1658 assert(0 && "All other types should have been promoted!!");
1660 BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1663 BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1667 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1672 Select(N.getOperand(0));
1675 // Just emit a 'ret' instruction
1676 AlphaLowering.restoreRA(BB);
1677 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
1680 case ISD::TRUNCSTORE:
1683 SDOperand Chain = N.getOperand(0);
1684 SDOperand Value = N.getOperand(1);
1685 SDOperand Address = N.getOperand(2);
1688 Tmp1 = SelectExpr(Value); //value
1690 if (opcode == ISD::STORE) {
1691 switch(Value.getValueType()) {
1692 default: assert(0 && "unknown Type in store");
1693 case MVT::i64: Opc = Alpha::STQ; break;
1694 case MVT::f64: Opc = Alpha::STT; break;
1695 case MVT::f32: Opc = Alpha::STS; break;
1697 } else { //ISD::TRUNCSTORE
1698 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1699 default: assert(0 && "unknown Type in store");
1700 case MVT::i8: Opc = Alpha::STB; break;
1701 case MVT::i16: Opc = Alpha::STW; break;
1702 case MVT::i32: Opc = Alpha::STL; break;
1707 if (EnableAlphaLSMark)
1708 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
1711 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1712 if (GASD && !GASD->getGlobal()->isExternal()) {
1713 Tmp2 = MakeReg(MVT::i64);
1714 AlphaLowering.restoreGP(BB);
1715 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1716 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1717 if (EnableAlphaLSMark)
1718 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1720 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1721 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
1722 } else if(Address.getOpcode() == ISD::FrameIndex) {
1723 if (EnableAlphaLSMark)
1724 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1726 BuildMI(BB, Opc, 3).addReg(Tmp1)
1727 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1728 .addReg(Alpha::F31);
1731 SelectAddr(Address, Tmp2, offset);
1732 if (EnableAlphaLSMark)
1733 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1735 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1744 case ISD::CopyFromReg:
1747 case ISD::DYNAMIC_STACKALLOC:
1752 case ISD::CALLSEQ_START:
1753 case ISD::CALLSEQ_END:
1754 Select(N.getOperand(0));
1755 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1757 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
1758 Alpha::ADJUSTSTACKUP;
1759 BuildMI(BB, Opc, 1).addImm(Tmp1);
1763 Select(N.getOperand(0)); //Chain
1764 BuildMI(BB, Alpha::PCLABEL, 2)
1765 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
1768 assert(0 && "Should not be reached!");
1772 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1773 /// into a machine code representation using pattern matching and a machine
1774 /// description file.
1776 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1777 return new AlphaISel(TM);