blob: 43bc1b8396ddda0ef43145ec1183dea44c87ba85 [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
67 FTOI,
68 };
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 Lenharth6968bff2005-06-27 23:24:11 +000095
Andrew Lenharthec151362005-06-26 22:23:06 +000096 setOperationAction(ISD::ZEXTLOAD, MVT::i1 , Expand);
97 setOperationAction(ISD::ZEXTLOAD, MVT::i32 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000098
Andrew Lenharthec151362005-06-26 22:23:06 +000099 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
100 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
102
103 setOperationAction(ISD::SREM, MVT::f32, Expand);
104 setOperationAction(ISD::SREM, MVT::f64, Expand);
105
106 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000107
Andrew Lenharth59009192005-05-04 19:12:09 +0000108 if (!EnableAlphaCT) {
109 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
110 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000111 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000112 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000113
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000114 //If this didn't legalize into a div....
115 // setOperationAction(ISD::SREM , MVT::i64, Expand);
116 // setOperationAction(ISD::UREM , MVT::i64, Expand);
117
118 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
119 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
120 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000121
Chris Lattner17234b72005-04-30 04:26:06 +0000122 // We don't support sin/cos/sqrt
123 setOperationAction(ISD::FSIN , MVT::f64, Expand);
124 setOperationAction(ISD::FCOS , MVT::f64, Expand);
125 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
126 setOperationAction(ISD::FSIN , MVT::f32, Expand);
127 setOperationAction(ISD::FCOS , MVT::f32, Expand);
128 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
129
Andrew Lenharth33819132005-03-04 20:09:23 +0000130 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000131 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000132
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000133 //Try a couple things with a custom expander
134 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
135
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000136 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000137
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000138 addLegalFPImmediate(+0.0); //F31
139 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 }
141
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000142 /// LowerOperation - Provide custom lowering hooks for some operations.
143 ///
144 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
145
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 /// LowerArguments - This hook must be implemented to indicate how we should
147 /// lower the arguments for the specified function, into the specified DAG.
148 virtual std::vector<SDOperand>
149 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000150
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151 /// LowerCallTo - This hook lowers an abstract call to a function into an
152 /// actual call.
153 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000154 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000155 bool isTailCall, SDOperand Callee, ArgListTy &Args,
156 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000157
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000158 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000159 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000160
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000162 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000163 const Type *ArgTy, SelectionDAG &DAG);
164
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000165 std::pair<SDOperand,SDOperand>
166 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
167 SelectionDAG &DAG);
168
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000169 virtual std::pair<SDOperand, SDOperand>
170 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
171 SelectionDAG &DAG);
172
173 void restoreGP(MachineBasicBlock* BB)
174 {
175 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
176 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000177 void restoreRA(MachineBasicBlock* BB)
178 {
179 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
180 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000181 unsigned getRA()
182 {
183 return RA;
184 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000185
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000186 };
187}
188
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000189/// LowerOperation - Provide custom lowering hooks for some operations.
190///
191SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
192 MachineFunction &MF = DAG.getMachineFunction();
193 switch (Op.getOpcode()) {
194 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000195#if 0
196 case ISD::SINT_TO_FP:
197 {
198 assert (Op.getOperand(0).getValueType() == MVT::i64
199 && "only quads can be loaded from");
200 SDOperand SRC;
201 if (EnableAlphaFTOI)
202 {
203 std::vector<MVT::ValueType> RTs;
204 RTs.push_back(Op.getValueType());
205 std::vector<SDOperand> Ops;
206 Ops.push_back(Op.getOperand(0));
207 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
208 } else {
209 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
210 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000211 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
212 DAG.getEntryNode(), Op.getOperand(0),
213 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000214 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
215 DAG.getSrcValue(NULL));
216 }
217 std::vector<MVT::ValueType> RTs;
218 RTs.push_back(Op.getValueType());
219 std::vector<SDOperand> Ops;
220 Ops.push_back(SRC);
221 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
222 }
223#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000224 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000225 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000226}
227
228
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000229/// AddLiveIn - This helper function adds the specified physical register to the
230/// MachineFunction as a live in value. It also creates a corresponding virtual
231/// register for it.
232static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
233 TargetRegisterClass *RC) {
234 assert(RC->contains(PReg) && "Not the correct regclass!");
235 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
236 MF.addLiveIn(PReg, VReg);
237 return VReg;
238}
239
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
241
242//For now, just use variable size stack frame format
243
244//In a standard call, the first six items are passed in registers $16
245//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
246//of argument-to-register correspondence.) The remaining items are
247//collected in a memory argument list that is a naturally aligned
248//array of quadwords. In a standard call, this list, if present, must
249//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000250//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000252// //#define FP $15
253// //#define RA $26
254// //#define PV $27
255// //#define GP $29
256// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000257
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000259AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260{
261 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000262
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000264 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 MachineBasicBlock& BB = MF.front();
267
Misha Brukman4633f1c2005-04-21 23:13:11 +0000268 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000269 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000270 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000271 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000273
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000274 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000275 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000276
Chris Lattnere4d5c442005-03-15 04:54:21 +0000277 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000278 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000279 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000280 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000281 unsigned Vreg;
282 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000283 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000284 default:
285 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 abort();
287 case MVT::f64:
288 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000289 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
290 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000291 break;
292 case MVT::i1:
293 case MVT::i8:
294 case MVT::i16:
295 case MVT::i32:
296 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000297 args_int[count] = AddLiveIn(MF, args_int[count],
298 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000299 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000300 if (VT != MVT::i64)
301 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000302 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000303 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000304 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000305 } else { //more args
306 // Create the frame index object for this incoming parameter...
307 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000308
309 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000310 //from this parameter
311 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000312 argt = DAG.getLoad(getValueType(I->getType()),
313 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000314 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000315 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000316 ArgValues.push_back(argt);
317 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000318
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000319 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000320 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000321 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000322 std::vector<SDOperand> LS;
323 for (int i = 0; i < 6; ++i) {
324 if (args_int[i] < 1024)
325 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
326 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000327 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000328 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000329 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000330 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
331 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000332
333 if (args_float[i] < 1024)
334 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
335 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000336 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
337 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000338 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
339 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000340 }
341
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000342 //Set up a token factor with all the stack traffic
343 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
344 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000345
346 // Finally, inform the code generator which regs we return values in.
347 switch (getValueType(F.getReturnType())) {
348 default: assert(0 && "Unknown type!");
349 case MVT::isVoid: break;
350 case MVT::i1:
351 case MVT::i8:
352 case MVT::i16:
353 case MVT::i32:
354 case MVT::i64:
355 MF.addLiveOut(Alpha::R0);
356 break;
357 case MVT::f32:
358 case MVT::f64:
359 MF.addLiveOut(Alpha::F0);
360 break;
361 }
362
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000363 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000364 return ArgValues;
365}
366
367std::pair<SDOperand, SDOperand>
368AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000369 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000370 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000371 SDOperand Callee, ArgListTy &Args,
372 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000373 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000374 if (Args.size() > 6)
375 NumBytes = (Args.size() - 6) * 8;
376
Chris Lattner16cd04d2005-05-12 23:24:06 +0000377 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000378 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000379 std::vector<SDOperand> args_to_use;
380 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381 {
382 switch (getValueType(Args[i].second)) {
383 default: assert(0 && "Unexpected ValueType for argument!");
384 case MVT::i1:
385 case MVT::i8:
386 case MVT::i16:
387 case MVT::i32:
388 // Promote the integer to 64 bits. If the input type is signed use a
389 // sign extend, otherwise use a zero extend.
390 if (Args[i].second->isSigned())
391 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
392 else
393 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
394 break;
395 case MVT::i64:
396 case MVT::f64:
397 case MVT::f32:
398 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 args_to_use.push_back(Args[i].first);
401 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000402
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000403 std::vector<MVT::ValueType> RetVals;
404 MVT::ValueType RetTyVT = getValueType(RetTy);
405 if (RetTyVT != MVT::isVoid)
406 RetVals.push_back(RetTyVT);
407 RetVals.push_back(MVT::Other);
408
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000411 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000412 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000413 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000414 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000415}
416
417std::pair<SDOperand, SDOperand>
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000418AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG,
419 SDOperand Dest) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000420 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000421 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000422 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest,
423 DAG.getSrcValue(NULL));
424 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest,
425 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000426 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000427 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000428 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000429 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000430}
431
432std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000433LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000434 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000435 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
436 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
437 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000438 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
439 Tmp, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000440 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000441 if (ArgTy->isFloatingPoint())
442 {
443 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
444 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
445 DAG.getConstant(8*6, MVT::i64));
446 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
447 Offset, DAG.getConstant(8*6, MVT::i64));
448 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
449 }
450
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000451 SDOperand Result;
452 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000453 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 DAG.getSrcValue(NULL), MVT::i32);
455 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000456 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000457 DAG.getSrcValue(NULL), MVT::i32);
458 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000459 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000460 DAG.getSrcValue(NULL));
461
Andrew Lenharth558bc882005-06-18 18:34:52 +0000462 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
463 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000464 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
465 Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000466 Tmp, DAG.getSrcValue(NULL), MVT::i32);
467 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
468
Andrew Lenharth558bc882005-06-18 18:34:52 +0000469 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000470}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000471
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000472std::pair<SDOperand,SDOperand> AlphaTargetLowering::
473LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
474 SelectionDAG &DAG) {
475 //Default to returning the input list
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000476 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src,
477 DAG.getSrcValue(NULL));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000478 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
479 Val, Dest, DAG.getSrcValue(NULL));
480 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
481 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000482 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000483 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000484 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
485 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000486 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000487 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000488 return std::make_pair(Result, Result);
489}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000490
491std::pair<SDOperand, SDOperand> AlphaTargetLowering::
492LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
493 SelectionDAG &DAG) {
494 abort();
495}
496
497
498
499
500
501namespace {
502
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000503//===--------------------------------------------------------------------===//
504/// ISel - Alpha specific code to select Alpha machine instructions for
505/// SelectionDAG operations.
506//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000507class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000508
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000509 /// AlphaLowering - This object fully describes how to lower LLVM code to an
510 /// Alpha-specific SelectionDAG.
511 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000512
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000513 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
514 // for sdiv and udiv until it is put into the future
515 // dag combiner.
516
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000517 /// ExprMap - As shared expressions are codegen'd, we keep track of which
518 /// vreg the value is produced in, so we only emit one copy of each compiled
519 /// tree.
520 static const unsigned notIn = (unsigned)(-1);
521 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000522
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000523 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
524 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000525
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000526 int count_ins;
527 int count_outs;
528 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000529 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000530
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000531public:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000532 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
533 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000534 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000535
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000536 /// InstructionSelectBasicBlock - This callback is invoked by
537 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
538 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000539 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000540 count_ins = 0;
541 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000542 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000543 has_sym = false;
544
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000545 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000546 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000547 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000548 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000549
550 if(has_sym)
551 ++count_ins;
552 if(EnableAlphaCount)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000553 std::cerr << "COUNT: "
554 << BB->getParent()->getFunction ()->getName() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000555 << BB->getNumber() << " "
556 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000557 << count_ins << " "
558 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000559
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000560 // Clear state used for selection.
561 ExprMap.clear();
562 CCInvMap.clear();
563 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000564
565 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000566
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000567 unsigned SelectExpr(SDOperand N);
568 unsigned SelectExprFP(SDOperand N, unsigned Result);
569 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000570
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000571 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
572 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000573 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
574 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000575 //returns whether the sense of the comparison was inverted
576 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000577
578 // dag -> dag expanders for integer divide by constant
579 SDOperand BuildSDIVSequence(SDOperand N);
580 SDOperand BuildUDIVSequence(SDOperand N);
581
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000582};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000583}
584
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000585void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000586 // If this function has live-in values, emit the copies from pregs to vregs at
587 // the top of the function, before anything else.
588 MachineBasicBlock *BB = MF.begin();
589 if (MF.livein_begin() != MF.livein_end()) {
590 SSARegMap *RegMap = MF.getSSARegMap();
591 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
592 E = MF.livein_end(); LI != E; ++LI) {
593 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
594 if (RC == Alpha::GPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000595 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
596 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000597 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000598 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
599 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000600 } else {
601 assert(0 && "Unknown regclass!");
602 }
603 }
604 }
605}
606
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000607//Find the offset of the arg in it's parent's function
608static int getValueOffset(const Value* v)
609{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000610 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000611 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000612 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000613
614 const Instruction* itarget = dyn_cast<Instruction>(v);
615 const BasicBlock* btarget = itarget->getParent();
616 const Function* ftarget = btarget->getParent();
617
618 //offset due to earlier BBs
619 int i = 0;
620 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
621 i += ii->size();
622
623 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
624 ++i;
625
626 return i;
627}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000628//Find the offset of the function in it's module
629static int getFunctionOffset(const Function* fun)
630{
631 const Module* M = fun->getParent();
632
633 //offset due to earlier BBs
634 int i = 0;
635 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
636 ++i;
637
638 return i;
639}
640
641static int getUID()
642{
643 static int id = 0;
644 return ++id;
645}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000646
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000647//Factorize a number using the list of constants
648static bool factorize(int v[], int res[], int size, uint64_t c)
649{
650 bool cont = true;
651 while (c != 1 && cont)
652 {
653 cont = false;
654 for(int i = 0; i < size; ++i)
655 {
656 if (c % v[i] == 0)
657 {
658 c /= v[i];
659 ++res[i];
660 cont=true;
661 }
662 }
663 }
664 return c == 1;
665}
666
667
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000668//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000669// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000670// a multiply.
671struct ms {
672 int64_t m; // magic number
673 int64_t s; // shift amount
674};
675
676struct mu {
677 uint64_t m; // magic number
678 int64_t a; // add indicator
679 int64_t s; // shift amount
680};
681
682/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000683/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000684/// or -1.
685static struct ms magic(int64_t d) {
686 int64_t p;
687 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
688 const uint64_t two63 = 9223372036854775808ULL; // 2^63
689 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000690
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000691 ad = abs(d);
692 t = two63 + ((uint64_t)d >> 63);
693 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000694 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000695 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
696 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
697 q2 = two63/ad; // initialize q2 = 2p/abs(d)
698 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
699 do {
700 p = p + 1;
701 q1 = 2*q1; // update q1 = 2p/abs(nc)
702 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
703 if (r1 >= anc) { // must be unsigned comparison
704 q1 = q1 + 1;
705 r1 = r1 - anc;
706 }
707 q2 = 2*q2; // update q2 = 2p/abs(d)
708 r2 = 2*r2; // update r2 = rem(2p/abs(d))
709 if (r2 >= ad) { // must be unsigned comparison
710 q2 = q2 + 1;
711 r2 = r2 - ad;
712 }
713 delta = ad - r2;
714 } while (q1 < delta || (q1 == delta && r1 == 0));
715
716 mag.m = q2 + 1;
717 if (d < 0) mag.m = -mag.m; // resulting magic number
718 mag.s = p - 64; // resulting shift
719 return mag;
720}
721
722/// magicu - calculate the magic numbers required to codegen an integer udiv as
723/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
724static struct mu magicu(uint64_t d)
725{
726 int64_t p;
727 uint64_t nc, delta, q1, r1, q2, r2;
728 struct mu magu;
729 magu.a = 0; // initialize "add" indicator
730 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000731 p = 63; // initialize p
732 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
733 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
734 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
735 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000736 do {
737 p = p + 1;
738 if (r1 >= nc - r1 ) {
739 q1 = 2*q1 + 1; // update q1
740 r1 = 2*r1 - nc; // update r1
741 }
742 else {
743 q1 = 2*q1; // update q1
744 r1 = 2*r1; // update r1
745 }
746 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000747 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000748 q2 = 2*q2 + 1; // update q2
749 r2 = 2*r2 + 1 - d; // update r2
750 }
751 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000752 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000753 q2 = 2*q2; // update q2
754 r2 = 2*r2 + 1; // update r2
755 }
756 delta = d - 1 - r2;
757 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
758 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000759 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000760 return magu;
761}
762
763/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
764/// return a DAG expression to select that will generate the same value by
765/// multiplying by a magic number. See:
766/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000767SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000768 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000769 ms magics = magic(d);
770 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000772 ISelDAG->getConstant(magics.m, MVT::i64));
773 // If d > 0 and m < 0, add the numerator
774 if (d > 0 && magics.m < 0)
775 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
776 // If d < 0 and m > 0, subtract the numerator.
777 if (d < 0 && magics.m > 0)
778 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
779 // Shift right algebraic if shift value is nonzero
780 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000782 ISelDAG->getConstant(magics.s, MVT::i64));
783 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000785 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
786 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
787}
788
789/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
790/// return a DAG expression to select that will generate the same value by
791/// multiplying by a magic number. See:
792/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000793SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000794 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000795 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
796 mu magics = magicu(d);
797 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000798 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000799 ISelDAG->getConstant(magics.m, MVT::i64));
800 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000801 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000802 ISelDAG->getConstant(magics.s, MVT::i64));
803 } else {
804 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000805 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000806 ISelDAG->getConstant(1, MVT::i64));
807 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000808 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000809 ISelDAG->getConstant(magics.s-1, MVT::i64));
810 }
811 return Q;
812}
813
Andrew Lenhartha565c272005-04-06 22:03:13 +0000814//From PPC32
815/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
816/// returns zero when the input is not exactly a power of two.
817static unsigned ExactLog2(uint64_t Val) {
818 if (Val == 0 || (Val & (Val-1))) return 0;
819 unsigned Count = 0;
820 while (Val != 1) {
821 Val >>= 1;
822 ++Count;
823 }
824 return Count;
825}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000826
827
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000828//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000829static const int IMM_LOW = -32768;
830static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000831static const int IMM_MULT = 65536;
832
833static long getUpper16(long l)
834{
835 long y = l / IMM_MULT;
836 if (l % IMM_MULT > IMM_HIGH)
837 ++y;
838 return y;
839}
840
841static long getLower16(long l)
842{
843 long h = getUpper16(l);
844 return l - h * IMM_MULT;
845}
846
Andrew Lenharth65838902005-02-06 16:22:15 +0000847static unsigned GetSymVersion(unsigned opcode)
848{
849 switch (opcode) {
850 default: assert(0 && "unknown load or store"); return 0;
851 case Alpha::LDQ: return Alpha::LDQ_SYM;
852 case Alpha::LDS: return Alpha::LDS_SYM;
853 case Alpha::LDT: return Alpha::LDT_SYM;
854 case Alpha::LDL: return Alpha::LDL_SYM;
855 case Alpha::LDBU: return Alpha::LDBU_SYM;
856 case Alpha::LDWU: return Alpha::LDWU_SYM;
857 case Alpha::LDW: return Alpha::LDW_SYM;
858 case Alpha::LDB: return Alpha::LDB_SYM;
859 case Alpha::STQ: return Alpha::STQ_SYM;
860 case Alpha::STS: return Alpha::STS_SYM;
861 case Alpha::STT: return Alpha::STT_SYM;
862 case Alpha::STL: return Alpha::STL_SYM;
863 case Alpha::STW: return Alpha::STW_SYM;
864 case Alpha::STB: return Alpha::STB_SYM;
865 }
866}
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000867static unsigned GetRelVersion(unsigned opcode)
868{
869 switch (opcode) {
870 default: assert(0 && "unknown load or store"); return 0;
871 case Alpha::LDQ: return Alpha::LDQr;
872 case Alpha::LDS: return Alpha::LDSr;
873 case Alpha::LDT: return Alpha::LDTr;
874 case Alpha::LDL: return Alpha::LDLr;
875 case Alpha::LDBU: return Alpha::LDBUr;
876 case Alpha::LDWU: return Alpha::LDWUr;
877 }
878}
Andrew Lenharth65838902005-02-06 16:22:15 +0000879
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000880void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000881{
882 unsigned Opc;
883 if (EnableAlphaFTOI) {
884 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
885 BuildMI(BB, Opc, 1, dst).addReg(src);
886 } else {
887 //The hard way:
888 // Spill the integer to memory and reload it from there.
889 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
890 MachineFunction *F = BB->getParent();
891 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
892
893 Opc = isDouble ? Alpha::STT : Alpha::STS;
894 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
895 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
896 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
897 }
898}
899
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000900void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000901{
902 unsigned Opc;
903 if (EnableAlphaFTOI) {
904 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
905 BuildMI(BB, Opc, 1, dst).addReg(src);
906 } else {
907 //The hard way:
908 // Spill the integer to memory and reload it from there.
909 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
910 MachineFunction *F = BB->getParent();
911 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
912
913 Opc = isDouble ? Alpha::STQ : Alpha::STL;
914 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
915 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
916 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
917 }
918}
919
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000920bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000921{
922 SDNode *Node = N.Val;
923 unsigned Opc, Tmp1, Tmp2, Tmp3;
924 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
925
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000926 bool rev = false;
927 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000928
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000929 switch (SetCC->getCondition()) {
930 default: Node->dump(); assert(0 && "Unknown FP comparison!");
931 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
932 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
933 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
934 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
935 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
936 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
937 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000938
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000939 ConstantFPSDNode *CN;
940 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
941 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
942 Tmp1 = Alpha::F31;
943 else
944 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000945
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000946 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
947 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
948 Tmp2 = Alpha::F31;
949 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000950 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000951
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000952 //Can only compare doubles, and dag won't promote for me
953 if (SetCC->getOperand(0).getValueType() == MVT::f32)
954 {
955 //assert(0 && "Setcc On float?\n");
956 std::cerr << "Setcc on float!\n";
957 Tmp3 = MakeReg(MVT::f64);
958 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
959 Tmp1 = Tmp3;
960 }
961 if (SetCC->getOperand(1).getValueType() == MVT::f32)
962 {
963 //assert (0 && "Setcc On float?\n");
964 std::cerr << "Setcc on float!\n";
965 Tmp3 = MakeReg(MVT::f64);
966 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
967 Tmp2 = Tmp3;
968 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000969
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000970 if (rev) std::swap(Tmp1, Tmp2);
971 //do the comparison
972 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
973 return inv;
974}
975
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000976//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000977void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000978{
979 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000980 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
981 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
982 { //Normal imm add
983 Reg = SelectExpr(N.getOperand(0));
984 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
985 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000986 }
987 Reg = SelectExpr(N);
988 offset = 0;
989 return;
990}
991
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000992void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000993{
994 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000995 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000996 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
997 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000998
Andrew Lenharth445171a2005-02-08 00:40:03 +0000999 Select(N.getOperand(0)); //chain
1000 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001001
Andrew Lenharth445171a2005-02-08 00:40:03 +00001002 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001003 {
1004 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1005 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1006 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001007 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1008 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001009 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001010
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001011 //Fix up CC
1012 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001013
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001014 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001015 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001016
Andrew Lenharth694c2982005-06-26 23:01:11 +00001017 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001018 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001019 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1020 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1021 case ISD::SETLT: Opc = Alpha::BLT; break;
1022 case ISD::SETLE: Opc = Alpha::BLE; break;
1023 case ISD::SETGT: Opc = Alpha::BGT; break;
1024 case ISD::SETGE: Opc = Alpha::BGE; break;
1025 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1026 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001027 //Technically you could have this CC
1028 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001029 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1030 case ISD::SETNE: Opc = Alpha::BNE; break;
1031 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001032 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001033 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1034 return;
1035 } else {
1036 unsigned Tmp1 = SelectExpr(CC);
1037 if (isNE)
1038 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1039 else
1040 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001041 return;
1042 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001043 } else { //FP
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001044 //Any comparison between 2 values should be codegened as an folded
1045 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001046 //for a cmp b: c = a - b;
1047 //a = b: c = 0
1048 //a < b: c < 0
1049 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001050
1051 bool invTest = false;
1052 unsigned Tmp3;
1053
1054 ConstantFPSDNode *CN;
1055 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1056 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1057 Tmp3 = SelectExpr(SetCC->getOperand(0));
1058 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1059 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1060 {
1061 Tmp3 = SelectExpr(SetCC->getOperand(1));
1062 invTest = true;
1063 }
1064 else
1065 {
1066 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1067 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1068 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1069 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1070 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1071 .addReg(Tmp1).addReg(Tmp2);
1072 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001073
1074 switch (SetCC->getCondition()) {
1075 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001076 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1077 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1078 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1079 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1080 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1081 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001082 }
1083 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001084 return;
1085 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001086 abort(); //Should never be reached
1087 } else {
1088 //Giveup and do the stupid thing
1089 unsigned Tmp1 = SelectExpr(CC);
1090 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1091 return;
1092 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001093 abort(); //Should never be reached
1094}
1095
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001096unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001097{
1098 unsigned Tmp1, Tmp2, Tmp3;
1099 unsigned Opc = 0;
1100 SDNode *Node = N.Val;
1101 MVT::ValueType DestType = N.getValueType();
1102 unsigned opcode = N.getOpcode();
1103
1104 switch (opcode) {
1105 default:
1106 Node->dump();
1107 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001108
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001109 case ISD::UNDEF: {
1110 BuildMI(BB, Alpha::IDEF, 0, Result);
1111 return Result;
1112 }
1113
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001114 case ISD::FNEG:
1115 if(ISD::FABS == N.getOperand(0).getOpcode())
1116 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001117 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1118 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001119 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001120 Tmp1 = SelectExpr(N.getOperand(0));
1121 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001122 }
1123 return Result;
1124
1125 case ISD::FABS:
1126 Tmp1 = SelectExpr(N.getOperand(0));
1127 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1128 return Result;
1129
Andrew Lenharth9818c052005-02-05 13:19:12 +00001130 case ISD::SELECT:
1131 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001132 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1133 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1134 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1135
1136 SDOperand CC = N.getOperand(0);
1137 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1138
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001140 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1141 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001142
1143
Andrew Lenharth45859692005-03-03 21:47:53 +00001144 //for a cmp b: c = a - b;
1145 //a = b: c = 0
1146 //a < b: c < 0
1147 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001148
Andrew Lenharth45859692005-03-03 21:47:53 +00001149 bool invTest = false;
1150 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001151
Andrew Lenharth45859692005-03-03 21:47:53 +00001152 ConstantFPSDNode *CN;
1153 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1154 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1155 Tmp3 = SelectExpr(SetCC->getOperand(0));
1156 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1157 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1158 {
1159 Tmp3 = SelectExpr(SetCC->getOperand(1));
1160 invTest = true;
1161 }
1162 else
1163 {
1164 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1165 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1166 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1167 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1168 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1169 .addReg(Tmp1).addReg(Tmp2);
1170 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001171
Andrew Lenharth45859692005-03-03 21:47:53 +00001172 switch (SetCC->getCondition()) {
1173 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1174 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1175 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1176 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1177 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1178 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1179 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1180 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001181 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001182 return Result;
1183 }
1184 else
1185 {
1186 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001187 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1188 .addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001189// // Spill the cond to memory and reload it from there.
1190// unsigned Tmp4 = MakeReg(MVT::f64);
1191// MoveIntFP(Tmp1, Tmp4, true);
1192// //now ideally, we don't have to do anything to the flag...
1193// // Get the condition into the zero flag.
1194// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001195 return Result;
1196 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001197 }
1198
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001199 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001200 assert (DestType == MVT::f32 &&
1201 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001202 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
1204 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1205 return Result;
1206
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001207 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001208 assert (DestType == MVT::f64 &&
1209 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001210 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001211 Tmp1 = SelectExpr(N.getOperand(0));
1212 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1213 return Result;
1214
Andrew Lenharth2c594352005-01-29 15:42:07 +00001215 case ISD::CopyFromReg:
1216 {
1217 // Make sure we generate both values.
1218 if (Result != notIn)
1219 ExprMap[N.getValue(1)] = notIn; // Generate the token
1220 else
1221 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001222
Andrew Lenharth2c594352005-01-29 15:42:07 +00001223 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001224
Andrew Lenharth2c594352005-01-29 15:42:07 +00001225 Select(Chain);
1226 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1227 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1228 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1229 return Result;
1230 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001231
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001232 case ISD::LOAD:
1233 {
1234 // Make sure we generate both values.
1235 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001236 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001237 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001238 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001239
Andrew Lenharth29219162005-02-07 06:31:44 +00001240 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001241
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001242 SDOperand Chain = N.getOperand(0);
1243 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001244 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001245 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1246
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001247 if (EnableAlphaLSMark)
1248 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001249 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))
1250 ->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001251 int j = getFunctionOffset(BB->getParent()->getFunction());
1252 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001253 }
1254
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 if (Address.getOpcode() == ISD::GlobalAddress) {
1256 AlphaLowering.restoreGP(BB);
1257 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001258 has_sym = true;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001259 BuildMI(BB, Opc, 1, Result)
1260 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001261 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001262 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001263 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001264 Opc = GetRelVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001265 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001266 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001267 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1268 .addReg(Alpha::R29);
1269 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CP->getIndex())
1270 .addReg(Tmp1);
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001271 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001272 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001273 BuildMI(BB, Opc, 2, Result)
1274 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1275 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001276 } else {
1277 long offset;
1278 SelectAddr(Address, Tmp1, offset);
1279 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1280 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001281 return Result;
1282 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001283 case ISD::ConstantFP:
1284 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1285 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001286 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1287 .addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001288 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001289 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1290 .addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001291 } else {
1292 abort();
1293 }
1294 }
1295 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001296
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001297 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001298 case ISD::MUL:
1299 case ISD::ADD:
1300 case ISD::SUB:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001301 switch( opcode ) {
1302 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1303 break;
1304 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1305 break;
1306 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1307 break;
1308 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1309 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001310 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001311
1312 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001313 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001314 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1315 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1316 {
1317 Tmp2 = SelectExpr(N.getOperand(1));
1318 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1319 } else {
1320 Tmp1 = SelectExpr(N.getOperand(0));
1321 Tmp2 = SelectExpr(N.getOperand(1));
1322 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1323 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001324 return Result;
1325
Andrew Lenharth2c594352005-01-29 15:42:07 +00001326 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001327 {
1328 //include a conversion sequence for float loads to double
1329 if (Result != notIn)
1330 ExprMap[N.getValue(1)] = notIn; // Generate the token
1331 else
1332 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001333
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001334 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001335
1336 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001337 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001338 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001339
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001340 SDOperand Chain = N.getOperand(0);
1341 SDOperand Address = N.getOperand(1);
1342 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001343
Andrew Lenharthb72bcbb2005-06-27 16:40:26 +00001344 if (EnableAlphaLSMark)
1345 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001346 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))
1347 ->getValue());
Andrew Lenharthb72bcbb2005-06-27 16:40:26 +00001348 int j = getFunctionOffset(BB->getParent()->getFunction());
1349 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1350 }
1351
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001352 if (Address.getOpcode() == ISD::GlobalAddress) {
1353 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001354 has_sym = true;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001355 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1)
1356 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358 else if (ConstantPoolSDNode *CP =
1359 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001360 {
1361 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001362 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001363 Tmp2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001364 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(CP->getIndex())
1365 .addReg(Alpha::R29);
1366 BuildMI(BB, Alpha::LDSr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1367 .addReg(Tmp2);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001368 }
1369 else if(Address.getOpcode() == ISD::FrameIndex) {
1370 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001371 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1372 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1373 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001374 } else {
1375 long offset;
1376 SelectAddr(Address, Tmp2, offset);
1377 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1378 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001379 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001380 return Result;
1381 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001382
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001383 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001384 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001385 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001386 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001387 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001388 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001389 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001390 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1391 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001392 return Result;
1393 }
1394 }
1395 assert(0 && "should not get here");
1396 return 0;
1397}
1398
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001399unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001400 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001401 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001402 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001403 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001404
1405 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001406 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001407
1408 unsigned &Reg = ExprMap[N];
1409 if (Reg) return Reg;
1410
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001411 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001412 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001413 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001414 else {
1415 // If this is a call instruction, make sure to prepare ALL of the result
1416 // values as well as the chain.
1417 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001418 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001419 else {
1420 Result = MakeReg(Node->getValueType(0));
1421 ExprMap[N.getValue(0)] = Result;
1422 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1423 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001424 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001425 }
1426 }
1427
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001428 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1429 (
1430 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1431 opcode == ISD::EXTLOAD) &&
1432 (N.getValue(0).getValueType() == MVT::f32 ||
1433 N.getValue(0).getValueType() == MVT::f64)
1434 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001435 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001436 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001437 return SelectExprFP(N, Result);
1438
1439 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001440 default:
1441 Node->dump();
1442 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001443
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001444 case ISD::CTPOP:
1445 case ISD::CTTZ:
1446 case ISD::CTLZ:
1447 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1448 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1449 Tmp1 = SelectExpr(N.getOperand(0));
1450 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1451 return Result;
1452
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001453 case ISD::MULHU:
1454 Tmp1 = SelectExpr(N.getOperand(0));
1455 Tmp2 = SelectExpr(N.getOperand(1));
1456 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001457 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001458 case ISD::MULHS:
1459 {
1460 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1461 Tmp1 = SelectExpr(N.getOperand(0));
1462 Tmp2 = SelectExpr(N.getOperand(1));
1463 Tmp3 = MakeReg(MVT::i64);
1464 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1465 unsigned V1 = MakeReg(MVT::i64);
1466 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001467 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1468 .addReg(Tmp1);
1469 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1470 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001471 unsigned IRes = MakeReg(MVT::i64);
1472 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1473 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1474 return Result;
1475 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001476 case ISD::UNDEF: {
1477 BuildMI(BB, Alpha::IDEF, 0, Result);
1478 return Result;
1479 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001480
Andrew Lenharth032f2352005-02-22 21:59:48 +00001481 case ISD::DYNAMIC_STACKALLOC:
1482 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001483 if (Result != notIn)
1484 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001485 else
1486 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1487
1488 // FIXME: We are currently ignoring the requested alignment for handling
1489 // greater than the stack alignment. This will need to be revisited at some
1490 // point. Align = N.getOperand(2);
1491
1492 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1493 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1494 std::cerr << "Cannot allocate stack object with greater alignment than"
1495 << " the stack alignment yet!";
1496 abort();
1497 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001498
Andrew Lenharth032f2352005-02-22 21:59:48 +00001499 Select(N.getOperand(0));
1500 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1501 {
1502 if (CN->getValue() < 32000)
1503 {
1504 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1505 .addImm(-CN->getValue()).addReg(Alpha::R30);
1506 } else {
1507 Tmp1 = SelectExpr(N.getOperand(1));
1508 // Subtract size from stack pointer, thereby allocating some space.
1509 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1510 }
1511 } else {
1512 Tmp1 = SelectExpr(N.getOperand(1));
1513 // Subtract size from stack pointer, thereby allocating some space.
1514 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1515 }
1516
1517 // Put a pointer to the space into the result register, by copying the stack
1518 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001519 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001520 return Result;
1521
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001522 case ISD::ConstantPool:
1523 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1524 AlphaLowering.restoreGP(BB);
1525 Tmp2 = MakeReg(MVT::i64);
1526 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1527 .addReg(Alpha::R29);
1528 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1529 .addReg(Tmp2);
1530 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001531
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001532 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001533 BuildMI(BB, Alpha::LDA, 2, Result)
1534 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1535 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001536 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001537
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001538 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001539 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001540 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001541 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001542 {
1543 // Make sure we generate both values.
1544 if (Result != notIn)
1545 ExprMap[N.getValue(1)] = notIn; // Generate the token
1546 else
1547 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001548
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001549 SDOperand Chain = N.getOperand(0);
1550 SDOperand Address = N.getOperand(1);
1551 Select(Chain);
1552
Misha Brukman4633f1c2005-04-21 23:13:11 +00001553 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001554 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001555 if (opcode == ISD::LOAD)
1556 Opc = Alpha::LDQ;
1557 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001558 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1559 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001560 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001561 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001562 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001563 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001564 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001565 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001566 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001567 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001568
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001569 if (EnableAlphaLSMark)
1570 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001571 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))
1572 ->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001573 int j = getFunctionOffset(BB->getParent()->getFunction());
1574 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1575 }
1576
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001577 if (Address.getOpcode() == ISD::GlobalAddress) {
1578 AlphaLowering.restoreGP(BB);
1579 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001580 has_sym = true;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001581 BuildMI(BB, Opc, 1, Result)
1582 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001583 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001584 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1585 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001586 Opc = GetRelVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001587 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001588 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001589 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1590 .addReg(Alpha::R29);
1591 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CP->getIndex())
1592 .addReg(Tmp1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001593 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001595 BuildMI(BB, Opc, 2, Result)
1596 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1597 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 } else {
1599 long offset;
1600 SelectAddr(Address, Tmp1, offset);
1601 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1602 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001603 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001604 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001605
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001606 case ISD::GlobalAddress:
1607 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001608 has_sym = true;
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001609 BuildMI(BB, Alpha::LDQrl, 2, Result)
1610 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1611 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001612 return Result;
1613
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001614 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001615 case ISD::CALL:
1616 {
1617 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001618
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001619 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001620 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001621
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001622 //grab the arguments
1623 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001624 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001625 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627
Andrew Lenharth684f2292005-01-30 00:35:27 +00001628 //in reg args
1629 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001631 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001632 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001633 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001634 Alpha::F19, Alpha::F20, Alpha::F21};
1635 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001636 default:
1637 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001638 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001639 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001640 N.getOperand(i+2).getValueType() << "\n";
1641 assert(0 && "Unknown value type for call");
1642 case MVT::i1:
1643 case MVT::i8:
1644 case MVT::i16:
1645 case MVT::i32:
1646 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001647 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1648 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001649 break;
1650 case MVT::f32:
1651 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001652 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1653 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001654 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001655 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001656 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001657 //in mem args
1658 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001659 {
1660 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001661 default:
1662 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001663 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001664 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001665 N.getOperand(i+2).getValueType() << "\n";
1666 assert(0 && "Unknown value type for call");
1667 case MVT::i1:
1668 case MVT::i8:
1669 case MVT::i16:
1670 case MVT::i32:
1671 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001672 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1673 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001674 break;
1675 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001676 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1677 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001678 break;
1679 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001680 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1681 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001682 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001683 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001684 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001685 //build the right kind of call
1686 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001687 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001688 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001689 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001690 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001691 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001692 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001693 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1694 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001695 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001696 AlphaLowering.restoreGP(BB);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001697 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1698 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001699 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001700 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001701 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001702 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001703 {
1704 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001705 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001706 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001707 } else {
1708 //no need to restore GP as we are doing an indirect call
1709 Tmp1 = SelectExpr(N.getOperand(1));
1710 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1711 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1712 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001713
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001714 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001715
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001716 switch (Node->getValueType(0)) {
1717 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001718 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001719 case MVT::i1:
1720 case MVT::i8:
1721 case MVT::i16:
1722 case MVT::i32:
1723 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001724 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1725 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001726 case MVT::f32:
1727 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001728 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1729 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001730 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001731 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001732 }
1733
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001734 case ISD::SIGN_EXTEND_INREG:
1735 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001736 //do SDIV opt for all levels of ints if not dividing by a constant
1737 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1738 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001739 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001740 unsigned Tmp4 = MakeReg(MVT::f64);
1741 unsigned Tmp5 = MakeReg(MVT::f64);
1742 unsigned Tmp6 = MakeReg(MVT::f64);
1743 unsigned Tmp7 = MakeReg(MVT::f64);
1744 unsigned Tmp8 = MakeReg(MVT::f64);
1745 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001746
1747 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1748 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1749 MoveInt2FP(Tmp1, Tmp4, true);
1750 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001751 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1752 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1753 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1754 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001755 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001756 return Result;
1757 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001758
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001759 //Alpha has instructions for a bunch of signed 32 bit stuff
1760 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001761 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001762 switch (N.getOperand(0).getOpcode()) {
1763 case ISD::ADD:
1764 case ISD::SUB:
1765 case ISD::MUL:
1766 {
1767 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1768 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1769 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001770 ConstantSDNode* CSD = NULL;
1771 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1772 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1773 (CSD->getValue() == 2 || CSD->getValue() == 3))
1774 {
1775 bool use4 = CSD->getValue() == 2;
1776 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1777 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1778 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1779 2,Result).addReg(Tmp1).addReg(Tmp2);
1780 }
1781 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1782 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1783 (CSD->getValue() == 2 || CSD->getValue() == 3))
1784 {
1785 bool use4 = CSD->getValue() == 2;
1786 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1787 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1788 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1789 }
1790 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001791 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1792 { //Normal imm add/sub
1793 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001794 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001795 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001797 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001798 else
1799 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001800 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001801 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001802 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001803 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1804 }
1805 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001806 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 default: break; //Fall Though;
1808 }
1809 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001810 Tmp1 = SelectExpr(N.getOperand(0));
1811 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001812 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001813 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001814 {
1815 default:
1816 Node->dump();
1817 assert(0 && "Sign Extend InReg not there yet");
1818 break;
1819 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001820 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001821 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001822 break;
1823 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001824 case MVT::i16:
1825 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1826 break;
1827 case MVT::i8:
1828 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1829 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001830 case MVT::i1:
1831 Tmp2 = MakeReg(MVT::i64);
1832 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001833 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001834 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001835 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001836 return Result;
1837 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001838
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001839 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001840 {
1841 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1842 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001843 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001844 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001845
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001846 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001847 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001848 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001849 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001850
1851 switch (SetCC->getCondition()) {
1852 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001853 case ISD::SETEQ:
1854 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001855 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001856 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001857 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001858 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1859 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1860 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001861 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001862 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1863 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001864 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001865 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1866 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001867 case ISD::SETNE: {//Handle this one special
1868 //std::cerr << "Alpha does not have a setne.\n";
1869 //abort();
1870 Tmp1 = SelectExpr(N.getOperand(0));
1871 Tmp2 = SelectExpr(N.getOperand(1));
1872 Tmp3 = MakeReg(MVT::i64);
1873 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001874 //Remeber we have the Inv for this CC
1875 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001876 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001877 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001878 return Result;
1879 }
1880 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001881 if (dir == 1) {
1882 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001883 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001884 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1885 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1886 } else {
1887 Tmp2 = SelectExpr(N.getOperand(1));
1888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001890 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001891 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001892 Tmp2 = SelectExpr(N.getOperand(0));
1893 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001894 }
1895 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001896 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001897 Tmp1 = MakeReg(MVT::f64);
1898 bool inv = SelectFPSetCC(N, Tmp1);
1899
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001900 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001901 Tmp2 = MakeReg(MVT::i64);
1902 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001903 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001904 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001905 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001906 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001907 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001908 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001909
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001910 case ISD::CopyFromReg:
1911 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001912 ++count_ins;
1913
Andrew Lenharth40831c52005-01-28 06:57:18 +00001914 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001915 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001916 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001917 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001918 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001919
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001920 SDOperand Chain = N.getOperand(0);
1921
1922 Select(Chain);
1923 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1924 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1925 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1926 return Result;
1927 }
1928
Misha Brukman4633f1c2005-04-21 23:13:11 +00001929 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001930 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001931 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001932 //Match Not
1933 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001934 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001935 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001936 Tmp1 = SelectExpr(N.getOperand(0));
1937 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1938 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001939 }
1940 //Fall through
1941 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001942 //handle zap
1943 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1944 {
1945 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1946 unsigned int build = 0;
1947 for(int i = 0; i < 8; ++i)
1948 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001949 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001950 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001951 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001952 { build = 0; break; }
1953 k >>= 8;
1954 }
1955 if (build)
1956 {
1957 Tmp1 = SelectExpr(N.getOperand(0));
1958 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1959 return Result;
1960 }
1961 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001962 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001963 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001964 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001965 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001966 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
1967 == -1) {
1968 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001969 case ISD::AND: Opc = Alpha::BIC; break;
1970 case ISD::OR: Opc = Alpha::ORNOT; break;
1971 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001972 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001973 Tmp1 = SelectExpr(N.getOperand(1));
1974 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1975 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1976 return Result;
1977 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001978 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001979 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001980 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001981 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1982 == -1) {
1983 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001984 case ISD::AND: Opc = Alpha::BIC; break;
1985 case ISD::OR: Opc = Alpha::ORNOT; break;
1986 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001987 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001988 Tmp1 = SelectExpr(N.getOperand(0));
1989 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1990 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1991 return Result;
1992 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001993 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001994 case ISD::SHL:
1995 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001996 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001997 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001998 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1999 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00002000 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002001 {
2002 switch(opcode) {
2003 case ISD::AND: Opc = Alpha::ANDi; break;
2004 case ISD::OR: Opc = Alpha::BISi; break;
2005 case ISD::XOR: Opc = Alpha::XORi; break;
2006 case ISD::SHL: Opc = Alpha::SLi; break;
2007 case ISD::SRL: Opc = Alpha::SRLi; break;
2008 case ISD::SRA: Opc = Alpha::SRAi; break;
2009 case ISD::MUL: Opc = Alpha::MULQi; break;
2010 };
2011 Tmp1 = SelectExpr(N.getOperand(0));
2012 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2013 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
2014 } else {
2015 switch(opcode) {
2016 case ISD::AND: Opc = Alpha::AND; break;
2017 case ISD::OR: Opc = Alpha::BIS; break;
2018 case ISD::XOR: Opc = Alpha::XOR; break;
2019 case ISD::SHL: Opc = Alpha::SL; break;
2020 case ISD::SRL: Opc = Alpha::SRL; break;
2021 case ISD::SRA: Opc = Alpha::SRA; break;
2022 case ISD::MUL: Opc = Alpha::MULQ; break;
2023 };
2024 Tmp1 = SelectExpr(N.getOperand(0));
2025 Tmp2 = SelectExpr(N.getOperand(1));
2026 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2027 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00002028 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002029
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002030 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002031 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00002032 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00002033 bool isAdd = opcode == ISD::ADD;
2034
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002035 //first check for Scaled Adds and Subs!
2036 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002037 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002038 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002039 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
2040 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002041 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002042 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002043 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002044 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2045 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2046 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002047 else {
2048 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002049 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2050 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002051 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002052 }
2053 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002054 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002055 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2056 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002057 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002058 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002059 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002060 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2061 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2062 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002063 else {
2064 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002065 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002066 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002067 }
2068 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002069 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2070 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002071 { //Normal imm add/sub
2072 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2073 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002074 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002075 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002076 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002077 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2078 CSD->getSignExtended() <= 32767 &&
2079 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002080 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002081 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002082 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002083 if (!isAdd)
2084 Tmp2 = -Tmp2;
2085 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002086 }
2087 //give up and do the operation
2088 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002089 //Normal add/sub
2090 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2091 Tmp1 = SelectExpr(N.getOperand(0));
2092 Tmp2 = SelectExpr(N.getOperand(1));
2093 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2094 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002095 return Result;
2096 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002097
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002098 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002099 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002100 ConstantSDNode* CSD;
2101 //check if we can convert into a shift!
2102 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2103 (int64_t)CSD->getSignExtended() != 0 &&
2104 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2105 {
2106 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2107 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002108 if (k == 1)
2109 Tmp2 = Tmp1;
2110 else
2111 {
2112 Tmp2 = MakeReg(MVT::i64);
2113 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2114 }
2115 Tmp3 = MakeReg(MVT::i64);
2116 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2117 unsigned Tmp4 = MakeReg(MVT::i64);
2118 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2119 if ((int64_t)CSD->getSignExtended() > 0)
2120 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2121 else
2122 {
2123 unsigned Tmp5 = MakeReg(MVT::i64);
2124 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2125 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2126 }
2127 return Result;
2128 }
2129 }
2130 //Else fall through
2131
2132 case ISD::UDIV:
2133 {
2134 ConstantSDNode* CSD;
2135 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2136 ((int64_t)CSD->getSignExtended() >= 2 ||
2137 (int64_t)CSD->getSignExtended() <= -2))
2138 {
2139 // If this is a divide by constant, we can emit code using some magic
2140 // constants to implement it as a multiply instead.
2141 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002142 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002143 return SelectExpr(BuildSDIVSequence(N));
2144 else
2145 return SelectExpr(BuildUDIVSequence(N));
2146 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002147 }
2148 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002149 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002150 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002151 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002152 // the ops are expanded into special library calls with
2153 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002154 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002155 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002156 case ISD::UREM: Opc = Alpha::REMQU; break;
2157 case ISD::SREM: Opc = Alpha::REMQ; break;
2158 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2159 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002160 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002161 Tmp1 = SelectExpr(N.getOperand(0));
2162 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002163 //set up regs explicitly (helps Reg alloc)
2164 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002165 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002166 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002167 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002168 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002169 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002170
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002171 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002172 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002173 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002174 assert (DestType == MVT::i64 && "only quads can be loaded to");
2175 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002176 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002177 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002178 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002179 {
2180 Tmp2 = MakeReg(MVT::f64);
2181 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2182 Tmp1 = Tmp2;
2183 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002184 Tmp2 = MakeReg(MVT::f64);
2185 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002186 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002187
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002188 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002189 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002190
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002191 case ISD::SELECT:
2192 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002193 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
2194 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002195 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002196 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2197 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002198 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002199 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002200
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002201 SDOperand CC = N.getOperand(0);
2202 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2203
Misha Brukman4633f1c2005-04-21 23:13:11 +00002204 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002205 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2206 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002207 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002208 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2209 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002210 bool inv = SelectFPSetCC(CC, Tmp1);
2211 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2212 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2213 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002214 }
2215 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002216 //Int SetCC -> Select
2217 //Dropping the CC is only useful if we are comparing to 0
2218 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002219 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002220 {
2221 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002222 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002223 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002224
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002225 //Fix up CC
2226 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002227 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002228 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002229
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002230 //Choose the CMOV
2231 switch (cCode) {
2232 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002233 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2234 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2235 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2236 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2237 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2238 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2239 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2240 //Technically you could have this CC
2241 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2242 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2243 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002244 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002245 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002246
Andrew Lenharth694c2982005-06-26 23:01:11 +00002247 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002248 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2249 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002250 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002251 .addReg(Tmp1);
2252 } else {
2253 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2254 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2255 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2256 }
2257 return Result;
2258 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002259 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002260 }
2261 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002262 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2263 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002264 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2265 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002266
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002267 return Result;
2268 }
2269
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002270 case ISD::Constant:
2271 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002272 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002273 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002274 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002275 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002276 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2277 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2278 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002279 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2280 .addReg(Alpha::R31);
Misha Brukman7847fca2005-04-22 17:54:37 +00002281 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002282 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002283 else {
2284 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002285 ConstantUInt *C =
2286 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002287 unsigned CPI = CP->getConstantPoolIndex(C);
2288 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002289 has_sym = true;
2290 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002291 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2292 .addReg(Alpha::R29);
2293 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2294 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002295 }
2296 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002297 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002298 }
2299
2300 return 0;
2301}
2302
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002303void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002304 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002305 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002306
Nate Begeman85fdeb22005-03-24 04:39:54 +00002307 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002308 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002309
2310 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002311
Andrew Lenharth760270d2005-02-07 23:02:23 +00002312 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002313
2314 default:
2315 Node->dump(); std::cerr << "\n";
2316 assert(0 && "Node not handled yet!");
2317
2318 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002319 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002320 return;
2321 }
2322
2323 case ISD::BR: {
2324 MachineBasicBlock *Dest =
2325 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2326
2327 Select(N.getOperand(0));
2328 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2329 return;
2330 }
2331
2332 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002333 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334 Select(N.getOperand(0));
2335 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2336 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002337
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002338 case ISD::EntryToken: return; // Noop
2339
2340 case ISD::TokenFactor:
2341 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2342 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002343
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 //N.Val->dump(); std::cerr << "\n";
2345 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002346
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002347 return;
2348
2349 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002350 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002351 Select(N.getOperand(0));
2352 Tmp1 = SelectExpr(N.getOperand(1));
2353 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002354
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002355 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002356 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002357 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002358 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2359 else
2360 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002361 }
2362 return;
2363
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002364 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002365 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002366 switch (N.getNumOperands()) {
2367 default:
2368 std::cerr << N.getNumOperands() << "\n";
2369 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2370 std::cerr << N.getOperand(i).getValueType() << "\n";
2371 Node->dump();
2372 assert(0 && "Unknown return instruction!");
2373 case 2:
2374 Select(N.getOperand(0));
2375 Tmp1 = SelectExpr(N.getOperand(1));
2376 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002377 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002378 assert(0 && "All other types should have been promoted!!");
2379 case MVT::f64:
2380 case MVT::f32:
2381 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2382 break;
2383 case MVT::i32:
2384 case MVT::i64:
2385 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2386 break;
2387 }
2388 break;
2389 case 1:
2390 Select(N.getOperand(0));
2391 break;
2392 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002393 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002394 AlphaLowering.restoreRA(BB);
2395 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002396 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002397
Misha Brukman4633f1c2005-04-21 23:13:11 +00002398 case ISD::TRUNCSTORE:
2399 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002400 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002401 SDOperand Chain = N.getOperand(0);
2402 SDOperand Value = N.getOperand(1);
2403 SDOperand Address = N.getOperand(2);
2404 Select(Chain);
2405
2406 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002407
2408 if (opcode == ISD::STORE) {
2409 switch(Value.getValueType()) {
2410 default: assert(0 && "unknown Type in store");
2411 case MVT::i64: Opc = Alpha::STQ; break;
2412 case MVT::f64: Opc = Alpha::STT; break;
2413 case MVT::f32: Opc = Alpha::STS; break;
2414 }
2415 } else { //ISD::TRUNCSTORE
2416 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2417 default: assert(0 && "unknown Type in store");
2418 case MVT::i1: //FIXME: DAG does not promote this load
2419 case MVT::i8: Opc = Alpha::STB; break;
2420 case MVT::i16: Opc = Alpha::STW; break;
2421 case MVT::i32: Opc = Alpha::STL; break;
2422 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002423 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002424
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002425 if (EnableAlphaLSMark)
2426 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002427 int i =
2428 getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002429 int j = getFunctionOffset(BB->getParent()->getFunction());
2430 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2431 }
2432
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002433 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002434 {
2435 AlphaLowering.restoreGP(BB);
2436 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002437 has_sym = true;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002438 BuildMI(BB, Opc, 2).addReg(Tmp1)
2439 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002440 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002441 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002442 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002443 BuildMI(BB, Opc, 3).addReg(Tmp1)
2444 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2445 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002446 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002447 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002448 {
2449 long offset;
2450 SelectAddr(Address, Tmp2, offset);
2451 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2452 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002453 return;
2454 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002455
2456 case ISD::EXTLOAD:
2457 case ISD::SEXTLOAD:
2458 case ISD::ZEXTLOAD:
2459 case ISD::LOAD:
2460 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002461 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002462 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002463 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002464 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002465 SelectExpr(N);
2466 return;
2467
Chris Lattner16cd04d2005-05-12 23:24:06 +00002468 case ISD::CALLSEQ_START:
2469 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002470 Select(N.getOperand(0));
2471 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002472
Chris Lattner16cd04d2005-05-12 23:24:06 +00002473 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002474 Alpha::ADJUSTSTACKUP;
2475 BuildMI(BB, Opc, 1).addImm(Tmp1);
2476 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002477
2478 case ISD::PCMARKER:
2479 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002480 BuildMI(BB, Alpha::PCLABEL, 2)
2481 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002482 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002483 }
2484 assert(0 && "Should not be reached!");
2485}
2486
2487
2488/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2489/// into a machine code representation using pattern matching and a machine
2490/// description file.
2491///
2492FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002493 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002494}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002495