blob: 5fa16d0e694963c0bcbe07aa8d5ca8f6e4484ee9 [file] [log] [blame]
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha -===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
34namespace {
35 class AlphaTargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
37 unsigned GP; //GOT vreg
38 public:
39 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
42 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
43
44 setOperationAction(ISD::EXTLOAD , MVT::i1 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000045
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000048
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
50 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
51 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
52
Andrew Lenharth02981182005-01-26 01:24:38 +000053 setOperationAction(ISD::ZERO_EXTEND_INREG, MVT::i1, Expand);
54 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
55
56
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 computeRegisterProperties();
58
Andrew Lenharth304d0f32005-01-22 23:41:55 +000059 // addLegalFPImmediate(+0.0); // FLD0
60 // addLegalFPImmediate(+1.0); // FLD1
61 // addLegalFPImmediate(-0.0); // FLD0/FCHS
62 // addLegalFPImmediate(-1.0); // FLD1/FCHS
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
72 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
74 ArgListTy &Args, SelectionDAG &DAG);
75
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
86
87 void restoreGP(MachineBasicBlock* BB)
88 {
89 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
90 }
91 };
92}
93
94//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
95
96//For now, just use variable size stack frame format
97
98//In a standard call, the first six items are passed in registers $16
99//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
100//of argument-to-register correspondence.) The remaining items are
101//collected in a memory argument list that is a naturally aligned
102//array of quadwords. In a standard call, this list, if present, must
103//be passed at 0(SP).
104//7 ... n 0(SP) ... (n-7)*8(SP)
105
106std::vector<SDOperand>
107AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
108{
109 std::vector<SDOperand> ArgValues;
110
111 // //#define FP $15
112 // //#define RA $26
113 // //#define PV $27
114 // //#define GP $29
115 // //#define SP $30
116
117 // assert(0 && "TODO");
118 MachineFunction &MF = DAG.getMachineFunction();
119 MachineFrameInfo *MFI = MF.getFrameInfo();
120
121 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
122 MachineBasicBlock& BB = MF.front();
123
124 //Handle the return address
125 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
126
127 unsigned args[] = {Alpha::R16, Alpha::R17, Alpha::R18,
128 Alpha::R19, Alpha::R20, Alpha::R21};
129 std::vector<unsigned> argVreg;
130
131 int count = 0;
132 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
133 {
134 ++count;
135 assert(count <= 6 && "More than 6 args not supported");
136 assert(getValueType(I->getType()) != MVT::f64 && "No floats yet");
137 BuildMI(&BB, Alpha::IDEF, 0, args[count - 1]);
138 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
139 }
140
141 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
142 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
143 count = 0;
144 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
145 {
146 BuildMI(&BB, Alpha::BIS, 2, argVreg[count]).addReg(args[count]).addReg(args[count]);
147
148 SDOperand argt, newroot;
149 switch (getValueType(I->getType()))
150 {
151 case MVT::i64:
152 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
153 break;
154 case MVT::i32:
155 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i32, DAG.getRoot());
156 break;
157 default:
158 newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
159 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
160 }
161 DAG.setRoot(newroot.getValue(1));
162 ArgValues.push_back(argt);
163 ++count;
164 }
165 return ArgValues;
166}
167
168std::pair<SDOperand, SDOperand>
169AlphaTargetLowering::LowerCallTo(SDOperand Chain,
170 const Type *RetTy, SDOperand Callee,
171 ArgListTy &Args, SelectionDAG &DAG) {
172 int NumBytes = 0;
173 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
174 DAG.getConstant(NumBytes, getPointerTy()));
175 std::vector<SDOperand> args_to_use;
176 for (unsigned i = 0, e = Args.size(); i != e; ++i)
177 {
178 switch (getValueType(Args[i].second)) {
179 default: assert(0 && "Unexpected ValueType for argument!");
180 case MVT::i1:
181 case MVT::i8:
182 case MVT::i16:
183 case MVT::i32:
184 // Promote the integer to 64 bits. If the input type is signed use a
185 // sign extend, otherwise use a zero extend.
186 if (Args[i].second->isSigned())
187 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
188 else
189 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
190 break;
191 case MVT::i64:
192 break;
193 }
194 args_to_use.push_back(Args[i].first);
195 }
196
197 std::vector<MVT::ValueType> RetVals;
198 MVT::ValueType RetTyVT = getValueType(RetTy);
199 if (RetTyVT != MVT::isVoid)
200 RetVals.push_back(RetTyVT);
201 RetVals.push_back(MVT::Other);
202
203 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
204 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
205 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
206 DAG.getConstant(NumBytes, getPointerTy()));
207 return std::make_pair(TheCall, Chain);
208}
209
210std::pair<SDOperand, SDOperand>
211AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
212 //vastart just returns the address of the VarArgsFrameIndex slot.
213 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
214}
215
216std::pair<SDOperand,SDOperand> AlphaTargetLowering::
217LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
218 const Type *ArgTy, SelectionDAG &DAG) {
219 abort();
220}
221
222
223std::pair<SDOperand, SDOperand> AlphaTargetLowering::
224LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
225 SelectionDAG &DAG) {
226 abort();
227}
228
229
230
231
232
233namespace {
234
235 //===--------------------------------------------------------------------===//
236 /// ISel - Alpha specific code to select Alpha machine instructions for
237 /// SelectionDAG operations.
238 ///
239 class ISel : public SelectionDAGISel {
240
241 /// AlphaLowering - This object fully describes how to lower LLVM code to an
242 /// Alpha-specific SelectionDAG.
243 AlphaTargetLowering AlphaLowering;
244
245
246 /// ExprMap - As shared expressions are codegen'd, we keep track of which
247 /// vreg the value is produced in, so we only emit one copy of each compiled
248 /// tree.
249 std::map<SDOperand, unsigned> ExprMap;
250 std::set<SDOperand> LoweredTokens;
251
252 public:
253 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
254 }
255
256 /// InstructionSelectBasicBlock - This callback is invoked by
257 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
258 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
259 // Codegen the basic block.
260 Select(DAG.getRoot());
261
262 // Clear state used for selection.
263 ExprMap.clear();
264 LoweredTokens.clear();
265 }
266
267 unsigned SelectExpr(SDOperand N);
268 void Select(SDOperand N);
269 };
270}
271
272unsigned ISel::SelectExpr(SDOperand N) {
273 unsigned Result;
274 unsigned Tmp1, Tmp2, Tmp3;
275 unsigned Opc = 0;
276
277 SDNode *Node = N.Val;
278
279 unsigned &Reg = ExprMap[N];
280 if (Reg) return Reg;
281
282 if (N.getOpcode() != ISD::CALL)
283 Reg = Result = (N.getValueType() != MVT::Other) ?
284 MakeReg(N.getValueType()) : 1;
285 else {
286 // If this is a call instruction, make sure to prepare ALL of the result
287 // values as well as the chain.
288 if (Node->getNumValues() == 1)
289 Reg = Result = 1; // Void call, just a chain.
290 else {
291 Result = MakeReg(Node->getValueType(0));
292 ExprMap[N.getValue(0)] = Result;
293 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
294 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
295 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
296 }
297 }
298
299 switch (N.getOpcode()) {
300 default:
301 Node->dump();
302 assert(0 && "Node not handled!\n");
303
304 case ISD::FrameIndex:
305 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
306 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp1 * 8).addReg(Alpha::R30);
307 return Result;
308
309 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000310 // Make sure we generate both values.
311 if (Result != 1)
312 ExprMap[N.getValue(1)] = 1; // Generate the token
313 else
314 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
315
316 Select(Node->getOperand(0)); // chain
317 Tmp1 = SelectExpr(Node->getOperand(1));
318
319 switch(Node->getValueType(0)) {
320 default: assert(0 && "Unknown type to sign extend to.");
321 case MVT::i64:
322 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
323 default:
Andrew Lenharthd279b412005-01-25 19:58:40 +0000324 std::cerr << cast<MVTSDNode>(Node)->getExtraValueType()
325 << "(i1 is " << MVT::i1
326 << " i8 is " << MVT::i8
327 << " i16 is " << MVT::i16
328 << " i32 is " << MVT::i32
329 << " i64 is " << MVT::i64
330 << ")\n";
331 assert(0 && "Bad extend load!");
332 case MVT::i64:
333 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
334 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000335 case MVT::i32:
336 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
337 break;
338 case MVT::i16:
339 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
340 break;
341 case MVT::i8:
Andrew Lenharthd279b412005-01-25 19:58:40 +0000342 case MVT::i1: //FIXME: DAG does not expand i8??
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000343 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
344 break;
345 }
346 break;
347 }
348 return Result;
349
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000350 case ISD::SEXTLOAD:
351 // Make sure we generate both values.
352 if (Result != 1)
353 ExprMap[N.getValue(1)] = 1; // Generate the token
354 else
355 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
356
357 Select(Node->getOperand(0)); // chain
358 Tmp1 = SelectExpr(Node->getOperand(1));
359 switch(Node->getValueType(0)) {
360 default: assert(0 && "Unknown type to sign extend to.");
361 case MVT::i64:
362 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
363 default:
364 assert(0 && "Bad sign extend!");
365 case MVT::i32:
366 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
367 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000368// case MVT::i16:
369// BuildMI(BB, Alpha::LDW, 2, Result).addImm(0).addReg(Tmp1);
370// break;
371// case MVT::i8:
372// BuildMI(BB, Alpha::LDB, 2, Result).addImm(0).addReg(Tmp1);
373// break;
374 }
375 break;
376 }
377 return Result;
378
379 case ISD::ZEXTLOAD:
380 // Make sure we generate both values.
381 if (Result != 1)
382 ExprMap[N.getValue(1)] = 1; // Generate the token
383 else
384 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
385
386 Select(Node->getOperand(0)); // chain
387 Tmp1 = SelectExpr(Node->getOperand(1));
388 switch(Node->getValueType(0)) {
389 default: assert(0 && "Unknown type to zero extend to.");
390 case MVT::i64:
391 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
392 default:
393 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000394 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000395 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000396 break;
397 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000398 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 break;
400 }
401 break;
402 }
403 return Result;
404
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000405
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000406 case ISD::GlobalAddress:
407 AlphaLowering.restoreGP(BB);
408 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
409 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
410 return Result;
411
412 case ISD::CALL:
413 {
414 Select(N.getOperand(0));
415
416 // The chain for this call is now lowered.
417 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
418
419 //grab the arguments
420 std::vector<unsigned> argvregs;
421 assert(Node->getNumOperands() < 8 && "Only 6 args supported");
422 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
423 {
424 argvregs.push_back(SelectExpr(N.getOperand(i)));
425 }
426 for(int i = 0, e = argvregs.size(); i < e; ++i)
427 {
428 unsigned args[] = {Alpha::R16, Alpha::R17, Alpha::R18,
429 Alpha::R19, Alpha::R20, Alpha::R21};
430
431 BuildMI(BB, Alpha::BIS, 2, args[i]).addReg(argvregs[i]).addReg(argvregs[i]);
432 }
433
434 //build the right kind of call
435 if (GlobalAddressSDNode *GASD =
436 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
437 {
438 Select(N.getOperand(0));
439 AlphaLowering.restoreGP(BB);
440 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
441 }
442 else if (ExternalSymbolSDNode *ESSDN =
443 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
444 {
445 Select(N.getOperand(0));
446 AlphaLowering.restoreGP(BB);
447 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
448 }
449 else {
450 Select(N.getOperand(0));
451 Tmp1 = SelectExpr(N.getOperand(1));
452 BuildMI(BB, Alpha::CALL, 1).addReg(Tmp1);
453 AlphaLowering.restoreGP(BB);
454 }
455
456 //push the result into a virtual register
457 // if (Result != 1)
458 // BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
459
460 switch (Node->getValueType(0)) {
461 default: assert(0 && "Unknown value type for call result!");
462 case MVT::Other: return 1;
463 case MVT::i1:
464 case MVT::i8:
465 case MVT::i16:
466 case MVT::i32:
467 case MVT::i64:
468 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
469 break;
470 }
471 return Result+N.ResNo;
472 }
473
474 case ISD::SIGN_EXTEND:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000475 case ISD::SIGN_EXTEND_INREG:
476 {
477 Tmp1 = SelectExpr(N.getOperand(0));
478 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
479 std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
480 switch(MVN->getExtraValueType())
481 {
482 default:
483 assert(0 && "Sign Extend InReg not there yet");
484 break;
485 case MVT::i32:
486 {
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000487 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000488 break;
489 }
490 case MVT::i16:
491 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
492 break;
493 case MVT::i8:
494 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
495 break;
496 }
497 return Result;
498 }
499 case ISD::ZERO_EXTEND_INREG:
500 {
501 Tmp1 = SelectExpr(N.getOperand(0));
502 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
503 std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
504 switch(MVN->getExtraValueType())
505 {
506 default:
507 assert(0 && "Zero Extend InReg not there yet");
508 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000509 case MVT::i32: Tmp2 = 0xf0; break;
510 case MVT::i16: Tmp2 = 0xfc; break;
511 case MVT::i8: Tmp2 = 0xfe; break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000512 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000513 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
514 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000515 }
516
517 case ISD::SETCC:
518 Tmp1 = SelectExpr(N.getOperand(0));
519 Tmp2 = SelectExpr(N.getOperand(1));
520 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
521 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
522 switch (SetCC->getCondition()) {
523 default: assert(0 && "Unknown integer comparison!");
524 case ISD::SETEQ:
525 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
526 break;
527 case ISD::SETGT:
528 BuildMI(BB, Alpha::CMPLT, 2, Result).addReg(Tmp2).addReg(Tmp1);
529 break;
530 case ISD::SETGE:
531 BuildMI(BB, Alpha::CMPLE, 2, Result).addReg(Tmp2).addReg(Tmp1);
532 break;
533 case ISD::SETLT:
534 BuildMI(BB, Alpha::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
535 break;
536 case ISD::SETLE:
537 BuildMI(BB, Alpha::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
538 break;
539 case ISD::SETNE:
540 {
541 unsigned Tmp3 = MakeReg(MVT::i64);
542 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
543 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp3).addReg(Alpha::R31);
544 break;
545 }
546 case ISD::SETULT:
547 BuildMI(BB, Alpha::CMPULT, 2, Result).addReg(Tmp1).addReg(Tmp2);
548 break;
549 case ISD::SETUGT:
550 BuildMI(BB, Alpha::CMPULT, 2, Result).addReg(Tmp2).addReg(Tmp1);
551 break;
552 case ISD::SETULE:
553 BuildMI(BB, Alpha::CMPULE, 2, Result).addReg(Tmp1).addReg(Tmp2);
554 break;
555 case ISD::SETUGE:
556 BuildMI(BB, Alpha::CMPULE, 2, Result).addReg(Tmp2).addReg(Tmp1);
557 break;
558 }
559 }
560 else
561 assert(0 && "only integer");
562 }
563 else
564 assert(0 && "Not a setcc in setcc");
565
566 return Result;
567
568 case ISD::CopyFromReg:
569 {
570 if (Result == 1)
571 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
572
573 SDOperand Chain = N.getOperand(0);
574
575 Select(Chain);
576 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
577 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
578 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
579 return Result;
580 }
581
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000582 //Most of the plain arithmetic and logic share the same form, and the same
583 //constant immediate test
584 case ISD::AND:
585 case ISD::OR:
586 case ISD::XOR:
587 case ISD::SHL:
588 case ISD::SRL:
589 case ISD::MUL:
590 if(N.getOperand(1).getOpcode() == ISD::Constant &&
591 cast<ConstantSDNode>(N.getOperand(1))->getValue() >= 0 &&
592 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
593 {
594 switch(N.getOpcode()) {
595 case ISD::AND: Opc = Alpha::ANDi; break;
596 case ISD::OR: Opc = Alpha::BISi; break;
597 case ISD::XOR: Opc = Alpha::XORi; break;
598 case ISD::SHL: Opc = Alpha::SLi; break;
599 case ISD::SRL: Opc = Alpha::SRLi; break;
600 case ISD::SRA: Opc = Alpha::SRAi; break;
601 case ISD::MUL: Opc = Alpha::MULQi; break;
602 };
603 Tmp1 = SelectExpr(N.getOperand(0));
604 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
605 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
606 }
607 else
608 {
609 switch(N.getOpcode()) {
610 case ISD::AND: Opc = Alpha::AND; break;
611 case ISD::OR: Opc = Alpha::BIS; break;
612 case ISD::XOR: Opc = Alpha::XOR; break;
613 case ISD::SHL: Opc = Alpha::SL; break;
614 case ISD::SRL: Opc = Alpha::SRL; break;
615 case ISD::SRA: Opc = Alpha::SRA; break;
616 case ISD::MUL: Opc = Alpha::MULQ; break;
617 };
618 Tmp1 = SelectExpr(N.getOperand(0));
619 Tmp2 = SelectExpr(N.getOperand(1));
620 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
621 }
622 return Result;
623
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000624 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000625 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000626 {
627 bool isAdd = N.getOpcode() == ISD::ADD;
628
629 //FIXME: first check for Scaled Adds and Subs!
630 if(N.getOperand(1).getOpcode() == ISD::Constant &&
631 cast<ConstantSDNode>(N.getOperand(1))->getValue() >= 0 &&
632 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
633 { //Normal imm add/sub
634 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
635 Tmp1 = SelectExpr(N.getOperand(0));
636 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
637 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
638 }
639 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
640 cast<ConstantSDNode>(N.getOperand(1))->getValue() >= 0 &&
641 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
642 { //LDA //FIXME: expand the above condition a bit
643 Tmp1 = SelectExpr(N.getOperand(0));
644 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
645 if (!isAdd)
646 Tmp2 = -Tmp2;
647 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
648 }
649 else
650 { //Normal add/sub
651 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
652 Tmp1 = SelectExpr(N.getOperand(0));
653 Tmp2 = SelectExpr(N.getOperand(1));
654 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
655
656 }
657 return Result;
658 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000659
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000660 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000661 case ISD::SREM:
662 case ISD::SDIV:
663 case ISD::UDIV:
664 //FIXME: alpha really doesn't support any of these operations,
665 // the ops are expanded into special library calls with
666 // special calling conventions
667 switch(N.getOpcode()) {
668 case UREM: Opc = Alpha::REMQU; break;
669 case SREM: Opc = Alpha::REMQ; break;
670 case UDIV: Opc = Alpha::DIVQU; break;
671 case SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000672 Tmp1 = SelectExpr(N.getOperand(0));
673 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000674 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000675 return Result;
676
677 case ISD::SELECT:
678 {
679 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
680 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
681 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
682 // Get the condition into the zero flag.
683 unsigned dummy = MakeReg(MVT::i64);
684 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
685 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
686 return Result;
687 }
688
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000689 case ISD::Constant:
690 {
691 long val = cast<ConstantSDNode>(N)->getValue();
692 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
693 return Result;
694 }
695
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000696 case ISD::LOAD:
697 {
698 // Make sure we generate both values.
699 if (Result != 1)
700 ExprMap[N.getValue(1)] = 1; // Generate the token
701 else
702 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
703
704 SDOperand Chain = N.getOperand(0);
705 SDOperand Address = N.getOperand(1);
706
707 if (Address.getOpcode() == ISD::GlobalAddress)
708 {
709 Select(Chain);
710 AlphaLowering.restoreGP(BB);
711 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
712 }
713 else
714 {
715 Select(Chain);
716 Tmp2 = SelectExpr(Address);
717 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
718 }
719 return Result;
720 }
721 }
722
723 return 0;
724}
725
726void ISel::Select(SDOperand N) {
727 unsigned Tmp1, Tmp2, Opc;
728
729 // FIXME: Disable for our current expansion model!
730 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
731 return; // Already selected.
732
733 SDNode *Node = N.Val;
734
735 switch (N.getOpcode()) {
736
737 default:
738 Node->dump(); std::cerr << "\n";
739 assert(0 && "Node not handled yet!");
740
741 case ISD::BRCOND: {
742 MachineBasicBlock *Dest =
743 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
744
745 Select(N.getOperand(0));
746 Tmp1 = SelectExpr(N.getOperand(1));
747 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
748 return;
749 }
750
751 case ISD::BR: {
752 MachineBasicBlock *Dest =
753 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
754
755 Select(N.getOperand(0));
756 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
757 return;
758 }
759
760 case ISD::ImplicitDef:
761 Select(N.getOperand(0));
762 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
763 return;
764
765 case ISD::EntryToken: return; // Noop
766
767 case ISD::TokenFactor:
768 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
769 Select(Node->getOperand(i));
770
771 //N.Val->dump(); std::cerr << "\n";
772 //assert(0 && "Node not handled yet!");
773
774 return;
775
776 case ISD::CopyToReg:
777 Select(N.getOperand(0));
778 Tmp1 = SelectExpr(N.getOperand(1));
779 Tmp2 = cast<RegSDNode>(N)->getReg();
780
781 if (Tmp1 != Tmp2) {
782 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
783 }
784 return;
785
786 case ISD::RET:
787 switch (N.getNumOperands()) {
788 default:
789 std::cerr << N.getNumOperands() << "\n";
790 for (unsigned i = 0; i < N.getNumOperands(); ++i)
791 std::cerr << N.getOperand(i).getValueType() << "\n";
792 assert(0 && "Unknown return instruction!");
793 case 2:
794 Select(N.getOperand(0));
795 Tmp1 = SelectExpr(N.getOperand(1));
796 switch (N.getOperand(1).getValueType()) {
797 default: assert(0 && "All other types should have been promoted!!");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000798 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000799 case MVT::i64:
800 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
801 break;
802 }
803 break;
804 case 1:
805 Select(N.getOperand(0));
806 break;
807 }
808 //Tmp2 = AlphaLowering.getRetAddr();
809 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
810 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
811 return;
812
813 case ISD::STORE:
814 Select(N.getOperand(0));
815 Tmp1 = SelectExpr(N.getOperand(1)); //value
816 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
817 {
818 AlphaLowering.restoreGP(BB);
819 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
820 }
821 else
822 {
823 Tmp2 = SelectExpr(N.getOperand(2)); //address
824 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
825 }
826 return;
827
828 case ISD::EXTLOAD:
829 case ISD::SEXTLOAD:
830 case ISD::ZEXTLOAD:
831 case ISD::LOAD:
832 case ISD::CopyFromReg:
833 case ISD::CALL:
834// case ISD::DYNAMIC_STACKALLOC:
835 SelectExpr(N);
836 return;
837
838
839 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
840 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
841 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
842
843 Select(N.getOperand(0));
844 Tmp1 = SelectExpr(N.getOperand(1));
845 Tmp2 = SelectExpr(N.getOperand(2));
846
847 switch (StoredTy) {
848 default: assert(0 && "Unhandled Type"); break;
Andrew Lenharthd279b412005-01-25 19:58:40 +0000849 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000850 case MVT::i8: Opc = Alpha::STB; break;
851 case MVT::i16: Opc = Alpha::STW; break;
852 case MVT::i32: Opc = Alpha::STL; break;
853 }
854
855 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
856 return;
857 }
858
859 case ISD::ADJCALLSTACKDOWN:
860 case ISD::ADJCALLSTACKUP:
861 Select(N.getOperand(0));
862 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
863
864 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
865 Alpha::ADJUSTSTACKUP;
866 BuildMI(BB, Opc, 1).addImm(Tmp1);
867 return;
868 }
869 assert(0 && "Should not be reached!");
870}
871
872
873/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
874/// into a machine code representation using pattern matching and a machine
875/// description file.
876///
877FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
878 return new ISel(TM);
879}