blob: 1b1c81fdcc9727bced9769c45727128488359fba [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.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"
30#include <set>
31#include <algorithm>
32using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
36namespace {
37 class IA64TargetLowering : public TargetLowering {
38 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
39
40 //int ReturnAddrIndex; // FrameIndex for return slot.
41 unsigned GP, SP, RP; // FIXME - clean this mess up
42 public:
43
44 unsigned VirtGPR; // this is public so it can be accessed in the selector
45 // for ISD::RET down below. add an accessor instead? FIXME
46
47 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
48
49 // register class for general registers
50 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
51
52 // register class for FP registers
53 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
54
55 // register class for predicate registers
56 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
57
58 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
59
60 setSetCCResultType(MVT::i1);
61 setShiftAmountType(MVT::i64);
62
63 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
64 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
65
66 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
67 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
68
69 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
72
73 setOperationAction(ISD::SREM , MVT::f32 , Expand);
74 setOperationAction(ISD::SREM , MVT::f64 , Expand);
75
76 setOperationAction(ISD::UREM , MVT::f32 , Expand);
77 setOperationAction(ISD::UREM , MVT::f64 , Expand);
78
79 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
80 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
81 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
82
83
84 computeRegisterProperties();
85
86 addLegalFPImmediate(+0.0);
87 addLegalFPImmediate(+1.0);
88 addLegalFPImmediate(-0.0);
89 addLegalFPImmediate(-1.0);
90 }
91
92 /// LowerArguments - This hook must be implemented to indicate how we should
93 /// lower the arguments for the specified function, into the specified DAG.
94 virtual std::vector<SDOperand>
95 LowerArguments(Function &F, SelectionDAG &DAG);
96
97 /// LowerCallTo - This hook lowers an abstract call to a function into an
98 /// actual call.
99 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000100 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
101 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000102
103 virtual std::pair<SDOperand, SDOperand>
104 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
105
106 virtual std::pair<SDOperand,SDOperand>
107 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
108 const Type *ArgTy, SelectionDAG &DAG);
109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
112 SelectionDAG &DAG);
113
114 void restoreGP_SP_RP(MachineBasicBlock* BB)
115 {
116 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
117 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
118 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
119 }
120
Duraid Madinabeeaab22005-03-31 12:31:11 +0000121 void restoreSP_RP(MachineBasicBlock* BB)
122 {
123 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
124 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
125 }
126
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000127 void restoreRP(MachineBasicBlock* BB)
128 {
129 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
130 }
131
132 void restoreGP(MachineBasicBlock* BB)
133 {
134 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
135 }
136
137 };
138}
139
140
141std::vector<SDOperand>
142IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
143 std::vector<SDOperand> ArgValues;
144
145 //
146 // add beautiful description of IA64 stack frame format
147 // here (from intel 24535803.pdf most likely)
148 //
149 MachineFunction &MF = DAG.getMachineFunction();
150 MachineFrameInfo *MFI = MF.getFrameInfo();
151
152 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
153 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
154 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
155
156 MachineBasicBlock& BB = MF.front();
157
158 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
159 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
160
161 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
162 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
163
164 unsigned argVreg[8];
165 unsigned argPreg[8];
166 unsigned argOpc[8];
167
Duraid Madinabeeaab22005-03-31 12:31:11 +0000168 unsigned used_FPArgs = 0; // how many FP args have been used so far?
169
170 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 int count = 0;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000172
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000173 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 {
175 SDOperand newroot, argt;
176 if(count < 8) { // need to fix this logic? maybe.
177
178 switch (getValueType(I->getType())) {
179 default:
180 std::cerr << "ERROR in LowerArgs: unknown type "
181 << getValueType(I->getType()) << "\n";
182 abort();
183 case MVT::f32:
184 // fixme? (well, will need to for weird FP structy stuff,
185 // see intel ABI docs)
186 case MVT::f64:
187 BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
188 // floating point args go into f8..f15 as-needed, the increment
189 argVreg[count] = // is below..:
190 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
191 // FP args go into f8..f15 as needed: (hence the ++)
192 argPreg[count] = args_FP[used_FPArgs++];
193 argOpc[count] = IA64::FMOV;
194 argt = newroot = DAG.getCopyFromReg(argVreg[count],
195 getValueType(I->getType()), DAG.getRoot());
196 break;
197 case MVT::i1: // NOTE: as far as C abi stuff goes,
198 // bools are just boring old ints
199 case MVT::i8:
200 case MVT::i16:
201 case MVT::i32:
202 case MVT::i64:
203 BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
204 argVreg[count] =
205 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
206 argPreg[count] = args_int[count];
207 argOpc[count] = IA64::MOV;
208 argt = newroot =
209 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
210 if ( getValueType(I->getType()) != MVT::i64)
211 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
212 newroot);
213 break;
214 }
215 } else { // more than 8 args go into the frame
216 // Create the frame index object for this incoming parameter...
Duraid Madinabeeaab22005-03-31 12:31:11 +0000217 ArgOffset = 16 + 8 * (count - 8);
218 int FI = MFI->CreateFixedObject(8, ArgOffset);
219
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000220 // Create the SelectionDAG nodes corresponding to a load
221 //from this parameter
222 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
223 argt = newroot = DAG.getLoad(getValueType(I->getType()),
224 DAG.getEntryNode(), FIN);
225 }
226 ++count;
227 DAG.setRoot(newroot.getValue(1));
228 ArgValues.push_back(argt);
229 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000230
231
232 // Create a vreg to hold the output of (what will become)
233 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000234 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
235 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
236 // we create a PSEUDO_ALLOC (pseudo)instruction for now
237
238 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
239
240 // hmm:
241 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
242 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
243 // ..hmm.
244
245 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
246
247 // hmm:
248 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
249 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
250 // ..hmm.
251
Duraid Madinabeeaab22005-03-31 12:31:11 +0000252 unsigned tempOffset=0;
253
254 // if this is a varargs function, we simply lower llvm.va_start by
255 // pointing to the first entry
256 if(F.isVarArg()) {
257 tempOffset=0;
258 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000259 }
260
Duraid Madinabeeaab22005-03-31 12:31:11 +0000261 // here we actually do the moving of args, and store them to the stack
262 // too if this is a varargs function:
263 for (int i = 0; i < count && i < 8; ++i) {
264 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
265 if(F.isVarArg()) {
266 // if this is a varargs function, we copy the input registers to the stack
267 int FI = MFI->CreateFixedObject(8, tempOffset);
268 tempOffset+=8; //XXX: is it safe to use r22 like this?
269 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
270 // FIXME: we should use st8.spill here, one day
271 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
272 }
273 }
274
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000275 return ArgValues;
276}
277
278std::pair<SDOperand, SDOperand>
279IA64TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000280 const Type *RetTy, bool isVarArg,
281 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000282
283 MachineFunction &MF = DAG.getMachineFunction();
284
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000285 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000286 unsigned outRegsUsed = 0;
287
288 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000289 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000290 outRegsUsed = 8;
291 } else {
292 outRegsUsed = Args.size();
293 }
294
295 // FIXME? this WILL fail if we ever try to pass around an arg that
296 // consumes more than a single output slot (a 'real' double, int128
297 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
298 // registers we use. Hopefully, the assembler will notice.
299 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
300 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000301
302 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000304
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000305 std::vector<SDOperand> args_to_use;
306 for (unsigned i = 0, e = Args.size(); i != e; ++i)
307 {
308 switch (getValueType(Args[i].second)) {
309 default: assert(0 && "unexpected argument type!");
310 case MVT::i1:
311 case MVT::i8:
312 case MVT::i16:
313 case MVT::i32:
314 //promote to 64-bits, sign/zero extending based on type
315 //of the argument
316 if(Args[i].second->isSigned())
317 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
318 Args[i].first);
319 else
320 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
321 Args[i].first);
322 break;
323 case MVT::f32:
324 //promote to 64-bits
325 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
326 case MVT::f64:
327 case MVT::i64:
328 break;
329 }
330 args_to_use.push_back(Args[i].first);
331 }
332
333 std::vector<MVT::ValueType> RetVals;
334 MVT::ValueType RetTyVT = getValueType(RetTy);
335 if (RetTyVT != MVT::isVoid)
336 RetVals.push_back(RetTyVT);
337 RetVals.push_back(MVT::Other);
338
339 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
340 Callee, args_to_use), 0);
341 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
342 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
343 DAG.getConstant(NumBytes, getPointerTy()));
344 return std::make_pair(TheCall, Chain);
345}
346
347std::pair<SDOperand, SDOperand>
348IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
349 // vastart just returns the address of the VarArgsFrameIndex slot.
350 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
351}
352
353std::pair<SDOperand,SDOperand> IA64TargetLowering::
354LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
355 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000356
357 MVT::ValueType ArgVT = getValueType(ArgTy);
358 SDOperand Result;
359 if (!isVANext) {
360 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
361 } else {
362 unsigned Amt;
363 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
364 Amt = 8;
365 else {
366 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
367 "Other types should have been promoted for varargs!");
368 Amt = 8;
369 }
370 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
371 DAG.getConstant(Amt, VAList.getValueType()));
372 }
373 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000374}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000375
376std::pair<SDOperand, SDOperand> IA64TargetLowering::
377LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
378 SelectionDAG &DAG) {
379
380 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000381 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000382}
383
384
385namespace {
386
387 //===--------------------------------------------------------------------===//
388 /// ISel - IA64 specific code to select IA64 machine instructions for
389 /// SelectionDAG operations.
390 ///
391 class ISel : public SelectionDAGISel {
392 /// IA64Lowering - This object fully describes how to lower LLVM code to an
393 /// IA64-specific SelectionDAG.
394 IA64TargetLowering IA64Lowering;
395
396 /// ExprMap - As shared expressions are codegen'd, we keep track of which
397 /// vreg the value is produced in, so we only emit one copy of each compiled
398 /// tree.
399 std::map<SDOperand, unsigned> ExprMap;
400 std::set<SDOperand> LoweredTokens;
401
402 public:
403 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {
404 }
405
406 /// InstructionSelectBasicBlock - This callback is invoked by
407 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
408 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
409
410// bool isFoldableLoad(SDOperand Op);
411// void EmitFoldedLoad(SDOperand Op, IA64AddressMode &AM);
412
413 unsigned SelectExpr(SDOperand N);
414 void Select(SDOperand N);
415 };
416}
417
418/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
419/// when it has created a SelectionDAG for us to codegen.
420void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
421
422 // Codegen the basic block.
423 Select(DAG.getRoot());
424
425 // Clear state used for selection.
426 ExprMap.clear();
427 LoweredTokens.clear();
428}
429
430unsigned ISel::SelectExpr(SDOperand N) {
431 unsigned Result;
432 unsigned Tmp1, Tmp2, Tmp3;
433 unsigned Opc = 0;
434 MVT::ValueType DestType = N.getValueType();
435
436 unsigned opcode = N.getOpcode();
437
438 SDNode *Node = N.Val;
439 SDOperand Op0, Op1;
440
441 if (Node->getOpcode() == ISD::CopyFromReg)
442 // Just use the specified register as our input.
443 return dyn_cast<RegSDNode>(Node)->getReg();
444
445 unsigned &Reg = ExprMap[N];
446 if (Reg) return Reg;
447
448 if (N.getOpcode() != ISD::CALL)
449 Reg = Result = (N.getValueType() != MVT::Other) ?
450 MakeReg(N.getValueType()) : 1;
451 else {
452 // If this is a call instruction, make sure to prepare ALL of the result
453 // values as well as the chain.
454 if (Node->getNumValues() == 1)
455 Reg = Result = 1; // Void call, just a chain.
456 else {
457 Result = MakeReg(Node->getValueType(0));
458 ExprMap[N.getValue(0)] = Result;
459 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
460 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
461 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
462 }
463 }
464
465 switch (N.getOpcode()) {
466 default:
467 Node->dump();
468 assert(0 && "Node not handled!\n");
469
470 case ISD::FrameIndex: {
471 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
472 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
473 return Result;
474 }
475
476 case ISD::ConstantPool: {
477 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
478 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
479 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
480 .addReg(IA64::r1);
481 return Result;
482 }
483
484 case ISD::ConstantFP: {
485 Tmp1 = Result; // Intermediate Register
486 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
487 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
488 Tmp1 = MakeReg(MVT::f64);
489
490 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
491 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
492 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
493 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
494 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
495 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
496 else
497 assert(0 && "Unexpected FP constant!");
498 if (Tmp1 != Result)
499 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
500 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
501 .addReg(IA64::F0);
502 return Result;
503 }
504
505 case ISD::DYNAMIC_STACKALLOC: {
506 // Generate both result values.
507 if (Result != 1)
508 ExprMap[N.getValue(1)] = 1; // Generate the token
509 else
510 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
511
512 // FIXME: We are currently ignoring the requested alignment for handling
513 // greater than the stack alignment. This will need to be revisited at some
514 // point. Align = N.getOperand(2);
515
516 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
517 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
518 std::cerr << "Cannot allocate stack object with greater alignment than"
519 << " the stack alignment yet!";
520 abort();
521 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000522
523/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000524 Select(N.getOperand(0));
525 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
526 {
527 if (CN->getValue() < 32000)
528 {
529 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
530 .addImm(-CN->getValue());
531 } else {
532 Tmp1 = SelectExpr(N.getOperand(1));
533 // Subtract size from stack pointer, thereby allocating some space.
534 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
535 }
536 } else {
537 Tmp1 = SelectExpr(N.getOperand(1));
538 // Subtract size from stack pointer, thereby allocating some space.
539 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
540 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000541*/
542 Select(N.getOperand(0));
543 Tmp1 = SelectExpr(N.getOperand(1));
544 // Subtract size from stack pointer, thereby allocating some space.
545 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000546 // Put a pointer to the space into the result register, by copying the
547 // stack pointer.
548 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
549 return Result;
550 }
551
552 case ISD::SELECT: {
553 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
554 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
555 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
556
557 // a temporary predicate register to hold the complement of the
558 // condition:
559 unsigned CondComplement=MakeReg(MVT::i1);
560 unsigned bogusTemp=MakeReg(MVT::i1);
561
562 unsigned bogoResult;
563
564 switch (N.getOperand(1).getValueType()) {
565 default: assert(0 &&
566 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
567 case MVT::i64:
568 bogoResult=MakeReg(MVT::i64);
569 break;
570 case MVT::f64:
571 bogoResult=MakeReg(MVT::f64);
572 break;
573 }
574 // set up the complement predicate reg (CondComplement = NOT Tmp1)
575 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp).addReg(IA64::r0).addReg(IA64::r0);
576 BuildMI(BB, IA64::TPCMPNE, 3, CondComplement).addReg(bogusTemp)
577 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
578
579 // and do a 'conditional move'
580 BuildMI(BB, IA64::PMOV, 2, bogoResult).addReg(Tmp2).addReg(Tmp1);
581 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp3)
582 .addReg(CondComplement);
583
584 return Result;
585 }
586
587 case ISD::Constant: {
588 unsigned depositPos=0;
589 unsigned depositLen=0;
590 switch (N.getValueType()) {
591 default: assert(0 && "Cannot use constants of this type!");
592 case MVT::i1: { // if a bool, we don't 'load' so much as generate
593 // the constant:
594 if(cast<ConstantSDNode>(N)->getValue()) // true:
595 BuildMI(BB, IA64::CMPEQ, 2, Result)
596 .addReg(IA64::r0).addReg(IA64::r0);
597 else // false:
598 BuildMI(BB, IA64::CMPNE, 2, Result)
599 .addReg(IA64::r0).addReg(IA64::r0);
600 return Result;
601 }
602 case MVT::i64: Opc = IA64::MOVLI32; break;
603 }
604
605 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
606 if(immediate>>32) { // if our immediate really is big:
607 int highPart = immediate>>32;
608 int lowPart = immediate&0xFFFFFFFF;
609 unsigned dummy = MakeReg(MVT::i64);
610 unsigned dummy2 = MakeReg(MVT::i64);
611 unsigned dummy3 = MakeReg(MVT::i64);
612
613 BuildMI(BB, IA64::MOVLI32, 1, dummy).addImm(highPart);
614 BuildMI(BB, IA64::SHLI, 2, dummy2).addReg(dummy).addImm(32);
615 BuildMI(BB, IA64::MOVLI32, 1, dummy3).addImm(lowPart);
616 BuildMI(BB, IA64::ADD, 2, Result).addReg(dummy2).addReg(dummy3);
617 } else {
618 BuildMI(BB, IA64::MOVLI32, 1, Result).addImm(immediate);
619 }
620
621 return Result;
622 }
623
624 case ISD::GlobalAddress: {
625 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
626 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000627
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000628 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000629 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000630
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000631 return Result;
632 }
633
634 case ISD::ExternalSymbol: {
635 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +0000636// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
637 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000638 return Result;
639 }
640
641 case ISD::FP_EXTEND: {
642 Tmp1 = SelectExpr(N.getOperand(0));
643 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
644 return Result;
645 }
646
647 case ISD::ZERO_EXTEND: {
648 Tmp1 = SelectExpr(N.getOperand(0)); // value
649
650 switch (N.getOperand(0).getValueType()) {
651 default: assert(0 && "Cannot zero-extend this type!");
652 case MVT::i8: Opc = IA64::ZXT1; break;
653 case MVT::i16: Opc = IA64::ZXT2; break;
654 case MVT::i32: Opc = IA64::ZXT4; break;
655
656 // we handle bools differently! :
657 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
658 unsigned dummy = MakeReg(MVT::i64);
659 // first load zero:
660 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
661 // ...then conditionally (PR:Tmp1) add 1:
662 BuildMI(BB, IA64::CADDIMM22, 3, Result).addReg(dummy)
663 .addImm(1).addReg(Tmp1);
664 return Result; // XXX early exit!
665 }
666 }
667
668 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
669 return Result;
670 }
671
672 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
673
674assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
675
676 Tmp1 = SelectExpr(N.getOperand(0)); // value
677
678 switch (N.getOperand(0).getValueType()) {
679 default: assert(0 && "Cannot sign-extend this type!");
680 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
681 Opc = IA64::SXT1; break;
682 // FIXME: for now, we treat bools the same as i8s
683 case MVT::i8: Opc = IA64::SXT1; break;
684 case MVT::i16: Opc = IA64::SXT2; break;
685 case MVT::i32: Opc = IA64::SXT4; break;
686 }
687
688 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
689 return Result;
690 }
691
692 case ISD::TRUNCATE: {
693 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
694 // of R0 appropriately.
695 switch (N.getOperand(0).getValueType()) {
696 default: assert(0 && "Unknown truncate!");
697 case MVT::i64: break;
698 }
699 Tmp1 = SelectExpr(N.getOperand(0));
700 unsigned depositPos, depositLen;
701
702 switch (N.getValueType()) {
703 default: assert(0 && "Unknown truncate!");
704 case MVT::i1: {
705 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
706 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
707 .addReg(IA64::r0);
708 return Result; // XXX early exit!
709 }
710 case MVT::i8: depositPos=0; depositLen=8; break;
711 case MVT::i16: depositPos=0; depositLen=16; break;
712 case MVT::i32: depositPos=0; depositLen=32; break;
713 }
714 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
715 .addImm(depositPos).addImm(depositLen);
716 return Result;
717 }
718
719/*
720 case ISD::FP_ROUND: {
721 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
722 "error: trying to FP_ROUND something other than f64 -> f32!\n");
723 Tmp1 = SelectExpr(N.getOperand(0));
724 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
725 // we add 0.0 using a single precision add to do rounding
726 return Result;
727 }
728*/
729
730// FIXME: the following 4 cases need cleaning
731 case ISD::SINT_TO_FP: {
732 Tmp1 = SelectExpr(N.getOperand(0));
733 Tmp2 = MakeReg(MVT::f64);
734 unsigned dummy = MakeReg(MVT::f64);
735 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
736 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
737 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
738 return Result;
739 }
740
741 case ISD::UINT_TO_FP: {
742 Tmp1 = SelectExpr(N.getOperand(0));
743 Tmp2 = MakeReg(MVT::f64);
744 unsigned dummy = MakeReg(MVT::f64);
745 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
746 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
747 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
748 return Result;
749 }
750
751 case ISD::FP_TO_SINT: {
752 Tmp1 = SelectExpr(N.getOperand(0));
753 Tmp2 = MakeReg(MVT::f64);
754 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
755 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
756 return Result;
757 }
758
759 case ISD::FP_TO_UINT: {
760 Tmp1 = SelectExpr(N.getOperand(0));
761 Tmp2 = MakeReg(MVT::f64);
762 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
763 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
764 return Result;
765 }
766
767 case ISD::ADD: {
768 Tmp1 = SelectExpr(N.getOperand(0));
769 Tmp2 = SelectExpr(N.getOperand(1));
770 if(DestType != MVT::f64)
771 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // int
772 else
773 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // FP
774 return Result;
775 }
776
777 case ISD::MUL: {
778 Tmp1 = SelectExpr(N.getOperand(0));
779 Tmp2 = SelectExpr(N.getOperand(1));
780 if(DestType != MVT::f64) { // integer multiply, emit some code (FIXME)
781 unsigned TempFR1=MakeReg(MVT::f64);
782 unsigned TempFR2=MakeReg(MVT::f64);
783 unsigned TempFR3=MakeReg(MVT::f64);
784 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
785 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
786 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
787 .addReg(IA64::F0);
788 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
789 }
790 else // floating point multiply
791 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
792 return Result;
793 }
794
795 case ISD::SUB: {
796 Tmp1 = SelectExpr(N.getOperand(0));
797 Tmp2 = SelectExpr(N.getOperand(1));
798 if(DestType != MVT::f64)
799 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
800 else
801 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
802 return Result;
803 }
804
805 case ISD::AND: {
806 switch (N.getValueType()) {
807 default: assert(0 && "Cannot AND this type!");
808 case MVT::i1: { // if a bool, we emit a pseudocode AND
809 unsigned pA = SelectExpr(N.getOperand(0));
810 unsigned pB = SelectExpr(N.getOperand(1));
811
812/* our pseudocode for AND is:
813 *
814(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
815 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
816 ;;
817(pB) cmp.ne pTemp,p0 = r0,r0
818 ;;
819(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
820
821*/
822 unsigned pTemp = MakeReg(MVT::i1);
823
824 unsigned bogusTemp1 = MakeReg(MVT::i1);
825 unsigned bogusTemp2 = MakeReg(MVT::i1);
826 unsigned bogusTemp3 = MakeReg(MVT::i1);
827 unsigned bogusTemp4 = MakeReg(MVT::i1);
828
829 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
830 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
831 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
832 .addReg(IA64::r0).addReg(IA64::r0);
833 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
834 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
835 BuildMI(BB, IA64::TPCMPNE, 3, Result)
836 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
837 break;
838 }
839 // if not a bool, we just AND away:
840 case MVT::i8:
841 case MVT::i16:
842 case MVT::i32:
843 case MVT::i64: {
844 Tmp1 = SelectExpr(N.getOperand(0));
845 Tmp2 = SelectExpr(N.getOperand(1));
846 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
847 break;
848 }
849 }
850 return Result;
851 }
852
853 case ISD::OR: {
854 switch (N.getValueType()) {
855 default: assert(0 && "Cannot OR this type!");
856 case MVT::i1: { // if a bool, we emit a pseudocode OR
857 unsigned pA = SelectExpr(N.getOperand(0));
858 unsigned pB = SelectExpr(N.getOperand(1));
859
860 unsigned pTemp1 = MakeReg(MVT::i1);
861
862/* our pseudocode for OR is:
863 *
864
865pC = pA OR pB
866-------------
867
868(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
869 ;;
870(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
871
872*/
873 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
874 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
875 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
876 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
877 break;
878 }
879 // if not a bool, we just OR away:
880 case MVT::i8:
881 case MVT::i16:
882 case MVT::i32:
883 case MVT::i64: {
884 Tmp1 = SelectExpr(N.getOperand(0));
885 Tmp2 = SelectExpr(N.getOperand(1));
886 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
887 break;
888 }
889 }
890 return Result;
891 }
892
893 case ISD::XOR: {
894 switch (N.getValueType()) {
895 default: assert(0 && "Cannot XOR this type!");
896 case MVT::i1: { // if a bool, we emit a pseudocode XOR
897 unsigned pY = SelectExpr(N.getOperand(0));
898 unsigned pZ = SelectExpr(N.getOperand(1));
899
900/* one possible routine for XOR is:
901
902 // Compute px = py ^ pz
903 // using sum of products: px = (py & !pz) | (pz & !py)
904 // Uses 5 instructions in 3 cycles.
905 // cycle 1
906(pz) cmp.eq.unc px = r0, r0 // px = pz
907(py) cmp.eq.unc pt = r0, r0 // pt = py
908 ;;
909 // cycle 2
910(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
911(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
912 ;;
913 } { .mmi
914 // cycle 3
915(pt) cmp.eq.or px = r0, r0 // px = px | pt
916
917*** Another, which we use here, requires one scratch GR. it is:
918
919 mov rt = 0 // initialize rt off critical path
920 ;;
921
922 // cycle 1
923(pz) cmp.eq.unc px = r0, r0 // px = pz
924(pz) mov rt = 1 // rt = pz
925 ;;
926 // cycle 2
927(py) cmp.ne px = 1, rt // if (py) px = !pz
928
929.. these routines kindly provided by Jim Hull
930*/
931 unsigned rt = MakeReg(MVT::i64);
932
933 // these two temporaries will never actually appear,
934 // due to the two-address form of some of the instructions below
935 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
936 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
937
938 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
939 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
940 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
941 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
942 .addReg(bogoGR).addImm(1).addReg(pZ);
943 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
944 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
945 break;
946 }
947 // if not a bool, we just XOR away:
948 case MVT::i8:
949 case MVT::i16:
950 case MVT::i32:
951 case MVT::i64: {
952 Tmp1 = SelectExpr(N.getOperand(0));
953 Tmp2 = SelectExpr(N.getOperand(1));
954 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
955 break;
956 }
957 }
958 return Result;
959 }
960
961 case ISD::SHL: {
962 Tmp1 = SelectExpr(N.getOperand(0));
963 Tmp2 = SelectExpr(N.getOperand(1));
964 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
965 return Result;
966 }
967 case ISD::SRL: {
968 Tmp1 = SelectExpr(N.getOperand(0));
969 Tmp2 = SelectExpr(N.getOperand(1));
970 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
971 return Result;
972 }
973 case ISD::SRA: {
974 Tmp1 = SelectExpr(N.getOperand(0));
975 Tmp2 = SelectExpr(N.getOperand(1));
976 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
977 return Result;
978 }
979
980 case ISD::SDIV:
981 case ISD::UDIV:
982 case ISD::SREM:
983 case ISD::UREM: {
984
985 Tmp1 = SelectExpr(N.getOperand(0));
986 Tmp2 = SelectExpr(N.getOperand(1));
987
988 bool isFP=false;
989
990 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
991 isFP=true;
992
993 bool isModulus=false; // is it a division or a modulus?
994 bool isSigned=false;
995
996 switch(N.getOpcode()) {
997 case ISD::SDIV: isModulus=false; isSigned=true; break;
998 case ISD::UDIV: isModulus=false; isSigned=false; break;
999 case ISD::SREM: isModulus=true; isSigned=true; break;
1000 case ISD::UREM: isModulus=true; isSigned=false; break;
1001 }
1002
Duraid Madinabeeaab22005-03-31 12:31:11 +00001003 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
1004 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001005 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1006 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1007 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1008 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1009 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1010 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1011 unsigned TmpF7=MakeReg(MVT::f64);
1012 unsigned TmpF8=MakeReg(MVT::f64);
1013 unsigned TmpF9=MakeReg(MVT::f64);
1014 unsigned TmpF10=MakeReg(MVT::f64);
1015 unsigned TmpF11=MakeReg(MVT::f64);
1016 unsigned TmpF12=MakeReg(MVT::f64);
1017 unsigned TmpF13=MakeReg(MVT::f64);
1018 unsigned TmpF14=MakeReg(MVT::f64);
1019 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001020
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001021 // OK, emit some code:
1022
1023 if(!isFP) {
1024 // first, load the inputs into FP regs.
1025 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1026 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1027
1028 // next, convert the inputs to FP
1029 if(isSigned) {
1030 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1031 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1032 } else {
1033 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1034 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1035 }
1036
1037 } else { // this is an FP divide/remainder, so we 'leak' some temp
1038 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1039 TmpF3=Tmp1;
1040 TmpF4=Tmp2;
1041 }
1042
1043 // we start by computing an approximate reciprocal (good to 9 bits?)
1044 // note, this instruction writes _both_ TmpF5 (answer) and tmpPR (predicate)
1045 // FIXME: or at least, it should!!
1046 BuildMI(BB, IA64::FRCPAS1FLOAT, 2, TmpF5).addReg(TmpF3).addReg(TmpF4);
1047 BuildMI(BB, IA64::FRCPAS1PREDICATE, 2, TmpPR).addReg(TmpF3).addReg(TmpF4);
1048
Duraid Madinabeeaab22005-03-31 12:31:11 +00001049 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1050 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1051 // TPCMPNE below
1052 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1053 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1054 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1055 }
1056
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001057 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1058 // precision, don't need this much for f32/i32)
1059 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1060 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1061 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1062 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1063 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1064 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1065 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1066 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1067 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1068 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1069 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1070 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1071 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1072 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1073 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1074 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
1075 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1076 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1077
1078 if(!isFP) {
1079 // round to an integer
1080 if(isSigned)
1081 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1082 else
1083 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1084 } else {
1085 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1086 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1087 // we really do need the above FMOV? ;)
1088 }
1089
1090 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001091 if(isFP) { // extra worrying about div-by-zero
1092 unsigned bogoResult=MakeReg(MVT::f64);
1093
1094 // we do a 'conditional fmov' (of the correct result, depending
1095 // on how the frcpa predicate turned out)
1096 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1097 .addReg(TmpF12).addReg(TmpPR2);
1098 BuildMI(BB, IA64::CFMOV, 2, Result)
1099 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1100 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001101 else
1102 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
1103 } else { // this is a modulus
1104 if(!isFP) {
1105 // answer = q * (-b) + a
1106 unsigned ModulusResult = MakeReg(MVT::f64);
1107 unsigned TmpF = MakeReg(MVT::f64);
1108 unsigned TmpI = MakeReg(MVT::i64);
1109 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1110 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1111 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1112 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1113 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1114 } else { // FP modulus! The horror... the horror....
1115 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1116 }
1117 }
1118
1119 return Result;
1120 }
1121
1122 case ISD::ZERO_EXTEND_INREG: {
1123 Tmp1 = SelectExpr(N.getOperand(0));
1124 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1125 switch(MVN->getExtraValueType())
1126 {
1127 default:
1128 Node->dump();
1129 assert(0 && "don't know how to zero extend this type");
1130 break;
1131 case MVT::i8: Opc = IA64::ZXT1; break;
1132 case MVT::i16: Opc = IA64::ZXT2; break;
1133 case MVT::i32: Opc = IA64::ZXT4; break;
1134 }
1135 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1136 return Result;
1137 }
1138
1139 case ISD::SIGN_EXTEND_INREG: {
1140 Tmp1 = SelectExpr(N.getOperand(0));
1141 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1142 switch(MVN->getExtraValueType())
1143 {
1144 default:
1145 Node->dump();
1146 assert(0 && "don't know how to sign extend this type");
1147 break;
1148 case MVT::i8: Opc = IA64::SXT1; break;
1149 case MVT::i16: Opc = IA64::SXT2; break;
1150 case MVT::i32: Opc = IA64::SXT4; break;
1151 }
1152 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1153 return Result;
1154 }
1155
1156 case ISD::SETCC: {
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 Tmp2 = SelectExpr(N.getOperand(1));
1159 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1160 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1161 switch (SetCC->getCondition()) {
1162 default: assert(0 && "Unknown integer comparison!");
1163 case ISD::SETEQ:
1164 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1165 break;
1166 case ISD::SETGT:
1167 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1168 break;
1169 case ISD::SETGE:
1170 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1171 break;
1172 case ISD::SETLT:
1173 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1174 break;
1175 case ISD::SETLE:
1176 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1177 break;
1178 case ISD::SETNE:
1179 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1180 break;
1181 case ISD::SETULT:
1182 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1183 break;
1184 case ISD::SETUGT:
1185 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1186 break;
1187 case ISD::SETULE:
1188 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1189 break;
1190 case ISD::SETUGE:
1191 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1192 break;
1193 }
1194 }
1195 else { // if not integer, should be FP. FIXME: what about bools? ;)
1196 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1197 "error: SETCC should have had incoming f32 promoted to f64!\n");
1198 switch (SetCC->getCondition()) {
1199 default: assert(0 && "Unknown FP comparison!");
1200 case ISD::SETEQ:
1201 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1202 break;
1203 case ISD::SETGT:
1204 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1205 break;
1206 case ISD::SETGE:
1207 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1208 break;
1209 case ISD::SETLT:
1210 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1211 break;
1212 case ISD::SETLE:
1213 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1214 break;
1215 case ISD::SETNE:
1216 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1217 break;
1218 case ISD::SETULT:
1219 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1220 break;
1221 case ISD::SETUGT:
1222 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1223 break;
1224 case ISD::SETULE:
1225 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1226 break;
1227 case ISD::SETUGE:
1228 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1229 break;
1230 }
1231 }
1232 }
1233 else
1234 assert(0 && "this setcc not implemented yet");
1235
1236 return Result;
1237 }
1238
1239 case ISD::EXTLOAD:
1240 case ISD::ZEXTLOAD:
1241 case ISD::LOAD: {
1242 // Make sure we generate both values.
1243 if (Result != 1)
1244 ExprMap[N.getValue(1)] = 1; // Generate the token
1245 else
1246 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1247
1248 bool isBool=false;
1249
1250 if(opcode == ISD::LOAD) { // this is a LOAD
1251 switch (Node->getValueType(0)) {
1252 default: assert(0 && "Cannot load this type!");
1253 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1254 // FIXME: for now, we treat bool loads the same as i8 loads */
1255 case MVT::i8: Opc = IA64::LD1; break;
1256 case MVT::i16: Opc = IA64::LD2; break;
1257 case MVT::i32: Opc = IA64::LD4; break;
1258 case MVT::i64: Opc = IA64::LD8; break;
1259
1260 case MVT::f32: Opc = IA64::LDF4; break;
1261 case MVT::f64: Opc = IA64::LDF8; break;
1262 }
1263 } else { // this is an EXTLOAD or ZEXTLOAD
1264 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1265 switch (TypeBeingLoaded) {
1266 default: assert(0 && "Cannot extload/zextload this type!");
1267 // FIXME: bools?
1268 case MVT::i8: Opc = IA64::LD1; break;
1269 case MVT::i16: Opc = IA64::LD2; break;
1270 case MVT::i32: Opc = IA64::LD4; break;
1271 case MVT::f32: Opc = IA64::LDF4; break;
1272 }
1273 }
1274
1275 SDOperand Chain = N.getOperand(0);
1276 SDOperand Address = N.getOperand(1);
1277
1278 if(Address.getOpcode() == ISD::GlobalAddress) {
1279 Select(Chain);
1280 unsigned dummy = MakeReg(MVT::i64);
1281 unsigned dummy2 = MakeReg(MVT::i64);
1282 BuildMI(BB, IA64::ADD, 2, dummy)
1283 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1284 .addReg(IA64::r1);
1285 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1286 if(!isBool)
1287 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1288 else { // emit a little pseudocode to load a bool (stored in one byte)
1289 // into a predicate register
1290 assert(Opc==IA64::LD1 && "problem loading a bool");
1291 unsigned dummy3 = MakeReg(MVT::i64);
1292 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1293 // we compare to 0. true? 0. false? 1.
1294 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1295 }
1296 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1297 Select(Chain);
1298 IA64Lowering.restoreGP(BB);
1299 unsigned dummy = MakeReg(MVT::i64);
1300 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1301 .addReg(IA64::r1); // CPI+GP
1302 if(!isBool)
1303 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1304 else { // emit a little pseudocode to load a bool (stored in one byte)
1305 // into a predicate register
1306 assert(Opc==IA64::LD1 && "problem loading a bool");
1307 unsigned dummy3 = MakeReg(MVT::i64);
1308 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1309 // we compare to 0. true? 0. false? 1.
1310 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1311 }
1312 } else if(Address.getOpcode() == ISD::FrameIndex) {
1313 Select(Chain); // FIXME ? what about bools?
1314 unsigned dummy = MakeReg(MVT::i64);
1315 BuildMI(BB, IA64::MOV, 1, dummy)
1316 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1317 if(!isBool)
1318 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1319 else { // emit a little pseudocode to load a bool (stored in one byte)
1320 // into a predicate register
1321 assert(Opc==IA64::LD1 && "problem loading a bool");
1322 unsigned dummy3 = MakeReg(MVT::i64);
1323 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1324 // we compare to 0. true? 0. false? 1.
1325 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1326 }
1327 } else { // none of the above...
1328 Select(Chain);
1329 Tmp2 = SelectExpr(Address);
1330 if(!isBool)
1331 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1332 else { // emit a little pseudocode to load a bool (stored in one byte)
1333 // into a predicate register
1334 assert(Opc==IA64::LD1 && "problem loading a bool");
1335 unsigned dummy = MakeReg(MVT::i64);
1336 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1337 // we compare to 0. true? 0. false? 1.
1338 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1339 }
1340 }
1341
1342 return Result;
1343 }
1344
1345 case ISD::CopyFromReg: {
1346 if (Result == 1)
1347 Result = ExprMap[N.getValue(0)] =
1348 MakeReg(N.getValue(0).getValueType());
1349
1350 SDOperand Chain = N.getOperand(0);
1351
1352 Select(Chain);
1353 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1354
1355 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1356 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1357 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1358 // (r) Result =cmp.eq.unc(r0,r0)
1359 else
1360 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1361 return Result;
1362 }
1363
1364 case ISD::CALL: {
1365 Select(N.getOperand(0));
1366
1367 // The chain for this call is now lowered.
1368 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1369
1370 //grab the arguments
1371 std::vector<unsigned> argvregs;
1372
1373 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1374 argvregs.push_back(SelectExpr(N.getOperand(i)));
1375
1376 // see section 8.5.8 of "Itanium Software Conventions and
1377 // Runtime Architecture Guide to see some examples of what's going
1378 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1379 // while FP args get mapped to F8->F15 as needed)
1380
1381 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1382
1383 // in reg args
1384 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1385 {
1386 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1387 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1388 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1389 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1390
1391 switch(N.getOperand(i+2).getValueType())
1392 {
1393 default: // XXX do we need to support MVT::i1 here?
1394 Node->dump();
1395 N.getOperand(i).Val->dump();
1396 std::cerr << "Type for " << i << " is: " <<
1397 N.getOperand(i+2).getValueType() << std::endl;
1398 assert(0 && "Unknown value type for call");
1399 case MVT::i64:
1400 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1401 break;
1402 case MVT::f64:
1403 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1404 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001405 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001406 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1407 break;
1408 }
1409 }
1410
1411 //in mem args
1412 for (int i = 8, e = argvregs.size(); i < e; ++i)
1413 {
1414 unsigned tempAddr = MakeReg(MVT::i64);
1415
1416 switch(N.getOperand(i+2).getValueType()) {
1417 default:
1418 Node->dump();
1419 N.getOperand(i).Val->dump();
1420 std::cerr << "Type for " << i << " is: " <<
1421 N.getOperand(i+2).getValueType() << "\n";
1422 assert(0 && "Unknown value type for call");
1423 case MVT::i1: // FIXME?
1424 case MVT::i8:
1425 case MVT::i16:
1426 case MVT::i32:
1427 case MVT::i64:
1428 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1429 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1430 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1431 break;
1432 case MVT::f32:
1433 case MVT::f64:
1434 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1435 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1436 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1437 break;
1438 }
1439 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001440
1441 /* XXX we want to re-enable direct branches! crippling them now
1442 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001443 //build the right kind of call
1444 if (GlobalAddressSDNode *GASD =
1445 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1446 {
1447 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1448 IA64Lowering.restoreGP_SP_RP(BB);
1449 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001450 ^^^^^^^^^^^^^ we want this code one day XXX */
1451 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001452 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001453 { // FIXME : currently need this case for correctness, to avoid
1454 // "non-pic code with imm relocation against dynamic symbol" errors
1455 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001456 .addExternalSymbol(ESSDN->getSymbol(), true);
1457 IA64Lowering.restoreGP_SP_RP(BB);
1458 }
1459 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001460 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001461
1462 unsigned targetEntryPoint=MakeReg(MVT::i64);
1463 unsigned targetGPAddr=MakeReg(MVT::i64);
1464 unsigned currentGP=MakeReg(MVT::i64);
1465
1466 // b6 is a scratch branch register, we load the target entry point
1467 // from the base of the function descriptor
1468 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1469 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1470
1471 // save the current GP:
1472 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1473
1474 /* TODO: we need to make sure doing this never, ever loads a
1475 * bogus value into r1 (GP). */
1476 // load the target GP (which is at mem[functiondescriptor+8])
1477 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1478 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1479 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1480
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001481 // and then jump: (well, call)
1482 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001483 // and finally restore the old GP
1484 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1485 IA64Lowering.restoreSP_RP(BB);
1486 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001487
1488 switch (Node->getValueType(0)) {
1489 default: assert(0 && "Unknown value type for call result!");
1490 case MVT::Other: return 1;
1491 case MVT::i1:
1492 BuildMI(BB, IA64::CMPNE, 2, Result)
1493 .addReg(IA64::r8).addReg(IA64::r0);
1494 break;
1495 case MVT::i8:
1496 case MVT::i16:
1497 case MVT::i32:
1498 case MVT::i64:
1499 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1500 break;
1501 case MVT::f64:
1502 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1503 break;
1504 }
1505 return Result+N.ResNo;
1506 }
1507
1508 } // <- uhhh XXX
1509 return 0;
1510}
1511
1512void ISel::Select(SDOperand N) {
1513 unsigned Tmp1, Tmp2, Opc;
1514 unsigned opcode = N.getOpcode();
1515
Nate Begeman85fdeb22005-03-24 04:39:54 +00001516 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001517 return; // Already selected.
1518
1519 SDNode *Node = N.Val;
1520
1521 switch (Node->getOpcode()) {
1522 default:
1523 Node->dump(); std::cerr << "\n";
1524 assert(0 && "Node not handled yet!");
1525
1526 case ISD::EntryToken: return; // Noop
1527
1528 case ISD::TokenFactor: {
1529 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1530 Select(Node->getOperand(i));
1531 return;
1532 }
1533
1534 case ISD::CopyToReg: {
1535 Select(N.getOperand(0));
1536 Tmp1 = SelectExpr(N.getOperand(1));
1537 Tmp2 = cast<RegSDNode>(N)->getReg();
1538
1539 if (Tmp1 != Tmp2) {
1540 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1541 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1542 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1543 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1544 else
1545 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1546 // XXX is this the right way 'round? ;)
1547 }
1548 return;
1549 }
1550
1551 case ISD::RET: {
1552
1553 /* what the heck is going on here:
1554
1555<_sabre_> ret with two operands is obvious: chain and value
1556<camel_> yep
1557<_sabre_> ret with 3 values happens when 'expansion' occurs
1558<_sabre_> e.g. i64 gets split into 2x i32
1559<camel_> oh right
1560<_sabre_> you don't have this case on ia64
1561<camel_> yep
1562<_sabre_> so the two returned values go into EAX/EDX on ia32
1563<camel_> ahhh *memories*
1564<_sabre_> :)
1565<camel_> ok, thanks :)
1566<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1567<_sabre_> this is the first operand always
1568<_sabre_> these operand often define chains, they are the last operand
1569<_sabre_> they are printed as 'ch' if you do DAG.dump()
1570 */
1571
1572 switch (N.getNumOperands()) {
1573 default:
1574 assert(0 && "Unknown return instruction!");
1575 case 2:
1576 Select(N.getOperand(0));
1577 Tmp1 = SelectExpr(N.getOperand(1));
1578 switch (N.getOperand(1).getValueType()) {
1579 default: assert(0 && "All other types should have been promoted!!");
1580 // FIXME: do I need to add support for bools here?
1581 // (return '0' or '1' r8, basically...)
1582 case MVT::i64:
1583 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1584 break;
1585 case MVT::f64:
1586 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1587 }
1588 break;
1589 case 1:
1590 Select(N.getOperand(0));
1591 break;
1592 }
1593 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1594 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1595 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1596 return;
1597 }
1598
1599 case ISD::BR: {
1600 Select(N.getOperand(0));
1601 MachineBasicBlock *Dest =
1602 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1603 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1604 // XXX HACK! we do _not_ need long branches all the time
1605 return;
1606 }
1607
1608 case ISD::ImplicitDef: {
1609 Select(N.getOperand(0));
1610 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1611 return;
1612 }
1613
1614 case ISD::BRCOND: {
1615 MachineBasicBlock *Dest =
1616 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1617
1618 Select(N.getOperand(0));
1619 Tmp1 = SelectExpr(N.getOperand(1));
1620 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1621 // XXX HACK! we do _not_ need long branches all the time
1622 return;
1623 }
1624
1625 case ISD::EXTLOAD:
1626 case ISD::ZEXTLOAD:
1627 case ISD::SEXTLOAD:
1628 case ISD::LOAD:
1629 case ISD::CALL:
1630 case ISD::CopyFromReg:
1631 case ISD::DYNAMIC_STACKALLOC:
1632 SelectExpr(N);
1633 return;
1634
1635 case ISD::TRUNCSTORE:
1636 case ISD::STORE: {
1637 Select(N.getOperand(0));
1638 Tmp1 = SelectExpr(N.getOperand(1)); // value
1639
1640 bool isBool=false;
1641
1642 if(opcode == ISD::STORE) {
1643 switch (N.getOperand(1).getValueType()) {
1644 default: assert(0 && "Cannot store this type!");
1645 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1646 // FIXME?: for now, we treat bool loads the same as i8 stores */
1647 case MVT::i8: Opc = IA64::ST1; break;
1648 case MVT::i16: Opc = IA64::ST2; break;
1649 case MVT::i32: Opc = IA64::ST4; break;
1650 case MVT::i64: Opc = IA64::ST8; break;
1651
1652 case MVT::f32: Opc = IA64::STF4; break;
1653 case MVT::f64: Opc = IA64::STF8; break;
1654 }
1655 } else { // truncstore
1656 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1657 default: assert(0 && "unknown type in truncstore");
1658 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1659 //FIXME: DAG does not promote this load?
1660 case MVT::i8: Opc = IA64::ST1; break;
1661 case MVT::i16: Opc = IA64::ST2; break;
1662 case MVT::i32: Opc = IA64::ST4; break;
1663 case MVT::f32: Opc = IA64::STF4; break;
1664 }
1665 }
1666
1667 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1668 unsigned dummy = MakeReg(MVT::i64);
1669 unsigned dummy2 = MakeReg(MVT::i64);
1670 BuildMI(BB, IA64::ADD, 2, dummy)
1671 .addGlobalAddress(cast<GlobalAddressSDNode>
1672 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1673 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1674
1675 if(!isBool)
1676 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1677 else { // we are storing a bool, so emit a little pseudocode
1678 // to store a predicate register as one byte
1679 assert(Opc==IA64::ST1);
1680 unsigned dummy3 = MakeReg(MVT::i64);
1681 unsigned dummy4 = MakeReg(MVT::i64);
1682 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1683 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1684 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1685 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1686 }
1687 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1688
1689 // FIXME? (what about bools?)
1690
1691 unsigned dummy = MakeReg(MVT::i64);
1692 BuildMI(BB, IA64::MOV, 1, dummy)
1693 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1694 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1695 } else { // otherwise
1696 Tmp2 = SelectExpr(N.getOperand(2)); //address
1697 if(!isBool)
1698 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1699 else { // we are storing a bool, so emit a little pseudocode
1700 // to store a predicate register as one byte
1701 assert(Opc==IA64::ST1);
1702 unsigned dummy3 = MakeReg(MVT::i64);
1703 unsigned dummy4 = MakeReg(MVT::i64);
1704 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1705 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1706 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1707 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1708 }
1709 }
1710 return;
1711 }
1712
1713 case ISD::ADJCALLSTACKDOWN:
1714 case ISD::ADJCALLSTACKUP: {
1715 Select(N.getOperand(0));
1716 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1717
1718 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1719 IA64::ADJUSTCALLSTACKUP;
1720 BuildMI(BB, Opc, 1).addImm(Tmp1);
1721 return;
1722 }
1723
1724 return;
1725 }
1726 assert(0 && "GAME OVER. INSERT COIN?");
1727}
1728
1729
1730/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1731/// into a machine code representation using pattern matching and a machine
1732/// description file.
1733///
1734FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1735 return new ISel(TM);
1736}
1737
1738