blob: 08e1898798bb9c7b0a248746ae47f53426cd4d0c [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#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"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000038 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000044 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
Andrew Lenharth59009192005-05-04 19:12:09 +000045 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000047 cl::desc("Print estimates on live ins and outs"),
48 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
51 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
Jeff Cohen00b168892005-07-27 06:12:32 +000067 FTOI,
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000068 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharthec151362005-06-26 22:23:06 +000094 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000095 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +000096
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000098 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
Andrew Lenharthec151362005-06-26 22:23:06 +0000101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // 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);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// 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);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Chris Lattnere0fe2252005-07-05 19:58:54 +0000159 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
160 Value *VAListV, SelectionDAG &DAG);
161 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
162 SDOperand DestP, Value *DestV,
163 SelectionDAG &DAG);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000165 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
166 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000167
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000168 void restoreGP(MachineBasicBlock* BB)
169 {
170 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
171 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000172 void restoreRA(MachineBasicBlock* BB)
173 {
174 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
175 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000176 unsigned getRA()
177 {
178 return RA;
179 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000180
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 };
182}
183
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000184/// LowerOperation - Provide custom lowering hooks for some operations.
185///
186SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
187 MachineFunction &MF = DAG.getMachineFunction();
188 switch (Op.getOpcode()) {
189 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000190#if 0
191 case ISD::SINT_TO_FP:
192 {
193 assert (Op.getOperand(0).getValueType() == MVT::i64
194 && "only quads can be loaded from");
195 SDOperand SRC;
196 if (EnableAlphaFTOI)
197 {
198 std::vector<MVT::ValueType> RTs;
199 RTs.push_back(Op.getValueType());
200 std::vector<SDOperand> Ops;
201 Ops.push_back(Op.getOperand(0));
202 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
203 } else {
204 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
205 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Jeff Cohen00b168892005-07-27 06:12:32 +0000206 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
207 DAG.getEntryNode(), Op.getOperand(0),
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000208 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000209 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
210 DAG.getSrcValue(NULL));
211 }
212 std::vector<MVT::ValueType> RTs;
213 RTs.push_back(Op.getValueType());
214 std::vector<SDOperand> Ops;
215 Ops.push_back(SRC);
216 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
217 }
218#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000219 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000220 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000221}
222
223
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000224/// AddLiveIn - This helper function adds the specified physical register to the
225/// MachineFunction as a live in value. It also creates a corresponding virtual
226/// register for it.
227static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
228 TargetRegisterClass *RC) {
229 assert(RC->contains(PReg) && "Not the correct regclass!");
230 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
231 MF.addLiveIn(PReg, VReg);
232 return VReg;
233}
234
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
236
237//For now, just use variable size stack frame format
238
239//In a standard call, the first six items are passed in registers $16
240//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
241//of argument-to-register correspondence.) The remaining items are
242//collected in a memory argument list that is a naturally aligned
243//array of quadwords. In a standard call, this list, if present, must
244//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000245//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000247// //#define FP $15
248// //#define RA $26
249// //#define PV $27
250// //#define GP $29
251// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000252
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000254AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255{
256 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000257
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000259 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 MachineBasicBlock& BB = MF.front();
262
Misha Brukman4633f1c2005-04-21 23:13:11 +0000263 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000264 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000265 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000266 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000268
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000269 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000270 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000271
Chris Lattnere4d5c442005-03-15 04:54:21 +0000272 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000273 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000274 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000275 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000276 unsigned Vreg;
277 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000279 default:
280 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 abort();
282 case MVT::f64:
283 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000284 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
285 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 break;
287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
290 case MVT::i32:
291 case MVT::i64:
Jeff Cohen00b168892005-07-27 06:12:32 +0000292 args_int[count] = AddLiveIn(MF, args_int[count],
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000293 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000294 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000295 if (VT != MVT::i64)
296 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000297 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000298 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000299 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 } else { //more args
301 // Create the frame index object for this incoming parameter...
302 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000303
304 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000305 //from this parameter
306 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000307 argt = DAG.getLoad(getValueType(I->getType()),
308 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000309 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000310 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 ArgValues.push_back(argt);
312 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000313
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000314 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000315 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000316 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000317 std::vector<SDOperand> LS;
318 for (int i = 0; i < 6; ++i) {
319 if (args_int[i] < 1024)
320 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
321 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000322 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000323 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000324 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000325 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000326 SDFI, DAG.getSrcValue(NULL)));
Jeff Cohen00b168892005-07-27 06:12:32 +0000327
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000328 if (args_float[i] < 1024)
329 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
330 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000331 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
332 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000333 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000334 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000335 }
336
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000337 //Set up a token factor with all the stack traffic
338 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
339 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000340
341 // Finally, inform the code generator which regs we return values in.
342 switch (getValueType(F.getReturnType())) {
343 default: assert(0 && "Unknown type!");
344 case MVT::isVoid: break;
345 case MVT::i1:
346 case MVT::i8:
347 case MVT::i16:
348 case MVT::i32:
349 case MVT::i64:
350 MF.addLiveOut(Alpha::R0);
351 break;
352 case MVT::f32:
353 case MVT::f64:
354 MF.addLiveOut(Alpha::F0);
355 break;
356 }
357
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000358 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000359 return ArgValues;
360}
361
362std::pair<SDOperand, SDOperand>
363AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000364 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000365 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000366 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000367 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000368 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000369 if (Args.size() > 6)
370 NumBytes = (Args.size() - 6) * 8;
371
Chris Lattner16cd04d2005-05-12 23:24:06 +0000372 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000373 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000374 std::vector<SDOperand> args_to_use;
375 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000376 {
377 switch (getValueType(Args[i].second)) {
378 default: assert(0 && "Unexpected ValueType for argument!");
379 case MVT::i1:
380 case MVT::i8:
381 case MVT::i16:
382 case MVT::i32:
383 // Promote the integer to 64 bits. If the input type is signed use a
384 // sign extend, otherwise use a zero extend.
385 if (Args[i].second->isSigned())
386 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
387 else
388 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
389 break;
390 case MVT::i64:
391 case MVT::f64:
392 case MVT::f32:
393 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000394 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000395 args_to_use.push_back(Args[i].first);
396 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000397
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000398 std::vector<MVT::ValueType> RetVals;
399 MVT::ValueType RetTyVT = getValueType(RetTy);
400 if (RetTyVT != MVT::isVoid)
401 RetVals.push_back(RetTyVT);
402 RetVals.push_back(MVT::Other);
403
Misha Brukman4633f1c2005-04-21 23:13:11 +0000404 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000406 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000407 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000408 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000409 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000410}
411
Chris Lattnere0fe2252005-07-05 19:58:54 +0000412SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
413 Value *VAListV, SelectionDAG &DAG) {
414 // vastart stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000415 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000416 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000417 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000418 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000419 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000420 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
421 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000422 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000423}
424
425std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000426LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
427 const Type *ArgTy, SelectionDAG &DAG) {
428 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
429 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000430 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000431 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000432 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000433 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000434 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000435 if (ArgTy->isFloatingPoint())
436 {
437 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
438 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000439 DAG.getConstant(8*6, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000440 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000441 Offset, DAG.getConstant(8*6, MVT::i64));
442 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
443 }
444
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000445 SDOperand Result;
446 if (ArgTy == Type::IntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000447 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
448 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000449 else if (ArgTy == Type::UIntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000450 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
451 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000452 else
Jeff Cohen00b168892005-07-27 06:12:32 +0000453 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 DAG.getSrcValue(NULL));
455
Jeff Cohen00b168892005-07-27 06:12:32 +0000456 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000457 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000458 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
459 Result.getValue(1), NewOffset,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000460 Tmp, DAG.getSrcValue(VAListV, 8),
461 DAG.getValueType(MVT::i32));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000462 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
463
Andrew Lenharth558bc882005-06-18 18:34:52 +0000464 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000465}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000466
Chris Lattnere0fe2252005-07-05 19:58:54 +0000467
468SDOperand AlphaTargetLowering::
469LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
470 Value *DestV, SelectionDAG &DAG) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000471 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000472 DAG.getSrcValue(SrcV));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000473 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Chris Lattnere0fe2252005-07-05 19:58:54 +0000474 Val, DestP, DAG.getSrcValue(DestV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000475 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000476 DAG.getConstant(8, MVT::i64));
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000477 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
478 DAG.getSrcValue(SrcV, 8), MVT::i32);
Jeff Cohen00b168892005-07-27 06:12:32 +0000479 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000480 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000481 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000482 Val, NPD, DAG.getSrcValue(DestV, 8),
483 DAG.getValueType(MVT::i32));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000484}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000485
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000486namespace {
487
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000488//===--------------------------------------------------------------------===//
489/// ISel - Alpha specific code to select Alpha machine instructions for
490/// SelectionDAG operations.
491//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000492class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000493
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494 /// AlphaLowering - This object fully describes how to lower LLVM code to an
495 /// Alpha-specific SelectionDAG.
496 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000497
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000498 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
499 // for sdiv and udiv until it is put into the future
500 // dag combiner.
501
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000502 /// ExprMap - As shared expressions are codegen'd, we keep track of which
503 /// vreg the value is produced in, so we only emit one copy of each compiled
504 /// tree.
505 static const unsigned notIn = (unsigned)(-1);
506 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000507
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000508 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
509 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000510
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000511 int count_ins;
512 int count_outs;
513 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000514 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000515
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000516public:
Jeff Cohen00b168892005-07-27 06:12:32 +0000517 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000518 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000519 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000520
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000521 /// InstructionSelectBasicBlock - This callback is invoked by
522 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
523 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000524 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000525 count_ins = 0;
526 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000527 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000528 has_sym = false;
529
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000530 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000531 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000532 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000533 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000534
535 if(has_sym)
536 ++count_ins;
537 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000538 std::cerr << "COUNT: "
539 << BB->getParent()->getFunction ()->getName() << " "
540 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000541 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000542 << count_ins << " "
543 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000544
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000545 // Clear state used for selection.
546 ExprMap.clear();
547 CCInvMap.clear();
548 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000549
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000550 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000551
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000552 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000553 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000554
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000555 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
556 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000557 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
558 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000559 //returns whether the sense of the comparison was inverted
560 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000561
562 // dag -> dag expanders for integer divide by constant
563 SDOperand BuildSDIVSequence(SDOperand N);
564 SDOperand BuildUDIVSequence(SDOperand N);
565
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000566};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000567}
568
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000569void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000570 // If this function has live-in values, emit the copies from pregs to vregs at
571 // the top of the function, before anything else.
572 MachineBasicBlock *BB = MF.begin();
573 if (MF.livein_begin() != MF.livein_end()) {
574 SSARegMap *RegMap = MF.getSSARegMap();
575 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
576 E = MF.livein_end(); LI != E; ++LI) {
577 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
578 if (RC == Alpha::GPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000579 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
580 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000581 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000582 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
583 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000584 } else {
585 assert(0 && "Unknown regclass!");
586 }
587 }
588 }
589}
590
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000591static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000592{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000593 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000594 if (v == NULL) {
595 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000596 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
597 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000598 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000599 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
600 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000601 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
602 type = 2;
603 const Function* F = Arg->getParent();
604 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000605 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000606 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000607 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000608 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000609 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000610 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000611 type = 3;
612 const BasicBlock* bb = I->getParent();
613 const Function* F = bb->getParent();
614 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000615 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000616 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000617 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000618 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000619 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000620 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000621 } else if (const Constant* C = dyn_cast<Constant>(v)) {
622 //Don't know how to look these up yet
623 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000624 } else {
625 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000626 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000627 //type = 4: register spilling
628 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000629}
630
631static int getUID()
632{
633 static int id = 0;
634 return ++id;
635}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000636
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000637//Factorize a number using the list of constants
638static bool factorize(int v[], int res[], int size, uint64_t c)
639{
640 bool cont = true;
641 while (c != 1 && cont)
642 {
643 cont = false;
644 for(int i = 0; i < size; ++i)
645 {
646 if (c % v[i] == 0)
647 {
648 c /= v[i];
649 ++res[i];
650 cont=true;
651 }
652 }
653 }
654 return c == 1;
655}
656
657
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000658//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000659// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000660// a multiply.
661struct ms {
662 int64_t m; // magic number
663 int64_t s; // shift amount
664};
665
666struct mu {
667 uint64_t m; // magic number
668 int64_t a; // add indicator
669 int64_t s; // shift amount
670};
671
672/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000673/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000674/// or -1.
675static struct ms magic(int64_t d) {
676 int64_t p;
677 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
678 const uint64_t two63 = 9223372036854775808ULL; // 2^63
679 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000680
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000681 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000682 t = two63 + ((uint64_t)d >> 63);
683 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000684 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000685 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
686 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
687 q2 = two63/ad; // initialize q2 = 2p/abs(d)
688 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
689 do {
690 p = p + 1;
691 q1 = 2*q1; // update q1 = 2p/abs(nc)
692 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
693 if (r1 >= anc) { // must be unsigned comparison
694 q1 = q1 + 1;
695 r1 = r1 - anc;
696 }
697 q2 = 2*q2; // update q2 = 2p/abs(d)
698 r2 = 2*r2; // update r2 = rem(2p/abs(d))
699 if (r2 >= ad) { // must be unsigned comparison
700 q2 = q2 + 1;
701 r2 = r2 - ad;
702 }
703 delta = ad - r2;
704 } while (q1 < delta || (q1 == delta && r1 == 0));
705
706 mag.m = q2 + 1;
707 if (d < 0) mag.m = -mag.m; // resulting magic number
708 mag.s = p - 64; // resulting shift
709 return mag;
710}
711
712/// magicu - calculate the magic numbers required to codegen an integer udiv as
713/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
714static struct mu magicu(uint64_t d)
715{
716 int64_t p;
717 uint64_t nc, delta, q1, r1, q2, r2;
718 struct mu magu;
719 magu.a = 0; // initialize "add" indicator
720 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000721 p = 63; // initialize p
722 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
723 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
724 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
725 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000726 do {
727 p = p + 1;
728 if (r1 >= nc - r1 ) {
729 q1 = 2*q1 + 1; // update q1
730 r1 = 2*r1 - nc; // update r1
731 }
732 else {
733 q1 = 2*q1; // update q1
734 r1 = 2*r1; // update r1
735 }
736 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000737 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000738 q2 = 2*q2 + 1; // update q2
739 r2 = 2*r2 + 1 - d; // update r2
740 }
741 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000742 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000743 q2 = 2*q2; // update q2
744 r2 = 2*r2 + 1; // update r2
745 }
746 delta = d - 1 - r2;
747 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
748 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000749 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000750 return magu;
751}
752
753/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
754/// return a DAG expression to select that will generate the same value by
755/// multiplying by a magic number. See:
756/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000757SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000758 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000759 ms magics = magic(d);
760 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000761 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000762 ISelDAG->getConstant(magics.m, MVT::i64));
763 // If d > 0 and m < 0, add the numerator
764 if (d > 0 && magics.m < 0)
765 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
766 // If d < 0 and m > 0, subtract the numerator.
767 if (d < 0 && magics.m > 0)
768 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
769 // Shift right algebraic if shift value is nonzero
770 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000772 ISelDAG->getConstant(magics.s, MVT::i64));
773 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000774 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000775 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
776 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
777}
778
779/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
780/// return a DAG expression to select that will generate the same value by
781/// multiplying by a magic number. See:
782/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000783SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000785 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
786 mu magics = magicu(d);
787 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000788 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000789 ISelDAG->getConstant(magics.m, MVT::i64));
790 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000791 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000792 ISelDAG->getConstant(magics.s, MVT::i64));
793 } else {
794 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000796 ISelDAG->getConstant(1, MVT::i64));
797 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000798 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000799 ISelDAG->getConstant(magics.s-1, MVT::i64));
800 }
801 return Q;
802}
803
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000804//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000805static const int IMM_LOW = -32768;
806static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000807static const int IMM_MULT = 65536;
808
809static long getUpper16(long l)
810{
811 long y = l / IMM_MULT;
812 if (l % IMM_MULT > IMM_HIGH)
813 ++y;
814 return y;
815}
816
817static long getLower16(long l)
818{
819 long h = getUpper16(l);
820 return l - h * IMM_MULT;
821}
822
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000823static unsigned GetRelVersion(unsigned opcode)
824{
825 switch (opcode) {
826 default: assert(0 && "unknown load or store"); return 0;
827 case Alpha::LDQ: return Alpha::LDQr;
828 case Alpha::LDS: return Alpha::LDSr;
829 case Alpha::LDT: return Alpha::LDTr;
830 case Alpha::LDL: return Alpha::LDLr;
831 case Alpha::LDBU: return Alpha::LDBUr;
832 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000833 case Alpha::STB: return Alpha::STBr;
834 case Alpha::STW: return Alpha::STWr;
835 case Alpha::STL: return Alpha::STLr;
836 case Alpha::STQ: return Alpha::STQr;
837 case Alpha::STS: return Alpha::STSr;
838 case Alpha::STT: return Alpha::STTr;
839
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000840 }
841}
Andrew Lenharth65838902005-02-06 16:22:15 +0000842
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000843void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000844{
845 unsigned Opc;
846 if (EnableAlphaFTOI) {
847 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000848 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000849 } else {
850 //The hard way:
851 // Spill the integer to memory and reload it from there.
852 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
853 MachineFunction *F = BB->getParent();
854 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
855
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000856 if (EnableAlphaLSMark)
857 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
858 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000859 Opc = isDouble ? Alpha::STT : Alpha::STS;
860 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000861
862 if (EnableAlphaLSMark)
863 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
864 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000865 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
866 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
867 }
868}
869
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000870void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000871{
872 unsigned Opc;
873 if (EnableAlphaFTOI) {
874 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000875 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000876 } else {
877 //The hard way:
878 // Spill the integer to memory and reload it from there.
879 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
880 MachineFunction *F = BB->getParent();
881 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
882
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000883 if (EnableAlphaLSMark)
884 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
885 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000886 Opc = isDouble ? Alpha::STQ : Alpha::STL;
887 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000888
889 if (EnableAlphaLSMark)
890 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
891 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000892 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
893 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
894 }
895}
896
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000897bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000898{
899 SDNode *Node = N.Val;
900 unsigned Opc, Tmp1, Tmp2, Tmp3;
901 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
902
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000903 bool rev = false;
904 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000905
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000906 switch (SetCC->getCondition()) {
907 default: Node->dump(); assert(0 && "Unknown FP comparison!");
908 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
909 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
910 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
911 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
912 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
913 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
914 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000915
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000916 ConstantFPSDNode *CN;
917 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
918 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
919 Tmp1 = Alpha::F31;
920 else
921 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000922
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000923 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
924 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
925 Tmp2 = Alpha::F31;
926 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000927 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000928
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000929 //Can only compare doubles, and dag won't promote for me
930 if (SetCC->getOperand(0).getValueType() == MVT::f32)
931 {
932 //assert(0 && "Setcc On float?\n");
933 std::cerr << "Setcc on float!\n";
934 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000935 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000936 Tmp1 = Tmp3;
937 }
938 if (SetCC->getOperand(1).getValueType() == MVT::f32)
939 {
940 //assert (0 && "Setcc On float?\n");
941 std::cerr << "Setcc on float!\n";
942 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000943 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000944 Tmp2 = Tmp3;
945 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000947 if (rev) std::swap(Tmp1, Tmp2);
948 //do the comparison
949 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
950 return inv;
951}
952
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000953//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000954void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000955{
956 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000957 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
958 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
959 { //Normal imm add
960 Reg = SelectExpr(N.getOperand(0));
961 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
962 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000963 }
964 Reg = SelectExpr(N);
965 offset = 0;
966 return;
967}
968
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000969void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000970{
971 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000972 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000973 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
974 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Andrew Lenharth445171a2005-02-08 00:40:03 +0000976 Select(N.getOperand(0)); //chain
977 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000978
Andrew Lenharth445171a2005-02-08 00:40:03 +0000979 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000980 {
981 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
982 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
983 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000984 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
985 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000986 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000988 //Fix up CC
989 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000990
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000991 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000992 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000993
Andrew Lenharth694c2982005-06-26 23:01:11 +0000994 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000995 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000996 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
997 case ISD::SETEQ: Opc = Alpha::BEQ; break;
998 case ISD::SETLT: Opc = Alpha::BLT; break;
999 case ISD::SETLE: Opc = Alpha::BLE; break;
1000 case ISD::SETGT: Opc = Alpha::BGT; break;
1001 case ISD::SETGE: Opc = Alpha::BGE; break;
1002 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1003 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001004 //Technically you could have this CC
1005 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001006 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1007 case ISD::SETNE: Opc = Alpha::BNE; break;
1008 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001009 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001010 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1011 return;
1012 } else {
1013 unsigned Tmp1 = SelectExpr(CC);
1014 if (isNE)
1015 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1016 else
1017 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001018 return;
1019 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001020 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +00001021 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001022 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001023 //for a cmp b: c = a - b;
1024 //a = b: c = 0
1025 //a < b: c < 0
1026 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001027
1028 bool invTest = false;
1029 unsigned Tmp3;
1030
1031 ConstantFPSDNode *CN;
1032 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1033 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1034 Tmp3 = SelectExpr(SetCC->getOperand(0));
1035 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1036 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1037 {
1038 Tmp3 = SelectExpr(SetCC->getOperand(1));
1039 invTest = true;
1040 }
1041 else
1042 {
1043 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1044 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1045 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1046 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1047 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1048 .addReg(Tmp1).addReg(Tmp2);
1049 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001050
1051 switch (SetCC->getCondition()) {
1052 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001053 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1054 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1055 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1056 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1057 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1058 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001059 }
1060 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001061 return;
1062 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001063 abort(); //Should never be reached
1064 } else {
1065 //Giveup and do the stupid thing
1066 unsigned Tmp1 = SelectExpr(CC);
1067 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1068 return;
1069 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001070 abort(); //Should never be reached
1071}
1072
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001073unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001074 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001075 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001076 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001077 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001078
1079 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001080 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001081 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001082
1083 unsigned &Reg = ExprMap[N];
1084 if (Reg) return Reg;
1085
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001086 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001087 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001088 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001089 else {
1090 // If this is a call instruction, make sure to prepare ALL of the result
1091 // values as well as the chain.
1092 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001093 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001094 else {
1095 Result = MakeReg(Node->getValueType(0));
1096 ExprMap[N.getValue(0)] = Result;
1097 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1098 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001099 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001100 }
1101 }
1102
Andrew Lenharth40831c52005-01-28 06:57:18 +00001103 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001104 default:
1105 Node->dump();
1106 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001107
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001108 case ISD::CTPOP:
1109 case ISD::CTTZ:
1110 case ISD::CTLZ:
1111 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1112 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1113 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001114 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001115 return Result;
1116
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001117 case ISD::MULHU:
1118 Tmp1 = SelectExpr(N.getOperand(0));
1119 Tmp2 = SelectExpr(N.getOperand(1));
1120 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001121 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001122 case ISD::MULHS:
1123 {
1124 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1125 Tmp1 = SelectExpr(N.getOperand(0));
1126 Tmp2 = SelectExpr(N.getOperand(1));
1127 Tmp3 = MakeReg(MVT::i64);
1128 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1129 unsigned V1 = MakeReg(MVT::i64);
1130 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001131 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1132 .addReg(Tmp1);
1133 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1134 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001135 unsigned IRes = MakeReg(MVT::i64);
1136 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1137 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1138 return Result;
1139 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001140 case ISD::UNDEF: {
1141 BuildMI(BB, Alpha::IDEF, 0, Result);
1142 return Result;
1143 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001144
Andrew Lenharth032f2352005-02-22 21:59:48 +00001145 case ISD::DYNAMIC_STACKALLOC:
1146 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001147 if (Result != notIn)
1148 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001149 else
1150 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1151
1152 // FIXME: We are currently ignoring the requested alignment for handling
1153 // greater than the stack alignment. This will need to be revisited at some
1154 // point. Align = N.getOperand(2);
1155
1156 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1157 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1158 std::cerr << "Cannot allocate stack object with greater alignment than"
1159 << " the stack alignment yet!";
1160 abort();
1161 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001162
Andrew Lenharth032f2352005-02-22 21:59:48 +00001163 Select(N.getOperand(0));
1164 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1165 {
1166 if (CN->getValue() < 32000)
1167 {
1168 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1169 .addImm(-CN->getValue()).addReg(Alpha::R30);
1170 } else {
1171 Tmp1 = SelectExpr(N.getOperand(1));
1172 // Subtract size from stack pointer, thereby allocating some space.
1173 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1174 }
1175 } else {
1176 Tmp1 = SelectExpr(N.getOperand(1));
1177 // Subtract size from stack pointer, thereby allocating some space.
1178 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1179 }
1180
1181 // Put a pointer to the space into the result register, by copying the stack
1182 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001183 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001184 return Result;
1185
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001186 case ISD::ConstantPool:
1187 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1188 AlphaLowering.restoreGP(BB);
1189 Tmp2 = MakeReg(MVT::i64);
1190 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1191 .addReg(Alpha::R29);
1192 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1193 .addReg(Tmp2);
1194 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001195
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001196 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001197 BuildMI(BB, Alpha::LDA, 2, Result)
1198 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1199 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001200 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001201
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001202 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001203 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001204 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001205 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001206 {
1207 // Make sure we generate both values.
1208 if (Result != notIn)
1209 ExprMap[N.getValue(1)] = notIn; // Generate the token
1210 else
1211 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001212
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001213 SDOperand Chain = N.getOperand(0);
1214 SDOperand Address = N.getOperand(1);
1215 Select(Chain);
1216
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001217 bool fpext = true;
1218
Andrew Lenharth03824012005-02-07 05:55:55 +00001219 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001220 switch (Node->getValueType(0)) {
1221 default: Node->dump(); assert(0 && "Bad load!");
1222 case MVT::i64: Opc = Alpha::LDQ; break;
1223 case MVT::f64: Opc = Alpha::LDT; break;
1224 case MVT::f32: Opc = Alpha::LDS; break;
1225 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001226 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001227 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001228 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001229 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001230 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001231 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001232 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001233 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001234 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001235 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001236 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001237
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001238 int i, j, k;
1239 if (EnableAlphaLSMark)
1240 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1241 i, j, k);
1242
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001243 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1244 if (GASD && !GASD->getGlobal()->isExternal()) {
1245 Tmp1 = MakeReg(MVT::i64);
1246 AlphaLowering.restoreGP(BB);
1247 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1248 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1249 if (EnableAlphaLSMark)
1250 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1251 .addImm(getUID());
1252 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1253 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001254 } else if (ConstantPoolSDNode *CP =
1255 dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001256 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001257 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001258 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001259 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1260 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001261 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001262 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1263 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001264 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1265 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1266 } else if(Address.getOpcode() == ISD::FrameIndex) {
1267 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001268 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1269 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001270 BuildMI(BB, Opc, 2, Result)
1271 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1272 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001273 } else {
1274 long offset;
1275 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001276 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001277 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1278 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001279 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1280 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001281 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001282 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001283
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284 case ISD::GlobalAddress:
1285 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001286 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +00001287
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001288 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001289
1290 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001291 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001292 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001293
1294 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001295 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1296 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001297 return Result;
1298
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001299 case ISD::ExternalSymbol:
1300 AlphaLowering.restoreGP(BB);
1301 has_sym = true;
1302
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001303 Reg = Result = MakeReg(MVT::i64);
1304
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001305 if (EnableAlphaLSMark)
1306 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1307 .addImm(getUID());
1308
1309 BuildMI(BB, Alpha::LDQl, 2, Result)
1310 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1311 .addReg(Alpha::R29);
1312 return Result;
1313
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001314 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001315 case ISD::CALL:
1316 {
1317 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001318
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001319 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001320 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001321
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001322 //grab the arguments
1323 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001324 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001325 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001326 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001327
Andrew Lenharth684f2292005-01-30 00:35:27 +00001328 //in reg args
1329 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001330 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001331 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001332 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001333 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001334 Alpha::F19, Alpha::F20, Alpha::F21};
1335 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001336 default:
1337 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001339 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001340 N.getOperand(i+2).getValueType() << "\n";
1341 assert(0 && "Unknown value type for call");
1342 case MVT::i1:
1343 case MVT::i8:
1344 case MVT::i16:
1345 case MVT::i32:
1346 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001347 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1348 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001349 break;
1350 case MVT::f32:
1351 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001352 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1353 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001354 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001355 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001356 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001357 //in mem args
1358 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 {
1360 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001361 default:
1362 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001363 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001364 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 N.getOperand(i+2).getValueType() << "\n";
1366 assert(0 && "Unknown value type for call");
1367 case MVT::i1:
1368 case MVT::i8:
1369 case MVT::i16:
1370 case MVT::i32:
1371 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001372 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1373 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001374 break;
1375 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001376 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1377 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001378 break;
1379 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001380 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1381 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001382 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001383 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001385 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001386 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1387 if (GASD && !GASD->getGlobal()->isExternal()) {
1388 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001390 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1391 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001392 } else {
1393 //no need to restore GP as we are doing an indirect call
1394 Tmp1 = SelectExpr(N.getOperand(1));
1395 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1396 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1397 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001398
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001399 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001401 switch (Node->getValueType(0)) {
1402 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001403 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001404 case MVT::i1:
1405 case MVT::i8:
1406 case MVT::i16:
1407 case MVT::i32:
1408 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001409 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1410 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001411 case MVT::f32:
1412 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001413 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1414 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001415 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001416 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001417 }
1418
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001419 case ISD::SIGN_EXTEND_INREG:
1420 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001421 //do SDIV opt for all levels of ints if not dividing by a constant
1422 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1423 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001424 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001425 unsigned Tmp4 = MakeReg(MVT::f64);
1426 unsigned Tmp5 = MakeReg(MVT::f64);
1427 unsigned Tmp6 = MakeReg(MVT::f64);
1428 unsigned Tmp7 = MakeReg(MVT::f64);
1429 unsigned Tmp8 = MakeReg(MVT::f64);
1430 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001431
1432 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1433 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1434 MoveInt2FP(Tmp1, Tmp4, true);
1435 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001436 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1437 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001438 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001439 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001440 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001441 return Result;
1442 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001443
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001444 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001445 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001446 switch (N.getOperand(0).getOpcode()) {
1447 case ISD::ADD:
1448 case ISD::SUB:
1449 case ISD::MUL:
1450 {
1451 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1452 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1453 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001454 ConstantSDNode* CSD = NULL;
1455 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthfec0e402005-07-12 04:20:52 +00001456 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001457 (CSD->getValue() == 2 || CSD->getValue() == 3))
1458 {
1459 bool use4 = CSD->getValue() == 2;
1460 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1461 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1462 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1463 2,Result).addReg(Tmp1).addReg(Tmp2);
1464 }
1465 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthfec0e402005-07-12 04:20:52 +00001466 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001467 (CSD->getValue() == 2 || CSD->getValue() == 3))
1468 {
1469 bool use4 = CSD->getValue() == 2;
1470 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1471 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1472 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1473 }
1474 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthfec0e402005-07-12 04:20:52 +00001475 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 { //Normal imm add/sub
1477 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001478 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001479 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1480 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001481 }
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001482 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1483 !isMul &&
1484 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1485 (((int64_t)(CSD->getValue() << 32) >> 32) >= -255) &&
1486 (((int64_t)(CSD->getValue() << 32) >> 32) <= 0))
1487 { //handle canonicalization
1488 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1489 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1490 int64_t t = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1491 t = 0 - ((t << 32) >> 32);
1492 assert(t >= 0 && t <= 255);
1493 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(t);
1494 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001495 else
1496 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001497 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001498 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001499 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001500 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1501 }
1502 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001503 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 default: break; //Fall Though;
1505 }
1506 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001507 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001508 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001509 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001510 default:
1511 Node->dump();
1512 assert(0 && "Sign Extend InReg not there yet");
1513 break;
1514 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001515 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001516 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001517 break;
1518 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001520 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 break;
1522 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001523 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001524 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001525 case MVT::i1:
1526 Tmp2 = MakeReg(MVT::i64);
1527 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001528 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001529 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001531 return Result;
1532 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001533
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001534 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001535 {
1536 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1537 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001538 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001539 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001540
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001541 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001542 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001543 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001544 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001545
1546 switch (SetCC->getCondition()) {
1547 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001548 case ISD::SETEQ:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001549 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001550 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001551 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001552 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001553 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1554 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1555 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001556 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001557 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1558 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001559 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001560 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1561 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001562 case ISD::SETNE: {//Handle this one special
1563 //std::cerr << "Alpha does not have a setne.\n";
1564 //abort();
1565 Tmp1 = SelectExpr(N.getOperand(0));
1566 Tmp2 = SelectExpr(N.getOperand(1));
1567 Tmp3 = MakeReg(MVT::i64);
1568 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001569 //Remeber we have the Inv for this CC
1570 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001571 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001572 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001573 return Result;
1574 }
1575 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001576 if (dir == 1) {
1577 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001578 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001579 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1580 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1581 } else {
1582 Tmp2 = SelectExpr(N.getOperand(1));
1583 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1584 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001585 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001586 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001587 Tmp2 = SelectExpr(N.getOperand(0));
1588 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001589 }
1590 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001591 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001592 Tmp1 = MakeReg(MVT::f64);
1593 bool inv = SelectFPSetCC(N, Tmp1);
1594
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001595 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001596 Tmp2 = MakeReg(MVT::i64);
1597 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001598 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001599 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001600 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001601 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001602 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001603 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001604
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001605 case ISD::CopyFromReg:
1606 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001607 ++count_ins;
1608
Andrew Lenharth40831c52005-01-28 06:57:18 +00001609 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001610 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001611 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001612 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001613 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001614
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001615 SDOperand Chain = N.getOperand(0);
1616
1617 Select(Chain);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001618 unsigned r = cast<RegSDNode>(Node)->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001619 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001620 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001621 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1622 else
1623 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001624 return Result;
1625 }
1626
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001628 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001629 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001630 //Match Not
1631 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001632 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001633 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001634 Tmp1 = SelectExpr(N.getOperand(0));
1635 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1636 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001637 }
1638 //Fall through
1639 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001640 //handle zap
1641 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1642 {
1643 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1644 unsigned int build = 0;
1645 for(int i = 0; i < 8; ++i)
1646 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001647 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001648 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001649 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001650 { build = 0; break; }
1651 k >>= 8;
1652 }
1653 if (build)
1654 {
1655 Tmp1 = SelectExpr(N.getOperand(0));
1656 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1657 return Result;
1658 }
1659 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001660 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001661 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001662 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001663 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Jeff Cohen00b168892005-07-27 06:12:32 +00001664 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001665 == -1) {
1666 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001667 case ISD::AND: Opc = Alpha::BIC; break;
1668 case ISD::OR: Opc = Alpha::ORNOT; break;
1669 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001670 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001671 Tmp1 = SelectExpr(N.getOperand(1));
1672 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1673 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1674 return Result;
1675 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001676 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001677 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001678 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001679 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1680 == -1) {
1681 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001682 case ISD::AND: Opc = Alpha::BIC; break;
1683 case ISD::OR: Opc = Alpha::ORNOT; break;
1684 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001685 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001686 Tmp1 = SelectExpr(N.getOperand(0));
1687 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1688 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1689 return Result;
1690 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001691 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001692 case ISD::SHL:
1693 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001694 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001695 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001696 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001697 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001698 {
1699 switch(opcode) {
1700 case ISD::AND: Opc = Alpha::ANDi; break;
1701 case ISD::OR: Opc = Alpha::BISi; break;
1702 case ISD::XOR: Opc = Alpha::XORi; break;
1703 case ISD::SHL: Opc = Alpha::SLi; break;
1704 case ISD::SRL: Opc = Alpha::SRLi; break;
1705 case ISD::SRA: Opc = Alpha::SRAi; break;
1706 case ISD::MUL: Opc = Alpha::MULQi; break;
1707 };
1708 Tmp1 = SelectExpr(N.getOperand(0));
1709 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1710 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1711 } else {
1712 switch(opcode) {
1713 case ISD::AND: Opc = Alpha::AND; break;
1714 case ISD::OR: Opc = Alpha::BIS; break;
1715 case ISD::XOR: Opc = Alpha::XOR; break;
1716 case ISD::SHL: Opc = Alpha::SL; break;
1717 case ISD::SRL: Opc = Alpha::SRL; break;
1718 case ISD::SRA: Opc = Alpha::SRA; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001719 case ISD::MUL:
1720 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001721 : Alpha::MULQ;
1722 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001723 };
1724 Tmp1 = SelectExpr(N.getOperand(0));
1725 Tmp2 = SelectExpr(N.getOperand(1));
1726 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1727 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001728 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001729
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001730 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001731 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001732 if (isFP) {
1733 ConstantFPSDNode *CN;
1734 if (opcode == ISD::ADD)
1735 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1736 else
1737 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1738 if (opcode == ISD::SUB
1739 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1740 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1741 {
1742 Tmp2 = SelectExpr(N.getOperand(1));
1743 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1744 } else {
1745 Tmp1 = SelectExpr(N.getOperand(0));
1746 Tmp2 = SelectExpr(N.getOperand(1));
1747 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1748 }
1749 return Result;
1750 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001751 bool isAdd = opcode == ISD::ADD;
1752
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001753 //first check for Scaled Adds and Subs!
1754 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001755 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001756 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001757 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1758 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001759 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001760 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001761 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001762 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1763 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1764 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001765 else {
1766 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001767 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1768 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001769 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001770 }
1771 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001772 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001773 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1774 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001775 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001776 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001777 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001778 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1779 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1780 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001781 else {
1782 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001783 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001784 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001785 }
1786 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001787 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1788 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001789 { //Normal imm add/sub
1790 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1791 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001792 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001793 }
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001794 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1795 (int64_t)CSD->getValue() >= 255 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00001796 (int64_t)CSD->getValue() <= 0)
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001797 { //inverted imm add/sub
1798 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1799 Tmp1 = SelectExpr(N.getOperand(0));
1800 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm((int64_t)CSD->getValue());
1801 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001802 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001803 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1804 CSD->getSignExtended() <= 32767 &&
1805 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001806 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001808 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001809 if (!isAdd)
1810 Tmp2 = -Tmp2;
1811 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001812 }
1813 //give up and do the operation
1814 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001815 //Normal add/sub
1816 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1));
1819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001821 return Result;
1822 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001823
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001824 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001825 if (isFP) {
1826 Tmp1 = SelectExpr(N.getOperand(0));
1827 Tmp2 = SelectExpr(N.getOperand(1));
1828 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1829 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001830 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001831 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001832 ConstantSDNode* CSD;
1833 //check if we can convert into a shift!
1834 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1835 (int64_t)CSD->getSignExtended() != 0 &&
Chris Lattner9e17df82005-08-02 19:35:29 +00001836 isPowerOf2_64(llabs(CSD->getSignExtended()))) {
1837 unsigned k = Log2_64(llabs(CSD->getSignExtended()));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001838 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001839 if (k == 1)
1840 Tmp2 = Tmp1;
1841 else
1842 {
1843 Tmp2 = MakeReg(MVT::i64);
1844 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1845 }
1846 Tmp3 = MakeReg(MVT::i64);
1847 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1848 unsigned Tmp4 = MakeReg(MVT::i64);
1849 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1850 if ((int64_t)CSD->getSignExtended() > 0)
1851 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1852 else
1853 {
1854 unsigned Tmp5 = MakeReg(MVT::i64);
1855 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1856 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1857 }
1858 return Result;
1859 }
1860 }
1861 //Else fall through
1862
1863 case ISD::UDIV:
1864 {
1865 ConstantSDNode* CSD;
1866 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1867 ((int64_t)CSD->getSignExtended() >= 2 ||
1868 (int64_t)CSD->getSignExtended() <= -2))
1869 {
1870 // If this is a divide by constant, we can emit code using some magic
1871 // constants to implement it as a multiply instead.
1872 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001873 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001874 return SelectExpr(BuildSDIVSequence(N));
1875 else
1876 return SelectExpr(BuildUDIVSequence(N));
1877 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001878 }
1879 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001880 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001881 case ISD::SREM: {
1882 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001883 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001884 case ISD::UREM: opstr = "__remqu"; break;
1885 case ISD::SREM: opstr = "__remq"; break;
1886 case ISD::UDIV: opstr = "__divqu"; break;
1887 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001888 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001889 Tmp1 = SelectExpr(N.getOperand(0));
1890 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001891 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001892 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1893 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001894 //set up regs explicitly (helps Reg alloc)
1895 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001896 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001897 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1898 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001899 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001900 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001901 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001902
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001903 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001904 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001905 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001906 assert (DestType == MVT::i64 && "only quads can be loaded to");
1907 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001908 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001909 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001910 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001911 {
1912 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001913 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001914 Tmp1 = Tmp2;
1915 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001916 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001917 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001918 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001919
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001920 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001921 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001922
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001923 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001924 if (isFP) {
1925 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1926 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1927 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1928
1929 SDOperand CC = N.getOperand(0);
1930 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1931
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001932 if (SetCC && !MVT::isInteger(SetCC->getOperand(0).getValueType()))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001933 { //FP Setcc -> Select yay!
1934
Jeff Cohen00b168892005-07-27 06:12:32 +00001935
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001936 //for a cmp b: c = a - b;
1937 //a = b: c = 0
1938 //a < b: c < 0
1939 //a > b: c > 0
1940
1941 bool invTest = false;
1942 unsigned Tmp3;
1943
1944 ConstantFPSDNode *CN;
1945 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1946 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1947 Tmp3 = SelectExpr(SetCC->getOperand(0));
1948 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1949 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1950 {
1951 Tmp3 = SelectExpr(SetCC->getOperand(1));
1952 invTest = true;
1953 }
1954 else
1955 {
1956 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1957 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1958 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1959 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1960 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1961 .addReg(Tmp1).addReg(Tmp2);
1962 }
1963
1964 switch (SetCC->getCondition()) {
1965 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1966 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1967 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1968 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1969 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1970 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1971 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1972 }
1973 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1974 return Result;
1975 }
1976 else
1977 {
1978 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1979 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1980 .addReg(Tmp1);
1981// // Spill the cond to memory and reload it from there.
1982// unsigned Tmp4 = MakeReg(MVT::f64);
1983// MoveIntFP(Tmp1, Tmp4, true);
1984// //now ideally, we don't have to do anything to the flag...
1985// // Get the condition into the zero flag.
1986// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1987 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001988 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001989 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001990 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1991 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001992 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001993 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1994 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001995 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001996 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001997
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001998 SDOperand CC = N.getOperand(0);
1999 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2000
Misha Brukman4633f1c2005-04-21 23:13:11 +00002001 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002002 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2003 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002004 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002005 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2006 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002007 bool inv = SelectFPSetCC(CC, Tmp1);
2008 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2009 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2010 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002011 }
2012 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002013 //Int SetCC -> Select
2014 //Dropping the CC is only useful if we are comparing to 0
2015 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002016 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002017 {
2018 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002019 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002020 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002021
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002022 //Fix up CC
2023 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002024 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002025 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002026
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002027 //Choose the CMOV
2028 switch (cCode) {
2029 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002030 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2031 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2032 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2033 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2034 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2035 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2036 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2037 //Technically you could have this CC
2038 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2039 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2040 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002041 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002042 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002043
Andrew Lenharth694c2982005-06-26 23:01:11 +00002044 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002045 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2046 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002047 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002048 .addReg(Tmp1);
2049 } else {
2050 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2051 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2052 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2053 }
2054 return Result;
2055 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002056 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002057 }
2058 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002059 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2060 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002061 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2062 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002063
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002064 return Result;
2065 }
2066
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002067 case ISD::Constant:
2068 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002069 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002070 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00002071 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002072 ((int32_t)val < 0)) {
2073 //try a small load and zero extend
2074 val = (int32_t)val;
2075 zero_extend_top = 15;
2076 }
2077
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002078 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002079 if(!zero_extend_top)
2080 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2081 else {
2082 Tmp1 = MakeReg(MVT::i64);
2083 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
2084 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
2085 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002086 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002087 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2088 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2089 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002090 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2091 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002092 if (!zero_extend_top)
2093 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2094 else {
2095 Tmp3 = MakeReg(MVT::i64);
2096 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
2097 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
2098 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002099 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002100 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002101 //re-get the val since we are going to mem anyway
2102 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002103 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00002104 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002105 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002106 unsigned CPI = CP->getConstantPoolIndex(C);
2107 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002108 has_sym = true;
2109 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002110 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2111 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002112 if (EnableAlphaLSMark)
2113 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2114 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002115 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2116 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002117 }
2118 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002120 case ISD::FNEG:
2121 if(ISD::FABS == N.getOperand(0).getOpcode())
2122 {
2123 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2124 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2125 } else {
2126 Tmp1 = SelectExpr(N.getOperand(0));
2127 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2128 }
2129 return Result;
2130
2131 case ISD::FABS:
2132 Tmp1 = SelectExpr(N.getOperand(0));
2133 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2134 return Result;
2135
2136 case ISD::FP_ROUND:
2137 assert (DestType == MVT::f32 &&
2138 N.getOperand(0).getValueType() == MVT::f64 &&
2139 "only f64 to f32 conversion supported here");
2140 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002141 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002142 return Result;
2143
2144 case ISD::FP_EXTEND:
2145 assert (DestType == MVT::f64 &&
2146 N.getOperand(0).getValueType() == MVT::f32 &&
2147 "only f32 to f64 conversion supported here");
2148 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002149 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002150 return Result;
2151
2152 case ISD::ConstantFP:
2153 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2154 if (CN->isExactlyValue(+0.0)) {
2155 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2156 .addReg(Alpha::F31);
2157 } else if ( CN->isExactlyValue(-0.0)) {
2158 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2159 .addReg(Alpha::F31);
2160 } else {
2161 abort();
2162 }
2163 }
2164 return Result;
2165
2166 case ISD::SINT_TO_FP:
2167 {
2168 assert (N.getOperand(0).getValueType() == MVT::i64
2169 && "only quads can be loaded from");
2170 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2171 Tmp2 = MakeReg(MVT::f64);
2172 MoveInt2FP(Tmp1, Tmp2, true);
2173 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00002174 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002175 return Result;
2176 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002177 }
2178
2179 return 0;
2180}
2181
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002182void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002183 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002184 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002185
Nate Begeman85fdeb22005-03-24 04:39:54 +00002186 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002187 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002188
2189 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002190
Andrew Lenharth760270d2005-02-07 23:02:23 +00002191 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002192
2193 default:
2194 Node->dump(); std::cerr << "\n";
2195 assert(0 && "Node not handled yet!");
2196
2197 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002198 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002199 return;
2200 }
2201
2202 case ISD::BR: {
2203 MachineBasicBlock *Dest =
2204 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2205
2206 Select(N.getOperand(0));
2207 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2208 return;
2209 }
2210
2211 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002212 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002213 Select(N.getOperand(0));
2214 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2215 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002216
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217 case ISD::EntryToken: return; // Noop
2218
2219 case ISD::TokenFactor:
2220 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2221 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002222
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 //N.Val->dump(); std::cerr << "\n";
2224 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002225
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002226 return;
2227
2228 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002229 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002230 Select(N.getOperand(0));
2231 Tmp1 = SelectExpr(N.getOperand(1));
2232 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002233
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002234 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002235 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002236 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002237 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2238 else
2239 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002240 }
2241 return;
2242
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002243 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002244 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002245 switch (N.getNumOperands()) {
2246 default:
2247 std::cerr << N.getNumOperands() << "\n";
2248 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2249 std::cerr << N.getOperand(i).getValueType() << "\n";
2250 Node->dump();
2251 assert(0 && "Unknown return instruction!");
2252 case 2:
2253 Select(N.getOperand(0));
2254 Tmp1 = SelectExpr(N.getOperand(1));
2255 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002256 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002257 assert(0 && "All other types should have been promoted!!");
2258 case MVT::f64:
2259 case MVT::f32:
2260 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2261 break;
2262 case MVT::i32:
2263 case MVT::i64:
2264 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2265 break;
2266 }
2267 break;
2268 case 1:
2269 Select(N.getOperand(0));
2270 break;
2271 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002272 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002273 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00002274 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002275 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002276
Misha Brukman4633f1c2005-04-21 23:13:11 +00002277 case ISD::TRUNCSTORE:
2278 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002279 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002280 SDOperand Chain = N.getOperand(0);
2281 SDOperand Value = N.getOperand(1);
2282 SDOperand Address = N.getOperand(2);
2283 Select(Chain);
2284
2285 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002286
2287 if (opcode == ISD::STORE) {
2288 switch(Value.getValueType()) {
2289 default: assert(0 && "unknown Type in store");
2290 case MVT::i64: Opc = Alpha::STQ; break;
2291 case MVT::f64: Opc = Alpha::STT; break;
2292 case MVT::f32: Opc = Alpha::STS; break;
2293 }
2294 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002295 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00002296 default: assert(0 && "unknown Type in store");
2297 case MVT::i1: //FIXME: DAG does not promote this load
2298 case MVT::i8: Opc = Alpha::STB; break;
2299 case MVT::i16: Opc = Alpha::STW; break;
2300 case MVT::i32: Opc = Alpha::STL; break;
2301 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002302 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002303
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002304 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00002305 if (EnableAlphaLSMark)
2306 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002307 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002308
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002309 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2310 if (GASD && !GASD->getGlobal()->isExternal()) {
2311 Tmp2 = MakeReg(MVT::i64);
2312 AlphaLowering.restoreGP(BB);
2313 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2314 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2315 if (EnableAlphaLSMark)
2316 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2317 .addImm(getUID());
2318 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2319 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002320 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002321 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002322 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2323 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002324 BuildMI(BB, Opc, 3).addReg(Tmp1)
2325 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2326 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002327 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002328 long offset;
2329 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002330 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002331 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2332 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002333 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2334 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002335 return;
2336 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002337
2338 case ISD::EXTLOAD:
2339 case ISD::SEXTLOAD:
2340 case ISD::ZEXTLOAD:
2341 case ISD::LOAD:
2342 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002343 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002345 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002346 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002347 SelectExpr(N);
2348 return;
2349
Chris Lattner16cd04d2005-05-12 23:24:06 +00002350 case ISD::CALLSEQ_START:
2351 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002352 Select(N.getOperand(0));
2353 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002354
Chris Lattner16cd04d2005-05-12 23:24:06 +00002355 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002356 Alpha::ADJUSTSTACKUP;
2357 BuildMI(BB, Opc, 1).addImm(Tmp1);
2358 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002359
2360 case ISD::PCMARKER:
2361 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002362 BuildMI(BB, Alpha::PCLABEL, 2)
2363 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002364 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002365 }
2366 assert(0 && "Should not be reached!");
2367}
2368
2369
2370/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2371/// into a machine code representation using pattern matching and a machine
2372/// description file.
2373///
2374FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002375 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002376}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002377