blob: 0c34c10b6f16a9772e741728ffeaa37b3d454f6a [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
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000409 unsigned SelectExpr(SDOperand N);
410 void Select(SDOperand N);
411 };
412}
413
414/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
415/// when it has created a SelectionDAG for us to codegen.
416void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
417
418 // Codegen the basic block.
419 Select(DAG.getRoot());
420
421 // Clear state used for selection.
422 ExprMap.clear();
423 LoweredTokens.clear();
424}
425
Duraid Madina4826a072005-04-06 09:55:17 +0000426/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
427/// returns zero when the input is not exactly a power of two.
428static uint64_t ExactLog2(uint64_t Val) {
429 if (Val == 0 || (Val & (Val-1))) return 0;
430 unsigned Count = 0;
431 while (Val != 1) {
432 Val >>= 1;
433 ++Count;
434 }
435 return Count;
436}
437
438/// ponderIntegerDivisionBy - When handling integer divides, if the divide
439/// is by a constant such that we can efficiently codegen it, this
440/// function says what to do. Currently, it returns 0 if the division must
441/// become a genuine divide, and 1 if the division can be turned into a
442/// right shift.
443static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
444 unsigned& Imm) {
445 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
446 // a constant, give up.
447
448 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
449
450 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
451 return 1;
452 }
453
454 return 0; // fallthrough
455}
456
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000457unsigned ISel::SelectExpr(SDOperand N) {
458 unsigned Result;
459 unsigned Tmp1, Tmp2, Tmp3;
460 unsigned Opc = 0;
461 MVT::ValueType DestType = N.getValueType();
462
463 unsigned opcode = N.getOpcode();
464
465 SDNode *Node = N.Val;
466 SDOperand Op0, Op1;
467
468 if (Node->getOpcode() == ISD::CopyFromReg)
469 // Just use the specified register as our input.
470 return dyn_cast<RegSDNode>(Node)->getReg();
471
472 unsigned &Reg = ExprMap[N];
473 if (Reg) return Reg;
474
475 if (N.getOpcode() != ISD::CALL)
476 Reg = Result = (N.getValueType() != MVT::Other) ?
477 MakeReg(N.getValueType()) : 1;
478 else {
479 // If this is a call instruction, make sure to prepare ALL of the result
480 // values as well as the chain.
481 if (Node->getNumValues() == 1)
482 Reg = Result = 1; // Void call, just a chain.
483 else {
484 Result = MakeReg(Node->getValueType(0));
485 ExprMap[N.getValue(0)] = Result;
486 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
487 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
488 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
489 }
490 }
491
492 switch (N.getOpcode()) {
493 default:
494 Node->dump();
495 assert(0 && "Node not handled!\n");
496
497 case ISD::FrameIndex: {
498 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
499 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
500 return Result;
501 }
502
503 case ISD::ConstantPool: {
504 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
505 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
506 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
507 .addReg(IA64::r1);
508 return Result;
509 }
510
511 case ISD::ConstantFP: {
512 Tmp1 = Result; // Intermediate Register
513 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
514 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
515 Tmp1 = MakeReg(MVT::f64);
516
517 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
518 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
519 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
520 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
521 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
522 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
523 else
524 assert(0 && "Unexpected FP constant!");
525 if (Tmp1 != Result)
526 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
527 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
528 .addReg(IA64::F0);
529 return Result;
530 }
531
532 case ISD::DYNAMIC_STACKALLOC: {
533 // Generate both result values.
534 if (Result != 1)
535 ExprMap[N.getValue(1)] = 1; // Generate the token
536 else
537 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
538
539 // FIXME: We are currently ignoring the requested alignment for handling
540 // greater than the stack alignment. This will need to be revisited at some
541 // point. Align = N.getOperand(2);
542
543 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
544 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
545 std::cerr << "Cannot allocate stack object with greater alignment than"
546 << " the stack alignment yet!";
547 abort();
548 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000549
550/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000551 Select(N.getOperand(0));
552 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
553 {
554 if (CN->getValue() < 32000)
555 {
556 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
557 .addImm(-CN->getValue());
558 } else {
559 Tmp1 = SelectExpr(N.getOperand(1));
560 // Subtract size from stack pointer, thereby allocating some space.
561 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
562 }
563 } else {
564 Tmp1 = SelectExpr(N.getOperand(1));
565 // Subtract size from stack pointer, thereby allocating some space.
566 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
567 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000568*/
569 Select(N.getOperand(0));
570 Tmp1 = SelectExpr(N.getOperand(1));
571 // Subtract size from stack pointer, thereby allocating some space.
572 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000573 // Put a pointer to the space into the result register, by copying the
574 // stack pointer.
575 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
576 return Result;
577 }
578
579 case ISD::SELECT: {
580 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
581 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
582 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
583
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000584 unsigned bogoResult;
585
586 switch (N.getOperand(1).getValueType()) {
587 default: assert(0 &&
588 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
589 case MVT::i64:
590 bogoResult=MakeReg(MVT::i64);
591 break;
592 case MVT::f64:
593 bogoResult=MakeReg(MVT::f64);
594 break;
595 }
Duraid Madina69c8e202005-04-01 10:35:00 +0000596
597 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
598 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
599 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
600 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000601 return Result;
602 }
603
604 case ISD::Constant: {
605 unsigned depositPos=0;
606 unsigned depositLen=0;
607 switch (N.getValueType()) {
608 default: assert(0 && "Cannot use constants of this type!");
609 case MVT::i1: { // if a bool, we don't 'load' so much as generate
610 // the constant:
611 if(cast<ConstantSDNode>(N)->getValue()) // true:
612 BuildMI(BB, IA64::CMPEQ, 2, Result)
613 .addReg(IA64::r0).addReg(IA64::r0);
614 else // false:
615 BuildMI(BB, IA64::CMPNE, 2, Result)
616 .addReg(IA64::r0).addReg(IA64::r0);
617 return Result;
618 }
619 case MVT::i64: Opc = IA64::MOVLI32; break;
620 }
621
622 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
623 if(immediate>>32) { // if our immediate really is big:
624 int highPart = immediate>>32;
625 int lowPart = immediate&0xFFFFFFFF;
626 unsigned dummy = MakeReg(MVT::i64);
627 unsigned dummy2 = MakeReg(MVT::i64);
628 unsigned dummy3 = MakeReg(MVT::i64);
629
630 BuildMI(BB, IA64::MOVLI32, 1, dummy).addImm(highPart);
631 BuildMI(BB, IA64::SHLI, 2, dummy2).addReg(dummy).addImm(32);
632 BuildMI(BB, IA64::MOVLI32, 1, dummy3).addImm(lowPart);
633 BuildMI(BB, IA64::ADD, 2, Result).addReg(dummy2).addReg(dummy3);
634 } else {
635 BuildMI(BB, IA64::MOVLI32, 1, Result).addImm(immediate);
636 }
637
638 return Result;
639 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000640
641 case ISD::UNDEF: {
642 BuildMI(BB, IA64::IDEF, 0, Result);
643 return Result;
644 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000645
646 case ISD::GlobalAddress: {
647 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
648 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000649
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000650 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000651 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000652
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000653 return Result;
654 }
655
656 case ISD::ExternalSymbol: {
657 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +0000658// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
659 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000660 return Result;
661 }
662
663 case ISD::FP_EXTEND: {
664 Tmp1 = SelectExpr(N.getOperand(0));
665 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
666 return Result;
667 }
668
669 case ISD::ZERO_EXTEND: {
670 Tmp1 = SelectExpr(N.getOperand(0)); // value
671
672 switch (N.getOperand(0).getValueType()) {
673 default: assert(0 && "Cannot zero-extend this type!");
674 case MVT::i8: Opc = IA64::ZXT1; break;
675 case MVT::i16: Opc = IA64::ZXT2; break;
676 case MVT::i32: Opc = IA64::ZXT4; break;
677
678 // we handle bools differently! :
679 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
680 unsigned dummy = MakeReg(MVT::i64);
681 // first load zero:
682 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
683 // ...then conditionally (PR:Tmp1) add 1:
684 BuildMI(BB, IA64::CADDIMM22, 3, Result).addReg(dummy)
685 .addImm(1).addReg(Tmp1);
686 return Result; // XXX early exit!
687 }
688 }
689
690 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
691 return Result;
692 }
693
694 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
695
696assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
697
698 Tmp1 = SelectExpr(N.getOperand(0)); // value
699
700 switch (N.getOperand(0).getValueType()) {
701 default: assert(0 && "Cannot sign-extend this type!");
702 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
703 Opc = IA64::SXT1; break;
704 // FIXME: for now, we treat bools the same as i8s
705 case MVT::i8: Opc = IA64::SXT1; break;
706 case MVT::i16: Opc = IA64::SXT2; break;
707 case MVT::i32: Opc = IA64::SXT4; break;
708 }
709
710 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
711 return Result;
712 }
713
714 case ISD::TRUNCATE: {
715 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
716 // of R0 appropriately.
717 switch (N.getOperand(0).getValueType()) {
718 default: assert(0 && "Unknown truncate!");
719 case MVT::i64: break;
720 }
721 Tmp1 = SelectExpr(N.getOperand(0));
722 unsigned depositPos, depositLen;
723
724 switch (N.getValueType()) {
725 default: assert(0 && "Unknown truncate!");
726 case MVT::i1: {
727 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
728 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
729 .addReg(IA64::r0);
730 return Result; // XXX early exit!
731 }
732 case MVT::i8: depositPos=0; depositLen=8; break;
733 case MVT::i16: depositPos=0; depositLen=16; break;
734 case MVT::i32: depositPos=0; depositLen=32; break;
735 }
736 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
737 .addImm(depositPos).addImm(depositLen);
738 return Result;
739 }
740
741/*
742 case ISD::FP_ROUND: {
743 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
744 "error: trying to FP_ROUND something other than f64 -> f32!\n");
745 Tmp1 = SelectExpr(N.getOperand(0));
746 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
747 // we add 0.0 using a single precision add to do rounding
748 return Result;
749 }
750*/
751
752// FIXME: the following 4 cases need cleaning
753 case ISD::SINT_TO_FP: {
754 Tmp1 = SelectExpr(N.getOperand(0));
755 Tmp2 = MakeReg(MVT::f64);
756 unsigned dummy = MakeReg(MVT::f64);
757 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
758 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
759 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
760 return Result;
761 }
762
763 case ISD::UINT_TO_FP: {
764 Tmp1 = SelectExpr(N.getOperand(0));
765 Tmp2 = MakeReg(MVT::f64);
766 unsigned dummy = MakeReg(MVT::f64);
767 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
768 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
769 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
770 return Result;
771 }
772
773 case ISD::FP_TO_SINT: {
774 Tmp1 = SelectExpr(N.getOperand(0));
775 Tmp2 = MakeReg(MVT::f64);
776 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
777 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
778 return Result;
779 }
780
781 case ISD::FP_TO_UINT: {
782 Tmp1 = SelectExpr(N.getOperand(0));
783 Tmp2 = MakeReg(MVT::f64);
784 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
785 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
786 return Result;
787 }
788
789 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +0000790 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
791 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
792 // into an fma, do so:
793 // ++FusedFP; // Statistic
794 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
795 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
796 Tmp3 = SelectExpr(N.getOperand(1));
797 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
798 return Result; // early exit
799 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000800 Tmp1 = SelectExpr(N.getOperand(0));
801 Tmp2 = SelectExpr(N.getOperand(1));
802 if(DestType != MVT::f64)
803 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // int
804 else
805 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2); // FP
806 return Result;
807 }
808
809 case ISD::MUL: {
810 Tmp1 = SelectExpr(N.getOperand(0));
811 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina4826a072005-04-06 09:55:17 +0000812
813 if(DestType != MVT::f64) { // TODO: speed!
814 // boring old integer multiply with xma
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000815 unsigned TempFR1=MakeReg(MVT::f64);
816 unsigned TempFR2=MakeReg(MVT::f64);
817 unsigned TempFR3=MakeReg(MVT::f64);
818 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
819 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
820 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
821 .addReg(IA64::F0);
822 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
823 }
824 else // floating point multiply
825 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
826 return Result;
827 }
828
829 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +0000830 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
831 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
832 // into an fms, do so:
833 // ++FusedFP; // Statistic
834 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
835 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
836 Tmp3 = SelectExpr(N.getOperand(1));
837 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
838 return Result; // early exit
839 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000840 Tmp1 = SelectExpr(N.getOperand(0));
841 Tmp2 = SelectExpr(N.getOperand(1));
842 if(DestType != MVT::f64)
843 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
844 else
845 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
846 return Result;
847 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000848
849 case ISD::FABS: {
850 Tmp1 = SelectExpr(N.getOperand(0));
851 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
852 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
853 return Result;
854 }
855
856 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000857 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000858
859 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
860 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
861 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
862 } else {
863 Tmp1 = SelectExpr(N.getOperand(0));
864 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
865 }
866
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000867 return Result;
868 }
869
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000870 case ISD::AND: {
871 switch (N.getValueType()) {
872 default: assert(0 && "Cannot AND this type!");
873 case MVT::i1: { // if a bool, we emit a pseudocode AND
874 unsigned pA = SelectExpr(N.getOperand(0));
875 unsigned pB = SelectExpr(N.getOperand(1));
876
877/* our pseudocode for AND is:
878 *
879(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
880 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
881 ;;
882(pB) cmp.ne pTemp,p0 = r0,r0
883 ;;
884(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
885
886*/
887 unsigned pTemp = MakeReg(MVT::i1);
888
889 unsigned bogusTemp1 = MakeReg(MVT::i1);
890 unsigned bogusTemp2 = MakeReg(MVT::i1);
891 unsigned bogusTemp3 = MakeReg(MVT::i1);
892 unsigned bogusTemp4 = MakeReg(MVT::i1);
893
894 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
895 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
896 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
897 .addReg(IA64::r0).addReg(IA64::r0);
898 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
899 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
900 BuildMI(BB, IA64::TPCMPNE, 3, Result)
901 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
902 break;
903 }
904 // if not a bool, we just AND away:
905 case MVT::i8:
906 case MVT::i16:
907 case MVT::i32:
908 case MVT::i64: {
909 Tmp1 = SelectExpr(N.getOperand(0));
910 Tmp2 = SelectExpr(N.getOperand(1));
911 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
912 break;
913 }
914 }
915 return Result;
916 }
917
918 case ISD::OR: {
919 switch (N.getValueType()) {
920 default: assert(0 && "Cannot OR this type!");
921 case MVT::i1: { // if a bool, we emit a pseudocode OR
922 unsigned pA = SelectExpr(N.getOperand(0));
923 unsigned pB = SelectExpr(N.getOperand(1));
924
925 unsigned pTemp1 = MakeReg(MVT::i1);
926
927/* our pseudocode for OR is:
928 *
929
930pC = pA OR pB
931-------------
932
933(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
934 ;;
935(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
936
937*/
938 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
939 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
940 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
941 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
942 break;
943 }
944 // if not a bool, we just OR away:
945 case MVT::i8:
946 case MVT::i16:
947 case MVT::i32:
948 case MVT::i64: {
949 Tmp1 = SelectExpr(N.getOperand(0));
950 Tmp2 = SelectExpr(N.getOperand(1));
951 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
952 break;
953 }
954 }
955 return Result;
956 }
957
958 case ISD::XOR: {
959 switch (N.getValueType()) {
960 default: assert(0 && "Cannot XOR this type!");
961 case MVT::i1: { // if a bool, we emit a pseudocode XOR
962 unsigned pY = SelectExpr(N.getOperand(0));
963 unsigned pZ = SelectExpr(N.getOperand(1));
964
965/* one possible routine for XOR is:
966
967 // Compute px = py ^ pz
968 // using sum of products: px = (py & !pz) | (pz & !py)
969 // Uses 5 instructions in 3 cycles.
970 // cycle 1
971(pz) cmp.eq.unc px = r0, r0 // px = pz
972(py) cmp.eq.unc pt = r0, r0 // pt = py
973 ;;
974 // cycle 2
975(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
976(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
977 ;;
978 } { .mmi
979 // cycle 3
980(pt) cmp.eq.or px = r0, r0 // px = px | pt
981
982*** Another, which we use here, requires one scratch GR. it is:
983
984 mov rt = 0 // initialize rt off critical path
985 ;;
986
987 // cycle 1
988(pz) cmp.eq.unc px = r0, r0 // px = pz
989(pz) mov rt = 1 // rt = pz
990 ;;
991 // cycle 2
992(py) cmp.ne px = 1, rt // if (py) px = !pz
993
994.. these routines kindly provided by Jim Hull
995*/
996 unsigned rt = MakeReg(MVT::i64);
997
998 // these two temporaries will never actually appear,
999 // due to the two-address form of some of the instructions below
1000 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1001 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1002
1003 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1004 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
1005 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
1006 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
1007 .addReg(bogoGR).addImm(1).addReg(pZ);
1008 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
1009 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
1010 break;
1011 }
1012 // if not a bool, we just XOR away:
1013 case MVT::i8:
1014 case MVT::i16:
1015 case MVT::i32:
1016 case MVT::i64: {
1017 Tmp1 = SelectExpr(N.getOperand(0));
1018 Tmp2 = SelectExpr(N.getOperand(1));
1019 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1020 break;
1021 }
1022 }
1023 return Result;
1024 }
1025
1026 case ISD::SHL: {
1027 Tmp1 = SelectExpr(N.getOperand(0));
1028 Tmp2 = SelectExpr(N.getOperand(1));
1029 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1030 return Result;
1031 }
1032 case ISD::SRL: {
1033 Tmp1 = SelectExpr(N.getOperand(0));
1034 Tmp2 = SelectExpr(N.getOperand(1));
1035 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1036 return Result;
1037 }
1038 case ISD::SRA: {
1039 Tmp1 = SelectExpr(N.getOperand(0));
1040 Tmp2 = SelectExpr(N.getOperand(1));
1041 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1042 return Result;
1043 }
1044
1045 case ISD::SDIV:
1046 case ISD::UDIV:
1047 case ISD::SREM:
1048 case ISD::UREM: {
1049
1050 Tmp1 = SelectExpr(N.getOperand(0));
1051 Tmp2 = SelectExpr(N.getOperand(1));
1052
1053 bool isFP=false;
1054
1055 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1056 isFP=true;
1057
1058 bool isModulus=false; // is it a division or a modulus?
1059 bool isSigned=false;
1060
1061 switch(N.getOpcode()) {
1062 case ISD::SDIV: isModulus=false; isSigned=true; break;
1063 case ISD::UDIV: isModulus=false; isSigned=false; break;
1064 case ISD::SREM: isModulus=true; isSigned=true; break;
1065 case ISD::UREM: isModulus=true; isSigned=false; break;
1066 }
1067
Duraid Madina4826a072005-04-06 09:55:17 +00001068 if(!isModulus && !isFP) { // if this is an integer divide,
1069 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
1070 case 1: // division by a constant that's a power of 2
1071 Tmp1 = SelectExpr(N.getOperand(0));
1072 if(isSigned) // becomes a shift right:
1073 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addImm(Tmp3);
1074 else
1075 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1076 return Result; // early exit
1077 }
1078 }
1079
Duraid Madinabeeaab22005-03-31 12:31:11 +00001080 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
1081 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001082 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1083 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1084 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1085 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1086 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1087 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1088 unsigned TmpF7=MakeReg(MVT::f64);
1089 unsigned TmpF8=MakeReg(MVT::f64);
1090 unsigned TmpF9=MakeReg(MVT::f64);
1091 unsigned TmpF10=MakeReg(MVT::f64);
1092 unsigned TmpF11=MakeReg(MVT::f64);
1093 unsigned TmpF12=MakeReg(MVT::f64);
1094 unsigned TmpF13=MakeReg(MVT::f64);
1095 unsigned TmpF14=MakeReg(MVT::f64);
1096 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001097
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001098 // OK, emit some code:
1099
1100 if(!isFP) {
1101 // first, load the inputs into FP regs.
1102 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1103 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1104
1105 // next, convert the inputs to FP
1106 if(isSigned) {
1107 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1108 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1109 } else {
1110 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1111 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1112 }
1113
1114 } else { // this is an FP divide/remainder, so we 'leak' some temp
1115 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1116 TmpF3=Tmp1;
1117 TmpF4=Tmp2;
1118 }
1119
1120 // we start by computing an approximate reciprocal (good to 9 bits?)
1121 // note, this instruction writes _both_ TmpF5 (answer) and tmpPR (predicate)
1122 // FIXME: or at least, it should!!
1123 BuildMI(BB, IA64::FRCPAS1FLOAT, 2, TmpF5).addReg(TmpF3).addReg(TmpF4);
1124 BuildMI(BB, IA64::FRCPAS1PREDICATE, 2, TmpPR).addReg(TmpF3).addReg(TmpF4);
1125
Duraid Madinabeeaab22005-03-31 12:31:11 +00001126 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1127 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1128 // TPCMPNE below
1129 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1130 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1131 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1132 }
1133
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001134 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1135 // precision, don't need this much for f32/i32)
1136 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1137 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1138 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1139 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1140 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1141 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1142 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1143 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1144 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1145 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1146 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1147 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1148 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1149 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1150 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1151 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001152
1153 // FIXME: this is unfortunate :(
1154 // the story is that the dest reg of the fnma above and the fma below
1155 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1156 // be the same register, or this code breaks if the first argument is
1157 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001158 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1159 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1160
Duraid Madina6e02e682005-04-04 05:05:52 +00001161 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1162 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1163 }
1164
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001165 if(!isFP) {
1166 // round to an integer
1167 if(isSigned)
1168 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1169 else
1170 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1171 } else {
1172 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1173 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1174 // we really do need the above FMOV? ;)
1175 }
1176
1177 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001178 if(isFP) { // extra worrying about div-by-zero
1179 unsigned bogoResult=MakeReg(MVT::f64);
1180
1181 // we do a 'conditional fmov' (of the correct result, depending
1182 // on how the frcpa predicate turned out)
1183 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1184 .addReg(TmpF12).addReg(TmpPR2);
1185 BuildMI(BB, IA64::CFMOV, 2, Result)
1186 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1187 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001188 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001189 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001190 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001191 } else { // this is a modulus
1192 if(!isFP) {
1193 // answer = q * (-b) + a
1194 unsigned ModulusResult = MakeReg(MVT::f64);
1195 unsigned TmpF = MakeReg(MVT::f64);
1196 unsigned TmpI = MakeReg(MVT::i64);
Duraid Madina6e02e682005-04-04 05:05:52 +00001197
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001198 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1199 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1200 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1201 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1202 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1203 } else { // FP modulus! The horror... the horror....
1204 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1205 }
1206 }
1207
1208 return Result;
1209 }
1210
1211 case ISD::ZERO_EXTEND_INREG: {
1212 Tmp1 = SelectExpr(N.getOperand(0));
1213 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1214 switch(MVN->getExtraValueType())
1215 {
1216 default:
1217 Node->dump();
1218 assert(0 && "don't know how to zero extend this type");
1219 break;
1220 case MVT::i8: Opc = IA64::ZXT1; break;
1221 case MVT::i16: Opc = IA64::ZXT2; break;
1222 case MVT::i32: Opc = IA64::ZXT4; break;
1223 }
1224 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1225 return Result;
1226 }
1227
1228 case ISD::SIGN_EXTEND_INREG: {
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1231 switch(MVN->getExtraValueType())
1232 {
1233 default:
1234 Node->dump();
1235 assert(0 && "don't know how to sign extend this type");
1236 break;
1237 case MVT::i8: Opc = IA64::SXT1; break;
1238 case MVT::i16: Opc = IA64::SXT2; break;
1239 case MVT::i32: Opc = IA64::SXT4; break;
1240 }
1241 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1242 return Result;
1243 }
1244
1245 case ISD::SETCC: {
1246 Tmp1 = SelectExpr(N.getOperand(0));
1247 Tmp2 = SelectExpr(N.getOperand(1));
1248 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1249 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1250 switch (SetCC->getCondition()) {
1251 default: assert(0 && "Unknown integer comparison!");
1252 case ISD::SETEQ:
1253 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1254 break;
1255 case ISD::SETGT:
1256 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1257 break;
1258 case ISD::SETGE:
1259 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1260 break;
1261 case ISD::SETLT:
1262 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1263 break;
1264 case ISD::SETLE:
1265 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1266 break;
1267 case ISD::SETNE:
1268 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1269 break;
1270 case ISD::SETULT:
1271 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1272 break;
1273 case ISD::SETUGT:
1274 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1275 break;
1276 case ISD::SETULE:
1277 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1278 break;
1279 case ISD::SETUGE:
1280 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1281 break;
1282 }
1283 }
1284 else { // if not integer, should be FP. FIXME: what about bools? ;)
1285 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1286 "error: SETCC should have had incoming f32 promoted to f64!\n");
1287 switch (SetCC->getCondition()) {
1288 default: assert(0 && "Unknown FP comparison!");
1289 case ISD::SETEQ:
1290 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1291 break;
1292 case ISD::SETGT:
1293 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1294 break;
1295 case ISD::SETGE:
1296 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1297 break;
1298 case ISD::SETLT:
1299 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1300 break;
1301 case ISD::SETLE:
1302 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1303 break;
1304 case ISD::SETNE:
1305 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1306 break;
1307 case ISD::SETULT:
1308 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 break;
1310 case ISD::SETUGT:
1311 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1312 break;
1313 case ISD::SETULE:
1314 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1315 break;
1316 case ISD::SETUGE:
1317 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1318 break;
1319 }
1320 }
1321 }
1322 else
1323 assert(0 && "this setcc not implemented yet");
1324
1325 return Result;
1326 }
1327
1328 case ISD::EXTLOAD:
1329 case ISD::ZEXTLOAD:
1330 case ISD::LOAD: {
1331 // Make sure we generate both values.
1332 if (Result != 1)
1333 ExprMap[N.getValue(1)] = 1; // Generate the token
1334 else
1335 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1336
1337 bool isBool=false;
1338
1339 if(opcode == ISD::LOAD) { // this is a LOAD
1340 switch (Node->getValueType(0)) {
1341 default: assert(0 && "Cannot load this type!");
1342 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1343 // FIXME: for now, we treat bool loads the same as i8 loads */
1344 case MVT::i8: Opc = IA64::LD1; break;
1345 case MVT::i16: Opc = IA64::LD2; break;
1346 case MVT::i32: Opc = IA64::LD4; break;
1347 case MVT::i64: Opc = IA64::LD8; break;
1348
1349 case MVT::f32: Opc = IA64::LDF4; break;
1350 case MVT::f64: Opc = IA64::LDF8; break;
1351 }
1352 } else { // this is an EXTLOAD or ZEXTLOAD
1353 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1354 switch (TypeBeingLoaded) {
1355 default: assert(0 && "Cannot extload/zextload this type!");
1356 // FIXME: bools?
1357 case MVT::i8: Opc = IA64::LD1; break;
1358 case MVT::i16: Opc = IA64::LD2; break;
1359 case MVT::i32: Opc = IA64::LD4; break;
1360 case MVT::f32: Opc = IA64::LDF4; break;
1361 }
1362 }
1363
1364 SDOperand Chain = N.getOperand(0);
1365 SDOperand Address = N.getOperand(1);
1366
1367 if(Address.getOpcode() == ISD::GlobalAddress) {
1368 Select(Chain);
1369 unsigned dummy = MakeReg(MVT::i64);
1370 unsigned dummy2 = MakeReg(MVT::i64);
1371 BuildMI(BB, IA64::ADD, 2, dummy)
1372 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1373 .addReg(IA64::r1);
1374 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1375 if(!isBool)
1376 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1377 else { // emit a little pseudocode to load a bool (stored in one byte)
1378 // into a predicate register
1379 assert(Opc==IA64::LD1 && "problem loading a bool");
1380 unsigned dummy3 = MakeReg(MVT::i64);
1381 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1382 // we compare to 0. true? 0. false? 1.
1383 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1384 }
1385 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1386 Select(Chain);
1387 IA64Lowering.restoreGP(BB);
1388 unsigned dummy = MakeReg(MVT::i64);
1389 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1390 .addReg(IA64::r1); // CPI+GP
1391 if(!isBool)
1392 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1393 else { // emit a little pseudocode to load a bool (stored in one byte)
1394 // into a predicate register
1395 assert(Opc==IA64::LD1 && "problem loading a bool");
1396 unsigned dummy3 = MakeReg(MVT::i64);
1397 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1398 // we compare to 0. true? 0. false? 1.
1399 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1400 }
1401 } else if(Address.getOpcode() == ISD::FrameIndex) {
1402 Select(Chain); // FIXME ? what about bools?
1403 unsigned dummy = MakeReg(MVT::i64);
1404 BuildMI(BB, IA64::MOV, 1, dummy)
1405 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1406 if(!isBool)
1407 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1408 else { // emit a little pseudocode to load a bool (stored in one byte)
1409 // into a predicate register
1410 assert(Opc==IA64::LD1 && "problem loading a bool");
1411 unsigned dummy3 = MakeReg(MVT::i64);
1412 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1413 // we compare to 0. true? 0. false? 1.
1414 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1415 }
1416 } else { // none of the above...
1417 Select(Chain);
1418 Tmp2 = SelectExpr(Address);
1419 if(!isBool)
1420 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1421 else { // emit a little pseudocode to load a bool (stored in one byte)
1422 // into a predicate register
1423 assert(Opc==IA64::LD1 && "problem loading a bool");
1424 unsigned dummy = MakeReg(MVT::i64);
1425 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1426 // we compare to 0. true? 0. false? 1.
1427 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1428 }
1429 }
1430
1431 return Result;
1432 }
1433
1434 case ISD::CopyFromReg: {
1435 if (Result == 1)
1436 Result = ExprMap[N.getValue(0)] =
1437 MakeReg(N.getValue(0).getValueType());
1438
1439 SDOperand Chain = N.getOperand(0);
1440
1441 Select(Chain);
1442 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1443
1444 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1445 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1446 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1447 // (r) Result =cmp.eq.unc(r0,r0)
1448 else
1449 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1450 return Result;
1451 }
1452
1453 case ISD::CALL: {
1454 Select(N.getOperand(0));
1455
1456 // The chain for this call is now lowered.
1457 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1458
1459 //grab the arguments
1460 std::vector<unsigned> argvregs;
1461
1462 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1463 argvregs.push_back(SelectExpr(N.getOperand(i)));
1464
1465 // see section 8.5.8 of "Itanium Software Conventions and
1466 // Runtime Architecture Guide to see some examples of what's going
1467 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1468 // while FP args get mapped to F8->F15 as needed)
1469
1470 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1471
1472 // in reg args
1473 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1474 {
1475 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1476 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1477 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1478 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1479
1480 switch(N.getOperand(i+2).getValueType())
1481 {
1482 default: // XXX do we need to support MVT::i1 here?
1483 Node->dump();
1484 N.getOperand(i).Val->dump();
1485 std::cerr << "Type for " << i << " is: " <<
1486 N.getOperand(i+2).getValueType() << std::endl;
1487 assert(0 && "Unknown value type for call");
1488 case MVT::i64:
1489 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1490 break;
1491 case MVT::f64:
1492 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1493 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001494 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001495 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1496 break;
1497 }
1498 }
1499
1500 //in mem args
1501 for (int i = 8, e = argvregs.size(); i < e; ++i)
1502 {
1503 unsigned tempAddr = MakeReg(MVT::i64);
1504
1505 switch(N.getOperand(i+2).getValueType()) {
1506 default:
1507 Node->dump();
1508 N.getOperand(i).Val->dump();
1509 std::cerr << "Type for " << i << " is: " <<
1510 N.getOperand(i+2).getValueType() << "\n";
1511 assert(0 && "Unknown value type for call");
1512 case MVT::i1: // FIXME?
1513 case MVT::i8:
1514 case MVT::i16:
1515 case MVT::i32:
1516 case MVT::i64:
1517 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1518 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1519 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1520 break;
1521 case MVT::f32:
1522 case MVT::f64:
1523 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1524 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1525 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1526 break;
1527 }
1528 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001529
1530 /* XXX we want to re-enable direct branches! crippling them now
1531 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001532 //build the right kind of call
1533 if (GlobalAddressSDNode *GASD =
1534 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1535 {
1536 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1537 IA64Lowering.restoreGP_SP_RP(BB);
1538 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001539 ^^^^^^^^^^^^^ we want this code one day XXX */
1540 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001541 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001542 { // FIXME : currently need this case for correctness, to avoid
1543 // "non-pic code with imm relocation against dynamic symbol" errors
1544 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001545 .addExternalSymbol(ESSDN->getSymbol(), true);
1546 IA64Lowering.restoreGP_SP_RP(BB);
1547 }
1548 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001549 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001550
1551 unsigned targetEntryPoint=MakeReg(MVT::i64);
1552 unsigned targetGPAddr=MakeReg(MVT::i64);
1553 unsigned currentGP=MakeReg(MVT::i64);
1554
1555 // b6 is a scratch branch register, we load the target entry point
1556 // from the base of the function descriptor
1557 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1558 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1559
1560 // save the current GP:
1561 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1562
1563 /* TODO: we need to make sure doing this never, ever loads a
1564 * bogus value into r1 (GP). */
1565 // load the target GP (which is at mem[functiondescriptor+8])
1566 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1567 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1568 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1569
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001570 // and then jump: (well, call)
1571 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001572 // and finally restore the old GP
1573 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1574 IA64Lowering.restoreSP_RP(BB);
1575 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001576
1577 switch (Node->getValueType(0)) {
1578 default: assert(0 && "Unknown value type for call result!");
1579 case MVT::Other: return 1;
1580 case MVT::i1:
1581 BuildMI(BB, IA64::CMPNE, 2, Result)
1582 .addReg(IA64::r8).addReg(IA64::r0);
1583 break;
1584 case MVT::i8:
1585 case MVT::i16:
1586 case MVT::i32:
1587 case MVT::i64:
1588 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1589 break;
1590 case MVT::f64:
1591 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1592 break;
1593 }
1594 return Result+N.ResNo;
1595 }
1596
1597 } // <- uhhh XXX
1598 return 0;
1599}
1600
1601void ISel::Select(SDOperand N) {
1602 unsigned Tmp1, Tmp2, Opc;
1603 unsigned opcode = N.getOpcode();
1604
Nate Begeman85fdeb22005-03-24 04:39:54 +00001605 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001606 return; // Already selected.
1607
1608 SDNode *Node = N.Val;
1609
1610 switch (Node->getOpcode()) {
1611 default:
1612 Node->dump(); std::cerr << "\n";
1613 assert(0 && "Node not handled yet!");
1614
1615 case ISD::EntryToken: return; // Noop
1616
1617 case ISD::TokenFactor: {
1618 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1619 Select(Node->getOperand(i));
1620 return;
1621 }
1622
1623 case ISD::CopyToReg: {
1624 Select(N.getOperand(0));
1625 Tmp1 = SelectExpr(N.getOperand(1));
1626 Tmp2 = cast<RegSDNode>(N)->getReg();
1627
1628 if (Tmp1 != Tmp2) {
1629 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1630 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1631 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1632 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1633 else
1634 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1635 // XXX is this the right way 'round? ;)
1636 }
1637 return;
1638 }
1639
1640 case ISD::RET: {
1641
1642 /* what the heck is going on here:
1643
1644<_sabre_> ret with two operands is obvious: chain and value
1645<camel_> yep
1646<_sabre_> ret with 3 values happens when 'expansion' occurs
1647<_sabre_> e.g. i64 gets split into 2x i32
1648<camel_> oh right
1649<_sabre_> you don't have this case on ia64
1650<camel_> yep
1651<_sabre_> so the two returned values go into EAX/EDX on ia32
1652<camel_> ahhh *memories*
1653<_sabre_> :)
1654<camel_> ok, thanks :)
1655<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1656<_sabre_> this is the first operand always
1657<_sabre_> these operand often define chains, they are the last operand
1658<_sabre_> they are printed as 'ch' if you do DAG.dump()
1659 */
1660
1661 switch (N.getNumOperands()) {
1662 default:
1663 assert(0 && "Unknown return instruction!");
1664 case 2:
1665 Select(N.getOperand(0));
1666 Tmp1 = SelectExpr(N.getOperand(1));
1667 switch (N.getOperand(1).getValueType()) {
1668 default: assert(0 && "All other types should have been promoted!!");
1669 // FIXME: do I need to add support for bools here?
1670 // (return '0' or '1' r8, basically...)
1671 case MVT::i64:
1672 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1673 break;
1674 case MVT::f64:
1675 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1676 }
1677 break;
1678 case 1:
1679 Select(N.getOperand(0));
1680 break;
1681 }
1682 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1683 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1684 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1685 return;
1686 }
1687
1688 case ISD::BR: {
1689 Select(N.getOperand(0));
1690 MachineBasicBlock *Dest =
1691 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1692 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1693 // XXX HACK! we do _not_ need long branches all the time
1694 return;
1695 }
1696
1697 case ISD::ImplicitDef: {
1698 Select(N.getOperand(0));
1699 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1700 return;
1701 }
1702
1703 case ISD::BRCOND: {
1704 MachineBasicBlock *Dest =
1705 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1706
1707 Select(N.getOperand(0));
1708 Tmp1 = SelectExpr(N.getOperand(1));
1709 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1710 // XXX HACK! we do _not_ need long branches all the time
1711 return;
1712 }
1713
1714 case ISD::EXTLOAD:
1715 case ISD::ZEXTLOAD:
1716 case ISD::SEXTLOAD:
1717 case ISD::LOAD:
1718 case ISD::CALL:
1719 case ISD::CopyFromReg:
1720 case ISD::DYNAMIC_STACKALLOC:
1721 SelectExpr(N);
1722 return;
1723
1724 case ISD::TRUNCSTORE:
1725 case ISD::STORE: {
1726 Select(N.getOperand(0));
1727 Tmp1 = SelectExpr(N.getOperand(1)); // value
1728
1729 bool isBool=false;
1730
1731 if(opcode == ISD::STORE) {
1732 switch (N.getOperand(1).getValueType()) {
1733 default: assert(0 && "Cannot store this type!");
1734 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1735 // FIXME?: for now, we treat bool loads the same as i8 stores */
1736 case MVT::i8: Opc = IA64::ST1; break;
1737 case MVT::i16: Opc = IA64::ST2; break;
1738 case MVT::i32: Opc = IA64::ST4; break;
1739 case MVT::i64: Opc = IA64::ST8; break;
1740
1741 case MVT::f32: Opc = IA64::STF4; break;
1742 case MVT::f64: Opc = IA64::STF8; break;
1743 }
1744 } else { // truncstore
1745 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1746 default: assert(0 && "unknown type in truncstore");
1747 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1748 //FIXME: DAG does not promote this load?
1749 case MVT::i8: Opc = IA64::ST1; break;
1750 case MVT::i16: Opc = IA64::ST2; break;
1751 case MVT::i32: Opc = IA64::ST4; break;
1752 case MVT::f32: Opc = IA64::STF4; break;
1753 }
1754 }
1755
1756 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1757 unsigned dummy = MakeReg(MVT::i64);
1758 unsigned dummy2 = MakeReg(MVT::i64);
1759 BuildMI(BB, IA64::ADD, 2, dummy)
1760 .addGlobalAddress(cast<GlobalAddressSDNode>
1761 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1762 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1763
1764 if(!isBool)
1765 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1766 else { // we are storing a bool, so emit a little pseudocode
1767 // to store a predicate register as one byte
1768 assert(Opc==IA64::ST1);
1769 unsigned dummy3 = MakeReg(MVT::i64);
1770 unsigned dummy4 = MakeReg(MVT::i64);
1771 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1772 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1773 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1774 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1775 }
1776 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1777
1778 // FIXME? (what about bools?)
1779
1780 unsigned dummy = MakeReg(MVT::i64);
1781 BuildMI(BB, IA64::MOV, 1, dummy)
1782 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1783 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1784 } else { // otherwise
1785 Tmp2 = SelectExpr(N.getOperand(2)); //address
1786 if(!isBool)
1787 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1788 else { // we are storing a bool, so emit a little pseudocode
1789 // to store a predicate register as one byte
1790 assert(Opc==IA64::ST1);
1791 unsigned dummy3 = MakeReg(MVT::i64);
1792 unsigned dummy4 = MakeReg(MVT::i64);
1793 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
1794 BuildMI(BB, IA64::CADDIMM22, 3, dummy4)
1795 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1796 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1797 }
1798 }
1799 return;
1800 }
1801
1802 case ISD::ADJCALLSTACKDOWN:
1803 case ISD::ADJCALLSTACKUP: {
1804 Select(N.getOperand(0));
1805 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1806
1807 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1808 IA64::ADJUSTCALLSTACKUP;
1809 BuildMI(BB, Opc, 1).addImm(Tmp1);
1810 return;
1811 }
1812
1813 return;
1814 }
1815 assert(0 && "GAME OVER. INSERT COIN?");
1816}
1817
1818
1819/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1820/// into a machine code representation using pattern matching and a machine
1821/// description file.
1822///
1823FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1824 return new ISel(TM);
1825}
1826
1827