blob: e898916f805d1ad44ae171652c79f8605bdea6d5 [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));
Chris Lattner88ac32c2005-08-09 20:21:10 +0000440 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
441 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000442 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 Lenharthd2284272005-08-15 14:31:37 +0000591static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
592 // test for constant
593 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
594 // retrieve value
595 Imm = CN->getSignExtended();
596 // passes muster
597 return true;
598 }
599 // not a constant
600 return false;
601}
602
603// isSIntImmediateBounded - This method tests to see if a constant operand
604// bounded s.t. low <= Imm <= high
605// If so Imm will receive the 64 bit value.
606static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
607 int64_t low, int64_t high) {
608 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= high)
609 return true;
610 return false;
611}
612static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
613 // test for constant
614 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
615 // retrieve value
616 Imm = (uint64_t)CN->getValue();
617 // passes muster
618 return true;
619 }
620 // not a constant
621 return false;
622}
623
624static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
625 uint64_t low, uint64_t high) {
626 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= high)
627 return true;
628 return false;
629}
630
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000631static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000632{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000633 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000634 if (v == NULL) {
635 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000636 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
637 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000638 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000639 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
640 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000641 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
642 type = 2;
643 const Function* F = Arg->getParent();
644 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000645 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000646 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000647 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000648 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000649 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000650 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000651 type = 3;
652 const BasicBlock* bb = I->getParent();
653 const Function* F = bb->getParent();
654 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000655 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000656 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000657 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000658 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000659 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000660 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000661 } else if (const Constant* C = dyn_cast<Constant>(v)) {
662 //Don't know how to look these up yet
663 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000664 } else {
665 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000666 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000667 //type = 4: register spilling
668 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000669}
670
671static int getUID()
672{
673 static int id = 0;
674 return ++id;
675}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000676
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000677//Factorize a number using the list of constants
678static bool factorize(int v[], int res[], int size, uint64_t c)
679{
680 bool cont = true;
681 while (c != 1 && cont)
682 {
683 cont = false;
684 for(int i = 0; i < size; ++i)
685 {
686 if (c % v[i] == 0)
687 {
688 c /= v[i];
689 ++res[i];
690 cont=true;
691 }
692 }
693 }
694 return c == 1;
695}
696
697
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000698//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000699// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000700// a multiply.
701struct ms {
702 int64_t m; // magic number
703 int64_t s; // shift amount
704};
705
706struct mu {
707 uint64_t m; // magic number
708 int64_t a; // add indicator
709 int64_t s; // shift amount
710};
711
712/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000713/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000714/// or -1.
715static struct ms magic(int64_t d) {
716 int64_t p;
717 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
718 const uint64_t two63 = 9223372036854775808ULL; // 2^63
719 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000720
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000721 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000722 t = two63 + ((uint64_t)d >> 63);
723 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000724 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000725 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
726 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
727 q2 = two63/ad; // initialize q2 = 2p/abs(d)
728 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
729 do {
730 p = p + 1;
731 q1 = 2*q1; // update q1 = 2p/abs(nc)
732 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
733 if (r1 >= anc) { // must be unsigned comparison
734 q1 = q1 + 1;
735 r1 = r1 - anc;
736 }
737 q2 = 2*q2; // update q2 = 2p/abs(d)
738 r2 = 2*r2; // update r2 = rem(2p/abs(d))
739 if (r2 >= ad) { // must be unsigned comparison
740 q2 = q2 + 1;
741 r2 = r2 - ad;
742 }
743 delta = ad - r2;
744 } while (q1 < delta || (q1 == delta && r1 == 0));
745
746 mag.m = q2 + 1;
747 if (d < 0) mag.m = -mag.m; // resulting magic number
748 mag.s = p - 64; // resulting shift
749 return mag;
750}
751
752/// magicu - calculate the magic numbers required to codegen an integer udiv as
753/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
754static struct mu magicu(uint64_t d)
755{
756 int64_t p;
757 uint64_t nc, delta, q1, r1, q2, r2;
758 struct mu magu;
759 magu.a = 0; // initialize "add" indicator
760 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000761 p = 63; // initialize p
762 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
763 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
764 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
765 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000766 do {
767 p = p + 1;
768 if (r1 >= nc - r1 ) {
769 q1 = 2*q1 + 1; // update q1
770 r1 = 2*r1 - nc; // update r1
771 }
772 else {
773 q1 = 2*q1; // update q1
774 r1 = 2*r1; // update r1
775 }
776 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000777 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000778 q2 = 2*q2 + 1; // update q2
779 r2 = 2*r2 + 1 - d; // update r2
780 }
781 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000782 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000783 q2 = 2*q2; // update q2
784 r2 = 2*r2 + 1; // update r2
785 }
786 delta = d - 1 - r2;
787 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
788 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000789 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000790 return magu;
791}
792
793/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
794/// return a DAG expression to select that will generate the same value by
795/// multiplying by a magic number. See:
796/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000797SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000798 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000799 ms magics = magic(d);
800 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000801 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000802 ISelDAG->getConstant(magics.m, MVT::i64));
803 // If d > 0 and m < 0, add the numerator
804 if (d > 0 && magics.m < 0)
805 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
806 // If d < 0 and m > 0, subtract the numerator.
807 if (d < 0 && magics.m > 0)
808 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
809 // Shift right algebraic if shift value is nonzero
810 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000811 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000812 ISelDAG->getConstant(magics.s, MVT::i64));
813 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000814 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000815 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
816 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
817}
818
819/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
820/// return a DAG expression to select that will generate the same value by
821/// multiplying by a magic number. See:
822/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000823SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000824 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000825 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
826 mu magics = magicu(d);
827 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000828 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000829 ISelDAG->getConstant(magics.m, MVT::i64));
830 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000831 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000832 ISelDAG->getConstant(magics.s, MVT::i64));
833 } else {
834 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000835 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000836 ISelDAG->getConstant(1, MVT::i64));
837 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000838 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000839 ISelDAG->getConstant(magics.s-1, MVT::i64));
840 }
841 return Q;
842}
843
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000844//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000845static const int IMM_LOW = -32768;
846static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000847static const int IMM_MULT = 65536;
848
849static long getUpper16(long l)
850{
851 long y = l / IMM_MULT;
852 if (l % IMM_MULT > IMM_HIGH)
853 ++y;
854 return y;
855}
856
857static long getLower16(long l)
858{
859 long h = getUpper16(l);
860 return l - h * IMM_MULT;
861}
862
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000863static unsigned GetRelVersion(unsigned opcode)
864{
865 switch (opcode) {
866 default: assert(0 && "unknown load or store"); return 0;
867 case Alpha::LDQ: return Alpha::LDQr;
868 case Alpha::LDS: return Alpha::LDSr;
869 case Alpha::LDT: return Alpha::LDTr;
870 case Alpha::LDL: return Alpha::LDLr;
871 case Alpha::LDBU: return Alpha::LDBUr;
872 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000873 case Alpha::STB: return Alpha::STBr;
874 case Alpha::STW: return Alpha::STWr;
875 case Alpha::STL: return Alpha::STLr;
876 case Alpha::STQ: return Alpha::STQr;
877 case Alpha::STS: return Alpha::STSr;
878 case Alpha::STT: return Alpha::STTr;
879
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000880 }
881}
Andrew Lenharth65838902005-02-06 16:22:15 +0000882
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000883void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000884{
885 unsigned Opc;
886 if (EnableAlphaFTOI) {
887 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000888 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000889 } else {
890 //The hard way:
891 // Spill the integer to memory and reload it from there.
892 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
893 MachineFunction *F = BB->getParent();
894 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
895
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000896 if (EnableAlphaLSMark)
897 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
898 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000899 Opc = isDouble ? Alpha::STT : Alpha::STS;
900 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000901
902 if (EnableAlphaLSMark)
903 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
904 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000905 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
906 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
907 }
908}
909
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000910void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000911{
912 unsigned Opc;
913 if (EnableAlphaFTOI) {
914 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000915 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000916 } else {
917 //The hard way:
918 // Spill the integer to memory and reload it from there.
919 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
920 MachineFunction *F = BB->getParent();
921 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
922
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000923 if (EnableAlphaLSMark)
924 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
925 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000926 Opc = isDouble ? Alpha::STQ : Alpha::STL;
927 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000928
929 if (EnableAlphaLSMark)
930 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
931 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000932 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
933 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
934 }
935}
936
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000937bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000938{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000939 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000940 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000941 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000942 bool rev = false;
943 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000944
Chris Lattner88ac32c2005-08-09 20:21:10 +0000945 switch (CC) {
946 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000947 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
948 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
949 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
950 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
951 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
952 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
953 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000955 ConstantFPSDNode *CN;
956 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
957 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
958 Tmp1 = Alpha::F31;
959 else
960 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000961
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000962 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
963 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
964 Tmp2 = Alpha::F31;
965 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000966 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000967
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000968 //Can only compare doubles, and dag won't promote for me
969 if (SetCC->getOperand(0).getValueType() == MVT::f32)
970 {
971 //assert(0 && "Setcc On float?\n");
972 std::cerr << "Setcc on float!\n";
973 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000974 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000975 Tmp1 = Tmp3;
976 }
977 if (SetCC->getOperand(1).getValueType() == MVT::f32)
978 {
979 //assert (0 && "Setcc On float?\n");
980 std::cerr << "Setcc on float!\n";
981 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000982 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000983 Tmp2 = Tmp3;
984 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000985
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000986 if (rev) std::swap(Tmp1, Tmp2);
987 //do the comparison
988 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
989 return inv;
990}
991
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000992//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000993void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000994{
995 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000996 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
997 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
998 { //Normal imm add
999 Reg = SelectExpr(N.getOperand(0));
1000 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1001 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001002 }
1003 Reg = SelectExpr(N);
1004 offset = 0;
1005 return;
1006}
1007
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001008void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +00001009{
1010 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001011 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001012 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1013 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001014
Andrew Lenharth445171a2005-02-08 00:40:03 +00001015 Select(N.getOperand(0)); //chain
1016 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001017
Andrew Lenharth445171a2005-02-08 00:40:03 +00001018 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001019 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001020 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1021 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001022 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +00001023 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
1024 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001025 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001026
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001027 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001028 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001029 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001030
Andrew Lenharth694c2982005-06-26 23:01:11 +00001031 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001032 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001033 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1034 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1035 case ISD::SETLT: Opc = Alpha::BLT; break;
1036 case ISD::SETLE: Opc = Alpha::BLE; break;
1037 case ISD::SETGT: Opc = Alpha::BGT; break;
1038 case ISD::SETGE: Opc = Alpha::BGE; break;
1039 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1040 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001041 //Technically you could have this CC
1042 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001043 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1044 case ISD::SETNE: Opc = Alpha::BNE; break;
1045 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001046 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001047 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1048 return;
1049 } else {
1050 unsigned Tmp1 = SelectExpr(CC);
1051 if (isNE)
1052 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1053 else
1054 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001055 return;
1056 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001057 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +00001058 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001059 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001060 //for a cmp b: c = a - b;
1061 //a = b: c = 0
1062 //a < b: c < 0
1063 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001064
1065 bool invTest = false;
1066 unsigned Tmp3;
1067
1068 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001069 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001070 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001071 Tmp3 = SelectExpr(CC.getOperand(0));
1072 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001073 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1074 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001075 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001076 invTest = true;
1077 }
1078 else
1079 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001080 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1081 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1082 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001083 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1084 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1085 .addReg(Tmp1).addReg(Tmp2);
1086 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001087
Chris Lattner88ac32c2005-08-09 20:21:10 +00001088 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001089 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001090 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1091 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1092 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1093 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1094 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1095 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001096 }
1097 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001098 return;
1099 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001100 abort(); //Should never be reached
1101 } else {
1102 //Giveup and do the stupid thing
1103 unsigned Tmp1 = SelectExpr(CC);
1104 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1105 return;
1106 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001107 abort(); //Should never be reached
1108}
1109
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001110unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001111 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001112 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001113 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001114 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +00001115 int64_t SImm;
1116 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001117
1118 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001119 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001120 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001121
1122 unsigned &Reg = ExprMap[N];
1123 if (Reg) return Reg;
1124
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001125 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001126 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001127 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001128 else {
1129 // If this is a call instruction, make sure to prepare ALL of the result
1130 // values as well as the chain.
1131 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001132 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001133 else {
1134 Result = MakeReg(Node->getValueType(0));
1135 ExprMap[N.getValue(0)] = Result;
1136 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1137 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001138 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001139 }
1140 }
1141
Andrew Lenharth40831c52005-01-28 06:57:18 +00001142 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001143 default:
1144 Node->dump();
1145 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001147 case ISD::CTPOP:
1148 case ISD::CTTZ:
1149 case ISD::CTLZ:
1150 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1151 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1152 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001153 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001154 return Result;
1155
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001156 case ISD::MULHU:
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 Tmp2 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001160 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001161 case ISD::MULHS:
1162 {
1163 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1164 Tmp1 = SelectExpr(N.getOperand(0));
1165 Tmp2 = SelectExpr(N.getOperand(1));
1166 Tmp3 = MakeReg(MVT::i64);
1167 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1168 unsigned V1 = MakeReg(MVT::i64);
1169 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001170 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1171 .addReg(Tmp1);
1172 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1173 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001174 unsigned IRes = MakeReg(MVT::i64);
1175 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1176 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1177 return Result;
1178 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001179 case ISD::UNDEF: {
1180 BuildMI(BB, Alpha::IDEF, 0, Result);
1181 return Result;
1182 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001183
Andrew Lenharth032f2352005-02-22 21:59:48 +00001184 case ISD::DYNAMIC_STACKALLOC:
1185 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001186 if (Result != notIn)
1187 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001188 else
1189 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1190
1191 // FIXME: We are currently ignoring the requested alignment for handling
1192 // greater than the stack alignment. This will need to be revisited at some
1193 // point. Align = N.getOperand(2);
1194
1195 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1196 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1197 std::cerr << "Cannot allocate stack object with greater alignment than"
1198 << " the stack alignment yet!";
1199 abort();
1200 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001201
Andrew Lenharth032f2352005-02-22 21:59:48 +00001202 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001203 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
1204 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
1205 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001206 Tmp1 = SelectExpr(N.getOperand(1));
1207 // Subtract size from stack pointer, thereby allocating some space.
1208 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1209 }
1210
1211 // Put a pointer to the space into the result register, by copying the stack
1212 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001213 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001214 return Result;
1215
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001216 case ISD::ConstantPool:
1217 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1218 AlphaLowering.restoreGP(BB);
1219 Tmp2 = MakeReg(MVT::i64);
1220 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1221 .addReg(Alpha::R29);
1222 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1223 .addReg(Tmp2);
1224 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001225
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001226 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001227 BuildMI(BB, Alpha::LDA, 2, Result)
1228 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1229 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001230 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001231
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001232 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001233 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001234 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001235 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001236 {
1237 // Make sure we generate both values.
1238 if (Result != notIn)
1239 ExprMap[N.getValue(1)] = notIn; // Generate the token
1240 else
1241 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001242
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001243 SDOperand Chain = N.getOperand(0);
1244 SDOperand Address = N.getOperand(1);
1245 Select(Chain);
1246
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001247 bool fpext = true;
1248
Andrew Lenharth03824012005-02-07 05:55:55 +00001249 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001250 switch (Node->getValueType(0)) {
1251 default: Node->dump(); assert(0 && "Bad load!");
1252 case MVT::i64: Opc = Alpha::LDQ; break;
1253 case MVT::f64: Opc = Alpha::LDT; break;
1254 case MVT::f32: Opc = Alpha::LDS; break;
1255 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001256 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001257 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001258 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001259 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001260 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001261 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001262 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001263 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001264 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001265 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001266 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001267
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001268 int i, j, k;
1269 if (EnableAlphaLSMark)
1270 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1271 i, j, k);
1272
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001273 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1274 if (GASD && !GASD->getGlobal()->isExternal()) {
1275 Tmp1 = MakeReg(MVT::i64);
1276 AlphaLowering.restoreGP(BB);
1277 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1278 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1279 if (EnableAlphaLSMark)
1280 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1281 .addImm(getUID());
1282 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1283 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001284 } else if (ConstantPoolSDNode *CP =
1285 dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001286 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001287 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001288 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001289 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1290 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001291 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001292 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1293 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001294 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1295 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1296 } else if(Address.getOpcode() == ISD::FrameIndex) {
1297 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001298 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1299 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001300 BuildMI(BB, Opc, 2, Result)
1301 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1302 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001303 } else {
1304 long offset;
1305 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001306 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001307 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1308 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001309 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1310 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001311 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001312 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001313
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001314 case ISD::GlobalAddress:
1315 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001316 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +00001317
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001318 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001319
1320 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001321 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001322 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001323
1324 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001325 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1326 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001327 return Result;
1328
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001329 case ISD::ExternalSymbol:
1330 AlphaLowering.restoreGP(BB);
1331 has_sym = true;
1332
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001333 Reg = Result = MakeReg(MVT::i64);
1334
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001335 if (EnableAlphaLSMark)
1336 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1337 .addImm(getUID());
1338
1339 BuildMI(BB, Alpha::LDQl, 2, Result)
1340 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1341 .addReg(Alpha::R29);
1342 return Result;
1343
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001344 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001345 case ISD::CALL:
1346 {
1347 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001348
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001349 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001350 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001351
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001352 //grab the arguments
1353 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001354 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001355 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001356 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001357
Andrew Lenharth684f2292005-01-30 00:35:27 +00001358 //in reg args
1359 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001360 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001361 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001362 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001363 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001364 Alpha::F19, Alpha::F20, Alpha::F21};
1365 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366 default:
1367 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001368 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001369 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001370 N.getOperand(i+2).getValueType() << "\n";
1371 assert(0 && "Unknown value type for call");
1372 case MVT::i1:
1373 case MVT::i8:
1374 case MVT::i16:
1375 case MVT::i32:
1376 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001377 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1378 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001379 break;
1380 case MVT::f32:
1381 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001382 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1383 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001385 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001386 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001387 //in mem args
1388 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 {
1390 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001391 default:
1392 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001393 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001395 N.getOperand(i+2).getValueType() << "\n";
1396 assert(0 && "Unknown value type for call");
1397 case MVT::i1:
1398 case MVT::i8:
1399 case MVT::i16:
1400 case MVT::i32:
1401 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001402 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1403 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001404 break;
1405 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001406 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1407 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001408 break;
1409 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001410 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1411 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001412 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001413 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001414 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001415 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001416 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1417 if (GASD && !GASD->getGlobal()->isExternal()) {
1418 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001420 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1421 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001422 } else {
1423 //no need to restore GP as we are doing an indirect call
1424 Tmp1 = SelectExpr(N.getOperand(1));
1425 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1426 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1427 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001428
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001429 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001430
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001431 switch (Node->getValueType(0)) {
1432 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001433 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001434 case MVT::i1:
1435 case MVT::i8:
1436 case MVT::i16:
1437 case MVT::i32:
1438 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001439 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1440 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001441 case MVT::f32:
1442 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001443 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1444 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001445 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001446 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001447 }
1448
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001449 case ISD::SIGN_EXTEND_INREG:
1450 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001451 //do SDIV opt for all levels of ints if not dividing by a constant
1452 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1453 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001454 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001455 unsigned Tmp4 = MakeReg(MVT::f64);
1456 unsigned Tmp5 = MakeReg(MVT::f64);
1457 unsigned Tmp6 = MakeReg(MVT::f64);
1458 unsigned Tmp7 = MakeReg(MVT::f64);
1459 unsigned Tmp8 = MakeReg(MVT::f64);
1460 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001461
1462 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1463 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1464 MoveInt2FP(Tmp1, Tmp4, true);
1465 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001466 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1467 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001468 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001469 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001470 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001471 return Result;
1472 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001473
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001474 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001475 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 switch (N.getOperand(0).getOpcode()) {
1477 case ISD::ADD:
1478 case ISD::SUB:
1479 case ISD::MUL:
1480 {
1481 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1482 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1483 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001484 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001485 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001486 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001487 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001488 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1489 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1490 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1491 2,Result).addReg(Tmp1).addReg(Tmp2);
1492 }
1493 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001494 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001495 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001496 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001497 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1498 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1499 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1500 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001501 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001502 { //Normal imm add/sub
1503 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001504 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001505 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001506 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001507 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1508 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001509 { //handle canonicalization
1510 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1511 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001512 SImm = 0 - ((SImm << 32) >> 32);
1513 assert(SImm >= 0 && SImm <= 255);
1514 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001515 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001516 else
1517 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001518 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001520 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1522 }
1523 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001524 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001525 default: break; //Fall Though;
1526 }
1527 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001528 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001529 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001530 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001531 default:
1532 Node->dump();
1533 assert(0 && "Sign Extend InReg not there yet");
1534 break;
1535 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001536 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001538 break;
1539 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001540 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001541 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 break;
1543 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001544 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001545 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001546 case MVT::i1:
1547 Tmp2 = MakeReg(MVT::i64);
1548 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001549 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001550 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001552 return Result;
1553 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001554
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001556 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001557 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1558 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1559 bool isConst = false;
1560 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001561
Chris Lattner88ac32c2005-08-09 20:21:10 +00001562 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001563 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001564 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001565
Chris Lattner88ac32c2005-08-09 20:21:10 +00001566 switch (CC) {
1567 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1568 case ISD::SETEQ:
1569 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1570 case ISD::SETLT:
1571 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1572 case ISD::SETLE:
1573 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1574 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1575 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1576 case ISD::SETULT:
1577 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1578 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1579 case ISD::SETULE:
1580 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1581 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1582 case ISD::SETNE: {//Handle this one special
1583 //std::cerr << "Alpha does not have a setne.\n";
1584 //abort();
1585 Tmp1 = SelectExpr(N.getOperand(0));
1586 Tmp2 = SelectExpr(N.getOperand(1));
1587 Tmp3 = MakeReg(MVT::i64);
1588 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1589 //Remeber we have the Inv for this CC
1590 CCInvMap[N] = Tmp3;
1591 //and invert
1592 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1593 return Result;
1594 }
1595 }
1596 if (dir == 1) {
1597 Tmp1 = SelectExpr(N.getOperand(0));
1598 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001599 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001600 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001601 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001602 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001603 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001604 } else { //if (dir == 2) {
1605 Tmp1 = SelectExpr(N.getOperand(1));
1606 Tmp2 = SelectExpr(N.getOperand(0));
1607 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001608 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001609 } else {
1610 //do the comparison
1611 Tmp1 = MakeReg(MVT::f64);
1612 bool inv = SelectFPSetCC(N, Tmp1);
1613
1614 //now arrange for Result (int) to have a 1 or 0
1615 Tmp2 = MakeReg(MVT::i64);
1616 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1617 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1618 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001619 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001620 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001621 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001622
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001623 case ISD::CopyFromReg:
1624 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001625 ++count_ins;
1626
Andrew Lenharth40831c52005-01-28 06:57:18 +00001627 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001628 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001629 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001630 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001631 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001632
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001633 SDOperand Chain = N.getOperand(0);
1634
1635 Select(Chain);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001636 unsigned r = cast<RegSDNode>(Node)->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001637 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001638 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001639 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1640 else
1641 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001642 return Result;
1643 }
1644
Misha Brukman4633f1c2005-04-21 23:13:11 +00001645 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001646 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001647 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001648 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001649 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1650 Tmp1 = SelectExpr(N.getOperand(0));
1651 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1652 return Result;
1653 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001654 //Fall through
1655 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001656 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001657 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001658 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001659 unsigned int build = 0;
1660 for(int i = 0; i < 8; ++i)
1661 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001662 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001663 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001664 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001665 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001666 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001667 }
1668 if (build)
1669 {
1670 Tmp1 = SelectExpr(N.getOperand(0));
1671 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1672 return Result;
1673 }
1674 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001675 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001676 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001677 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001678 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001679 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001680 case ISD::AND: Opc = Alpha::BIC; break;
1681 case ISD::OR: Opc = Alpha::ORNOT; break;
1682 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001683 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001684 Tmp1 = SelectExpr(N.getOperand(1));
1685 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1686 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1687 return Result;
1688 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001689 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001690 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001691 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001692 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001693 case ISD::AND: Opc = Alpha::BIC; break;
1694 case ISD::OR: Opc = Alpha::ORNOT; break;
1695 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001696 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001697 Tmp1 = SelectExpr(N.getOperand(0));
1698 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1699 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1700 return Result;
1701 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001702 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001703 case ISD::SHL:
1704 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001705 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001706 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001707 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001708 switch(opcode) {
1709 case ISD::AND: Opc = Alpha::ANDi; break;
1710 case ISD::OR: Opc = Alpha::BISi; break;
1711 case ISD::XOR: Opc = Alpha::XORi; break;
1712 case ISD::SHL: Opc = Alpha::SLi; break;
1713 case ISD::SRL: Opc = Alpha::SRLi; break;
1714 case ISD::SRA: Opc = Alpha::SRAi; break;
1715 case ISD::MUL: Opc = Alpha::MULQi; break;
1716 };
1717 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001718 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001719 } else {
1720 switch(opcode) {
1721 case ISD::AND: Opc = Alpha::AND; break;
1722 case ISD::OR: Opc = Alpha::BIS; break;
1723 case ISD::XOR: Opc = Alpha::XOR; break;
1724 case ISD::SHL: Opc = Alpha::SL; break;
1725 case ISD::SRL: Opc = Alpha::SRL; break;
1726 case ISD::SRA: Opc = Alpha::SRA; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001727 case ISD::MUL:
1728 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001729 : Alpha::MULQ;
1730 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001731 };
1732 Tmp1 = SelectExpr(N.getOperand(0));
1733 Tmp2 = SelectExpr(N.getOperand(1));
1734 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1735 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001736 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001737
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001738 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001739 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001740 if (isFP) {
1741 ConstantFPSDNode *CN;
1742 if (opcode == ISD::ADD)
1743 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1744 else
1745 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1746 if (opcode == ISD::SUB
1747 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1748 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1749 {
1750 Tmp2 = SelectExpr(N.getOperand(1));
1751 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1752 } else {
1753 Tmp1 = SelectExpr(N.getOperand(0));
1754 Tmp2 = SelectExpr(N.getOperand(1));
1755 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1756 }
1757 return Result;
1758 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001759 bool isAdd = opcode == ISD::ADD;
1760
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001761 //first check for Scaled Adds and Subs!
1762 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001763 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1764 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1765 (SImm == 2 || SImm == 3)) {
1766 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001767 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001768 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001769 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001770 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001771 else {
1772 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001773 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1774 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001775 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001776 }
1777 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001778 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001779 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1780 (SImm == 2 || SImm == 3)) {
1781 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001782 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001783 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1784 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001785 else {
1786 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001787 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001788 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001789 }
1790 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001791 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001792 { //Normal imm add/sub
1793 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1794 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001795 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001796 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001797 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001798 { //inverted imm add/sub
1799 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1800 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001801 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001802 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001803 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001804 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001805 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001806 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001808 SImm = -SImm;
1809 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001810 }
1811 //give up and do the operation
1812 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001813 //Normal add/sub
1814 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1815 Tmp1 = SelectExpr(N.getOperand(0));
1816 Tmp2 = SelectExpr(N.getOperand(1));
1817 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1818 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001819 return Result;
1820 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001821
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001822 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001823 if (isFP) {
1824 Tmp1 = SelectExpr(N.getOperand(0));
1825 Tmp2 = SelectExpr(N.getOperand(1));
1826 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1827 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001828 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001829 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001830 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001831 if (isSIntImmediate(N.getOperand(1), SImm) &&
1832 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1833 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001834 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001835 if (k == 1)
1836 Tmp2 = Tmp1;
1837 else
1838 {
1839 Tmp2 = MakeReg(MVT::i64);
1840 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1841 }
1842 Tmp3 = MakeReg(MVT::i64);
1843 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1844 unsigned Tmp4 = MakeReg(MVT::i64);
1845 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001846 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001847 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1848 else
1849 {
1850 unsigned Tmp5 = MakeReg(MVT::i64);
1851 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1852 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1853 }
1854 return Result;
1855 }
1856 }
1857 //Else fall through
1858
1859 case ISD::UDIV:
1860 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001861 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001862 {
1863 // If this is a divide by constant, we can emit code using some magic
1864 // constants to implement it as a multiply instead.
1865 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001866 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001867 return SelectExpr(BuildSDIVSequence(N));
1868 else
1869 return SelectExpr(BuildUDIVSequence(N));
1870 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001871 }
1872 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001873 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001874 case ISD::SREM: {
1875 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001876 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001877 case ISD::UREM: opstr = "__remqu"; break;
1878 case ISD::SREM: opstr = "__remq"; break;
1879 case ISD::UDIV: opstr = "__divqu"; break;
1880 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001881 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001882 Tmp1 = SelectExpr(N.getOperand(0));
1883 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001884 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001885 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1886 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001887 //set up regs explicitly (helps Reg alloc)
1888 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001889 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001890 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1891 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001892 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001893 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001894 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001895
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001896 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001897 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001898 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001899 assert (DestType == MVT::i64 && "only quads can be loaded to");
1900 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001901 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001902 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001903 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001904 {
1905 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001906 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001907 Tmp1 = Tmp2;
1908 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001909 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001910 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001911 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001912
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001913 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001914 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001915
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001916 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001917 if (isFP) {
1918 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1919 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1920 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1921
1922 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001923
Chris Lattner88ac32c2005-08-09 20:21:10 +00001924 if (CC.getOpcode() == ISD::SETCC &&
1925 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1926 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001927
Jeff Cohen00b168892005-07-27 06:12:32 +00001928
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001929 //for a cmp b: c = a - b;
1930 //a = b: c = 0
1931 //a < b: c < 0
1932 //a > b: c > 0
1933
1934 bool invTest = false;
1935 unsigned Tmp3;
1936
1937 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001938 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001939 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001940 Tmp3 = SelectExpr(CC.getOperand(0));
1941 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001942 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1943 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001944 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001945 invTest = true;
1946 }
1947 else
1948 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001949 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1950 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1951 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001952 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1953 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1954 .addReg(Tmp1).addReg(Tmp2);
1955 }
1956
Chris Lattner88ac32c2005-08-09 20:21:10 +00001957 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001958 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1959 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1960 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1961 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1962 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1963 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1964 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1965 }
1966 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1967 return Result;
1968 }
1969 else
1970 {
1971 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1972 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1973 .addReg(Tmp1);
1974// // Spill the cond to memory and reload it from there.
1975// unsigned Tmp4 = MakeReg(MVT::f64);
1976// MoveIntFP(Tmp1, Tmp4, true);
1977// //now ideally, we don't have to do anything to the flag...
1978// // Get the condition into the zero flag.
1979// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1980 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001981 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001982 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001983 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1984 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001985 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001986 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1987 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001988 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001989 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001990
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001991 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001992
Misha Brukman4633f1c2005-04-21 23:13:11 +00001993 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001994 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001995 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001997 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1998 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001999 bool inv = SelectFPSetCC(CC, Tmp1);
2000 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2001 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2002 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002003 }
2004 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002005 //Int SetCC -> Select
2006 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00002007 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002008 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00002009 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002010
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002011 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00002012 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002013 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002014 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002015
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002016 //Choose the CMOV
2017 switch (cCode) {
2018 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002019 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2020 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2021 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2022 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2023 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2024 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2025 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2026 //Technically you could have this CC
2027 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2028 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2029 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002030 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002031 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002032
Andrew Lenharth694c2982005-06-26 23:01:11 +00002033 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002034 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00002035 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002036 } else {
2037 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2038 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2039 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2040 }
2041 return Result;
2042 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002043 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002044 }
2045 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002046 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2047 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002048 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2049 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002050
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002051 return Result;
2052 }
2053
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002054 case ISD::Constant:
2055 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002056 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002057 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00002058 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002059 ((int32_t)val < 0)) {
2060 //try a small load and zero extend
2061 val = (int32_t)val;
2062 zero_extend_top = 15;
2063 }
2064
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002065 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002066 if(!zero_extend_top)
2067 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2068 else {
2069 Tmp1 = MakeReg(MVT::i64);
2070 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
2071 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
2072 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002073 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002074 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2075 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2076 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002077 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2078 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002079 if (!zero_extend_top)
2080 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2081 else {
2082 Tmp3 = MakeReg(MVT::i64);
2083 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
2084 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
2085 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002086 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002087 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002088 //re-get the val since we are going to mem anyway
2089 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002090 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00002091 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002092 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002093 unsigned CPI = CP->getConstantPoolIndex(C);
2094 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002095 has_sym = true;
2096 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002097 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2098 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002099 if (EnableAlphaLSMark)
2100 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2101 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002102 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2103 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002104 }
2105 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002106 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002107 case ISD::FNEG:
2108 if(ISD::FABS == N.getOperand(0).getOpcode())
2109 {
2110 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2111 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2112 } else {
2113 Tmp1 = SelectExpr(N.getOperand(0));
2114 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2115 }
2116 return Result;
2117
2118 case ISD::FABS:
2119 Tmp1 = SelectExpr(N.getOperand(0));
2120 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2121 return Result;
2122
2123 case ISD::FP_ROUND:
2124 assert (DestType == MVT::f32 &&
2125 N.getOperand(0).getValueType() == MVT::f64 &&
2126 "only f64 to f32 conversion supported here");
2127 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002128 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002129 return Result;
2130
2131 case ISD::FP_EXTEND:
2132 assert (DestType == MVT::f64 &&
2133 N.getOperand(0).getValueType() == MVT::f32 &&
2134 "only f32 to f64 conversion supported here");
2135 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002136 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002137 return Result;
2138
2139 case ISD::ConstantFP:
2140 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2141 if (CN->isExactlyValue(+0.0)) {
2142 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2143 .addReg(Alpha::F31);
2144 } else if ( CN->isExactlyValue(-0.0)) {
2145 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2146 .addReg(Alpha::F31);
2147 } else {
2148 abort();
2149 }
2150 }
2151 return Result;
2152
2153 case ISD::SINT_TO_FP:
2154 {
2155 assert (N.getOperand(0).getValueType() == MVT::i64
2156 && "only quads can be loaded from");
2157 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2158 Tmp2 = MakeReg(MVT::f64);
2159 MoveInt2FP(Tmp1, Tmp2, true);
2160 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00002161 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002162 return Result;
2163 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002164 }
2165
2166 return 0;
2167}
2168
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002169void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002170 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002171 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002172
Nate Begeman85fdeb22005-03-24 04:39:54 +00002173 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002174 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002175
2176 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002177
Andrew Lenharth760270d2005-02-07 23:02:23 +00002178 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002179
2180 default:
2181 Node->dump(); std::cerr << "\n";
2182 assert(0 && "Node not handled yet!");
2183
2184 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002185 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002186 return;
2187 }
2188
2189 case ISD::BR: {
2190 MachineBasicBlock *Dest =
2191 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2192
2193 Select(N.getOperand(0));
2194 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2195 return;
2196 }
2197
2198 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002199 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002200 Select(N.getOperand(0));
2201 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2202 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002203
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002204 case ISD::EntryToken: return; // Noop
2205
2206 case ISD::TokenFactor:
2207 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2208 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002209
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002210 //N.Val->dump(); std::cerr << "\n";
2211 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002212
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002213 return;
2214
2215 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002216 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217 Select(N.getOperand(0));
2218 Tmp1 = SelectExpr(N.getOperand(1));
2219 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002220
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002221 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002222 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002223 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002224 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2225 else
2226 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 }
2228 return;
2229
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002230 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002231 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002232 switch (N.getNumOperands()) {
2233 default:
2234 std::cerr << N.getNumOperands() << "\n";
2235 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2236 std::cerr << N.getOperand(i).getValueType() << "\n";
2237 Node->dump();
2238 assert(0 && "Unknown return instruction!");
2239 case 2:
2240 Select(N.getOperand(0));
2241 Tmp1 = SelectExpr(N.getOperand(1));
2242 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002243 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002244 assert(0 && "All other types should have been promoted!!");
2245 case MVT::f64:
2246 case MVT::f32:
2247 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2248 break;
2249 case MVT::i32:
2250 case MVT::i64:
2251 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2252 break;
2253 }
2254 break;
2255 case 1:
2256 Select(N.getOperand(0));
2257 break;
2258 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002259 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002260 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00002261 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002262 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002263
Misha Brukman4633f1c2005-04-21 23:13:11 +00002264 case ISD::TRUNCSTORE:
2265 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002266 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002267 SDOperand Chain = N.getOperand(0);
2268 SDOperand Value = N.getOperand(1);
2269 SDOperand Address = N.getOperand(2);
2270 Select(Chain);
2271
2272 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002273
2274 if (opcode == ISD::STORE) {
2275 switch(Value.getValueType()) {
2276 default: assert(0 && "unknown Type in store");
2277 case MVT::i64: Opc = Alpha::STQ; break;
2278 case MVT::f64: Opc = Alpha::STT; break;
2279 case MVT::f32: Opc = Alpha::STS; break;
2280 }
2281 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002282 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00002283 default: assert(0 && "unknown Type in store");
2284 case MVT::i1: //FIXME: DAG does not promote this load
2285 case MVT::i8: Opc = Alpha::STB; break;
2286 case MVT::i16: Opc = Alpha::STW; break;
2287 case MVT::i32: Opc = Alpha::STL; break;
2288 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002289 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002290
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002291 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00002292 if (EnableAlphaLSMark)
2293 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002294 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002295
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002296 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2297 if (GASD && !GASD->getGlobal()->isExternal()) {
2298 Tmp2 = MakeReg(MVT::i64);
2299 AlphaLowering.restoreGP(BB);
2300 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2301 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2302 if (EnableAlphaLSMark)
2303 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2304 .addImm(getUID());
2305 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2306 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002307 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002308 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002309 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2310 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002311 BuildMI(BB, Opc, 3).addReg(Tmp1)
2312 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2313 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002314 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002315 long offset;
2316 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002317 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002318 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2319 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002320 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2321 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002322 return;
2323 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002324
2325 case ISD::EXTLOAD:
2326 case ISD::SEXTLOAD:
2327 case ISD::ZEXTLOAD:
2328 case ISD::LOAD:
2329 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002330 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002331 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002332 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002333 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334 SelectExpr(N);
2335 return;
2336
Chris Lattner16cd04d2005-05-12 23:24:06 +00002337 case ISD::CALLSEQ_START:
2338 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002339 Select(N.getOperand(0));
2340 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002341
Chris Lattner16cd04d2005-05-12 23:24:06 +00002342 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002343 Alpha::ADJUSTSTACKUP;
2344 BuildMI(BB, Opc, 1).addImm(Tmp1);
2345 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002346
2347 case ISD::PCMARKER:
2348 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002349 BuildMI(BB, Alpha::PCLABEL, 2)
2350 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002351 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002352 }
2353 assert(0 && "Should not be reached!");
2354}
2355
2356
2357/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2358/// into a machine code representation using pattern matching and a machine
2359/// description file.
2360///
2361FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002362 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002363}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002364