blob: ed285a3d9ff5507a6cfd375ae85fb18d19054c1b [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 Lenharthc7989ce2005-06-29 00:31:08 +000095 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
Andrew Lenharth6968bff2005-06-27 23:24:11 +000096
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenharthec151362005-06-26 22:23:06 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 const Type *ArgTy, SelectionDAG &DAG);
165
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
168 SelectionDAG &DAG);
169
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
172 SelectionDAG &DAG);
173
174 void restoreGP(MachineBasicBlock* BB)
175 {
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
177 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000178 void restoreRA(MachineBasicBlock* BB)
179 {
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
181 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000182 unsigned getRA()
183 {
184 return RA;
185 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000186
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187 };
188}
189
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000190/// LowerOperation - Provide custom lowering hooks for some operations.
191///
192SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
193 MachineFunction &MF = DAG.getMachineFunction();
194 switch (Op.getOpcode()) {
195 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000196#if 0
197 case ISD::SINT_TO_FP:
198 {
199 assert (Op.getOperand(0).getValueType() == MVT::i64
200 && "only quads can be loaded from");
201 SDOperand SRC;
202 if (EnableAlphaFTOI)
203 {
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(Op.getOperand(0));
208 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
209 } else {
210 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
211 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
213 DAG.getEntryNode(), Op.getOperand(0),
214 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000215 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
216 DAG.getSrcValue(NULL));
217 }
218 std::vector<MVT::ValueType> RTs;
219 RTs.push_back(Op.getValueType());
220 std::vector<SDOperand> Ops;
221 Ops.push_back(SRC);
222 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
223 }
224#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000225 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000226 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000227}
228
229
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000230/// AddLiveIn - This helper function adds the specified physical register to the
231/// MachineFunction as a live in value. It also creates a corresponding virtual
232/// register for it.
233static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
234 TargetRegisterClass *RC) {
235 assert(RC->contains(PReg) && "Not the correct regclass!");
236 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
237 MF.addLiveIn(PReg, VReg);
238 return VReg;
239}
240
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
242
243//For now, just use variable size stack frame format
244
245//In a standard call, the first six items are passed in registers $16
246//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
247//of argument-to-register correspondence.) The remaining items are
248//collected in a memory argument list that is a naturally aligned
249//array of quadwords. In a standard call, this list, if present, must
250//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000251//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000253// //#define FP $15
254// //#define RA $26
255// //#define PV $27
256// //#define GP $29
257// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261{
262 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000263
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000264 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000265 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 MachineBasicBlock& BB = MF.front();
268
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000270 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000271 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000272 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000273 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000274
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000275 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000276 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000277
Chris Lattnere4d5c442005-03-15 04:54:21 +0000278 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000279 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000280 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000282 unsigned Vreg;
283 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000284 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000285 default:
286 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000287 abort();
288 case MVT::f64:
289 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000290 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
291 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000292 break;
293 case MVT::i1:
294 case MVT::i8:
295 case MVT::i16:
296 case MVT::i32:
297 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000298 args_int[count] = AddLiveIn(MF, args_int[count],
299 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000300 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000301 if (VT != MVT::i64)
302 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000303 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000304 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000305 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000306 } else { //more args
307 // Create the frame index object for this incoming parameter...
308 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000309
310 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 //from this parameter
312 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000313 argt = DAG.getLoad(getValueType(I->getType()),
314 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000315 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000316 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000317 ArgValues.push_back(argt);
318 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000319
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000320 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000321 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000322 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000323 std::vector<SDOperand> LS;
324 for (int i = 0; i < 6; ++i) {
325 if (args_int[i] < 1024)
326 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
327 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000328 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000329 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000330 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000331 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
332 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000333
334 if (args_float[i] < 1024)
335 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
336 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000337 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
338 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000339 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
340 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000341 }
342
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000343 //Set up a token factor with all the stack traffic
344 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
345 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000346
347 // Finally, inform the code generator which regs we return values in.
348 switch (getValueType(F.getReturnType())) {
349 default: assert(0 && "Unknown type!");
350 case MVT::isVoid: break;
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 case MVT::i32:
355 case MVT::i64:
356 MF.addLiveOut(Alpha::R0);
357 break;
358 case MVT::f32:
359 case MVT::f64:
360 MF.addLiveOut(Alpha::F0);
361 break;
362 }
363
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000364 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000365 return ArgValues;
366}
367
368std::pair<SDOperand, SDOperand>
369AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000370 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000371 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000372 SDOperand Callee, ArgListTy &Args,
373 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000374 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000375 if (Args.size() > 6)
376 NumBytes = (Args.size() - 6) * 8;
377
Chris Lattner16cd04d2005-05-12 23:24:06 +0000378 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000379 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000380 std::vector<SDOperand> args_to_use;
381 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000382 {
383 switch (getValueType(Args[i].second)) {
384 default: assert(0 && "Unexpected ValueType for argument!");
385 case MVT::i1:
386 case MVT::i8:
387 case MVT::i16:
388 case MVT::i32:
389 // Promote the integer to 64 bits. If the input type is signed use a
390 // sign extend, otherwise use a zero extend.
391 if (Args[i].second->isSigned())
392 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
393 else
394 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
395 break;
396 case MVT::i64:
397 case MVT::f64:
398 case MVT::f32:
399 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000400 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 args_to_use.push_back(Args[i].first);
402 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000403
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000404 std::vector<MVT::ValueType> RetVals;
405 MVT::ValueType RetTyVT = getValueType(RetTy);
406 if (RetTyVT != MVT::isVoid)
407 RetVals.push_back(RetTyVT);
408 RetVals.push_back(MVT::Other);
409
Misha Brukman4633f1c2005-04-21 23:13:11 +0000410 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000412 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000413 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000414 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000416}
417
418std::pair<SDOperand, SDOperand>
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000419AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG,
420 SDOperand Dest) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000421 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000422 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000423 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest,
424 DAG.getSrcValue(NULL));
425 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest,
426 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000427 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000428 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000429 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000430 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000431}
432
433std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000434LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000435 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000436 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
437 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
438 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000439 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
440 Tmp, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000441 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000442 if (ArgTy->isFloatingPoint())
443 {
444 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
445 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
446 DAG.getConstant(8*6, MVT::i64));
447 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
448 Offset, DAG.getConstant(8*6, MVT::i64));
449 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
450 }
451
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000452 SDOperand Result;
453 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000454 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000455 DAG.getSrcValue(NULL), MVT::i32);
456 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000457 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000458 DAG.getSrcValue(NULL), MVT::i32);
459 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000460 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000461 DAG.getSrcValue(NULL));
462
Andrew Lenharth558bc882005-06-18 18:34:52 +0000463 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
464 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000465 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
466 Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000467 Tmp, DAG.getSrcValue(NULL), MVT::i32);
468 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
469
Andrew Lenharth558bc882005-06-18 18:34:52 +0000470 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000471}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000472
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000473std::pair<SDOperand,SDOperand> AlphaTargetLowering::
474LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
475 SelectionDAG &DAG) {
476 //Default to returning the input list
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000477 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src,
478 DAG.getSrcValue(NULL));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000479 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
480 Val, Dest, DAG.getSrcValue(NULL));
481 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
482 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000483 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000484 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000485 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
486 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000487 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000488 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000489 return std::make_pair(Result, Result);
490}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000491
492std::pair<SDOperand, SDOperand> AlphaTargetLowering::
493LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
494 SelectionDAG &DAG) {
495 abort();
496}
497
498
499
500
501
502namespace {
503
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000504//===--------------------------------------------------------------------===//
505/// ISel - Alpha specific code to select Alpha machine instructions for
506/// SelectionDAG operations.
507//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000508class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000509
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000510 /// AlphaLowering - This object fully describes how to lower LLVM code to an
511 /// Alpha-specific SelectionDAG.
512 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000514 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
515 // for sdiv and udiv until it is put into the future
516 // dag combiner.
517
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000518 /// ExprMap - As shared expressions are codegen'd, we keep track of which
519 /// vreg the value is produced in, so we only emit one copy of each compiled
520 /// tree.
521 static const unsigned notIn = (unsigned)(-1);
522 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000523
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000524 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
525 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000526
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000527 int count_ins;
528 int count_outs;
529 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000530 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000531
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000532public:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000533 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
534 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000535 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000536
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000537 /// InstructionSelectBasicBlock - This callback is invoked by
538 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
539 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000540 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000541 count_ins = 0;
542 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000543 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000544 has_sym = false;
545
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000546 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000547 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000548 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000549 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000550
551 if(has_sym)
552 ++count_ins;
553 if(EnableAlphaCount)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000554 std::cerr << "COUNT: "
555 << BB->getParent()->getFunction ()->getName() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000556 << BB->getNumber() << " "
557 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000558 << count_ins << " "
559 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000560
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000561 // Clear state used for selection.
562 ExprMap.clear();
563 CCInvMap.clear();
564 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000565
566 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000567
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000568 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000569 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 Lenharth06ef8842005-06-29 18:54:02 +0000607static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000608{
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000609 if (v == NULL) {
610 type = 0;
611 fun = 0;
612 offset = 0;
613 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
614 type = 1;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000615 fun = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000616 const Module* M = GV->getParent();
617 int i = 0;
618 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
619 ++i;
620 offset = i;
621 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
622 type = 2;
623 const Function* F = Arg->getParent();
624 const Module* M = F->getParent();
625 int i = 0;
626 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
627 ++i;
628 fun = i;
629 i = 0;
630 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
631 ++i;
632 offset = i;
633 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
634 type = 3;
635 const BasicBlock* bb = I->getParent();
636 const Function* F = bb->getParent();
637 const Module* M = F->getParent();
638 int i = 0;
639 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
640 ++i;
641 fun = i;
642 i = 0;
643 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
644 i += ii->size();
645 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
646 ++i;
647 offset = i;
648 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000649 //type = 4: register spilling
650 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000651}
652
653static int getUID()
654{
655 static int id = 0;
656 return ++id;
657}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000658
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000659//Factorize a number using the list of constants
660static bool factorize(int v[], int res[], int size, uint64_t c)
661{
662 bool cont = true;
663 while (c != 1 && cont)
664 {
665 cont = false;
666 for(int i = 0; i < size; ++i)
667 {
668 if (c % v[i] == 0)
669 {
670 c /= v[i];
671 ++res[i];
672 cont=true;
673 }
674 }
675 }
676 return c == 1;
677}
678
679
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000680//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000681// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000682// a multiply.
683struct ms {
684 int64_t m; // magic number
685 int64_t s; // shift amount
686};
687
688struct mu {
689 uint64_t m; // magic number
690 int64_t a; // add indicator
691 int64_t s; // shift amount
692};
693
694/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000695/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000696/// or -1.
697static struct ms magic(int64_t d) {
698 int64_t p;
699 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
700 const uint64_t two63 = 9223372036854775808ULL; // 2^63
701 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000702
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000703 ad = abs(d);
704 t = two63 + ((uint64_t)d >> 63);
705 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000706 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000707 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
708 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
709 q2 = two63/ad; // initialize q2 = 2p/abs(d)
710 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
711 do {
712 p = p + 1;
713 q1 = 2*q1; // update q1 = 2p/abs(nc)
714 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
715 if (r1 >= anc) { // must be unsigned comparison
716 q1 = q1 + 1;
717 r1 = r1 - anc;
718 }
719 q2 = 2*q2; // update q2 = 2p/abs(d)
720 r2 = 2*r2; // update r2 = rem(2p/abs(d))
721 if (r2 >= ad) { // must be unsigned comparison
722 q2 = q2 + 1;
723 r2 = r2 - ad;
724 }
725 delta = ad - r2;
726 } while (q1 < delta || (q1 == delta && r1 == 0));
727
728 mag.m = q2 + 1;
729 if (d < 0) mag.m = -mag.m; // resulting magic number
730 mag.s = p - 64; // resulting shift
731 return mag;
732}
733
734/// magicu - calculate the magic numbers required to codegen an integer udiv as
735/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
736static struct mu magicu(uint64_t d)
737{
738 int64_t p;
739 uint64_t nc, delta, q1, r1, q2, r2;
740 struct mu magu;
741 magu.a = 0; // initialize "add" indicator
742 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000743 p = 63; // initialize p
744 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
745 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
746 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
747 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000748 do {
749 p = p + 1;
750 if (r1 >= nc - r1 ) {
751 q1 = 2*q1 + 1; // update q1
752 r1 = 2*r1 - nc; // update r1
753 }
754 else {
755 q1 = 2*q1; // update q1
756 r1 = 2*r1; // update r1
757 }
758 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000759 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000760 q2 = 2*q2 + 1; // update q2
761 r2 = 2*r2 + 1 - d; // update r2
762 }
763 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000764 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000765 q2 = 2*q2; // update q2
766 r2 = 2*r2 + 1; // update r2
767 }
768 delta = d - 1 - r2;
769 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
770 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000771 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000772 return magu;
773}
774
775/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
776/// return a DAG expression to select that will generate the same value by
777/// multiplying by a magic number. See:
778/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000779SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000780 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000781 ms magics = magic(d);
782 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000783 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000784 ISelDAG->getConstant(magics.m, MVT::i64));
785 // If d > 0 and m < 0, add the numerator
786 if (d > 0 && magics.m < 0)
787 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
788 // If d < 0 and m > 0, subtract the numerator.
789 if (d < 0 && magics.m > 0)
790 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
791 // Shift right algebraic if shift value is nonzero
792 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000793 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000794 ISelDAG->getConstant(magics.s, MVT::i64));
795 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000796 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000797 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
798 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
799}
800
801/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
802/// return a DAG expression to select that will generate the same value by
803/// multiplying by a magic number. See:
804/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000805SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000806 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000807 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
808 mu magics = magicu(d);
809 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000810 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000811 ISelDAG->getConstant(magics.m, MVT::i64));
812 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000813 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000814 ISelDAG->getConstant(magics.s, MVT::i64));
815 } else {
816 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000817 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000818 ISelDAG->getConstant(1, MVT::i64));
819 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000820 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000821 ISelDAG->getConstant(magics.s-1, MVT::i64));
822 }
823 return Q;
824}
825
Andrew Lenhartha565c272005-04-06 22:03:13 +0000826//From PPC32
827/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
828/// returns zero when the input is not exactly a power of two.
829static unsigned ExactLog2(uint64_t Val) {
830 if (Val == 0 || (Val & (Val-1))) return 0;
831 unsigned Count = 0;
832 while (Val != 1) {
833 Val >>= 1;
834 ++Count;
835 }
836 return Count;
837}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000838
839
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000840//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000841static const int IMM_LOW = -32768;
842static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000843static const int IMM_MULT = 65536;
844
845static long getUpper16(long l)
846{
847 long y = l / IMM_MULT;
848 if (l % IMM_MULT > IMM_HIGH)
849 ++y;
850 return y;
851}
852
853static long getLower16(long l)
854{
855 long h = getUpper16(l);
856 return l - h * IMM_MULT;
857}
858
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000859static unsigned GetRelVersion(unsigned opcode)
860{
861 switch (opcode) {
862 default: assert(0 && "unknown load or store"); return 0;
863 case Alpha::LDQ: return Alpha::LDQr;
864 case Alpha::LDS: return Alpha::LDSr;
865 case Alpha::LDT: return Alpha::LDTr;
866 case Alpha::LDL: return Alpha::LDLr;
867 case Alpha::LDBU: return Alpha::LDBUr;
868 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000869 case Alpha::STB: return Alpha::STBr;
870 case Alpha::STW: return Alpha::STWr;
871 case Alpha::STL: return Alpha::STLr;
872 case Alpha::STQ: return Alpha::STQr;
873 case Alpha::STS: return Alpha::STSr;
874 case Alpha::STT: return Alpha::STTr;
875
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000876 }
877}
Andrew Lenharth65838902005-02-06 16:22:15 +0000878
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000879void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000880{
881 unsigned Opc;
882 if (EnableAlphaFTOI) {
883 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
884 BuildMI(BB, Opc, 1, dst).addReg(src);
885 } else {
886 //The hard way:
887 // Spill the integer to memory and reload it from there.
888 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
889 MachineFunction *F = BB->getParent();
890 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
891
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000892 if (EnableAlphaLSMark)
893 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
894 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000895 Opc = isDouble ? Alpha::STT : Alpha::STS;
896 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000897
898 if (EnableAlphaLSMark)
899 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
900 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000901 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
902 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
903 }
904}
905
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000906void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000907{
908 unsigned Opc;
909 if (EnableAlphaFTOI) {
910 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
911 BuildMI(BB, Opc, 1, dst).addReg(src);
912 } else {
913 //The hard way:
914 // Spill the integer to memory and reload it from there.
915 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
916 MachineFunction *F = BB->getParent();
917 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
918
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000919 if (EnableAlphaLSMark)
920 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
921 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000922 Opc = isDouble ? Alpha::STQ : Alpha::STL;
923 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000924
925 if (EnableAlphaLSMark)
926 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
927 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000928 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
929 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
930 }
931}
932
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000933bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000934{
935 SDNode *Node = N.Val;
936 unsigned Opc, Tmp1, Tmp2, Tmp3;
937 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
938
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000939 bool rev = false;
940 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000941
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000942 switch (SetCC->getCondition()) {
943 default: Node->dump(); assert(0 && "Unknown FP comparison!");
944 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
945 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
946 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
947 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
948 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
949 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
950 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000951
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000952 ConstantFPSDNode *CN;
953 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
954 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
955 Tmp1 = Alpha::F31;
956 else
957 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000959 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
960 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
961 Tmp2 = Alpha::F31;
962 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000963 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000964
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000965 //Can only compare doubles, and dag won't promote for me
966 if (SetCC->getOperand(0).getValueType() == MVT::f32)
967 {
968 //assert(0 && "Setcc On float?\n");
969 std::cerr << "Setcc on float!\n";
970 Tmp3 = MakeReg(MVT::f64);
971 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
972 Tmp1 = Tmp3;
973 }
974 if (SetCC->getOperand(1).getValueType() == MVT::f32)
975 {
976 //assert (0 && "Setcc On float?\n");
977 std::cerr << "Setcc on float!\n";
978 Tmp3 = MakeReg(MVT::f64);
979 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
980 Tmp2 = Tmp3;
981 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000982
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000983 if (rev) std::swap(Tmp1, Tmp2);
984 //do the comparison
985 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
986 return inv;
987}
988
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000989//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000990void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000991{
992 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000993 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
994 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
995 { //Normal imm add
996 Reg = SelectExpr(N.getOperand(0));
997 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
998 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000999 }
1000 Reg = SelectExpr(N);
1001 offset = 0;
1002 return;
1003}
1004
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001005void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +00001006{
1007 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001008 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001009 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1010 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001011
Andrew Lenharth445171a2005-02-08 00:40:03 +00001012 Select(N.getOperand(0)); //chain
1013 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001014
Andrew Lenharth445171a2005-02-08 00:40:03 +00001015 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001016 {
1017 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1018 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1019 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001020 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1021 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001022 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001023
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001024 //Fix up CC
1025 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001026
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001027 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001028 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001029
Andrew Lenharth694c2982005-06-26 23:01:11 +00001030 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001031 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001032 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1033 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1034 case ISD::SETLT: Opc = Alpha::BLT; break;
1035 case ISD::SETLE: Opc = Alpha::BLE; break;
1036 case ISD::SETGT: Opc = Alpha::BGT; break;
1037 case ISD::SETGE: Opc = Alpha::BGE; break;
1038 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1039 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001040 //Technically you could have this CC
1041 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001042 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1043 case ISD::SETNE: Opc = Alpha::BNE; break;
1044 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001045 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001046 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1047 return;
1048 } else {
1049 unsigned Tmp1 = SelectExpr(CC);
1050 if (isNE)
1051 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1052 else
1053 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001054 return;
1055 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 } else { //FP
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001057 //Any comparison between 2 values should be codegened as an folded
1058 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001059 //for a cmp b: c = a - b;
1060 //a = b: c = 0
1061 //a < b: c < 0
1062 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001063
1064 bool invTest = false;
1065 unsigned Tmp3;
1066
1067 ConstantFPSDNode *CN;
1068 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1069 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1070 Tmp3 = SelectExpr(SetCC->getOperand(0));
1071 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1072 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1073 {
1074 Tmp3 = SelectExpr(SetCC->getOperand(1));
1075 invTest = true;
1076 }
1077 else
1078 {
1079 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1080 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1081 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1082 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1083 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1084 .addReg(Tmp1).addReg(Tmp2);
1085 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001086
1087 switch (SetCC->getCondition()) {
1088 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001089 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1090 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1091 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1092 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1093 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1094 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001095 }
1096 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001097 return;
1098 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001099 abort(); //Should never be reached
1100 } else {
1101 //Giveup and do the stupid thing
1102 unsigned Tmp1 = SelectExpr(CC);
1103 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1104 return;
1105 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001106 abort(); //Should never be reached
1107}
1108
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001109unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001110 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001111 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001112 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001113 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001114
1115 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001116 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001117 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001118
1119 unsigned &Reg = ExprMap[N];
1120 if (Reg) return Reg;
1121
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001122 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001123 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001124 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001125 else {
1126 // If this is a call instruction, make sure to prepare ALL of the result
1127 // values as well as the chain.
1128 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001129 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001130 else {
1131 Result = MakeReg(Node->getValueType(0));
1132 ExprMap[N.getValue(0)] = Result;
1133 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1134 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001135 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 }
1137 }
1138
Andrew Lenharth40831c52005-01-28 06:57:18 +00001139 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001140 default:
1141 Node->dump();
1142 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001143
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001144 case ISD::CTPOP:
1145 case ISD::CTTZ:
1146 case ISD::CTLZ:
1147 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1148 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1149 Tmp1 = SelectExpr(N.getOperand(0));
1150 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1151 return Result;
1152
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001153 case ISD::MULHU:
1154 Tmp1 = SelectExpr(N.getOperand(0));
1155 Tmp2 = SelectExpr(N.getOperand(1));
1156 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001157 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001158 case ISD::MULHS:
1159 {
1160 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1161 Tmp1 = SelectExpr(N.getOperand(0));
1162 Tmp2 = SelectExpr(N.getOperand(1));
1163 Tmp3 = MakeReg(MVT::i64);
1164 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1165 unsigned V1 = MakeReg(MVT::i64);
1166 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001167 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1168 .addReg(Tmp1);
1169 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1170 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001171 unsigned IRes = MakeReg(MVT::i64);
1172 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1173 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1174 return Result;
1175 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001176 case ISD::UNDEF: {
1177 BuildMI(BB, Alpha::IDEF, 0, Result);
1178 return Result;
1179 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001180
Andrew Lenharth032f2352005-02-22 21:59:48 +00001181 case ISD::DYNAMIC_STACKALLOC:
1182 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001183 if (Result != notIn)
1184 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001185 else
1186 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1187
1188 // FIXME: We are currently ignoring the requested alignment for handling
1189 // greater than the stack alignment. This will need to be revisited at some
1190 // point. Align = N.getOperand(2);
1191
1192 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1193 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1194 std::cerr << "Cannot allocate stack object with greater alignment than"
1195 << " the stack alignment yet!";
1196 abort();
1197 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001198
Andrew Lenharth032f2352005-02-22 21:59:48 +00001199 Select(N.getOperand(0));
1200 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1201 {
1202 if (CN->getValue() < 32000)
1203 {
1204 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1205 .addImm(-CN->getValue()).addReg(Alpha::R30);
1206 } else {
1207 Tmp1 = SelectExpr(N.getOperand(1));
1208 // Subtract size from stack pointer, thereby allocating some space.
1209 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1210 }
1211 } else {
1212 Tmp1 = SelectExpr(N.getOperand(1));
1213 // Subtract size from stack pointer, thereby allocating some space.
1214 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1215 }
1216
1217 // Put a pointer to the space into the result register, by copying the stack
1218 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001219 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001220 return Result;
1221
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001222 case ISD::ConstantPool:
1223 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1224 AlphaLowering.restoreGP(BB);
1225 Tmp2 = MakeReg(MVT::i64);
1226 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1227 .addReg(Alpha::R29);
1228 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1229 .addReg(Tmp2);
1230 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001231
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001232 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001233 BuildMI(BB, Alpha::LDA, 2, Result)
1234 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1235 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001236 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001237
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001238 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001239 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001240 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001241 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001242 {
1243 // Make sure we generate both values.
1244 if (Result != notIn)
1245 ExprMap[N.getValue(1)] = notIn; // Generate the token
1246 else
1247 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001248
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001249 SDOperand Chain = N.getOperand(0);
1250 SDOperand Address = N.getOperand(1);
1251 Select(Chain);
1252
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001253 bool fpext = true;
1254
Andrew Lenharth03824012005-02-07 05:55:55 +00001255 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001256 switch (Node->getValueType(0)) {
1257 default: Node->dump(); assert(0 && "Bad load!");
1258 case MVT::i64: Opc = Alpha::LDQ; break;
1259 case MVT::f64: Opc = Alpha::LDT; break;
1260 case MVT::f32: Opc = Alpha::LDS; break;
1261 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001262 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001263 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1264 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001265 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001266 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001267 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001268 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001269 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001270 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001271 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001272 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001273
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001274 int i, j, k;
1275 if (EnableAlphaLSMark)
1276 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1277 i, j, k);
1278
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001279 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1280 if (GASD && !GASD->getGlobal()->isExternal()) {
1281 Tmp1 = MakeReg(MVT::i64);
1282 AlphaLowering.restoreGP(BB);
1283 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1284 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1285 if (EnableAlphaLSMark)
1286 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1287 .addImm(getUID());
1288 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1289 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001290 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001291 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001292 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001293 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001294 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1295 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001296 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001297 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1298 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001299 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1300 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1301 } else if(Address.getOpcode() == ISD::FrameIndex) {
1302 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001303 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1304 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001305 BuildMI(BB, Opc, 2, Result)
1306 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1307 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001308 } else {
1309 long offset;
1310 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001311 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001312 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1313 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1315 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001316 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001317 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001318
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001319 case ISD::GlobalAddress:
1320 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001321 has_sym = true;
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001322
1323 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001324
1325 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001326 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001327 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001328
1329 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001330 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1331 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001332 return Result;
1333
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001334 case ISD::ExternalSymbol:
1335 AlphaLowering.restoreGP(BB);
1336 has_sym = true;
1337
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001338 Reg = Result = MakeReg(MVT::i64);
1339
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001340 if (EnableAlphaLSMark)
1341 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1342 .addImm(getUID());
1343
1344 BuildMI(BB, Alpha::LDQl, 2, Result)
1345 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1346 .addReg(Alpha::R29);
1347 return Result;
1348
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001349 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 case ISD::CALL:
1351 {
1352 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001354 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001355 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001356
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 //grab the arguments
1358 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001359 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001360 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001361 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362
Andrew Lenharth684f2292005-01-30 00:35:27 +00001363 //in reg args
1364 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 Alpha::F19, Alpha::F20, Alpha::F21};
1370 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001371 default:
1372 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001373 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001374 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001375 N.getOperand(i+2).getValueType() << "\n";
1376 assert(0 && "Unknown value type for call");
1377 case MVT::i1:
1378 case MVT::i8:
1379 case MVT::i16:
1380 case MVT::i32:
1381 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001382 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1383 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 break;
1385 case MVT::f32:
1386 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001387 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1388 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001390 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001391 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001392 //in mem args
1393 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001394 {
1395 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 default:
1397 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001398 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001399 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001400 N.getOperand(i+2).getValueType() << "\n";
1401 assert(0 && "Unknown value type for call");
1402 case MVT::i1:
1403 case MVT::i8:
1404 case MVT::i16:
1405 case MVT::i32:
1406 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001407 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1408 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001409 break;
1410 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001411 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1412 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001413 break;
1414 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001415 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1416 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001418 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001420 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001421 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1422 if (GASD && !GASD->getGlobal()->isExternal()) {
1423 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001425 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1426 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001427 } else {
1428 //no need to restore GP as we are doing an indirect call
1429 Tmp1 = SelectExpr(N.getOperand(1));
1430 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1431 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1432 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001433
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001434 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001435
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001436 switch (Node->getValueType(0)) {
1437 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001438 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001439 case MVT::i1:
1440 case MVT::i8:
1441 case MVT::i16:
1442 case MVT::i32:
1443 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001444 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1445 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001446 case MVT::f32:
1447 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001448 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1449 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001450 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001451 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001452 }
1453
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001454 case ISD::SIGN_EXTEND_INREG:
1455 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001456 //do SDIV opt for all levels of ints if not dividing by a constant
1457 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1458 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001459 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001460 unsigned Tmp4 = MakeReg(MVT::f64);
1461 unsigned Tmp5 = MakeReg(MVT::f64);
1462 unsigned Tmp6 = MakeReg(MVT::f64);
1463 unsigned Tmp7 = MakeReg(MVT::f64);
1464 unsigned Tmp8 = MakeReg(MVT::f64);
1465 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001466
1467 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1468 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1469 MoveInt2FP(Tmp1, Tmp4, true);
1470 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001471 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1472 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1473 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1474 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001475 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001476 return Result;
1477 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001479 //Alpha has instructions for a bunch of signed 32 bit stuff
1480 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001481 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001482 switch (N.getOperand(0).getOpcode()) {
1483 case ISD::ADD:
1484 case ISD::SUB:
1485 case ISD::MUL:
1486 {
1487 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1488 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1489 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001490 ConstantSDNode* CSD = NULL;
1491 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1492 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1493 (CSD->getValue() == 2 || CSD->getValue() == 3))
1494 {
1495 bool use4 = CSD->getValue() == 2;
1496 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1497 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1498 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1499 2,Result).addReg(Tmp1).addReg(Tmp2);
1500 }
1501 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1502 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1503 (CSD->getValue() == 2 || CSD->getValue() == 3))
1504 {
1505 bool use4 = CSD->getValue() == 2;
1506 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1507 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1508 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1509 }
1510 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001511 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1512 { //Normal imm add/sub
1513 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001514 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001515 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1516 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001517 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001518 else
1519 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001520 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001522 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001523 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1524 }
1525 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001526 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 default: break; //Fall Though;
1528 }
1529 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001530 Tmp1 = SelectExpr(N.getOperand(0));
1531 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001532 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001533 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001534 {
1535 default:
1536 Node->dump();
1537 assert(0 && "Sign Extend InReg not there yet");
1538 break;
1539 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001540 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001542 break;
1543 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001544 case MVT::i16:
1545 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1546 break;
1547 case MVT::i8:
1548 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1549 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001550 case MVT::i1:
1551 Tmp2 = MakeReg(MVT::i64);
1552 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001553 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001554 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001555 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001556 return Result;
1557 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001558
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001559 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001560 {
1561 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1562 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001563 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001564 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001565
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001566 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001567 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001568 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001569 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001570
1571 switch (SetCC->getCondition()) {
1572 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001573 case ISD::SETEQ:
1574 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001576 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001577 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001578 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1579 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1580 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001581 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001582 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1583 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001584 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001585 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1586 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001587 case ISD::SETNE: {//Handle this one special
1588 //std::cerr << "Alpha does not have a setne.\n";
1589 //abort();
1590 Tmp1 = SelectExpr(N.getOperand(0));
1591 Tmp2 = SelectExpr(N.getOperand(1));
1592 Tmp3 = MakeReg(MVT::i64);
1593 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001594 //Remeber we have the Inv for this CC
1595 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001596 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001597 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001598 return Result;
1599 }
1600 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001601 if (dir == 1) {
1602 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001603 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001604 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1605 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1606 } else {
1607 Tmp2 = SelectExpr(N.getOperand(1));
1608 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1609 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001610 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001611 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001612 Tmp2 = SelectExpr(N.getOperand(0));
1613 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001614 }
1615 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001616 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001617 Tmp1 = MakeReg(MVT::f64);
1618 bool inv = SelectFPSetCC(N, Tmp1);
1619
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001620 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001621 Tmp2 = MakeReg(MVT::i64);
1622 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001623 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001624 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001625 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001626 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001627 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001628 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001629
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001630 case ISD::CopyFromReg:
1631 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001632 ++count_ins;
1633
Andrew Lenharth40831c52005-01-28 06:57:18 +00001634 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001635 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001636 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001637 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001638 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001639
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001640 SDOperand Chain = N.getOperand(0);
1641
1642 Select(Chain);
1643 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1644 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001645 if (isFP)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001646 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1647 else
1648 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001649 return Result;
1650 }
1651
Misha Brukman4633f1c2005-04-21 23:13:11 +00001652 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001653 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001654 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001655 //Match Not
1656 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001657 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001658 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001659 Tmp1 = SelectExpr(N.getOperand(0));
1660 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1661 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001662 }
1663 //Fall through
1664 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001665 //handle zap
1666 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1667 {
1668 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1669 unsigned int build = 0;
1670 for(int i = 0; i < 8; ++i)
1671 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001672 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001673 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001674 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001675 { build = 0; break; }
1676 k >>= 8;
1677 }
1678 if (build)
1679 {
1680 Tmp1 = SelectExpr(N.getOperand(0));
1681 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1682 return Result;
1683 }
1684 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001685 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001686 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001687 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001688 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001689 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
1690 == -1) {
1691 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001692 case ISD::AND: Opc = Alpha::BIC; break;
1693 case ISD::OR: Opc = Alpha::ORNOT; break;
1694 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001695 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001696 Tmp1 = SelectExpr(N.getOperand(1));
1697 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1698 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1699 return Result;
1700 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001701 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001702 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001703 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001704 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1705 == -1) {
1706 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001707 case ISD::AND: Opc = Alpha::BIC; break;
1708 case ISD::OR: Opc = Alpha::ORNOT; break;
1709 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001710 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001711 Tmp1 = SelectExpr(N.getOperand(0));
1712 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1713 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1714 return Result;
1715 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001716 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001717 case ISD::SHL:
1718 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001719 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001720 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001721 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001722 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001723 {
1724 switch(opcode) {
1725 case ISD::AND: Opc = Alpha::ANDi; break;
1726 case ISD::OR: Opc = Alpha::BISi; break;
1727 case ISD::XOR: Opc = Alpha::XORi; break;
1728 case ISD::SHL: Opc = Alpha::SLi; break;
1729 case ISD::SRL: Opc = Alpha::SRLi; break;
1730 case ISD::SRA: Opc = Alpha::SRAi; break;
1731 case ISD::MUL: Opc = Alpha::MULQi; break;
1732 };
1733 Tmp1 = SelectExpr(N.getOperand(0));
1734 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1735 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1736 } else {
1737 switch(opcode) {
1738 case ISD::AND: Opc = Alpha::AND; break;
1739 case ISD::OR: Opc = Alpha::BIS; break;
1740 case ISD::XOR: Opc = Alpha::XOR; break;
1741 case ISD::SHL: Opc = Alpha::SL; break;
1742 case ISD::SRL: Opc = Alpha::SRL; break;
1743 case ISD::SRA: Opc = Alpha::SRA; break;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001744 case ISD::MUL:
1745 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
1746 : Alpha::MULQ;
1747 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001748 };
1749 Tmp1 = SelectExpr(N.getOperand(0));
1750 Tmp2 = SelectExpr(N.getOperand(1));
1751 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1752 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001753 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001754
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001755 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001756 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001757 if (isFP) {
1758 ConstantFPSDNode *CN;
1759 if (opcode == ISD::ADD)
1760 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1761 else
1762 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1763 if (opcode == ISD::SUB
1764 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1765 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1766 {
1767 Tmp2 = SelectExpr(N.getOperand(1));
1768 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1769 } else {
1770 Tmp1 = SelectExpr(N.getOperand(0));
1771 Tmp2 = SelectExpr(N.getOperand(1));
1772 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1773 }
1774 return Result;
1775 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001776 bool isAdd = opcode == ISD::ADD;
1777
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001778 //first check for Scaled Adds and Subs!
1779 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001780 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001781 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001782 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1783 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001784 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001785 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001786 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001787 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1788 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1789 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001790 else {
1791 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001792 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1793 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001794 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001795 }
1796 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001797 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001798 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1799 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001800 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001801 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001802 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001803 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1804 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1805 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001806 else {
1807 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001808 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001809 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001810 }
1811 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001812 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1813 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001814 { //Normal imm add/sub
1815 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1816 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001817 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001818 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001819 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001820 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1821 CSD->getSignExtended() <= 32767 &&
1822 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001823 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001824 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001825 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001826 if (!isAdd)
1827 Tmp2 = -Tmp2;
1828 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001829 }
1830 //give up and do the operation
1831 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001832 //Normal add/sub
1833 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1834 Tmp1 = SelectExpr(N.getOperand(0));
1835 Tmp2 = SelectExpr(N.getOperand(1));
1836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1837 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001838 return Result;
1839 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001840
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001841 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001842 if (isFP) {
1843 Tmp1 = SelectExpr(N.getOperand(0));
1844 Tmp2 = SelectExpr(N.getOperand(1));
1845 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1846 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001847 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001848 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001849 ConstantSDNode* CSD;
1850 //check if we can convert into a shift!
1851 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1852 (int64_t)CSD->getSignExtended() != 0 &&
1853 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1854 {
1855 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1856 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001857 if (k == 1)
1858 Tmp2 = Tmp1;
1859 else
1860 {
1861 Tmp2 = MakeReg(MVT::i64);
1862 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1863 }
1864 Tmp3 = MakeReg(MVT::i64);
1865 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1866 unsigned Tmp4 = MakeReg(MVT::i64);
1867 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1868 if ((int64_t)CSD->getSignExtended() > 0)
1869 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1870 else
1871 {
1872 unsigned Tmp5 = MakeReg(MVT::i64);
1873 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1874 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1875 }
1876 return Result;
1877 }
1878 }
1879 //Else fall through
1880
1881 case ISD::UDIV:
1882 {
1883 ConstantSDNode* CSD;
1884 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1885 ((int64_t)CSD->getSignExtended() >= 2 ||
1886 (int64_t)CSD->getSignExtended() <= -2))
1887 {
1888 // If this is a divide by constant, we can emit code using some magic
1889 // constants to implement it as a multiply instead.
1890 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001891 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001892 return SelectExpr(BuildSDIVSequence(N));
1893 else
1894 return SelectExpr(BuildUDIVSequence(N));
1895 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001896 }
1897 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001898 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001899 case ISD::SREM: {
1900 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001901 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001902 case ISD::UREM: opstr = "__remqu"; break;
1903 case ISD::SREM: opstr = "__remq"; break;
1904 case ISD::UDIV: opstr = "__divqu"; break;
1905 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001906 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001907 Tmp1 = SelectExpr(N.getOperand(0));
1908 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001909 SDOperand Addr =
1910 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1911 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001912 //set up regs explicitly (helps Reg alloc)
1913 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001914 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001915 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1916 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001917 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001918 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001919 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001920
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001921 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001922 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001923 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001924 assert (DestType == MVT::i64 && "only quads can be loaded to");
1925 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001926 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001927 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001928 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001929 {
1930 Tmp2 = MakeReg(MVT::f64);
1931 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1932 Tmp1 = Tmp2;
1933 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001934 Tmp2 = MakeReg(MVT::f64);
1935 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001936 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001937
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001938 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001939 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001940
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001941 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001942 if (isFP) {
1943 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1944 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1945 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1946
1947 SDOperand CC = N.getOperand(0);
1948 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1949
1950 if (CC.getOpcode() == ISD::SETCC &&
1951 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1952 { //FP Setcc -> Select yay!
1953
1954
1955 //for a cmp b: c = a - b;
1956 //a = b: c = 0
1957 //a < b: c < 0
1958 //a > b: c > 0
1959
1960 bool invTest = false;
1961 unsigned Tmp3;
1962
1963 ConstantFPSDNode *CN;
1964 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1965 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1966 Tmp3 = SelectExpr(SetCC->getOperand(0));
1967 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1968 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1969 {
1970 Tmp3 = SelectExpr(SetCC->getOperand(1));
1971 invTest = true;
1972 }
1973 else
1974 {
1975 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1976 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1977 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1978 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1979 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1980 .addReg(Tmp1).addReg(Tmp2);
1981 }
1982
1983 switch (SetCC->getCondition()) {
1984 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1985 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1986 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1987 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1988 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1989 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1990 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1991 }
1992 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1993 return Result;
1994 }
1995 else
1996 {
1997 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1998 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1999 .addReg(Tmp1);
2000// // Spill the cond to memory and reload it from there.
2001// unsigned Tmp4 = MakeReg(MVT::f64);
2002// MoveIntFP(Tmp1, Tmp4, true);
2003// //now ideally, we don't have to do anything to the flag...
2004// // Get the condition into the zero flag.
2005// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
2006 return Result;
2007 }
2008 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002009 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
2010 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002011 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002012 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2013 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002014 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002015 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002016
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002017 SDOperand CC = N.getOperand(0);
2018 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2019
Misha Brukman4633f1c2005-04-21 23:13:11 +00002020 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002021 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2022 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002023 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002024 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2025 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002026 bool inv = SelectFPSetCC(CC, Tmp1);
2027 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2028 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2029 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002030 }
2031 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002032 //Int SetCC -> Select
2033 //Dropping the CC is only useful if we are comparing to 0
2034 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002035 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002036 {
2037 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002038 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002039 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002040
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002041 //Fix up CC
2042 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002043 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002044 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002045
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002046 //Choose the CMOV
2047 switch (cCode) {
2048 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002049 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2050 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2051 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2052 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2053 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2054 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2055 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2056 //Technically you could have this CC
2057 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2058 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2059 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002060 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002061 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002062
Andrew Lenharth694c2982005-06-26 23:01:11 +00002063 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002064 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2065 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002066 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002067 .addReg(Tmp1);
2068 } else {
2069 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2070 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2071 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2072 }
2073 return Result;
2074 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002075 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002076 }
2077 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002078 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2079 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002080 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2081 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002082
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002083 return Result;
2084 }
2085
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002086 case ISD::Constant:
2087 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002088 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002089 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002090 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002091 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002092 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2093 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2094 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002095 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2096 .addReg(Alpha::R31);
Misha Brukman7847fca2005-04-22 17:54:37 +00002097 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002098 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002099 else {
2100 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002101 ConstantUInt *C =
2102 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002103 unsigned CPI = CP->getConstantPoolIndex(C);
2104 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002105 has_sym = true;
2106 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002107 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2108 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002109 if (EnableAlphaLSMark)
2110 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2111 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002112 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2113 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002114 }
2115 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002116 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002117 case ISD::FNEG:
2118 if(ISD::FABS == N.getOperand(0).getOpcode())
2119 {
2120 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2121 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2122 } else {
2123 Tmp1 = SelectExpr(N.getOperand(0));
2124 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2125 }
2126 return Result;
2127
2128 case ISD::FABS:
2129 Tmp1 = SelectExpr(N.getOperand(0));
2130 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2131 return Result;
2132
2133 case ISD::FP_ROUND:
2134 assert (DestType == MVT::f32 &&
2135 N.getOperand(0).getValueType() == MVT::f64 &&
2136 "only f64 to f32 conversion supported here");
2137 Tmp1 = SelectExpr(N.getOperand(0));
2138 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
2139 return Result;
2140
2141 case ISD::FP_EXTEND:
2142 assert (DestType == MVT::f64 &&
2143 N.getOperand(0).getValueType() == MVT::f32 &&
2144 "only f32 to f64 conversion supported here");
2145 Tmp1 = SelectExpr(N.getOperand(0));
2146 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
2147 return Result;
2148
2149 case ISD::ConstantFP:
2150 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2151 if (CN->isExactlyValue(+0.0)) {
2152 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2153 .addReg(Alpha::F31);
2154 } else if ( CN->isExactlyValue(-0.0)) {
2155 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2156 .addReg(Alpha::F31);
2157 } else {
2158 abort();
2159 }
2160 }
2161 return Result;
2162
2163 case ISD::SINT_TO_FP:
2164 {
2165 assert (N.getOperand(0).getValueType() == MVT::i64
2166 && "only quads can be loaded from");
2167 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2168 Tmp2 = MakeReg(MVT::f64);
2169 MoveInt2FP(Tmp1, Tmp2, true);
2170 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
2171 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2172 return Result;
2173 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002174 }
2175
2176 return 0;
2177}
2178
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002179void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002180 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002181 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002182
Nate Begeman85fdeb22005-03-24 04:39:54 +00002183 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002184 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002185
2186 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002187
Andrew Lenharth760270d2005-02-07 23:02:23 +00002188 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002189
2190 default:
2191 Node->dump(); std::cerr << "\n";
2192 assert(0 && "Node not handled yet!");
2193
2194 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002195 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002196 return;
2197 }
2198
2199 case ISD::BR: {
2200 MachineBasicBlock *Dest =
2201 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2202
2203 Select(N.getOperand(0));
2204 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2205 return;
2206 }
2207
2208 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002209 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002210 Select(N.getOperand(0));
2211 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2212 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002214 case ISD::EntryToken: return; // Noop
2215
2216 case ISD::TokenFactor:
2217 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2218 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002219
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002220 //N.Val->dump(); std::cerr << "\n";
2221 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002222
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 return;
2224
2225 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002226 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 Select(N.getOperand(0));
2228 Tmp1 = SelectExpr(N.getOperand(1));
2229 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002230
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002231 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002232 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002233 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002234 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2235 else
2236 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237 }
2238 return;
2239
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002240 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002241 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002242 switch (N.getNumOperands()) {
2243 default:
2244 std::cerr << N.getNumOperands() << "\n";
2245 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2246 std::cerr << N.getOperand(i).getValueType() << "\n";
2247 Node->dump();
2248 assert(0 && "Unknown return instruction!");
2249 case 2:
2250 Select(N.getOperand(0));
2251 Tmp1 = SelectExpr(N.getOperand(1));
2252 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002253 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002254 assert(0 && "All other types should have been promoted!!");
2255 case MVT::f64:
2256 case MVT::f32:
2257 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2258 break;
2259 case MVT::i32:
2260 case MVT::i64:
2261 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2262 break;
2263 }
2264 break;
2265 case 1:
2266 Select(N.getOperand(0));
2267 break;
2268 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002269 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002270 AlphaLowering.restoreRA(BB);
2271 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002272 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002273
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274 case ISD::TRUNCSTORE:
2275 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002276 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002277 SDOperand Chain = N.getOperand(0);
2278 SDOperand Value = N.getOperand(1);
2279 SDOperand Address = N.getOperand(2);
2280 Select(Chain);
2281
2282 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002283
2284 if (opcode == ISD::STORE) {
2285 switch(Value.getValueType()) {
2286 default: assert(0 && "unknown Type in store");
2287 case MVT::i64: Opc = Alpha::STQ; break;
2288 case MVT::f64: Opc = Alpha::STT; break;
2289 case MVT::f32: Opc = Alpha::STS; break;
2290 }
2291 } else { //ISD::TRUNCSTORE
2292 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2293 default: assert(0 && "unknown Type in store");
2294 case MVT::i1: //FIXME: DAG does not promote this load
2295 case MVT::i8: Opc = Alpha::STB; break;
2296 case MVT::i16: Opc = Alpha::STW; break;
2297 case MVT::i32: Opc = Alpha::STL; break;
2298 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002299 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002300
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002301 int i, j, k;
2302 if (EnableAlphaLSMark)
2303 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
2304 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002305
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002306 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2307 if (GASD && !GASD->getGlobal()->isExternal()) {
2308 Tmp2 = MakeReg(MVT::i64);
2309 AlphaLowering.restoreGP(BB);
2310 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2311 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2312 if (EnableAlphaLSMark)
2313 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2314 .addImm(getUID());
2315 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2316 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002317 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002318 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002319 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2320 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002321 BuildMI(BB, Opc, 3).addReg(Tmp1)
2322 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2323 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002324 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002325 long offset;
2326 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002327 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002328 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2329 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002330 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2331 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002332 return;
2333 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334
2335 case ISD::EXTLOAD:
2336 case ISD::SEXTLOAD:
2337 case ISD::ZEXTLOAD:
2338 case ISD::LOAD:
2339 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002340 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002341 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002342 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002343 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 SelectExpr(N);
2345 return;
2346
Chris Lattner16cd04d2005-05-12 23:24:06 +00002347 case ISD::CALLSEQ_START:
2348 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002349 Select(N.getOperand(0));
2350 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002351
Chris Lattner16cd04d2005-05-12 23:24:06 +00002352 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002353 Alpha::ADJUSTSTACKUP;
2354 BuildMI(BB, Opc, 1).addImm(Tmp1);
2355 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002356
2357 case ISD::PCMARKER:
2358 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002359 BuildMI(BB, Alpha::PCLABEL, 2)
2360 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002361 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002362 }
2363 assert(0 && "Should not be reached!");
2364}
2365
2366
2367/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2368/// into a machine code representation using pattern matching and a machine
2369/// description file.
2370///
2371FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002372 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002373}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002374