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