blob: 16607cbcc32ee680aba19b8ffcc80653bbb9b11b [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
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000557 unsigned bogoResult;
558
559 switch (N.getOperand(1).getValueType()) {
560 default: assert(0 &&
561 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
562 case MVT::i64:
563 bogoResult=MakeReg(MVT::i64);
564 break;
565 case MVT::f64:
566 bogoResult=MakeReg(MVT::f64);
567 break;
568 }
Duraid Madina69c8e202005-04-01 10:35:00 +0000569
570 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
571 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
572 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
573 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000574 return Result;
575 }
576
577 case ISD::Constant: {
578 unsigned depositPos=0;
579 unsigned depositLen=0;
580 switch (N.getValueType()) {
581 default: assert(0 && "Cannot use constants of this type!");
582 case MVT::i1: { // if a bool, we don't 'load' so much as generate
583 // the constant:
584 if(cast<ConstantSDNode>(N)->getValue()) // true:
585 BuildMI(BB, IA64::CMPEQ, 2, Result)
586 .addReg(IA64::r0).addReg(IA64::r0);
587 else // false:
588 BuildMI(BB, IA64::CMPNE, 2, Result)
589 .addReg(IA64::r0).addReg(IA64::r0);
590 return Result;
591 }
592 case MVT::i64: Opc = IA64::MOVLI32; break;
593 }
594
595 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
596 if(immediate>>32) { // if our immediate really is big:
597 int highPart = immediate>>32;
598 int lowPart = immediate&0xFFFFFFFF;
599 unsigned dummy = MakeReg(MVT::i64);
600 unsigned dummy2 = MakeReg(MVT::i64);
601 unsigned dummy3 = MakeReg(MVT::i64);
602
603 BuildMI(BB, IA64::MOVLI32, 1, dummy).addImm(highPart);
604 BuildMI(BB, IA64::SHLI, 2, dummy2).addReg(dummy).addImm(32);
605 BuildMI(BB, IA64::MOVLI32, 1, dummy3).addImm(lowPart);
606 BuildMI(BB, IA64::ADD, 2, Result).addReg(dummy2).addReg(dummy3);
607 } else {
608 BuildMI(BB, IA64::MOVLI32, 1, Result).addImm(immediate);
609 }
610
611 return Result;
612 }
613
614 case ISD::GlobalAddress: {
615 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
616 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000617
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000618 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000619 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000620
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000621 return Result;
622 }
623
624 case ISD::ExternalSymbol: {
625 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +0000626// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
627 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000628 return Result;
629 }
630
631 case ISD::FP_EXTEND: {
632 Tmp1 = SelectExpr(N.getOperand(0));
633 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
634 return Result;
635 }
636
637 case ISD::ZERO_EXTEND: {
638 Tmp1 = SelectExpr(N.getOperand(0)); // value
639
640 switch (N.getOperand(0).getValueType()) {
641 default: assert(0 && "Cannot zero-extend this type!");
642 case MVT::i8: Opc = IA64::ZXT1; break;
643 case MVT::i16: Opc = IA64::ZXT2; break;
644 case MVT::i32: Opc = IA64::ZXT4; break;
645
646 // we handle bools differently! :
647 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
648 unsigned dummy = MakeReg(MVT::i64);
649 // first load zero:
650 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
651 // ...then conditionally (PR:Tmp1) add 1:
652 BuildMI(BB, IA64::CADDIMM22, 3, Result).addReg(dummy)
653 .addImm(1).addReg(Tmp1);
654 return Result; // XXX early exit!
655 }
656 }
657
658 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
659 return Result;
660 }
661
662 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
663
664assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
665
666 Tmp1 = SelectExpr(N.getOperand(0)); // value
667
668 switch (N.getOperand(0).getValueType()) {
669 default: assert(0 && "Cannot sign-extend this type!");
670 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
671 Opc = IA64::SXT1; break;
672 // FIXME: for now, we treat bools the same as i8s
673 case MVT::i8: Opc = IA64::SXT1; break;
674 case MVT::i16: Opc = IA64::SXT2; break;
675 case MVT::i32: Opc = IA64::SXT4; break;
676 }
677
678 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
679 return Result;
680 }
681
682 case ISD::TRUNCATE: {
683 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
684 // of R0 appropriately.
685 switch (N.getOperand(0).getValueType()) {
686 default: assert(0 && "Unknown truncate!");
687 case MVT::i64: break;
688 }
689 Tmp1 = SelectExpr(N.getOperand(0));
690 unsigned depositPos, depositLen;
691
692 switch (N.getValueType()) {
693 default: assert(0 && "Unknown truncate!");
694 case MVT::i1: {
695 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
696 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
697 .addReg(IA64::r0);
698 return Result; // XXX early exit!
699 }
700 case MVT::i8: depositPos=0; depositLen=8; break;
701 case MVT::i16: depositPos=0; depositLen=16; break;
702 case MVT::i32: depositPos=0; depositLen=32; break;
703 }
704 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
705 .addImm(depositPos).addImm(depositLen);
706 return Result;
707 }
708
709/*
710 case ISD::FP_ROUND: {
711 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
712 "error: trying to FP_ROUND something other than f64 -> f32!\n");
713 Tmp1 = SelectExpr(N.getOperand(0));
714 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
715 // we add 0.0 using a single precision add to do rounding
716 return Result;
717 }
718*/
719
720// FIXME: the following 4 cases need cleaning
721 case ISD::SINT_TO_FP: {
722 Tmp1 = SelectExpr(N.getOperand(0));
723 Tmp2 = MakeReg(MVT::f64);
724 unsigned dummy = MakeReg(MVT::f64);
725 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
726 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
727 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
728 return Result;
729 }
730
731 case ISD::UINT_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::FCVTXUF, 1, dummy).addReg(Tmp2);
737 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
738 return Result;
739 }
740
741 case ISD::FP_TO_SINT: {
742 Tmp1 = SelectExpr(N.getOperand(0));
743 Tmp2 = MakeReg(MVT::f64);
744 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
745 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
746 return Result;
747 }
748
749 case ISD::FP_TO_UINT: {
750 Tmp1 = SelectExpr(N.getOperand(0));
751 Tmp2 = MakeReg(MVT::f64);
752 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
753 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
754 return Result;
755 }
756
757 case ISD::ADD: {
758 Tmp1 = SelectExpr(N.getOperand(0));
759 Tmp2 = SelectExpr(N.getOperand(1));
760 if(DestType != MVT::f64)
761 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // int
762 else
763 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // FP
764 return Result;
765 }
766
767 case ISD::MUL: {
768 Tmp1 = SelectExpr(N.getOperand(0));
769 Tmp2 = SelectExpr(N.getOperand(1));
770 if(DestType != MVT::f64) { // integer multiply, emit some code (FIXME)
771 unsigned TempFR1=MakeReg(MVT::f64);
772 unsigned TempFR2=MakeReg(MVT::f64);
773 unsigned TempFR3=MakeReg(MVT::f64);
774 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
775 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
776 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
777 .addReg(IA64::F0);
778 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
779 }
780 else // floating point multiply
781 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
782 return Result;
783 }
784
785 case ISD::SUB: {
786 Tmp1 = SelectExpr(N.getOperand(0));
787 Tmp2 = SelectExpr(N.getOperand(1));
788 if(DestType != MVT::f64)
789 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
790 else
791 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
792 return Result;
793 }
794
795 case ISD::AND: {
796 switch (N.getValueType()) {
797 default: assert(0 && "Cannot AND this type!");
798 case MVT::i1: { // if a bool, we emit a pseudocode AND
799 unsigned pA = SelectExpr(N.getOperand(0));
800 unsigned pB = SelectExpr(N.getOperand(1));
801
802/* our pseudocode for AND is:
803 *
804(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
805 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
806 ;;
807(pB) cmp.ne pTemp,p0 = r0,r0
808 ;;
809(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
810
811*/
812 unsigned pTemp = MakeReg(MVT::i1);
813
814 unsigned bogusTemp1 = MakeReg(MVT::i1);
815 unsigned bogusTemp2 = MakeReg(MVT::i1);
816 unsigned bogusTemp3 = MakeReg(MVT::i1);
817 unsigned bogusTemp4 = MakeReg(MVT::i1);
818
819 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
820 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
821 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
822 .addReg(IA64::r0).addReg(IA64::r0);
823 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
824 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
825 BuildMI(BB, IA64::TPCMPNE, 3, Result)
826 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
827 break;
828 }
829 // if not a bool, we just AND away:
830 case MVT::i8:
831 case MVT::i16:
832 case MVT::i32:
833 case MVT::i64: {
834 Tmp1 = SelectExpr(N.getOperand(0));
835 Tmp2 = SelectExpr(N.getOperand(1));
836 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
837 break;
838 }
839 }
840 return Result;
841 }
842
843 case ISD::OR: {
844 switch (N.getValueType()) {
845 default: assert(0 && "Cannot OR this type!");
846 case MVT::i1: { // if a bool, we emit a pseudocode OR
847 unsigned pA = SelectExpr(N.getOperand(0));
848 unsigned pB = SelectExpr(N.getOperand(1));
849
850 unsigned pTemp1 = MakeReg(MVT::i1);
851
852/* our pseudocode for OR is:
853 *
854
855pC = pA OR pB
856-------------
857
858(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
859 ;;
860(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
861
862*/
863 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
864 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
865 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
866 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
867 break;
868 }
869 // if not a bool, we just OR away:
870 case MVT::i8:
871 case MVT::i16:
872 case MVT::i32:
873 case MVT::i64: {
874 Tmp1 = SelectExpr(N.getOperand(0));
875 Tmp2 = SelectExpr(N.getOperand(1));
876 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
877 break;
878 }
879 }
880 return Result;
881 }
882
883 case ISD::XOR: {
884 switch (N.getValueType()) {
885 default: assert(0 && "Cannot XOR this type!");
886 case MVT::i1: { // if a bool, we emit a pseudocode XOR
887 unsigned pY = SelectExpr(N.getOperand(0));
888 unsigned pZ = SelectExpr(N.getOperand(1));
889
890/* one possible routine for XOR is:
891
892 // Compute px = py ^ pz
893 // using sum of products: px = (py & !pz) | (pz & !py)
894 // Uses 5 instructions in 3 cycles.
895 // cycle 1
896(pz) cmp.eq.unc px = r0, r0 // px = pz
897(py) cmp.eq.unc pt = r0, r0 // pt = py
898 ;;
899 // cycle 2
900(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
901(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
902 ;;
903 } { .mmi
904 // cycle 3
905(pt) cmp.eq.or px = r0, r0 // px = px | pt
906
907*** Another, which we use here, requires one scratch GR. it is:
908
909 mov rt = 0 // initialize rt off critical path
910 ;;
911
912 // cycle 1
913(pz) cmp.eq.unc px = r0, r0 // px = pz
914(pz) mov rt = 1 // rt = pz
915 ;;
916 // cycle 2
917(py) cmp.ne px = 1, rt // if (py) px = !pz
918
919.. these routines kindly provided by Jim Hull
920*/
921 unsigned rt = MakeReg(MVT::i64);
922
923 // these two temporaries will never actually appear,
924 // due to the two-address form of some of the instructions below
925 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
926 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
927
928 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
929 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
930 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
931 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
932 .addReg(bogoGR).addImm(1).addReg(pZ);
933 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
934 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
935 break;
936 }
937 // if not a bool, we just XOR away:
938 case MVT::i8:
939 case MVT::i16:
940 case MVT::i32:
941 case MVT::i64: {
942 Tmp1 = SelectExpr(N.getOperand(0));
943 Tmp2 = SelectExpr(N.getOperand(1));
944 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
945 break;
946 }
947 }
948 return Result;
949 }
950
951 case ISD::SHL: {
952 Tmp1 = SelectExpr(N.getOperand(0));
953 Tmp2 = SelectExpr(N.getOperand(1));
954 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
955 return Result;
956 }
957 case ISD::SRL: {
958 Tmp1 = SelectExpr(N.getOperand(0));
959 Tmp2 = SelectExpr(N.getOperand(1));
960 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
961 return Result;
962 }
963 case ISD::SRA: {
964 Tmp1 = SelectExpr(N.getOperand(0));
965 Tmp2 = SelectExpr(N.getOperand(1));
966 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
967 return Result;
968 }
969
970 case ISD::SDIV:
971 case ISD::UDIV:
972 case ISD::SREM:
973 case ISD::UREM: {
974
975 Tmp1 = SelectExpr(N.getOperand(0));
976 Tmp2 = SelectExpr(N.getOperand(1));
977
978 bool isFP=false;
979
980 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
981 isFP=true;
982
983 bool isModulus=false; // is it a division or a modulus?
984 bool isSigned=false;
985
986 switch(N.getOpcode()) {
987 case ISD::SDIV: isModulus=false; isSigned=true; break;
988 case ISD::UDIV: isModulus=false; isSigned=false; break;
989 case ISD::SREM: isModulus=true; isSigned=true; break;
990 case ISD::UREM: isModulus=true; isSigned=false; break;
991 }
992
Duraid Madinabeeaab22005-03-31 12:31:11 +0000993 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
994 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000995 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
996 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
997 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
998 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
999 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1000 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1001 unsigned TmpF7=MakeReg(MVT::f64);
1002 unsigned TmpF8=MakeReg(MVT::f64);
1003 unsigned TmpF9=MakeReg(MVT::f64);
1004 unsigned TmpF10=MakeReg(MVT::f64);
1005 unsigned TmpF11=MakeReg(MVT::f64);
1006 unsigned TmpF12=MakeReg(MVT::f64);
1007 unsigned TmpF13=MakeReg(MVT::f64);
1008 unsigned TmpF14=MakeReg(MVT::f64);
1009 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001010
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001011 // OK, emit some code:
1012
1013 if(!isFP) {
1014 // first, load the inputs into FP regs.
1015 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1016 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1017
1018 // next, convert the inputs to FP
1019 if(isSigned) {
1020 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1021 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1022 } else {
1023 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1024 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1025 }
1026
1027 } else { // this is an FP divide/remainder, so we 'leak' some temp
1028 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1029 TmpF3=Tmp1;
1030 TmpF4=Tmp2;
1031 }
1032
1033 // we start by computing an approximate reciprocal (good to 9 bits?)
1034 // note, this instruction writes _both_ TmpF5 (answer) and tmpPR (predicate)
1035 // FIXME: or at least, it should!!
1036 BuildMI(BB, IA64::FRCPAS1FLOAT, 2, TmpF5).addReg(TmpF3).addReg(TmpF4);
1037 BuildMI(BB, IA64::FRCPAS1PREDICATE, 2, TmpPR).addReg(TmpF3).addReg(TmpF4);
1038
Duraid Madinabeeaab22005-03-31 12:31:11 +00001039 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1040 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1041 // TPCMPNE below
1042 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1043 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1044 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1045 }
1046
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001047 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1048 // precision, don't need this much for f32/i32)
1049 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1050 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1051 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1052 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1053 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1054 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1055 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1056 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1057 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1058 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1059 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1060 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1061 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1062 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1063 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1064 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
1065 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1066 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1067
1068 if(!isFP) {
1069 // round to an integer
1070 if(isSigned)
1071 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1072 else
1073 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1074 } else {
1075 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1076 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1077 // we really do need the above FMOV? ;)
1078 }
1079
1080 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001081 if(isFP) { // extra worrying about div-by-zero
1082 unsigned bogoResult=MakeReg(MVT::f64);
1083
1084 // we do a 'conditional fmov' (of the correct result, depending
1085 // on how the frcpa predicate turned out)
1086 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1087 .addReg(TmpF12).addReg(TmpPR2);
1088 BuildMI(BB, IA64::CFMOV, 2, Result)
1089 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1090 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001091 else
1092 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
1093 } else { // this is a modulus
1094 if(!isFP) {
1095 // answer = q * (-b) + a
1096 unsigned ModulusResult = MakeReg(MVT::f64);
1097 unsigned TmpF = MakeReg(MVT::f64);
1098 unsigned TmpI = MakeReg(MVT::i64);
1099 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1100 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1101 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1102 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1103 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1104 } else { // FP modulus! The horror... the horror....
1105 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1106 }
1107 }
1108
1109 return Result;
1110 }
1111
1112 case ISD::ZERO_EXTEND_INREG: {
1113 Tmp1 = SelectExpr(N.getOperand(0));
1114 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1115 switch(MVN->getExtraValueType())
1116 {
1117 default:
1118 Node->dump();
1119 assert(0 && "don't know how to zero extend this type");
1120 break;
1121 case MVT::i8: Opc = IA64::ZXT1; break;
1122 case MVT::i16: Opc = IA64::ZXT2; break;
1123 case MVT::i32: Opc = IA64::ZXT4; break;
1124 }
1125 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1126 return Result;
1127 }
1128
1129 case ISD::SIGN_EXTEND_INREG: {
1130 Tmp1 = SelectExpr(N.getOperand(0));
1131 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1132 switch(MVN->getExtraValueType())
1133 {
1134 default:
1135 Node->dump();
1136 assert(0 && "don't know how to sign extend this type");
1137 break;
1138 case MVT::i8: Opc = IA64::SXT1; break;
1139 case MVT::i16: Opc = IA64::SXT2; break;
1140 case MVT::i32: Opc = IA64::SXT4; break;
1141 }
1142 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1143 return Result;
1144 }
1145
1146 case ISD::SETCC: {
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 Tmp2 = SelectExpr(N.getOperand(1));
1149 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1150 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1151 switch (SetCC->getCondition()) {
1152 default: assert(0 && "Unknown integer comparison!");
1153 case ISD::SETEQ:
1154 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1155 break;
1156 case ISD::SETGT:
1157 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1158 break;
1159 case ISD::SETGE:
1160 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1161 break;
1162 case ISD::SETLT:
1163 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1164 break;
1165 case ISD::SETLE:
1166 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1167 break;
1168 case ISD::SETNE:
1169 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1170 break;
1171 case ISD::SETULT:
1172 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1173 break;
1174 case ISD::SETUGT:
1175 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1176 break;
1177 case ISD::SETULE:
1178 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1179 break;
1180 case ISD::SETUGE:
1181 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1182 break;
1183 }
1184 }
1185 else { // if not integer, should be FP. FIXME: what about bools? ;)
1186 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1187 "error: SETCC should have had incoming f32 promoted to f64!\n");
1188 switch (SetCC->getCondition()) {
1189 default: assert(0 && "Unknown FP comparison!");
1190 case ISD::SETEQ:
1191 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1192 break;
1193 case ISD::SETGT:
1194 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1195 break;
1196 case ISD::SETGE:
1197 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1198 break;
1199 case ISD::SETLT:
1200 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1201 break;
1202 case ISD::SETLE:
1203 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1204 break;
1205 case ISD::SETNE:
1206 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1207 break;
1208 case ISD::SETULT:
1209 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1210 break;
1211 case ISD::SETUGT:
1212 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1213 break;
1214 case ISD::SETULE:
1215 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1216 break;
1217 case ISD::SETUGE:
1218 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1219 break;
1220 }
1221 }
1222 }
1223 else
1224 assert(0 && "this setcc not implemented yet");
1225
1226 return Result;
1227 }
1228
1229 case ISD::EXTLOAD:
1230 case ISD::ZEXTLOAD:
1231 case ISD::LOAD: {
1232 // Make sure we generate both values.
1233 if (Result != 1)
1234 ExprMap[N.getValue(1)] = 1; // Generate the token
1235 else
1236 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1237
1238 bool isBool=false;
1239
1240 if(opcode == ISD::LOAD) { // this is a LOAD
1241 switch (Node->getValueType(0)) {
1242 default: assert(0 && "Cannot load this type!");
1243 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1244 // FIXME: for now, we treat bool loads the same as i8 loads */
1245 case MVT::i8: Opc = IA64::LD1; break;
1246 case MVT::i16: Opc = IA64::LD2; break;
1247 case MVT::i32: Opc = IA64::LD4; break;
1248 case MVT::i64: Opc = IA64::LD8; break;
1249
1250 case MVT::f32: Opc = IA64::LDF4; break;
1251 case MVT::f64: Opc = IA64::LDF8; break;
1252 }
1253 } else { // this is an EXTLOAD or ZEXTLOAD
1254 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1255 switch (TypeBeingLoaded) {
1256 default: assert(0 && "Cannot extload/zextload this type!");
1257 // FIXME: bools?
1258 case MVT::i8: Opc = IA64::LD1; break;
1259 case MVT::i16: Opc = IA64::LD2; break;
1260 case MVT::i32: Opc = IA64::LD4; break;
1261 case MVT::f32: Opc = IA64::LDF4; break;
1262 }
1263 }
1264
1265 SDOperand Chain = N.getOperand(0);
1266 SDOperand Address = N.getOperand(1);
1267
1268 if(Address.getOpcode() == ISD::GlobalAddress) {
1269 Select(Chain);
1270 unsigned dummy = MakeReg(MVT::i64);
1271 unsigned dummy2 = MakeReg(MVT::i64);
1272 BuildMI(BB, IA64::ADD, 2, dummy)
1273 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1274 .addReg(IA64::r1);
1275 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1276 if(!isBool)
1277 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1278 else { // emit a little pseudocode to load a bool (stored in one byte)
1279 // into a predicate register
1280 assert(Opc==IA64::LD1 && "problem loading a bool");
1281 unsigned dummy3 = MakeReg(MVT::i64);
1282 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1283 // we compare to 0. true? 0. false? 1.
1284 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1285 }
1286 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1287 Select(Chain);
1288 IA64Lowering.restoreGP(BB);
1289 unsigned dummy = MakeReg(MVT::i64);
1290 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1291 .addReg(IA64::r1); // CPI+GP
1292 if(!isBool)
1293 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1294 else { // emit a little pseudocode to load a bool (stored in one byte)
1295 // into a predicate register
1296 assert(Opc==IA64::LD1 && "problem loading a bool");
1297 unsigned dummy3 = MakeReg(MVT::i64);
1298 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1299 // we compare to 0. true? 0. false? 1.
1300 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1301 }
1302 } else if(Address.getOpcode() == ISD::FrameIndex) {
1303 Select(Chain); // FIXME ? what about bools?
1304 unsigned dummy = MakeReg(MVT::i64);
1305 BuildMI(BB, IA64::MOV, 1, dummy)
1306 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1307 if(!isBool)
1308 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1309 else { // emit a little pseudocode to load a bool (stored in one byte)
1310 // into a predicate register
1311 assert(Opc==IA64::LD1 && "problem loading a bool");
1312 unsigned dummy3 = MakeReg(MVT::i64);
1313 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1314 // we compare to 0. true? 0. false? 1.
1315 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1316 }
1317 } else { // none of the above...
1318 Select(Chain);
1319 Tmp2 = SelectExpr(Address);
1320 if(!isBool)
1321 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1322 else { // emit a little pseudocode to load a bool (stored in one byte)
1323 // into a predicate register
1324 assert(Opc==IA64::LD1 && "problem loading a bool");
1325 unsigned dummy = MakeReg(MVT::i64);
1326 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1327 // we compare to 0. true? 0. false? 1.
1328 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1329 }
1330 }
1331
1332 return Result;
1333 }
1334
1335 case ISD::CopyFromReg: {
1336 if (Result == 1)
1337 Result = ExprMap[N.getValue(0)] =
1338 MakeReg(N.getValue(0).getValueType());
1339
1340 SDOperand Chain = N.getOperand(0);
1341
1342 Select(Chain);
1343 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1344
1345 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1346 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1347 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1348 // (r) Result =cmp.eq.unc(r0,r0)
1349 else
1350 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1351 return Result;
1352 }
1353
1354 case ISD::CALL: {
1355 Select(N.getOperand(0));
1356
1357 // The chain for this call is now lowered.
1358 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1359
1360 //grab the arguments
1361 std::vector<unsigned> argvregs;
1362
1363 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1364 argvregs.push_back(SelectExpr(N.getOperand(i)));
1365
1366 // see section 8.5.8 of "Itanium Software Conventions and
1367 // Runtime Architecture Guide to see some examples of what's going
1368 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1369 // while FP args get mapped to F8->F15 as needed)
1370
1371 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1372
1373 // in reg args
1374 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1375 {
1376 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1377 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1378 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1379 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1380
1381 switch(N.getOperand(i+2).getValueType())
1382 {
1383 default: // XXX do we need to support MVT::i1 here?
1384 Node->dump();
1385 N.getOperand(i).Val->dump();
1386 std::cerr << "Type for " << i << " is: " <<
1387 N.getOperand(i+2).getValueType() << std::endl;
1388 assert(0 && "Unknown value type for call");
1389 case MVT::i64:
1390 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1391 break;
1392 case MVT::f64:
1393 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1394 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001395 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001396 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1397 break;
1398 }
1399 }
1400
1401 //in mem args
1402 for (int i = 8, e = argvregs.size(); i < e; ++i)
1403 {
1404 unsigned tempAddr = MakeReg(MVT::i64);
1405
1406 switch(N.getOperand(i+2).getValueType()) {
1407 default:
1408 Node->dump();
1409 N.getOperand(i).Val->dump();
1410 std::cerr << "Type for " << i << " is: " <<
1411 N.getOperand(i+2).getValueType() << "\n";
1412 assert(0 && "Unknown value type for call");
1413 case MVT::i1: // FIXME?
1414 case MVT::i8:
1415 case MVT::i16:
1416 case MVT::i32:
1417 case MVT::i64:
1418 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1419 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1420 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1421 break;
1422 case MVT::f32:
1423 case MVT::f64:
1424 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1425 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1426 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1427 break;
1428 }
1429 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001430
1431 /* XXX we want to re-enable direct branches! crippling them now
1432 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001433 //build the right kind of call
1434 if (GlobalAddressSDNode *GASD =
1435 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1436 {
1437 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1438 IA64Lowering.restoreGP_SP_RP(BB);
1439 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001440 ^^^^^^^^^^^^^ we want this code one day XXX */
1441 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001442 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001443 { // FIXME : currently need this case for correctness, to avoid
1444 // "non-pic code with imm relocation against dynamic symbol" errors
1445 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001446 .addExternalSymbol(ESSDN->getSymbol(), true);
1447 IA64Lowering.restoreGP_SP_RP(BB);
1448 }
1449 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001450 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001451
1452 unsigned targetEntryPoint=MakeReg(MVT::i64);
1453 unsigned targetGPAddr=MakeReg(MVT::i64);
1454 unsigned currentGP=MakeReg(MVT::i64);
1455
1456 // b6 is a scratch branch register, we load the target entry point
1457 // from the base of the function descriptor
1458 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1459 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1460
1461 // save the current GP:
1462 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1463
1464 /* TODO: we need to make sure doing this never, ever loads a
1465 * bogus value into r1 (GP). */
1466 // load the target GP (which is at mem[functiondescriptor+8])
1467 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1468 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1469 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1470
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001471 // and then jump: (well, call)
1472 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001473 // and finally restore the old GP
1474 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1475 IA64Lowering.restoreSP_RP(BB);
1476 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001477
1478 switch (Node->getValueType(0)) {
1479 default: assert(0 && "Unknown value type for call result!");
1480 case MVT::Other: return 1;
1481 case MVT::i1:
1482 BuildMI(BB, IA64::CMPNE, 2, Result)
1483 .addReg(IA64::r8).addReg(IA64::r0);
1484 break;
1485 case MVT::i8:
1486 case MVT::i16:
1487 case MVT::i32:
1488 case MVT::i64:
1489 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1490 break;
1491 case MVT::f64:
1492 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1493 break;
1494 }
1495 return Result+N.ResNo;
1496 }
1497
1498 } // <- uhhh XXX
1499 return 0;
1500}
1501
1502void ISel::Select(SDOperand N) {
1503 unsigned Tmp1, Tmp2, Opc;
1504 unsigned opcode = N.getOpcode();
1505
Nate Begeman85fdeb22005-03-24 04:39:54 +00001506 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001507 return; // Already selected.
1508
1509 SDNode *Node = N.Val;
1510
1511 switch (Node->getOpcode()) {
1512 default:
1513 Node->dump(); std::cerr << "\n";
1514 assert(0 && "Node not handled yet!");
1515
1516 case ISD::EntryToken: return; // Noop
1517
1518 case ISD::TokenFactor: {
1519 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1520 Select(Node->getOperand(i));
1521 return;
1522 }
1523
1524 case ISD::CopyToReg: {
1525 Select(N.getOperand(0));
1526 Tmp1 = SelectExpr(N.getOperand(1));
1527 Tmp2 = cast<RegSDNode>(N)->getReg();
1528
1529 if (Tmp1 != Tmp2) {
1530 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1531 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1532 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1533 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1534 else
1535 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1536 // XXX is this the right way 'round? ;)
1537 }
1538 return;
1539 }
1540
1541 case ISD::RET: {
1542
1543 /* what the heck is going on here:
1544
1545<_sabre_> ret with two operands is obvious: chain and value
1546<camel_> yep
1547<_sabre_> ret with 3 values happens when 'expansion' occurs
1548<_sabre_> e.g. i64 gets split into 2x i32
1549<camel_> oh right
1550<_sabre_> you don't have this case on ia64
1551<camel_> yep
1552<_sabre_> so the two returned values go into EAX/EDX on ia32
1553<camel_> ahhh *memories*
1554<_sabre_> :)
1555<camel_> ok, thanks :)
1556<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1557<_sabre_> this is the first operand always
1558<_sabre_> these operand often define chains, they are the last operand
1559<_sabre_> they are printed as 'ch' if you do DAG.dump()
1560 */
1561
1562 switch (N.getNumOperands()) {
1563 default:
1564 assert(0 && "Unknown return instruction!");
1565 case 2:
1566 Select(N.getOperand(0));
1567 Tmp1 = SelectExpr(N.getOperand(1));
1568 switch (N.getOperand(1).getValueType()) {
1569 default: assert(0 && "All other types should have been promoted!!");
1570 // FIXME: do I need to add support for bools here?
1571 // (return '0' or '1' r8, basically...)
1572 case MVT::i64:
1573 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1574 break;
1575 case MVT::f64:
1576 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1577 }
1578 break;
1579 case 1:
1580 Select(N.getOperand(0));
1581 break;
1582 }
1583 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1584 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1585 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1586 return;
1587 }
1588
1589 case ISD::BR: {
1590 Select(N.getOperand(0));
1591 MachineBasicBlock *Dest =
1592 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1593 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1594 // XXX HACK! we do _not_ need long branches all the time
1595 return;
1596 }
1597
1598 case ISD::ImplicitDef: {
1599 Select(N.getOperand(0));
1600 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1601 return;
1602 }
1603
1604 case ISD::BRCOND: {
1605 MachineBasicBlock *Dest =
1606 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1607
1608 Select(N.getOperand(0));
1609 Tmp1 = SelectExpr(N.getOperand(1));
1610 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1611 // XXX HACK! we do _not_ need long branches all the time
1612 return;
1613 }
1614
1615 case ISD::EXTLOAD:
1616 case ISD::ZEXTLOAD:
1617 case ISD::SEXTLOAD:
1618 case ISD::LOAD:
1619 case ISD::CALL:
1620 case ISD::CopyFromReg:
1621 case ISD::DYNAMIC_STACKALLOC:
1622 SelectExpr(N);
1623 return;
1624
1625 case ISD::TRUNCSTORE:
1626 case ISD::STORE: {
1627 Select(N.getOperand(0));
1628 Tmp1 = SelectExpr(N.getOperand(1)); // value
1629
1630 bool isBool=false;
1631
1632 if(opcode == ISD::STORE) {
1633 switch (N.getOperand(1).getValueType()) {
1634 default: assert(0 && "Cannot store this type!");
1635 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1636 // FIXME?: for now, we treat bool loads the same as i8 stores */
1637 case MVT::i8: Opc = IA64::ST1; break;
1638 case MVT::i16: Opc = IA64::ST2; break;
1639 case MVT::i32: Opc = IA64::ST4; break;
1640 case MVT::i64: Opc = IA64::ST8; break;
1641
1642 case MVT::f32: Opc = IA64::STF4; break;
1643 case MVT::f64: Opc = IA64::STF8; break;
1644 }
1645 } else { // truncstore
1646 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1647 default: assert(0 && "unknown type in truncstore");
1648 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1649 //FIXME: DAG does not promote this load?
1650 case MVT::i8: Opc = IA64::ST1; break;
1651 case MVT::i16: Opc = IA64::ST2; break;
1652 case MVT::i32: Opc = IA64::ST4; break;
1653 case MVT::f32: Opc = IA64::STF4; break;
1654 }
1655 }
1656
1657 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1658 unsigned dummy = MakeReg(MVT::i64);
1659 unsigned dummy2 = MakeReg(MVT::i64);
1660 BuildMI(BB, IA64::ADD, 2, dummy)
1661 .addGlobalAddress(cast<GlobalAddressSDNode>
1662 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1663 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1664
1665 if(!isBool)
1666 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1667 else { // we are storing a bool, so emit a little pseudocode
1668 // to store a predicate register as one byte
1669 assert(Opc==IA64::ST1);
1670 unsigned dummy3 = MakeReg(MVT::i64);
1671 unsigned dummy4 = MakeReg(MVT::i64);
1672 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1673 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1674 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1675 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1676 }
1677 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1678
1679 // FIXME? (what about bools?)
1680
1681 unsigned dummy = MakeReg(MVT::i64);
1682 BuildMI(BB, IA64::MOV, 1, dummy)
1683 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1684 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1685 } else { // otherwise
1686 Tmp2 = SelectExpr(N.getOperand(2)); //address
1687 if(!isBool)
1688 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1689 else { // we are storing a bool, so emit a little pseudocode
1690 // to store a predicate register as one byte
1691 assert(Opc==IA64::ST1);
1692 unsigned dummy3 = MakeReg(MVT::i64);
1693 unsigned dummy4 = MakeReg(MVT::i64);
1694 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1695 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1696 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1697 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1698 }
1699 }
1700 return;
1701 }
1702
1703 case ISD::ADJCALLSTACKDOWN:
1704 case ISD::ADJCALLSTACKUP: {
1705 Select(N.getOperand(0));
1706 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1707
1708 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1709 IA64::ADJUSTCALLSTACKUP;
1710 BuildMI(BB, Opc, 1).addImm(Tmp1);
1711 return;
1712 }
1713
1714 return;
1715 }
1716 assert(0 && "GAME OVER. INSERT COIN?");
1717}
1718
1719
1720/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1721/// into a machine code representation using pattern matching and a machine
1722/// description file.
1723///
1724FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1725 return new ISel(TM);
1726}
1727
1728