blob: 32259631ce18859a99c14caa078e8028df7c0175 [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 Lenhartha48f3ce2005-07-07 19:52:58 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000098 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
Andrew Lenharthec151362005-06-26 22:23:06 +0000101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Chris Lattnere0fe2252005-07-05 19:58:54 +0000159 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
160 Value *VAListV, SelectionDAG &DAG);
161 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
162 SDOperand DestP, Value *DestV,
163 SelectionDAG &DAG);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000165 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
166 const Type *ArgTy, SelectionDAG &DAG);
167
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000168 void restoreGP(MachineBasicBlock* BB)
169 {
170 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
171 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000172 void restoreRA(MachineBasicBlock* BB)
173 {
174 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
175 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000176 unsigned getRA()
177 {
178 return RA;
179 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000180
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 };
182}
183
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000184/// LowerOperation - Provide custom lowering hooks for some operations.
185///
186SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
187 MachineFunction &MF = DAG.getMachineFunction();
188 switch (Op.getOpcode()) {
189 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000190#if 0
191 case ISD::SINT_TO_FP:
192 {
193 assert (Op.getOperand(0).getValueType() == MVT::i64
194 && "only quads can be loaded from");
195 SDOperand SRC;
196 if (EnableAlphaFTOI)
197 {
198 std::vector<MVT::ValueType> RTs;
199 RTs.push_back(Op.getValueType());
200 std::vector<SDOperand> Ops;
201 Ops.push_back(Op.getOperand(0));
202 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
203 } else {
204 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
205 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000206 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
207 DAG.getEntryNode(), Op.getOperand(0),
208 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000209 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
210 DAG.getSrcValue(NULL));
211 }
212 std::vector<MVT::ValueType> RTs;
213 RTs.push_back(Op.getValueType());
214 std::vector<SDOperand> Ops;
215 Ops.push_back(SRC);
216 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
217 }
218#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000219 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000220 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000221}
222
223
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000224/// AddLiveIn - This helper function adds the specified physical register to the
225/// MachineFunction as a live in value. It also creates a corresponding virtual
226/// register for it.
227static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
228 TargetRegisterClass *RC) {
229 assert(RC->contains(PReg) && "Not the correct regclass!");
230 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
231 MF.addLiveIn(PReg, VReg);
232 return VReg;
233}
234
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
236
237//For now, just use variable size stack frame format
238
239//In a standard call, the first six items are passed in registers $16
240//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
241//of argument-to-register correspondence.) The remaining items are
242//collected in a memory argument list that is a naturally aligned
243//array of quadwords. In a standard call, this list, if present, must
244//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000245//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000247// //#define FP $15
248// //#define RA $26
249// //#define PV $27
250// //#define GP $29
251// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000252
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000254AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255{
256 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000257
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000259 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 MachineBasicBlock& BB = MF.front();
262
Misha Brukman4633f1c2005-04-21 23:13:11 +0000263 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000264 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000265 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000266 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000268
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000269 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000270 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000271
Chris Lattnere4d5c442005-03-15 04:54:21 +0000272 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000273 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000274 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000275 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000276 unsigned Vreg;
277 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000279 default:
280 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 abort();
282 case MVT::f64:
283 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000284 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
285 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 break;
287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
290 case MVT::i32:
291 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000292 args_int[count] = AddLiveIn(MF, args_int[count],
293 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000294 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000295 if (VT != MVT::i64)
296 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000297 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000298 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000299 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 } else { //more args
301 // Create the frame index object for this incoming parameter...
302 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000303
304 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000305 //from this parameter
306 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000307 argt = DAG.getLoad(getValueType(I->getType()),
308 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000309 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000310 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 ArgValues.push_back(argt);
312 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000313
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000314 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000315 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000316 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000317 std::vector<SDOperand> LS;
318 for (int i = 0; i < 6; ++i) {
319 if (args_int[i] < 1024)
320 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
321 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000322 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000323 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000324 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000325 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
326 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000327
328 if (args_float[i] < 1024)
329 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
330 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000331 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
332 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000333 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
334 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000335 }
336
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000337 //Set up a token factor with all the stack traffic
338 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
339 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000340
341 // Finally, inform the code generator which regs we return values in.
342 switch (getValueType(F.getReturnType())) {
343 default: assert(0 && "Unknown type!");
344 case MVT::isVoid: break;
345 case MVT::i1:
346 case MVT::i8:
347 case MVT::i16:
348 case MVT::i32:
349 case MVT::i64:
350 MF.addLiveOut(Alpha::R0);
351 break;
352 case MVT::f32:
353 case MVT::f64:
354 MF.addLiveOut(Alpha::F0);
355 break;
356 }
357
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000358 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000359 return ArgValues;
360}
361
362std::pair<SDOperand, SDOperand>
363AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000364 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000365 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000366 SDOperand Callee, ArgListTy &Args,
367 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000368 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000369 if (Args.size() > 6)
370 NumBytes = (Args.size() - 6) * 8;
371
Chris Lattner16cd04d2005-05-12 23:24:06 +0000372 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000373 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000374 std::vector<SDOperand> args_to_use;
375 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000376 {
377 switch (getValueType(Args[i].second)) {
378 default: assert(0 && "Unexpected ValueType for argument!");
379 case MVT::i1:
380 case MVT::i8:
381 case MVT::i16:
382 case MVT::i32:
383 // Promote the integer to 64 bits. If the input type is signed use a
384 // sign extend, otherwise use a zero extend.
385 if (Args[i].second->isSigned())
386 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
387 else
388 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
389 break;
390 case MVT::i64:
391 case MVT::f64:
392 case MVT::f32:
393 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000394 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000395 args_to_use.push_back(Args[i].first);
396 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000397
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000398 std::vector<MVT::ValueType> RetVals;
399 MVT::ValueType RetTyVT = getValueType(RetTy);
400 if (RetTyVT != MVT::isVoid)
401 RetVals.push_back(RetTyVT);
402 RetVals.push_back(MVT::Other);
403
Misha Brukman4633f1c2005-04-21 23:13:11 +0000404 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000406 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000407 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000408 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000409 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000410}
411
Chris Lattnere0fe2252005-07-05 19:58:54 +0000412SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
413 Value *VAListV, SelectionDAG &DAG) {
414 // vastart stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000415 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000416 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
417 DAG.getSrcValue(VAListV));
418 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000419 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000420 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
421 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
422 DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000423}
424
425std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000426LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
427 const Type *ArgTy, SelectionDAG &DAG) {
428 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
429 DAG.getSrcValue(VAListV));
430 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000431 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000432 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
Chris Lattnere0fe2252005-07-05 19:58:54 +0000433 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000434 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000435 if (ArgTy->isFloatingPoint())
436 {
437 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
438 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000439 DAG.getConstant(8*6, MVT::i64));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000440 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
441 Offset, DAG.getConstant(8*6, MVT::i64));
442 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
443 }
444
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000445 SDOperand Result;
446 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000447 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000448 DAG.getSrcValue(NULL), MVT::i32);
449 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000450 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000451 DAG.getSrcValue(NULL), MVT::i32);
452 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000453 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 DAG.getSrcValue(NULL));
455
Andrew Lenharth558bc882005-06-18 18:34:52 +0000456 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
457 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000458 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
459 Result.getValue(1), NewOffset,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000460 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000461 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
462
Andrew Lenharth558bc882005-06-18 18:34:52 +0000463 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000464}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000465
Chris Lattnere0fe2252005-07-05 19:58:54 +0000466
467SDOperand AlphaTargetLowering::
468LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
469 Value *DestV, SelectionDAG &DAG) {
470 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
471 DAG.getSrcValue(SrcV));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000472 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Chris Lattnere0fe2252005-07-05 19:58:54 +0000473 Val, DestP, DAG.getSrcValue(DestV));
474 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000475 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000476 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP,
477 DAG.getSrcValue(SrcV, 8), MVT::i32);
478 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000479 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000480 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
481 Val, NPD, DAG.getSrcValue(DestV, 8), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000482}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000483
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000484namespace {
485
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000486//===--------------------------------------------------------------------===//
487/// ISel - Alpha specific code to select Alpha machine instructions for
488/// SelectionDAG operations.
489//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000490class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000491
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000492 /// AlphaLowering - This object fully describes how to lower LLVM code to an
493 /// Alpha-specific SelectionDAG.
494 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000495
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000496 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
497 // for sdiv and udiv until it is put into the future
498 // dag combiner.
499
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000500 /// ExprMap - As shared expressions are codegen'd, we keep track of which
501 /// vreg the value is produced in, so we only emit one copy of each compiled
502 /// tree.
503 static const unsigned notIn = (unsigned)(-1);
504 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000505
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000506 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
507 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000508
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000509 int count_ins;
510 int count_outs;
511 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000512 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000513
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000514public:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000515 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
516 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000517 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000518
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000519 /// InstructionSelectBasicBlock - This callback is invoked by
520 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
521 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000522 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000523 count_ins = 0;
524 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000525 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000526 has_sym = false;
527
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000528 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000529 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000530 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000531 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000532
533 if(has_sym)
534 ++count_ins;
535 if(EnableAlphaCount)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000536 std::cerr << "COUNT: "
537 << BB->getParent()->getFunction ()->getName() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000538 << BB->getNumber() << " "
539 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000540 << count_ins << " "
541 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000542
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000543 // Clear state used for selection.
544 ExprMap.clear();
545 CCInvMap.clear();
546 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000547
548 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000549
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000550 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000551 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000552
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000553 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
554 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000555 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
556 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000557 //returns whether the sense of the comparison was inverted
558 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000559
560 // dag -> dag expanders for integer divide by constant
561 SDOperand BuildSDIVSequence(SDOperand N);
562 SDOperand BuildUDIVSequence(SDOperand N);
563
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000564};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565}
566
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000567void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000568 // If this function has live-in values, emit the copies from pregs to vregs at
569 // the top of the function, before anything else.
570 MachineBasicBlock *BB = MF.begin();
571 if (MF.livein_begin() != MF.livein_end()) {
572 SSARegMap *RegMap = MF.getSSARegMap();
573 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
574 E = MF.livein_end(); LI != E; ++LI) {
575 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
576 if (RC == Alpha::GPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000577 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
578 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000579 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000580 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
581 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000582 } else {
583 assert(0 && "Unknown regclass!");
584 }
585 }
586 }
587}
588
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000589static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000590{
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000591 if (v == NULL) {
592 type = 0;
593 fun = 0;
594 offset = 0;
595 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
596 type = 1;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000597 fun = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000598 const Module* M = GV->getParent();
599 int i = 0;
600 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
601 ++i;
602 offset = i;
603 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
604 type = 2;
605 const Function* F = Arg->getParent();
606 const Module* M = F->getParent();
607 int i = 0;
608 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
609 ++i;
610 fun = i;
611 i = 0;
612 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
613 ++i;
614 offset = i;
615 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000616 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000617 type = 3;
618 const BasicBlock* bb = I->getParent();
619 const Function* F = bb->getParent();
620 const Module* M = F->getParent();
621 int i = 0;
622 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
623 ++i;
624 fun = i;
625 i = 0;
626 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
627 i += ii->size();
628 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
629 ++i;
630 offset = i;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000631 } else if (const Constant* C = dyn_cast<Constant>(v)) {
632 //Don't know how to look these up yet
633 type = 0;
634 fun = 0;
635 offset = 0;
636 } else {
637 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000638 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000639 //type = 4: register spilling
640 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000641}
642
643static int getUID()
644{
645 static int id = 0;
646 return ++id;
647}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000648
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000649//Factorize a number using the list of constants
650static bool factorize(int v[], int res[], int size, uint64_t c)
651{
652 bool cont = true;
653 while (c != 1 && cont)
654 {
655 cont = false;
656 for(int i = 0; i < size; ++i)
657 {
658 if (c % v[i] == 0)
659 {
660 c /= v[i];
661 ++res[i];
662 cont=true;
663 }
664 }
665 }
666 return c == 1;
667}
668
669
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000670//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000671// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000672// a multiply.
673struct ms {
674 int64_t m; // magic number
675 int64_t s; // shift amount
676};
677
678struct mu {
679 uint64_t m; // magic number
680 int64_t a; // add indicator
681 int64_t s; // shift amount
682};
683
684/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000685/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000686/// or -1.
687static struct ms magic(int64_t d) {
688 int64_t p;
689 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
690 const uint64_t two63 = 9223372036854775808ULL; // 2^63
691 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000692
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000693 ad = abs(d);
694 t = two63 + ((uint64_t)d >> 63);
695 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000696 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000697 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
698 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
699 q2 = two63/ad; // initialize q2 = 2p/abs(d)
700 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
701 do {
702 p = p + 1;
703 q1 = 2*q1; // update q1 = 2p/abs(nc)
704 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
705 if (r1 >= anc) { // must be unsigned comparison
706 q1 = q1 + 1;
707 r1 = r1 - anc;
708 }
709 q2 = 2*q2; // update q2 = 2p/abs(d)
710 r2 = 2*r2; // update r2 = rem(2p/abs(d))
711 if (r2 >= ad) { // must be unsigned comparison
712 q2 = q2 + 1;
713 r2 = r2 - ad;
714 }
715 delta = ad - r2;
716 } while (q1 < delta || (q1 == delta && r1 == 0));
717
718 mag.m = q2 + 1;
719 if (d < 0) mag.m = -mag.m; // resulting magic number
720 mag.s = p - 64; // resulting shift
721 return mag;
722}
723
724/// magicu - calculate the magic numbers required to codegen an integer udiv as
725/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
726static struct mu magicu(uint64_t d)
727{
728 int64_t p;
729 uint64_t nc, delta, q1, r1, q2, r2;
730 struct mu magu;
731 magu.a = 0; // initialize "add" indicator
732 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000733 p = 63; // initialize p
734 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
735 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
736 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
737 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000738 do {
739 p = p + 1;
740 if (r1 >= nc - r1 ) {
741 q1 = 2*q1 + 1; // update q1
742 r1 = 2*r1 - nc; // update r1
743 }
744 else {
745 q1 = 2*q1; // update q1
746 r1 = 2*r1; // update r1
747 }
748 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000749 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000750 q2 = 2*q2 + 1; // update q2
751 r2 = 2*r2 + 1 - d; // update r2
752 }
753 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000754 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000755 q2 = 2*q2; // update q2
756 r2 = 2*r2 + 1; // update r2
757 }
758 delta = d - 1 - r2;
759 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
760 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000761 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000762 return magu;
763}
764
765/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
766/// return a DAG expression to select that will generate the same value by
767/// multiplying by a magic number. See:
768/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000769SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000770 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000771 ms magics = magic(d);
772 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000773 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000774 ISelDAG->getConstant(magics.m, MVT::i64));
775 // If d > 0 and m < 0, add the numerator
776 if (d > 0 && magics.m < 0)
777 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
778 // If d < 0 and m > 0, subtract the numerator.
779 if (d < 0 && magics.m > 0)
780 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
781 // Shift right algebraic if shift value is nonzero
782 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000783 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000784 ISelDAG->getConstant(magics.s, MVT::i64));
785 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000786 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000787 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
788 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
789}
790
791/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
792/// return a DAG expression to select that will generate the same value by
793/// multiplying by a magic number. See:
794/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000795SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000796 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000797 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
798 mu magics = magicu(d);
799 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000800 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000801 ISelDAG->getConstant(magics.m, MVT::i64));
802 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000803 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000804 ISelDAG->getConstant(magics.s, MVT::i64));
805 } else {
806 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000807 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000808 ISelDAG->getConstant(1, MVT::i64));
809 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000810 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000811 ISelDAG->getConstant(magics.s-1, MVT::i64));
812 }
813 return Q;
814}
815
Andrew Lenhartha565c272005-04-06 22:03:13 +0000816//From PPC32
817/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
818/// returns zero when the input is not exactly a power of two.
819static unsigned ExactLog2(uint64_t Val) {
820 if (Val == 0 || (Val & (Val-1))) return 0;
821 unsigned Count = 0;
822 while (Val != 1) {
823 Val >>= 1;
824 ++Count;
825 }
826 return Count;
827}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000828
829
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000830//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000831static const int IMM_LOW = -32768;
832static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000833static const int IMM_MULT = 65536;
834
835static long getUpper16(long l)
836{
837 long y = l / IMM_MULT;
838 if (l % IMM_MULT > IMM_HIGH)
839 ++y;
840 return y;
841}
842
843static long getLower16(long l)
844{
845 long h = getUpper16(l);
846 return l - h * IMM_MULT;
847}
848
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000849static unsigned GetRelVersion(unsigned opcode)
850{
851 switch (opcode) {
852 default: assert(0 && "unknown load or store"); return 0;
853 case Alpha::LDQ: return Alpha::LDQr;
854 case Alpha::LDS: return Alpha::LDSr;
855 case Alpha::LDT: return Alpha::LDTr;
856 case Alpha::LDL: return Alpha::LDLr;
857 case Alpha::LDBU: return Alpha::LDBUr;
858 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000859 case Alpha::STB: return Alpha::STBr;
860 case Alpha::STW: return Alpha::STWr;
861 case Alpha::STL: return Alpha::STLr;
862 case Alpha::STQ: return Alpha::STQr;
863 case Alpha::STS: return Alpha::STSr;
864 case Alpha::STT: return Alpha::STTr;
865
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000866 }
867}
Andrew Lenharth65838902005-02-06 16:22:15 +0000868
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000869void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000870{
871 unsigned Opc;
872 if (EnableAlphaFTOI) {
873 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
874 BuildMI(BB, Opc, 1, dst).addReg(src);
875 } else {
876 //The hard way:
877 // Spill the integer to memory and reload it from there.
878 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
879 MachineFunction *F = BB->getParent();
880 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
881
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000882 if (EnableAlphaLSMark)
883 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
884 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000885 Opc = isDouble ? Alpha::STT : Alpha::STS;
886 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000887
888 if (EnableAlphaLSMark)
889 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
890 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000891 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
892 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
893 }
894}
895
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000896void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000897{
898 unsigned Opc;
899 if (EnableAlphaFTOI) {
900 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
901 BuildMI(BB, Opc, 1, dst).addReg(src);
902 } else {
903 //The hard way:
904 // Spill the integer to memory and reload it from there.
905 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
906 MachineFunction *F = BB->getParent();
907 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
908
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000909 if (EnableAlphaLSMark)
910 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
911 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000912 Opc = isDouble ? Alpha::STQ : Alpha::STL;
913 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000914
915 if (EnableAlphaLSMark)
916 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
917 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000918 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
919 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
920 }
921}
922
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000923bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000924{
925 SDNode *Node = N.Val;
926 unsigned Opc, Tmp1, Tmp2, Tmp3;
927 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
928
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000929 bool rev = false;
930 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000931
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000932 switch (SetCC->getCondition()) {
933 default: Node->dump(); assert(0 && "Unknown FP comparison!");
934 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
935 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
936 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
937 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
938 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
939 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
940 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000941
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000942 ConstantFPSDNode *CN;
943 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
944 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
945 Tmp1 = Alpha::F31;
946 else
947 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000948
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000949 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
950 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
951 Tmp2 = Alpha::F31;
952 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000953 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000955 //Can only compare doubles, and dag won't promote for me
956 if (SetCC->getOperand(0).getValueType() == MVT::f32)
957 {
958 //assert(0 && "Setcc On float?\n");
959 std::cerr << "Setcc on float!\n";
960 Tmp3 = MakeReg(MVT::f64);
961 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
962 Tmp1 = Tmp3;
963 }
964 if (SetCC->getOperand(1).getValueType() == MVT::f32)
965 {
966 //assert (0 && "Setcc On float?\n");
967 std::cerr << "Setcc on float!\n";
968 Tmp3 = MakeReg(MVT::f64);
969 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
970 Tmp2 = Tmp3;
971 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000972
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000973 if (rev) std::swap(Tmp1, Tmp2);
974 //do the comparison
975 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
976 return inv;
977}
978
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000979//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000980void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000981{
982 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000983 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
984 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
985 { //Normal imm add
986 Reg = SelectExpr(N.getOperand(0));
987 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
988 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000989 }
990 Reg = SelectExpr(N);
991 offset = 0;
992 return;
993}
994
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000995void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000996{
997 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000998 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000999 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1000 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001001
Andrew Lenharth445171a2005-02-08 00:40:03 +00001002 Select(N.getOperand(0)); //chain
1003 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001004
Andrew Lenharth445171a2005-02-08 00:40:03 +00001005 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001006 {
1007 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1008 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1009 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001010 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1011 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001012 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001013
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001014 //Fix up CC
1015 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001016
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001017 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001018 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001019
Andrew Lenharth694c2982005-06-26 23:01:11 +00001020 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001021 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001022 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1023 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1024 case ISD::SETLT: Opc = Alpha::BLT; break;
1025 case ISD::SETLE: Opc = Alpha::BLE; break;
1026 case ISD::SETGT: Opc = Alpha::BGT; break;
1027 case ISD::SETGE: Opc = Alpha::BGE; break;
1028 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1029 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001030 //Technically you could have this CC
1031 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001032 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1033 case ISD::SETNE: Opc = Alpha::BNE; break;
1034 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001035 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001036 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1037 return;
1038 } else {
1039 unsigned Tmp1 = SelectExpr(CC);
1040 if (isNE)
1041 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1042 else
1043 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001044 return;
1045 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001046 } else { //FP
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001047 //Any comparison between 2 values should be codegened as an folded
1048 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001049 //for a cmp b: c = a - b;
1050 //a = b: c = 0
1051 //a < b: c < 0
1052 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001053
1054 bool invTest = false;
1055 unsigned Tmp3;
1056
1057 ConstantFPSDNode *CN;
1058 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1059 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1060 Tmp3 = SelectExpr(SetCC->getOperand(0));
1061 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1062 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1063 {
1064 Tmp3 = SelectExpr(SetCC->getOperand(1));
1065 invTest = true;
1066 }
1067 else
1068 {
1069 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1070 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1071 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1072 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1073 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1074 .addReg(Tmp1).addReg(Tmp2);
1075 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001076
1077 switch (SetCC->getCondition()) {
1078 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001079 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1080 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1081 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1082 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1083 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1084 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001085 }
1086 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001087 return;
1088 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001089 abort(); //Should never be reached
1090 } else {
1091 //Giveup and do the stupid thing
1092 unsigned Tmp1 = SelectExpr(CC);
1093 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1094 return;
1095 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001096 abort(); //Should never be reached
1097}
1098
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001099unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001100 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001101 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001102 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001103 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001104
1105 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001106 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001107 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001108
1109 unsigned &Reg = ExprMap[N];
1110 if (Reg) return Reg;
1111
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001112 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001113 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001114 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001115 else {
1116 // If this is a call instruction, make sure to prepare ALL of the result
1117 // values as well as the chain.
1118 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001119 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001120 else {
1121 Result = MakeReg(Node->getValueType(0));
1122 ExprMap[N.getValue(0)] = Result;
1123 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1124 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001125 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001126 }
1127 }
1128
Andrew Lenharth40831c52005-01-28 06:57:18 +00001129 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001130 default:
1131 Node->dump();
1132 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001133
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001134 case ISD::CTPOP:
1135 case ISD::CTTZ:
1136 case ISD::CTLZ:
1137 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1138 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1139 Tmp1 = SelectExpr(N.getOperand(0));
1140 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1141 return Result;
1142
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001143 case ISD::MULHU:
1144 Tmp1 = SelectExpr(N.getOperand(0));
1145 Tmp2 = SelectExpr(N.getOperand(1));
1146 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001147 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001148 case ISD::MULHS:
1149 {
1150 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1151 Tmp1 = SelectExpr(N.getOperand(0));
1152 Tmp2 = SelectExpr(N.getOperand(1));
1153 Tmp3 = MakeReg(MVT::i64);
1154 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1155 unsigned V1 = MakeReg(MVT::i64);
1156 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001157 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1158 .addReg(Tmp1);
1159 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1160 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001161 unsigned IRes = MakeReg(MVT::i64);
1162 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1163 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1164 return Result;
1165 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001166 case ISD::UNDEF: {
1167 BuildMI(BB, Alpha::IDEF, 0, Result);
1168 return Result;
1169 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001170
Andrew Lenharth032f2352005-02-22 21:59:48 +00001171 case ISD::DYNAMIC_STACKALLOC:
1172 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001173 if (Result != notIn)
1174 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001175 else
1176 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1177
1178 // FIXME: We are currently ignoring the requested alignment for handling
1179 // greater than the stack alignment. This will need to be revisited at some
1180 // point. Align = N.getOperand(2);
1181
1182 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1183 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1184 std::cerr << "Cannot allocate stack object with greater alignment than"
1185 << " the stack alignment yet!";
1186 abort();
1187 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001188
Andrew Lenharth032f2352005-02-22 21:59:48 +00001189 Select(N.getOperand(0));
1190 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1191 {
1192 if (CN->getValue() < 32000)
1193 {
1194 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1195 .addImm(-CN->getValue()).addReg(Alpha::R30);
1196 } else {
1197 Tmp1 = SelectExpr(N.getOperand(1));
1198 // Subtract size from stack pointer, thereby allocating some space.
1199 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1200 }
1201 } else {
1202 Tmp1 = SelectExpr(N.getOperand(1));
1203 // Subtract size from stack pointer, thereby allocating some space.
1204 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1205 }
1206
1207 // Put a pointer to the space into the result register, by copying the stack
1208 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001209 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001210 return Result;
1211
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001212 case ISD::ConstantPool:
1213 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1214 AlphaLowering.restoreGP(BB);
1215 Tmp2 = MakeReg(MVT::i64);
1216 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1217 .addReg(Alpha::R29);
1218 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1219 .addReg(Tmp2);
1220 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001221
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001222 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001223 BuildMI(BB, Alpha::LDA, 2, Result)
1224 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1225 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001226 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001227
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001228 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001229 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001230 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001231 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001232 {
1233 // Make sure we generate both values.
1234 if (Result != notIn)
1235 ExprMap[N.getValue(1)] = notIn; // Generate the token
1236 else
1237 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001238
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001239 SDOperand Chain = N.getOperand(0);
1240 SDOperand Address = N.getOperand(1);
1241 Select(Chain);
1242
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001243 bool fpext = true;
1244
Andrew Lenharth03824012005-02-07 05:55:55 +00001245 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001246 switch (Node->getValueType(0)) {
1247 default: Node->dump(); assert(0 && "Bad load!");
1248 case MVT::i64: Opc = Alpha::LDQ; break;
1249 case MVT::f64: Opc = Alpha::LDT; break;
1250 case MVT::f32: Opc = Alpha::LDS; break;
1251 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001252 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001253 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1254 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001255 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001256 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001257 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001258 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001259 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001260 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001261 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001262 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001263
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001264 int i, j, k;
1265 if (EnableAlphaLSMark)
1266 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1267 i, j, k);
1268
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001269 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1270 if (GASD && !GASD->getGlobal()->isExternal()) {
1271 Tmp1 = MakeReg(MVT::i64);
1272 AlphaLowering.restoreGP(BB);
1273 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1274 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1275 if (EnableAlphaLSMark)
1276 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1277 .addImm(getUID());
1278 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1279 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001280 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001281 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001282 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001283 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001284 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1285 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001286 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001287 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1288 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001289 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1290 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1291 } else if(Address.getOpcode() == ISD::FrameIndex) {
1292 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001293 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1294 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001295 BuildMI(BB, Opc, 2, Result)
1296 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1297 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001298 } else {
1299 long offset;
1300 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001301 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001302 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1303 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001304 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1305 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001306 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001307 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001308
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001309 case ISD::GlobalAddress:
1310 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001311 has_sym = true;
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001312
1313 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001314
1315 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001316 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001317 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001318
1319 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001320 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1321 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001322 return Result;
1323
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001324 case ISD::ExternalSymbol:
1325 AlphaLowering.restoreGP(BB);
1326 has_sym = true;
1327
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001328 Reg = Result = MakeReg(MVT::i64);
1329
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001330 if (EnableAlphaLSMark)
1331 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1332 .addImm(getUID());
1333
1334 BuildMI(BB, Alpha::LDQl, 2, Result)
1335 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1336 .addReg(Alpha::R29);
1337 return Result;
1338
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001339 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001340 case ISD::CALL:
1341 {
1342 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001343
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001344 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001345 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001346
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001347 //grab the arguments
1348 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001349 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001351 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001352
Andrew Lenharth684f2292005-01-30 00:35:27 +00001353 //in reg args
1354 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001355 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001356 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 Alpha::F19, Alpha::F20, Alpha::F21};
1360 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001361 default:
1362 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001363 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001364 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 N.getOperand(i+2).getValueType() << "\n";
1366 assert(0 && "Unknown value type for call");
1367 case MVT::i1:
1368 case MVT::i8:
1369 case MVT::i16:
1370 case MVT::i32:
1371 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001372 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1373 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001374 break;
1375 case MVT::f32:
1376 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001377 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1378 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001379 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001380 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001381 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001382 //in mem args
1383 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 {
1385 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001386 default:
1387 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001388 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001389 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001390 N.getOperand(i+2).getValueType() << "\n";
1391 assert(0 && "Unknown value type for call");
1392 case MVT::i1:
1393 case MVT::i8:
1394 case MVT::i16:
1395 case MVT::i32:
1396 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001397 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1398 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001399 break;
1400 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001401 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1402 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001403 break;
1404 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001405 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1406 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001407 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001408 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001409 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001410 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001411 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1412 if (GASD && !GASD->getGlobal()->isExternal()) {
1413 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001414 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001415 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1416 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 } else {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +00001418 //Must always reread relocation table before a call
1419 if (GASD)
1420 ExprMap.erase(N.getOperand(1));
1421
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001422 //no need to restore GP as we are doing an indirect call
1423 Tmp1 = SelectExpr(N.getOperand(1));
1424 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1425 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1426 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001428 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001429
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001430 switch (Node->getValueType(0)) {
1431 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001432 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001433 case MVT::i1:
1434 case MVT::i8:
1435 case MVT::i16:
1436 case MVT::i32:
1437 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001438 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1439 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001440 case MVT::f32:
1441 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001442 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1443 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001444 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001445 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001446 }
1447
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001448 case ISD::SIGN_EXTEND_INREG:
1449 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001450 //do SDIV opt for all levels of ints if not dividing by a constant
1451 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1452 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001453 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001454 unsigned Tmp4 = MakeReg(MVT::f64);
1455 unsigned Tmp5 = MakeReg(MVT::f64);
1456 unsigned Tmp6 = MakeReg(MVT::f64);
1457 unsigned Tmp7 = MakeReg(MVT::f64);
1458 unsigned Tmp8 = MakeReg(MVT::f64);
1459 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001460
1461 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1462 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1463 MoveInt2FP(Tmp1, Tmp4, true);
1464 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001465 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1466 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1467 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1468 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001469 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001470 return Result;
1471 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001472
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001473 //Alpha has instructions for a bunch of signed 32 bit stuff
1474 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001475 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 switch (N.getOperand(0).getOpcode()) {
1477 case ISD::ADD:
1478 case ISD::SUB:
1479 case ISD::MUL:
1480 {
1481 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1482 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1483 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001484 ConstantSDNode* CSD = NULL;
1485 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1486 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1487 (CSD->getValue() == 2 || CSD->getValue() == 3))
1488 {
1489 bool use4 = CSD->getValue() == 2;
1490 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1491 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1492 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1493 2,Result).addReg(Tmp1).addReg(Tmp2);
1494 }
1495 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1496 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1497 (CSD->getValue() == 2 || CSD->getValue() == 3))
1498 {
1499 bool use4 = CSD->getValue() == 2;
1500 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1501 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1502 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1503 }
1504 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001505 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1506 { //Normal imm add/sub
1507 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001508 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001509 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1510 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001511 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001512 else
1513 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001514 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001515 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001516 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001517 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1518 }
1519 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001520 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 default: break; //Fall Though;
1522 }
1523 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001524 Tmp1 = SelectExpr(N.getOperand(0));
1525 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001526 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001527 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001528 {
1529 default:
1530 Node->dump();
1531 assert(0 && "Sign Extend InReg not there yet");
1532 break;
1533 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001534 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001535 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001536 break;
1537 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001538 case MVT::i16:
1539 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1540 break;
1541 case MVT::i8:
1542 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1543 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001544 case MVT::i1:
1545 Tmp2 = MakeReg(MVT::i64);
1546 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001547 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001548 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001549 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001550 return Result;
1551 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001552
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001553 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001554 {
1555 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1556 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001557 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001558 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001559
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001560 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001561 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001562 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001563 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001564
1565 switch (SetCC->getCondition()) {
1566 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001567 case ISD::SETEQ:
1568 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001569 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001570 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001571 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001572 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1573 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1574 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001576 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1577 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001578 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001579 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1580 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001581 case ISD::SETNE: {//Handle this one special
1582 //std::cerr << "Alpha does not have a setne.\n";
1583 //abort();
1584 Tmp1 = SelectExpr(N.getOperand(0));
1585 Tmp2 = SelectExpr(N.getOperand(1));
1586 Tmp3 = MakeReg(MVT::i64);
1587 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001588 //Remeber we have the Inv for this CC
1589 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001590 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001591 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001592 return Result;
1593 }
1594 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001595 if (dir == 1) {
1596 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001597 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001598 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1599 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1600 } else {
1601 Tmp2 = SelectExpr(N.getOperand(1));
1602 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1603 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001604 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001605 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001606 Tmp2 = SelectExpr(N.getOperand(0));
1607 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001608 }
1609 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001610 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001611 Tmp1 = MakeReg(MVT::f64);
1612 bool inv = SelectFPSetCC(N, Tmp1);
1613
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001614 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001615 Tmp2 = MakeReg(MVT::i64);
1616 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001617 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001618 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001619 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001620 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001621 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001622 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001624 case ISD::CopyFromReg:
1625 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001626 ++count_ins;
1627
Andrew Lenharth40831c52005-01-28 06:57:18 +00001628 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001629 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001630 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001631 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001632 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001633
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001634 SDOperand Chain = N.getOperand(0);
1635
1636 Select(Chain);
1637 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1638 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001639 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001640 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1641 else
1642 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001643 return Result;
1644 }
1645
Misha Brukman4633f1c2005-04-21 23:13:11 +00001646 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001647 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001648 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001649 //Match Not
1650 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001651 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001652 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001653 Tmp1 = SelectExpr(N.getOperand(0));
1654 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1655 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001656 }
1657 //Fall through
1658 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001659 //handle zap
1660 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1661 {
1662 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1663 unsigned int build = 0;
1664 for(int i = 0; i < 8; ++i)
1665 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001666 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001667 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001668 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001669 { build = 0; break; }
1670 k >>= 8;
1671 }
1672 if (build)
1673 {
1674 Tmp1 = SelectExpr(N.getOperand(0));
1675 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1676 return Result;
1677 }
1678 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001679 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001680 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001681 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001682 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001683 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
1684 == -1) {
1685 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001686 case ISD::AND: Opc = Alpha::BIC; break;
1687 case ISD::OR: Opc = Alpha::ORNOT; break;
1688 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001689 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001690 Tmp1 = SelectExpr(N.getOperand(1));
1691 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1692 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1693 return Result;
1694 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001695 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001696 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001697 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001698 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1699 == -1) {
1700 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001701 case ISD::AND: Opc = Alpha::BIC; break;
1702 case ISD::OR: Opc = Alpha::ORNOT; break;
1703 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001704 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001705 Tmp1 = SelectExpr(N.getOperand(0));
1706 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1707 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1708 return Result;
1709 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001710 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001711 case ISD::SHL:
1712 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001713 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001714 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001715 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001716 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001717 {
1718 switch(opcode) {
1719 case ISD::AND: Opc = Alpha::ANDi; break;
1720 case ISD::OR: Opc = Alpha::BISi; break;
1721 case ISD::XOR: Opc = Alpha::XORi; break;
1722 case ISD::SHL: Opc = Alpha::SLi; break;
1723 case ISD::SRL: Opc = Alpha::SRLi; break;
1724 case ISD::SRA: Opc = Alpha::SRAi; break;
1725 case ISD::MUL: Opc = Alpha::MULQi; break;
1726 };
1727 Tmp1 = SelectExpr(N.getOperand(0));
1728 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1729 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1730 } else {
1731 switch(opcode) {
1732 case ISD::AND: Opc = Alpha::AND; break;
1733 case ISD::OR: Opc = Alpha::BIS; break;
1734 case ISD::XOR: Opc = Alpha::XOR; break;
1735 case ISD::SHL: Opc = Alpha::SL; break;
1736 case ISD::SRL: Opc = Alpha::SRL; break;
1737 case ISD::SRA: Opc = Alpha::SRA; break;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001738 case ISD::MUL:
1739 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
1740 : Alpha::MULQ;
1741 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001742 };
1743 Tmp1 = SelectExpr(N.getOperand(0));
1744 Tmp2 = SelectExpr(N.getOperand(1));
1745 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1746 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001747 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001748
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001749 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001750 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001751 if (isFP) {
1752 ConstantFPSDNode *CN;
1753 if (opcode == ISD::ADD)
1754 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1755 else
1756 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1757 if (opcode == ISD::SUB
1758 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1759 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1760 {
1761 Tmp2 = SelectExpr(N.getOperand(1));
1762 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1763 } else {
1764 Tmp1 = SelectExpr(N.getOperand(0));
1765 Tmp2 = SelectExpr(N.getOperand(1));
1766 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 }
1768 return Result;
1769 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001770 bool isAdd = opcode == ISD::ADD;
1771
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001772 //first check for Scaled Adds and Subs!
1773 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001774 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001775 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001776 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1777 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001778 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001779 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001780 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001781 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1782 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1783 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001784 else {
1785 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001786 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1787 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001788 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001789 }
1790 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001791 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001792 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1793 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001794 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001795 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001796 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001797 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1798 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1799 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001800 else {
1801 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001802 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001803 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001804 }
1805 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001806 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1807 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001808 { //Normal imm add/sub
1809 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1810 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001811 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001812 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001813 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001814 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1815 CSD->getSignExtended() <= 32767 &&
1816 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001817 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001818 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001819 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001820 if (!isAdd)
1821 Tmp2 = -Tmp2;
1822 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001823 }
1824 //give up and do the operation
1825 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001826 //Normal add/sub
1827 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1828 Tmp1 = SelectExpr(N.getOperand(0));
1829 Tmp2 = SelectExpr(N.getOperand(1));
1830 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1831 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001832 return Result;
1833 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001834
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001835 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001836 if (isFP) {
1837 Tmp1 = SelectExpr(N.getOperand(0));
1838 Tmp2 = SelectExpr(N.getOperand(1));
1839 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1840 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001841 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001842 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001843 ConstantSDNode* CSD;
1844 //check if we can convert into a shift!
1845 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1846 (int64_t)CSD->getSignExtended() != 0 &&
1847 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1848 {
1849 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1850 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001851 if (k == 1)
1852 Tmp2 = Tmp1;
1853 else
1854 {
1855 Tmp2 = MakeReg(MVT::i64);
1856 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1857 }
1858 Tmp3 = MakeReg(MVT::i64);
1859 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1860 unsigned Tmp4 = MakeReg(MVT::i64);
1861 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1862 if ((int64_t)CSD->getSignExtended() > 0)
1863 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1864 else
1865 {
1866 unsigned Tmp5 = MakeReg(MVT::i64);
1867 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1868 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1869 }
1870 return Result;
1871 }
1872 }
1873 //Else fall through
1874
1875 case ISD::UDIV:
1876 {
1877 ConstantSDNode* CSD;
1878 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1879 ((int64_t)CSD->getSignExtended() >= 2 ||
1880 (int64_t)CSD->getSignExtended() <= -2))
1881 {
1882 // If this is a divide by constant, we can emit code using some magic
1883 // constants to implement it as a multiply instead.
1884 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001885 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001886 return SelectExpr(BuildSDIVSequence(N));
1887 else
1888 return SelectExpr(BuildUDIVSequence(N));
1889 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001890 }
1891 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001892 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001893 case ISD::SREM: {
1894 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001895 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001896 case ISD::UREM: opstr = "__remqu"; break;
1897 case ISD::SREM: opstr = "__remq"; break;
1898 case ISD::UDIV: opstr = "__divqu"; break;
1899 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001900 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001901 Tmp1 = SelectExpr(N.getOperand(0));
1902 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001903 SDOperand Addr =
1904 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1905 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001906 //set up regs explicitly (helps Reg alloc)
1907 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001908 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001909 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1910 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001911 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001912 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001913 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001914
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001915 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001916 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001917 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001918 assert (DestType == MVT::i64 && "only quads can be loaded to");
1919 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001920 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001921 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001922 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001923 {
1924 Tmp2 = MakeReg(MVT::f64);
1925 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1926 Tmp1 = Tmp2;
1927 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001928 Tmp2 = MakeReg(MVT::f64);
1929 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001930 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001931
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001932 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001933 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001934
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001935 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001936 if (isFP) {
1937 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1938 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1939 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1940
1941 SDOperand CC = N.getOperand(0);
1942 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1943
1944 if (CC.getOpcode() == ISD::SETCC &&
1945 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1946 { //FP Setcc -> Select yay!
1947
1948
1949 //for a cmp b: c = a - b;
1950 //a = b: c = 0
1951 //a < b: c < 0
1952 //a > b: c > 0
1953
1954 bool invTest = false;
1955 unsigned Tmp3;
1956
1957 ConstantFPSDNode *CN;
1958 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1959 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1960 Tmp3 = SelectExpr(SetCC->getOperand(0));
1961 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1962 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1963 {
1964 Tmp3 = SelectExpr(SetCC->getOperand(1));
1965 invTest = true;
1966 }
1967 else
1968 {
1969 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1970 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1971 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1972 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1973 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1974 .addReg(Tmp1).addReg(Tmp2);
1975 }
1976
1977 switch (SetCC->getCondition()) {
1978 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1979 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1980 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1981 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1982 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1983 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1984 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1985 }
1986 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1987 return Result;
1988 }
1989 else
1990 {
1991 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1992 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1993 .addReg(Tmp1);
1994// // Spill the cond to memory and reload it from there.
1995// unsigned Tmp4 = MakeReg(MVT::f64);
1996// MoveIntFP(Tmp1, Tmp4, true);
1997// //now ideally, we don't have to do anything to the flag...
1998// // Get the condition into the zero flag.
1999// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
2000 return Result;
2001 }
2002 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002003 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
2004 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002005 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002006 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2007 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002008 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002009 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002010
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002011 SDOperand CC = N.getOperand(0);
2012 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2013
Misha Brukman4633f1c2005-04-21 23:13:11 +00002014 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002015 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2016 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002018 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2019 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002020 bool inv = SelectFPSetCC(CC, Tmp1);
2021 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2022 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2023 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002024 }
2025 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002026 //Int SetCC -> Select
2027 //Dropping the CC is only useful if we are comparing to 0
2028 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002029 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002030 {
2031 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002032 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002033 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002034
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002035 //Fix up CC
2036 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002037 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002038 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002039
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002040 //Choose the CMOV
2041 switch (cCode) {
2042 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002043 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2044 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2045 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2046 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2047 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2048 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2049 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2050 //Technically you could have this CC
2051 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2052 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2053 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002054 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002055 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002056
Andrew Lenharth694c2982005-06-26 23:01:11 +00002057 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002058 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2059 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002060 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002061 .addReg(Tmp1);
2062 } else {
2063 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2064 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2065 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2066 }
2067 return Result;
2068 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002069 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002070 }
2071 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002072 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2073 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002074 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2075 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002076
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002077 return Result;
2078 }
2079
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002080 case ISD::Constant:
2081 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002082 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002083 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002084 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002085 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002086 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2087 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2088 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002089 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2090 .addReg(Alpha::R31);
Misha Brukman7847fca2005-04-22 17:54:37 +00002091 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002092 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002093 else {
2094 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002095 ConstantUInt *C =
2096 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002097 unsigned CPI = CP->getConstantPoolIndex(C);
2098 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002099 has_sym = true;
2100 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002101 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2102 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002103 if (EnableAlphaLSMark)
2104 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2105 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002106 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2107 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002108 }
2109 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002110 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002111 case ISD::FNEG:
2112 if(ISD::FABS == N.getOperand(0).getOpcode())
2113 {
2114 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2115 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2116 } else {
2117 Tmp1 = SelectExpr(N.getOperand(0));
2118 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2119 }
2120 return Result;
2121
2122 case ISD::FABS:
2123 Tmp1 = SelectExpr(N.getOperand(0));
2124 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2125 return Result;
2126
2127 case ISD::FP_ROUND:
2128 assert (DestType == MVT::f32 &&
2129 N.getOperand(0).getValueType() == MVT::f64 &&
2130 "only f64 to f32 conversion supported here");
2131 Tmp1 = SelectExpr(N.getOperand(0));
2132 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
2133 return Result;
2134
2135 case ISD::FP_EXTEND:
2136 assert (DestType == MVT::f64 &&
2137 N.getOperand(0).getValueType() == MVT::f32 &&
2138 "only f32 to f64 conversion supported here");
2139 Tmp1 = SelectExpr(N.getOperand(0));
2140 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
2141 return Result;
2142
2143 case ISD::ConstantFP:
2144 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2145 if (CN->isExactlyValue(+0.0)) {
2146 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2147 .addReg(Alpha::F31);
2148 } else if ( CN->isExactlyValue(-0.0)) {
2149 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2150 .addReg(Alpha::F31);
2151 } else {
2152 abort();
2153 }
2154 }
2155 return Result;
2156
2157 case ISD::SINT_TO_FP:
2158 {
2159 assert (N.getOperand(0).getValueType() == MVT::i64
2160 && "only quads can be loaded from");
2161 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2162 Tmp2 = MakeReg(MVT::f64);
2163 MoveInt2FP(Tmp1, Tmp2, true);
2164 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
2165 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2166 return Result;
2167 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002168 }
2169
2170 return 0;
2171}
2172
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002173void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002174 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002175 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002176
Nate Begeman85fdeb22005-03-24 04:39:54 +00002177 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002178 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002179
2180 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002181
Andrew Lenharth760270d2005-02-07 23:02:23 +00002182 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002183
2184 default:
2185 Node->dump(); std::cerr << "\n";
2186 assert(0 && "Node not handled yet!");
2187
2188 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002189 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002190 return;
2191 }
2192
2193 case ISD::BR: {
2194 MachineBasicBlock *Dest =
2195 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2196
2197 Select(N.getOperand(0));
2198 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2199 return;
2200 }
2201
2202 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002203 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002204 Select(N.getOperand(0));
2205 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2206 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002207
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002208 case ISD::EntryToken: return; // Noop
2209
2210 case ISD::TokenFactor:
2211 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2212 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002214 //N.Val->dump(); std::cerr << "\n";
2215 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002216
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217 return;
2218
2219 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002220 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002221 Select(N.getOperand(0));
2222 Tmp1 = SelectExpr(N.getOperand(1));
2223 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002224
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002225 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002226 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002227 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002228 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2229 else
2230 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002231 }
2232 return;
2233
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002234 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002235 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002236 switch (N.getNumOperands()) {
2237 default:
2238 std::cerr << N.getNumOperands() << "\n";
2239 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2240 std::cerr << N.getOperand(i).getValueType() << "\n";
2241 Node->dump();
2242 assert(0 && "Unknown return instruction!");
2243 case 2:
2244 Select(N.getOperand(0));
2245 Tmp1 = SelectExpr(N.getOperand(1));
2246 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002247 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002248 assert(0 && "All other types should have been promoted!!");
2249 case MVT::f64:
2250 case MVT::f32:
2251 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2252 break;
2253 case MVT::i32:
2254 case MVT::i64:
2255 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2256 break;
2257 }
2258 break;
2259 case 1:
2260 Select(N.getOperand(0));
2261 break;
2262 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002263 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002264 AlphaLowering.restoreRA(BB);
2265 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002266 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002267
Misha Brukman4633f1c2005-04-21 23:13:11 +00002268 case ISD::TRUNCSTORE:
2269 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002270 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002271 SDOperand Chain = N.getOperand(0);
2272 SDOperand Value = N.getOperand(1);
2273 SDOperand Address = N.getOperand(2);
2274 Select(Chain);
2275
2276 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002277
2278 if (opcode == ISD::STORE) {
2279 switch(Value.getValueType()) {
2280 default: assert(0 && "unknown Type in store");
2281 case MVT::i64: Opc = Alpha::STQ; break;
2282 case MVT::f64: Opc = Alpha::STT; break;
2283 case MVT::f32: Opc = Alpha::STS; break;
2284 }
2285 } else { //ISD::TRUNCSTORE
2286 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2287 default: assert(0 && "unknown Type in store");
2288 case MVT::i1: //FIXME: DAG does not promote this load
2289 case MVT::i8: Opc = Alpha::STB; break;
2290 case MVT::i16: Opc = Alpha::STW; break;
2291 case MVT::i32: Opc = Alpha::STL; break;
2292 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002293 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002294
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002295 int i, j, k;
2296 if (EnableAlphaLSMark)
2297 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
2298 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002299
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002300 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2301 if (GASD && !GASD->getGlobal()->isExternal()) {
2302 Tmp2 = MakeReg(MVT::i64);
2303 AlphaLowering.restoreGP(BB);
2304 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2305 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2306 if (EnableAlphaLSMark)
2307 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2308 .addImm(getUID());
2309 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2310 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002311 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002312 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002313 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2314 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002315 BuildMI(BB, Opc, 3).addReg(Tmp1)
2316 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2317 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002318 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002319 long offset;
2320 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002321 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002322 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2323 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002324 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2325 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002326 return;
2327 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002328
2329 case ISD::EXTLOAD:
2330 case ISD::SEXTLOAD:
2331 case ISD::ZEXTLOAD:
2332 case ISD::LOAD:
2333 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002334 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002335 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002336 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002337 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002338 SelectExpr(N);
2339 return;
2340
Chris Lattner16cd04d2005-05-12 23:24:06 +00002341 case ISD::CALLSEQ_START:
2342 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002343 Select(N.getOperand(0));
2344 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002345
Chris Lattner16cd04d2005-05-12 23:24:06 +00002346 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002347 Alpha::ADJUSTSTACKUP;
2348 BuildMI(BB, Opc, 1).addImm(Tmp1);
2349 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002350
2351 case ISD::PCMARKER:
2352 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002353 BuildMI(BB, Alpha::PCLABEL, 2)
2354 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002355 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002356 }
2357 assert(0 && "Should not be reached!");
2358}
2359
2360
2361/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2362/// into a machine code representation using pattern matching and a machine
2363/// description file.
2364///
2365FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002366 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002367}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002368