1 //===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for Alpha.
12 //===----------------------------------------------------------------------===//
15 #include "AlphaRegisterInfo.h"
16 #include "llvm/Constants.h" // FIXME: REMOVE
17 #include "llvm/Function.h"
18 #include "llvm/Module.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/CodeGen/SSARegMap.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/CommandLine.h"
37 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
40 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
41 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
43 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
46 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
49 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
50 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
55 // Alpha Specific DAG Nodes
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
64 //Move an Ireg to a FPreg
66 //Move a FPreg to an Ireg
72 //===----------------------------------------------------------------------===//
73 // AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
75 class AlphaTargetLowering : public TargetLowering {
76 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
78 unsigned GP; //GOT vreg
79 unsigned RA; //Return Address
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
83 //I am having problems with shr n ubyte 1
84 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
86 setSetCCResultContents(ZeroOrOneSetCCResult);
88 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
90 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
92 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
94 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
95 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
97 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
137 computeRegisterProperties();
139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
143 /// LowerOperation - Provide custom lowering hooks for some operations.
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
152 /// LowerCallTo - This hook lowers an abstract call to a function into an
154 virtual std::pair<SDOperand, SDOperand>
155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
159 virtual std::pair<SDOperand, SDOperand>
160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
162 virtual std::pair<SDOperand,SDOperand>
163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
164 const Type *ArgTy, SelectionDAG &DAG);
166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
174 void restoreGP(MachineBasicBlock* BB)
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
178 void restoreRA(MachineBasicBlock* BB)
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
190 /// LowerOperation - Provide custom lowering hooks for some operations.
192 SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
193 MachineFunction &MF = DAG.getMachineFunction();
194 switch (Op.getOpcode()) {
195 default: assert(0 && "Should not custom lower this!");
197 case ISD::SINT_TO_FP:
199 assert (Op.getOperand(0).getValueType() == MVT::i64
200 && "only quads can be loaded from");
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(Op.getOperand(0));
208 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
210 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
211 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
213 DAG.getEntryNode(), Op.getOperand(0),
214 StackSlot, DAG.getSrcValue(NULL));
215 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
216 DAG.getSrcValue(NULL));
218 std::vector<MVT::ValueType> RTs;
219 RTs.push_back(Op.getValueType());
220 std::vector<SDOperand> Ops;
222 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
230 /// AddLiveIn - This helper function adds the specified physical register to the
231 /// MachineFunction as a live in value. It also creates a corresponding virtual
233 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
234 TargetRegisterClass *RC) {
235 assert(RC->contains(PReg) && "Not the correct regclass!");
236 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
237 MF.addLiveIn(PReg, VReg);
241 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
243 //For now, just use variable size stack frame format
245 //In a standard call, the first six items are passed in registers $16
246 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
247 //of argument-to-register correspondence.) The remaining items are
248 //collected in a memory argument list that is a naturally aligned
249 //array of quadwords. In a standard call, this list, if present, must
250 //be passed at 0(SP).
251 //7 ... n 0(SP) ... (n-7)*8(SP)
259 std::vector<SDOperand>
260 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
262 std::vector<SDOperand> ArgValues;
264 MachineFunction &MF = DAG.getMachineFunction();
265 MachineFrameInfo*MFI = MF.getFrameInfo();
267 MachineBasicBlock& BB = MF.front();
269 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
270 Alpha::R19, Alpha::R20, Alpha::R21};
271 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
272 Alpha::F19, Alpha::F20, Alpha::F21};
275 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
276 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
278 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
283 MVT::ValueType VT = getValueType(I->getType());
286 std::cerr << "Unknown Type " << VT << "\n";
290 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
291 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
298 args_int[count] = AddLiveIn(MF, args_int[count],
299 getRegClassFor(MVT::i64));
300 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
302 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
305 DAG.setRoot(argt.getValue(1));
307 // Create the frame index object for this incoming parameter...
308 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
310 // Create the SelectionDAG nodes corresponding to a load
311 //from this parameter
312 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
313 argt = DAG.getLoad(getValueType(I->getType()),
314 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
317 ArgValues.push_back(argt);
320 // If the functions takes variable number of arguments, copy all regs to stack
322 VarArgsOffset = count * 8;
323 std::vector<SDOperand> LS;
324 for (int i = 0; i < 6; ++i) {
325 if (args_int[i] < 1024)
326 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
327 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
328 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
329 if (i == 0) VarArgsBase = FI;
330 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
331 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
332 SDFI, DAG.getSrcValue(NULL)));
334 if (args_float[i] < 1024)
335 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
336 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
337 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
338 SDFI = DAG.getFrameIndex(FI, MVT::i64);
339 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
340 SDFI, DAG.getSrcValue(NULL)));
343 //Set up a token factor with all the stack traffic
344 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
347 // Finally, inform the code generator which regs we return values in.
348 switch (getValueType(F.getReturnType())) {
349 default: assert(0 && "Unknown type!");
350 case MVT::isVoid: break;
356 MF.addLiveOut(Alpha::R0);
360 MF.addLiveOut(Alpha::F0);
364 //return the arguments
368 std::pair<SDOperand, SDOperand>
369 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
370 const Type *RetTy, bool isVarArg,
371 unsigned CallingConv, bool isTailCall,
372 SDOperand Callee, ArgListTy &Args,
376 NumBytes = (Args.size() - 6) * 8;
378 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
379 DAG.getConstant(NumBytes, getPointerTy()));
380 std::vector<SDOperand> args_to_use;
381 for (unsigned i = 0, e = Args.size(); i != e; ++i)
383 switch (getValueType(Args[i].second)) {
384 default: assert(0 && "Unexpected ValueType for argument!");
389 // Promote the integer to 64 bits. If the input type is signed use a
390 // sign extend, otherwise use a zero extend.
391 if (Args[i].second->isSigned())
392 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
394 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
401 args_to_use.push_back(Args[i].first);
404 std::vector<MVT::ValueType> RetVals;
405 MVT::ValueType RetTyVT = getValueType(RetTy);
406 if (RetTyVT != MVT::isVoid)
407 RetVals.push_back(RetTyVT);
408 RetVals.push_back(MVT::Other);
410 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
411 Chain, Callee, args_to_use), 0);
412 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
413 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
414 DAG.getConstant(NumBytes, getPointerTy()));
415 return std::make_pair(TheCall, Chain);
418 std::pair<SDOperand, SDOperand>
419 AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG,
421 // vastart just stores the address of the VarArgsBase and VarArgsOffset
422 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
423 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest,
424 DAG.getSrcValue(NULL));
425 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest,
426 DAG.getConstant(8, MVT::i64));
427 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
428 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
429 DAG.getSrcValue(NULL), MVT::i32);
430 return std::make_pair(S2, S2);
433 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
434 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
435 const Type *ArgTy, SelectionDAG &DAG) {
436 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
437 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
438 DAG.getConstant(8, MVT::i64));
439 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
440 Tmp, DAG.getSrcValue(NULL), MVT::i32);
441 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
442 if (ArgTy->isFloatingPoint())
444 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
445 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
446 DAG.getConstant(8*6, MVT::i64));
447 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
448 Offset, DAG.getConstant(8*6, MVT::i64));
449 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
453 if (ArgTy == Type::IntTy)
454 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
455 DAG.getSrcValue(NULL), MVT::i32);
456 else if (ArgTy == Type::UIntTy)
457 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
458 DAG.getSrcValue(NULL), MVT::i32);
460 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
461 DAG.getSrcValue(NULL));
463 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
464 DAG.getConstant(8, MVT::i64));
465 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
466 Result.getValue(1), NewOffset,
467 Tmp, DAG.getSrcValue(NULL), MVT::i32);
468 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
470 return std::make_pair(Result, Update);
473 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
474 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
476 //Default to returning the input list
477 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src,
478 DAG.getSrcValue(NULL));
479 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
480 Val, Dest, DAG.getSrcValue(NULL));
481 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
482 DAG.getConstant(8, MVT::i64));
483 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
485 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
486 DAG.getConstant(8, MVT::i64));
487 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
488 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
489 return std::make_pair(Result, Result);
492 std::pair<SDOperand, SDOperand> AlphaTargetLowering::
493 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
504 //===--------------------------------------------------------------------===//
505 /// ISel - Alpha specific code to select Alpha machine instructions for
506 /// SelectionDAG operations.
507 //===--------------------------------------------------------------------===//
508 class AlphaISel : public SelectionDAGISel {
510 /// AlphaLowering - This object fully describes how to lower LLVM code to an
511 /// Alpha-specific SelectionDAG.
512 AlphaTargetLowering AlphaLowering;
514 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
515 // for sdiv and udiv until it is put into the future
518 /// ExprMap - As shared expressions are codegen'd, we keep track of which
519 /// vreg the value is produced in, so we only emit one copy of each compiled
521 static const unsigned notIn = (unsigned)(-1);
522 std::map<SDOperand, unsigned> ExprMap;
524 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
525 std::map<SDOperand, unsigned> CCInvMap;
533 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
537 /// InstructionSelectBasicBlock - This callback is invoked by
538 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
539 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
546 // Codegen the basic block.
548 max_depth = DAG.getRoot().getNodeDepth();
549 Select(DAG.getRoot());
554 std::cerr << "COUNT: "
555 << BB->getParent()->getFunction ()->getName() << " "
556 << BB->getNumber() << " "
559 << count_outs << "\n";
561 // Clear state used for selection.
566 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
568 unsigned SelectExpr(SDOperand N);
569 unsigned SelectExprFP(SDOperand N, unsigned Result);
570 void Select(SDOperand N);
572 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
573 void SelectBranchCC(SDOperand N);
574 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
575 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
576 //returns whether the sense of the comparison was inverted
577 bool SelectFPSetCC(SDOperand N, unsigned dst);
579 // dag -> dag expanders for integer divide by constant
580 SDOperand BuildSDIVSequence(SDOperand N);
581 SDOperand BuildUDIVSequence(SDOperand N);
586 void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
587 // If this function has live-in values, emit the copies from pregs to vregs at
588 // the top of the function, before anything else.
589 MachineBasicBlock *BB = MF.begin();
590 if (MF.livein_begin() != MF.livein_end()) {
591 SSARegMap *RegMap = MF.getSSARegMap();
592 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
593 E = MF.livein_end(); LI != E; ++LI) {
594 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
595 if (RC == Alpha::GPRCRegisterClass) {
596 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
598 } else if (RC == Alpha::FPRCRegisterClass) {
599 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
602 assert(0 && "Unknown regclass!");
608 //Find the offset of the arg in it's parent's function
609 static int getValueOffset(const Value* v)
611 static int uniqneg = -1;
615 const Instruction* itarget = dyn_cast<Instruction>(v);
616 const BasicBlock* btarget = itarget->getParent();
617 const Function* ftarget = btarget->getParent();
619 //offset due to earlier BBs
621 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
624 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
629 //Find the offset of the function in it's module
630 static int getFunctionOffset(const Function* fun)
632 const Module* M = fun->getParent();
634 //offset due to earlier BBs
636 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
648 //Factorize a number using the list of constants
649 static bool factorize(int v[], int res[], int size, uint64_t c)
652 while (c != 1 && cont)
655 for(int i = 0; i < size; ++i)
669 //Shamelessly adapted from PPC32
670 // Structure used to return the necessary information to codegen an SDIV as
673 int64_t m; // magic number
674 int64_t s; // shift amount
678 uint64_t m; // magic number
679 int64_t a; // add indicator
680 int64_t s; // shift amount
683 /// magic - calculate the magic numbers required to codegen an integer sdiv as
684 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
686 static struct ms magic(int64_t d) {
688 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
689 const uint64_t two63 = 9223372036854775808ULL; // 2^63
693 t = two63 + ((uint64_t)d >> 63);
694 anc = t - 1 - t%ad; // absolute value of nc
695 p = 63; // initialize p
696 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
697 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
698 q2 = two63/ad; // initialize q2 = 2p/abs(d)
699 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
702 q1 = 2*q1; // update q1 = 2p/abs(nc)
703 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
704 if (r1 >= anc) { // must be unsigned comparison
708 q2 = 2*q2; // update q2 = 2p/abs(d)
709 r2 = 2*r2; // update r2 = rem(2p/abs(d))
710 if (r2 >= ad) { // must be unsigned comparison
715 } while (q1 < delta || (q1 == delta && r1 == 0));
718 if (d < 0) mag.m = -mag.m; // resulting magic number
719 mag.s = p - 64; // resulting shift
723 /// magicu - calculate the magic numbers required to codegen an integer udiv as
724 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
725 static struct mu magicu(uint64_t d)
728 uint64_t nc, delta, q1, r1, q2, r2;
730 magu.a = 0; // initialize "add" indicator
732 p = 63; // initialize p
733 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
734 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
735 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
736 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
739 if (r1 >= nc - r1 ) {
740 q1 = 2*q1 + 1; // update q1
741 r1 = 2*r1 - nc; // update r1
744 q1 = 2*q1; // update q1
745 r1 = 2*r1; // update r1
747 if (r2 + 1 >= d - r2) {
748 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
749 q2 = 2*q2 + 1; // update q2
750 r2 = 2*r2 + 1 - d; // update r2
753 if (q2 >= 0x8000000000000000ull) magu.a = 1;
754 q2 = 2*q2; // update q2
755 r2 = 2*r2 + 1; // update r2
758 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
759 magu.m = q2 + 1; // resulting magic number
760 magu.s = p - 64; // resulting shift
764 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
765 /// return a DAG expression to select that will generate the same value by
766 /// multiplying by a magic number. See:
767 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
768 SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
769 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
770 ms magics = magic(d);
771 // Multiply the numerator (operand 0) by the magic value
772 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
773 ISelDAG->getConstant(magics.m, MVT::i64));
774 // If d > 0 and m < 0, add the numerator
775 if (d > 0 && magics.m < 0)
776 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
777 // If d < 0 and m > 0, subtract the numerator.
778 if (d < 0 && magics.m > 0)
779 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
780 // Shift right algebraic if shift value is nonzero
782 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
783 ISelDAG->getConstant(magics.s, MVT::i64));
784 // Extract the sign bit and add it to the quotient
786 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
787 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
790 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
791 /// return a DAG expression to select that will generate the same value by
792 /// multiplying by a magic number. See:
793 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
794 SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
796 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
797 mu magics = magicu(d);
798 // Multiply the numerator (operand 0) by the magic value
799 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
800 ISelDAG->getConstant(magics.m, MVT::i64));
802 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
803 ISelDAG->getConstant(magics.s, MVT::i64));
805 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
806 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
807 ISelDAG->getConstant(1, MVT::i64));
808 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
809 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
810 ISelDAG->getConstant(magics.s-1, MVT::i64));
816 /// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
817 /// returns zero when the input is not exactly a power of two.
818 static unsigned ExactLog2(uint64_t Val) {
819 if (Val == 0 || (Val & (Val-1))) return 0;
829 //These describe LDAx
830 static const int IMM_LOW = -32768;
831 static const int IMM_HIGH = 32767;
832 static const int IMM_MULT = 65536;
834 static long getUpper16(long l)
836 long y = l / IMM_MULT;
837 if (l % IMM_MULT > IMM_HIGH)
842 static long getLower16(long l)
844 long h = getUpper16(l);
845 return l - h * IMM_MULT;
848 static unsigned GetRelVersion(unsigned opcode)
851 default: assert(0 && "unknown load or store"); return 0;
852 case Alpha::LDQ: return Alpha::LDQr;
853 case Alpha::LDS: return Alpha::LDSr;
854 case Alpha::LDT: return Alpha::LDTr;
855 case Alpha::LDL: return Alpha::LDLr;
856 case Alpha::LDBU: return Alpha::LDBUr;
857 case Alpha::LDWU: return Alpha::LDWUr;
858 case Alpha::STB: return Alpha::STBr;
859 case Alpha::STW: return Alpha::STWr;
860 case Alpha::STL: return Alpha::STLr;
861 case Alpha::STQ: return Alpha::STQr;
862 case Alpha::STS: return Alpha::STSr;
863 case Alpha::STT: return Alpha::STTr;
868 void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
871 if (EnableAlphaFTOI) {
872 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
873 BuildMI(BB, Opc, 1, dst).addReg(src);
876 // Spill the integer to memory and reload it from there.
877 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
878 MachineFunction *F = BB->getParent();
879 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
881 Opc = isDouble ? Alpha::STT : Alpha::STS;
882 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
883 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
884 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
888 void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
891 if (EnableAlphaFTOI) {
892 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
893 BuildMI(BB, Opc, 1, dst).addReg(src);
896 // Spill the integer to memory and reload it from there.
897 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
898 MachineFunction *F = BB->getParent();
899 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
901 Opc = isDouble ? Alpha::STQ : Alpha::STL;
902 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
903 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
904 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
908 bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
910 SDNode *Node = N.Val;
911 unsigned Opc, Tmp1, Tmp2, Tmp3;
912 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
917 switch (SetCC->getCondition()) {
918 default: Node->dump(); assert(0 && "Unknown FP comparison!");
919 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
920 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
921 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
922 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
923 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
924 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
927 ConstantFPSDNode *CN;
928 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
929 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
932 Tmp1 = SelectExpr(N.getOperand(0));
934 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
935 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
938 Tmp2 = SelectExpr(N.getOperand(1));
940 //Can only compare doubles, and dag won't promote for me
941 if (SetCC->getOperand(0).getValueType() == MVT::f32)
943 //assert(0 && "Setcc On float?\n");
944 std::cerr << "Setcc on float!\n";
945 Tmp3 = MakeReg(MVT::f64);
946 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
949 if (SetCC->getOperand(1).getValueType() == MVT::f32)
951 //assert (0 && "Setcc On float?\n");
952 std::cerr << "Setcc on float!\n";
953 Tmp3 = MakeReg(MVT::f64);
954 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
958 if (rev) std::swap(Tmp1, Tmp2);
960 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
964 //Check to see if the load is a constant offset from a base register
965 void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
967 unsigned opcode = N.getOpcode();
968 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
969 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
971 Reg = SelectExpr(N.getOperand(0));
972 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
980 void AlphaISel::SelectBranchCC(SDOperand N)
982 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
983 MachineBasicBlock *Dest =
984 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
985 unsigned Opc = Alpha::WTF;
987 Select(N.getOperand(0)); //chain
988 SDOperand CC = N.getOperand(1);
990 if (CC.getOpcode() == ISD::SETCC)
992 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
993 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
994 //Dropping the CC is only useful if we are comparing to 0
995 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
996 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
1000 ISD::CondCode cCode= SetCC->getCondition();
1002 if(cCode == ISD::SETNE)
1007 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1008 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1009 case ISD::SETLT: Opc = Alpha::BLT; break;
1010 case ISD::SETLE: Opc = Alpha::BLE; break;
1011 case ISD::SETGT: Opc = Alpha::BGT; break;
1012 case ISD::SETGE: Opc = Alpha::BGE; break;
1013 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1014 case ISD::SETUGT: Opc = Alpha::BNE; break;
1015 //Technically you could have this CC
1016 case ISD::SETULE: Opc = Alpha::BEQ; break;
1017 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1018 case ISD::SETNE: Opc = Alpha::BNE; break;
1020 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
1021 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1024 unsigned Tmp1 = SelectExpr(CC);
1026 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1028 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1032 //Any comparison between 2 values should be codegened as an folded
1033 //branch, as moving CC to the integer register is very expensive
1034 //for a cmp b: c = a - b;
1039 bool invTest = false;
1042 ConstantFPSDNode *CN;
1043 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1044 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1045 Tmp3 = SelectExpr(SetCC->getOperand(0));
1046 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1047 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1049 Tmp3 = SelectExpr(SetCC->getOperand(1));
1054 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1055 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1056 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1057 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1058 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1059 .addReg(Tmp1).addReg(Tmp2);
1062 switch (SetCC->getCondition()) {
1063 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1064 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1065 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1066 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1067 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1068 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1069 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
1071 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
1074 abort(); //Should never be reached
1076 //Giveup and do the stupid thing
1077 unsigned Tmp1 = SelectExpr(CC);
1078 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1081 abort(); //Should never be reached
1084 unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
1086 unsigned Tmp1, Tmp2, Tmp3;
1088 SDNode *Node = N.Val;
1089 MVT::ValueType DestType = N.getValueType();
1090 unsigned opcode = N.getOpcode();
1095 assert(0 && "Node not handled!\n");
1098 BuildMI(BB, Alpha::IDEF, 0, Result);
1103 if(ISD::FABS == N.getOperand(0).getOpcode())
1105 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1106 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1108 Tmp1 = SelectExpr(N.getOperand(0));
1109 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1114 Tmp1 = SelectExpr(N.getOperand(0));
1115 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1120 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1121 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1122 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1124 SDOperand CC = N.getOperand(0);
1125 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1127 if (CC.getOpcode() == ISD::SETCC &&
1128 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1129 { //FP Setcc -> Select yay!
1132 //for a cmp b: c = a - b;
1137 bool invTest = false;
1140 ConstantFPSDNode *CN;
1141 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1142 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1143 Tmp3 = SelectExpr(SetCC->getOperand(0));
1144 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1145 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1147 Tmp3 = SelectExpr(SetCC->getOperand(1));
1152 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1153 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1154 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1155 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1156 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1157 .addReg(Tmp1).addReg(Tmp2);
1160 switch (SetCC->getCondition()) {
1161 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1162 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1163 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1164 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1165 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1166 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1167 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1169 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1174 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1175 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1177 // // Spill the cond to memory and reload it from there.
1178 // unsigned Tmp4 = MakeReg(MVT::f64);
1179 // MoveIntFP(Tmp1, Tmp4, true);
1180 // //now ideally, we don't have to do anything to the flag...
1181 // // Get the condition into the zero flag.
1182 // BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1188 assert (DestType == MVT::f32 &&
1189 N.getOperand(0).getValueType() == MVT::f64 &&
1190 "only f64 to f32 conversion supported here");
1191 Tmp1 = SelectExpr(N.getOperand(0));
1192 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1195 case ISD::FP_EXTEND:
1196 assert (DestType == MVT::f64 &&
1197 N.getOperand(0).getValueType() == MVT::f32 &&
1198 "only f32 to f64 conversion supported here");
1199 Tmp1 = SelectExpr(N.getOperand(0));
1200 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1203 case ISD::ConstantFP:
1204 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1205 if (CN->isExactlyValue(+0.0)) {
1206 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1207 .addReg(Alpha::F31);
1208 } else if ( CN->isExactlyValue(-0.0)) {
1209 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1210 .addReg(Alpha::F31);
1222 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1224 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1226 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1228 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1232 ConstantFPSDNode *CN;
1233 if (opcode == ISD::SUB
1234 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1235 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1237 Tmp2 = SelectExpr(N.getOperand(1));
1238 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 case ISD::SINT_TO_FP:
1248 assert (N.getOperand(0).getValueType() == MVT::i64
1249 && "only quads can be loaded from");
1250 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1251 Tmp2 = MakeReg(MVT::f64);
1252 MoveInt2FP(Tmp1, Tmp2, true);
1253 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1254 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1258 assert(0 && "should not get here");
1262 unsigned AlphaISel::SelectExpr(SDOperand N) {
1264 unsigned Tmp1, Tmp2 = 0, Tmp3;
1266 unsigned opcode = N.getOpcode();
1268 SDNode *Node = N.Val;
1269 MVT::ValueType DestType = N.getValueType();
1271 unsigned &Reg = ExprMap[N];
1272 if (Reg) return Reg;
1274 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
1275 Reg = Result = (N.getValueType() != MVT::Other) ?
1276 MakeReg(N.getValueType()) : notIn;
1278 // If this is a call instruction, make sure to prepare ALL of the result
1279 // values as well as the chain.
1280 if (Node->getNumValues() == 1)
1281 Reg = Result = notIn; // Void call, just a chain.
1283 Result = MakeReg(Node->getValueType(0));
1284 ExprMap[N.getValue(0)] = Result;
1285 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1286 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1287 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
1291 if ((DestType == MVT::f64 || DestType == MVT::f32)
1292 && opcode != ISD::CALL && opcode != ISD::TAILCALL)
1293 return SelectExprFP(N, Result);
1298 assert(0 && "Node not handled!\n");
1303 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1304 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1305 Tmp1 = SelectExpr(N.getOperand(0));
1306 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1310 Tmp1 = SelectExpr(N.getOperand(0));
1311 Tmp2 = SelectExpr(N.getOperand(1));
1312 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
1316 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1317 Tmp1 = SelectExpr(N.getOperand(0));
1318 Tmp2 = SelectExpr(N.getOperand(1));
1319 Tmp3 = MakeReg(MVT::i64);
1320 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1321 unsigned V1 = MakeReg(MVT::i64);
1322 unsigned V2 = MakeReg(MVT::i64);
1323 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1325 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1327 unsigned IRes = MakeReg(MVT::i64);
1328 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1329 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1333 BuildMI(BB, Alpha::IDEF, 0, Result);
1337 case ISD::DYNAMIC_STACKALLOC:
1338 // Generate both result values.
1339 if (Result != notIn)
1340 ExprMap[N.getValue(1)] = notIn; // Generate the token
1342 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1344 // FIXME: We are currently ignoring the requested alignment for handling
1345 // greater than the stack alignment. This will need to be revisited at some
1346 // point. Align = N.getOperand(2);
1348 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1349 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1350 std::cerr << "Cannot allocate stack object with greater alignment than"
1351 << " the stack alignment yet!";
1355 Select(N.getOperand(0));
1356 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1358 if (CN->getValue() < 32000)
1360 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1361 .addImm(-CN->getValue()).addReg(Alpha::R30);
1363 Tmp1 = SelectExpr(N.getOperand(1));
1364 // Subtract size from stack pointer, thereby allocating some space.
1365 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1368 Tmp1 = SelectExpr(N.getOperand(1));
1369 // Subtract size from stack pointer, thereby allocating some space.
1370 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1373 // Put a pointer to the space into the result register, by copying the stack
1375 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
1378 case ISD::ConstantPool:
1379 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1380 AlphaLowering.restoreGP(BB);
1381 Tmp2 = MakeReg(MVT::i64);
1382 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1383 .addReg(Alpha::R29);
1384 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1388 case ISD::FrameIndex:
1389 BuildMI(BB, Alpha::LDA, 2, Result)
1390 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1391 .addReg(Alpha::F31);
1399 // Make sure we generate both values.
1400 if (Result != notIn)
1401 ExprMap[N.getValue(1)] = notIn; // Generate the token
1403 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1405 SDOperand Chain = N.getOperand(0);
1406 SDOperand Address = N.getOperand(1);
1411 if (opcode == ISD::LOAD)
1412 switch (Node->getValueType(0)) {
1413 default: Node->dump(); assert(0 && "Bad load!");
1414 case MVT::i64: Opc = Alpha::LDQ; break;
1415 case MVT::f64: Opc = Alpha::LDT; break;
1416 case MVT::f32: Opc = Alpha::LDS; break;
1419 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1420 default: Node->dump(); assert(0 && "Bad sign extend!");
1421 case MVT::i32: Opc = Alpha::LDL;
1422 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
1423 case MVT::i16: Opc = Alpha::LDWU;
1424 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
1425 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
1426 case MVT::i8: Opc = Alpha::LDBU;
1427 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
1431 if (EnableAlphaLSMark) {
1432 i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))
1434 j = getFunctionOffset(BB->getParent()->getFunction());
1436 if (GlobalAddressSDNode *GASD =
1437 dyn_cast<GlobalAddressSDNode>(Address)) {
1438 if (GASD->getGlobal()->isExternal()) {
1439 Tmp1 = SelectExpr(Address);
1440 if (EnableAlphaLSMark)
1441 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1442 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp1);
1444 Tmp1 = MakeReg(MVT::i64);
1445 AlphaLowering.restoreGP(BB);
1446 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1447 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1448 if (EnableAlphaLSMark)
1449 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1450 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1451 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
1453 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1454 AlphaLowering.restoreGP(BB);
1456 Tmp1 = MakeReg(MVT::i64);
1457 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1458 .addReg(Alpha::R29);
1459 if (EnableAlphaLSMark)
1460 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1461 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1462 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1463 } else if(Address.getOpcode() == ISD::FrameIndex) {
1464 if (EnableAlphaLSMark)
1465 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1466 BuildMI(BB, Opc, 2, Result)
1467 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1468 .addReg(Alpha::F31);
1471 SelectAddr(Address, Tmp1, offset);
1472 if (EnableAlphaLSMark)
1473 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1474 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1479 case ISD::GlobalAddress:
1480 AlphaLowering.restoreGP(BB);
1483 if (EnableAlphaLSMark)
1484 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(0).addImm(0).addImm(getUID());
1486 BuildMI(BB, Alpha::LDQl, 2, Result)
1487 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1488 .addReg(Alpha::R29);
1494 Select(N.getOperand(0));
1496 // The chain for this call is now lowered.
1497 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
1499 //grab the arguments
1500 std::vector<unsigned> argvregs;
1501 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
1502 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1503 argvregs.push_back(SelectExpr(N.getOperand(i)));
1506 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
1508 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
1509 Alpha::R19, Alpha::R20, Alpha::R21};
1510 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
1511 Alpha::F19, Alpha::F20, Alpha::F21};
1512 switch(N.getOperand(i+2).getValueType()) {
1515 N.getOperand(i).Val->dump();
1516 std::cerr << "Type for " << i << " is: " <<
1517 N.getOperand(i+2).getValueType() << "\n";
1518 assert(0 && "Unknown value type for call");
1524 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1525 .addReg(argvregs[i]);
1529 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1530 .addReg(argvregs[i]);
1535 for (int i = 6, e = argvregs.size(); i < e; ++i)
1537 switch(N.getOperand(i+2).getValueType()) {
1540 N.getOperand(i).Val->dump();
1541 std::cerr << "Type for " << i << " is: " <<
1542 N.getOperand(i+2).getValueType() << "\n";
1543 assert(0 && "Unknown value type for call");
1549 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1550 .addReg(Alpha::R30);
1553 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1554 .addReg(Alpha::R30);
1557 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1558 .addReg(Alpha::R30);
1562 //build the right kind of call
1563 if (GlobalAddressSDNode *GASD =
1564 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1566 if (GASD->getGlobal()->isExternal()) {
1567 //use safe calling convention
1568 AlphaLowering.restoreGP(BB);
1570 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1572 //use PC relative branch call
1573 AlphaLowering.restoreGP(BB);
1574 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1575 .addGlobalAddress(GASD->getGlobal(),true);
1578 else if (ExternalSymbolSDNode *ESSDN =
1579 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
1581 AlphaLowering.restoreGP(BB);
1583 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1585 //no need to restore GP as we are doing an indirect call
1586 Tmp1 = SelectExpr(N.getOperand(1));
1587 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1588 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1591 //push the result into a virtual register
1593 switch (Node->getValueType(0)) {
1594 default: Node->dump(); assert(0 && "Unknown value type for call result!");
1595 case MVT::Other: return notIn;
1601 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1605 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1608 return Result+N.ResNo;
1611 case ISD::SIGN_EXTEND_INREG:
1613 //do SDIV opt for all levels of ints if not dividing by a constant
1614 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1615 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
1617 unsigned Tmp4 = MakeReg(MVT::f64);
1618 unsigned Tmp5 = MakeReg(MVT::f64);
1619 unsigned Tmp6 = MakeReg(MVT::f64);
1620 unsigned Tmp7 = MakeReg(MVT::f64);
1621 unsigned Tmp8 = MakeReg(MVT::f64);
1622 unsigned Tmp9 = MakeReg(MVT::f64);
1624 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1625 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1626 MoveInt2FP(Tmp1, Tmp4, true);
1627 MoveInt2FP(Tmp2, Tmp5, true);
1628 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1629 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1630 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1631 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
1632 MoveFP2Int(Tmp9, Result, true);
1636 //Alpha has instructions for a bunch of signed 32 bit stuff
1637 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
1639 switch (N.getOperand(0).getOpcode()) {
1644 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1645 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1646 //FIXME: first check for Scaled Adds and Subs!
1647 ConstantSDNode* CSD = NULL;
1648 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1649 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1650 (CSD->getValue() == 2 || CSD->getValue() == 3))
1652 bool use4 = CSD->getValue() == 2;
1653 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1654 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1655 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1656 2,Result).addReg(Tmp1).addReg(Tmp2);
1658 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1659 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1660 (CSD->getValue() == 2 || CSD->getValue() == 3))
1662 bool use4 = CSD->getValue() == 2;
1663 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1664 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1665 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1667 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1668 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1669 { //Normal imm add/sub
1670 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
1671 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1672 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1673 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1677 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
1678 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1679 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1680 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1684 default: break; //Fall Though;
1686 } //Every thing else fall though too, including unhandled opcodes above
1687 Tmp1 = SelectExpr(N.getOperand(0));
1688 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1689 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
1690 switch(MVN->getExtraValueType())
1694 assert(0 && "Sign Extend InReg not there yet");
1698 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
1702 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1705 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1708 Tmp2 = MakeReg(MVT::i64);
1709 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
1710 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
1718 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1719 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1720 bool isConst = false;
1723 //Tmp1 = SelectExpr(N.getOperand(0));
1724 if(N.getOperand(1).getOpcode() == ISD::Constant &&
1725 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1728 switch (SetCC->getCondition()) {
1729 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1731 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1733 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1735 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1736 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1737 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1739 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1740 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1742 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1743 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1744 case ISD::SETNE: {//Handle this one special
1745 //std::cerr << "Alpha does not have a setne.\n";
1747 Tmp1 = SelectExpr(N.getOperand(0));
1748 Tmp2 = SelectExpr(N.getOperand(1));
1749 Tmp3 = MakeReg(MVT::i64);
1750 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1751 //Remeber we have the Inv for this CC
1754 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1759 Tmp1 = SelectExpr(N.getOperand(0));
1761 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1762 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1764 Tmp2 = SelectExpr(N.getOperand(1));
1765 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 } else { //if (dir == 2) {
1768 Tmp1 = SelectExpr(N.getOperand(1));
1769 Tmp2 = SelectExpr(N.getOperand(0));
1770 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1774 Tmp1 = MakeReg(MVT::f64);
1775 bool inv = SelectFPSetCC(N, Tmp1);
1777 //now arrange for Result (int) to have a 1 or 0
1778 Tmp2 = MakeReg(MVT::i64);
1779 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1780 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1781 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
1787 case ISD::CopyFromReg:
1791 // Make sure we generate both values.
1792 if (Result != notIn)
1793 ExprMap[N.getValue(1)] = notIn; // Generate the token
1795 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1797 SDOperand Chain = N.getOperand(0);
1800 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1801 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1802 if (DestType == MVT::f32 || DestType == MVT::f64)
1803 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1805 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1809 //Most of the plain arithmetic and logic share the same form, and the same
1810 //constant immediate test
1813 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1814 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
1816 Tmp1 = SelectExpr(N.getOperand(0));
1817 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1823 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1825 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1826 unsigned int build = 0;
1827 for(int i = 0; i < 8; ++i)
1829 if ((k & 0x00FF) == 0x00FF)
1831 else if ((k & 0x00FF) != 0)
1832 { build = 0; break; }
1837 Tmp1 = SelectExpr(N.getOperand(0));
1838 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1843 //Check operand(0) == Not
1844 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1845 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1846 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
1849 case ISD::AND: Opc = Alpha::BIC; break;
1850 case ISD::OR: Opc = Alpha::ORNOT; break;
1851 case ISD::XOR: Opc = Alpha::EQV; break;
1853 Tmp1 = SelectExpr(N.getOperand(1));
1854 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1855 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1858 //Check operand(1) == Not
1859 if (N.getOperand(1).getOpcode() == ISD::XOR &&
1860 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1861 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1864 case ISD::AND: Opc = Alpha::BIC; break;
1865 case ISD::OR: Opc = Alpha::ORNOT; break;
1866 case ISD::XOR: Opc = Alpha::EQV; break;
1868 Tmp1 = SelectExpr(N.getOperand(0));
1869 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1870 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1879 if(N.getOperand(1).getOpcode() == ISD::Constant &&
1880 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1883 case ISD::AND: Opc = Alpha::ANDi; break;
1884 case ISD::OR: Opc = Alpha::BISi; break;
1885 case ISD::XOR: Opc = Alpha::XORi; break;
1886 case ISD::SHL: Opc = Alpha::SLi; break;
1887 case ISD::SRL: Opc = Alpha::SRLi; break;
1888 case ISD::SRA: Opc = Alpha::SRAi; break;
1889 case ISD::MUL: Opc = Alpha::MULQi; break;
1891 Tmp1 = SelectExpr(N.getOperand(0));
1892 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1893 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1896 case ISD::AND: Opc = Alpha::AND; break;
1897 case ISD::OR: Opc = Alpha::BIS; break;
1898 case ISD::XOR: Opc = Alpha::XOR; break;
1899 case ISD::SHL: Opc = Alpha::SL; break;
1900 case ISD::SRL: Opc = Alpha::SRL; break;
1901 case ISD::SRA: Opc = Alpha::SRA; break;
1902 case ISD::MUL: Opc = Alpha::MULQ; break;
1904 Tmp1 = SelectExpr(N.getOperand(0));
1905 Tmp2 = SelectExpr(N.getOperand(1));
1906 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1913 bool isAdd = opcode == ISD::ADD;
1915 //first check for Scaled Adds and Subs!
1916 //Valid for add and sub
1917 ConstantSDNode* CSD = NULL;
1918 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1919 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1920 (CSD->getValue() == 2 || CSD->getValue() == 3))
1922 bool use4 = CSD->getValue() == 2;
1923 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1924 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1925 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1926 2, Result).addReg(Tmp2).addImm(CSD->getValue());
1928 Tmp1 = SelectExpr(N.getOperand(1));
1929 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1930 2, Result).addReg(Tmp2).addReg(Tmp1);
1933 //Position prevents subs
1934 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
1935 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1936 (CSD->getValue() == 2 || CSD->getValue() == 3))
1938 bool use4 = CSD->getValue() == 2;
1939 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1940 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1941 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1942 .addImm(CSD->getValue());
1944 Tmp1 = SelectExpr(N.getOperand(0));
1945 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1949 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1950 CSD->getValue() <= 255)
1951 { //Normal imm add/sub
1952 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1953 Tmp1 = SelectExpr(N.getOperand(0));
1954 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
1957 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1958 CSD->getSignExtended() <= 32767 &&
1959 CSD->getSignExtended() >= -32767)
1961 Tmp1 = SelectExpr(N.getOperand(0));
1962 Tmp2 = (long)CSD->getSignExtended();
1965 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1967 //give up and do the operation
1970 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1971 Tmp1 = SelectExpr(N.getOperand(0));
1972 Tmp2 = SelectExpr(N.getOperand(1));
1973 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1980 ConstantSDNode* CSD;
1981 //check if we can convert into a shift!
1982 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1983 (int64_t)CSD->getSignExtended() != 0 &&
1984 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1986 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1987 Tmp1 = SelectExpr(N.getOperand(0));
1992 Tmp2 = MakeReg(MVT::i64);
1993 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1995 Tmp3 = MakeReg(MVT::i64);
1996 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1997 unsigned Tmp4 = MakeReg(MVT::i64);
1998 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1999 if ((int64_t)CSD->getSignExtended() > 0)
2000 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2003 unsigned Tmp5 = MakeReg(MVT::i64);
2004 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2005 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2014 ConstantSDNode* CSD;
2015 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2016 ((int64_t)CSD->getSignExtended() >= 2 ||
2017 (int64_t)CSD->getSignExtended() <= -2))
2019 // If this is a divide by constant, we can emit code using some magic
2020 // constants to implement it as a multiply instead.
2022 if (opcode == ISD::SDIV)
2023 return SelectExpr(BuildSDIVSequence(N));
2025 return SelectExpr(BuildUDIVSequence(N));
2031 //FIXME: alpha really doesn't support any of these operations,
2032 // the ops are expanded into special library calls with
2033 // special calling conventions
2034 //Restore GP because it is a call after all...
2036 case ISD::UREM: Opc = Alpha::REMQU; break;
2037 case ISD::SREM: Opc = Alpha::REMQ; break;
2038 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2039 case ISD::SDIV: Opc = Alpha::DIVQ; break;
2041 Tmp1 = SelectExpr(N.getOperand(0));
2042 Tmp2 = SelectExpr(N.getOperand(1));
2043 //set up regs explicitly (helps Reg alloc)
2044 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
2045 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
2046 AlphaLowering.restoreGP(BB);
2047 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
2048 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
2051 case ISD::FP_TO_UINT:
2052 case ISD::FP_TO_SINT:
2054 assert (DestType == MVT::i64 && "only quads can be loaded to");
2055 MVT::ValueType SrcType = N.getOperand(0).getValueType();
2056 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
2057 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2058 if (SrcType == MVT::f32)
2060 Tmp2 = MakeReg(MVT::f64);
2061 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2064 Tmp2 = MakeReg(MVT::f64);
2065 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
2066 MoveFP2Int(Tmp2, Result, true);
2073 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
2074 //and can save stack use
2075 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
2076 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2077 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2078 // Get the condition into the zero flag.
2079 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2081 SDOperand CC = N.getOperand(0);
2082 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2084 if (CC.getOpcode() == ISD::SETCC &&
2085 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2086 { //FP Setcc -> Int Select
2087 Tmp1 = MakeReg(MVT::f64);
2088 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2089 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2090 bool inv = SelectFPSetCC(CC, Tmp1);
2091 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2092 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2095 if (CC.getOpcode() == ISD::SETCC) {
2096 //Int SetCC -> Select
2097 //Dropping the CC is only useful if we are comparing to 0
2098 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2099 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
2101 //figure out a few things
2102 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
2103 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2106 ISD::CondCode cCode= SetCC->getCondition();
2107 if (useImm) //Invert sense to get Imm field right
2108 cCode = ISD::getSetCCInverse(cCode, true);
2112 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2113 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2114 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2115 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2116 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2117 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2118 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2119 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2120 //Technically you could have this CC
2121 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2122 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2123 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2125 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2128 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2129 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
2130 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
2133 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2134 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2135 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2139 //Otherwise, fall though
2141 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
2142 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2143 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2144 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2152 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
2153 if (val <= IMM_HIGH && val >= IMM_LOW) {
2154 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2156 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2157 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2158 Tmp1 = MakeReg(MVT::i64);
2159 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2160 .addReg(Alpha::R31);
2161 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2164 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2166 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2167 unsigned CPI = CP->getConstantPoolIndex(C);
2168 AlphaLowering.restoreGP(BB);
2170 Tmp1 = MakeReg(MVT::i64);
2171 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2172 .addReg(Alpha::R29);
2173 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2183 void AlphaISel::Select(SDOperand N) {
2184 unsigned Tmp1, Tmp2, Opc;
2185 unsigned opcode = N.getOpcode();
2187 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
2188 return; // Already selected.
2190 SDNode *Node = N.Val;
2195 Node->dump(); std::cerr << "\n";
2196 assert(0 && "Node not handled yet!");
2204 MachineBasicBlock *Dest =
2205 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2207 Select(N.getOperand(0));
2208 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2212 case ISD::ImplicitDef:
2214 Select(N.getOperand(0));
2215 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2218 case ISD::EntryToken: return; // Noop
2220 case ISD::TokenFactor:
2221 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2222 Select(Node->getOperand(i));
2224 //N.Val->dump(); std::cerr << "\n";
2225 //assert(0 && "Node not handled yet!");
2229 case ISD::CopyToReg:
2231 Select(N.getOperand(0));
2232 Tmp1 = SelectExpr(N.getOperand(1));
2233 Tmp2 = cast<RegSDNode>(N)->getReg();
2236 if (N.getOperand(1).getValueType() == MVT::f64 ||
2237 N.getOperand(1).getValueType() == MVT::f32)
2238 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2240 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2246 switch (N.getNumOperands()) {
2248 std::cerr << N.getNumOperands() << "\n";
2249 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2250 std::cerr << N.getOperand(i).getValueType() << "\n";
2252 assert(0 && "Unknown return instruction!");
2254 Select(N.getOperand(0));
2255 Tmp1 = SelectExpr(N.getOperand(1));
2256 switch (N.getOperand(1).getValueType()) {
2257 default: Node->dump();
2258 assert(0 && "All other types should have been promoted!!");
2261 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2265 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2270 Select(N.getOperand(0));
2273 // Just emit a 'ret' instruction
2274 AlphaLowering.restoreRA(BB);
2275 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26);
2278 case ISD::TRUNCSTORE:
2281 SDOperand Chain = N.getOperand(0);
2282 SDOperand Value = N.getOperand(1);
2283 SDOperand Address = N.getOperand(2);
2286 Tmp1 = SelectExpr(Value); //value
2288 if (opcode == ISD::STORE) {
2289 switch(Value.getValueType()) {
2290 default: assert(0 && "unknown Type in store");
2291 case MVT::i64: Opc = Alpha::STQ; break;
2292 case MVT::f64: Opc = Alpha::STT; break;
2293 case MVT::f32: Opc = Alpha::STS; break;
2295 } else { //ISD::TRUNCSTORE
2296 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2297 default: assert(0 && "unknown Type in store");
2298 case MVT::i1: //FIXME: DAG does not promote this load
2299 case MVT::i8: Opc = Alpha::STB; break;
2300 case MVT::i16: Opc = Alpha::STW; break;
2301 case MVT::i32: Opc = Alpha::STL; break;
2306 if (EnableAlphaLSMark) {
2308 getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2309 j = getFunctionOffset(BB->getParent()->getFunction());
2312 if (GlobalAddressSDNode *GASD =
2313 dyn_cast<GlobalAddressSDNode>(Address)) {
2314 if (GASD->getGlobal()->isExternal()) {
2315 Tmp2 = SelectExpr(Address);
2316 if (EnableAlphaLSMark)
2317 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2318 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
2320 Tmp2 = MakeReg(MVT::i64);
2321 AlphaLowering.restoreGP(BB);
2322 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2323 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2324 if (EnableAlphaLSMark)
2325 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2326 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2327 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
2329 } else if(Address.getOpcode() == ISD::FrameIndex) {
2330 if (EnableAlphaLSMark)
2331 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2332 BuildMI(BB, Opc, 3).addReg(Tmp1)
2333 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2334 .addReg(Alpha::F31);
2337 SelectAddr(Address, Tmp2, offset);
2338 if (EnableAlphaLSMark)
2339 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2340 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2349 case ISD::CopyFromReg:
2352 case ISD::DYNAMIC_STACKALLOC:
2357 case ISD::CALLSEQ_START:
2358 case ISD::CALLSEQ_END:
2359 Select(N.getOperand(0));
2360 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2362 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
2363 Alpha::ADJUSTSTACKUP;
2364 BuildMI(BB, Opc, 1).addImm(Tmp1);
2368 Select(N.getOperand(0)); //Chain
2369 BuildMI(BB, Alpha::PCLABEL, 2)
2370 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2373 assert(0 && "Should not be reached!");
2377 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2378 /// into a machine code representation using pattern matching and a machine
2379 /// description file.
2381 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2382 return new AlphaISel(TM);